-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterp.rkt
369 lines (326 loc) · 14.7 KB
/
interp.rkt
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#lang typed/racket
(require racket/fixnum)
(require "utilities.rkt"
"untyped-utilities.rkt"
(prefix-in runtime-config: "runtime-config.rkt"))
(provide interp-pseudo-x86-0 interp-x86-0)
;; The interpreters in this file are for the intermediate languages
;; produced by the various passes of the compiler.
;;
;; The interpreters for the source languages (Rvar, Rif, ...)
;; and the C intermediate languages Cvar and Cif
;; are in separate files, e.g., interp-Rvar.rkt.
(: interp-pseudo-x86-0 [-> Pseudo-X86Program FinalAnswer])
(define interp-pseudo-x86-0
(lambda (p)
((send (new interp-R1-class) interp-pseudo-x86 ((inst empty-env Fixnum))) p)))
(: interp-x86-0 [-> X86Program FinalAnswer])
(define interp-x86-0
(lambda (p)
((send (new interp-R1-class) interp-x86 ((inst empty-env Fixnum))) p)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Interpreters for R1: fixnum arithmetic and 'let'
(: interp-R1-class (Class (field [result Symbol]
[x86-ops (Immutable-HashTable Symbol (List Natural [-> Fixnum * Fixnum]))])
[observe-value [-> Fixnum Fixnum]]
[return-from-tail [-> Fixnum (Env Fixnum) (Pair (Pair Symbol Fixnum) (Env Fixnum))]]
[is-return? [-> Any Boolean]]
[primitives [-> (List '+ '- 'read)]]
;; R1
[interp-op [-> Symbol [-> Fixnum * Fixnum]]]
[interp-scheme-exp [-> (Env Fixnum) [-> Exp Fixnum]]]
[interp-scheme [-> (Env Fixnum) [-> Program FinalAnswer]]]
;; C0
[interp-C-exp [-> (Env Fixnum) [-> Exp Fixnum]]]
[interp-C-tail [-> (Env Fixnum) [-> Tail Fixnum]]]
[interp-C-stmt [-> (Env Fixnum) [-> Assign (Env Fixnum)]]]
[interp-C [-> CProgram FinalAnswer]]
;; x86
[get-name [-> AST Symbol]]
[interp-x86-op [-> Symbol [-> Fixnum * Fixnum]]]
[interp-x86-exp [-> (Env Fixnum) [-> AST Fixnum]]]
[interp-x86-instr [-> (Env Fixnum) [-> (Listof X86Instr) (Env Fixnum)]]]
[interp-x86-block [-> (Env Fixnum) [-> Block (Env Fixnum)]]]
[interp-x86 [-> (Env Fixnum) [-> X86Program FinalAnswer]]]
;; psuedo-x86
[pseudo-get-name [-> AST Symbol]]
[interp-pseudo-x86-op [-> Symbol [-> Fixnum * Fixnum]]]
[interp-pseudo-x86-exp [-> (Env Fixnum) [-> AST Fixnum]]]
[interp-pseudo-x86-instr [-> (Env Fixnum) [-> (Listof Pseudo-X86Instr) (Env Fixnum)]]]
[interp-pseudo-x86-block [-> (Env Fixnum) [-> Pseudo-Block (Env Fixnum)]]]
[interp-pseudo-x86 [-> (Env Fixnum) [-> Pseudo-X86Program FinalAnswer]]]
))
(define interp-R1-class
(class object%
(super-new)
(field [result : Symbol (gensym 'result)])
;; Hide details for debug output.
(: observe-value [-> Fixnum Fixnum])
(define/public (observe-value v) v)
(: return-from-tail [-> Fixnum (Env Fixnum) (Pair (Pair Symbol Fixnum) (Env Fixnum))])
(define/public (return-from-tail v env)
(cons (cons result v) env))
(: is-return? [-> Any Boolean])
(define/public (is-return? e)
(match e
[(cons (cons res v) env) (equal? res result)]
[_ #f]))
(: primitives [-> (List '+ '- 'read)])
(define/public (primitives) '(+ - read))
(: interp-op [-> Symbol [-> Fixnum * Fixnum]])
(define/public (interp-op op)
(match op
['+ (dim-fixnum-binary fx+)]
['- (dim-fixnum-binary fx-)]
['read (dim-fixnum-nullary read-fixnum)]
[_ (error "in interp-op R1, unmatched" op)]))
(: interp-scheme-exp [-> (Env Fixnum) [-> Exp Fixnum]])
(define/public (interp-scheme-exp env)
(lambda (ast)
(define recur (interp-scheme-exp env))
(verbose "R1/interp-scheme-exp" ast)
(match ast
[(Var x)
(env-ref env x)]
[(Int n) n]
[(Let x e body)
(define v (recur e))
((interp-scheme-exp (env-set env x v)) body)]
[(Prim op args)
(apply (interp-op op)
(for/list : (Listof Fixnum) ([e args]) (recur e)))]
[_ (error (format "R1/no match in interp-scheme-exp for ~a" ast))])))
(: interp-scheme [-> (Env Fixnum) [-> Program FinalAnswer]])
(define/public (interp-scheme env)
(lambda (ast)
(verbose "R1/interp-scheme" ast)
(match ast
[(Program _ e)
((interp-scheme-exp (empty-env)) e)]
[_ (error (format "R1/no match in interp-scheme for ~a" ast))])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; C0
(: interp-C-exp [-> (Env Fixnum) [-> Exp Fixnum]])
(define/public (interp-C-exp env)
(lambda (ast)
(define recur (interp-C-exp env))
(define result
(match ast
[(Var x) (env-ref env x)]
[(Int n) n]
[(Prim op args)
(apply (interp-op op) (map (interp-C-exp env) args))]
[_ (error "C0/interp-C-exp unhandled" ast)]))
(verbose "C0/interp-C-exp" ast result)
result))
(: interp-C-tail [-> (Env Fixnum) [-> Tail Fixnum]])
(define/public (interp-C-tail env)
(lambda (ast)
(match ast
[(Return e)
((interp-C-exp env) e)]
;; (return-from-tail v env) hmm -Jeremy
[(Seq s t)
(: new-env (Env Fixnum))
(define new-env ((interp-C-stmt env) s))
((interp-C-tail new-env) t)]
[_ (error "interp-C-tail unhandled" ast)])))
(: interp-C-stmt [-> (Env Fixnum) [-> Assign (Env Fixnum)]])
(define/public (interp-C-stmt env)
(lambda (ast)
(verbose "C0/interp-C-stmt" ast)
(match ast
[(Assign (Var x) e)
(env-set env x ((interp-C-exp env) e))]
[_ (error "interp-C-stmt unhandled" ast)])))
(: interp-C [-> CProgram FinalAnswer])
(define/public (interp-C ast)
(debug "R1/interp-C" ast)
(match ast
[(CProgram _ `((start . ,t1) (,ls . ,ts) ...))
((interp-C-tail ((inst empty-env Fixnum))) t1)]
[_ (error "no match in interp-C for " ast)]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; psuedo-x86 and x86
;; s,d ::= (var x) | (int n) | (reg r) | (deref r n)
;; i ::= (movq s d) | (addq s d) | (subq s d) | (imulq s d)
;; | (negq d) | (callq f)
;; psuedo-x86 ::= (program info i ...)
;; x86
(: get-name [-> AST Symbol])
(define/public (get-name ast)
(match ast
[(Var x) x]
[(Reg x) x]
[(Deref r n) (string->symbol (format "~a:~a" r n))]
[_ (error 'interp-R1-class/get-name "doesn't have a name: ~a" ast)]))
(field [x86-ops : (Immutable-HashTable Symbol (List Natural [-> Fixnum * Fixnum]))
((inst hash Symbol (List Natural [-> Fixnum * Fixnum]))
'addq (list 2 (dim-fixnum-binary fx+))
'imulq (list 2 (dim-fixnum-binary fx*))
'subq (list 2 (dim-fixnum-binary fx-))
'negq (list 1 (dim-fixnum-unary (curry (ann fx- (-> Zero Fixnum Fixnum)) 0))))])
(: interp-x86-op [-> Symbol [-> Fixnum * Fixnum]])
(define/public (interp-x86-op op)
(cadr (hash-ref x86-ops op
(λ () (error 'interp-R1-class/interp-x86-op "unmatched ~a" op)))))
(: interp-x86-exp [-> (Env Fixnum) [-> AST Fixnum]])
(define/public (interp-x86-exp env)
(lambda (ast)
(copious "interp-x86-exp" ast)
(define result
(match ast
[(Reg x)
(env-ref env (get-name ast))]
[(Deref r n)
(env-ref env (get-name ast))]
[(Imm n) n]
[_ (error 'interp-R1-class/interp-x86-exp "unhandled ~a" ast)]))
(copious "R1/interp-x86-exp" (observe-value result))
result))
(: interp-x86-instr [-> (Env Fixnum) [-> (Listof X86Instr) (Env Fixnum)]])
(define/public (interp-x86-instr env)
(lambda (ast)
(when (pair? ast)
(copious "R1/interp-x86-instr" (car ast)))
(match ast
['() env]
[(cons (Callq 'read_int _) ss)
(let ([v (read-fixnum)])
(copious "read " v)
((interp-x86-instr (env-set env 'rax v)) ss))]
[(cons (Instr 'movq (list s d)) ss)
(define x (get-name d))
(define v ((interp-x86-exp env) s))
(copious "move " (observe-value v))
((interp-x86-instr (env-set env x v)) ss)]
[(cons (Jmp conclusion) ss)
#:when (string-suffix? (symbol->string conclusion) "conclusion")
env]
[(cons (Jmp label) ss)
((interp-x86-block env) (goto-label label))]
[(cons (Instr binary-op (list s d)) ss)
(let ([s ((interp-x86-exp env) s)]
[d ((interp-x86-exp env) d)]
[x (get-name d)]
[f (interp-x86-op binary-op)])
(let ([v (f d s)])
(copious "binary-op result " (observe-value v))
((interp-x86-instr (env-set env x v)) ss)))]
[(cons (Instr unary-op (list d)) ss)
(let ([d ((interp-x86-exp env) d)]
[x (get-name d)]
[f (interp-x86-op unary-op)])
(let ([v (f d)])
(copious "unary-op result " (observe-value v))
((interp-x86-instr (env-set env x v)) ss)))]
[_ (error "R1/interp-x86-instr no match for" ast)])))
(: interp-x86-block [-> (Env Fixnum) [-> Block (Env Fixnum)]])
(define/public (interp-x86-block env)
(lambda (ast)
(match ast
[(Block info ss)
((interp-x86-instr env) ss)]
[_ (error "R1/interp-x86-block unhandled" ast)])))
(: interp-x86 [-> (Env Fixnum) [-> X86Program FinalAnswer]])
(define/public (interp-x86 env)
(lambda (ast)
(when (pair? ast)
(copious "R1/interp-x86" (car ast)))
(match ast
[(X86Program info G)
(parameterize ([get-CFG G])
(define start-block (cdar G))
(define result-env ((interp-x86-block ((inst empty-env Fixnum))) start-block))
(env-ref result-env 'rax))]
[_ (error "R1/interp-x86 no match in for" ast)])))
;; pseudo x86
(: pseudo-get-name [-> AST Symbol])
(define/public (pseudo-get-name ast)
(match ast
[(Pseudo-Var x) x]
[(Pseudo-Reg x) x]
[(Pseudo-Deref r n) (string->symbol (format "~a:~a" r n))]
[_ (error 'interp-R1-class/pseudo-get-name "doesn't have a name: ~a" ast)]))
(: interp-pseudo-x86-op [-> Symbol [-> Fixnum * Fixnum]])
(define/public (interp-pseudo-x86-op op)
(cadr (hash-ref x86-ops op
(λ () (error 'interp-R1-class/interp-x86-op "unmatched ~a" op)))))
(: interp-pseudo-x86-exp [-> (Env Fixnum) [-> AST Fixnum]])
(define/public (interp-pseudo-x86-exp env)
(lambda (ast)
(copious "interp-pseudo-x86-exp" ast)
(define result
(match ast
[(Pseudo-Var x)
(env-ref env (pseudo-get-name ast))]
[(Pseudo-Reg x)
(env-ref env (pseudo-get-name ast))]
[(Pseudo-Deref r n)
(env-ref env (pseudo-get-name ast))]
[(Pseudo-Imm n) n]
[_ (error 'interp-R1-class/interp-pseudo-x86-exp "unhandled ~a" ast)]))
(copious "R1/interp-pseudo-x86-exp" (observe-value result))
result))
(: interp-pseudo-x86-instr [-> (Env Fixnum) [-> (Listof Pseudo-X86Instr) (Env Fixnum)]])
(define/public (interp-pseudo-x86-instr env)
(lambda (ast)
(when (pair? ast)
(copious "R1/interp-pseudo-x86-instr" (car ast)))
(match ast
['() env]
[(cons (Pseudo-Callq 'read_int _) ss)
(let ([v (read-fixnum)])
(copious "read " v)
((interp-pseudo-x86-instr (env-set env 'rax v)) ss))]
[(cons (Pseudo-Instr 'movq (list s d)) ss)
(define x (pseudo-get-name d))
(define v ((interp-pseudo-x86-exp env) s))
(copious "move " (observe-value v))
((interp-pseudo-x86-instr (env-set env x v)) ss)]
;; [(cons (Pseudo-Jmp conclusion) ss)
;; #:when (string-suffix? (symbol->string conclusion) "conclusion")
;; env]
;; [(cons (Pseudo-Jmp label) ss)
;; ((interp-pseudo-x86-block env) (pseudo-goto-label label))]
[(cons (Pseudo-Jmp label) ss)
(define block (pseudo-goto-label label))
(if (false? block)
env
((interp-pseudo-x86-block env) block))]
[(cons (Pseudo-Instr binary-op (list s d)) ss)
(let ([s ((interp-pseudo-x86-exp env) s)]
[d ((interp-pseudo-x86-exp env) d)]
[x (pseudo-get-name d)]
[f (interp-pseudo-x86-op binary-op)])
(let ([v (f d s)])
(copious "binary-op result " (observe-value v))
((interp-pseudo-x86-instr (env-set env x v)) ss)))]
[(cons (Pseudo-Instr unary-op (list d)) ss)
(let ([d ((interp-pseudo-x86-exp env) d)]
[x (pseudo-get-name d)]
[f (interp-pseudo-x86-op unary-op)])
(let ([v (f d)])
(copious "unary-op result " (observe-value v))
((interp-pseudo-x86-instr (env-set env x v)) ss)))]
[_ (error "R1/interp-pseudo-x86-instr no match for" ast)])))
(: interp-pseudo-x86-block [-> (Env Fixnum) [-> Pseudo-Block (Env Fixnum)]])
(define/public (interp-pseudo-x86-block env)
(lambda (ast)
(match ast
[(Pseudo-Block info ss)
((interp-pseudo-x86-instr env) ss)]
[_ (error "R1/interp-pseudo-x86-block unhandled" ast)])))
(: interp-pseudo-x86 [-> (Env Fixnum) [-> Pseudo-X86Program FinalAnswer]])
(define/public (interp-pseudo-x86 env)
(lambda (ast)
(when (pair? ast)
(copious "R1/interp-pseudo-x86" (car ast)))
(match ast
[(Pseudo-X86Program info G)
(parameterize ([pseudo-get-CFG G])
(define start-block (cdar G))
(define result-env ((interp-pseudo-x86-block ((inst empty-env Fixnum))) start-block))
(env-ref result-env 'rax))]
[_ (error "R1/interp-pseudo-x86 no match in for" ast)])))
)) ;; class interp-R1-class