Assembler refactor
This commit is contained in:
parent
ce963c498c
commit
bfe6db57b6
@ -20,32 +20,30 @@
|
||||
(and (not (label? x))
|
||||
(not (variable? x))))
|
||||
|
||||
(define (find-labels inst-seq n)
|
||||
(define (label-pass instructions address)
|
||||
(cond
|
||||
[(null? inst-seq) '()]
|
||||
[(label? (car inst-seq))
|
||||
(acons (car inst-seq) n (find-labels (cdr inst-seq) n))]
|
||||
[(variable? (car inst-seq))
|
||||
(acons (cadar inst-seq) n (find-labels (cdr inst-seq) (+ n 4)))]
|
||||
[(null? instructions) '()]
|
||||
[(label? (car instructions))
|
||||
(acons (car instructions) address (label-pass (cdr instructions) address))]
|
||||
[(variable? (car instructions))
|
||||
(acons (cadar instructions) address (label-pass (cdr instructions) (+ address 4)))]
|
||||
[else
|
||||
(find-labels (cdr inst-seq) (if (eq? (caar inst-seq) 'push) (+ n 5) (+ n 1)))]))
|
||||
(label-pass (cdr instructions) (if (eq? (caar instructions) 'push)
|
||||
(+ address 5)
|
||||
(+ address 1)))]))
|
||||
|
||||
(define (write-word word)
|
||||
(define bv (make-bytevector 4))
|
||||
(bytevector-s32-native-set! bv 0 word)
|
||||
(write-bytevector bv))
|
||||
|
||||
(define (assemble inst-seq port)
|
||||
(define labels (find-labels inst-seq 1))
|
||||
(with-output-to-port port
|
||||
(lambda ()
|
||||
(let loop ([seq inst-seq])
|
||||
(define (assembly-pass seq labels)
|
||||
(cond
|
||||
[(null? seq) '()]
|
||||
[(label? (car seq)) (loop (cdr seq))]
|
||||
[(label? (car seq)) (assembly-pass (cdr seq) labels)]
|
||||
[(variable? (car seq))
|
||||
(write-word (caddar seq))
|
||||
(loop (cdr seq))]
|
||||
(assembly-pass (cdr seq) labels)]
|
||||
[else
|
||||
(let* [(inst (car seq))
|
||||
(inst-obj (lookup-instruction (car inst)))]
|
||||
@ -54,5 +52,9 @@
|
||||
(if (number? (cadr inst))
|
||||
(write-word (cadr inst))
|
||||
(write-word (assq-ref labels (cadr inst)))))
|
||||
(loop (cdr seq)))]))))
|
||||
(assembly-pass (cdr seq) labels))]))
|
||||
|
||||
(define (assemble instructions port)
|
||||
(define labels (label-pass instructions 1))
|
||||
(with-output-to-port port (lambda () (assembly-pass instructions labels)))
|
||||
labels)
|
||||
|
Loading…
Reference in New Issue
Block a user