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)
65 (int32 . 4) ;; TODO should the default int be 32 bits ?
68 (define (type->bytes type)
69 (cond ((assq type types-bytes)
70 => (lambda (x) (cdr x)))
71 (else (error "wrong number of bytes ?"))))
73 (define (bytes->type n)
74 (let loop ((l types-bytes))
75 (cond ((null? l) (error (string-append "no type contains "
78 ((= n (cdar l)) (caar l))
79 (else (loop (cdr l))))))
81 (define (int->value n type)
82 (let ((len (type->bytes type)))
83 (let loop ((len len) (n n) (rev-bytes '()))
85 (new-value (reverse rev-bytes))
87 (arithmetic-shift n -8)
88 (cons (new-byte-lit (modulo n 256))
91 (define (extend value type)
92 value);;;;;;;;;;;;;;;;;;;;;
94 (define (alloc-value type)
95 (let ((len (type->bytes type)))
96 (let loop ((len len) (rev-bytes '()))
98 (new-value (reverse rev-bytes)) ;; TODO why reverse, everything is empty
100 (cons (new-byte-cell)
103 (define-type-of-def def-variable
108 (define (new-def-variable subasts id refs type value sets)
111 (make-def-variable #f subasts id refs type value sets)))
113 (define-type-of-def def-procedure
119 (define (new-def-procedure subasts id refs type value params)
122 (make-def-procedure #f subasts id refs type value params #f '())))
125 (define-type-of-ast expr
126 extender: define-type-of-expr
129 (define-type-of-expr literal
131 (define (new-literal type val)
132 (make-literal #f '() type val))
134 (define-type-of-expr ref
136 (define (new-ref type def)
137 (make-ref #f '() type def))
139 (define-type-of-expr oper
141 (define (new-oper subasts type op)
144 (make-oper #f subasts type op)))
146 (define-type-of-expr call
148 (define (new-call subasts type proc-def)
151 (make-call #f subasts type proc-def)))
153 (define-type-of-ast block
154 name) ; blocks that begin with a label have a name, the other have #f
155 (define (new-block subasts)
158 (make-block #f subasts #f)))
159 (define (new-named-block name subasts)
162 (make-block #f subasts name)))
164 (define-type-of-ast if)
165 (define (new-if subasts)
168 (make-if #f subasts)))
170 (define-type-of-ast switch)
171 (define (new-switch subasts)
174 (make-switch #f subasts)))
176 (define-type-of-ast while)
177 (define (new-while subasts)
180 (make-while #f subasts)))
182 (define-type-of-ast do-while)
183 (define (new-do-while subasts)
186 (make-do-while #f subasts)))
188 (define-type-of-ast for)
189 (define (new-for subasts)
192 (make-for #f subasts)))
194 (define-type-of-ast return)
195 (define (new-return subasts)
198 (make-return #f subasts)))
200 (define-type-of-ast break)
204 (define-type-of-ast continue)
205 (define (new-continue)
206 (make-continue #f '()))
208 (define-type-of-ast goto)
209 (define (new-goto label)
210 (make-goto #f (list label)))
212 (define-type-of-ast program)
213 (define (new-program subasts) ;; TODO add suport for main
216 (make-program #f subasts)))
219 extender: define-type-of-op
220 (six-id unprintable:)
227 (define-type-of-op op1)
228 (define-type-of-op op2)