1 ;;;; File: "picobit.scm", Time-stamp: <2006-05-08 16:04:37 feeley>
3 ;;;; Copyright (C) 2004-2009 by Marc Feeley and Vincent St-Amour
4 ;;;; All Rights Reserved.
7 (proper-tail-calls-set! #f)
11 ;-----------------------------------------------------------------------------
13 (define compiler-error
14 (lambda (msg . others)
15 (display "*** ERROR -- ")
17 (for-each (lambda (x) (display " ") (write x)) others)
21 ;-----------------------------------------------------------------------------
23 (include "utilities.scm")
26 (include "parser.scm")
27 (include "context.scm")
30 (include "encoding.scm")
32 ;-----------------------------------------------------------------------------
34 (define expand-includes
37 (if (eq? (car e) 'include)
40 (with-input-from-file (cadr e) read-all)))
47 (with-input-from-file "library.scm" read-all))
51 (with-input-from-file filename read-all))))
55 (parse-top (cons 'begin toplevel-exprs) global-env)))
59 (mark-needed-global-vars! global-env node))
64 (lambda (defs after-defs)
66 (define make-seq-preparsed
68 (let ((r (make-seq #f exprs)))
69 (for-each (lambda (x) (node-parent-set! x r)) exprs)
72 (define make-call-preparsed
74 (let ((r (make-call #f exprs)))
75 (for-each (lambda (x) (node-parent-set! x r)) exprs)
79 (env-lookup global-env '#%readyq))
81 (list (make-seq-preparsed defs)
83 (list (parse 'value '#%start-first-process global-env)
87 (extract-ids pattern))
92 (has-rest-param? pattern)
95 (env-extend global-env ids r))
97 (make-seq-preparsed after-defs)))
100 (map (lambda (id) (env-lookup new-env id))
102 (node-children-set! r (list body))
103 (node-parent-set! body r)
115 (define extract-parts
118 (not (def? (car lst))))
123 (cont (cons (car lst) d) ad))))))
125 ;------------------------------------------------------------------------------
128 (lambda (hex-filename)
132 (shell-command "gcc -o picobit-vm picobit-vm.c")
133 (shell-command (string-append "./picobit-vm " hex-filename)))
134 (shell-command (string-append "./robot . 1 " hex-filename)))))
136 ;------------------------------------------------------------------------------
140 (let* ((node (parse-file filename))
143 (path-strip-extension filename)
146 (adjust-unmutable-references! node)
148 ; (pp (node->expr node))
150 (let ((ctx (comp-none node (make-init-context))))
151 (let ((prog (linearize (optimize-code (context-code ctx)))))
152 ; (pp (list code: prog env: (context-env ctx)))
153 (assemble prog hex-filename)
154 (execute hex-filename))))))
161 ;------------------------------------------------------------------------------
164 (define (asm-write-hex-file filename)
165 (with-output-to-file filename
168 (define (print-hex n)
169 (display (string-ref "0123456789ABCDEF" n)))
171 (define (print-byte n)
173 (print-hex (quotient n 16))
174 (print-hex (modulo n 16)))
176 (define (print-line type addr bytes)
177 (let ((n (length bytes))
178 (addr-hi (quotient addr 256))
179 (addr-lo (modulo addr 256)))
182 ; (print-byte addr-hi)
183 ; (print-byte addr-lo)
185 (for-each print-byte bytes)
187 (modulo (- (apply + n addr-hi addr-lo type bytes)) 256)))
191 (let loop ((lst (cdr asm-code-stream))
194 (if (not (null? lst))
197 (let ((kind (vector-ref x 0)))
198 (if (not (eq? kind 'LISTING))
199 (compiler-internal-error
200 "asm-write-hex-file, code stream not assembled"))
208 (if (= (modulo pos 8) 0)
211 (- pos (length rev-bytes))
219 (if (not (null? rev-bytes))
221 (- pos (length rev-bytes))
222 (reverse rev-bytes)))
223 (print-line 1 0 '())))))))