我想创建一个以下类型,
void (i8*)*
我尝试使用Type类来创建上述类型,但是我没有找到任何直接的方法来做同样的事情。
有人请向我建议一种创建上述类型的方法。
提前致谢。
最佳答案
如果您的意思是i8**
(指向i8
的指针的指针),则:
// This creates the i8* type
PointerType* PointerTy = PointerType::get(IntegerType::get(mod->getContext(), 8), 0);
// This creates the i8** type
PointerType* PointerPtrTy = PointerType::get(PointerTy, 0);
如果您需要一个指向不返回任何内容并采用
i8*
的函数的指针,则:// This creates the i8* type
PointerType* PointerTy = PointerType::get(IntegerType::get(mod->getContext(), 8), 0);
// Create a function type. Its argument types are passed as a vector
std::vector<Type*>FuncTy_args;
FuncTy_args.push_back(PointerTy); // one argument: char*
FunctionType* FuncTy = FunctionType::get(
/*Result=*/Type::getVoidTy(mod->getContext()), // returning void
/*Params=*/FuncTy_args, // taking those args
/*isVarArg=*/false);
// Finally this is the pointer to the function type described above
PointerType* PtrToFuncTy = PointerType::get(FuncTy, 0);
一个更通用的答案是:您可以使用LLVM C++ API后端生成创建任何类型的IR所需的C++代码。可以通过在线LLVM演示http://llvm.org/demo/方便地完成此操作-这就是我为该答案生成代码的方式。
关于llvm - 在LLVM中创建新类型(尤其是指向函数类型的指针),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9434602/