Vyacheslav Akhmechet
2005-Apr-21 13:31 UTC
[LLVMdev] Using LLVM for a dynamically typed language
I recently ran into the following problem. I'm prototyping a compiler for a dynamically typed language in which functions are first class objects. Assuming I have something like this: if(rand() > 5) i = define(x, y, z) { return x + y + z; } else i = define(x, y) { return x + y; } At this point I cannot know the type of 'i' at compile time. At runtime 'i' is a structure that contains a type and a function pointer. What I can't figure out is how to cast my llvm function pointer to an appropriate function type. I cannot know until runtime what the type will be. Naturally I can add runtime code that will do all appropriate checks (number of parameters, etc.) but how do I do the actual cast? I hope I'm being clear. Thanks, - Slava.
On Thu, 2005-21-04 at 09:31 -0400, Vyacheslav Akhmechet wrote:> At this point I cannot know the type of 'i' at compile time. At > runtime 'i' is a structure that contains a type and a function > pointer. What I can't figure out is how to cast my llvm function > pointer to an appropriate function type. I cannot know until runtime > what the type will be. Naturally I can add runtime code that will do > all appropriate checks (number of parameters, etc.) but how do I do > the actual cast?I'm not sure that I understand. LLVM permits you to cast any pointer to any other pointer. If you are using LLVM to generate your code, this basically has 3 steps: 1. Get a Type* for the function type you want to cast to. 2. Use a cast instruction to actually perform the type cast. In the code I'm currently working on, I do the following to cast a function pointer from one type to another. Hopefully the line wrapping won't totally destroy this. std::vector<const Type*> paramTypes; // First parameter: a void pointer paramTypes.push_back( PointerType::get( Type::getPrimitiveType( Type::SByteTyID ) ) ); FunctionType* pthreadFunctionType = FunctionType::get( PointerType::get( Type::getPrimitiveType( Type::SByteTyID ) ), paramTypes, false ); assert( pthreadFunctionType != NULL ); // Cast the function to the pthread function type Value* castedFunction = new CastInst( function, PointerType::get( pthreadFunctionType ), "", &terminator ); I hope this helps, Evan Jones
Vyacheslav Akhmechet
2005-Apr-21 14:28 UTC
[LLVMdev] Using LLVM for a dynamically typed language
Evan, The problem is that I do not know the type of a target function at compile time. If you consider my code example, I don't know the type of 'i' until runtime (in fact, I can't even know a possible range of types 'i' may assume). Thanks, - Slava. On 4/21/05, Evan Jones <ejones at uwaterloo.ca> wrote:> On Thu, 2005-21-04 at 09:31 -0400, Vyacheslav Akhmechet wrote: > > At this point I cannot know the type of 'i' at compile time. At > > runtime 'i' is a structure that contains a type and a function > > pointer. What I can't figure out is how to cast my llvm function > > pointer to an appropriate function type. I cannot know until runtime > > what the type will be. Naturally I can add runtime code that will do > > all appropriate checks (number of parameters, etc.) but how do I do > > the actual cast? > > I'm not sure that I understand. LLVM permits you to cast any pointer to > any other pointer. If you are using LLVM to generate your code, this > basically has 3 steps: > > 1. Get a Type* for the function type you want to cast to. > 2. Use a cast instruction to actually perform the type cast. > > In the code I'm currently working on, I do the following to cast a > function pointer from one type to another. Hopefully the line wrapping > won't totally destroy this. > > std::vector<const Type*> paramTypes; > > // First parameter: a void pointer > paramTypes.push_back( PointerType::get( Type::getPrimitiveType( Type::SByteTyID ) ) ); > > FunctionType* pthreadFunctionType = FunctionType::get( > PointerType::get( Type::getPrimitiveType( Type::SByteTyID ) ), > paramTypes, false ); > assert( pthreadFunctionType != NULL ); > > // Cast the function to the pthread function type > Value* castedFunction = new CastInst( function, > PointerType::get( pthreadFunctionType ), "", &terminator ); > > I hope this helps, > > Evan Jones > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://mail.cs.uiuc.edu/mailman/listinfo/llvmdev >
Apparently Analagous Threads
- [LLVMdev] Using LLVM for a dynamically typed language
- [LLVMdev] Using LLVM for a dynamically typed language
- [LLVMdev] Newbie question: LLVM IR, printf, and varargs
- [LLVMdev] Newbie question: LLVM IR, printf, and varargs
- [LLVMdev] Using LLVM for a dynamically typed language