[LLVMdev] Address of labels = 1 with llvm-g++
Duncan Sands
baldrick at free.fr
Sat Apr 19 01:37:27 PDT 2008
Hi,
> I'm trying to put the address of a label in a register by passing in
> &&label to an inline asm call, but noticed this reduced case also has
> problems in llvm-g++:
>
> int main() {
> label:
> return (int)&&label;
> }
>
> The assembly code generated by g++ -S:
> movl $.L2, %eax ; where L2 is the label just inside main
>
> Assembly code from llvm-g++ -S:
> movl $1, %eax
>
> ll code with llvm-g++ -emit-llvm:
> ret i32 1
the gcc documentation ("5.3 Labels as Values") says:
"You may not use this mechanism to jump to code in a different function."
The value you get by taking the address of a label is a pointer that
only has meaning inside the function where that label lives. It is
represented in a way that only has meaning inside that function - in
your example by the value 1. I can see that you are expecting it to
be a pointer to the memory location where the instruction following
the label lives. That is a possible implementation but not the one
used by llvm-gcc. I know that the llvm-gcc implementation is less
flexible than the one you had in mind. The reason llvm-gcc does it
this way is that it doesn't require adding special concepts to LLVM's
internal representation in order to handle computed gotos - it can
be implemented using ordinary constructions like switch statements.
As for using labels in asm, I don't know anything about that, sorry.
Best wishes,
Duncan.
More information about the llvm-dev
mailing list