6.2.3 C Code restrictions
Vmgen generates code and performs some optimizations under the assumption
that the user-supplied C code does not access the stack pointers or stack
items, and that accesses to the instruction pointer only occur through
special macros. In general you should heed these restrictions. However, if
you need to break these restrictions, read the following.
Accessing a stack or stack pointer directly can be a problem for several
reasons:
- Vmgen optionally supports caching the top-of-stack item in a local variable
(that is allocated to a register). This is the most frequent source of
trouble. You can deal with it either by not using top-of-stack caching
(slowdown factor 1-1.4, depending on machine), or by inserting flushing code
(e.g., ‘IF_spTOS(sp[...] = spTOS);’) at the start and reloading code
(e.g., ‘IF_spTOS(spTOS = sp[0])’) at the end of problematic C code.
Vmgen inserts a stack pointer update before the start of the user-supplied C
code, so the flushing code has to use an index that corrects for that. In
the future, this flushing may be done automatically by mentioning a special
string in the C code.
- The Vmgen-erated code loads the stack items from stack-pointer-indexed
memory into variables before the user-supplied C code, and stores them from
variables to stack-pointer-indexed memory afterwards. If you do any writes
to the stack through its stack pointer in your C code, it will not affect
the variables, and your write may be overwritten by the stores after the C
code. Similarly, a read from a stack using a stack pointer will not reflect
computations of stack items in the same VM instruction.
- Superinstructions keep stack items in variables across the whole
superinstruction. So you should not include VM instructions, that access a
stack or stack pointer, as components of superinstructions (see VM profiler).
You should access the instruction pointer only through its special macros
(‘IP’, ‘SET_IP’, ‘IPTOS’); this ensure that these macros can
be implemented in several ways for best performance. ‘IP’ points to
the next instruction, and ‘IPTOS’ is its contents.