Skip to content

Commit b525eca

Browse files
committed
Polishing compilers.
1 parent 18d7307 commit b525eca

3 files changed

Lines changed: 35 additions & 37 deletions

File tree

www/notes/fraud/compile-refactor.rkt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33

44
;; type CEnv = [Listof Variable]
55

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+
614
;; Expr -> Asm
715
(define (compile e)
816
`(entry
@@ -27,25 +35,25 @@
2735

2836
;; Integer -> Asm
2937
(define (compile-integer i)
30-
`((mov rax ,(* i 2))))
38+
`((mov rax ,(arithmetic-shift i imm-shift))))
3139

3240
;; Boolean -> Asm
3341
(define (compile-boolean b)
34-
`((mov rax ,(if b #b11 #b01))))
42+
`((mov rax ,(if b imm-type-true imm-type-false))))
3543

3644
;; Expr CEnv -> Asm
3745
(define (compile-add1 e0 c)
3846
(let ((c0 (compile-e e0 c)))
3947
`(,@c0
4048
,@assert-integer
41-
(add rax 2))))
49+
(add rax ,(arithmetic-shift 1 imm-shift)))))
4250

4351
;; Expr CEnv -> Asm
4452
(define (compile-sub1 e0 c)
4553
(let ((c0 (compile-e e0 c)))
4654
`(,@c0
4755
,@assert-integer
48-
(sub rax 2))))
56+
(sub rax ,(arithmetic-shift 1 imm-shift)))))
4957

5058
;; Expr CEnv -> Asm
5159
(define (compile-zero? e0 c)
@@ -55,9 +63,9 @@
5563
`(,@c0
5664
,@assert-integer
5765
(cmp rax 0)
58-
(mov rax #b01) ; #f
66+
(mov rax ,imm-type-false)
5967
(jne ,l0)
60-
(mov rax #b11) ; #t
68+
(mov rax ,imm-type-true)
6169
,l0)))
6270

6371
;; Expr Expr Expr CEnv -> Asm
@@ -68,7 +76,7 @@
6876
(l0 (gensym))
6977
(l1 (gensym)))
7078
`(,@c0
71-
(cmp rax #b01) ; compare to #f
79+
(cmp rax ,imm-type-false)
7280
(je ,l0) ; jump to c2 if #f
7381
,@c1
7482
(jmp ,l1) ; jump past c2
@@ -100,6 +108,6 @@
100108

101109
(define assert-integer
102110
`((mov rbx rax)
103-
(and rbx 1)
104-
(cmp rbx 0)
111+
(and rbx ,imm-type-mask)
112+
(cmp rbx ,imm-type-int)
105113
(jne err)))

www/notes/grift/compile.rkt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#lang racket
22

3+
(define imm-shift 3)
4+
(define imm-type-mask (sub1 (arithmetic-shift 1 imm-shift)))
5+
(define imm-type-int #b000)
6+
(define imm-type-true #b001)
7+
(define imm-type-false #b010)
8+
(define imm-type-empty #b011)
9+
(define imm-type-char #b100)
10+
311
;; Expr -> Asm
412
(define (compile e)
513
`(entry
@@ -26,25 +34,25 @@
2634

2735
;; Integer -> Asm
2836
(define (compile-integer i)
29-
`((mov rax ,(* i 2))))
37+
`((mov rax ,(arithmetic-shift i imm-shift))))
3038

3139
;; Boolean -> Asm
3240
(define (compile-boolean b)
33-
`((mov rax ,(if b #b11 #b01))))
41+
`((mov rax ,(if b imm-type-true imm-type-false))))
3442

3543
;; Expr CEnv -> Asm
36-
(define (compile-add1 e0 c)
44+
(define (compile-add1 e0 c)
3745
(let ((c0 (compile-e e0 c)))
3846
`(,@c0
3947
,@assert-integer
40-
(add rax 2))))
48+
(add rax ,(arithmetic-shift 1 imm-shift)))))
4149

4250
;; Expr CEnv -> Asm
4351
(define (compile-sub1 e0 c)
4452
(let ((c0 (compile-e e0 c)))
4553
`(,@c0
4654
,@assert-integer
47-
(sub rax 2))))
55+
(sub rax ,(arithmetic-shift 1 imm-shift)))))
4856

4957
;; Expr CEnv -> Asm
5058
(define (compile-zero? e0 c)
@@ -54,9 +62,9 @@
5462
`(,@c0
5563
,@assert-integer
5664
(cmp rax 0)
57-
(mov rax #b01) ; #f
65+
(mov rax ,imm-type-false)
5866
(jne ,l0)
59-
(mov rax #b11) ; #t
67+
(mov rax ,imm-type-true)
6068
,l0)))
6169

6270
;; Expr Expr Expr CEnv -> Asm
@@ -67,7 +75,7 @@
6775
(l0 (gensym))
6876
(l1 (gensym)))
6977
`(,@c0
70-
(cmp rax #b01) ; compare to #f
78+
(cmp rax ,imm-type-false)
7179
(je ,l0) ; jump to c2 if #f
7280
,@c1
7381
(jmp ,l1) ; jump past c2
@@ -121,6 +129,6 @@
121129

122130
(define assert-integer
123131
`((mov rbx rax)
124-
(and rbx 1)
125-
(cmp rbx 0)
132+
(and rbx ,imm-type-mask)
133+
(cmp rbx ,imm-type-int)
126134
(jne err)))

www/notes/hustle/compile.rkt

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
;; Expr -> Asm
2828
(define (compile e)
2929
`(entry
30-
(sar rdi 4) ;; align on quad boundary
31-
(sal rdi 4)
32-
(add rdi 16)
3330
,@(compile-e e '())
3431
ret
3532
err
@@ -195,21 +192,6 @@
195192
,@assert-integer
196193
(sub rax (offset rsp ,(- (add1 (length c))))))))
197194

198-
;; code for "abc"
199-
'((mov (offset rdi 0)
200-
(integer->bits 3))
201-
(mov (offset rdi 1)
202-
(compile-char #\a))
203-
(mov (offset rdi 1)
204-
(char->bits #\b))
205-
(mov (offset-rdi 2)
206-
(char->bits #\c))
207-
(mov rdi rax)
208-
(or rax ,str-tag))
209-
210-
211-
212-
213195
;; Variable CEnv -> Natural
214196
(define (lookup x cenv)
215197
(match cenv

0 commit comments

Comments
 (0)