Skip to content

Commit

Permalink
Old changes in ziggy.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvanhorn committed Oct 19, 2023
1 parent 06574e5 commit 27a1efe
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 81 deletions.
23 changes: 17 additions & 6 deletions ziggy/src/ast.rkt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#lang crook
{:= A B C D0 D1 E0 E1 F H0 H1 I J K}
(provide {:> A} Lit {:> E0} Prim0 {:> B} Prim1 {:> F} Prim2 {:> H1} Prim3 {:> C D0} IfZero {:> D0} If {:> E0} Eof {:> E0} Begin
{:= A B C D0 D0.A D1 E0 E1 F H0 H1 I J K}
(provide {:> A} Lit {:> E0} Prim0 {:> B} Prim1 {:> F} Prim2 {:> H1} Prim3 {:> C C} IfZero {:> D0} If {:> E0} Eof {:> E0} Begin
{:> F} Let {:> F} Var {:> H0} Empty {:> I} Prog {:> I} Defn {:> I} App
{:> K} Match {:> K} Box {:> K} Cons {:> K} Conj)
;;
Expand All @@ -11,21 +11,30 @@
{:> I} ;; type Defn = (Defn Id (Listof Id) Expr)
{:> I} (struct Defn (f xs e) #:prefab)

{:> A D0} ;; type Expr = (Lit Integer)
{:> A C} ;; type Expr = (Lit Integer)
{:> D0} ;; type Expr = (Lit Datum)
{:> E0} ;; | (Eof)
{:> H0} ;; | (Empty)
{:> E0} ;; | (Prim0 Op0)
{:> B} ;; | (Prim1 Op1 Expr)
{:> F} ;; | (Prim2 Op2 Expr Expr)
{:> H1} ;; | (Prim3 Op3 Expr Expr Expr)
{:> C D0} ;; | (IfZero Expr Expr Expr)
{:> C C} ;; | (IfZero Expr Expr Expr)
{:> D0} ;; | (If Expr Expr Expr)
{:> D0.A D0.A}
;; | (Cond [Listof CondClause] Expr)
{:> D0.A D0.A}
;; | (Case Expr [Listof CaseClause] Expr)
{:> F} ;; | (Let Id Expr Expr)
{:> F} ;; | (Var Id)
{:> I} ;; | (App Id (Listof Expr))
{:> K} ;; | (Match Expr (Listof Pat) (Listof Expr))

{:> D0.A D0.A}
;; type CondClause = (Clause Expr Expr)
{:> D0.A D0.A}
;; type CaseClause = (Clause [Listof Datum] Expr)

{:> F} ;; type Id = Symbol
{:> D0} ;; type Datum = Integer
{:> D0} ;; | Boolean
Expand All @@ -34,6 +43,8 @@
{:> E0} ;; type Op0 = 'read-byte | 'peek-byte | 'void
{:> B} ;; type Op1 = 'add1 | 'sub1
{:> D0} ;; | 'zero?
{:> D0.A D0.A}
;; | 'abs | '- | 'not
{:> D1} ;; | 'char? | 'integer->char | 'char->integer
{:> E0} ;; | 'write-byte | 'eof-object?
{:> H0} ;; | 'box | 'car | 'cdr | 'unbox
Expand All @@ -53,13 +64,13 @@

{:> E0} (struct Eof () #:prefab)
{:> H0} (struct Empty () #:prefab)
{:> A D0} (struct Lit (i) #:prefab)
{:> A C} (struct Lit (i) #:prefab)
{:> D0} (struct Lit (d) #:prefab)
{:> E0} (struct Prim0 (p) #:prefab)
{:> B} (struct Prim1 (p e) #:prefab)
{:> F} (struct Prim2 (p e1 e2) #:prefab)
{:> H1} (struct Prim3 (p e1 e2 e3) #:prefab)
{:> C D0} (struct IfZero (e1 e2 e3) #:prefab)
{:> C C} (struct IfZero (e1 e2 e3) #:prefab)
{:> D0} (struct If (e1 e2 e3) #:prefab)
{:> E0} (struct Begin (e1 e2) #:prefab)
{:> F} (struct Let (x e1 e2) #:prefab)
Expand Down
12 changes: 6 additions & 6 deletions ziggy/src/compile-ops.rkt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#lang crook
{:= B C D0 D1 E0 E1 F H0 H1 I J K}
{:= B C D0 D0.A D1 E0 E1 F H0 H1 I J K}
(provide {:> E0} compile-op0 compile-op1 {:> F} compile-op2 {:> H1} compile-op3 {:> F} pad-stack)
(require "ast.rkt")
{:> D0} (require "types.rkt")
Expand Down Expand Up @@ -27,18 +27,18 @@
;; Op1 -> Asm
(define (compile-op1 p)
(match p
{:> B D0} ['add1 (Add rax 1)]
{:> B D0} ['sub1 (Sub rax 1)]
{:> D0 E1} ['add1 (Add rax (value->bits 1))]
{:> B C} ['add1 (Add rax 1)]
{:> B C} ['sub1 (Sub rax 1)]
{:> D0 E0} ['add1 (Add rax (value->bits 1))]
{:> E1} ['add1
(seq (assert-integer rax)
(Add rax (value->bits 1)))]
{:> D0 E1} ['sub1 (Sub rax (value->bits 1))]
{:> D0 E0} ['sub1 (Sub rax (value->bits 1))]
{:> E1} ['sub1
(seq (assert-integer rax)
(Sub rax (value->bits 1)))]
{:> D0} ['zero?
{:> D0 D1}
{:> D0 D0.A}
(seq (Cmp rax 0)
(Mov rax (value->bits #f))
(Mov r9 (value->bits #t))
Expand Down
2 changes: 1 addition & 1 deletion ziggy/src/compile-stdin.rkt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#lang crook
{:= A B C D0 D1 E0 E1 F H0 H1 I J K}
{:= A B C D0 D0.A D1 E0 E1 F H0 H1 I J K}
(provide main)
(require "parse.rkt")
(require "compile.rkt")
Expand Down
44 changes: 22 additions & 22 deletions ziggy/src/compile.rkt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#lang crook
{:= A B C D0 D1 E0 E1 F H0 H1 I J K}
{:= A B C D0 D0.A D1 E0 E1 F H0 H1 I J K}
(provide (all-defined-out))
(require "ast.rkt")
{:> B} (require "compile-ops.rkt")
Expand All @@ -13,18 +13,18 @@
{:> J} (define r8 'r8) {:> J} ; scratch
{:> F} (define r15 'r15) {:> F} ; stack pad (non-volatile)

{:> A I} ;; Expr -> Asm
{:> A I}
{:> A H0} ;; Expr -> Asm
{:> A H0}
(define (compile e)
(prog (Global 'entry)
{:> E0} (Extern 'peek_byte)
{:> E0} (Extern 'read_byte)
{:> E0} (Extern 'write_byte)
{:> E1} (Extern 'raise_error)
(Label 'entry)
{:> E0 F} (Sub rsp 8)
{:> A F} (compile-e e)
{:> E0 F} (Add rsp 8)
{:> E0 H0} (Sub rsp 8)
{:> A H0} (compile-e e)
{:> E0 H0} (Add rsp 8)
{:> F} (Push r15) {:> F} ; save callee-saved register
{:> H0} (Push rbx)
{:> H0} (Mov rbx rdi) {:> H0} ; recv heap pointer
Expand Down Expand Up @@ -86,7 +86,7 @@
{:> J} ;; Expr CEnv Boolean -> Asm
(define (compile-e e {:> F} c {:> J} t?)
(match e
{:> A D0}
{:> A C}
[(Lit i) (seq (Mov rax i))]
{:> D0}
[(Lit d) (compile-value d)]
Expand All @@ -104,9 +104,9 @@
[(Prim2 p e1 e2) (compile-prim2 p e1 e2 c)]
{:> H1}
[(Prim3 p e1 e2 e3) (compile-prim3 p e1 e2 e3 c)]
{:> C D0}
{:> C C}
[(IfZero e1 e2 e3)
(compile-ifzero e1 e2 e3 {:> J} t?)]
(compile-ifzero e1 e2 e3)]
{:> D0}
[(If e1 e2 e3)
(compile-if e1 e2 e3 {:> F} c {:> J} t?)]
Expand All @@ -125,7 +125,7 @@
{:> D0} ;; Value -> Asm
{:> D0}
(define (compile-value v)
{:> D0 H1}
{:> D0 H0}
(seq (Mov rax (value->bits v)))
{:> H1}
(cond [(string? v) (compile-string v)]
Expand Down Expand Up @@ -166,8 +166,8 @@
(define (compile-prim0 p)
(compile-op0 p))

{:> B F} ;; Op1 Expr -> Asm
{:> F} ;; Op1 Expr CEnv -> Asm
{:> B H0} ;; Op1 Expr -> Asm
{:> F} ;; Op1 Expr CEnv -> Asm
{:> B}
(define (compile-prim1 p e {:> F} c)
(seq (compile-e e {:> F} c {:> J} #f)
Expand All @@ -192,8 +192,8 @@
(compile-op3 p)))


{:> C D0} ;; Expr Expr Expr -> Asm
{:> C D0}
{:> C C} ;; Expr Expr Expr -> Asm
{:> C C}
(define (compile-ifzero e1 e2 e3)
(let ((l1 (gensym 'ifz))
(l2 (gensym 'ifz)))
Expand All @@ -206,9 +206,9 @@
(compile-e e3)
(Label l2))))

{:> D0 F} ;; Expr Expr Expr -> Asm
{:> F J} ;; Expr Expr Expr CEnv -> Asm
{:> J} ;; Expr Expr Expr CEnv Boolean -> Asm
{:> D0 H0} ;; Expr Expr Expr -> Asm
{:> F I} ;; Expr Expr Expr CEnv -> Asm
{:> J} ;; Expr Expr Expr CEnv Boolean -> Asm
{:> D0}
(define (compile-if e1 e2 e3 {:> F} c {:> J} t?)
(let ((l1 (gensym 'if))
Expand All @@ -222,15 +222,15 @@
(compile-e e3 {:> F} c {:> J} t?)
(Label l2))))

{:> E0 F} ;; Expr Expr -> Asm
{:> F J} ;; Expr Expr CEnv -> Asm
{:> J} ;; Expr Expr CEnv Boolean -> Asm
{:> E0 H0} ;; Expr Expr -> Asm
{:> F I} ;; Expr Expr CEnv -> Asm
{:> J} ;; Expr Expr CEnv Boolean -> Asm
{:> E0}
(define (compile-begin e1 e2 {:> F} c {:> J} t?)
(seq (compile-e e1 {:> F} c {:> J} #f)
(compile-e e2 {:> F} c {:> J} t?)))

{:> F J} ;; Id Expr Expr CEnv -> Asm
{:> F I} ;; Id Expr Expr CEnv -> Asm
{:> J} ;; Id Expr Expr CEnv -> Asm
{:> F}
(define (compile-let x e1 e2 c {:> J} t?)
Expand Down Expand Up @@ -267,7 +267,7 @@
{:> I} ;; Id [Listof Expr] CEnv -> Asm
{:> I} ;; The return address is placed above the arguments, so callee pops
{:> I} ;; arguments and return address is next frame
{:> I J}
{:> I I}
(define (compile-app f es c)
(let ((r (gensym 'ret)))
(seq (Lea rax r)
Expand Down
10 changes: 5 additions & 5 deletions ziggy/src/interp-prim.rkt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#lang crook
{:= B C D0 D1 E0 E1 F H0 H1 I J K}
{:= B C D0 D0.A D1 E0 E1 F H0 H1 I J K}
(provide {:> E0} interp-prim0 interp-prim1 {:> F} interp-prim2 {:> H1} interp-prim3)

{:> E0} ;; Op0 -> Value
Expand All @@ -10,15 +10,15 @@
['peek-byte (peek-byte)]
['void (void)]))

{:> B D0} ;; Op1 Integer -> Integer
{:> B D0}
{:> B C} ;; Op1 Integer -> Integer
{:> B C}
(define (interp-prim1 op i)
(match op
['add1 (add1 i)]
['sub1 (sub1 i)]))

{:> D0 E1} ;; Op1 Value -> Value
{:> D0 E1}
{:> D0 E0} ;; Op1 Value -> Value
{:> D0 E0}
(define (interp-prim1 op v)
(match op
['add1 (add1 v)]
Expand Down
2 changes: 1 addition & 1 deletion ziggy/src/interp-stdin.rkt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#lang crook
{:= A B C D0 D1 E0 E1 F H0 H1 I J K}
{:= A B C D0 D0.A D1 E0 E1 F H0 H1 I J K}
(provide main)
(require "parse.rkt")
(require "interp.rkt")
Expand Down
24 changes: 12 additions & 12 deletions ziggy/src/interp.rkt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#lang crook
{:= A B C D0 D1 E0 E1 F H0 H1 I J K}
{:= A B C D0 D0.A D1 E0 E1 F H0 H1 I J K}
(provide interp)
{:> F} (provide interp-env)
{:> K} (provide interp-match-pat)
Expand All @@ -20,35 +20,35 @@

{:> F} ;; type Env = (Listof (List Id Value))

{:> A D0} ;; Expr -> Integer
{:> D0 E1} ;; Expr -> Value
{:> E1 I} ;; Expr -> Answer
{:> A C} ;; Expr -> Integer
{:> D0 E0} ;; Expr -> Value
{:> E1 H1} ;; Expr -> Answer
{:> I} ;; Prog -> Answer
(define (interp {:> A I} e {:> I} p)
{:> F I}
(define (interp {:> A H1} e {:> I} p)
{:> F H1}
(interp-env e '())
{:> I}
(match p
[(Prog ds e)
(interp-env e '() ds)])
{:> A F}
{:> A H1}
(match e
{:> A D0} [(Lit i) i]
{:> A C} [(Lit i) i]
{:> D0} [(Lit d) d]
{:> E0} [(Eof) eof]
{:> E0} [(Prim0 p)
(interp-prim0 p)]
{:> B E1} [(Prim1 p e)
{:> B E0} [(Prim1 p e)
(interp-prim1 p (interp e))]
{:> E1} [(Prim1 p e)
(match (interp e)
['err 'err]
[v (interp-prim1 p v)])]
{:> C D0} [(IfZero e1 e2 e3)
{:> C C} [(IfZero e1 e2 e3)
(if (zero? (interp e1))
(interp e2)
(interp e3))]
{:> D0 E1} [(If e1 e2 e3)
{:> D0 E0} [(If e1 e2 e3)
(if (interp e1)
(interp e2)
(interp e3))]
Expand All @@ -58,7 +58,7 @@
[v (if v
(interp e2)
(interp e3))])]
{:> E0 E1} [(Begin e1 e2)
{:> E0 E0} [(Begin e1 e2)
(begin (interp e1)
(interp e2))]
{:> E1} [(Begin e1 e2)
Expand Down
10 changes: 5 additions & 5 deletions ziggy/src/parse.rkt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#lang crook
{:= A B C D0 D1 E0 E1 F H0 H1 I J K}
{:= A B C D0 D0.A D1 E0 E1 F H0 H1 I J K}
(provide parse {:> I} parse-e {:> I} parse-define)
(require "ast.rkt")

{:> A I} ;; S-Expr -> Expr
{:> A I}
{:> A H1} ;; S-Expr -> Expr
{:> A H1}
(define (parse s)
(match s
{:> E0}
['eof (Eof)]
{:> A D0}
{:> A C}
[(? exact-integer?) (Lit s)]
{:> D0}
[(? datum?) (Lit s)]
Expand All @@ -27,7 +27,7 @@
[(list (? op3? o) e1 e2 e3) (Prim3 o (parse e1) (parse e2) (parse e3))]
{:> E0}
[(list 'begin e1 e2) (Begin (parse e1) (parse e2))]
{:> C D0}
{:> C C}
[(list 'if (list 'zero? e1) e2 e3)
(IfZero (parse e1) (parse e2) (parse e3))]
{:> D0}
Expand Down
10 changes: 5 additions & 5 deletions ziggy/src/run.rkt
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#lang crook
{:= A B C D0 D1 E0 E1 F H0 H1 I J K}
{:= A B C D0 D0.A D1 E0 E1 F H0 H1 I J K}
(require a86/interp)
{:> D0} (require "types.rkt")
{:> E0} (require "build-runtime.rkt")
(provide run {:> E0} run/io)

{:> A D0} ;; Asm -> Integer
{:> D0 E1} ;; Asm -> Value
{:> A C} ;; Asm -> Integer
{:> D0 E0} ;; Asm -> Value
{:> E1} ;; Asm -> Answer
(define (run is)
{:> A D0}
{:> A C}
(asm-interp is)
{:> D0 E1}
(bits->value (asm-interp is))
{:> E0}
(parameterize ((current-objs (list (path->string runtime-path))))
{:> E0 E1}
{:> E0 E0}
(bits->value (asm-interp is))
{:> E1}
(match (asm-interp is)
Expand Down
Loading

0 comments on commit 27a1efe

Please sign in to comment.