[LLVMdev] "Bound Methods" in LLVM Bytecode

Evan Jones ejones at uwaterloo.ca
Sat Oct 29 07:20:05 PDT 2005

On Oct 29, 2005, at 1:04, Chris Lattner wrote:
>> The question is, how can I implement this efficiently, ideally using 
>> LLVM?
> Okay.  One simple option would be to insert code like this:
> if (isamethod(functionObject))
>   functionObject->functionPointer(a)
> else
>   functionObject->functionPointer()

Ah yes, the good old fashioned simple approach. The only change is that 
by the time I get to the function call, I may no longer have reference 
to the object (in the compiler), so I would have to stuff that into the 
bound method object itself.

> Sure, you can do this.  Another simple option would be to just make 
> every "function" take a first pointer argument which they ignore.  
> This would allow the caller to always pass a this pointer without 
> knowing anything about the callee.

Ah, of course! This is probably the best way to do it, since it is so 
simple. The "FunctionObject" type would contain not only a function 
pointer, but also a "this" pointer. For normal functions, "this" would 
be NULL. Why didn't I think of that, since I was halfway to that 
solution already? That would change the call implementation to the 

functionObject->functionPointer( functionObject->thisPointer )

>> Will it work if I just create a char array and copy in the 
>> appropriate native code for the current platform?
> Hrm, sometimes, sometimes not.  Code is not always relocatable like 
> that, it sounds dangerous.

Ah, also a good point. A copying garbage collector, for example, would 
definitely make things more complicated.

Thanks for your help! I was definitely thinking the wrong way.

Evan Jones

Evan Jones

More information about the llvm-dev mailing list