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)
67 (int . 4))) ;; TODO should the default int be 32 bits ?
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 (define (extend value type) ;; TODO instead of carrying types around, use the length instead
100 ;; literals must be extended with literal 0s, while variables must be
101 ;; extended with byte cells
102 (let* ((bytes (value-bytes value))
103 (lit? (byte-lit? (car bytes))))
104 (let loop ((rev-bytes (reverse bytes)) ;; TODO put in cfg.scm ?
105 (n (max 0 (- (type->bytes type) (length bytes)))))
107 (new-value (reverse rev-bytes))
108 (loop (cons (if lit? (new-byte-lit 0) (new-byte-cell))
110 (- n 1)))))) ;; TODO would need to move 0 in the new byte ?
112 (define (alloc-value type)
113 (let ((len (type->bytes type)))
114 (let loop ((len len) (rev-bytes '()))
116 (new-value (reverse rev-bytes)) ;; TODO why reverse, everything is empty
118 (cons (new-byte-cell)
121 (define-type-of-def def-variable
126 (define (new-def-variable subasts id refs type value sets)
129 (make-def-variable #f subasts id refs type value sets)))
131 (define-type-of-def def-procedure
137 (define (new-def-procedure subasts id refs type value params)
140 (make-def-procedure #f subasts id refs type value params #f '())))
143 (define-type-of-ast expr
144 extender: define-type-of-expr
147 (define-type-of-expr literal
149 (define (new-literal type val)
150 (make-literal #f '() type val))
152 (define-type-of-expr ref
154 (define (new-ref type def)
155 (make-ref #f '() type def))
157 (define-type-of-expr oper
159 (define (new-oper subasts type op)
162 (make-oper #f subasts type op)))
164 (define-type-of-expr call
165 (def-proc unprintable:))
166 (define (new-call subasts type proc-def)
169 (make-call #f subasts type proc-def)))
171 (define-type-of-ast block
172 name) ; blocks that begin with a label have a name, the other have #f
173 (define (new-block subasts)
176 (make-block #f subasts #f)))
177 (define (new-named-block name subasts)
180 (make-block #f subasts name)))
182 (define-type-of-ast if)
183 (define (new-if subasts)
186 (make-if #f subasts)))
188 (define-type-of-ast switch)
189 (define (new-switch subasts)
192 (make-switch #f subasts)))
194 (define-type-of-ast while)
195 (define (new-while subasts)
198 (make-while #f subasts)))
200 (define-type-of-ast do-while)
201 (define (new-do-while subasts)
204 (make-do-while #f subasts)))
206 (define-type-of-ast for)
207 (define (new-for subasts)
210 (make-for #f subasts)))
212 (define-type-of-ast return)
213 (define (new-return subasts)
216 (make-return #f subasts)))
218 (define-type-of-ast break)
222 (define-type-of-ast continue)
223 (define (new-continue)
224 (make-continue #f '()))
226 (define-type-of-ast goto)
227 (define (new-goto label)
228 (make-goto #f (list label)))
230 (define-type-of-ast program)
231 (define (new-program subasts) ;; TODO add suport for main
234 (make-program #f subasts)))
237 extender: define-type-of-op
238 (six-id unprintable:)
245 (define-type-of-op op1)
246 (define-type-of-op op2)
247 (define-type-of-op op3)