Frage

In C++/C you can do this:

unsigned char A[12];
unsigned int *B;
int *C;
B = malloc(sizeof(unsigned int));
C = malloc(2*sizeof(int));
A[0] = *B;
A[4] = *C;
//Then go on to access A byte by byte.

I was wondering if this was possible in LLVM-IR, or would it immediately complain of a problem with types. Was about to dive into this, but thought I would see if anyone has tried this particular example. Would I GEP A's 0th location as a i8* and then B and C's as a i32*. I'm a bit confused as how to proceed, if this is at all possible.

Thanks ahead of time.

UPDATE:

Ok, if I instead added initialization for *B and C[0], C[1], it would the answer change for LLVM-IR /C / C++?

War es hilfreich?

Lösung

LLVM has the bitcast instruction which is often used to convert one type of pointer to another type of pointer - e.g., i32* to i8*.

So for example, if you want to access the 3rd byte of a 4-byte number, doing the following is perfectly legitimate:

%bytes = bitcast i32* %num to i8*
%third_byte = getelementptr i8* %bytes, i32 2

Just keep in mind the endianess when you do stuff like that.

And yes, you can use this technique to obtain pointers to specific locations in an array and store and load values from there, enabling you to do duplicate your entire example.

Andere Tipps

No. This is not possible in C/C++ too. You should not assign an uninitialized variable to another variable. It invokes undefined behavior. *B and *c are uninitialized.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top