|
| 1 | +#lang racket |
| 2 | +(provide (all-defined-out)) |
| 3 | + |
| 4 | +(define int-tag #b0000) ; 0, 2, 4... integer |
| 5 | +(define box-tag #b0001) ; 1 box |
| 6 | +(define pair-tag #b0011) ; 3 pairs |
| 7 | +(define vect-tag #b0101) ; 5 vectors |
| 8 | +(define str-tag #b0111) ; 7 strings |
| 9 | +(define true-tag #b1001) ; 9 |
| 10 | +(define false-tag #b1011) ; 11 |
| 11 | +(define empty-tag #b1101) ; 13 |
| 12 | +(define char-tag #b1111) ; 15 |
| 13 | + |
| 14 | +;; Allocate in 64-bit (8-byte) increments, so pointers |
| 15 | +;; end in #b000 and we tag with #b001 for pairs, etc. |
| 16 | + |
| 17 | +;; type CEnv = (Listof (Maybe Variable)) |
| 18 | + |
| 19 | +;; Expr -> Asm |
| 20 | +(define (compile e) |
| 21 | + `(entry |
| 22 | + (add rdi 16) |
| 23 | + ,@(compile-e e '()) |
| 24 | + ret |
| 25 | + err |
| 26 | + (push rbp) |
| 27 | + (call error) |
| 28 | + ret)) |
| 29 | + |
| 30 | +;; Expr CEnv -> Asm |
| 31 | +(define (compile-e e c) |
| 32 | + (match e |
| 33 | + [''() `((mov rax ,empty-tag))] |
| 34 | + [`(box ,e0) |
| 35 | + (let ((c0 (compile-e e0 c))) |
| 36 | + `(,@c0 |
| 37 | + (mov (offset rdi 0) rax) |
| 38 | + (mov rax rdi) |
| 39 | + (or rax ,box-tag) |
| 40 | + (add rdi 8) ; bump by 8 bytes |
| 41 | + ))] |
| 42 | + [`(unbox ,e0) |
| 43 | + (let ((c0 (compile-e e0 c))) |
| 44 | + `(,@c0 |
| 45 | + ;; assert box |
| 46 | + (xor rax ,box-tag) |
| 47 | + (mov rax (offset rax 0))))] |
| 48 | + [`(cons ,e0 ,e1) |
| 49 | + (let ((c0 (compile-e e0 c)) |
| 50 | + (c1 (compile-e e1 (cons #f c)))) |
| 51 | + `(,@c0 |
| 52 | + (mov (offset rsp ,(- (add1 (length c)))) rax) |
| 53 | + ,@c1 |
| 54 | + (mov (offset rdi 0) rax) |
| 55 | + (mov rax (offset rsp ,(- (add1 (length c))))) |
| 56 | + (mov (offset rdi 1) rax) |
| 57 | + (mov rax rdi) |
| 58 | + (or rax ,pair-tag) |
| 59 | + (add rdi 16)))] |
| 60 | + [`(car ,e0) |
| 61 | + (let ((c0 (compile-e e0 c))) |
| 62 | + `(,@c0 |
| 63 | + ;; assert pair |
| 64 | + (xor rax ,pair-tag) |
| 65 | + (mov rax (offset rax 1))))] |
| 66 | + [`(cdr ,e0) |
| 67 | + (let ((c0 (compile-e e0 c))) |
| 68 | + `(,@c0 |
| 69 | + ;; assert pair |
| 70 | + (xor rax ,pair-tag) |
| 71 | + (mov rax (offset rax 0))))] |
| 72 | + [(? integer? i) |
| 73 | + `((mov rax ,(* i 2)))] |
| 74 | + [(? boolean? b) |
| 75 | + `((mov rax ,(if b true-tag false-tag)))] |
| 76 | + [`(add1 ,e0) |
| 77 | + (let ((c0 (compile-e e0 c))) |
| 78 | + `(,@c0 |
| 79 | + ,@assert-integer |
| 80 | + (add rax 2)))] |
| 81 | + [`(sub1 ,e0) |
| 82 | + (let ((c0 (compile-e e0 c))) |
| 83 | + `(,@c0 |
| 84 | + ,@assert-integer |
| 85 | + (sub rax 2)))] |
| 86 | + [`(zero? ,e0) |
| 87 | + (let ((c0 (compile-e e0 c)) |
| 88 | + (l0 (gensym)) |
| 89 | + (l1 (gensym))) |
| 90 | + `(,@c0 |
| 91 | + ,@assert-integer |
| 92 | + (cmp rax 0) |
| 93 | + (mov rax ,false-tag) ; #f |
| 94 | + (jne ,l0) |
| 95 | + (mov rax ,true-tag) ; #t |
| 96 | + ,l0))] |
| 97 | + [`(if ,e0 ,e1 ,e2) |
| 98 | + (let ((c0 (compile-e e0 c)) |
| 99 | + (c1 (compile-e e1 c)) |
| 100 | + (c2 (compile-e e2 c)) |
| 101 | + (l0 (gensym)) |
| 102 | + (l1 (gensym))) |
| 103 | + `(,@c0 |
| 104 | + (cmp rax ,false-tag) ; compare to #f |
| 105 | + (je ,l0) ; jump to c2 if #f |
| 106 | + ,@c1 |
| 107 | + (jmp ,l1) ; jump past c2 |
| 108 | + ,l0 |
| 109 | + ,@c2 |
| 110 | + ,l1))] |
| 111 | + [(? symbol? x) |
| 112 | + (let ((i (lookup x c))) |
| 113 | + `((mov rax (offset rsp ,(- (add1 i))))))] |
| 114 | + [`(let ((,x ,e0)) ,e1) |
| 115 | + (let ((c0 (compile-e e0 c)) |
| 116 | + (c1 (compile-e e1 (cons x c)))) |
| 117 | + `(,@c0 |
| 118 | + (mov (offset rsp ,(- (add1 (length c)))) rax) |
| 119 | + ,@c1))] |
| 120 | + |
| 121 | + [`(+ ,e0 ,e1) |
| 122 | + (let ((c1 (compile-e e1 c)) |
| 123 | + (c0 (compile-e e0 (cons #f c)))) |
| 124 | + `(,@c1 |
| 125 | + ,@assert-integer |
| 126 | + (mov (offset rsp ,(- (add1 (length c)))) rax) |
| 127 | + ,@c0 |
| 128 | + ,@assert-integer |
| 129 | + (add rax (offset rsp ,(- (add1 (length c)))))))] |
| 130 | + |
| 131 | + [`(- ,e0 ,e1) |
| 132 | + (let ((c1 (compile-e e1 c)) |
| 133 | + (c0 (compile-e e0 (cons #f c)))) |
| 134 | + `(,@c1 |
| 135 | + ,@assert-integer |
| 136 | + (mov (offset rsp ,(- (add1 (length c)))) rax) |
| 137 | + ,@c0 |
| 138 | + ,@assert-integer |
| 139 | + (sub rax (offset rsp ,(- (add1 (length c)))))))])) |
| 140 | + |
| 141 | + |
| 142 | +;; Variable CEnv -> Natural |
| 143 | +(define (lookup x cenv) |
| 144 | + (match cenv |
| 145 | + ['() (error "undefined variable:" x)] |
| 146 | + [(cons y cenv) |
| 147 | + (match (eq? x y) |
| 148 | + [#t (length cenv)] |
| 149 | + [#f (lookup x cenv)])])) |
| 150 | + |
| 151 | +(define assert-integer |
| 152 | + `((mov rbx rax) |
| 153 | + (and rbx 1) |
| 154 | + (cmp rbx 0) |
| 155 | + (jne err))) |
0 commit comments