]> git.mdlowis.com Git - proto/alvm.git/commitdiff
basic working interpreter with arguments, return values, and local storage implemented
authorMichael D. Lowis <mike@mdlowis.com>
Fri, 26 Feb 2021 03:48:18 +0000 (22:48 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Fri, 26 Feb 2021 03:48:18 +0000 (22:48 -0500)
README [new file with mode: 0644]
main.s [new file with mode: 0644]

diff --git a/README b/README
new file mode 100644 (file)
index 0000000..5be3297
--- /dev/null
+++ b/README
@@ -0,0 +1,48 @@
+Values:
+    push_imm <val>
+        push_true
+        push_false
+        push_null
+        push_
+    push_str <addr>
+    push_sym <addr>
+    push_flt <val>
+
+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 <offset>
+    branch_true <offset>
+    branch_false <offset>
+    tail_call <function>
+    halt
+    return
diff --git a/main.s b/main.s
new file mode 100644 (file)
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