The approach described here is based on generation from the intermediate representation in the form of a an abstract syntax tree formed by the nodes created during the parsing process.
The target machine is the Postfix engine described below.
SL0 - single stack, large stack, 0 operand machine.
Three special purpose registers: IP (instruction pointer), for jumps and function calls; FP (frame pointer), the basis of the frame pointer for each function; SP (stack pointer), keeps track of the top of the stack.
The stack grows in the direction of lower addresses.
Almost all operations use the stack as input and output (exceptions are PUSH and POP, for returning values from functions in accordance with CDecl), and instructions that use either immediate arguments or arguments in global memory (i.e., with explicit named addressing).
The Postfix reference guide (a.k.a. Appendix B) contains further details about the Postfix machine and its operations.
The mnemonics are those of the Postfix language. Comments indicate missing parts.
Basic structure of a "while" cycle:
<asm> LABEL condition
JZ endwhile
JMP condition LABEL endwhile </asm>
Basic structure of a C-style "for" cycle:
<asm>
LABEL condition
JZ endfor
LABEL increment
JMP condition LABEL endfor </asm>
The C-style "break" and "continue" instructions must jump, respectively, to the end of the cycle or to its condition (for deciding on whether to execute the next iteration). In the case of "for" cycles, the jump to the condition is not direct, but rather via a jump to the increment label.
If there are multiple nested cycles, the code generator must keep track of the innermost one, so that it can generate the appropriate jumps. This can be achieved through the use of address stacks (one for the condition labels and another for increment labels).
Basic structure of an "if-then" instruction:
<asm>
JZ endif
LABEL endif </asm>
Basic structure of an "if-then-else" instruction:
<asm>
JZ else
JMP endif LABEL else
LABEL endif </asm>
The function structure presented here assumes a "void" return. If a return were desired, the use of POP or DPOP would be required before the LEAVE instruction.
"sizeoflocalvars" is the number of bytes needed to store all of the function's local variables. If this value is 0 (zero), START may be used instead of ENTER.
If the function is not global (e.g. C-style "static" function), then GLOBAL should not be used. <asm> GLOBAL functionname,FUNC LABEL functionname ENTER sizeoflocalvars
LEAVE RET </asm>
We include in this category C-like "static" function variables (the only difference is that they are not accessed with their explicit names, but rather with a automatic symbol table-based name).
Addressing of these variables is by name, as shown in the following examples.
<asm>
INT 1 ; put 1 on the stack DUP ; duplicate value ADDR a ; put address of "a" on the stack STORE ; write the value on the given address </asm>
If the resulting value is not needed, throw it away:
<asm>
INT 1 ; put 1 on the stack DUP ; duplicate value ADDR a ; put address of "a" on the stack STORE ; write the value on the given address TRASH 4 </asm>
<asm>
ADDR b ; put address of "b" on the stack LOAD ; read the value at the given address DUP ; duplicate value (= is an expression) ADDR a ; put address of "a" on the stack STORE ; write the value on the given address </asm>
If ADDR is immediately followed by LOAD, then the two instructions may be replaced by a single one with the same effect: ADDRV. Similarly, ADDR+STORE = ADDRA.
<asm>
INT 1 ; put 1 on the stack DUP ; duplicate value (= is an expression) ADDRA a ; write the value in the address corresponding to "a" </asm>
<asm>
ADDRV b ; read value in the address of "b" and put it on the stack DUP ; duplicate value (= is an expression) ADDRA a ; write the value in the address corresponding to "a" </asm>
In the following examples, we assume that both a and b are local variables (thus having negative offsets relative to the framepointer). If they were function arguments, the offsets would be positive.
<asm>
INT 1 ; put 1 on the stack DUP ; duplicate value (= is an expression) LOCAL -4 ; put address of "a" (assuming offset -4) on the stack STORE ; write the value on the given address </asm>
<asm>
LOCAL -8 ; put address of "b" (assuming offset -8) on the stack LOAD ; read the value at the given address DUP ; duplicate value (= is an expression) LOCAL -4 ; put address of "a" (assuming offset -4) on the stack STORE ; write the value on the given address </asm>
If LOCAL is immediately followed by LOAD, then the two instructions may be replaced by a single one with the same effect: LOCV. Similarly, LOCAL+STORE = LOCA.
<asm>
INT 1 ; put 1 on the stack DUP ; duplicate value (= is an expression) LOCA -4 ; write the value on the address of "a" (assuming offset -4) </asm>
<asm>
LOCV -8 ; read value in the address of "b" (assuming offset -8) and put it on the stack DUP ; duplicate value (= is an expression) LOCA -4 ; write the value on the address of "a" (assuming offset -4) </asm>
A simple pure-Postfix example to illustrate duplication of stack values for double-precision floating point numbers.