-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompile.rkt
276 lines (252 loc) · 7.14 KB
/
compile.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
#lang racket
(provide (all-defined-out))
(require "ast.rkt" "types.rkt" a86/ast)
;; Registers used
(define rax 'rax) ; return
(define rbx 'rbx) ; heap
(define r8 'r8) ; scratch in +, -
(define r9 'r9) ; scratch in assert-type
(define rsp 'rsp) ; stack
(define rdi 'rdi) ; arg
;; type CEnv = [Listof Variable]
;; Expr -> Asm
(define (compile e)
(prog (Extern 'peek_byte)
(Extern 'read_byte)
(Extern 'write_byte)
(Extern 'raise_error)
(Label 'entry)
(Mov rbx rdi) ; recv heap pointer
(compile-e e '())
(Ret)))
;; Expr CEnv -> Asm
(define (compile-e e c)
(match e
[(Int i) (compile-value i)]
[(Bool b) (compile-value b)]
[(Char c) (compile-value c)]
[(String s) (compile-string s)]
[(Eof) (compile-value eof)]
[(Empty) (compile-value '())]
[(Var x) (compile-variable x c)]
[(Prim0 p) (compile-prim0 p c)]
[(Prim1 p e) (compile-prim1 p e c)]
[(Prim2 p e1 e2) (compile-prim2 p e1 e2 c)]
[(If e1 e2 e3) (compile-if e1 e2 e3 c)]
[(Begin e1 e2) (compile-begin e1 e2 c)]
[(Let x e1 e2) (compile-let x e1 e2 c)]))
;; TODO: Compile string
(define (compile-string s)
;; TODO: Replace with your solution
(seq)
)
;; Value -> Asm
(define (compile-value v)
(seq (Mov rax (imm->bits v))))
;; Id CEnv -> Asm
(define (compile-variable x c)
(let ((i (lookup x c)))
(seq (Mov rax (Offset rsp i)))))
;; Op0 CEnv -> Asm
(define (compile-prim0 p c)
(match p
['void (seq (Mov rax val-void))]
['read-byte (seq (pad-stack c)
(Call 'read_byte)
(unpad-stack c))]
['peek-byte (seq (pad-stack c)
(Call 'peek_byte)
(unpad-stack c))]))
;; Op1 Expr CEnv -> Asm
(define (compile-prim1 p e c)
(seq (compile-e e c)
(match p
['add1
(seq (assert-integer rax)
(Add rax (imm->bits 1)))]
['sub1
(seq (assert-integer rax)
(Sub rax (imm->bits 1)))]
['zero?
(let ((l1 (gensym)))
(seq (assert-integer rax)
(Cmp rax 0)
(Mov rax val-true)
(Je l1)
(Mov rax val-false)
(Label l1)))]
['char?
(let ((l1 (gensym)))
(seq (And rax mask-char)
(Xor rax type-char)
(Cmp rax 0)
(Mov rax val-true)
(Je l1)
(Mov rax val-false)
(Label l1)))]
['char->integer
(seq (assert-char rax)
(Sar rax char-shift)
(Sal rax int-shift))]
['integer->char
(seq (assert-codepoint #f)
(Sar rax int-shift)
(Sal rax char-shift)
(Xor rax type-char))]
['eof-object? (eq-imm val-eof)]
['write-byte
(seq assert-byte
(pad-stack c)
(Mov rdi rax)
(Call 'write_byte)
(unpad-stack c)
(Mov rax val-void))]
['box
(seq (Mov (Offset rbx 0) rax)
(Mov rax rbx)
(Or rax type-box)
(Add rbx 8))]
['unbox
(seq (assert-box rax)
(Xor rax type-box)
(Mov rax (Offset rax 0)))]
['car
(seq (assert-cons rax)
(Xor rax type-cons)
(Mov rax (Offset rax 8)))]
['cdr
(seq (assert-cons rax)
(Xor rax type-cons)
(Mov rax (Offset rax 0)))]
['empty? (eq-imm val-empty)]
;; TODO: string-length string?
['string? (seq)]
['string-length (seq)]
)))
;; Op2 Expr Expr CEnv -> Asm
(define (compile-prim2 p e1 e2 c)
(seq (compile-e e1 c)
(Push rax)
(compile-e e2 (cons #f c))
(match p
['+
(seq (Pop r8)
(assert-integer r8)
(assert-integer rax)
(Add rax r8))]
['-
(seq (Pop r8)
(assert-integer r8)
(assert-integer rax)
(Sub r8 rax)
(Mov rax r8))]
['eq?
(let ((l (gensym)))
(seq (Cmp rax (Offset rsp 0))
(Mov rax val-true)
(Je l)
(Mov rax val-false)
(Label l)
(Add rsp 8)))]
['cons
(seq (Mov (Offset rbx 0) rax)
(Pop rax)
(Mov (Offset rbx 8) rax)
(Mov rax rbx)
(Or rax type-cons)
(Add rbx 16))]
;; TODO: string-ref make-string
['string-ref (seq)]
['make-string (seq)]
)))
;; Imm -> Asm
(define (eq-imm imm)
(let ((l1 (gensym)))
(seq (Cmp rax imm)
(Mov rax val-true)
(Je l1)
(Mov rax val-false)
(Label l1))))
;; Expr Expr Expr CEnv -> Asm
(define (compile-if e1 e2 e3 c)
(let ((l1 (gensym 'if))
(l2 (gensym 'if)))
(seq (compile-e e1 c)
(Cmp rax val-false)
(Je l1)
(compile-e e2 c)
(Jmp l2)
(Label l1)
(compile-e e3 c)
(Label l2))))
;; Expr Expr CEnv -> Asm
(define (compile-begin e1 e2 c)
(seq (compile-e e1 c)
(compile-e e2 c)))
;; Id Expr Expr CEnv -> Asm
(define (compile-let x e1 e2 c)
(seq (compile-e e1 c)
(Push rax)
(compile-e e2 (cons x c))
(Add rsp 8)))
;; CEnv -> Asm
;; Pad the stack to be aligned for a call
(define (pad-stack c)
(match (even? (length c))
[#t (seq (Sub rsp 8))]
[#f (seq)]))
;; CEnv -> Asm
;; Undo the stack alignment after a call
(define (unpad-stack c)
(match (even? (length c))
[#t (seq (Add rsp 8))]
[#f (seq)]))
;; Id CEnv -> Integer
(define (lookup x cenv)
(match cenv
['() (error "undefined variable:" x)]
[(cons y rest)
(match (eq? x y)
[#t 0]
[#f (+ 8 (lookup x rest))])]))
(define (assert-type mask type)
(λ (arg)
(seq (Mov r9 arg)
(And r9 mask)
(Cmp r9 type)
(Jne 'raise_error))))
(define (type-pred mask type)
(let ((l (gensym)))
(seq (And rax mask)
(Cmp rax type)
(Mov rax (imm->bits #t))
(Je l)
(Mov rax (imm->bits #f))
(Label l))))
(define assert-integer
(assert-type mask-int type-int))
(define assert-char
(assert-type mask-char type-char))
(define assert-box
(assert-type ptr-mask type-box))
(define assert-cons
(assert-type ptr-mask type-cons))
(define (assert-codepoint x)
(let ((ok (gensym)))
(seq (assert-integer rax)
(Cmp rax (imm->bits 0))
(Jl 'raise_error)
(Cmp rax (imm->bits 1114111))
(Jg 'raise_error)
(Cmp rax (imm->bits 55295))
(Jl ok)
(Cmp rax (imm->bits 57344))
(Jg ok)
(Jmp 'raise_error)
(Label ok))))
(define assert-byte
(seq (assert-integer rax)
(Cmp rax (imm->bits 0))
(Jl 'raise_error)
(Cmp rax (imm->bits 255))
(Jg 'raise_error)))