forked from ParkerTewell/Typed-Iniquity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile-ops.rkt~
361 lines (329 loc) · 8.7 KB
/
compile-ops.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
#lang racket
(provide (all-defined-out))
(require "ast.rkt" "types.rkt" a86/ast)
(define rax 'rax) ; return
(define eax 'eax) ; 32-bit load/store
(define rbx 'rbx) ; heap
(define rdi 'rdi) ; arg
(define r8 'r8) ; scratch
(define r9 'r9) ; scratch
(define r10 'r10) ; scratch
(define r15 'r15) ; stack pad (non-volatile)
(define rsp 'rsp) ; stack
;; Op0 -> Asm
(define (compile-op0 p)
(match p
['void (seq (Mov rax val-void))]
['read-byte (seq pad-stack
(Call 'read_byte)
unpad-stack)]
['peek-byte (seq pad-stack
(Call 'peek_byte)
unpad-stack)]))
;; Op1 -> Asm
(define (compile-op1 p)
(match p
['add1
(seq (assert-integer rax)
(Add rax (imm->bits 1)))]
['sub1
(seq (assert-integer rax)
(Sub rax (imm->bits 1)))]
['zero?
(seq (assert-integer rax)
(eq-imm 0))]
['char?
(type-pred mask-char type-char)]
['char->integer
(seq (assert-char rax)
(Sar rax char-shift)
(Sal rax int-shift))]
['integer->char
(seq (assert-codepoint rax)
(Sar rax int-shift)
(Sal rax char-shift)
(Xor rax type-char))]
['eof-object? (eq-imm eof)]
['write-byte
(seq (assert-byte rax)
pad-stack
(Mov rdi rax)
(Call 'write_byte)
unpad-stack
(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 '())]
['box?
(type-pred ptr-mask type-box)]
['cons?
(type-pred ptr-mask type-cons)]
['vector?
(type-pred ptr-mask type-vect)]
['string?
(type-pred ptr-mask type-str)]
['vector-length
(let ((zero (gensym))
(done (gensym)))
(seq (assert-vector rax)
(Xor rax type-vect)
(Cmp rax 0)
(Je zero)
(Mov rax (Offset rax 0))
(Sal rax int-shift)
(Jmp done)
(Label zero)
(Mov rax 0)
(Label done)))]
['string-length
(let ((zero (gensym))
(done (gensym)))
(seq (assert-string rax)
(Xor rax type-str)
(Cmp rax 0)
(Je zero)
(Mov rax (Offset rax 0))
(Sal rax int-shift)
(Jmp done)
(Label zero)
(Mov rax 0)
(Label done)))]))
;; Op2 -> Asm
(define (compile-op2 p)
(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))]
['<
(seq (Pop r8)
(assert-integer r8)
(assert-integer rax)
(Cmp r8 rax)
(Mov rax val-true)
(let ((true (gensym)))
(seq (Jl true)
(Mov rax val-false)
(Label true))))]
['=
(seq (Pop r8)
(assert-integer r8)
(assert-integer rax)
(Cmp r8 rax)
(Mov rax val-true)
(let ((true (gensym)))
(seq (Je true)
(Mov rax val-false)
(Label true))))]
['cons
(seq (Mov (Offset rbx 0) rax)
(Pop rax)
(Mov (Offset rbx 8) rax)
(Mov rax rbx)
(Or rax type-cons)
(Add rbx 16))]
['eq?
(seq (Pop r8)
(eq r8 rax))]
['make-vector
(let ((loop (gensym))
(done (gensym))
(empty (gensym)))
(seq (Pop r8)
(assert-natural r8)
(Cmp r8 0) ; special case empty vector
(Je empty)
(Mov r9 rbx)
(Or r9 type-vect)
(Sar r8 int-shift)
(Mov (Offset rbx 0) r8)
(Add rbx 8)
(Label loop)
(Mov (Offset rbx 0) rax)
(Add rbx 8)
(Sub r8 1)
(Cmp r8 0)
(Jne loop)
(Mov rax r9)
(Jmp done)
(Label empty)
(Mov rax type-vect)
(Label done)))]
['vector-ref
(seq (Pop r8)
(assert-vector r8)
(assert-integer rax)
(Cmp rax 0)
(Jl 'raise_error_align)
(Xor r8 type-vect) ; r8 = ptr
(Mov r9 (Offset r8 0)) ; r9 = len
(Sar rax int-shift) ; rax = index
(Sub r9 1)
(Cmp r9 rax)
(Jl 'raise_error_align)
(Sal rax 3)
(Add r8 rax)
(Mov rax (Offset r8 8)))]
['make-string
(let ((loop (gensym))
(done (gensym))
(empty (gensym)))
(seq (Pop r8)
(assert-natural r8)
(assert-char rax)
(Cmp r8 0) ; special case empty string
(Je empty)
(Mov r9 rbx)
(Or r9 type-str)
(Sar r8 int-shift)
(Mov (Offset rbx 0) r8)
(Add rbx 8)
(Sar rax char-shift)
(Add r8 1) ; adds 1
(Sar r8 1) ; when
(Sal r8 1) ; len is odd
(Label loop)
(Mov (Offset rbx 0) eax)
(Add rbx 4)
(Sub r8 1)
(Cmp r8 0)
(Jne loop)
(Mov rax r9)
(Jmp done)
(Label empty)
(Mov rax type-str)
(Label done)))]
['string-ref
(seq (Pop r8)
(assert-string r8)
(assert-integer rax)
(Cmp rax 0)
(Jl 'raise_error_align)
(Xor r8 type-str) ; r8 = ptr
(Mov r9 (Offset r8 0)) ; r9 = len
(Sar rax int-shift) ; rax = index
(Sub r9 1)
(Cmp r9 rax)
(Jl 'raise_error_align)
(Sal rax 2)
(Add r8 rax)
(Mov 'eax (Offset r8 8))
(Sal rax char-shift)
(Or rax type-char))]))
;; Op3 -> Asm
(define (compile-op3 p)
(match p
['vector-set!
(seq (Pop r10)
(Pop r8)
(assert-vector r8)
(assert-integer r10)
(Cmp r10 0)
(Jl 'raise_error_align)
(Xor r8 type-vect) ; r8 = ptr
(Mov r9 (Offset r8 0)) ; r9 = len
(Sar r10 int-shift) ; r10 = index
(Sub r9 1)
(Cmp r9 r10)
(Jl 'raise_error_align)
(Sal r10 3)
(Add r8 r10)
(Mov (Offset r8 8) rax)
(Mov rax val-void))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (assert-type mask type)
(λ (arg)
(seq (Mov r9 arg)
(And r9 mask)
(Cmp r9 type)
(Jne 'raise_error_align))))
(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-vector
(assert-type ptr-mask type-vect))
(define assert-string
(assert-type ptr-mask type-str))
(define (assert-codepoint r)
(let ((ok (gensym)))
(seq (assert-integer r)
(Cmp r (imm->bits 0))
(Jl 'raise_error_align)
(Cmp r (imm->bits 1114111))
(Jg 'raise_error_align)
(Cmp r (imm->bits 55295))
(Jl ok)
(Cmp r (imm->bits 57344))
(Jg ok)
(Jmp 'raise_error_align)
(Label ok))))
(define (assert-byte r)
(seq (assert-integer r)
(Cmp r (imm->bits 0))
(Jl 'raise_error_align)
(Cmp r (imm->bits 255))
(Jg 'raise_error_align)))
(define (assert-natural r)
(seq (assert-integer r)
(Cmp r (imm->bits 0))
(Jl 'raise_error_align)))
;; Value -> Asm
(define (eq-imm imm)
(let ((l1 (gensym)))
(seq (Cmp rax (imm->bits imm))
(Mov rax val-true)
(Je l1)
(Mov rax val-false)
(Label l1))))
(define (eq ir1 ir2)
(let ((l1 (gensym)))
(seq (Cmp ir1 ir2)
(Mov rax val-true)
(Je l1)
(Mov rax val-false)
(Label l1))))
;; Asm
;; Dynamically pad the stack to be aligned for a call
(define pad-stack
(seq (Mov r15 rsp)
(And r15 #b1000)
(Sub rsp r15)))
;; Asm
;; Undo the stack alignment after a call
(define unpad-stack
(seq (Add rsp r15)))