This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
asm(...) and how to properly align jump labels
- From: Denys Duchier <duchier at ps dot uni-sb dot de>
- To: gcc at gcc dot gnu dot org
- Date: Fri, 28 Nov 2003 01:18:48 +0100
- Subject: asm(...) and how to properly align jump labels
My application is a VM (for a programming language) executing
bytecodes using the good old threaded code technique: i.e. each
bytecode is actually an address to jump to.
There is a very useful trick for getting an O(1) reverse mapping from
bytecodes to opcodes (very useful e.g. for marshaling bytecode or
doing dynamic liveness analysis). The trick is to store an opcode
descriptor just before the address to jump to for the bytecode. In
this fashion one can use the bytecode either to jump to it or to
retrieve information about the opcode that it implements.
We used to be able to cheat and use C++ labels for that purpose. This
hasn't been working for quite some time now. It is now very rarely
the case that if you put something just before a label (in C++), you
will actually find it there: gcc moves code blocks around very freely
:-)
I was able to make this work using asm() statements but I am very
unsure about how to properly align things.
The general shape of the trick is something like this:
FAKE_OPCODE3:
asm(".byte 3");
asm(".p2align 4,3");
asm(" TRUE_OPCODE3:");
execute_opcode3();
The FAKE_OPCODE3 is needed to fool gcc into believing this code is
reachable (the address of FAKE_OPCODE3 is taken and used somewhere
else). I use the address of label TRUE_OPCODE3 as the bytecode for
opcode3. Using this address, I can find the opcode itself in the byte
that precedes it.
My worry is: what alignment should I use in the .p2align instruction?
Is there a safe value? Is there a minimal value? What are the
implications? Is there a way for me to automatically configure this
alignment appropriately (optimally?) for the target platform?
Any advice would be greatly appreciated
Cheers,
--
Dr. Denys Duchier
Équipe Calligramme
LORIA, Nancy, FRANCE