Question

I am using LLVM IR to generate some structure data.

For example:

struct mystruct {int a:1; int b:2; unsigned long c} = {{1, 2, 3}};

I read the document and tried the demo, it seems the API requires user to handle padding and value combination by themselves.

Is there any way to create the struct more simply like this:

fields.push_back(Type::getIntNTy(context, 1));
fields.push_back(Type::getIntNTy(context, 2));
fields.push_back(Type::getIntNTy(context, 64));
structTy.setBody(fields, false /* isPacked*/);

It's really tough to do layout work and it's very likely to cause bugs. Combining the values of the initializer is also tough.

As my understanding, if I tell llvm API that the struct is not packed, it should handle padding automatically, right?

Any comments?

Was it helpful?

Solution

The padding is dictated by C standard and ABI of your target system. LLVM itself has no idea about these rules (since they are for C, not for LLVM IR).

You are responsible to handle all the padding weirdness and bitfield layout, unfortunately. Usually, this is a work of C/C++ frontend (e.g. clang).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top