Self-referential function pointer

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Self-referential function pointer

Cameron McInally
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?

Ty,
Cameron

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Self-referential function pointer

Duncan Sands
Hi Cameron,

> 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?

no it's not possible, just use i8* or some other such artificial marker for the
parameter type.

Ciao, Duncan.
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Self-referential function pointer

Jay Foad-2
In reply to this post by Cameron McInally
On 6 July 2012 12:44, Cameron McInally <[hidden email]> 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.
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev