Skip to content

Commit 0204e39

Browse files
committed
Fix typo in Jig+ compile.rkt causing incorrect tail call behavior
Also includes minor typographical updates
1 parent 1e8e3a5 commit 0204e39

File tree

8 files changed

+34
-41
lines changed

8 files changed

+34
-41
lines changed

langs/fraud/ast.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#lang racket
2-
(provide Lit Prim0 Prim1 Prim2 If Eof Begin
3-
Let Var)
2+
(provide Lit Prim0 Prim1 Prim2 If Eof Begin Let
3+
Var)
44
;;
55
;; type Expr = (Lit Datum)
66
;; | (Eof)

langs/fraud/compile-ops.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
(Mov rdi rax)
5454
(Call 'write_byte)
5555
unpad-stack)]))
56-
56+
5757

5858
;; Op2 -> Asm
5959
(define (compile-op2 p)
@@ -79,7 +79,7 @@
7979
(seq (Pop r8)
8080
(assert-integer r8)
8181
(assert-integer rax)
82-
(Cmp r8 rax)
82+
(Cmp r8 rax)
8383
if-equal)]))
8484

8585

langs/fraud/compile.rkt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
(define r15 'r15) ; stack pad (non-volatile)
1010

1111
;; Expr -> Asm
12-
(define (compile e)
12+
(define (compile e)
1313
(prog (Global 'entry)
1414
(Extern 'peek_byte)
1515
(Extern 'read_byte)
@@ -26,14 +26,12 @@
2626
(Call 'raise_error)))
2727

2828
;; type CEnv = (Listof [Maybe Id])
29-
30-
;; Expr -> Asm
3129
;; Expr CEnv -> Asm
3230
(define (compile-e e c)
3331
(match e
3432
[(Lit d) (compile-value d)]
3533
[(Eof) (compile-value eof)]
36-
[(Var x) (compile-variable x c)]
34+
[(Var x) (compile-variable x c)]
3735
[(Prim0 p) (compile-prim0 p)]
3836
[(Prim1 p e) (compile-prim1 p e c)]
3937
[(Prim2 p e1 e2) (compile-prim2 p e1 e2 c)]
@@ -57,7 +55,6 @@
5755
(define (compile-prim0 p)
5856
(compile-op0 p))
5957

60-
;; Op1 Expr -> Asm
6158
;; Op1 Expr CEnv -> Asm
6259
(define (compile-prim1 p e c)
6360
(seq (compile-e e c)
@@ -70,7 +67,6 @@
7067
(compile-e e2 (cons #f c))
7168
(compile-op2 p)))
7269

73-
;; Expr Expr Expr -> Asm
7470
;; Expr Expr Expr CEnv -> Asm
7571
(define (compile-if e1 e2 e3 c)
7672
(let ((l1 (gensym 'if))
@@ -84,7 +80,6 @@
8480
(compile-e e3 c)
8581
(Label l2))))
8682

87-
;; Expr Expr -> Asm
8883
;; Expr Expr CEnv -> Asm
8984
(define (compile-begin e1 e2 c)
9085
(seq (compile-e e1 c)
@@ -105,4 +100,3 @@
105100
(match (eq? x y)
106101
[#t 0]
107102
[#f (+ 8 (lookup x rest))])]))
108-

langs/fraud/interp.rkt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,3 @@
6161
(define (ext r x v)
6262
(cons (list x v) r))
6363

64-

langs/fraud/test/interp.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
(require "../interp-io.rkt")
44
(require "../parse.rkt")
55
(require "test-runner.rkt")
6-
6+
77
(test (λ (e) (interp (parse e))))
88

99
(test/io (λ (in e) (interp/io (parse e) in)))

langs/fraud/test/test-runner.rkt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
(check-equal? (run '(if #f 3 4)) 4)
3535
(check-equal? (run '(if 0 3 4)) 3)
3636
(check-equal? (run '(zero? 4)) #f)
37-
(check-equal? (run '(zero? 0)) #t))
38-
37+
(check-equal? (run '(zero? 0)) #t))
38+
3939
(begin ;; Dodger
4040
(check-equal? (run #\a) #\a)
4141
(check-equal? (run #\b) #\b)
@@ -72,7 +72,7 @@
7272
(check-equal? (run '(let ((x 7)) (let ((y 2)) x))) 7)
7373
(check-equal? (run '(let ((x 7)) (let ((x 2)) x))) 2)
7474
(check-equal? (run '(let ((x 7)) (let ((x (add1 x))) x))) 8)
75-
75+
7676
(check-equal? (run '(let ((x 0))
7777
(if (zero? x) 7 8)))
7878
7)
@@ -87,7 +87,7 @@
8787
(let ((z (- 4 x)))
8888
(+ (+ x x) z))))
8989
7)
90-
90+
9191
(check-equal? (run '(= 5 5)) #t)
9292
(check-equal? (run '(= 4 5)) #f)
9393
(check-equal? (run '(= (add1 4) 5)) #t)
@@ -107,7 +107,7 @@
107107
(check-equal? (run "a" '(eof-object? (read-byte))) (cons #f ""))
108108
(check-equal? (run "" '(begin (write-byte 97) (write-byte 98)))
109109
(cons (void) "ab"))
110-
110+
111111
(check-equal? (run "ab" '(peek-byte)) (cons 97 ""))
112112
(check-equal? (run "ab" '(begin (peek-byte) (read-byte))) (cons 97 ""))
113113
(check-equal? (run "" '(read-byte)) (cons 226 ""))
@@ -128,4 +128,3 @@
128128
(check-equal? (run "b" '(let ((x 97)) (begin (peek-byte) x)))
129129
(cons 97 ""))))
130130

131-

www/notes/jig.scrbl

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ Here's what this code will compile to, roughly:
212212
(asm-interp
213213
(seq (Global 'entry)
214214
(Label 'entry)
215-
215+
216216
;; calling (f 100), so set up return address,
217217
;; push argument, then jump
218218
(Lea 'rax 'r1)
@@ -221,20 +221,20 @@ Here's what this code will compile to, roughly:
221221
(Push 'rax)
222222
(Jmp 'f)
223223
(Label 'r1)
224-
224+
225225
;; done with (f 100), return
226226
(Ret)
227-
227+
228228
;; (define (f x) ...)
229229
(Label 'f)
230230
(Mov 'rax (Offset 'rsp 0))
231231
(Cmp 'rax 0)
232232
(Jne 'if_false)
233-
233+
234234
;; if-then branch
235235
(Mov 'rax 42)
236236
(Jmp 'done)
237-
237+
238238
;; if-else branch
239239
(Label 'if_false)
240240
;; calling (f (sub1 x)), so set up return address,
@@ -246,7 +246,7 @@ Here's what this code will compile to, roughly:
246246
(Push 'rax)
247247
(Jmp 'f)
248248
(Label 'r2)
249-
249+
250250
(Label 'done)
251251
(Add 'rsp 8) ; pop x
252252
(Ret)))
@@ -348,11 +348,11 @@ You can see where this is going.
348348
| . |
349349
| . |
350350
| . |
351-
+----------------------+
351+
+----------------------+
352352
| return to r2 |
353353
+----------------------+
354354
| x : 1 |
355-
+----------------------+
355+
+----------------------+
356356
| return to r2 |
357357
+----------------------+
358358
rsp ---> | x : 0 |
@@ -422,7 +422,7 @@ We can modify the code to embody these ideas:
422422
(asm-interp
423423
(seq (Global 'entry)
424424
(Label 'entry)
425-
425+
426426
;; calling (f 100), so set up return address,
427427
;; push argument, then jump
428428
(Lea 'rax 'r1)
@@ -431,20 +431,20 @@ We can modify the code to embody these ideas:
431431
(Push 'rax)
432432
(Jmp 'f)
433433
(Label 'r1)
434-
434+
435435
;; done with (f 100), return
436436
(Ret)
437-
437+
438438
;; (define (f x) ...)
439439
(Label 'f)
440440
(Mov 'rax (Offset 'rsp 0))
441441
(Cmp 'rax 0)
442442
(Jne 'if_false)
443-
443+
444444
;; if-then branch
445445
(Mov 'rax 42)
446446
(Jmp 'done)
447-
447+
448448
;; if-else branch
449449
(Label 'if_false)
450450
;; TAIL calling (f (sub1 x)),
@@ -456,7 +456,7 @@ We can modify the code to embody these ideas:
456456
(Add 'rsp 8) ; pop x
457457
(Push 'rax) ; push arg
458458
(Jmp 'f)
459-
459+
460460
(Label 'done)
461461
(Add 'rsp 8) ; pop x
462462
(Ret)))
@@ -550,17 +550,17 @@ call:
550550

551551
;; No need for this since we never come back:
552552
;; (Ret)
553-
553+
554554
;; (define (f x) ...)
555555
(Label 'f)
556556
(Mov 'rax (Offset 'rsp 0))
557557
(Cmp 'rax 0)
558558
(Jne 'if_false)
559-
559+
560560
;; if-then branch
561561
(Mov 'rax 42)
562562
(Jmp 'done)
563-
563+
564564
;; if-else branch
565565
(Label 'if_false)
566566
;; TAIL calling (f (sub1 x)),
@@ -572,7 +572,7 @@ call:
572572
(Add 'rsp 8) ; pop x
573573
(Push 'rax) ; push arg
574574
(Jmp 'f)
575-
575+
576576
(Label 'done)
577577
(Add 'rsp 8) ; pop x
578578
(Ret)))
@@ -637,7 +637,7 @@ ready to be made, the stack will look like:
637637
| 3 |
638638
+----------------------+
639639
rsp ---> | 5 |
640-
+----------------------+
640+
+----------------------+
641641
}|
642642

643643
At which point we need to remove the @racket[x] and @racket[y] part,
@@ -651,7 +651,7 @@ below the return address, i.e. we want:
651651
| 3 |
652652
+----------------------+
653653
rsp ---> | 5 |
654-
+----------------------+
654+
+----------------------+
655655
}|
656656

657657
To accomplish, we rely on the following helper function for generating
@@ -732,6 +732,7 @@ There are two important places where @racket[t?] is seeded to @racket[#t]:
732732
@item{The body of every function is in tail position.}
733733
]
734734

735+
735736
The complete compiler:
736737

737738
@codeblock-include["jig/compile.rkt"]

ziggy/src/compile.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@
234234
{:> J} ;; Id Expr Expr CEnv Boolean -> Asm
235235
{:> F}
236236
(define (compile-let x e1 e2 c {:> J} t?)
237-
(seq (compile-e e1 c {:> J} t?)
237+
(seq (compile-e e1 c {:> J} #f)
238238
(Push rax)
239239
(compile-e e2 (cons x c) {:> J} t?)
240240
(Add rsp 8)))

0 commit comments

Comments
 (0)