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 name ; to display in the listing
53 (interferes-with unprintable:) ; these 2 are stored as sets
54 (coalesceable-with unprintable:))
55 (define (new-byte-cell #!optional (name #f))
56 (let ((id (byte-cell-next-id)))
57 (make-byte-cell id (if allocate-registers? #f id)
58 (string-append (if name name "__tmp") "$" (number->string id))
59 (new-empty-set) (new-empty-set))))
60 (define (get-register n)
61 (make-byte-cell (byte-cell-next-id) n
62 (symbol->string (cdr (assv n file-reg-names)))
63 (new-empty-set) (new-empty-set)))
67 (define (new-byte-lit x)
80 (define (type->bytes type)
81 (cond ((assq type types-bytes)
82 => (lambda (x) (cdr x)))
83 (else (error "wrong type?"))))
85 (define (bytes->type n)
86 (let loop ((l types-bytes))
87 (cond ((null? l) (error (string-append "no type contains "
90 ((= n (cdar l)) (caar l))
91 (else (loop (cdr l))))))
93 (define (int->value n type)
94 (let ((len (type->bytes type)))
95 (let loop ((len len) (n n) (rev-bytes '()))
97 (new-value (reverse rev-bytes))
99 (arithmetic-shift n -8)
100 (cons (new-byte-lit (modulo n 256))
102 (define (value->int val)
103 (let loop ((bytes (reverse (value-bytes val)))
108 (+ (* 256 n) (byte-lit-val (car bytes)))))))
110 ;; TODO instead of carrying types around, use the length instead
111 (define (extend value type)
112 ;; literals must be extended with literal 0s, while variables must be
113 ;; extended with byte cells
114 (let* ((bytes (value-bytes value))
115 (lit? (byte-lit? (car bytes))))
116 (let loop ((rev-bytes (reverse bytes))
117 (n (max 0 (- (type->bytes type) (length bytes)))))
119 (new-value (reverse rev-bytes))
120 (loop (cons (if lit? (new-byte-lit 0) (new-byte-cell))
124 (define (alloc-value type #!optional (name #f))
125 (let ((len (type->bytes type)))
126 (let loop ((len len) (rev-bytes '()))
128 (new-value rev-bytes) ;; TODO FOO had a reverse, see if it breaks anything, it shouldn't
132 ;; the lsb is 0, and so on
133 (string-append (symbol->string name)
134 (number->string (- len 1)))
138 (define-type-of-def def-variable
143 (define (new-def-variable subasts id refs type value sets)
146 (make-def-variable #f subasts id refs type value sets)))
148 (define-type-of-def def-procedure
153 live-after-calls) ; stored as a set
154 (define (new-def-procedure subasts id refs type value params)
157 (make-def-procedure #f subasts id refs type value params #f (new-empty-set))))
160 (define-type-of-ast expr
161 extender: define-type-of-expr
164 (define-type-of-expr literal
166 (define (new-literal type val)
167 (make-literal #f '() type val))
169 (define-type-of-expr ref
171 (define (new-ref type def)
172 (make-ref #f '() type def))
174 (define-type-of-expr oper
176 (define (new-oper subasts type op)
179 (make-oper #f subasts type op)))
181 (define-type-of-expr call
182 (def-proc unprintable:))
183 (define (new-call subasts type proc-def)
186 (make-call #f subasts type proc-def)))
188 (define-type-of-ast block
189 name) ; blocks that begin with a label have a name, the other have #f
190 (define (new-block subasts)
193 (make-block #f subasts #f)))
194 (define (new-named-block name subasts)
197 (make-block #f subasts name)))
199 (define-type-of-ast if)
200 (define (new-if subasts)
203 (make-if #f subasts)))
205 (define-type-of-ast switch)
206 (define (new-switch subasts)
209 (make-switch #f subasts)))
211 (define-type-of-ast while)
212 (define (new-while subasts)
215 (make-while #f subasts)))
217 (define-type-of-ast do-while)
218 (define (new-do-while subasts)
221 (make-do-while #f subasts)))
223 (define-type-of-ast for)
224 (define (new-for subasts)
227 (make-for #f subasts)))
229 (define-type-of-ast return)
230 (define (new-return subasts)
233 (make-return #f subasts)))
235 (define-type-of-ast break)
239 (define-type-of-ast continue)
240 (define (new-continue)
241 (make-continue #f '()))
243 (define-type-of-ast goto)
244 (define (new-goto label)
245 (make-goto #f (list label)))
247 (define-type-of-ast program)
248 (define (new-program subasts) ;; TODO add support for main
251 (make-program #f subasts)))
254 extender: define-type-of-op
255 (six-id unprintable:)
262 (define-type-of-op op1)
263 (define-type-of-op op2)
264 (define-type-of-op op3)