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-type byte-cell
47 (interferes-with unprintable:)
48 (coalesceable-with unprintable:))
49 (define (new-byte-cell)
50 (make-byte-cell #f '() '()))
51 (define (get-register n)
52 (make-byte-cell n '() '()))
56 (define (new-byte-lit x)
69 (define (type->bytes type)
70 (cond ((assq type types-bytes)
71 => (lambda (x) (cdr x)))
72 (else (error "wrong number of bytes ?"))))
74 (define (bytes->type n)
75 (let loop ((l types-bytes))
76 (cond ((null? l) (error (string-append "no type contains "
79 ((= n (cdar l)) (caar l))
80 (else (loop (cdr l))))))
82 (define (int->value n type)
83 (let ((len (type->bytes type)))
84 (let loop ((len len) (n n) (rev-bytes '()))
86 (new-value (reverse rev-bytes))
88 (arithmetic-shift n -8)
89 (cons (new-byte-lit (modulo n 256))
91 (define (value->int val)
92 (let loop ((bytes (reverse (value-bytes val)))
97 (+ (* 256 n) (byte-lit-val (car bytes)))))))
99 ;; TODO instead of carrying types around, use the length instead
100 (define (extend value type)
101 ;; literals must be extended with literal 0s, while variables must be
102 ;; extended with byte cells
103 (let* ((bytes (value-bytes value))
104 (lit? (byte-lit? (car bytes))))
105 (let loop ((rev-bytes (reverse bytes))
106 (n (max 0 (- (type->bytes type) (length bytes)))))
108 (new-value (reverse rev-bytes))
109 (loop (cons (if lit? (new-byte-lit 0) (new-byte-cell))
113 (define (alloc-value type)
114 (let ((len (type->bytes type)))
115 (let loop ((len len) (rev-bytes '()))
117 (new-value (reverse rev-bytes))
119 (cons (new-byte-cell)
122 (define-type-of-def def-variable
127 (define (new-def-variable subasts id refs type value sets)
130 (make-def-variable #f subasts id refs type value sets)))
132 (define-type-of-def def-procedure
138 (define (new-def-procedure subasts id refs type value params)
141 (make-def-procedure #f subasts id refs type value params #f '())))
144 (define-type-of-ast expr
145 extender: define-type-of-expr
148 (define-type-of-expr literal
150 (define (new-literal type val)
151 (make-literal #f '() type val))
153 (define-type-of-expr ref
155 (define (new-ref type def)
156 (make-ref #f '() type def))
158 (define-type-of-expr oper
160 (define (new-oper subasts type op)
163 (make-oper #f subasts type op)))
165 (define-type-of-expr call
166 (def-proc unprintable:))
167 (define (new-call subasts type proc-def)
170 (make-call #f subasts type proc-def)))
172 (define-type-of-ast block
173 name) ; blocks that begin with a label have a name, the other have #f
174 (define (new-block subasts)
177 (make-block #f subasts #f)))
178 (define (new-named-block name subasts)
181 (make-block #f subasts name)))
183 (define-type-of-ast if)
184 (define (new-if subasts)
187 (make-if #f subasts)))
189 (define-type-of-ast switch)
190 (define (new-switch subasts)
193 (make-switch #f subasts)))
195 (define-type-of-ast while)
196 (define (new-while subasts)
199 (make-while #f subasts)))
201 (define-type-of-ast do-while)
202 (define (new-do-while subasts)
205 (make-do-while #f subasts)))
207 (define-type-of-ast for)
208 (define (new-for subasts)
211 (make-for #f subasts)))
213 (define-type-of-ast return)
214 (define (new-return subasts)
217 (make-return #f subasts)))
219 (define-type-of-ast break)
223 (define-type-of-ast continue)
224 (define (new-continue)
225 (make-continue #f '()))
227 (define-type-of-ast goto)
228 (define (new-goto label)
229 (make-goto #f (list label)))
231 (define-type-of-ast program)
232 (define (new-program subasts) ;; TODO add suport for main
235 (make-program #f subasts)))
238 extender: define-type-of-op
239 (six-id unprintable:)
246 (define-type-of-op op1)
247 (define-type-of-op op2)
248 (define-type-of-op op3)