From 351bcc91b53c3559309002388fd3357f5700f4b6 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Sat, 30 Jul 2022 09:50:29 -0700 Subject: Fix some test cases in the compiler. --- csc/encoding-test.csc | 4 ++-- csc/linker-test.csc | 27 ++++++++------------------- csc/linker.csc | 28 ++++++++++++++++++++-------- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/csc/encoding-test.csc b/csc/encoding-test.csc index 7a96c51..6b28d8e 100644 --- a/csc/encoding-test.csc +++ b/csc/encoding-test.csc @@ -117,5 +117,5 @@ (test encode-exit (assert-equal - #u8(52) - (encode '((exit))))))) + #u8(#x36 #x1 0 0 0 0 0 0 0) + (encode '((exit (const 0)))))))) diff --git a/csc/linker-test.csc b/csc/linker-test.csc index 7b5d5db..3c24e90 100644 --- a/csc/linker-test.csc +++ b/csc/linker-test.csc @@ -18,43 +18,32 @@ (test link-labels (assert-equal - '((jmp (const 3)) - (jmp (const 2)) + '((alloc (local 0) (const 0)) (jmp (const 1)) - (jmp (const 1))) + (jmp (const 0))) (link '(((label 0) (jmp (label 1)) (label 1) - (jmp (label 0)) - (label init) (jmp (label 0))))))) (test link-labels-are-unique-per-program (assert-equal - '((jmp (const 2)) - (jmp (const 1)) - (jmp (const 1)) - (jmp (const 5)) - (jmp (const 4)) - (jmp (const 4))) + '((alloc (local 0) (const 0)) + (jmp (const 0)) + (jmp (const 1))) (link '(((label 0) - (jmp (label 0)) - (label init) (jmp (label 0))) ((label 0) - (jmp (label 0)) - (label init) (jmp (label 0))))))) (test link-globals (assert-equal - '((jmp (const 1)) - (peek (local 0) (const 10))) + '((alloc (local 0) (const 11)) + (peek (local 1) (local 0) (const 10))) (link - '(((label init) - (peek (local 0) (global cons (csc based))))) + '(((peek (local 1) (local 0) (global cons (csc based))))) (alist->map compare-globals '(((global cons (csc based)) . 10)))))))) diff --git a/csc/linker.csc b/csc/linker.csc index a2ab447..6502cb0 100644 --- a/csc/linker.csc +++ b/csc/linker.csc @@ -92,16 +92,28 @@ (_ arg)))))) + (define (num-globals environment) + (define n 0) + (map-for-each (lambda (k v) + (when (<= n v) + (set! n (+ 1 v)))) + environment) + n) + + (define link (case-lambda ((programs environment) - (translate-globals - (loop for prog in programs - for off = 0 then (+ off (length converted-prog)) - for converted-prog = (translate-labels - prog - (make-label-map off prog)) - append converted-prog) - environment)) + (append + (list + (list 'alloc (list 'local 0) (list 'const (num-globals environment)))) + (translate-globals + (loop for prog in programs + for off = 0 then (+ off (length converted-prog)) + for converted-prog = (translate-labels + prog + (make-label-map off prog)) + append converted-prog) + environment))) ((programs) (link programs (add-to-environment (make-map compare-globals) programs))))))) -- cgit v1.3.1