6.2 Returning from a Function Call

Returning from a function call on the Perq is done by a micro-coded instruction. On the IBM RT PC, return has to do the following:

  1. Pop the binding stack back to the binding stack pointer stored in the frame we’re returning from. For each symbol/value pair popped of the binding stack, restore that value for the symbol.
  2. Save the current value of the frame pointer in a temporary registers. This will be used to restore the control stack pointer at the end.
  3. Restore all the registers that are preserved across a function call.
  4. Get a pointer to the code vector for the function we’re returning to. This is retrieved from the code slot of what is now the active function.
  5. Make sure the relative PC (which is now in a register) is positive and add it to the code vector pointer above, giving the address of the instruction to return to.
  6. If the function is returning multiple values do a block transfer of all the return values down over the stack frame just released, i.e., the first return value should be stored where the temporarily saved frame pointer points to. In effect the return values can be pushed onto the stack using the saved frame pointer above as a stack pointer that is incremented everytime a value is pushed. Register A0 can be examined to determine the number of values that must be transferred.
  7. Set the control stack register to the saved frame pointer above. NB: it may have been updated if multiple values are being returned.
  8. Resume execution of the calling function.

Again, it is not always necessary to use the general return code. At compile time it is often possible to determine that no special symbols have to be unbound and/or only one value is being returned. For example the code to perform a return when only one value is returned and it is unnecessary to unbind any special symbols is:

	cas	NL1,FP,0		; Save frame register.
	lm	L0,FP,0			; Restore all preserved regs.
	ls	A3,AF,%function-code	; Get pointer to code vector.
	niuo	PC,PC,#x07FF		; Make relative PC positive.
	cas	PC,A3,PC		; Get addr. of instruction
	bnbrx	pz,PC			; to return to and do so while
	cas	CS,NL1,0		; updating control stack reg.