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 bb ; label of the basic in which this byte-cell is used
54 (interferes-with unprintable:) ; these 3 are stored as sets
55 (coalesceable-with unprintable:)
56 (coalesced-with unprintable:))
57 (define (new-byte-cell #!optional (name #f) (bb #f))
58 (let ((id (byte-cell-next-id)))
60 id (if allocate-registers? #f id)
61 (if name (string-append name "$" (number->string id)) "__tmp") bb
62 (new-empty-set) (new-empty-set) (new-empty-set))))
63 (define (get-register n)
66 (symbol->string (cdr (assv n file-reg-names))) #f
67 (new-empty-set) (new-empty-set) (new-empty-set)))
71 (define (new-byte-lit x)
85 (cond ((and (>= n 0) (< n 256)) 'int8)
86 ((and (>= n 0) (< n 65536)) 'int16)
88 (define (type->bytes type)
89 (cond ((assq type types-bytes)
90 => (lambda (x) (cdr x)))
91 (else (error "wrong type?"))))
92 (define (bytes->type n)
93 (let loop ((l types-bytes))
94 (cond ((null? l) (error (string-append "no type contains "
97 ((= n (cdar l)) (caar l))
98 (else (loop (cdr l))))))
100 (define (int->value n type)
101 (let ((len (type->bytes type)))
102 (let loop ((len len) (n n) (rev-bytes '()))
104 (new-value (reverse rev-bytes))
106 (arithmetic-shift n -8)
107 (cons (new-byte-lit (modulo n 256))
109 (define (value->int val)
110 (let loop ((bytes (reverse (value-bytes val)))
115 (+ (* 256 n) (byte-lit-val (car bytes)))))))
117 (define (alloc-value type #!optional (name #f) (bb #f))
118 (let ((len (type->bytes type)))
119 (let loop ((len len) (rev-bytes '()))
121 (new-value rev-bytes)
125 ;; the lsb is 0, and so on
126 (string-append (symbol->string name)
127 (number->string (- len 1)))
132 (define-type-of-def def-variable
137 (define (new-def-variable subasts id refs type value sets)
140 (make-def-variable #f subasts id refs type value sets)))
142 (define-type-of-def def-procedure
147 live-after-calls) ; stored as a set
148 (define (new-def-procedure subasts id refs type value params)
151 (make-def-procedure #f subasts id refs type value params #f (new-empty-set))))
154 (define-type-of-ast expr
155 extender: define-type-of-expr
158 (define-type-of-expr literal
160 (define (new-literal type val)
161 (make-literal #f '() type val))
163 (define-type-of-expr ref
165 (define (new-ref type def)
166 (make-ref #f '() type def))
168 (define-type-of-expr oper
170 (define (new-oper subasts type op)
173 (make-oper #f subasts type op)))
175 (define-type-of-expr call
176 (def-proc unprintable:))
177 (define (new-call subasts type proc-def)
180 (make-call #f subasts type proc-def)))
182 (define-type-of-ast block
183 name) ; blocks that begin with a label have a name, the other have #f
184 (define (new-block subasts)
187 (make-block #f subasts #f)))
188 (define (new-named-block name subasts)
191 (make-block #f subasts name)))
193 (define-type-of-ast if)
194 (define (new-if subasts)
197 (make-if #f subasts)))
199 (define-type-of-ast switch)
200 (define (new-switch subasts)
203 (make-switch #f subasts)))
205 (define-type-of-ast while)
206 (define (new-while subasts)
209 (make-while #f subasts)))
211 (define-type-of-ast do-while)
212 (define (new-do-while subasts)
215 (make-do-while #f subasts)))
217 (define-type-of-ast for)
218 (define (new-for subasts)
221 (make-for #f subasts)))
223 (define-type-of-ast return)
224 (define (new-return subasts)
227 (make-return #f subasts)))
229 (define-type-of-ast break)
233 (define-type-of-ast continue)
234 (define (new-continue)
235 (make-continue #f '()))
237 (define-type-of-ast goto)
238 (define (new-goto label)
239 (make-goto #f (list label)))
241 (define-type-of-ast program)
242 (define (new-program subasts) ;; TODO add support for main
245 (make-program #f subasts)))
248 extender: define-type-of-op
249 (six-id unprintable:)
256 (define-type-of-op op1)
257 (define-type-of-op op2)
258 (define-type-of-op op3)