aboutsummaryrefslogtreecommitdiffstats
path: root/csc
diff options
context:
space:
mode:
authorRose Hogenson <rhogenson@posteo.net>2022-07-29 17:52:54 -0700
committerRose Hogenson <rhogenson@posteo.net>2022-07-29 17:52:54 -0700
commit762431b0f0a6ead18a93a05c0f3269dca5b8fef1 (patch)
tree068057d0da92aa852fd4674477f7f50784e7f652 /csc
parentHandle the (csc builtins) library separately. (diff)
downloadchromatopelma-762431b0f0a6ead18a93a05c0f3269dca5b8fef1.tar.zst
Insert an exit op at the end of the program.
Diffstat (limited to 'csc')
-rw-r--r--csc/codegen.csc1
-rw-r--r--csc/compiler.csc2
-rw-r--r--csc/encoding.csc5
-rw-r--r--csc/linker.csc16
4 files changed, 9 insertions, 15 deletions
diff --git a/csc/codegen.csc b/csc/codegen.csc
index 7712427..ac5b313 100644
--- a/csc/codegen.csc
+++ b/csc/codegen.csc
@@ -24,6 +24,7 @@
%label
%library-ref
%primitive
+ %tail
%variable
closure-arguments
closure-body
diff --git a/csc/compiler.csc b/csc/compiler.csc
index fcf7598..20d5d37 100644
--- a/csc/compiler.csc
+++ b/csc/compiler.csc
@@ -160,5 +160,5 @@
(compile (cons (list 'import (append imports1 imports2)) rest)))
((('import . imports) . body)
(define compiled-body (ir1->bytecode (expand-body 'main body (make-import-map imports))))
- (encode (link (revappend library-code (list compiled-body)))))
+ (encode (link (revappend library-code (list compiled-body (list (list 'exit (list 'const 0))))))))
(_ (error "unexpected form in compile" program))))))
diff --git a/csc/encoding.csc b/csc/encoding.csc
index 7da5370..f6a003d 100644
--- a/csc/encoding.csc
+++ b/csc/encoding.csc
@@ -164,8 +164,9 @@
(arg->le-bytes w word)
(arg->le-bytes w (list 'local ptr))
(arg->le-bytes w offset))
- (('exit)
- (make-opcode w 13 #f #f))
+ (('exit code)
+ (make-opcode w 13 (is-const? code) #f)
+ (arg->le-bytes w code))
(_ (error "invalid opcode" opcode))))
diff --git a/csc/linker.csc b/csc/linker.csc
index 9095647..a2ab447 100644
--- a/csc/linker.csc
+++ b/csc/linker.csc
@@ -38,8 +38,6 @@
unless (symbol=? op 'label)
collect (cons op (loop for arg in args
collect (match arg
- (('label 'init)
- (list 'const (lookup label-map -1)))
(('label x)
(list 'const (lookup label-map x)))
(_ arg))))))
@@ -50,12 +48,9 @@
for i from offset
with m = (make-map compare-numbers)
do (match op
- (('label 'init)
- (set! m (insert m -1 i))
- (set! i (- i 1))) ; Labels will be removed later.
(('label id)
(set! m (insert m id i))
- (set! i (- i 1))))
+ (set! i (- i 1)))) ; Labels will be removed later.
finally (return m)))
@@ -103,12 +98,9 @@
(translate-globals
(loop for prog in programs
for off = 0 then (+ off (length converted-prog))
- for converted-prog = (let ((prog-prelude (cons
- (list 'jmp '(label init))
- prog)))
- (translate-labels
- prog-prelude
- (make-label-map off prog-prelude)))
+ for converted-prog = (translate-labels
+ prog
+ (make-label-map off prog))
append converted-prog)
environment))
((programs)