From 4655afa0035032dc3a5e46048b1bc22341d085a3 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 25 Feb 2021 22:48:18 -0500 Subject: [PATCH] basic working interpreter with arguments, return values, and local storage implemented --- README | 48 ++++++++++++++++++++ main.s | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 README create mode 100644 main.s diff --git a/README b/README new file mode 100644 index 0000000..5be3297 --- /dev/null +++ b/README @@ -0,0 +1,48 @@ +Values: + push_imm + push_true + push_false + push_null + push_ + push_str + push_sym + push_flt + +List Ops: + cons + car + cdr + +Value Predicates: + is_fixnum + is_flonum + is_number + is_bool + is_pair + is_null + is_list ?? needed ?? + is_symbol + is_string + +Comparison Operators: + cmp_eq + cmp_lt + cmp_gt + cmp_lte + cmp_gte + +Arithmetic Operators: + add + sub + mul + div + mod + neg + +Flow Control Operators: + branch + branch_true + branch_false + tail_call + halt + return diff --git a/main.s b/main.s new file mode 100644 index 0000000..9ba7050 --- /dev/null +++ b/main.s @@ -0,0 +1,141 @@ +//--------------------------------------------------------------------- +// Register Usage +//--------------------------------------------------------------------- +// %rax Accumulator Register +// %rsi Program Counter +// %rsp Argument Stack Pointer + +//--------------------------------------------------------------------- +// Main Routine +//--------------------------------------------------------------------- + +// Execute next instruction +.macro NEXT + lodsq + jmpq *(%rax) +.endm + +// Macro to define a primitive +.macro defcode name + .data + .global \name + .p2align 4 +\name : + .quad code_\name + .text + .globl code_\name + .p2align 4, 0x90 +code_\name : + // ...the code goes here... +.endm + +// Macro to define a function +.macro deffun name + .data + .global \name + .p2align 4 +\name : + .quad EXEC_FUN + // ...the code goes here... +.endm + +// Main Interpreter Definition + .text + .globl EXEC_FUN + .p2align 4, 0x90 +EXEC_FUN: + pushq %rsi + pushq %rsp + pushq %rbp + leaq 8(%rax), %rsi + movq %rsp, %rbp + NEXT + +//--------------------------------------------------------------------- +// Primitive Definitions +//--------------------------------------------------------------------- + +// Exit the application +defcode OP_halt + popq %rdi + call _exit + +// Exit a function, returning to the caller +defcode OP_return + popq %rbx + lodsq + movq %rbp, %rsp + popq %rbp + popq %rsp + popq %rsi + leaq (%rsp, %rax, 8), %rsp + pushq %rbx + NEXT + +// Pushes an immediate value onto the stack +defcode OP_imm + lodsq + pushq %rax + NEXT + +// Duplicates the top value on the stack +defcode OP_dup + movq (%rsp), %rax + pushq %rax + NEXT + +// Drops the top N values from the stack +defcode OP_dropn + lodsq + leaq (%rsp, %rax, 8), %rsp + NEXT + +// Allocates space for N locals in the current stack frame +defcode OP_localsn + lodsq + leaq (%rsp, %rax, 8), %rsp + NEXT + +// Push the Nth argument onto the stack +defcode OP_ldargn + NEXT + +// Push the Nth local onto the stack +defcode OP_ldlocn + NEXT + +// Pop top of stack and store to the Nth argument slot +defcode OP_stargn + NEXT + +// Pop top of stack and store to the Nth local slot +defcode OP_stlocn + NEXT + +//--------------------------------------------------------------------- +// Main Routine +//--------------------------------------------------------------------- + .text + .global _main + .global main + .p2align 4, 0x90 +_main: +main: + pushq %rax + movq %rsp, %rbp + leaq cold_start(%rip), %rsi + NEXT + +// First instructions to execute by the interpreter + .data + .global cold_start + .p2align 4 +cold_start: + .quad OP_imm, 1 + .quad OP_imm, 2 + .quad dummy + .quad OP_halt + +deffun dummy + .quad OP_imm, 0x2A + .quad OP_return, -2 -- 2.49.0