I'm using LLVM api in order to parse bitcode files. I have the following snippet and I'm using this command to generate the bitcode $CC -emit-llvm -c -g source.c
where CC is set to the clang
path.
#include <stdio.h>
struct Point {
int a;
int b;
};
int func_0(struct Point p, int x) {
return 0;
}
The TypeID is supposed to have a numeric value, based on the type of the parameter. However, both for the integer x
and the struct Point
I obtain the value of 10
which is referred as a TokenTyID
. So, I decided to use the functions isIntegerTy()
and isStructTy()
, respectively, to see if at least in this case, I obtain the right result. This solution works for the integer parameter x
, but not for the struct. How can I correctly identify structs and read their fields?
Just to completeness, to parse the bitcode I use this code:
using namespace llvm;
int main(int argc, char** argv) {
LLVMContext context;
OwningPtr<MemoryBuffer> mb;
MemoryBuffer::getFile(FileName, mb);
Module *m = ParseBitcodeFile(mb.get(), context);
for (Module::const_iterator i = m->getFunctionList().begin(), e = m->getFunctionList().end(); i != e; ++i) {
if (i->isDeclaration() || i->getName().str() == "main")
continue;
std::cout << i->getName().str() << std::endl;
Type* ret_type = i->getReturnType();
std::cout << "\t(ret) " << ret_type->getTypeID() << std::endl;
Function::const_arg_iterator ai;
Function::const_arg_iterator ae;
for (ai = i->arg_begin(), ae = i->arg_end(); ai != ae; ++ai) {
Type* t = ai->getType();
std::cout << "\t" << ai->getName().str() << " " << t->getTypeID()
<< "(" << t->getFunctionNumParams() << ")"
<< " is struct? " << (t->isStructTy() ? "Y" : "N")
<< " is int? " << (t->isIntegerTy() ? "Y" : "N")
<< "\n";
}
}
return 0;
}
I read this post Why does Clang coerce struct parameters to ints about the translation performed by clang with the structs and I'm pretty sure that is my same problem.