Skip to content

Commit

Permalink
Step 9 passes again
Browse files Browse the repository at this point in the history
3 soft failures, due to hash-map comparisons
  • Loading branch information
bendudson committed Dec 26, 2017
1 parent e64592a commit 90fb865
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 160 deletions.
157 changes: 18 additions & 139 deletions nasm/step9_try.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 @@ -86,137 +84,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
; Note: This can't use content_pointer or release
; will try to release this memory address
mov [rax], BYTE (block_cons + container_list + content_function)
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
mov [rsi + Cons.typecdr], BYTE 0
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 @@ -1481,6 +1348,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 Expand Up @@ -1716,7 +1585,8 @@ eval:
push R9
push R10

push r15 ; Env
; Set the error handler
mov rsi, rsp ; Stack pointer
mov rdi, .catch ; Address to jump to
Expand All @@ -1731,10 +1601,12 @@ eval:
mov rsi, r8 ; The form to evaluate (A)
call incref_object ; AST released by eval

call eval
mov r8, rax ; Result in R8
pop r15 ; Environment
; Discard B and C
;add rsi, 8 ; pop R10 and R9
pop r10
Expand All @@ -1749,7 +1621,12 @@ eval:
; Jumps here on error
; Value thrown in RSI
;

push rsi
call error_handler_pop
pop rsi

pop r15 ; Env
pop r12 ; B (symbol to bind)
pop r13 ; C (form to evaluate)

Expand Down Expand Up @@ -1783,9 +1660,11 @@ eval:
mov rdi, rsi ; Env in RDI (will be released)
mov rsi, [r13 + Cons.car] ; Form to evaluate
call incref_object ; will be released
call eval

push r15
call eval
pop r15
jmp .return
.try_missing_catch:
Expand Down
21 changes: 0 additions & 21 deletions nasm/stepA_mal.asm
Original file line number Diff line number Diff line change
Expand Up @@ -1815,27 +1815,6 @@ apply_fn:
mov rax, [rax + Cons.car] ; Body
pop rcx ; Exprs

;;;
; push rax
; push rcx
; push rsi
; push rdi
; push rdx
; push r15
; push r13

; mov rsi, rcx
; call core_println
; pop r13
; pop r15
; pop rdx
; pop rdi
; pop rsi
; pop rcx
; pop rax

;;;
; Check the type of the body
mov bl, BYTE [rax]
Expand Down

0 comments on commit 90fb865

Please sign in to comment.