forked from yuyang0/iu-p423
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler-r1.rkt
221 lines (209 loc) · 7.15 KB
/
compiler-r1.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
#lang racket
(require racket/fixnum)
(require racket/format)
(require "interp.rkt")
(require "utilities.rkt")
;; This exports r0-passes, defined below, to users of this file.
(provide r1-passes)
(define new-id
(let ([idx 0])
(lambda(pfix)
(set! idx (+ idx 1))
(string->symbol (~a pfix "." idx)))))
(define (uniquify alist)
(lambda (e)
(match e
[(? symbol?) (lookup e alist)]
[(? integer?) e]
[`(let ([,x ,e]) ,body)
(let* ([id (new-id x)]
[new-alist (cons `(,x . ,id) alist)])
`(let ([,id ,((uniquify new-alist) e)])
,((uniquify new-alist) body)))]
[`(program ,e)
`(program ,((uniquify alist) e))]
[`(,op ,es ...)
`(,op ,@(map (uniquify alist) es))]
)))
(define (flatten e)
(define (flatten1 is-let e)
(match e
[(? symbol?) (values e '() '())]
[(? integer?) (values e '() '())]
[`(let ([,x ,e]) ,body)
(let-values ([(r1 sl1 vl1) (flatten1 #t e)]
[(r2 sl2 vl2) (flatten1 #f body)])
(values r2
(append sl1 (cons `(assign ,x ,r1) sl2))
(append vl1 (cons x vl2))))]
[`(program ,e)
(let-values ([(r sl vl) (flatten1 #f e)])
(values r sl vl))]
[`(read) (if is-let
(values '(read) '() '())
(let ([id (new-id 'x)])
(values id `((assign ,id (read))) `(,id))))]
[`(,op ,es)
(let-values ([(r sl vl) (flatten1 #f es)])
(if is-let
(values `(,op ,r)
sl
vl)
(let ([id (new-id 'x)])
(values id
`(,@sl (assign ,id (,op ,r)))
(cons id vl)))))]
[`(,op ,e1 ,e2)
(let-values ([(r1 sl1 vl1) (flatten1 #f e1)]
[(r2 sl2 vl2) (flatten1 #f e2)])
(if is-let
(values `(,op ,r1 ,r2)
`(,@sl1 ,@sl2)
`(,@vl1 ,@vl2))
(let ([id (new-id 'x)])
(values id
`(,@sl1 ,@sl2 (assign ,id (,op ,r1 ,r2)))
`(,@vl1 ,@vl2 ,id)))))
]))
(let-values ([(r sl vl) (flatten1 #f e)])
(let ([sl1 `(,@sl (return ,r))])
`(program ,vl ,@sl1))))
;; (flatten '(program 42))
;; (flatten '(let ([a 42]) (let ([b a]) b)))
;; (flatten '(program (+ (- (read)) (read))))
;; (flatten '(program (+ 52 (- 10))))
;; (flatten '(+ 52 (- 10)))
;; (flatten '(- 10))
;; (flatten '(let ([x (+ (- 10) 11)]) (+ x 41)))
(define op-instr-table
'((+ addq)
(- subq)))
(define (select-instr e)
(letrec ([get-op
(lambda(op)
(cond
[(assoc op op-instr-table) => cadr]
[else #f]))]
[atom-instr
(lambda(ee)
(match ee
[(? symbol?) `(var ,ee)]
[(? integer?) `(int ,ee)]
))])
(match e
[`(assign ,v ,ee)
(match ee
[(? integer?)
`((movq (int ,ee) (var ,v)))]
[(? symbol?)
`((movq (var ,ee) (var ,v)))]
[`(,op ,e1 ,e2)
(let ([av1 (atom-instr e1)]
[av2 (atom-instr e2)])
(cond
[(eq? (cadr av2) v) `((,(get-op op) ,av1 ,(atom-instr v)))]
[(eq? (cadr av1) v) `((,(get-op op) ,av2 ,(atom-instr v)))]
[else `((movq ,av1 ,(atom-instr v))
(,(get-op op) ,av2 ,(atom-instr v)))]))
]
[`(read)
`((callq read_int)
(movq (reg rax) ,(atom-instr v)))]
[`(- ,e1)
(let ([av (atom-instr e1)])
(if (eq? v (cadr av))
`((negq ,(atom-instr v)))
`((movq ,av ,(atom-instr v))
(negq ,(atom-instr v)))))
])]
[`(return ,v)
`((movq ,(atom-instr v) (reg rax)))]
[`(program ,vl ,sl ...)
(let ([ls (map select-instr sl)])
`(program (,(* (length vl) 8)) ,@(foldr append '() ls)))]))
)
;; (select-instr '(program (x)
;; (assign x (+ 10 32))
;; (assign x (+ 10 x))))
(define (assign-homes e)
(letrec ([mem-idx 0]
[env '()]
[var->stack
(lambda(ee)
(cond
[(and (pair? ee) (eq? (car ee) 'var))
(cond
[(assq (cadr ee) env) =>
(lambda(entry)
`(deref rbp ,(cadr entry))
)]
[else
(set! mem-idx (- mem-idx 8))
(set! env (cons `(,(cadr ee) ,mem-idx) env))
`(deref rbp ,mem-idx)
])
]
[else ee]))]
[inner
(lambda(ee)
(match ee
[`(program ,size ,instrs ...)
`(program ,size ,@(map inner instrs))]
[`(,op ,es ...)
`(,op ,@(map var->stack es))]
))])
(inner e)))
(define (patch-instr e)
(match e
[`(program ,size ,instrs ...)
(let* ([patched (map patch-instr instrs)]
[lst (foldr append '() patched)])
`(program ,size ,@lst))]
[`(,op ,e1 ,e2)
(if (and (eq? 'deref (car e1))
(eq? 'deref (car e2)))
`((movq ,e1 (reg rax))
(,op (reg rax) ,e2))
`((,op ,e1 ,e2)))]
[else `(,e)]))
(define (print-x86 e)
(define (arg->str arg)
(match arg
[`(int ,e) (format "$~a" e)]
[`(reg ,register) (format "%~a" register)]
[`(deref ,register ,int) (format "~a(%~a)" int register)]
[else (format "~a" arg)]))
(match e
[`(program (,size) ,instrs ...)
(let* ([str-lst (map print-x86 instrs)]
[start (format ".globl main\nmain:\npushq %rbp\nmovq %rsp, %rbp\nsubq $~a, %rsp\n" size)]
[end (format "addq $~a, %rsp\npopq %rbp\nmovq $0, %rax\nretq\n" size)]
[body (foldr string-append "" str-lst)])
(format "~a~amovq %rax, %rdi \ncallq print_int\n~a" start body end))]
[`(,op ,arg1 ,arg2)
(format "~a ~a, ~a\n" op (arg->str arg1) (arg->str arg2))]
[`(,op ,arg)
(format "~a ~a\n" op (arg->str arg))]))
;; (display (print-x86 '(program
;; (24)
;; (movq (int 14) (deref rbp -8))
;; (movq (deref rbp -8) (reg rax))
;; (movq (reg rax) (deref rbp -16))
;; (movq (deref rbp -8) (reg rax))
;; (addq (reg rax) (deref rbp -16))
;; (movq (deref rbp -16) (reg rax))
;; (movq (reg rax) (deref rbp -24))
;; (movq (deref rbp -8) (reg rax))
;; (addq (reg rax) (deref rbp -24))
;; (movq (deref rbp -24) (reg rax)))))
;; Define the passes to be used by interp-tests and the grader
;; Note that your compiler file (or whatever file provides your passes)
;; should be named "compiler.rkt"
(define r1-passes
`(("uniquify" ,(uniquify '()) ,interp-scheme)
("flatten" ,flatten ,interp-C)
("select-instructions" ,select-instr ,interp-x86)
("assign-homes" ,assign-homes ,interp-x86)
("patch-instr" ,patch-instr ,interp-x86)
("print-x86" ,print-x86 #f)
))