1 ;;; definition of ast types
4 extender: define-type-of-ast
8 (define (link-parent! subast parent)
9 (ast-parent-set! subast parent)
12 (define (multi-link-parent! subasts parent)
13 (for-each (lambda (subast) (link-parent! subast parent))
17 (define (unlink-parent! subast)
18 (let ((parent (ast-parent subast)))
19 (if (and (def-variable? subast) (def-procedure? parent))
20 (def-procedure-params-set!
22 (remove subast (def-procedure-params parent)))
25 (remove subast (ast-subasts parent))))
26 (ast-parent-set! subast #f)
29 (define (subast1 ast) (car (ast-subasts ast)))
30 (define (subast2 ast) (cadr (ast-subasts ast)))
31 (define (subast3 ast) (caddr (ast-subasts ast)))
32 (define (subast4 ast) (cadddr (ast-subasts ast)))
34 (define-type-of-ast def
35 extender: define-type-of-def
42 (define (new-value bytes)
45 (define byte-cell-counter 0)
46 (define (byte-cell-next-id) (let ((id byte-cell-counter))
47 (set! byte-cell-counter (+ id 1))
49 (define-type byte-cell
52 (interferes-with unprintable:) ; these 2 are stored as sets
53 (coalesceable-with unprintable:))
54 (define (new-byte-cell)
55 (let ((id (byte-cell-next-id)))
56 (make-byte-cell id (if allocate-registers? #f id)
57 (new-empty-set) (new-empty-set))))
58 (define (get-register n) ;; TODO will these byte cells be used for register allocation ? do they need an id ?
59 (make-byte-cell (byte-cell-next-id) n (new-empty-set) (new-empty-set)))
63 (define (new-byte-lit x)
76 (define (type->bytes type)
77 (cond ((assq type types-bytes)
78 => (lambda (x) (cdr x)))
79 (else (error "wrong number of bytes ?"))))
81 (define (bytes->type n)
82 (let loop ((l types-bytes))
83 (cond ((null? l) (error (string-append "no type contains "
86 ((= n (cdar l)) (caar l))
87 (else (loop (cdr l))))))
89 (define (int->value n type)
90 (let ((len (type->bytes type)))
91 (let loop ((len len) (n n) (rev-bytes '()))
93 (new-value (reverse rev-bytes))
95 (arithmetic-shift n -8)
96 (cons (new-byte-lit (modulo n 256))
98 (define (value->int val)
99 (let loop ((bytes (reverse (value-bytes val)))
104 (+ (* 256 n) (byte-lit-val (car bytes)))))))
106 ;; TODO instead of carrying types around, use the length instead
107 (define (extend value type)
108 ;; literals must be extended with literal 0s, while variables must be
109 ;; extended with byte cells
110 (let* ((bytes (value-bytes value))
111 (lit? (byte-lit? (car bytes))))
112 (let loop ((rev-bytes (reverse bytes))
113 (n (max 0 (- (type->bytes type) (length bytes)))))
115 (new-value (reverse rev-bytes))
116 (loop (cons (if lit? (new-byte-lit 0) (new-byte-cell))
120 (define (alloc-value type)
121 (let ((len (type->bytes type)))
122 (let loop ((len len) (rev-bytes '()))
124 (new-value (reverse rev-bytes))
126 (cons (new-byte-cell)
129 (define-type-of-def def-variable
134 (define (new-def-variable subasts id refs type value sets)
137 (make-def-variable #f subasts id refs type value sets)))
139 (define-type-of-def def-procedure
144 live-after-calls) ; stored as a set
145 (define (new-def-procedure subasts id refs type value params)
148 (make-def-procedure #f subasts id refs type value params #f (new-empty-set))))
151 (define-type-of-ast expr
152 extender: define-type-of-expr
155 (define-type-of-expr literal
157 (define (new-literal type val)
158 (make-literal #f '() type val))
160 (define-type-of-expr ref
162 (define (new-ref type def)
163 (make-ref #f '() type def))
165 (define-type-of-expr oper
167 (define (new-oper subasts type op)
170 (make-oper #f subasts type op)))
172 (define-type-of-expr call
173 (def-proc unprintable:))
174 (define (new-call subasts type proc-def)
177 (make-call #f subasts type proc-def)))
179 (define-type-of-ast block
180 name) ; blocks that begin with a label have a name, the other have #f
181 (define (new-block subasts)
184 (make-block #f subasts #f)))
185 (define (new-named-block name subasts)
188 (make-block #f subasts name)))
190 (define-type-of-ast if)
191 (define (new-if subasts)
194 (make-if #f subasts)))
196 (define-type-of-ast switch)
197 (define (new-switch subasts)
200 (make-switch #f subasts)))
202 (define-type-of-ast while)
203 (define (new-while subasts)
206 (make-while #f subasts)))
208 (define-type-of-ast do-while)
209 (define (new-do-while subasts)
212 (make-do-while #f subasts)))
214 (define-type-of-ast for)
215 (define (new-for subasts)
218 (make-for #f subasts)))
220 (define-type-of-ast return)
221 (define (new-return subasts)
224 (make-return #f subasts)))
226 (define-type-of-ast break)
230 (define-type-of-ast continue)
231 (define (new-continue)
232 (make-continue #f '()))
234 (define-type-of-ast goto)
235 (define (new-goto label)
236 (make-goto #f (list label)))
238 (define-type-of-ast program)
239 (define (new-program subasts) ;; TODO add support for main
242 (make-program #f subasts)))
245 extender: define-type-of-op
246 (six-id unprintable:)
253 (define-type-of-op op1)
254 (define-type-of-op op2)
255 (define-type-of-op op3)