Add meta assembly commands for dealing with variables
This commit is contained in:
parent
bfe6db57b6
commit
227dcc662e
@ -1,6 +1,5 @@
|
|||||||
(variable result 0)
|
(variable result 0)
|
||||||
(push result)
|
(ref result)
|
||||||
(@)
|
|
||||||
(push fib)
|
(push fib)
|
||||||
(call)
|
(call)
|
||||||
(push cleanup)
|
(push cleanup)
|
||||||
@ -31,6 +30,5 @@ recur
|
|||||||
(nip)
|
(nip)
|
||||||
(return)
|
(return)
|
||||||
cleanup
|
cleanup
|
||||||
(push result)
|
(set! result)
|
||||||
(!)
|
|
||||||
(bye)
|
(bye)
|
||||||
|
@ -16,9 +16,20 @@
|
|||||||
(define label? (compose not pair?))
|
(define label? (compose not pair?))
|
||||||
(define (variable? x)
|
(define (variable? x)
|
||||||
(and (pair? x) (eq? (car x) 'variable)))
|
(and (pair? x) (eq? (car x) 'variable)))
|
||||||
|
(define (ref? x)
|
||||||
|
(and (pair? x) (eq? (car x) 'ref)))
|
||||||
|
(define (set!? x)
|
||||||
|
(and (pair? x) (eq? (car x) 'set!)))
|
||||||
(define (instruction? x)
|
(define (instruction? x)
|
||||||
(and (not (label? x))
|
(and (not (label? x))
|
||||||
(not (variable? x))))
|
(not (variable? x))
|
||||||
|
(not (ref? x))
|
||||||
|
(not (set!? x))))
|
||||||
|
(define (instruction-size inst)
|
||||||
|
(case (car inst)
|
||||||
|
[(push) 5]
|
||||||
|
[(ref set!) 6]
|
||||||
|
[else 1]))
|
||||||
|
|
||||||
(define (label-pass instructions address)
|
(define (label-pass instructions address)
|
||||||
(cond
|
(cond
|
||||||
@ -28,9 +39,7 @@
|
|||||||
[(variable? (car instructions))
|
[(variable? (car instructions))
|
||||||
(acons (cadar instructions) address (label-pass (cdr instructions) (+ address 4)))]
|
(acons (cadar instructions) address (label-pass (cdr instructions) (+ address 4)))]
|
||||||
[else
|
[else
|
||||||
(label-pass (cdr instructions) (if (eq? (caar instructions) 'push)
|
(label-pass (cdr instructions) (+ address (instruction-size (car instructions))))]))
|
||||||
(+ address 5)
|
|
||||||
(+ address 1)))]))
|
|
||||||
|
|
||||||
(define (write-word word)
|
(define (write-word word)
|
||||||
(define bv (make-bytevector 4))
|
(define bv (make-bytevector 4))
|
||||||
@ -44,9 +53,19 @@
|
|||||||
[(variable? (car seq))
|
[(variable? (car seq))
|
||||||
(write-word (caddar seq))
|
(write-word (caddar seq))
|
||||||
(assembly-pass (cdr seq) labels)]
|
(assembly-pass (cdr seq) labels)]
|
||||||
|
[(ref? (car seq))
|
||||||
|
(write-u8 (cadr (lookup-instruction 'push)))
|
||||||
|
(write-word (assq-ref labels (cadar seq)))
|
||||||
|
(write-u8 (cadr (lookup-instruction '@)))
|
||||||
|
(assembly-pass (cdr seq) labels)]
|
||||||
|
[(set!? (car seq))
|
||||||
|
(write-u8 (cadr (lookup-instruction 'push)))
|
||||||
|
(write-word (assq-ref labels (cadar seq)))
|
||||||
|
(write-u8 (cadr (lookup-instruction '!)))
|
||||||
|
(assembly-pass (cdr seq) labels)]
|
||||||
[else
|
[else
|
||||||
(let* [(inst (car seq))
|
(let* ([inst (car seq)]
|
||||||
(inst-obj (lookup-instruction (car inst)))]
|
[inst-obj (lookup-instruction (car inst))])
|
||||||
(write-u8 (instruction-code inst-obj))
|
(write-u8 (instruction-code inst-obj))
|
||||||
(when (eq? (car inst) 'push)
|
(when (eq? (car inst) 'push)
|
||||||
(if (number? (cadr inst))
|
(if (number? (cadr inst))
|
||||||
|
@ -18,8 +18,7 @@
|
|||||||
|
|
||||||
(define fib-program-asm
|
(define fib-program-asm
|
||||||
'( (variable result 0)
|
'( (variable result 0)
|
||||||
(push result)
|
(ref result)
|
||||||
(@)
|
|
||||||
(push fib)
|
(push fib)
|
||||||
(call)
|
(call)
|
||||||
(push cleanup)
|
(push cleanup)
|
||||||
@ -50,8 +49,7 @@
|
|||||||
(nip)
|
(nip)
|
||||||
(return)
|
(return)
|
||||||
cleanup
|
cleanup
|
||||||
(push result)
|
(set! result)
|
||||||
(!)
|
|
||||||
(bye)))
|
(bye)))
|
||||||
|
|
||||||
(define adder-program-bytecode
|
(define adder-program-bytecode
|
||||||
|
Loading…
Reference in New Issue
Block a user