|
1 | 1 | #lang racket |
2 | 2 | (provide (all-defined-out)) |
3 | | -(require "compile/help.rkt") |
4 | 3 |
|
5 | 4 | ;; type CEnv = [Listof Variable] |
6 | 5 |
|
|
18 | 17 | (define (compile-e e c) |
19 | 18 | (match e |
20 | 19 | [(? integer? i) |
21 | | - `((mov rax ,(arithmetic-shift i fixnum-shift)))] |
| 20 | + `((mov rax ,(* i 2)))] |
22 | 21 | [(? boolean? b) |
23 | | - `((mov rax ,(if b true-rep false-rep)))] |
| 22 | + `((mov rax ,(if b #b11 #b01)))] |
24 | 23 | [`(add1 ,e0) |
25 | 24 | (let ((c0 (compile-e e0 c))) |
26 | 25 | `(,@c0 |
27 | 26 | ,@assert-integer |
28 | | - (add rax ,(arithmetic-shift 1 fixnum-shift))))] |
| 27 | + (add rax 2)))] |
29 | 28 | [`(sub1 ,e0) |
30 | 29 | (let ((c0 (compile-e e0 c))) |
31 | 30 | `(,@c0 |
32 | 31 | ,@assert-integer |
33 | | - (sub rax ,(arithmetic-shift 1 fixnum-shift))))] |
| 32 | + (sub rax 2)))] |
34 | 33 | [`(zero? ,e0) |
35 | | - (let ((c0 (compile-e e0 c))) |
| 34 | + (let ((c0 (compile-e e0 c)) |
| 35 | + (l0 (gensym)) |
| 36 | + (l1 (gensym))) |
36 | 37 | `(,@c0 |
37 | 38 | ,@assert-integer |
38 | 39 | (cmp rax 0) |
39 | | - (sete al) |
40 | | - (movzx rax al) |
41 | | - (sal rax ,bool-shift) |
42 | | - (or eax ,false-rep)))] |
| 40 | + (mov rax #b01) ; #f |
| 41 | + (jne ,l0) |
| 42 | + (mov rax #b11) ; #t |
| 43 | + ,l0))] |
43 | 44 | [`(if ,e0 ,e1 ,e2) |
44 | 45 | (let ((c0 (compile-e e0 c)) |
45 | 46 | (c1 (compile-e e1 c)) |
46 | | - (c2 (compile-e e2 c))) |
47 | | - (match (gen-if-labels) |
48 | | - [(list if-f if-x) |
49 | | - `(,@c0 |
50 | | - (cmp rax ,false-rep) |
51 | | - (je ,if-f) |
52 | | - ,@c1 |
53 | | - (jmp ,if-x) |
54 | | - ,if-f |
55 | | - ,@c2 |
56 | | - ,if-x)]))] |
| 47 | + (c2 (compile-e e2 c)) |
| 48 | + (l0 (gensym)) |
| 49 | + (l1 (gensym))) |
| 50 | + `(,@c0 |
| 51 | + (cmp rax #b01) ; compare to #f |
| 52 | + (je ,l0) ; jump to c2 if #f |
| 53 | + ,@c1 |
| 54 | + (jmp ,l1) ; jump past c2 |
| 55 | + ,l0 |
| 56 | + ,@c2 |
| 57 | + ,l1))] |
57 | 58 | [(? symbol? x) |
58 | 59 | (let ((i (lookup x c))) |
59 | | - `((mov rax (offset rsp ,(- (add1 i))))))] |
| 60 | + `((mov rax (offset rsp ,(- (add1 i))))))] |
60 | 61 | [`(let ((,x ,e0)) ,e1) |
61 | 62 | (let ((c0 (compile-e e0 c)) |
62 | 63 | (c1 (compile-e e1 (cons x c)))) |
63 | 64 | `(,@c0 |
64 | 65 | (mov (offset rsp ,(- (add1 (length c)))) rax) |
65 | 66 | ,@c1))] |
| 67 | + |
66 | 68 | [`(+ ,e0 ,e1) |
67 | 69 | (let ((c1 (compile-e e1 c)) |
68 | 70 | (c0 (compile-e e0 (cons #f c)))) |
69 | | - |
70 | 71 | `(,@c1 |
71 | 72 | ,@assert-integer |
72 | 73 | (mov (offset rsp ,(- (add1 (length c)))) rax) |
|
77 | 78 | [`(- ,e0 ,e1) |
78 | 79 | (let ((c1 (compile-e e1 c)) |
79 | 80 | (c0 (compile-e e0 (cons #f c)))) |
80 | | - |
81 | 81 | `(,@c1 |
82 | 82 | ,@assert-integer |
83 | 83 | (mov (offset rsp ,(- (add1 (length c)))) rax) |
|
91 | 91 | (match cenv |
92 | 92 | ['() (error "undefined variable:" x)] |
93 | 93 | [(cons y cenv) |
94 | | - (match (symbol=? x y) |
| 94 | + (match (eq? x y) |
95 | 95 | [#t (length cenv)] |
96 | 96 | [#f (lookup x cenv)])])) |
| 97 | + |
| 98 | +(define assert-integer |
| 99 | + `((mov rbx rax) |
| 100 | + (and rbx 1) |
| 101 | + (cmp rbx 0) |
| 102 | + (jne err))) |
0 commit comments