-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathast.scm
271 lines (237 loc) · 6.92 KB
/
ast.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
;;; definition of ast types
(define-type ast
extender: define-type-of-ast
(parent unprintable: equality-skip:)
subasts)
(define (link-parent! subast parent)
(ast-parent-set! subast parent)
parent)
(define (multi-link-parent! subasts parent)
(for-each (lambda (subast) (link-parent! subast parent))
subasts)
parent)
(define (unlink-parent! subast)
(let ((parent (ast-parent subast)))
(if (and (def-variable? subast) (def-procedure? parent))
(def-procedure-params-set!
parent
(remove subast (def-procedure-params parent)))
(ast-subasts-set!
parent
(remove subast (ast-subasts parent))))
(ast-parent-set! subast #f)
subast))
(define (subast1 ast) (car (ast-subasts ast)))
(define (subast2 ast) (cadr (ast-subasts ast)))
(define (subast3 ast) (caddr (ast-subasts ast)))
(define (subast4 ast) (cadddr (ast-subasts ast)))
(define-type-of-ast def
extender: define-type-of-def
id
(refs unprintable: equality-skip:))
(define-type value
bytes)
(define (new-value bytes)
(make-value bytes))
(define byte-cell-counter 0)
(define (byte-cell-next-id) (let ((id byte-cell-counter))
(set! byte-cell-counter (+ id 1))
id))
(define all-byte-cells (make-table)) ;; TODO does not contain those defined (using make-byte-cell) in cte.scm
(define-type byte-cell
id
adr
name ; to display in the listing
bb ; label of the basic in which this byte-cell is used
def-proc-id ; id of the procedure it was defined in
(interferes-with unprintable: equality-skip:) ; bitset
nb-neighbours ; cached length of interferes-with
(coalesceable-with unprintable: equality-skip:) ; set
(coalesced-with unprintable: equality-skip:)) ; set
(define (new-byte-cell def-proc-id #!optional (name #f) (bb #f))
(let* ((id (byte-cell-next-id))
(cell (make-byte-cell
id (if allocate-registers? #f id)
name bb def-proc-id #f 0 (new-empty-set) (new-empty-set))))
(table-set! all-byte-cells id cell)
cell))
(define (get-register n)
(let* ((id (byte-cell-next-id))
(cell (make-byte-cell
id n (symbol->string (cdr (assv n file-reg-names))) #f #f
#f 0 (new-empty-set) (new-empty-set))))
(table-set! all-byte-cells id cell)
cell))
(define-type byte-lit
val)
(define (new-byte-lit x)
(make-byte-lit x))
(define types-bytes
'((void . 0)
(bool . 1)
(int . 2)
(byte . 1)
(uint8 . 1)
(uint16 . 2)
(uint24 . 3)
(uint32 . 4)))
(define (val->type n) ;; TODO negative literals won't work
(cond ((and (>= n 0) (< n 256)) 'uint8)
((and (>= n 0) (< n 65536)) 'uint16)
(else 'uint32)))
(define (type->bytes type)
(cond ((assq type types-bytes)
=> (lambda (x) (cdr x)))
(else (error "wrong type?"))))
(define (bytes->type n)
(let loop ((l types-bytes))
(cond ((null? l) (error (string-append "no type contains "
(number->string n)
" bytes")))
((= n (cdar l)) (caar l))
(else (loop (cdr l))))))
(define (int->value n type)
(let ((len (type->bytes type)))
(let loop ((len len) (n n) (rev-bytes '()))
(if (= len 0)
(new-value (reverse rev-bytes))
(loop (- len 1)
(arithmetic-shift n -8)
(cons (new-byte-lit (modulo n 256))
rev-bytes))))))
(define (value->int val)
(let loop ((bytes (reverse (value-bytes val)))
(n 0))
(if (null? bytes)
n
(loop (cdr bytes)
(+ (* 256 n) (byte-lit-val (car bytes)))))))
(define (alloc-value type def-proc-id #!optional (name #f) (bb #f))
(let ((len (type->bytes type)))
(let loop ((len len) (rev-bytes '()))
(if (= len 0)
(new-value rev-bytes)
(loop (- len 1)
(cons (new-byte-cell
def-proc-id
(if name
;; the lsb is 0, and so on
(string-append (symbol->string name) "$"
(number->string (- len 1)))
#f)
bb)
rev-bytes))))))
(define-type-of-def def-variable
type
value
(sets unprintable: equality-skip:)
def-proc-id) ; name of the procedure it was defined in, #f if global
(define (new-def-variable subasts id refs type value sets def-proc-id)
(multi-link-parent!
subasts
(make-def-variable #f subasts id refs type value sets def-proc-id)))
(define-type-of-def def-procedure
type
value
params
entry
(live-after-calls unprintable: equality-skip:) ; bitset
;; used for common subexpression elimination
(computed-expressions unprintable: equality-skip:))
(define all-def-procedures (make-table))
(define (new-def-procedure subasts id refs type value params)
(multi-link-parent!
subasts
(let ((d (make-def-procedure
#f subasts id refs type value params #f #f
(make-table)))) ; needs an equal? hash-table
(table-set! all-def-procedures id d)
d)))
(define-type-of-ast expr
extender: define-type-of-expr
type)
(define-type-of-expr literal
val)
(define (new-literal type val)
(make-literal #f '() type val))
(define-type-of-expr ref
def-var)
(define (new-ref type def)
(make-ref #f '() type def))
(define-type-of-expr oper
op)
(define (new-oper subasts type op)
(multi-link-parent!
subasts
(make-oper #f subasts type op)))
(define-type-of-expr call
(def-proc unprintable: equality-skip:))
(define (new-call subasts type proc-def)
(multi-link-parent!
subasts
(make-call #f subasts type proc-def)))
(define-type-of-ast block
name) ; blocks that begin with a label have a name, the other have #f
(define (new-block subasts)
(multi-link-parent!
subasts
(make-block #f subasts #f)))
(define (new-named-block name subasts)
(multi-link-parent!
subasts
(make-block #f subasts name)))
(define-type-of-ast if)
(define (new-if subasts)
(multi-link-parent!
subasts
(make-if #f subasts)))
(define-type-of-ast switch)
(define (new-switch subasts)
(multi-link-parent!
subasts
(make-switch #f subasts)))
(define-type-of-ast while)
(define (new-while subasts)
(multi-link-parent!
subasts
(make-while #f subasts)))
(define-type-of-ast do-while)
(define (new-do-while subasts)
(multi-link-parent!
subasts
(make-do-while #f subasts)))
(define-type-of-ast for)
(define (new-for subasts)
(multi-link-parent!
subasts
(make-for #f subasts)))
(define-type-of-ast return)
(define (new-return subasts)
(multi-link-parent!
subasts
(make-return #f subasts)))
(define-type-of-ast break)
(define (new-break)
(make-break #f '()))
(define-type-of-ast continue)
(define (new-continue)
(make-continue #f '()))
(define-type-of-ast goto)
(define (new-goto label)
(make-goto #f (list label)))
(define-type-of-ast program)
(define (new-program subasts) ;; TODO add support for main
(multi-link-parent!
subasts
(make-program #f subasts)))
(define-type op
extender: define-type-of-op
(six-id unprintable:)
id
unprintable:
type-rule
constant-fold
code-gen)
(define-type-of-op op1)
(define-type-of-op op2)
(define-type-of-op op3)