[LLVMdev] Using LLVM for a dynamically typed language
Evan Jones
ejones at uwaterloo.ca
Thu Apr 21 06:54:05 PDT 2005
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
More information about the llvm-dev
mailing list