Skip to content

Commit

Permalink
have link return its symbol addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
devyn committed Aug 10, 2023
1 parent fde09d4 commit 7dcc83e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
27 changes: 16 additions & 11 deletions stage2/03-list-util.lsp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
(proc args scope
(eval (eval scope (car args)) (cons map (eval scope (cadr args)))))))

; make a list from args
(define list
(proc args scope (eval-list scope args)))

; associate two lists into pairs
; if the second list is shorter than the first, remaining pairs will be associated to nil
(define assoc
Expand All @@ -37,14 +41,14 @@
(cons
(car (car args))
(car (cadr args)))
(unquote (cons map
(cons (cdr (car args))
(cons (cdr (cadr args)))))))))
(unquote (list map
(cdr (car args))
(cdr (cadr args)))))))
; pass evaluated first and second arg to `map`
(proc args scope
(unquote (cons map
(cons (eval scope (car args))
(cons (eval scope (cadr args)))))))))
(unquote (list map
(eval scope (car args))
(eval scope (cadr args)))))))

; concat two lists
(define concat
Expand All @@ -53,10 +57,11 @@
(if (nil? (car args))
(cadr args)
(cons (car (car args))
(unquote (cons rec
(cons (cdr (car args)) (cons (cadr args))))))))
(unquote (list rec
(cdr (car args))
(cadr args))))))
(proc args scope
(unquote (cons rec
(cons (eval scope (car args))
(cons (eval scope (cadr args)))))))))
(unquote (list rec
(eval scope (car args))
(eval scope (cadr args)))))))

13 changes: 7 additions & 6 deletions stage2/15-link.lsp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
; link a program
; expects multiple named sections with instructions following the name
; symbols defined in context: pc, rel, rel+, all sections
; returns the address and size of the program
; returns the address, size, and sections of the program
(define link (proc program scope
(let
(
Expand All @@ -49,9 +49,10 @@
(program-scope
(concat
; define rel and rel+
(assoc
(quote (rel rel+))
(cons rel (cons rel+ ())))
(list
(cons (quote rel) rel)
(cons (quote rel+) rel+)
)
(concat section-addrs scope)))
(put-instruction
(fn (pc instruction-expr)
Expand All @@ -70,6 +71,6 @@
program-addr ; start pc = base addr
program)
; the output of the above should be the end address of the program,
; but we want to return (addr size)
(cons program-addr (cons program-size ()))))))
; but we want to return (addr size section-addrs)
(list program-addr program-size section-addrs)))))

5 changes: 3 additions & 2 deletions stage2/16-awesome.lsp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; try a simple assembler program
(define awesome.str$ (ref "Awesome string!"))
(define awesome$ (car (link
(define awesome.out (link
(start
; initialize counter, stack
(\addi $sp $sp -0x10)
Expand Down Expand Up @@ -35,5 +35,6 @@
(\addi $sp $sp 0x10)
(\ret)
)
)))
))
(define awesome$ (car awesome.out))
(call-native awesome$)

0 comments on commit 7dcc83e

Please sign in to comment.