Skip to content

Commit

Permalink
Step 8 passes
Browse files Browse the repository at this point in the history
  • Loading branch information
bendudson committed Dec 26, 2017
1 parent 90fb865 commit 7f8ae81
Showing 1 changed file with 3 additions and 131 deletions.
134 changes: 3 additions & 131 deletions nasm/step8_macros.asm
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ global _start
%include "reader.asm" ; String -> Data structures
%include "core.asm" ; Core functions
%include "printer.asm" ; Data structures -> String
%include "exceptions.asm" ; Error handling
section .bss
;; Top-level (REPL) environment
repl_env:resq 1

;; Error handler list
error_handler: resq 1
section .data

Expand Down Expand Up @@ -80,134 +78,6 @@ section .data
static run_script_string, db "(load-file ",34
section .text

;; ----------------------------------------------
;;
;; Error handling
;;
;; A handler consists of:
;; - A stack pointer address to reset to
;; - An address to jump to
;; - An optional data structure to pass
;;
;; When jumped to, an error handler will be given:
;; - the object thrown in RSI
;; - the optional data structure in RDI
;;

;; Add an error handler to the front of the list
;;
;; Input: RSI - Stack pointer
;; RDI - Address to jump to
;; RCX - Data structure. Set to zero for none.
;; If not zero, reference count incremented
;;
;; Modifies registers:
;; RAX
;; RBX
error_handler_push:
call alloc_cons
; car will point to a list (stack, addr, data)
; cdr will point to the previous handler
mov [rax], BYTE (block_cons + container_list + content_pointer)
mov rbx, [error_handler]
cmp rbx, 0 ; Check if previous handler was zero
je .create_handler ; Zero, so leave null
; Not zero, so create pointer to it
mov [rax + Cons.typecdr], BYTE content_pointer
mov [rax + Cons.cdr], rbx

; note: not incrementing reference count, since
; we're replacing one reference with another
.create_handler:
mov [error_handler], rax ; new error handler
mov rdx, rax
call alloc_cons
mov [rdx + Cons.car], rax
; Store stack pointer
mov [rax], BYTE (block_cons + container_list + content_function)
mov [rax + Cons.car], rsi ; stack pointer
mov rdx, rax
call alloc_cons
mov [rdx + Cons.typecdr], BYTE content_pointer
mov [rdx + Cons.cdr], rax
; Store function pointer to jump to
mov [rax], BYTE (block_cons + container_list + content_pointer)
mov [rax + Cons.car], rdi

; Check if there is an object to pass to handler
cmp rcx, 0
je .done

; Set the final CDR to point to the object
mov [rax + Cons.typecdr], BYTE content_pointer
mov [rax + Cons.cdr], rcx

mov rsi, rcx
call incref_object
.done:
ret
;; Removes an error handler from the list
;;
;; Modifies registers:
;; RSI
;; RAX
;; RCX
error_handler_pop:
; get the address
mov rsi, [error_handler]
cmp rsi, 0
je .done ; Nothing to remove
push rsi
mov rsi, [rsi + Cons.cdr] ; next handler
mov [error_handler], rsi
call incref_object ; needed because releasing soon
pop rsi ; handler being removed
call release_cons
.done:
ret
;; Throw an error
;; Object to pass to handler should be in RSI
error_throw:
; Get the next error handler
mov rax, [error_handler]
cmp rax, 0
je .no_handler
; Got a handler
mov rax, [rax + Cons.car] ; handler
mov rbx, [rax + Cons.car] ; stack pointer
mov rax, [rax + Cons.cdr]
mov rcx, [rax + Cons.car] ; function
mov rdi, [rax + Cons.cdr] ; data structure

; Reset stack
mov rsp, rbx

; Jump to the handler
jmp rcx
.no_handler:
; Print the object in RSI then quit
cmp rsi, 0
je .done ; nothing to print
mov rdi, 1 ; print_readably
call pr_str
mov rsi, rax
call print_string
.done:
jmp quit_error
;; ----------------------------------------------
;; Evaluates a form
;;
Expand Down Expand Up @@ -1450,6 +1320,8 @@ eval:
mov rsi, r15
call incref_object
pop rax

; Binds
call alloc_cons
mov [rax], BYTE (block_cons + container_function + content_pointer)
Expand Down

0 comments on commit 7f8ae81

Please sign in to comment.