Address of labels (MSVC)

The only way of doing this in MSVC is by using inline assembly (which basically buggers you for x64):

int _tmain(int argc, _TCHAR* argv[])
{
case_1:
    void* p;
    __asm{ mov [p],offset case_1 }
    printf("0x%p\n",p);
    return 0;
}

If you plan on doing something like this, then the best way would be to write the whole interpreter in assembly then link that in to the main binary via the linker (this is what LuaJIT did, and it is the main reason the VM is so blindingly fast, when its not running JIT’ed code that is).

LuaJIT is open-source, so you might pick up some tips from it if you go that route. Alternatively you might want to look into the source of forth (whose creator developed the principle you’re trying to use), if there is an MSVC build you can see how they accomplished it, else you’re stuck with GCC (which isn’t a bad thing, it works on all major platforms).

Take a look at what Erlang does for building on Windows. They use MSVC for most of the build, and then GCC for one file to make use of the labels-as-values extension. The resulting object code is then hacked to be made compatible with the MSVC linker.

http://www.erlang.org/doc/installation_guide/INSTALL-WIN32.html

Leave a Comment