Skip to content

Commit 45bbc63

Browse files
authored
Merge pull request #24 from dvanhorn/next
Next
2 parents 0bbd268 + cb593e8 commit 45bbc63

6 files changed

Lines changed: 53 additions & 82 deletions

File tree

www/assignments/3.scrbl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ language based on the following grammar:
142142

143143
@verbatim{
144144
<expr> ::= integer
145+
| boolean
145146
| ( <compound> )
146147
| [ <compound> ]
147148

@@ -170,6 +171,7 @@ which are defined as:
170171
(racketblock
171172
;; type Token =
172173
;; | Integer
174+
;; | Boolean
173175
;; | 'add1
174176
;; | 'sub1
175177
;; | 'zero?

www/notes/fraud/compile.rkt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#lang racket
22
(provide (all-defined-out))
3-
(require "compile/help.rkt")
43

54
;; type CEnv = [Listof Variable]
65

@@ -42,9 +41,6 @@
4241
(jne ,l0)
4342
(mov rax #b11) ; #t
4443
,l0))]
45-
[(? symbol? x)
46-
(let ((i (lookup x c)))
47-
`((mov rax (offset rsp ,(- (add1 i))))))]
4844
[`(if ,e0 ,e1 ,e2)
4945
(let ((c0 (compile-e e0 c))
5046
(c1 (compile-e e1 c))
@@ -59,6 +55,9 @@
5955
,l0
6056
,@c2
6157
,l1))]
58+
[(? symbol? x)
59+
(let ((i (lookup x c)))
60+
`((mov rax (offset rsp ,(- (add1 i))))))]
6261
[`(let ((,x ,e0)) ,e1)
6362
(let ((c0 (compile-e e0 c))
6463
(c1 (compile-e e1 (cons x c))))
@@ -74,3 +73,9 @@
7473
(match (symbol=? x y)
7574
[#t (length cenv)]
7675
[#f (lookup x cenv)])]))
76+
77+
(define assert-integer
78+
`((mov rbx rax)
79+
(and rbx 1)
80+
(cmp rbx 0)
81+
(jne err)))

www/notes/fraud/compile/help.rkt

Lines changed: 0 additions & 8 deletions
This file was deleted.

www/notes/grift/compile.rkt

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#lang racket
22
(provide (all-defined-out))
3-
(require "compile/help.rkt")
43

54
;; type CEnv = [Listof Variable]
65

@@ -18,55 +17,57 @@
1817
(define (compile-e e c)
1918
(match e
2019
[(? integer? i)
21-
`((mov rax ,(arithmetic-shift i fixnum-shift)))]
20+
`((mov rax ,(* i 2)))]
2221
[(? boolean? b)
23-
`((mov rax ,(if b true-rep false-rep)))]
22+
`((mov rax ,(if b #b11 #b01)))]
2423
[`(add1 ,e0)
2524
(let ((c0 (compile-e e0 c)))
2625
`(,@c0
2726
,@assert-integer
28-
(add rax ,(arithmetic-shift 1 fixnum-shift))))]
27+
(add rax 2)))]
2928
[`(sub1 ,e0)
3029
(let ((c0 (compile-e e0 c)))
3130
`(,@c0
3231
,@assert-integer
33-
(sub rax ,(arithmetic-shift 1 fixnum-shift))))]
32+
(sub rax 2)))]
3433
[`(zero? ,e0)
35-
(let ((c0 (compile-e e0 c)))
34+
(let ((c0 (compile-e e0 c))
35+
(l0 (gensym))
36+
(l1 (gensym)))
3637
`(,@c0
3738
,@assert-integer
3839
(cmp rax 0)
39-
(sete al)
40-
(movzx rax al)
41-
(sal rax ,bool-shift)
42-
(or eax ,false-rep)))]
40+
(mov rax #b01) ; #f
41+
(jne ,l0)
42+
(mov rax #b11) ; #t
43+
,l0))]
4344
[`(if ,e0 ,e1 ,e2)
4445
(let ((c0 (compile-e e0 c))
4546
(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)]))]
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))]
5758
[(? symbol? x)
5859
(let ((i (lookup x c)))
59-
`((mov rax (offset rsp ,(- (add1 i))))))]
60+
`((mov rax (offset rsp ,(- (add1 i))))))]
6061
[`(let ((,x ,e0)) ,e1)
6162
(let ((c0 (compile-e e0 c))
6263
(c1 (compile-e e1 (cons x c))))
6364
`(,@c0
6465
(mov (offset rsp ,(- (add1 (length c)))) rax)
6566
,@c1))]
67+
6668
[`(+ ,e0 ,e1)
6769
(let ((c1 (compile-e e1 c))
6870
(c0 (compile-e e0 (cons #f c))))
69-
7071
`(,@c1
7172
,@assert-integer
7273
(mov (offset rsp ,(- (add1 (length c)))) rax)
@@ -77,7 +78,6 @@
7778
[`(- ,e0 ,e1)
7879
(let ((c1 (compile-e e1 c))
7980
(c0 (compile-e e0 (cons #f c))))
80-
8181
`(,@c1
8282
,@assert-integer
8383
(mov (offset rsp ,(- (add1 (length c)))) rax)
@@ -91,6 +91,12 @@
9191
(match cenv
9292
['() (error "undefined variable:" x)]
9393
[(cons y cenv)
94-
(match (symbol=? x y)
94+
(match (eq? x y)
9595
[#t (length cenv)]
9696
[#f (lookup x cenv)])]))
97+
98+
(define assert-integer
99+
`((mov rbx rax)
100+
(and rbx 1)
101+
(cmp rbx 0)
102+
(jne err)))

www/notes/grift/compile/help.rkt

Lines changed: 0 additions & 28 deletions
This file was deleted.

www/notes/grift/main.c

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,22 @@
22
#include <stdlib.h>
33
#include <inttypes.h>
44

5-
#define fixnum_mask 3
6-
#define fixnum_tag 0
7-
#define fixnum_shift 2
8-
9-
#define boolean_tag 31
10-
#define boolean_mask 127
11-
#define boolean_shift 7
5+
#define typeof_mask 1
6+
#define val_shift 1
7+
#define type_fixnum 0
8+
#define type_bool 1
129

1310
int64_t entry();
1411

1512
int main(int argc, char** argv) {
1613
int64_t result = entry();
17-
if ((result & fixnum_mask) == fixnum_tag) {
18-
printf("%" PRId64 "\n", result >> fixnum_shift);
19-
} else if ((result & boolean_mask) == boolean_tag) {
20-
if (result >> boolean_shift) {
21-
printf("#t\n");
22-
} else {
23-
printf("#f\n");
24-
}
25-
} else {
26-
printf("unknown value\n");
14+
switch (typeof_mask & result) {
15+
case type_fixnum:
16+
printf("%" PRId64 "\n", result >> val_shift);
17+
break;
18+
case type_bool:
19+
printf("#%c\n", result >> val_shift ? 't' : 'f');
20+
break;
2721
}
2822
return 0;
2923
}

0 commit comments

Comments
 (0)