Skip to content

Commit 49a1d1f

Browse files
committed
New Shakedown
1 parent ae684bf commit 49a1d1f

5 files changed

Lines changed: 209 additions & 159 deletions

File tree

www/notes/shakedown/asm/printer.rkt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
(string-append "\tcall " (arg->string l) "\n")]
3535
[`(push ,r)
3636
(string-append "\tpush " (reg->string r) "\n")]
37+
[`(extern ,f)
38+
(string-append "\textern " (label->string f) "\n")]
39+
[`(section text) "\tsection .text\n"]
3740
[l (string-append (label->string l) ":\n")]))
3841

3942
(define (opcode2? x)
@@ -75,6 +78,4 @@
7578
(string-append "\tglobal " (label->string g) "\n"
7679
"\tdefault rel\n"
7780
"\textern " (label->string 'error) "\n"
78-
"\textern " (label->string 'c_fun) "\n"
79-
"\tsection .text\n"
8081
(asm->string a)))))

www/notes/shakedown/compile-file.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#lang racket
22
(provide (all-defined-out))
3-
(require "compile.rkt" #;"syntax.rkt" "asm/printer.rkt")
3+
(require "compile.rkt" "syntax.rkt" "asm/printer.rkt")
44

55
;; String -> Void
66
;; Compile contents of given file name,
@@ -12,7 +12,7 @@
1212
; assumed OK for now
1313
;(unless (and (prog? p) (closed? p))
1414
; (error "syntax error"))
15-
(asm-display (compile p))))))
15+
(asm-display (compile (sexpr->prog p)))))))
1616

1717
(define (read-program)
1818
(regexp-match "^#lang racket" (current-input-port))

www/notes/shakedown/compile.rkt

Lines changed: 91 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#lang racket
2-
(require "syntax.rkt")
2+
(require "syntax.rkt" "ast.rkt")
33
(provide (all-defined-out))
44

55
;; An immediate is anything ending in #b000
@@ -11,7 +11,7 @@
1111
(define type-box #b001)
1212
(define type-pair #b010)
1313
(define type-string #b011)
14-
(define type-proc #b100) ;; <-- NEW: procedure value: points to function label in memory
14+
(define type-proc #b100)
1515

1616
(define imm-shift (+ 2 result-shift))
1717
(define imm-type-mask (sub1 (arithmetic-shift 1 imm-shift)))
@@ -35,17 +35,30 @@
3535

3636
;; type Label = (quote Symbol)
3737

38+
;; Prog -> Asm
39+
(define (compile p)
40+
; Remove all of the explicit function definitions
41+
(match (desugar-prog p)
42+
[(prog _ e)
43+
(compile-entry (label-λ e))]))
44+
45+
3846
;; Expr -> Asm
39-
(define (compile e)
40-
(let ((le (label-λ (desugar e))))
41-
`(entry
42-
,@(compile-tail-e le '())
47+
(define (compile-entry e)
48+
`(,@(make-externs (ffi-calls e))
49+
(section text)
50+
entry
51+
,@(compile-tail-e e '())
4352
ret
44-
,@(compile-λ-definitions (λs le))
53+
,@(compile-λ-definitions (λs e))
4554
err
4655
(push rbp)
4756
(call error)
48-
ret)))
57+
ret))
58+
59+
;; (Listof Symbol) -> Asm
60+
(define (make-externs fs)
61+
(map (lambda (s) `(extern ,s)) fs))
4962

5063
;; (Listof Lambda) -> Asm
5164
(define (compile-λ-definitions ls)
@@ -54,41 +67,46 @@
5467
;; Lambda -> Asm
5568
(define (compile-λ-definition l)
5669
(match l
57-
[`(λ ,xs ',f ,e0)
70+
[(lam-t f xs e0)
5871
(let ((c0 (compile-tail-e e0 (reverse (append xs (fvs l))))))
5972
`(,f
6073
,@c0
61-
ret))]))
74+
ret))]
75+
[(lam-e _ _) (error "Lambdas need to be labeled before compiling")]))
6276

6377
;; LExpr CEnv -> Asm
6478
;; Compile an expression in tail position
6579
(define (compile-tail-e e c)
6680
(match e
67-
[(? symbol? x) (compile-variable x c)]
68-
[(? imm? i) (compile-imm i)]
69-
[`(box ,e0) (compile-box e0 c)]
70-
[`(unbox ,e0) (compile-unbox e0 c)]
71-
[`(cons ,e0 ,e1) (compile-cons e0 e1 c)]
72-
[`(car ,e0) (compile-car e0 c)]
73-
[`(cdr ,e0) (compile-cdr e0 c)]
74-
[`(add1 ,e0) (compile-add1 e0 c)]
75-
[`(sub1 ,e0) (compile-sub1 e0 c)]
76-
[`(zero? ,e0) (compile-zero? e0 c)]
77-
[`(empty? ,e0) (compile-empty? e0 c)]
78-
[`(if ,e0 ,e1 ,e2) (compile-tail-if e0 e1 e2 c)]
79-
[`(+ ,e0 ,e1) (compile-+ e0 e1 c)]
80-
[`(let ((,x ,e0)) ,e1) (compile-tail-let x e0 e1 c)]
81-
[`(letrec ,bs ,e0) (compile-tail-letrec (map first bs) (map second bs) e0 c)]
82-
[`(λ ,xs ',l ,e0) (compile-λ xs l (fvs e) c)]
83-
[`(ccall ,f . ,es) (compile-ccall f es c)]
84-
[`(,e . ,es) (compile-tail-call e es c)]))
81+
[(var-e v) (compile-variable v c)]
82+
[(? imm? i) (compile-imm i)]
83+
[(prim-e (? prim? p) es) (compile-prim p es c)]
84+
[(if-e p t f) (compile-tail-if p t f c)]
85+
[(let-e (list b) body) (compile-tail-let b body c)]
86+
[(letr-e bs body) (compile-tail-letrec (get-vars bs) (get-defs bs) body c)]
87+
[(app-e f es) (compile-tail-call f es c)]
88+
[(lam-t l xs e0) (compile-λ xs l (fvs e) c)]))
89+
90+
8591

8692
;; LExpr CEnv -> Asm
8793
;; Compile an expression in non-tail position
8894
(define (compile-e e c)
8995
(match e
90-
[(? symbol? x) (compile-variable x c)]
91-
[(? imm? i) (compile-imm i)]
96+
[(var-e v) (compile-variable v c)]
97+
[(? imm? i) (compile-imm i)]
98+
[(prim-e (? prim? p) es) (compile-prim p es c)]
99+
[(if-e p t f) (compile-if p t f c)]
100+
[(let-e (list b) body) (compile-let b body c)]
101+
[(letr-e bs body) (compile-letrec (get-vars bs) (get-defs bs) body c)]
102+
[(ccall-e f es) (compile-ccall f es c)]
103+
[(app-e f es) (compile-call f es c)]
104+
[(lam-t l xs e0) (compile-λ xs l (fvs e) c)]))
105+
106+
;; Our current set of primitive operations require no function calls,
107+
;; so there's no difference between tail and non-tail call positions
108+
(define (compile-prim p es c)
109+
(match (cons p es)
92110
[`(box ,e0) (compile-box e0 c)]
93111
[`(unbox ,e0) (compile-unbox e0 c)]
94112
[`(cons ,e0 ,e1) (compile-cons e0 e1 c)]
@@ -98,13 +116,9 @@
98116
[`(sub1 ,e0) (compile-sub1 e0 c)]
99117
[`(zero? ,e0) (compile-zero? e0 c)]
100118
[`(empty? ,e0) (compile-empty? e0 c)]
101-
[`(if ,e0 ,e1 ,e2) (compile-if e0 e1 e2 c)]
102119
[`(+ ,e0 ,e1) (compile-+ e0 e1 c)]
103-
[`(let ((,x ,e0)) ,e1) (compile-let x e0 e1 c)]
104-
[`(λ ,xs ',l ,e0) (compile-λ xs l (fvs e) c)]
105-
[`(letrec ,bs ,e0) (compile-letrec (map first bs) (map second bs) e0 c)]
106-
[`(ccall ,f . ,es) (compile-ccall f es c)]
107-
[`(,e . ,es) (compile-call e es c)]))
120+
[_ (error
121+
(format "prim applied to wrong number of args: ~a ~a" p es))]))
108122

109123
;; Label (listof Expr) -> Asm
110124
(define (compile-ccall f es c)
@@ -128,20 +142,20 @@
128142
)]
129143
[_ `()])))
130144

131-
;; (Listof Variable) Label (Listof Varialbe) CEnv -> Asm
145+
;; (Listof Variable) Label (Listof Variable) CEnv -> Asm
132146
(define (compile-λ xs f ys c)
133-
`(;; Save label address
134-
(lea rax (offset ,f 0))
147+
; Save label address
148+
`((lea rax (offset ,f 0))
135149
(mov (offset rdi 0) rax)
136150

137-
;; Save the environment
151+
; Save the environment
138152
(mov r8 ,(length ys))
139153
(mov (offset rdi 1) r8)
140154
(mov r9 rdi)
141155
(add r9 16)
142156
,@(copy-env-to-heap ys c 0)
143157

144-
;; Return a pointer to the closure
158+
; Return a pointer to the closure
145159
(mov rax rdi)
146160
(or rax ,type-proc)
147161
(add rdi ,(* 8 (+ 2 (length ys))))))
@@ -206,6 +220,7 @@
206220
;,@(copy-closure-env-to-stack (length es))
207221
(jmp (offset rax 0)))))
208222

223+
209224
;; -> Asm
210225
;; Copy closure's (in rax) env to stack in rcx
211226
(define (copy-closure-env-to-stack)
@@ -218,7 +233,7 @@
218233
(cmp r8 0)
219234
(je ,copy-done)
220235
(mov rbx (offset r9 0))
221-
(mov (offset rcx 0) rbx)
236+
(mov (offset rcx 0) rbx) ; Move val onto stack
222237
(sub r8 1)
223238
(add r9 8)
224239
(sub rcx 8)
@@ -249,17 +264,19 @@
249264
(match ls
250265
['() '()]
251266
[(cons l ls)
252-
(let ((cs (compile-letrec-λs ls (cons #f c)))
253-
(ys (fvs l)))
254-
`((lea rax (offset ,(second (third l)) 0))
255-
(mov (offset rdi 0) rax)
256-
(mov rax ,(length ys))
257-
(mov (offset rdi 1) rax)
258-
(mov rax rdi)
259-
(or rax ,type-proc)
260-
(add rdi ,(* 8 (+ 2 (length ys))))
261-
(mov (offset rsp ,(- (add1 (length c)))) rax)
262-
,@cs))]))
267+
(match l
268+
[(lam-t lab as body)
269+
(let ((cs (compile-letrec-λs ls (cons #f c)))
270+
(ys (fvs l)))
271+
`((lea rax (offset ,lab 0))
272+
(mov (offset rdi 0) rax)
273+
(mov rax ,(length ys))
274+
(mov (offset rdi 1) rax)
275+
(mov rax rdi)
276+
(or rax ,type-proc)
277+
(add rdi ,(* 8 (+ 2 (length ys))))
278+
(mov (offset rsp ,(- (add1 (length c)))) rax)
279+
,@cs))])]))
263280

264281
;; (Listof Variable) (Listof Lambda) CEnv -> Asm
265282
(define (compile-letrec-init fs ls c)
@@ -292,10 +309,11 @@
292309
;; Imm -> Integer
293310
(define (imm->bits i)
294311
(match i
295-
[(? integer? i) (arithmetic-shift i imm-shift)]
296-
[(? char? c) (+ (arithmetic-shift (char->integer c) imm-shift) imm-type-char)]
297-
[(? boolean? b) (if b imm-val-true imm-val-false)]
298-
[''() imm-type-empty]))
312+
[(int-e i) (arithmetic-shift i imm-shift)]
313+
[(char-e c) (+ (arithmetic-shift (char->integer c) imm-shift) imm-type-char)]
314+
[(bool-e b) (if b imm-val-true imm-val-false)]
315+
[(nil-e) imm-type-empty]))
316+
299317

300318
;; Variable CEnv -> Asm
301319
(define (compile-variable x c)
@@ -421,20 +439,24 @@
421439
,l1)))
422440

423441
;; Variable LExpr LExpr CEnv -> Asm
424-
(define (compile-tail-let x e0 e1 c)
425-
(let ((c0 (compile-e e0 c))
426-
(c1 (compile-tail-e e1 (cons x c))))
427-
`(,@c0
428-
(mov (offset rsp ,(- (add1 (length c)))) rax)
429-
,@c1)))
442+
(define (compile-tail-let b body c)
443+
(match b
444+
[(binding x def)
445+
(let ((c0 (compile-e def c))
446+
(c1 (compile-tail-e body (cons x c))))
447+
`(,@c0
448+
(mov (offset rsp ,(- (add1 (length c)))) rax)
449+
,@c1))]))
430450

431451
;; Variable LExpr LExpr CEnv -> Asm
432-
(define (compile-let x e0 e1 c)
433-
(let ((c0 (compile-e e0 c))
434-
(c1 (compile-e e1 (cons x c))))
435-
`(,@c0
436-
(mov (offset rsp ,(- (add1 (length c)))) rax)
437-
,@c1)))
452+
(define (compile-let b body c)
453+
(match b
454+
[(binding x def)
455+
(let ((c0 (compile-e def c))
456+
(c1 (compile-e body (cons x c))))
457+
`(,@c0
458+
(mov (offset rsp ,(- (add1 (length c)))) rax)
459+
,@c1))]))
438460

439461
;; LExpr LExpr CEnv -> Asm
440462
(define (compile-+ e0 e1 c)

www/notes/shakedown/example.rkt-ffi

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

0 commit comments

Comments
 (0)