LLVM.如何根据结构名称访问结构域? [英] LLVM. How to access to struct fields based on their names?
问题描述
我在C ++中几乎没有示例代码:
I have little example code in C++:
struct RecordTest
{
int value1;
int value2;
};
void test()
{
RecordTest rt;
rt.value1 = 15;
rt.value2 = 75;
}
和LLVM 3.4 IR:
and LLVM 3.4 IR for it:
%struct.RecordTest = type { i32, i32 }
; Function Attrs: nounwind
define void @_Z4testv() #0 {
entry:
%rt = alloca %struct.RecordTest, align 4
%value1 = getelementptr inbounds %struct.RecordTest* %rt, i32 0, i32 0
store i32 15, i32* %value1, align 4
%value2 = getelementptr inbounds %struct.RecordTest* %rt, i32 0, i32 1
store i32 75, i32* %value2, align 4
ret void
}
和一个很简单的问题:如何访问RecordTest
字段(在解析.cpp时),而没有它们的索引,而仅使用名称(value1
和value2
)?
and a pretty easy question: How can I access to RecordTest
fields (when I parsing .cpp), without their indexes, with only names (value1
and value2
)?
我只知道一种方法(来自llc -march=cpp
)-具有索引:
I know only one way (from llc -march=cpp
) - with indexes:
AllocaInst* ptr_rt = new AllocaInst(StructTy_struct_RecordTest, "rt", label_entry);
ptr_rt->setAlignment(4);
std::vector<Value*> ptr_value1_indices;
ptr_value1_indices.push_back(const_int32_6);
ptr_value1_indices.push_back(const_int32_6);
Instruction* ptr_value1 = GetElementPtrInst::Create(ptr_rt, ptr_value1_indices, "value1", label_entry);
StoreInst* void_9 = new StoreInst(const_int32_7, ptr_value1, false, label_entry);
void_9->setAlignment(4);
std::vector<Value*> ptr_value2_indices;
ptr_value2_indices.push_back(const_int32_6);
ptr_value2_indices.push_back(const_int32_5);
Instruction* ptr_value2 = GetElementPtrInst::Create(ptr_rt, ptr_value2_indices, "value2", label_entry);
StoreInst* void_10 = new StoreInst(const_int32_8, ptr_value2, false, label_entry);
void_10->setAlignment(4);
因此,如果我不知道字段的索引(上面的代码中的const_int32_5
和const_int32_6
),我可以从C ++转换为LLVM IR吗?
So, can I translate from C++ to LLVM IR, if I don't know the indexes of the fields (const_int32_5
and const_int32_6
in code above) ?
UPD ===============================
UPD================================
因此,我们无法访问字段名称.如果我们需要它(如果解析.cpp,我们也会这样做), 我们可以这样写:
So, we can't access to field names. And if we need it (and we do, if we parse .cpp), we can write something like this:
// It can be some kind of singletone
static std::map<std::string, std::vector<std::string>> mymap;
// Some function, where we first time meet RecordTest
std::vector<std::string> fieldNames;
fieldNames.push_back("value1");
fieldNames.push_back("value2");
mymap["RecordTest"] = fieldNames;
// Some function, where we need to access to RecordTest field
std::vector<std::string> fieldNamesAgain = mymap.find("RecordTest")->second;
std::string fieldName = "value1";
int idxValue1 = -1;
for (int i = 0, e = fieldNamesAgain.size(); i < e; i++) // little ugly search
{
if (fieldName == fieldNamesAgain[i])
{
// we get field index, and now we can build code
// as in example above (llc -march=cpp)
idxValue1 = i;
break;
}
}
这是对的吗?
推荐答案
您不能按名称(仅按索引)访问结构的字段.当您使用Clang编译时,此信息通常不存在.
You cannot access the fields of the struct by name, only by index. This information is just normally not there when you compile with Clang.
对此有一个例外,那就是您编译了调试信息.在这种情况下,您将拥有有关类型的大量数据;具体来说,您将获得字段的顺序,以及每个字段的元数据条目,其中包含该字段的名称(以及其他有用的内容,例如从类型开头的偏移量).
There is one exception to this, and this is if you compiled with debug information. In that case, you'll have ample data about the type; specifically, you'll get the order of the fields, along with a metadata entry for each field which contains its name (and other useful stuff, such as its offset from the beginning of the type).
在源代码级调试指南中了解有关此内容的详细信息-特别是,请参见关于结构编码的这一部分,其示例非常好.
Read more about this on the Source Level Debugging guide - and particularly, see this section about struct encoding, with its very nice example.
查看 DebugInfo.h 中的类,以帮助查询调试信息,尽管我认为您还是必须手动进行一些挖掘.
Take a look at DebugInfo.h for classes to help on querying debug info, though I think you're going to have to do some manually digging anyway.
这篇关于LLVM.如何根据结构名称访问结构域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!