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 all-byte-cells (make-table)) ;; TODO does not contain those defined (using make-byte-cell) in cte.scm
50 (define-type byte-cell
53 name ; to display in the listing
54 bb ; label of the basic in which this byte-cell is used
55 (interferes-with unprintable:) ; bitset
56 nb-neighbours ; cached length of interferes-with
57 (coalesceable-with unprintable:) ; set
58 (coalesced-with unprintable:)) ; set
59 (define (new-byte-cell #!optional (name #f) (bb #f))
60 (let* ((id (byte-cell-next-id))
62 id (if allocate-registers? #f id)
63 (if name (string-append name "$" (number->string id)) "__tmp")
64 bb #f 0 (new-empty-set) (new-empty-set))))
65 (table-set! all-byte-cells id cell)
67 (define (get-register n)
68 (let* ((id (byte-cell-next-id))
70 id n (symbol->string (cdr (assv n file-reg-names))) #f
71 #f 0 (new-empty-set) (new-empty-set))))
72 (table-set! all-byte-cells id cell)
77 (define (new-byte-lit x)
91 (cond ((and (>= n 0) (< n 256)) 'int8)
92 ((and (>= n 0) (< n 65536)) 'int16)
94 (define (type->bytes type)
95 (cond ((assq type types-bytes)
96 => (lambda (x) (cdr x)))
97 (else (error "wrong type?"))))
98 (define (bytes->type n)
99 (let loop ((l types-bytes))
100 (cond ((null? l) (error (string-append "no type contains "
103 ((= n (cdar l)) (caar l))
104 (else (loop (cdr l))))))
106 (define (int->value n type)
107 (let ((len (type->bytes type)))
108 (let loop ((len len) (n n) (rev-bytes '()))
110 (new-value (reverse rev-bytes))
112 (arithmetic-shift n -8)
113 (cons (new-byte-lit (modulo n 256))
115 (define (value->int val)
116 (let loop ((bytes (reverse (value-bytes val)))
121 (+ (* 256 n) (byte-lit-val (car bytes)))))))
123 (define (alloc-value type #!optional (name #f) (bb #f))
124 (let ((len (type->bytes type)))
125 (let loop ((len len) (rev-bytes '()))
127 (new-value rev-bytes)
131 ;; the lsb is 0, and so on
132 (string-append (symbol->string name)
133 (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)