[LLVMdev] simulating c style unions in LLVM
me22
me22.ca at gmail.com
Mon Jun 29 23:32:51 PDT 2009
2009/6/29 Carter Cheng <carter_cheng at yahoo.com>:
>
> I am trying to create a boxed tagged datatype for a language where it is necessary to resolve the type at runtime. If I were writing an interpreter in C- it would most likely look something like this-
>
> Is there a standard way for constructing a type like this in LLVM?
>
Well, you can always ask bitter melon, who translates this:
struct foo
{
unsigned tag;
union { long Int; double Float; } data;
};
void bar(struct foo *x) {
x->data.Int = x->tag;
}
into this:
%struct.anon = type { double }
%struct.foo = type { i32, %struct.anon }
define void @bar(%struct.foo* nocapture %x) nounwind {
entry:
%0 = getelementptr %struct.foo* %x, i32 0, i32 0 ; <i32*> [#uses=1]
%1 = load i32* %0, align 4 ; <i32> [#uses=1]
%2 = getelementptr %struct.foo* %x, i32 0, i32 1 ; <%struct.anon*> [#uses=1]
%3 = bitcast %struct.anon* %2 to i32* ; <i32*> [#uses=1]
store i32 %1, i32* %3, align 4
ret void
}
So essentially the union just turns into bitcasts.
I know there was some discussion about adding a first-order union to
LLVM a bit back, but IIRC disagreement over semantics prevented it
from going anywhere.
More information about the llvm-dev
mailing list