[LLVMdev] Self-referential function pointer

Jay Foad jay.foad at gmail.com
Fri Jul 6 05:28:28 PDT 2012


On 6 July 2012 12:44, Cameron McInally <cameron.mcinally at nyu.edu> wrote:
> Hey guys,
>
> I could use some advice on a special case of a function pointer as a formal
> argument. I would like the function pointer type to contain the actual
> signature of the function, i.e. not a pointer to var args function. This
> becomes an issue when I have a function which can take a pointer to itself
> as an argument... our terminology for this is "a recursive procedure". That
> is, of course, in a context where C-like recursion is not intrinsic to the
> language.
>
> With LLVM 2.9, the solution was to create an OpaqueType when creating the
> function type signature, use the OpaqueType as the function pointer argument
> type, and then finish the function type signature later. No problem.
>
> If I'm not mistaken, only StructType can be self-referential in LLVM 3.0 and
> beyond. So, is it not possible to have a self-referencing FunctionType,
> short of making the function pointer formals var args? Has anyone developed
> a solution for similar behavior?

C lets you pass in a struct whose single element is the pointer to the function:

struct s;
typedef void functype(struct s);
struct s {
  functype *funcptr;
};

void myfunc(struct s s) {
}

int main() {
  struct s s = { &myfunc };
  myfunc(s);
}

... but I'm not sure if LLVM's type system will let you do the moral
equivalent of this; it might not let you construct the type "functype"
when "struct s" is still opaque.

Jay.



More information about the llvm-dev mailing list