简体   繁体   English

可以将 llvm::FunctionType 转换为 C/C++ 原始 function 指针吗?

[英]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?我们可以将llvm::FunctionType的 object 转换为 C/C++ 风格的 function 原始指针吗?

A example for a C/C++-styled function raw pointer: uint64_t (*funPtr)(uint64_t* args); C/C++ 风格的 function 原始指针示例: uint64_t (*funPtr)(uint64_t* args);

llvm::Function is represented as an abstract syntax tree. llvm::Function表示为抽象语法树。 You can't call it, just like you can't call arrays, lists, or any other data structures.你不能调用它,就像你不能调用 arrays、列表或任何其他数据结构一样。

Instead, you need to leverage LLVM's ExecutionEngine functionality to be able to call llvm::Function s.相反,您需要利用 LLVM 的ExecutionEngine功能来调用llvm::Function s。 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.在内部,引擎会将其编译为本机可执行代码并返回一个void* (我不记得 API 的详细信息,但类似的东西)到该代码。 Then you will be able to cast this pointer to a function pointer and finally use it to call the function.然后,您将能够将此指针转换为 function 指针,最后使用它来调用 function。

I'm not 100% sure with regards to the capabilities of llvm , but this text from n4659 may be helpful:关于llvm的功能,我不是 100% 确定,但是来自n4659的这段文字可能会有所帮助:

Converting a function pointer to an object pointer type or vice versa is conditionally-supported.有条件地支持将 function 指针转换为 object 指针类型或反之亦然。 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.这种转换的含义是实现定义的,除非实现支持双向转换,将一种类型的纯右值转换为另一种类型并返回,可能具有不同的 cv 限定,应产生原始指针值。

I'm pretty sure that function pointers aren't like regular pointers, and so these conversions are problematic.我很确定function 指针不像常规指针,所以这些转换是有问题的。 I think they discuss the issue at length in this stack overflow question .我认为他们在这个堆栈溢出问题中详细讨论了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM