JGL
JGL

Reputation: 158

Possible convert a llvm::FunctionType to C/C++ raw function pointer?

Can we convert llvm::FunctionType's object into a C/C++-styled function raw pointer?

A example for a C/C++-styled function raw pointer: uint64_t (*funPtr)(uint64_t* args);

Upvotes: 1

Views: 170

Answers (2)

arrowd
arrowd

Reputation: 34391

llvm::Function is represented as an abstract syntax tree. You can't call it, just like you can't call arrays, lists, or any other data structures.

Instead, you need to leverage LLVM's ExecutionEngine functionality to be able to call llvm::Functions. Internally, the engine will compile it into native executable code and return a void* (I don't remember the API details, but something like that) to that code. Then you will be able to cast this pointer to a function pointer and finally use it to call the function.

Upvotes: 2

Nathan Chappell
Nathan Chappell

Reputation: 2436

I'm not 100% sure with regards to the capabilities of llvm, but this text from n4659 may be helpful:

Converting a function pointer to an object pointer type or vice versa is conditionally-supported. The meaning of such a conversion is implementation-defined, except that if an implementation supports conversions in both directions, converting a prvalue of one type to the other type and back, possibly with different cv-qualification, shall yield the original pointer value.

I'm pretty sure that function pointers aren't like regular pointers, and so these conversions are problematic. I think they discuss the issue at length in this stack overflow question.

Upvotes: 1

Related Questions