Line 5: | Line 5: | ||
The current postfix code generator class maintains the stack machine abstraction, but does not rely on macros. Instead, it defines an interface to be used by semantic analysers, as defined by a strategy pattern (Gamma et al., 1995). Specific implementations provide the realization of the postfix commands for a particular target machine. Since it is written in C++, it's very easy to extend to new needs and implementations (new target machines). | The current postfix code generator class maintains the stack machine abstraction, but does not rely on macros. Instead, it defines an interface to be used by semantic analysers, as defined by a strategy pattern (Gamma et al., 1995). Specific implementations provide the realization of the postfix commands for a particular target machine. Since it is written in C++, it's very easy to extend to new needs and implementations (new target machines). | ||
+ | |||
+ | Like the original postfix code generator, the current abstraction uses an architecture based on a stack machine, hence the name ``postfix'', and three registers. | ||
+ | # IP -- the instruction pointer -- indicates the position of the next instruction to be executed; | ||
+ | # SP -- the stack pointer -- indicates the position of the element currently at the stack top; | ||
+ | # FP -- the frame pointer -- indicates the position of the activation register of the function currently being executed. | ||
+ | |||
+ | In some of the following tables, the "Stack" column presents the actions on the values at the top of the stack. Note that only elements relevant in a given context, i.e., that of the postfix instruction being executed, are shown. The notation '''#length''' represents a set of ''length'' consecutive bytes in the stack, i.e., a vector. | ||
+ | |||
+ | Consider the following example: | ||
+ | |||
+ | $ a #8 b : $ a b | ||
+ | |||
+ | The stack had at its top '''b''', followed by eight bytes, followed by '''a'''. After executing some postfix instruction using these elements, the stack has at its top '''b''', followed by '''a'''. We use '''$''' to denote the point in the stack not affected by the current operation (this could be the top if the stack were empty). | ||
The following groups of operations are available in the Postfix interface: | The following groups of operations are available in the Postfix interface: | ||
Line 12: | Line 25: | ||
text data rodata bss align label extrn globl const str char id byte double | text data rodata bss align label extrn globl const str char id byte double | ||
− | == | + | == Addressing == |
+ | |||
+ | Absolute addressing uses addresses based on named labels. Local addressing is used in function frames and uses offsets relative to the frame pointer to load data: negative addresses correspond to local variables, offset zero contains the previous (saved) value of the frame pointer, offset 4 (32 bits) contains the previous (saved) value of the instruction pointer, and, after offset 8, reside the function arguments. | ||
+ | |||
+ | {| | ||
+ | |ADDR name || $ || $ &name || Absolute addressing: load address of '''name''' | ||
+ | |- | ||
+ | |ADDRA name || $ a || $ || Absolute addressing: store '''a''' to '''name''' | ||
+ | |- | ||
+ | |ADDRV name || $ || $ *name || Absolute addressing: load value at '''name''' | ||
+ | |- | ||
+ | |LOCAL offset || $ || $ fp+offset || Local addressing: load address of '''offset''' | ||
+ | |- | ||
+ | |LOCA offset || $ a || $ || Local addressing: writes '''a''' to '''offset''' | ||
+ | |- | ||
+ | |LOCV offset || $ || $ *(fp+offset) || Local addressing: load value at '''offset''' | ||
+ | |- | ||
+ | |LOAD || $ address || $ value || Load value pointed to by '''*SP''' | ||
+ | |- | ||
+ | |DLOAD | ||
+ | |- | ||
+ | |LDCHR | ||
+ | |- | ||
+ | |STORE | ||
+ | |- | ||
+ | |DSTORE | ||
+ | |- | ||
+ | |STCHR | ||
+ | |} | ||
+ | |||
+ | == Simple Instructions == | ||
− | + | {| | |
+ | |ALLOC | ||
+ | |- | ||
+ | |DUP | ||
+ | |- | ||
+ | |DDUP | ||
+ | |- | ||
+ | |SWAP | ||
+ | |- | ||
+ | |PUSH | ||
+ | |- | ||
+ | |DPUSH | ||
+ | |- | ||
+ | |POP | ||
+ | |- | ||
+ | |DPOP | ||
+ | |- | ||
+ | |INT | ||
+ | |- | ||
+ | |SP || $ || $ sp | ||
+ | |} | ||
== aritmética/lógica == | == aritmética/lógica == |
The Postfix reference guide contains information about the structure and operations of the stack machine.
The original stack machine was created by Santos (2004). Is was composed by a set of macros to be used with printf functions. Each macro would “take” as arguments, either a number or a string. This was a simple and effective approach but was limited in its expressiveness.
The current postfix code generator class maintains the stack machine abstraction, but does not rely on macros. Instead, it defines an interface to be used by semantic analysers, as defined by a strategy pattern (Gamma et al., 1995). Specific implementations provide the realization of the postfix commands for a particular target machine. Since it is written in C++, it's very easy to extend to new needs and implementations (new target machines).
Like the original postfix code generator, the current abstraction uses an architecture based on a stack machine, hence the name ``postfix, and three registers.
In some of the following tables, the "Stack" column presents the actions on the values at the top of the stack. Note that only elements relevant in a given context, i.e., that of the postfix instruction being executed, are shown. The notation #length represents a set of length consecutive bytes in the stack, i.e., a vector.
Consider the following example:
$ a #8 b : $ a b
The stack had at its top b, followed by eight bytes, followed by a. After executing some postfix instruction using these elements, the stack has at its top b, followed by a. We use $ to denote the point in the stack not affected by the current operation (this could be the top if the stack were empty).
The following groups of operations are available in the Postfix interface:
text data rodata bss align label extrn globl const str char id byte double
Absolute addressing uses addresses based on named labels. Local addressing is used in function frames and uses offsets relative to the frame pointer to load data: negative addresses correspond to local variables, offset zero contains the previous (saved) value of the frame pointer, offset 4 (32 bits) contains the previous (saved) value of the instruction pointer, and, after offset 8, reside the function arguments.
ADDR name | $ | $ &name | Absolute addressing: load address of name |
ADDRA name | $ a | $ | Absolute addressing: store a to name |
ADDRV name | $ | $ *name | Absolute addressing: load value at name |
LOCAL offset | $ | $ fp+offset | Local addressing: load address of offset |
LOCA offset | $ a | $ | Local addressing: writes a to offset |
LOCV offset | $ | $ *(fp+offset) | Local addressing: load value at offset |
LOAD | $ address | $ value | Load value pointed to by *SP |
DLOAD | |||
LDCHR | |||
STORE | |||
DSTORE | |||
STCHR |
ALLOC | ||
DUP | ||
DDUP | ||
SWAP | ||
PUSH | ||
DPUSH | ||
POP | ||
DPOP | ||
INT | ||
SP | $ | $ sp |
add dadd sub dsub mul dmul div ddiv neg dneg mod gt ge lt le eq ne dcmp d2i d2f i2d f2d incr decr
rotl rotr shtl shtru shtrs and or not xor
call ret start enter leave trash jmp jz jnz branch leap nil nop