public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* asm(...) and how to properly align jump labels
@ 2003-11-28 11:08 Denys Duchier
  2003-11-29  0:28 ` Richard Henderson
  0 siblings, 1 reply; 2+ messages in thread
From: Denys Duchier @ 2003-11-28 11:08 UTC (permalink / raw)
  To: gcc

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: asm(...) and how to properly align jump labels
  2003-11-28 11:08 asm(...) and how to properly align jump labels Denys Duchier
@ 2003-11-29  0:28 ` Richard Henderson
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Henderson @ 2003-11-29  0:28 UTC (permalink / raw)
  To: Denys Duchier; +Cc: gcc

On Fri, Nov 28, 2003 at 01:18:48AM +0100, Denys Duchier wrote:
> 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?

No.

If you're going to do this, I recommend something like

	OPCODE3:
	  asm (" whatever data");
	  execute_opcode3();

	  ...

	  void *addr;
	  addr = labels[next_opcode];
	  addr += sizeof_data;
	  goto *addr;

And have the data sized such that it is a multiple of the required
instruction alignment of the platform.  E.g. 4 for risc machines.

If you really think you need higher alignment than that, then add
code to force ADDR to be aligned at the time you make the branch.


r~

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-11-28 22:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-28 11:08 asm(...) and how to properly align jump labels Denys Duchier
2003-11-29  0:28 ` Richard Henderson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).