|
1 | 1 | #lang racket |
2 | | -(provide (all-defined-out)) |
3 | | - |
4 | | -;; type CEnv = [Listof Variable] |
5 | 2 |
|
6 | 3 | ;; Expr -> Asm |
7 | 4 | (define (compile e) |
8 | 5 | `(entry |
9 | 6 | ,@(compile-e e '()) |
10 | 7 | ret |
11 | 8 | err |
12 | | - (push rbp) |
| 9 | + (push rbp) |
13 | 10 | (call error) |
14 | 11 | ret)) |
15 | 12 |
|
16 | 13 | ;; Expr CEnv -> Asm |
17 | 14 | (define (compile-e e c) |
18 | 15 | (match e |
19 | | - [(? integer? i) |
20 | | - `((mov rax ,(* i 2)))] |
21 | | - [(? boolean? b) |
22 | | - `((mov rax ,(if b #b11 #b01)))] |
23 | | - [`(add1 ,e0) |
24 | | - (let ((c0 (compile-e e0 c))) |
25 | | - `(,@c0 |
26 | | - ,@assert-integer |
27 | | - (add rax 2)))] |
28 | | - [`(sub1 ,e0) |
29 | | - (let ((c0 (compile-e e0 c))) |
30 | | - `(,@c0 |
31 | | - ,@assert-integer |
32 | | - (sub rax 2)))] |
33 | | - [`(zero? ,e0) |
34 | | - (let ((c0 (compile-e e0 c)) |
35 | | - (l0 (gensym)) |
36 | | - (l1 (gensym))) |
37 | | - `(,@c0 |
38 | | - ,@assert-integer |
39 | | - (cmp rax 0) |
40 | | - (mov rax #b01) ; #f |
41 | | - (jne ,l0) |
42 | | - (mov rax #b11) ; #t |
43 | | - ,l0))] |
44 | | - [`(if ,e0 ,e1 ,e2) |
45 | | - (let ((c0 (compile-e e0 c)) |
46 | | - (c1 (compile-e e1 c)) |
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))] |
58 | | - [(? symbol? x) |
59 | | - (let ((i (lookup x c))) |
60 | | - `((mov rax (offset rsp ,(- (add1 i))))))] |
61 | | - [`(let ((,x ,e0)) ,e1) |
62 | | - (let ((c0 (compile-e e0 c)) |
63 | | - (c1 (compile-e e1 (cons x c)))) |
64 | | - `(,@c0 |
65 | | - (mov (offset rsp ,(- (add1 (length c)))) rax) |
66 | | - ,@c1))] |
| 16 | + [(? integer? i) (compile-integer i)] |
| 17 | + [(? boolean? b) (compile-boolean b)] |
| 18 | + [(? symbol? x) (compile-variable x c)] |
| 19 | + [`(add1 ,e0) (compile-add1 e0 c)] |
| 20 | + [`(sub1 ,e0) (compile-sub1 e0 c)] |
| 21 | + [`(zero? ,e0) (compile-zero? e0 c)] |
| 22 | + [`(if ,e0 ,e1 ,e2) (compile-if e0 e1 e2 c)] |
| 23 | + [`(let ((,x ,e0)) ,e1) (compile-let x e0 e1 c)] |
| 24 | + [`(+ ,e0 ,e1) (compile-+ e0 e1 c)] |
| 25 | + [`(- ,e0 ,e1) (compile-- e0 e1 c)])) |
| 26 | + |
| 27 | +;; Integer -> Asm |
| 28 | +(define (compile-integer i) |
| 29 | + `((mov rax ,(* i 2)))) |
| 30 | + |
| 31 | +;; Boolean -> Asm |
| 32 | +(define (compile-boolean b) |
| 33 | + `((mov rax ,(if b #b11 #b01)))) |
| 34 | + |
| 35 | +;; Expr CEnv -> Asm |
| 36 | +(define (compile-add1 e0 c) |
| 37 | + (let ((c0 (compile-e e0 c))) |
| 38 | + `(,@c0 |
| 39 | + ,@assert-integer |
| 40 | + (add rax 2)))) |
| 41 | + |
| 42 | +;; Expr CEnv -> Asm |
| 43 | +(define (compile-sub1 e0 c) |
| 44 | + (let ((c0 (compile-e e0 c))) |
| 45 | + `(,@c0 |
| 46 | + ,@assert-integer |
| 47 | + (sub rax 2)))) |
| 48 | + |
| 49 | +;; Expr CEnv -> Asm |
| 50 | +(define (compile-zero? e0 c) |
| 51 | + (let ((c0 (compile-e e0 c)) |
| 52 | + (l0 (gensym)) |
| 53 | + (l1 (gensym))) |
| 54 | + `(,@c0 |
| 55 | + ,@assert-integer |
| 56 | + (cmp rax 0) |
| 57 | + (mov rax #b01) ; #f |
| 58 | + (jne ,l0) |
| 59 | + (mov rax #b11) ; #t |
| 60 | + ,l0))) |
| 61 | + |
| 62 | +;; Expr Expr Expr CEnv -> Asm |
| 63 | +(define (compile-if e0 e1 e2 c) |
| 64 | + (let ((c0 (compile-e e0 c)) |
| 65 | + (c1 (compile-e e1 c)) |
| 66 | + (c2 (compile-e e2 c)) |
| 67 | + (l0 (gensym)) |
| 68 | + (l1 (gensym))) |
| 69 | + `(,@c0 |
| 70 | + (cmp rax #b01) ; compare to #f |
| 71 | + (je ,l0) ; jump to c2 if #f |
| 72 | + ,@c1 |
| 73 | + (jmp ,l1) ; jump past c2 |
| 74 | + ,l0 |
| 75 | + ,@c2 |
| 76 | + ,l1))) |
| 77 | + |
| 78 | +;; Variable CEnv -> Asm |
| 79 | +(define (compile-variable x c) |
| 80 | + (let ((i (lookup x c))) |
| 81 | + `((mov rax (offset rsp ,(- (add1 i))))))) |
67 | 82 |
|
68 | | - [`(+ ,e0 ,e1) |
69 | | - (let ((c1 (compile-e e1 c)) |
70 | | - (c0 (compile-e e0 (cons #f c)))) |
71 | | - `(,@c1 |
72 | | - ,@assert-integer |
73 | | - (mov (offset rsp ,(- (add1 (length c)))) rax) |
74 | | - ,@c0 |
75 | | - ,@assert-integer |
76 | | - (add rax (offset rsp ,(- (add1 (length c)))))))] |
| 83 | +;; Variable Expr Expr CEnv -> Asm |
| 84 | +(define (compile-let x e0 e1 c) |
| 85 | + (let ((c0 (compile-e e0 c)) |
| 86 | + (c1 (compile-e e1 (cons x c)))) |
| 87 | + `(,@c0 |
| 88 | + (mov (offset rsp ,(- (add1 (length c)))) rax) |
| 89 | + ,@c1))) |
77 | 90 |
|
78 | | - [`(- ,e0 ,e1) |
79 | | - (let ((c1 (compile-e e1 c)) |
80 | | - (c0 (compile-e e0 (cons #f c)))) |
81 | | - `(,@c1 |
82 | | - ,@assert-integer |
83 | | - (mov (offset rsp ,(- (add1 (length c)))) rax) |
84 | | - ,@c0 |
85 | | - ,@assert-integer |
86 | | - (sub rax (offset rsp ,(- (add1 (length c)))))))])) |
| 91 | +;; Expr Expr CEnv -> Asm |
| 92 | +(define (compile-+ e0 e1 c) |
| 93 | + (let ((c1 (compile-e e1 c)) |
| 94 | + (c0 (compile-e e0 (cons #f c)))) |
| 95 | + `(,@c1 |
| 96 | + ,@assert-integer |
| 97 | + (mov (offset rsp ,(- (add1 (length c)))) rax) |
| 98 | + ,@c0 |
| 99 | + ,@assert-integer |
| 100 | + (add rax (offset rsp ,(- (add1 (length c)))))))) |
87 | 101 |
|
| 102 | +;; Expr Expr CEnv -> Asm |
| 103 | +(define (compile-- e0 e1 c) |
| 104 | + (let ((c1 (compile-e e1 c)) |
| 105 | + (c0 (compile-e e0 (cons #f c)))) |
| 106 | + `(,@c1 |
| 107 | + ,@assert-integer |
| 108 | + (mov (offset rsp ,(- (add1 (length c)))) rax) |
| 109 | + ,@c0 |
| 110 | + ,@assert-integer |
| 111 | + (sub rax (offset rsp ,(- (add1 (length c)))))))) |
88 | 112 |
|
89 | 113 | ;; Variable CEnv -> Natural |
90 | 114 | (define (lookup x cenv) |
|
0 commit comments