Skip to content

Commit b5d0bca

Browse files
committed
Fix-up Fraud compiler.
1 parent 55504ae commit b5d0bca

2 files changed

Lines changed: 27 additions & 45 deletions

File tree

www/notes/fraud/compile.rkt

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,55 @@
1010
,@(compile-e e '())
1111
ret
1212
err
13-
(push rbp)
13+
(push rbp)
1414
(call error)
1515
ret))
1616

1717
;; Expr CEnv -> Asm
1818
(define (compile-e e c)
1919
(match e
2020
[(? integer? i)
21-
`((mov rax ,(arithmetic-shift i fixnum-shift)))]
21+
`((mov rax ,(* i 2)))]
2222
[(? boolean? b)
23-
`((mov rax ,(if b true-rep false-rep)))]
23+
`((mov rax ,(if b #b11 #b01)))]
2424
[`(add1 ,e0)
2525
(let ((c0 (compile-e e0 c)))
2626
`(,@c0
2727
,@assert-integer
28-
(add rax ,(arithmetic-shift 1 fixnum-shift))))]
28+
(add rax 2)))]
2929
[`(sub1 ,e0)
3030
(let ((c0 (compile-e e0 c)))
3131
`(,@c0
3232
,@assert-integer
33-
(sub rax ,(arithmetic-shift 1 fixnum-shift))))]
33+
(sub rax 2)))]
3434
[`(zero? ,e0)
35-
(let ((c0 (compile-e e0 c)))
35+
(let ((c0 (compile-e e0 c))
36+
(l0 (gensym))
37+
(l1 (gensym)))
3638
`(,@c0
3739
,@assert-integer
3840
(cmp rax 0)
39-
(sete al)
40-
(movzx rax al)
41-
(sal rax ,bool-shift)
42-
(or eax ,false-rep)))]
41+
(mov rax #b01) ; #f
42+
(jne ,l0)
43+
(mov rax #b11) ; #t
44+
,l0))]
45+
[(? symbol? x)
46+
(let ((i (lookup x c)))
47+
`((mov rax (offset rsp ,(- (add1 i))))))]
4348
[`(if ,e0 ,e1 ,e2)
4449
(let ((c0 (compile-e e0 c))
4550
(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)]))]
57-
[(? symbol? x)
58-
(let ((i (lookup x c)))
59-
`((mov rax (offset rsp ,(- (add1 i))))))]
51+
(c2 (compile-e e2 c))
52+
(l0 (gensym))
53+
(l1 (gensym)))
54+
`(,@c0
55+
(cmp rax #b01) ; compare to #f
56+
(je ,l0) ; jump to c2 if #f
57+
,@c1
58+
(jmp ,l1) ; jump past c2
59+
,l0
60+
,@c2
61+
,l1))]
6062
[`(let ((,x ,e0)) ,e1)
6163
(let ((c0 (compile-e e0 c))
6264
(c1 (compile-e e1 (cons x c))))

www/notes/fraud/compile/help.rkt

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,8 @@
11
#lang racket
22
(provide (all-defined-out))
33

4-
;; -> [List Label Label]
5-
;; Guaranteed to be unique on each call
6-
(define gen-if-labels
7-
(let ((i 0))
8-
(λ ()
9-
(set! i (add1 i))
10-
(list (lab "f" i)
11-
(lab "x" i)))))
12-
13-
;; String Integer -> Symbol
14-
(define (lab s i)
15-
(string->symbol (string-append "if_" s "_" (number->string i))))
16-
17-
18-
(define true-rep #b10011111)
19-
(define false-rep #b00011111)
20-
(define fixnum-mask #b11)
21-
(define fixnum-shift 2)
22-
(define bool-shift 8)
23-
244
(define assert-integer
255
`((mov rbx rax)
26-
(and rbx ,fixnum-mask)
6+
(and rbx 1)
277
(cmp rbx 0)
288
(jne err)))

0 commit comments

Comments
 (0)