Compare commits

..

No commits in common. "227dcc662ea33caf6e44e5fdbf82882146f217c5" and "ce963c498c09622d6cdd59e8adaef6307d9e2b8d" have entirely different histories.

3 changed files with 36 additions and 53 deletions

View File

@ -1,5 +1,6 @@
(variable result 0) (variable result 0)
(ref result) (push result)
(@)
(push fib) (push fib)
(call) (call)
(push cleanup) (push cleanup)
@ -30,5 +31,6 @@ recur
(nip) (nip)
(return) (return)
cleanup cleanup
(set! result) (push result)
(!)
(bye) (bye)

View File

@ -16,64 +16,43 @@
(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 (find-labels inst-seq n)
(cond (cond
[(null? instructions) '()] [(null? inst-seq) '()]
[(label? (car instructions)) [(label? (car inst-seq))
(acons (car instructions) address (label-pass (cdr instructions) address))] (acons (car inst-seq) n (find-labels (cdr inst-seq) n))]
[(variable? (car instructions)) [(variable? (car inst-seq))
(acons (cadar instructions) address (label-pass (cdr instructions) (+ address 4)))] (acons (cadar inst-seq) n (find-labels (cdr inst-seq) (+ n 4)))]
[else [else
(label-pass (cdr instructions) (+ address (instruction-size (car instructions))))])) (find-labels (cdr inst-seq) (if (eq? (caar inst-seq) 'push) (+ n 5) (+ n 1)))]))
(define (write-word word) (define (write-word word)
(define bv (make-bytevector 4)) (define bv (make-bytevector 4))
(bytevector-s32-native-set! bv 0 word) (bytevector-s32-native-set! bv 0 word)
(write-bytevector bv)) (write-bytevector bv))
(define (assembly-pass seq labels) (define (assemble inst-seq port)
(cond (define labels (find-labels inst-seq 1))
[(null? seq) '()] (with-output-to-port port
[(label? (car seq)) (assembly-pass (cdr seq) labels)] (lambda ()
[(variable? (car seq)) (let loop ([seq inst-seq])
(write-word (caddar seq)) (cond
(assembly-pass (cdr seq) labels)] [(null? seq) '()]
[(ref? (car seq)) [(label? (car seq)) (loop (cdr seq))]
(write-u8 (cadr (lookup-instruction 'push))) [(variable? (car seq))
(write-word (assq-ref labels (cadar seq))) (write-word (caddar seq))
(write-u8 (cadr (lookup-instruction '@))) (loop (cdr seq))]
(assembly-pass (cdr seq) labels)] [else
[(set!? (car seq)) (let* [(inst (car seq))
(write-u8 (cadr (lookup-instruction 'push))) (inst-obj (lookup-instruction (car inst)))]
(write-word (assq-ref labels (cadar seq))) (write-u8 (instruction-code inst-obj))
(write-u8 (cadr (lookup-instruction '!))) (when (eq? (car inst) 'push)
(assembly-pass (cdr seq) labels)] (if (number? (cadr inst))
[else (write-word (cadr inst))
(let* ([inst (car seq)] (write-word (assq-ref labels (cadr inst)))))
[inst-obj (lookup-instruction (car inst))]) (loop (cdr seq)))]))))
(write-u8 (instruction-code inst-obj))
(when (eq? (car inst) 'push)
(if (number? (cadr inst))
(write-word (cadr inst))
(write-word (assq-ref labels (cadr inst)))))
(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) labels)

View File

@ -18,7 +18,8 @@
(define fib-program-asm (define fib-program-asm
'( (variable result 0) '( (variable result 0)
(ref result) (push result)
(@)
(push fib) (push fib)
(call) (call)
(push cleanup) (push cleanup)
@ -49,7 +50,8 @@
(nip) (nip)
(return) (return)
cleanup cleanup
(set! result) (push result)
(!)
(bye))) (bye)))
(define adder-program-bytecode (define adder-program-bytecode