3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
22 (define-module (system vm assemble)
23 :use-syntax (system base syntax)
24 :use-module (system il glil)
25 :use-module (system vm core)
26 :use-module (system vm conv)
27 :use-module (ice-9 match)
28 :use-module (ice-9 regex)
29 :use-module (ice-9 common-list)
32 (define (assemble glil env . opts)
33 (codegen (preprocess glil #f) #t))
40 (define-record (<vm-asm> venv glil body))
41 (define-record (<venv> parent nexts closure?))
42 (define-record (<vmod> id))
43 (define-record (<vlink> module name))
44 (define-record (<bytespec> vars bytes meta objs closure?))
48 ;;; Stage 1: Preprocess
51 (define (preprocess x e)
53 (($ <glil-asm> vars body)
54 (let* ((venv (<venv> :parent e :nexts vars.nexts :closure? #f))
55 (body (map (lambda (x) (preprocess x venv)) body)))
56 (<vm-asm> :venv venv :glil x :body body)))
57 (($ <glil-external> op depth index)
67 ;;; Stage 2: Bytecode generation
70 (define (codegen glil toplevel)
72 (($ <vm-asm> venv ($ <glil-asm> vars _) body)
78 (define (push-code! code)
79 (set! stack (cons (code->bytes code) stack)))
80 (define (push-object! x)
81 (cond ((object->code x) => push-code!)
82 (toplevel (dump-object! push-code! x))
84 (let ((i (cond ((object-assoc x object-alist) => cdr)
86 (let ((i (length object-alist)))
87 (set! object-alist (acons x i object-alist))
89 (push-code! `(object-ref ,i))))))
90 (define (current-address)
91 (define (byte-length x)
92 (cond ((string? x) (string-length x))
94 (apply + (map byte-length stack)))
95 (define (generate-code x)
98 (push-object! (codegen x #f))
99 (if venv.closure? (push-code! `(make-closure))))
101 (($ <glil-bind> binds)
104 (let ((name (car v)) (type (cadr v)) (i (caddr v)))
106 ((argument) (make-binding name #f i))
107 ((local) (make-binding name #f (+ vars.nargs i)))
108 ((external) (make-binding name #t i)))))
111 (acons (current-address) bindings binding-alist))))
114 (set! binding-alist (acons (current-address) #f binding-alist)))
116 (($ <glil-source> loc)
117 (set! source-alist (acons (current-address) loc source-alist)))
120 (push-code! '(void)))
125 (($ <glil-argument> op index)
127 (push-code! `(local-ref ,index))
128 (push-code! `(local-set ,index))))
130 (($ <glil-local> op index)
132 (push-code! `(local-ref ,(+ vars.nargs index)))
133 (push-code! `(local-set ,(+ vars.nargs index)))))
135 (($ <glil-external> op depth index)
136 (do ((e venv e.parent)
141 (push-code! `(external-ref ,(+ n index)))
142 (push-code! `(external-set ,(+ n index)))))))
144 (($ <glil-module> op module name)
145 (push-object! (<vlink> :module #f :name name))
147 (push-code! '(variable-ref))
148 (push-code! '(variable-set))))
150 (($ <glil-label> label)
151 (set! label-alist (assq-set! label-alist label (current-address))))
153 (($ <glil-branch> inst label)
154 (set! stack (cons (list inst label) stack)))
156 (($ <glil-call> inst nargs)
157 (if (instruction? inst)
158 (let ((pops (instruction-pops inst)))
160 (push-code! (list inst nargs)))
162 (push-code! (list inst)))
164 (error "Wrong number of arguments:" inst nargs))))
165 (error "Unknown instruction:" inst)))))
168 (for-each generate-code body)
169 (let ((bytes (stack->bytes (reverse! stack) label-alist)))
171 (bytecode->objcode bytes vars.nlocs vars.nexts)
172 (<bytespec> :vars vars :bytes bytes
173 :meta (if (and (null? binding-alist)
174 (null? source-alist))
176 (cons (reverse! binding-alist)
177 (reverse! source-alist)))
178 :objs (let ((objs (map car (reverse! object-alist))))
179 (if (null? objs) #f (list->vector objs)))
180 :closure? venv.closure?)))))))
182 (define (object-assoc x alist)
184 (($ <vlink>) (assoc x alist))
185 (else (assq x alist))))
187 (define (stack->bytes stack label-alist)
188 (let loop ((result '()) (stack stack) (addr 0))
190 (apply string-append (reverse! result))
191 (let ((bytes (car stack)))
193 (let* ((offset (- (assq-ref label-alist (cadr bytes))
195 (n (if (< offset 0) (+ offset 65536) offset)))
196 (set! bytes (code->bytes (list (car bytes)
199 (loop (cons bytes result)
201 (+ addr (string-length bytes)))))))
208 ;; NOTE: undumpped in vm_load.c.
210 (define (dump-object! push-code! x)
213 ((object->code x) => push-code!)
216 (($ <bytespec> vars bytes meta objs closure?)
218 (let ((nargs vars.nargs) (nrest vars.nrest)
219 (nlocs vars.nlocs) (nexts vars.nexts))
221 ((and (< nargs 4) (< nlocs 8) (< nexts 4))
222 ;; 8-bit representation
223 (let ((x (+ (* nargs 64) (* nrest 32) (* nlocs 4) nexts)))
224 (push-code! `(make-int8 ,x))))
225 ((and (< nargs 16) (< nlocs 128) (< nexts 16))
226 ;; 16-bit representation
227 (let ((x (+ (* nargs 4096) (* nrest 2048) (* nlocs 16) nexts)))
228 (push-code! `(make-int16 ,(quotient x 256) ,(modulo x 256)))))
231 (push-code! (object->code nargs))
232 (push-code! (object->code nrest))
233 (push-code! (object->code nlocs))
234 (push-code! (object->code nexts))
235 (push-code! (object->code #f)))))
237 (if objs (dump! objs))
239 (if meta (dump! meta))
241 (push-code! `(load-program ,bytes)))
242 (($ <vlink> module name)
243 ;; FIXME: dump module
244 (push-code! `(link ,(symbol->string name))))
246 (push-code! `(load-module ,id)))
247 ((and ($ integer) ($ exact))
248 (let ((str (do ((n x (quotient n 256))
249 (l '() (cons (modulo n 256) l)))
251 (list->string (map integer->char l))))))
252 (push-code! `(load-integer ,str))))
254 (push-code! `(load-number ,(number->string x))))
256 (push-code! `(load-string ,x)))
258 (push-code! `(load-symbol ,(symbol->string x))))
260 (push-code! `(load-keyword
261 ,(symbol->string (keyword-dash-symbol x)))))
264 (push-code! `(list ,(length x))))
268 (push-code! `(cons)))
270 (for-each dump! (vector->list x))
271 (push-code! `(vector ,(vector-length x))))
273 (error "Cannot dump:" x)))))))