views:

444

answers:

3

I have a C function that contains all the code that will implement the bytecodes of a bytecode interpreter.

I'm wondering if there is a way to align segments of the compiled code in memory on fixed size boundaries so that I could directly calculate the address to jump to from the value of the bytecode? Sort of the same way an array works but instead of reading from the calculated address, I'm jumping to it.

I am aware that i will have to put the code to perform the next jump at the end of every "bytecode code" segment and that I will have to make the boundary size at least as big as the size of the largest segment.

If this is even possible, how would I tell the compiler/assembler (gcc / g++ / as) to align in said way?

+5  A: 

I realize this is not exactly what you are asking for, but this is the standard way to implement byte code interpreters with GCC.

GCC's "computed goto" or "labels as values" feature allows you to put labels in an array and efficiently jump to different bytecode instructions. See Fast interpreter using gcc's computed goto. Also look at this related Stack Overflow question: C/C++ goto, and the GCC documentation on labels as values.

The code to do this would look something like this:

void* jumptable[] = {&&label1, &&label2};

label:
  /* Code here... */

label2:
  /* Other code here... */

You can then jump to different instructions using the table:

goto *jumptable[i];
Ville Laurikari
A: 

There are two issues here, but the answer is the same. First, you're writing (binary) data to a (binary) file. Second, you're loading that (binary) data into memory. You control where it goes on disk, and you control where it goes in memory. You can easily calculate what you're looking for.

Personally, I would probably use an array when loading data into memory, and I would make sure all data started at a valid index in that array. Arrays are laid out contiguously and are relatively easy to work with. Kernighan and Ritchie's book The C Programming Language mentions a technique of using unions for alignment, but that doesn't make pointer arithmetic any easier.

Max Lybbert
A: 

If you are using linux use posix_memalign(). I am sure there is a similar function for Windows.

If you want to align your own code have a look at the gcc __attribute__ syntax.

The ld -Ttext options may also be helpful.

teambob