Manipulation of activation records in Apoo
Keywords: apoo

##
`Manipulation of activation records in Apoo`

This is an extension of the `Apoo` vpu for dealing with activation records. It is available as version 3.0.

There are two programmable registers to address the system stack: stack register and frame register. They correspond to the last two registers of a `Apoo` vpu configuration, `Rn-1` and `Rn-2`, but are aliased to `rs` and `rf`, respectively. The stack register `rs` contains the address of the last stack memory cell (or -1 if no static memory is allocated). The instructions `jsr`, `rtn`, `push` and `pop` manipulates the stack in the usual way. Besides that, the contents of the stack register can be manipulated as any other register.

The frame register can be used for the implementation of local information (on the system stack). It contents should be the first stack address of the current activation record. Like the stack register it can be manipulated as any other register, but it is also used in two special instructions: `storeo` and `loado`.

`storeo Ri Num`
  stores the contents of register `Ri` at memory address `(rf) + Num`, where Num is an integer.

`loadeo Num Ri`
  loads the contents of memory address `(rf) + Num` into register `Ri`, where Num is an integer.

In both instructions, if `Num` is non negative it should correspond to local memory and if it is negative, possibly corresponds to arguments of a subroutine call.

Here is an example of the use of these registers, for the implementation of subroutines with arguments (passed on the stack) and local memory.

    n:	const 5
    	loadn 4 r1
          # an argument
    	push r1
    	zero r1
    	jsr test
    	pop r1
    	halt
    test:	push rf
    	#saves the current frame pointer
    	#current frame pointer
    	storer rs rf
    	loadn  6 r2
    	# reserves some local space
    	add    r2 rs
    	# gets the argument
            loado -2 r1
          # only testing the rs
    	push r1
    	pop r1
    	#stores contents of r1 at rf+1
    	storeo r1 1
    	#loads the same value into r3
    	loado 1 r3
    	# restores stack before return
    	sub rs r2
    	storer r2 rs
    	# restores frame before return
    	pop rf
    	rtn

A more complete example can be found here.