Skip to content

Commit

Permalink
Fix typo in Jig+ compile.rkt causing incorrect tail call behavior
Browse files Browse the repository at this point in the history
Also includes minor typographical updates
  • Loading branch information
pdarragh committed Nov 7, 2023
1 parent 1e8e3a5 commit 0204e39
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 41 deletions.
4 changes: 2 additions & 2 deletions langs/fraud/ast.rkt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#lang racket
(provide Lit Prim0 Prim1 Prim2 If Eof Begin
Let Var)
(provide Lit Prim0 Prim1 Prim2 If Eof Begin Let
Var)
;;
;; type Expr = (Lit Datum)
;; | (Eof)
Expand Down
4 changes: 2 additions & 2 deletions langs/fraud/compile-ops.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
(Mov rdi rax)
(Call 'write_byte)
unpad-stack)]))


;; Op2 -> Asm
(define (compile-op2 p)
Expand All @@ -79,7 +79,7 @@
(seq (Pop r8)
(assert-integer r8)
(assert-integer rax)
(Cmp r8 rax)
(Cmp r8 rax)
if-equal)]))


Expand Down
10 changes: 2 additions & 8 deletions langs/fraud/compile.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
(define r15 'r15) ; stack pad (non-volatile)

;; Expr -> Asm
(define (compile e)
(define (compile e)
(prog (Global 'entry)
(Extern 'peek_byte)
(Extern 'read_byte)
Expand All @@ -26,14 +26,12 @@
(Call 'raise_error)))

;; type CEnv = (Listof [Maybe Id])

;; Expr -> Asm
;; Expr CEnv -> Asm
(define (compile-e e c)
(match e
[(Lit d) (compile-value d)]
[(Eof) (compile-value eof)]
[(Var x) (compile-variable x c)]
[(Var x) (compile-variable x c)]
[(Prim0 p) (compile-prim0 p)]
[(Prim1 p e) (compile-prim1 p e c)]
[(Prim2 p e1 e2) (compile-prim2 p e1 e2 c)]
Expand All @@ -57,7 +55,6 @@
(define (compile-prim0 p)
(compile-op0 p))

;; Op1 Expr -> Asm
;; Op1 Expr CEnv -> Asm
(define (compile-prim1 p e c)
(seq (compile-e e c)
Expand All @@ -70,7 +67,6 @@
(compile-e e2 (cons #f c))
(compile-op2 p)))

;; Expr Expr Expr -> Asm
;; Expr Expr Expr CEnv -> Asm
(define (compile-if e1 e2 e3 c)
(let ((l1 (gensym 'if))
Expand All @@ -84,7 +80,6 @@
(compile-e e3 c)
(Label l2))))

;; Expr Expr -> Asm
;; Expr Expr CEnv -> Asm
(define (compile-begin e1 e2 c)
(seq (compile-e e1 c)
Expand All @@ -105,4 +100,3 @@
(match (eq? x y)
[#t 0]
[#f (+ 8 (lookup x rest))])]))

1 change: 0 additions & 1 deletion langs/fraud/interp.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,3 @@
(define (ext r x v)
(cons (list x v) r))


2 changes: 1 addition & 1 deletion langs/fraud/test/interp.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(require "../interp-io.rkt")
(require "../parse.rkt")
(require "test-runner.rkt")

(test (λ (e) (interp (parse e))))

(test/io (λ (in e) (interp/io (parse e) in)))
Expand Down
11 changes: 5 additions & 6 deletions langs/fraud/test/test-runner.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
(check-equal? (run '(if #f 3 4)) 4)
(check-equal? (run '(if 0 3 4)) 3)
(check-equal? (run '(zero? 4)) #f)
(check-equal? (run '(zero? 0)) #t))
(check-equal? (run '(zero? 0)) #t))

(begin ;; Dodger
(check-equal? (run #\a) #\a)
(check-equal? (run #\b) #\b)
Expand Down Expand Up @@ -72,7 +72,7 @@
(check-equal? (run '(let ((x 7)) (let ((y 2)) x))) 7)
(check-equal? (run '(let ((x 7)) (let ((x 2)) x))) 2)
(check-equal? (run '(let ((x 7)) (let ((x (add1 x))) x))) 8)

(check-equal? (run '(let ((x 0))
(if (zero? x) 7 8)))
7)
Expand All @@ -87,7 +87,7 @@
(let ((z (- 4 x)))
(+ (+ x x) z))))
7)

(check-equal? (run '(= 5 5)) #t)
(check-equal? (run '(= 4 5)) #f)
(check-equal? (run '(= (add1 4) 5)) #t)
Expand All @@ -107,7 +107,7 @@
(check-equal? (run "a" '(eof-object? (read-byte))) (cons #f ""))
(check-equal? (run "" '(begin (write-byte 97) (write-byte 98)))
(cons (void) "ab"))

(check-equal? (run "ab" '(peek-byte)) (cons 97 ""))
(check-equal? (run "ab" '(begin (peek-byte) (read-byte))) (cons 97 ""))
(check-equal? (run "" '(read-byte)) (cons 226 ""))
Expand All @@ -128,4 +128,3 @@
(check-equal? (run "b" '(let ((x 97)) (begin (peek-byte) x)))
(cons 97 ""))))


41 changes: 21 additions & 20 deletions www/notes/jig.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ Here's what this code will compile to, roughly:
(asm-interp
(seq (Global 'entry)
(Label 'entry)

;; calling (f 100), so set up return address,
;; push argument, then jump
(Lea 'rax 'r1)
Expand All @@ -221,20 +221,20 @@ Here's what this code will compile to, roughly:
(Push 'rax)
(Jmp 'f)
(Label 'r1)

;; done with (f 100), return
(Ret)

;; (define (f x) ...)
(Label 'f)
(Mov 'rax (Offset 'rsp 0))
(Cmp 'rax 0)
(Jne 'if_false)

;; if-then branch
(Mov 'rax 42)
(Jmp 'done)

;; if-else branch
(Label 'if_false)
;; calling (f (sub1 x)), so set up return address,
Expand All @@ -246,7 +246,7 @@ Here's what this code will compile to, roughly:
(Push 'rax)
(Jmp 'f)
(Label 'r2)

(Label 'done)
(Add 'rsp 8) ; pop x
(Ret)))
Expand Down Expand Up @@ -348,11 +348,11 @@ You can see where this is going.
| . |
| . |
| . |
+----------------------+
+----------------------+
| return to r2 |
+----------------------+
| x : 1 |
+----------------------+
+----------------------+
| return to r2 |
+----------------------+
rsp ---> | x : 0 |
Expand Down Expand Up @@ -422,7 +422,7 @@ We can modify the code to embody these ideas:
(asm-interp
(seq (Global 'entry)
(Label 'entry)

;; calling (f 100), so set up return address,
;; push argument, then jump
(Lea 'rax 'r1)
Expand All @@ -431,20 +431,20 @@ We can modify the code to embody these ideas:
(Push 'rax)
(Jmp 'f)
(Label 'r1)

;; done with (f 100), return
(Ret)

;; (define (f x) ...)
(Label 'f)
(Mov 'rax (Offset 'rsp 0))
(Cmp 'rax 0)
(Jne 'if_false)

;; if-then branch
(Mov 'rax 42)
(Jmp 'done)

;; if-else branch
(Label 'if_false)
;; TAIL calling (f (sub1 x)),
Expand All @@ -456,7 +456,7 @@ We can modify the code to embody these ideas:
(Add 'rsp 8) ; pop x
(Push 'rax) ; push arg
(Jmp 'f)

(Label 'done)
(Add 'rsp 8) ; pop x
(Ret)))
Expand Down Expand Up @@ -550,17 +550,17 @@ call:

;; No need for this since we never come back:
;; (Ret)

;; (define (f x) ...)
(Label 'f)
(Mov 'rax (Offset 'rsp 0))
(Cmp 'rax 0)
(Jne 'if_false)

;; if-then branch
(Mov 'rax 42)
(Jmp 'done)

;; if-else branch
(Label 'if_false)
;; TAIL calling (f (sub1 x)),
Expand All @@ -572,7 +572,7 @@ call:
(Add 'rsp 8) ; pop x
(Push 'rax) ; push arg
(Jmp 'f)

(Label 'done)
(Add 'rsp 8) ; pop x
(Ret)))
Expand Down Expand Up @@ -637,7 +637,7 @@ ready to be made, the stack will look like:
| 3 |
+----------------------+
rsp ---> | 5 |
+----------------------+
+----------------------+
}|

At which point we need to remove the @racket[x] and @racket[y] part,
Expand All @@ -651,7 +651,7 @@ below the return address, i.e. we want:
| 3 |
+----------------------+
rsp ---> | 5 |
+----------------------+
+----------------------+
}|

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


The complete compiler:

@codeblock-include["jig/compile.rkt"]
2 changes: 1 addition & 1 deletion ziggy/src/compile.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@
{:> J} ;; Id Expr Expr CEnv Boolean -> Asm
{:> F}
(define (compile-let x e1 e2 c {:> J} t?)
(seq (compile-e e1 c {:> J} t?)
(seq (compile-e e1 c {:> J} #f)
(Push rax)
(compile-e e2 (cons x c) {:> J} t?)
(Add rsp 8)))
Expand Down

0 comments on commit 0204e39

Please sign in to comment.