[LLVMdev] Using LLVM for a dynamically typed language

Chris Lattner sabre at nondot.org
Thu Apr 21 08:58:02 PDT 2005


On Thu, 21 Apr 2005, Evan Jones wrote:
> I don't think I properly understand what the issue is here. For my
> perspective, if you can figure out the type of 'i' at runtime, you
> either have to:
>
> a) Make all functions the same type. For example, make them all return
> void, take a vector of parameters as the first argument, and a vector
> for return values as the second argument.
>
> b) Call the appropriate LLVM functions to emit the code for the cast at
> runtime.
>
> I don't see how this is a specific challenge with LLVM. It seems to me

I agree with Evan.   To be more specific, for your initial example:

if(rand() > 5)
   i = define(x, y, z) { return x + y + z; }
else
   i = define(x, y) { return x + y; }



As you've noticed here, the result of a "define" has to have the same LLVM 
type.  I suggest compiling this to the equivalent of this C code:

void anon1(int *Result, void *Args) {
   RealArgs = (sometype*)Args;
   *Result = RealArgs->X + RealArgs->Y + RealArgs->Z;
}

void anon2(int *Result, void *Args) {
   RealArgs = (sometype2*)Args;
   *Result = RealArgs->X + RealArgs->Y;
}

Of course, the result pointer would want to be generic somehow, but I just 
made it int* to simplify the issue.  Despite this, "i" can now just be a 
function pointer.  If you have closures to deal with, you make each 
function a pair of function pointer and environment pointer, and pass them 
around together.

-Chris

-- 
http://nondot.org/sabre/
http://llvm.cs.uiuc.edu/




More information about the llvm-dev mailing list