开发者

LLVM: Passing a pointer to a struct, which holds a pointer to a function, to a JIT function

开发者 https://www.devze.com 2023-01-02 03:32 出处:网络
I have an LLVM (version 2.7) module with a function that takes a pointer to a struct. That struct contains a function pointer to a C++ function. The module function is going to be JIT-compiled, and I

I have an LLVM (version 2.7) module with a function that takes a pointer to a struct. That struct contains a function pointer to a C++ function. The module function is going to be JIT-compiled, and I need to build that struct in C++ using the LLVM API. I can't seem get the pointer to the function as an LLVM value, let alone pass a pointer to the ConstantStruct that I can't build.

I'm not sure if I'm even on the track, but this is what I have so far:

void print(char*);

vector<Constant*> functions;
functions.push_back(ConstantExpr::getIntToPtr(
    ConstantInt::get(Type::getInt32Ty(context), (int)print),
    /* function pointer type here, FunctionType::get(...) doesn't seem to work */
));
ConstantStruct* struct = cast<ConstantStruct>(ConstantStruct::get(
    cas开发者_如何学Got<StructType>(m->getTypeByName("printer")),
    functions
));

Function* main = m->getFunction("main");
vector<GenericValue> args;
args[0].PointerVal = /* not sure what goes here */
ee->runFunction(main, args);


Actually, nevermind. I wouldn't use the LLVM API, just pass it a C++ struct that matches the LLVM struct type's layout. Ignore the first bit of that code and set args[0].PointerVal to a pointer to that struct.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号