在LLVM中创建包含指向自身指针的结构
我目前正在使用LLVM构建JIT。有一些C在LLVM中创建包含指向自身指针的结构,llvm,llvm-c++-api,Llvm,Llvm C++ Api,我目前正在使用LLVM构建JIT。有一些Cstructs,我希望能够在我的JIT的IR中使用。其中一个具有以下布局: struct myStruct { int depth; myStruct* parent; } 当使用clang编译并使用-S-emit llvm时,我得到了以下结果,这似乎是绝对合理的: type myStruct = { i32, myStruct* } 好的。现在,如果我想使用LLVMAPI做同样的事情,我不太确定应该如何做。以下(预期)不起作用:
structs
,我希望能够在我的JIT的IR中使用。其中一个具有以下布局:
struct myStruct {
int depth;
myStruct* parent;
}
当使用clang
编译并使用-S-emit llvm
时,我得到了以下结果,这似乎是绝对合理的:
type myStruct = { i32, myStruct* }
好的。现在,如果我想使用LLVMAPI做同样的事情,我不太确定应该如何做。以下(预期)不起作用:
auto intType = IntegerType::get(context, 32); // 32 bits integer
Type* myStructPtrType = nullptr; // Pointer to myStruct
// The following crashes because myStructPtrType is null:
auto myStructType = StructType::create(context, { intType, myStructPtrType }, "myStruct"); // myStruct
myStructPtrType = PointerType::get(myStructType, 0); // Initialise the pointer type now
我真的不知道如何在这里继续。
欢迎提出任何建议。我能够回答这个问题,谢谢@arnt的评论。以防任何人有相同的目标/问题。其思想是首先创建一个不透明类型,然后获取指向该不透明类型的指针类型,然后使用
setBody
设置聚合体(这是解决方案的关键)
下面是一些代码:
auto intType = IntegerType::get(context, 32); // 32 bits integer
auto myStructType = StructType::create(context, "myStruct"); // Create opaque type
auto myStructPtrType = PointerType::get(myStructType, 0); // Initialise the pointer type now
myStructType->setBody({ intType, myStructPtrType }, /* packed */ false); // Set the body of the aggregate
创建一个不透明(命名)结构,创建字段类型的数组/向量,其中可能包含指向该结构类型本身的指针,然后调用为不透明结构类型定义字段。非常感谢!这正是我想要的。