Introduction
Kitsch code is compiled into an assembly-like intermediate language. This intermediate language could then be compiled to a real target assembly language and then machine code. In the case of this project, the intermediate language is going to be interpreted by a virtual machine.
Instruction Summary
All operations "pop"/remove the required arguments from the stack. Nothing is "peeked".
Arithmetic Instructions
- ADD : Add the top two parameters on the stack, push result onto the stack
- SUB : Subtract parameter on top of stack by 2nd parameter from top of stack. Push result onto the stack.
- MUL : Multiply top two parameters on stack, push result onto the stack
- DIV : Divide parameter on top of stack by 2nd parameter from top of stack. Push quotient onto the stack
- MOD : Divide parameter on top of stack by 2nd parameter from top of stack. Push remainder onto the stack
Bitwise Instructions
- ANDI : Bitwise AND top two parameters on stack, push result onto the stack
- ORI : Bitwise OR top two parameters on stack, push result onto the stack
- XORI : Bitwise XOR top two parameters on stack, push result onto the stack
- NOTI : Negate top parameter on stack, push result onto the stack
Control Instructions
$STACK refers to the top-most value on the stack
$STACK-1 refers to the 2nd value from the top of the stack
- BEQ #offset : Branch by offset if $STACK == $STACK-1
- BNE #offset : Branch by offset if $STACK != $STACK-1
- BLT #offset : Branch by offset if $STACK < $STACK-1
- BLE #offset : Branch by offset if $STACK <= $STACK-1
- BGT #offset : Branch by offset if $STACK > $STACK-1
- BGE #offset : Branch by offset if $STACK >= $STACK-1
- BRA #offset : Unconditional branch by offset
- JMP [#]address : Unconditional jump to "absolute" address. Address can be either an immediate value or stored in memory.
Load/Store Instructions
- LOAD [#]Number : Push immediate value (with #) or value at specified address onto the stack from memory
- STORE Address : Pop value from stack and store it into specified address.
- LOADSP: Push current memory stack pointer value onto the stack
- STORESP: Pop current value off stack and store it in the memory stack pointer register
Addressing Modes
There are four addressing modes for LOAD/STORE instructions
| Mode | Address Prefix | Description |
| Immediate | # | To be interpretted as an immediate value, not an address. (Only valid for LOAD) |
| Stack Relative | | Address is relative to the stack pointer. Effective Address = Address + SP |
| Absolute | & | Address is absolute (relative to 0), with no stack pointer offset |
| Indirect | @ | Effective address is the value stored in the specified address. This is essentially a pointer dereference. |
I/O Instructions
While not practical for a real machine language, we leave the implementation of simple I/O instructions up to the virtual machine by adding these instructions.
- PNT : Prints the value from the top of the stack, TOSTRING should be called before printing non-string types.
- READINT: Reads an integer (followed by newline) and pushes it onto the stack.
- READSTR: Reads a string (followed by a newline) and pushes it onto the stack. The newline is not included in the string on the stack.
- READBOOL: Reads either "true" or "false" (case does not matter) and pushes 1 or 0 onto the stack respectively. Any other input will cause a runtime error.
Type Conversion
Machine instructions for doing conversion between types.
- TOINT : Casts value on top of stack to an integer. If the value is already an integer, no effect. If the value is a string of numerical digits, it is cast to the equivilent integer - otherwise it generates a runtime error.
- TOSTR : Casts the value on top of the stack to a string representation. If the value is a string, the value will be unchanged. If the value is an integer, it will be a string of the decimal digits (1234 --> "1234").
- TOBOOL : The internal representation of a bool is an integer. True = 1, False = 0. Casts the value on the top of the stack to a 0 or 1. If the value is a string, string of length 1 or more --> True, an empty string --> False
String Instructions
For simplicity and flexibility strings are handled by the virtual machine, not as individual characters on the stack. However different machine instructions are used for operations on strings so as to remove the notion of typing as much as possible from the virtual machine.
- STRADD : Concatenate (add) the top two parameters on the stack. $STACK + $STACK-1
- SUBSTR : Find substring of the string on the top of the stack and place it onto the top of the stack. Requires the starting and ending indices to be on the stack as well.
http://code.google.com/p/kitsch/wiki/SUBSTR
Miscellaneous
- NOOP : No-operation instruction, does nothing