Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

123 lines (90 sloc) 3.281 kb
On April 5 2012, #0x10c-dev agreed to the following standard ABI:
- Registers A, B, C are clobberable across calls
- Registers I, J, X, Y, Z are preserved across calls
- Return in A
- Function local variables are kept on the stack
- Caller cleans stack
- First three arguments to A, B, C, remaining arguments pushed right-to-left onto
the stack
- Varargs: follow normal rules; the vararg callee should push C,B,A in its
prolog to have a nice walkable array, and then clear off these three registers
from the stack in its epilog.
- No stupid tricks with the overflow flag
Note: A compiler is free to optimize away pointless preservations of registers.
The storage location of preserved copies of registers must be re-entrant safe.
(For all practical purposes, on the stack.)
Non-required suggestions for convention:
- J is used for base stack pointer (preserving the value of SP before allocating
data for locals)
--------------------------------------------------------------------------------
EXAMPLE FUNCTION CALL:
# func(1, 2, 3, 4, 5);
# first 3 arguments are passed via registers
SET A, 1
SET B, 2
SET C, 3
# 4 and 5 are pushed to stack in reverse order
SET PUSH, 5
SET PUSH, 4
JSR func
ADD SP, 2 # callers cleans up the stack (two passed words)
EXAMPLE FUNCTION PROLOGUE/EPILOGUE:
# Prologue:
SET PUSH, J
SET J, SP
SUB SP, 5 # Reserve 5 words for function locals
# Function body:
SET A, [J-1] # Access first local variable
SET B, [J+2] # Access 4th function argument
SET C, [J+3] # Access 5th function argument
# etc ...
# Epilogue:
SET A, return_value
SET SP, J
SET J, POP
SET PC, POP # return
Last updated for DCPU16v11
example by masterm
---------------------------------------------------------------------
The calling convention I am currently using in DCPUC. I'm not necessarily
proposing a complete alternative, just a working implementation to compare with. ~~Blecki
DCPUC Calling Convention
First three arguments are passed in registers A, B and C.
Remaining arguments are pushed onto the stack left-to-right (Such that the last argument has the lowest address)
Push the return address last (Using the JSR instruction is preferred)
Return value is placed in A.
Callee is free to clobber all registers.
Caller must preserve the registers it cares about (Which includes A, B and C, which may be it's own parameters).
The Caller removes any arguments on the stack itself.
A sample program and the generated assembly, illustrating a function call.
function foo(a,b,c,d) {
return foo(1,2,3,4);
}
foo(1,2,3,4);
SET A, 0x0001 ;Literal
SET B, 0x0002 ;Literal
SET C, 0x0003 ;Literal
SET PUSH, 0x0004 ;Literal
JSR LABEL1_foo ;Calling function
ADD SP, 0x0001 ;Remove parameters
BRK ;Non-standard
:LABEL1_foo
SET PUSH, A ;Saving register
SET A, 0x0001 ;Literal
SET PUSH, B ;Saving register
SET B, 0x0002 ;Literal
SET PUSH, C ;Saving register
SET C, 0x0003 ;Literal
SET PUSH, X ;Saving register
SET PUSH, 0x0004 ;Literal
JSR LABEL1_foo ;Calling function
ADD SP, 0x0001 ;Remove parameters
SET J, A ;Save return value from being overwritten by stored register
SET X, POP ;Restoring register
SET C, POP ;Restoring register
SET B, POP ;Restoring register
SET A, POP ;Restoring register
SET X, J
SET A, X
SET PC, POP ;Return
SET PC, POP ;Return
Jump to Line
Something went wrong with that request. Please try again.