|
3 | 3 |
|
4 | 4 | ;; type CEnv = [Listof Variable] |
5 | 5 |
|
| 6 | +(define imm-shift 3) |
| 7 | +(define imm-type-mask (sub1 (arithmetic-shift 1 imm-shift))) |
| 8 | +(define imm-type-int #b000) |
| 9 | +(define imm-type-true #b001) |
| 10 | +(define imm-type-false #b010) |
| 11 | +(define imm-type-empty #b011) |
| 12 | +(define imm-type-char #b100) |
| 13 | + |
6 | 14 | ;; Expr -> Asm |
7 | 15 | (define (compile e) |
8 | 16 | `(entry |
|
27 | 35 |
|
28 | 36 | ;; Integer -> Asm |
29 | 37 | (define (compile-integer i) |
30 | | - `((mov rax ,(* i 2)))) |
| 38 | + `((mov rax ,(arithmetic-shift i imm-shift)))) |
31 | 39 |
|
32 | 40 | ;; Boolean -> Asm |
33 | 41 | (define (compile-boolean b) |
34 | | - `((mov rax ,(if b #b11 #b01)))) |
| 42 | + `((mov rax ,(if b imm-type-true imm-type-false)))) |
35 | 43 |
|
36 | 44 | ;; Expr CEnv -> Asm |
37 | 45 | (define (compile-add1 e0 c) |
38 | 46 | (let ((c0 (compile-e e0 c))) |
39 | 47 | `(,@c0 |
40 | 48 | ,@assert-integer |
41 | | - (add rax 2)))) |
| 49 | + (add rax ,(arithmetic-shift 1 imm-shift))))) |
42 | 50 |
|
43 | 51 | ;; Expr CEnv -> Asm |
44 | 52 | (define (compile-sub1 e0 c) |
45 | 53 | (let ((c0 (compile-e e0 c))) |
46 | 54 | `(,@c0 |
47 | 55 | ,@assert-integer |
48 | | - (sub rax 2)))) |
| 56 | + (sub rax ,(arithmetic-shift 1 imm-shift))))) |
49 | 57 |
|
50 | 58 | ;; Expr CEnv -> Asm |
51 | 59 | (define (compile-zero? e0 c) |
|
55 | 63 | `(,@c0 |
56 | 64 | ,@assert-integer |
57 | 65 | (cmp rax 0) |
58 | | - (mov rax #b01) ; #f |
| 66 | + (mov rax ,imm-type-false) |
59 | 67 | (jne ,l0) |
60 | | - (mov rax #b11) ; #t |
| 68 | + (mov rax ,imm-type-true) |
61 | 69 | ,l0))) |
62 | 70 |
|
63 | 71 | ;; Expr Expr Expr CEnv -> Asm |
|
68 | 76 | (l0 (gensym)) |
69 | 77 | (l1 (gensym))) |
70 | 78 | `(,@c0 |
71 | | - (cmp rax #b01) ; compare to #f |
| 79 | + (cmp rax ,imm-type-false) |
72 | 80 | (je ,l0) ; jump to c2 if #f |
73 | 81 | ,@c1 |
74 | 82 | (jmp ,l1) ; jump past c2 |
|
100 | 108 |
|
101 | 109 | (define assert-integer |
102 | 110 | `((mov rbx rax) |
103 | | - (and rbx 1) |
104 | | - (cmp rbx 0) |
| 111 | + (and rbx ,imm-type-mask) |
| 112 | + (cmp rbx ,imm-type-int) |
105 | 113 | (jne err))) |
0 commit comments