+//---------------------------------------------------------------------
+// TODO
+//---------------------------------------------------------------------
+/*
+ * Optimize return, load, and store primitives using macros
+ * Get rid of the negates and addition/subtraction
+*/
+
//---------------------------------------------------------------------
// Register Usage
//---------------------------------------------------------------------
+// x86_64 Parameter Registers:
+// rdi, rsi, rdx, rcx, r8, r9
+//
+// x86_64 Preserved Registers:
+// rbx, rsp, rbp, r12, r13, r14, r15
+
.set PC, %rsi
.set SP, %rsp
.set FP, %rbp
.set AC, %rax
.set T1, %rbx
+// When calling C code PC and AC must be manually preserved!
+
+//---------------------------------------------------------------------
+// Value Tagging
+//---------------------------------------------------------------------
+
+.set IMM_BITS_BITMASK, $0x3
+.set IMM_BITS_FIXNUM, $0x1
+.set IMM_BITS_CHARACTER, $0x2
+
+.set OBJ_PRIMITIVE, $0x000000000000000
+.set OBJ_BOOLEAN, $0x100000000000000
+.set OBJ_TRUE, $0x100000000000004
+.set OBJ_FALSE, $0x100000000000000
+.set OBJ_NULL, $0x200000000000000
+.set OBJ_PAIR, $0x300000000000000
+.set OBJ_FLONUM, $0x400000000000000
+.set OBJ_SYMBOL, $0x500000000000000
+.set OBJ_STRING, $0x600000000000000
+.set OBJ_VECTOR, $0x700000000000000
+.set OBJ_PORT, $0x800000000000000
+.set OBJ_CLOSURE, $0x900000000000000
+.set OBJ_RESERVED1, $0xA00000000000000
+.set OBJ_RESERVED2, $0xB00000000000000
+.set OBJ_RESERVED3, $0xC00000000000000
+.set OBJ_RESERVED4, $0xD00000000000000
+.set OBJ_RESERVED5, $0xE00000000000000
+.set OBJ_BITMASK, $0xF00000000000003
+.set OBJ_VALUE_MASK, $0x0FFFFFFFFFFFFFC
+
//---------------------------------------------------------------------
// Main Routine
//---------------------------------------------------------------------
.p2align 4, 0x90
EXEC_FUN:
pushq PC
- pushq %rbp
- leaq 8(%rax), %rsi
- movq %rsp, %rbp
+ pushq FP
+ leaq 8(AC), PC
+ movq SP, FP
NEXT
//---------------------------------------------------------------------
// Exit a function, returning to the caller BROKEN!!
defcode OP_return
- popq %rbx
- movq 8(%rbp), %rsi
- movq (%rbp), %rbp
+ popq T1
lodsq
- leaq (%rsp, %rax, 8), %rsp
- pushq %rbx
+ addq $2, AC
+ leaq (FP, AC, 8), SP
+ movq 8(FP), PC
+ movq (FP), FP
+ pushq T1
NEXT
// Pushes an immediate value onto the stack
defcode OP_imm
lodsq
- pushq %rax
+ pushq AC
NEXT
// Duplicates the top value on the stack
defcode OP_dup
- movq (%rsp), %rax
- pushq %rax
+ movq (SP), AC
+ pushq AC
NEXT
// Drops the top N values from the stack
defcode OP_dropn
lodsq
- leaq (%rsp, %rax, 8), %rsp
+ leaq (SP, AC, 8), SP
NEXT
// Allocates space for N locals in the current stack frame
defcode OP_localsn
lodsq
- leaq (%rsp, %rax, 8), %rsp
+ neg AC
+ leaq (SP, AC, 8), SP
NEXT
// Push the Nth argument onto the stack
defcode OP_ldargn
lodsq
- addq $2, %rax
- neg %rax
- movq (%rbp, %rax, 8), %rax
- pushq %rax
+ addq $2, AC
+ movq (FP, AC, 8), AC
+ pushq AC
NEXT
// Pop top of stack and store to the Nth argument slot
defcode OP_stargn
- popq %rbx
+ popq T1
lodsq
- addq $2, %rax
- neg %rax
- movq %rbx, (%rbp, %rax, 8)
+ addq $2, AC
+ movq T1, (FP, AC, 8)
NEXT
// Push the Nth local onto the stack
defcode OP_ldlocn
lodsq
- movq (%rbp, %rax, 8), %rax
- pushq %rax
+ addq $1, AC
+ neg AC
+ movq (FP, AC, 8), AC
+ pushq AC
NEXT
// Pop top of stack and store to the Nth local slot
defcode OP_stlocn
- popq %rbx
+ popq T1
lodsq
- movq %rbx, (%rbp, %rax, 8)
+ addq $1, AC
+ neg AC
+ movq T1, (FP, AC, 8)
NEXT
//---------------------------------------------------------------------
.p2align 4, 0x90
_main:
main:
- pushq %rax
- movq %rsp, %rbp
- leaq cold_start(%rip), %rsi
+ pushq AC
+ movq SP, FP
+ leaq cold_start(%rip), PC
NEXT
// First instructions to execute by the interpreter
.quad OP_ldlocn, 0
.quad OP_stargn, 0
.quad OP_ldargn, 0
- .quad OP_return, -2
+ .quad OP_return, 2