I'm making some kind of interpreter and I'm computing a static const jump table thanks to local label addresses.
You know the drill,
static const int JUMP_TABLE[] = { &&case0 - &&case0, &&case1 - &&case0 and so on.
For various reasons, mostly performance, I'd like to copy/compress this table in an object during init.
I'm hurting my head against the wall because I can't figure how to escape the lexical scoping of the function !
How can I somehow reference &&case0 from another function ?
Does somebody have a good trick for this ?
Thanks in advance
Get a label address out of the function scope in gcc/clang (C++)
785 Views Asked by Tramboi AtThere are 2 best solutions below
On
Sometimes Goto is simply the best solution. Tho very rare. Its still part of c++ after many years for a good reason
So what i did was create a global bool and set it after I initialize my address array. So the first time my interpreter function is called, it loads a structure of addresses so everything is within the same function.
Then with a little study of the assemblers output i was able to save a few ticks by arranging my code as follows.
if(is_initialized) .. ex command else ... initialize stuff. Goto ex command
use a goto to jump back to the top and execute the command. My interpreter is using almost 200 commands.
With a switch statement it was taking 5-1100 ticks. Depending on how far down the list the command was
Using goto functions[command] has it down to 14 regardless of where the command is in the list
This provides a method that is purely c++ but not supported on all compilers
I'm not aware of ways to achieve this within pure GNU C so approaches below use other mechanisms.
Double compilation
You can compile your object file twice, collecting offsets on the first run and using them on the second. For example
Now you can compile, extract bytes from
.foo_offsetssection and embed them to you app on second runInline assembly
You can use inline assembly to globalize labels:
Unfortunately in this case you can only collect addresses (not offsets) so you'll need to manually compute offsets at startup.