2009-03-28 17:39:04 -04:00
|
|
|
; -*- scheme -*-
|
|
|
|
|
2009-05-31 17:06:04 -04:00
|
|
|
(define Instructions
|
|
|
|
(let ((e (table))
|
|
|
|
(keys
|
|
|
|
[:nop :dup :pop :call :tcall :jmp :brf :brt :jmp.l :brf.l :brt.l :ret
|
|
|
|
|
|
|
|
:eq? :eqv? :equal? :atom? :not :null? :boolean? :symbol?
|
|
|
|
:number? :bound? :pair? :builtin? :vector? :fixnum? :function?
|
|
|
|
|
|
|
|
:cons :list :car :cdr :set-car! :set-cdr!
|
|
|
|
:apply
|
|
|
|
|
|
|
|
:+ :- :* :/ :div0 := :< :compare
|
|
|
|
|
|
|
|
:vector :aref :aset!
|
|
|
|
|
2009-06-06 17:15:54 -04:00
|
|
|
:loadt :loadf :loadnil :load0 :load1 :loadi8
|
|
|
|
:loadv :loadv.l
|
|
|
|
:loadg :loadg.l
|
|
|
|
:loada :loada.l :loadc :loadc.l
|
|
|
|
:setg :setg.l
|
|
|
|
:seta :seta.l :setc :setc.l
|
2009-05-31 17:06:04 -04:00
|
|
|
|
|
|
|
:closure :argc :vargc :trycatch :copyenv :let :for :tapply
|
2009-06-06 17:15:54 -04:00
|
|
|
:add2 :sub2 :neg :largc :lvargc
|
2009-06-07 23:22:32 -04:00
|
|
|
:loada0 :loada1 :loadc00 :loadc01
|
2009-05-31 17:06:04 -04:00
|
|
|
|
|
|
|
dummy_t dummy_f dummy_nil]))
|
2009-03-28 17:39:04 -04:00
|
|
|
(for 0 (1- (length keys))
|
|
|
|
(lambda (i)
|
2009-05-31 17:06:04 -04:00
|
|
|
(put! e (aref keys i) i)))))
|
2009-04-01 22:22:38 -04:00
|
|
|
|
|
|
|
(define arg-counts
|
|
|
|
(table :eq? 2 :eqv? 2
|
|
|
|
:equal? 2 :atom? 1
|
|
|
|
:not 1 :null? 1
|
|
|
|
:boolean? 1 :symbol? 1
|
|
|
|
:number? 1 :bound? 1
|
|
|
|
:pair? 1 :builtin? 1
|
|
|
|
:vector? 1 :fixnum? 1
|
|
|
|
:cons 2 :car 1
|
|
|
|
:cdr 1 :set-car! 2
|
2009-05-11 19:55:40 -04:00
|
|
|
:set-cdr! 2 := 2
|
2009-04-20 20:56:05 -04:00
|
|
|
:< 2 :compare 2
|
2009-05-13 21:30:25 -04:00
|
|
|
:aref 2 :aset! 3
|
2009-05-14 13:54:59 -04:00
|
|
|
:div0 2))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
|
|
|
(define (make-code-emitter) (vector () (table) 0))
|
2009-06-06 17:15:54 -04:00
|
|
|
(define (bcode:code b) (aref b 0))
|
|
|
|
(define (bcode:ctable b) (aref b 1))
|
|
|
|
(define (bcode:nconst b) (aref b 2))
|
|
|
|
; get an index for a referenced value in a bytecode object
|
|
|
|
(define (bcode:indexfor b v)
|
|
|
|
(let ((const-to-idx (bcode:ctable b))
|
|
|
|
(nconst (bcode:nconst b)))
|
|
|
|
(if (has? const-to-idx v)
|
|
|
|
(get const-to-idx v)
|
|
|
|
(begin (put! const-to-idx v nconst)
|
|
|
|
(prog1 nconst
|
|
|
|
(aset! b 2 (+ nconst 1)))))))
|
2009-03-28 17:39:04 -04:00
|
|
|
(define (emit e inst . args)
|
2009-06-10 20:34:50 -04:00
|
|
|
(if (null? args)
|
|
|
|
(aset! e 0 (cons inst (aref e 0)))
|
|
|
|
(begin
|
|
|
|
(if (memq inst '(:loadv :loadg :setg))
|
|
|
|
(set! args (list (bcode:indexfor e (car args)))))
|
|
|
|
(let ((longform
|
|
|
|
(assq inst '((:loadv :loadv.l) (:loadg :loadg.l) (:setg :setg.l)
|
|
|
|
(:loada :loada.l) (:seta :seta.l)))))
|
|
|
|
(if (and longform
|
|
|
|
(> (car args) 255))
|
|
|
|
(set! inst (cadr longform))))
|
|
|
|
(let ((longform
|
|
|
|
(assq inst '((:loadc :loadc.l) (:setc :setc.l)))))
|
|
|
|
(if (and longform
|
|
|
|
(or (> (car args) 255)
|
|
|
|
(> (cadr args) 255)))
|
|
|
|
(set! inst (cadr longform))))
|
|
|
|
(if (eq? inst :loada)
|
|
|
|
(cond ((equal? args '(0))
|
|
|
|
(set! inst :loada0)
|
|
|
|
(set! args ()))
|
|
|
|
((equal? args '(1))
|
|
|
|
(set! inst :loada1)
|
|
|
|
(set! args ()))))
|
|
|
|
(if (eq? inst :loadc)
|
|
|
|
(cond ((equal? args '(0 0))
|
|
|
|
(set! inst :loadc00)
|
|
|
|
(set! args ()))
|
|
|
|
((equal? args '(0 1))
|
|
|
|
(set! inst :loadc01)
|
|
|
|
(set! args ()))))
|
|
|
|
(aset! e 0 (nreconc (cons inst args) (aref e 0)))))
|
2009-03-28 17:39:04 -04:00
|
|
|
e)
|
|
|
|
|
|
|
|
(define (make-label e) (gensym))
|
|
|
|
(define (mark-label e l) (emit e :label l))
|
|
|
|
|
|
|
|
; convert symbolic bytecode representation to a byte array.
|
|
|
|
; labels are fixed-up.
|
|
|
|
(define (encode-byte-code e)
|
2009-05-31 17:06:04 -04:00
|
|
|
(let* ((cl (reverse! e))
|
2009-06-06 17:15:54 -04:00
|
|
|
(v (list->vector cl))
|
2009-06-10 20:34:50 -04:00
|
|
|
(long? (>= (+ (length v) ; 1 byte for each entry, plus...
|
|
|
|
; at most half the entries in this vector can be
|
|
|
|
; instructions accepting 32-bit arguments
|
|
|
|
(* 3 (div0 (length v) 2))
|
|
|
|
#;(* 3 (count (lambda (i)
|
2009-06-06 17:15:54 -04:00
|
|
|
(memq i '(:loadv.l :loadg.l :setg.l
|
|
|
|
:loada.l :seta.l :loadc.l
|
|
|
|
:setc.l :jmp :brt :brf
|
|
|
|
:largc :lvargc)))
|
2009-03-28 19:46:02 -04:00
|
|
|
cl)))
|
2009-06-06 17:15:54 -04:00
|
|
|
65536)))
|
2009-03-28 17:39:04 -04:00
|
|
|
(let ((n (length v))
|
|
|
|
(i 0)
|
|
|
|
(label-to-loc (table))
|
|
|
|
(fixup-to-label (table))
|
|
|
|
(bcode (buffer))
|
2009-06-10 20:34:50 -04:00
|
|
|
(vi #f)
|
|
|
|
(nxt #f))
|
2009-06-27 19:07:22 -04:00
|
|
|
(io.write bcode #int32(0))
|
2009-03-28 17:39:04 -04:00
|
|
|
(while (< i n)
|
|
|
|
(begin
|
|
|
|
(set! vi (aref v i))
|
|
|
|
(if (eq? vi :label)
|
|
|
|
(begin (put! label-to-loc (aref v (+ i 1)) (sizeof bcode))
|
|
|
|
(set! i (+ i 2)))
|
|
|
|
(begin
|
2009-03-28 19:46:02 -04:00
|
|
|
(io.write bcode
|
|
|
|
(byte
|
|
|
|
(get Instructions
|
2009-06-10 20:34:50 -04:00
|
|
|
(if long?
|
2009-03-28 19:46:02 -04:00
|
|
|
(case vi
|
|
|
|
(:jmp :jmp.l)
|
|
|
|
(:brt :brt.l)
|
2009-06-10 20:34:50 -04:00
|
|
|
(:brf :brf.l)
|
|
|
|
(else vi))
|
2009-03-28 19:46:02 -04:00
|
|
|
vi))))
|
2009-03-28 17:39:04 -04:00
|
|
|
(set! i (+ i 1))
|
2009-06-10 20:34:50 -04:00
|
|
|
(set! nxt (if (< i n) (aref v i) #f))
|
|
|
|
(cond ((memq vi '(:jmp :brf :brt))
|
|
|
|
(put! fixup-to-label (sizeof bcode) nxt)
|
2009-06-14 22:25:21 -04:00
|
|
|
(io.write bcode ((if long? int32 int16) 0))
|
2009-06-10 20:34:50 -04:00
|
|
|
(set! i (+ i 1)))
|
|
|
|
((number? nxt)
|
|
|
|
(case vi
|
|
|
|
((:loadv.l :loadg.l :setg.l :loada.l :seta.l
|
|
|
|
:largc :lvargc)
|
2009-06-14 22:25:21 -04:00
|
|
|
(io.write bcode (int32 nxt))
|
2009-06-10 20:34:50 -04:00
|
|
|
(set! i (+ i 1)))
|
|
|
|
|
|
|
|
((:loadc :setc) ; 2 uint8 args
|
|
|
|
(io.write bcode (uint8 nxt))
|
|
|
|
(set! i (+ i 1))
|
|
|
|
(io.write bcode (uint8 (aref v i)))
|
|
|
|
(set! i (+ i 1)))
|
|
|
|
|
2009-06-14 22:25:21 -04:00
|
|
|
((:loadc.l :setc.l) ; 2 int32 args
|
|
|
|
(io.write bcode (int32 nxt))
|
2009-06-10 20:34:50 -04:00
|
|
|
(set! i (+ i 1))
|
2009-06-14 22:25:21 -04:00
|
|
|
(io.write bcode (int32 (aref v i)))
|
2009-06-10 20:34:50 -04:00
|
|
|
(set! i (+ i 1)))
|
|
|
|
|
|
|
|
(else
|
|
|
|
; other number arguments are always uint8
|
|
|
|
(io.write bcode (uint8 nxt))
|
|
|
|
(set! i (+ i 1)))))
|
|
|
|
(else #f))))))
|
2009-06-06 17:15:54 -04:00
|
|
|
|
2009-03-28 17:39:04 -04:00
|
|
|
(table.foreach
|
|
|
|
(lambda (addr labl)
|
|
|
|
(begin (io.seek bcode addr)
|
2009-06-14 22:25:21 -04:00
|
|
|
(io.write bcode ((if long? int32 int16)
|
|
|
|
(- (get label-to-loc labl)
|
|
|
|
addr)))))
|
2009-03-28 17:39:04 -04:00
|
|
|
fixup-to-label)
|
|
|
|
(io.tostring! bcode))))
|
|
|
|
|
|
|
|
(define (const-to-idx-vec e)
|
2009-06-06 17:15:54 -04:00
|
|
|
(let ((cvec (vector.alloc (bcode:nconst e))))
|
2009-04-26 18:19:32 -04:00
|
|
|
(table.foreach (lambda (val idx) (aset! cvec idx val))
|
2009-06-06 17:15:54 -04:00
|
|
|
(bcode:ctable e))
|
2009-04-26 18:19:32 -04:00
|
|
|
cvec))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
|
|
|
(define (index-of item lst start)
|
|
|
|
(cond ((null? lst) #f)
|
2009-04-26 18:19:32 -04:00
|
|
|
((eq? item (car lst)) start)
|
|
|
|
(else (index-of item (cdr lst) (+ start 1)))))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-04-26 18:19:32 -04:00
|
|
|
(define (in-env? s env) (any (lambda (e) (memq s e)) env))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
|
|
|
(define (lookup-sym s env lev arg?)
|
|
|
|
(if (null? env)
|
|
|
|
'(global)
|
|
|
|
(let* ((curr (car env))
|
|
|
|
(i (index-of s curr 0)))
|
|
|
|
(if i
|
|
|
|
(if arg?
|
|
|
|
`(arg ,i)
|
|
|
|
`(closed ,lev ,i))
|
|
|
|
(lookup-sym s
|
|
|
|
(cdr env)
|
2009-04-14 20:12:01 -04:00
|
|
|
(if (or arg? (null? curr)) lev (+ lev 1))
|
2009-03-28 17:39:04 -04:00
|
|
|
#f)))))
|
|
|
|
|
2009-04-01 22:22:38 -04:00
|
|
|
(define (compile-sym g env s Is)
|
2009-04-14 20:12:01 -04:00
|
|
|
(let ((loc (lookup-sym s env 0 #t)))
|
2009-03-28 17:39:04 -04:00
|
|
|
(case (car loc)
|
|
|
|
(arg (emit g (aref Is 0) (cadr loc)))
|
|
|
|
(closed (emit g (aref Is 1) (cadr loc) (caddr loc)))
|
|
|
|
(else (emit g (aref Is 2) s)))))
|
|
|
|
|
2009-04-01 22:22:38 -04:00
|
|
|
(define (compile-if g env tail? x)
|
2009-03-28 17:39:04 -04:00
|
|
|
(let ((elsel (make-label g))
|
2009-05-30 13:04:34 -04:00
|
|
|
(endl (make-label g))
|
|
|
|
(test (cadr x))
|
|
|
|
(then (caddr x))
|
|
|
|
(else (if (pair? (cdddr x))
|
|
|
|
(cadddr x)
|
|
|
|
#f)))
|
|
|
|
(cond ((eq? test #t)
|
|
|
|
(compile-in g env tail? then))
|
|
|
|
((eq? test #f)
|
|
|
|
(compile-in g env tail? else))
|
|
|
|
(else
|
|
|
|
(compile-in g env #f test)
|
|
|
|
(emit g :brf elsel)
|
|
|
|
(compile-in g env tail? then)
|
|
|
|
(if tail?
|
|
|
|
(emit g :ret)
|
|
|
|
(emit g :jmp endl))
|
|
|
|
(mark-label g elsel)
|
|
|
|
(compile-in g env tail? else)
|
|
|
|
(mark-label g endl)))))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-04-01 22:22:38 -04:00
|
|
|
(define (compile-begin g env tail? forms)
|
|
|
|
(cond ((atom? forms) (compile-in g env tail? #f))
|
2009-03-28 17:39:04 -04:00
|
|
|
((atom? (cdr forms))
|
2009-04-01 22:22:38 -04:00
|
|
|
(compile-in g env tail? (car forms)))
|
2009-03-28 17:39:04 -04:00
|
|
|
(else
|
2009-04-01 22:22:38 -04:00
|
|
|
(compile-in g env #f (car forms))
|
2009-03-28 17:39:04 -04:00
|
|
|
(emit g :pop)
|
2009-04-01 22:22:38 -04:00
|
|
|
(compile-begin g env tail? (cdr forms)))))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-04-01 22:22:38 -04:00
|
|
|
(define (compile-prog1 g env x)
|
|
|
|
(compile-in g env #f (cadr x))
|
2009-03-28 17:39:04 -04:00
|
|
|
(if (pair? (cddr x))
|
2009-04-01 22:22:38 -04:00
|
|
|
(begin (compile-begin g env #f (cddr x))
|
2009-03-28 17:39:04 -04:00
|
|
|
(emit g :pop))))
|
|
|
|
|
2009-04-01 22:22:38 -04:00
|
|
|
(define (compile-while g env cond body)
|
2009-03-28 17:39:04 -04:00
|
|
|
(let ((top (make-label g))
|
|
|
|
(end (make-label g)))
|
2009-04-14 20:12:01 -04:00
|
|
|
(compile-in g env #f #f)
|
2009-03-28 17:39:04 -04:00
|
|
|
(mark-label g top)
|
2009-04-01 22:22:38 -04:00
|
|
|
(compile-in g env #f cond)
|
2009-03-28 17:39:04 -04:00
|
|
|
(emit g :brf end)
|
|
|
|
(emit g :pop)
|
2009-04-14 20:12:01 -04:00
|
|
|
(compile-in g env #f body)
|
2009-03-28 17:39:04 -04:00
|
|
|
(emit g :jmp top)
|
|
|
|
(mark-label g end)))
|
|
|
|
|
2009-04-15 23:05:38 -04:00
|
|
|
(define (1arg-lambda? func)
|
|
|
|
(and (pair? func)
|
|
|
|
(eq? (car func) 'lambda)
|
|
|
|
(pair? (cdr func))
|
|
|
|
(pair? (cadr func))
|
|
|
|
(length= (cadr func) 1)))
|
|
|
|
|
|
|
|
(define (compile-for g env lo hi func)
|
|
|
|
(if (1arg-lambda? func)
|
|
|
|
(begin (compile-in g env #f lo)
|
|
|
|
(compile-in g env #f hi)
|
|
|
|
(compile-in g env #f func)
|
|
|
|
(emit g :for))
|
|
|
|
(error "for: third form must be a 1-argument lambda")))
|
|
|
|
|
2009-04-01 22:22:38 -04:00
|
|
|
(define (compile-short-circuit g env tail? forms default branch)
|
|
|
|
(cond ((atom? forms) (compile-in g env tail? default))
|
|
|
|
((atom? (cdr forms)) (compile-in g env tail? (car forms)))
|
2009-03-28 17:39:04 -04:00
|
|
|
(else
|
|
|
|
(let ((end (make-label g)))
|
2009-04-01 22:22:38 -04:00
|
|
|
(compile-in g env #f (car forms))
|
2009-03-28 17:39:04 -04:00
|
|
|
(emit g :dup)
|
2009-04-01 22:22:38 -04:00
|
|
|
(emit g branch end)
|
2009-03-28 17:39:04 -04:00
|
|
|
(emit g :pop)
|
2009-04-01 22:22:38 -04:00
|
|
|
(compile-short-circuit g env tail? (cdr forms) default branch)
|
2009-03-28 17:39:04 -04:00
|
|
|
(mark-label g end)))))
|
|
|
|
|
2009-04-01 22:22:38 -04:00
|
|
|
(define (compile-and g env tail? forms)
|
|
|
|
(compile-short-circuit g env tail? forms #t :brf))
|
|
|
|
(define (compile-or g env tail? forms)
|
|
|
|
(compile-short-circuit g env tail? forms #f :brt))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-04-01 00:31:49 -04:00
|
|
|
(define MAX_ARGS 127)
|
|
|
|
|
|
|
|
(define (list-partition l n)
|
2009-04-26 18:19:32 -04:00
|
|
|
(define (list-part- l n i subl acc)
|
|
|
|
(cond ((atom? l) (if (> i 0)
|
2009-05-31 17:06:04 -04:00
|
|
|
(cons (reverse! subl) acc)
|
2009-04-26 18:19:32 -04:00
|
|
|
acc))
|
2009-05-31 17:06:04 -04:00
|
|
|
((>= i n) (list-part- l n 0 () (cons (reverse! subl) acc)))
|
2009-04-26 18:19:32 -04:00
|
|
|
(else (list-part- (cdr l) n (+ 1 i) (cons (car l) subl) acc))))
|
2009-04-01 00:31:49 -04:00
|
|
|
(if (<= n 0)
|
|
|
|
(error "list-partition: invalid count")
|
2009-05-31 17:06:04 -04:00
|
|
|
(reverse! (list-part- l n 0 () ()))))
|
2009-04-01 00:31:49 -04:00
|
|
|
|
|
|
|
(define (just-compile-args g lst env)
|
2009-03-28 17:39:04 -04:00
|
|
|
(for-each (lambda (a)
|
2009-04-01 22:22:38 -04:00
|
|
|
(compile-in g env #f a))
|
2009-03-28 17:39:04 -04:00
|
|
|
lst))
|
|
|
|
|
2009-04-01 22:22:38 -04:00
|
|
|
(define (compile-arglist g env lst)
|
2009-04-01 00:31:49 -04:00
|
|
|
(let ((argtail (length> lst MAX_ARGS)))
|
|
|
|
(if argtail
|
|
|
|
(begin (just-compile-args g (list-head lst MAX_ARGS) env)
|
|
|
|
(let ((rest
|
|
|
|
(cons nconc
|
|
|
|
(map (lambda (l) (cons list l))
|
|
|
|
(list-partition argtail MAX_ARGS)))))
|
2009-04-01 22:22:38 -04:00
|
|
|
(compile-in g env #f rest))
|
2009-04-01 00:31:49 -04:00
|
|
|
(+ MAX_ARGS 1))
|
|
|
|
(begin (just-compile-args g lst env)
|
|
|
|
(length lst)))))
|
|
|
|
|
2009-04-09 00:04:27 -04:00
|
|
|
(define (argc-error head count)
|
|
|
|
(error (string "compile error: " head " expects " count
|
|
|
|
(if (= count 1)
|
|
|
|
" argument."
|
|
|
|
" arguments."))))
|
2009-04-16 17:20:15 -04:00
|
|
|
|
2009-04-01 22:22:38 -04:00
|
|
|
(define (compile-app g env tail? x)
|
2009-04-16 17:20:15 -04:00
|
|
|
(let ((head (car x)))
|
|
|
|
(if (and (pair? head)
|
|
|
|
(eq? (car head) 'lambda)
|
2009-06-06 17:15:54 -04:00
|
|
|
(list? (cadr head))
|
|
|
|
(not (length> (cadr head) MAX_ARGS)))
|
2009-04-16 17:20:15 -04:00
|
|
|
(compile-let g env tail? x)
|
|
|
|
(compile-call g env tail? x))))
|
|
|
|
|
|
|
|
(define (compile-let g env tail? x)
|
|
|
|
(let ((head (car x))
|
|
|
|
(args (cdr x)))
|
|
|
|
(unless (length= args (length (cadr head)))
|
|
|
|
(error (string "apply: incorrect number of arguments to " head)))
|
|
|
|
(emit g :loadv (compile-f env head #t))
|
|
|
|
(let ((nargs (compile-arglist g env args)))
|
2009-05-05 19:51:13 -04:00
|
|
|
(emit g :copyenv)
|
2009-04-16 17:20:15 -04:00
|
|
|
(emit g (if tail? :tcall :call) (+ 1 nargs)))))
|
|
|
|
|
2009-06-14 22:25:21 -04:00
|
|
|
(define builtin->instruction
|
|
|
|
(let ((b2i (table number? :number? cons :cons
|
|
|
|
fixnum? :fixnum? equal? :equal?
|
|
|
|
eq? :eq? symbol? :symbol?
|
|
|
|
div0 :div0 builtin? :builtin?
|
|
|
|
aset! :aset! - :- boolean? :boolean? not :not
|
|
|
|
apply :apply atom? :atom?
|
|
|
|
set-cdr! :set-cdr! / :/
|
|
|
|
function? :function? vector :vector
|
|
|
|
list :list bound? :bound?
|
|
|
|
< :< * :* cdr :cdr null? :null?
|
|
|
|
+ :+ eqv? :eqv? compare :compare aref :aref
|
|
|
|
set-car! :set-car! car :car
|
|
|
|
pair? :pair? = := vector? :vector?)))
|
|
|
|
(lambda (b)
|
|
|
|
(get b2i b #f))))
|
2009-04-26 18:19:32 -04:00
|
|
|
|
2009-04-16 17:20:15 -04:00
|
|
|
(define (compile-call g env tail? x)
|
2009-04-01 00:31:49 -04:00
|
|
|
(let ((head (car x)))
|
2009-03-28 17:39:04 -04:00
|
|
|
(let ((head
|
|
|
|
(if (and (symbol? head)
|
|
|
|
(not (in-env? head env))
|
|
|
|
(bound? head)
|
|
|
|
(constant? head)
|
2009-04-17 10:41:15 -04:00
|
|
|
(builtin? (top-level-value head)))
|
|
|
|
(top-level-value head)
|
2009-03-28 17:39:04 -04:00
|
|
|
head)))
|
|
|
|
(let ((b (and (builtin? head)
|
|
|
|
(builtin->instruction head))))
|
|
|
|
(if (not b)
|
2009-04-01 22:22:38 -04:00
|
|
|
(compile-in g env #f head))
|
|
|
|
(let ((nargs (compile-arglist g env (cdr x))))
|
|
|
|
(if b
|
|
|
|
(let ((count (get arg-counts b #f)))
|
|
|
|
(if (and count
|
|
|
|
(not (length= (cdr x) count)))
|
2009-04-09 00:04:27 -04:00
|
|
|
(argc-error head count))
|
|
|
|
(case b ; handle special cases of vararg builtins
|
|
|
|
(:list (if (= nargs 0) (emit g :loadnil) (emit g b nargs)))
|
2009-04-26 18:19:32 -04:00
|
|
|
(:+ (cond ((= nargs 0) (emit g :load0))
|
|
|
|
((= nargs 2) (emit g :add2))
|
|
|
|
(else (emit g b nargs))))
|
|
|
|
(:- (cond ((= nargs 0) (argc-error head 1))
|
|
|
|
((= nargs 1) (emit g :neg))
|
|
|
|
((= nargs 2) (emit g :sub2))
|
|
|
|
(else (emit g b nargs))))
|
2009-04-09 00:04:27 -04:00
|
|
|
(:* (if (= nargs 0) (emit g :load1)
|
2009-04-21 10:53:18 -04:00
|
|
|
(emit g b nargs)))
|
2009-04-09 00:04:27 -04:00
|
|
|
(:/ (if (= nargs 0)
|
|
|
|
(argc-error head 1)
|
|
|
|
(emit g b nargs)))
|
2009-04-20 20:56:05 -04:00
|
|
|
(:vector (if (= nargs 0)
|
|
|
|
(emit g :loadv [])
|
|
|
|
(emit g b nargs)))
|
2009-05-11 19:55:40 -04:00
|
|
|
(:apply (if (< nargs 2)
|
|
|
|
(argc-error head 2)
|
|
|
|
(emit g (if tail? :tapply :apply) nargs)))
|
|
|
|
(else (emit g b))))
|
2009-04-01 22:22:38 -04:00
|
|
|
(emit g (if tail? :tcall :call) nargs)))))))
|
|
|
|
|
2009-04-26 23:21:53 -04:00
|
|
|
(define (fits-i8 x) (and (fixnum? x) (>= x -128) (<= x 127)))
|
|
|
|
|
2009-04-01 22:22:38 -04:00
|
|
|
(define (compile-in g env tail? x)
|
|
|
|
(cond ((symbol? x) (compile-sym g env x [:loada :loadc :loadg]))
|
2009-03-28 17:39:04 -04:00
|
|
|
((atom? x)
|
2009-04-26 23:21:53 -04:00
|
|
|
(cond ((eq? x 0) (emit g :load0))
|
|
|
|
((eq? x 1) (emit g :load1))
|
|
|
|
((eq? x #t) (emit g :loadt))
|
|
|
|
((eq? x #f) (emit g :loadf))
|
|
|
|
((eq? x ()) (emit g :loadnil))
|
|
|
|
((fits-i8 x) (emit g :loadi8 x))
|
|
|
|
(else (emit g :loadv x))))
|
2009-03-28 17:39:04 -04:00
|
|
|
(else
|
|
|
|
(case (car x)
|
|
|
|
(quote (emit g :loadv (cadr x)))
|
2009-04-01 22:22:38 -04:00
|
|
|
(if (compile-if g env tail? x))
|
|
|
|
(begin (compile-begin g env tail? (cdr x)))
|
2009-04-14 20:12:01 -04:00
|
|
|
(prog1 (compile-prog1 g env x))
|
2009-04-01 22:22:38 -04:00
|
|
|
(lambda (begin (emit g :loadv (compile-f env x))
|
2009-03-28 17:39:04 -04:00
|
|
|
(emit g :closure)))
|
2009-04-01 22:22:38 -04:00
|
|
|
(and (compile-and g env tail? (cdr x)))
|
|
|
|
(or (compile-or g env tail? (cdr x)))
|
2009-04-14 20:12:01 -04:00
|
|
|
(while (compile-while g env (cadr x) (cons 'begin (cddr x))))
|
2009-04-15 23:05:38 -04:00
|
|
|
(for (compile-for g env (cadr x) (caddr x) (cadddr x)))
|
2009-04-26 18:19:32 -04:00
|
|
|
(return (compile-in g env #t (cadr x))
|
|
|
|
(emit g :ret))
|
2009-04-01 22:22:38 -04:00
|
|
|
(set! (compile-in g env #f (caddr x))
|
|
|
|
(compile-sym g env (cadr x) [:seta :setc :setg]))
|
|
|
|
(trycatch (compile-in g env #f `(lambda () ,(cadr x)))
|
2009-04-15 23:05:38 -04:00
|
|
|
(unless (1arg-lambda? (caddr x))
|
|
|
|
(error "trycatch: second form must be a 1-argument lambda"))
|
2009-04-01 22:22:38 -04:00
|
|
|
(compile-in g env #f (caddr x))
|
2009-03-28 17:39:04 -04:00
|
|
|
(emit g :trycatch))
|
2009-04-01 22:22:38 -04:00
|
|
|
(else (compile-app g env tail? x))))))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-04-16 17:20:15 -04:00
|
|
|
(define (compile-f env f . let?)
|
2009-04-09 00:04:27 -04:00
|
|
|
(let ((g (make-code-emitter))
|
|
|
|
(args (cadr f)))
|
2009-06-06 17:15:54 -04:00
|
|
|
(cond ((not (null? let?)) (emit g :let))
|
|
|
|
((length> args MAX_ARGS) (emit g (if (null? (lastcdr args))
|
|
|
|
:largc :lvargc)
|
|
|
|
(length args)))
|
|
|
|
((null? (lastcdr args)) (emit g :argc (length args)))
|
2009-04-16 17:20:15 -04:00
|
|
|
(else (emit g :vargc (if (atom? args) 0 (length args)))))
|
2009-04-09 00:04:27 -04:00
|
|
|
(compile-in g (cons (to-proper args) env) #t (caddr f))
|
2009-03-28 17:39:04 -04:00
|
|
|
(emit g :ret)
|
2009-06-06 17:15:54 -04:00
|
|
|
(function (encode-byte-code (bcode:code g))
|
2009-07-10 00:17:39 -04:00
|
|
|
(const-to-idx-vec g) (lastcdr f))))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-04-14 20:12:01 -04:00
|
|
|
(define (compile f) (compile-f () f))
|
|
|
|
|
|
|
|
(define (compile-thunk expr) (compile `(lambda () ,expr)))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-06-14 22:25:21 -04:00
|
|
|
(define (ref-int32-LE a i)
|
|
|
|
(int32 (+ (ash (aref a (+ i 0)) 0)
|
|
|
|
(ash (aref a (+ i 1)) 8)
|
|
|
|
(ash (aref a (+ i 2)) 16)
|
|
|
|
(ash (aref a (+ i 3)) 24))))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-06-14 22:25:21 -04:00
|
|
|
(define (ref-int16-LE a i)
|
|
|
|
(int16 (+ (ash (aref a (+ i 0)) 0)
|
|
|
|
(ash (aref a (+ i 1)) 8))))
|
2009-03-28 19:46:02 -04:00
|
|
|
|
2009-03-28 17:39:04 -04:00
|
|
|
(define (hex5 n)
|
2009-05-06 22:10:52 -04:00
|
|
|
(string.lpad (number->string n 16) 5 #\0))
|
2009-03-28 17:39:04 -04:00
|
|
|
|
2009-04-26 18:19:32 -04:00
|
|
|
(define (disassemble f . lev?)
|
|
|
|
(if (null? lev?)
|
|
|
|
(begin (disassemble f 0)
|
|
|
|
(newline)
|
|
|
|
(return #t)))
|
2009-05-12 21:13:40 -04:00
|
|
|
(let ((lev (car lev?))
|
|
|
|
(code (function:code f))
|
|
|
|
(vals (function:vals f)))
|
|
|
|
(define (print-val v)
|
|
|
|
(if (and (function? v) (not (builtin? v)))
|
|
|
|
(begin (princ "\n")
|
|
|
|
(disassemble v (+ lev 1)))
|
|
|
|
(print v)))
|
2009-06-27 19:07:22 -04:00
|
|
|
(dotimes (xx lev) (princ "\t"))
|
|
|
|
(princ "maxstack " (ref-int32-LE code 0) "\n")
|
|
|
|
(let ((i 4)
|
2009-05-12 21:13:40 -04:00
|
|
|
(N (length code)))
|
|
|
|
(while (< i N)
|
|
|
|
; find key whose value matches the current byte
|
|
|
|
(let ((inst (table.foldl (lambda (k v z)
|
|
|
|
(or z (and (eq? v (aref code i))
|
|
|
|
k)))
|
|
|
|
#f Instructions)))
|
2009-06-27 19:07:22 -04:00
|
|
|
(if (> i 4) (newline))
|
2009-05-12 21:13:40 -04:00
|
|
|
(dotimes (xx lev) (princ "\t"))
|
2009-06-27 19:07:22 -04:00
|
|
|
(princ (hex5 (- i 4)) ": "
|
2009-05-12 21:13:40 -04:00
|
|
|
(string.tail (string inst) 1) "\t")
|
|
|
|
(set! i (+ i 1))
|
|
|
|
(case inst
|
|
|
|
((:loadv.l :loadg.l :setg.l)
|
2009-06-14 22:25:21 -04:00
|
|
|
(print-val (aref vals (ref-int32-LE code i)))
|
2009-05-12 21:13:40 -04:00
|
|
|
(set! i (+ i 4)))
|
|
|
|
|
|
|
|
((:loadv :loadg :setg)
|
|
|
|
(print-val (aref vals (aref code i)))
|
|
|
|
(set! i (+ i 1)))
|
|
|
|
|
|
|
|
((:loada :seta :call :tcall :list :+ :- :* :/ :vector
|
2009-05-30 13:54:27 -04:00
|
|
|
:argc :vargc :loadi8 :apply :tapply)
|
2009-05-12 21:13:40 -04:00
|
|
|
(princ (number->string (aref code i)))
|
|
|
|
(set! i (+ i 1)))
|
|
|
|
|
2009-06-06 17:15:54 -04:00
|
|
|
((:loada.l :seta.l :largc :lvargc)
|
2009-06-14 22:25:21 -04:00
|
|
|
(princ (number->string (ref-int32-LE code i)))
|
2009-06-06 17:15:54 -04:00
|
|
|
(set! i (+ i 4)))
|
|
|
|
|
2009-05-12 21:13:40 -04:00
|
|
|
((:loadc :setc)
|
|
|
|
(princ (number->string (aref code i)) " ")
|
|
|
|
(set! i (+ i 1))
|
|
|
|
(princ (number->string (aref code i)))
|
|
|
|
(set! i (+ i 1)))
|
|
|
|
|
2009-06-06 17:15:54 -04:00
|
|
|
((:loadc.l :setc.l)
|
2009-06-14 22:25:21 -04:00
|
|
|
(princ (number->string (ref-int32-LE code i)) " ")
|
2009-06-06 17:15:54 -04:00
|
|
|
(set! i (+ i 4))
|
2009-06-14 22:25:21 -04:00
|
|
|
(princ (number->string (ref-int32-LE code i)))
|
2009-06-06 17:15:54 -04:00
|
|
|
(set! i (+ i 4)))
|
|
|
|
|
2009-05-12 21:13:40 -04:00
|
|
|
((:jmp :brf :brt)
|
2009-07-08 01:53:29 -04:00
|
|
|
(princ "@" (hex5 (+ i -4 (ref-int16-LE code i))))
|
2009-05-12 21:13:40 -04:00
|
|
|
(set! i (+ i 2)))
|
|
|
|
|
|
|
|
((:jmp.l :brf.l :brt.l)
|
2009-07-08 01:53:29 -04:00
|
|
|
(princ "@" (hex5 (+ i -4 (ref-int32-LE code i))))
|
2009-05-12 21:13:40 -04:00
|
|
|
(set! i (+ i 4)))
|
|
|
|
|
|
|
|
(else #f)))))))
|
2009-04-19 18:22:17 -04:00
|
|
|
|
2009-03-28 17:39:04 -04:00
|
|
|
#t
|