Target code

first line:

#include "cmm450.csci"

last line:

start

3 labels are required: INIT MAIN CALLSWITCH

INIT

will be final label and must end with start

MAIN

can be embedded in switch table or at location of main

CALLSWITCH

location of jump table

SP

stack register pointer. Contains the frame reference for functions.

TOS

Top Of Stack index

TOP

Logically the value at top of stack.

There is no literal value TOP but it is useful for this reference.

push(TOS) load(0) would duplicate TOP

push(VAL)

push a number onto the stack

val may be a integer literal, or SP

Stack grows.

logicaly STACK[++TOS]=val

load(BASE)

loads the stack with a value fetched from BASE+TOP

BASE should be 0 or SP.

Top should contain offset.

TOP is replaced with value at STACK[BASE+TOP].

store(BASE)

stores the value at top fetched from base+offset

offset being currently on the stack

BASE can be 0 or SP.

2 values popped from the stack.

brz( LABEL )

branches to location LABEL if TOP == zero.

TOP is popped.

brnz( LABEL )

branches to location LABEL if TOP != zero.

TOP is popped.

jump( LABEL )

unconditional branch to location LABEL

stack is ignored.

Operators

all operators reduce the stack size by one.

The new top is replaced by the result of the operation.

TOP = left OP Right

where left and right are values on the stack (right is TOP)

Relational Operations

eq ==

lt <

gt >

ne !=

nlt >=

ngt <=

Integer Operations

div /

mult *

sub -

add +

call( ARGC )

calls a function whose id is at TOP

ARGC is the number of parameters being passed to the function.

ret

A function returns a value which is at top

end

A function returns without a value

The switch table

This is how functions ids are associated with labels.

2 functions are automatic, input and output.

0 must go to init.

The last fuunction should be main and labeled as such.

After the call switch the INIT label provides a location to initialize global variables.

The INIT region should end with the keyword start (which among other things will jump to MAIN).

INIT must be the last region and start must be the end of the file.

CALLSWITCH:
	jumptable
	entry( -2 , INPUT )
	entry ( -1 , OUTPUT )
	entry ( 0 , INIT )
	entry ( 1 , LABEL_F1 )
MAIN:	entry ( 2 , LABEL_F2 )
	endtable
INIT:
	start

but this would be ok as well (Assuming main was labeled MAIN)

CALLSWITCH:
	jumptable
	entry( -2 , INPUT )
	entry ( -1 , OUTPUT )
	entry ( 0 , INIT )
	entry ( 1 , LABEL_F1 )
	entry ( 2 , MAIN )
	endtable
INIT:
	start