Skip to content

Commit 6b6e79d

Browse files
committed
Pull Mug overhaul through Mountebank.
1 parent f7e7bc6 commit 6b6e79d

17 files changed

Lines changed: 636 additions & 764 deletions

langs/mountebank/ast.rkt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@
3535
;; | 'write-byte | 'eof-object?
3636
;; | 'box | 'car | 'cdr | 'unbox
3737
;; | 'empty? | 'cons? | 'box?
38-
;; | 'vector? | vector-length
39-
;; | 'string? | string-length
40-
;; | 'symbol? | string->symbol | symbol->string
38+
;; | 'vector? | 'vector-length
39+
;; | 'string? | 'string-length
40+
;; | 'symbol? | 'string->symbol
41+
;; | 'string->symbol | 'string->uninterned-symbol
4142
;; type Op2 = '+ | '- | '< | '=
4243
;; | 'cons | 'eq?
4344
;; | 'make-vector | 'vector-ref

langs/mountebank/compile-datum.rkt

Lines changed: 34 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,37 @@
11
#lang racket
2-
(provide compile-datum compile-literals)
2+
(provide compile-datum)
33
(require "types.rkt"
4-
"intern.rkt"
4+
"utils.rkt"
55
a86/ast)
66

77
;; Registers used
88
(define rax 'rax) ; return
99

10-
;; QEnv -> Asm
11-
(define (compile-literals q)
12-
(match q
13-
['() (seq)]
14-
[(cons (cons s (Ref l _)) q)
15-
(seq (compile-literal (to-string s) l)
16-
(compile-literals q))]))
10+
(define (load-symbol s)
11+
(Plus (symbol->data-label s) type-symb))
1712

18-
;; String Label -> Asm
19-
(define (compile-literal s l)
20-
(seq (Label l)
21-
(Dq (string-length s))
22-
(compile-string-chars (string->list s))
23-
(if (odd? (string-length s))
24-
(seq (Dd 0))
25-
(seq))))
13+
(define (load-string s)
14+
(Plus (symbol->data-label (string->symbol s)) type-str))
2615

27-
(define (compound? x)
28-
(or ;(string? x)
29-
;(symbol? x)
30-
(cons? x)
31-
(vector? x)
32-
(box? x)))
16+
;; Value -> Asm
17+
(define (compile-atom v)
18+
(seq (Mov rax (imm->bits v))))
19+
20+
;; Datum -> Boolean
21+
(define (compound? d)
22+
(or (box? d)
23+
(cons? d)
24+
(vector? d)))
3325

3426
;; Datum -> Asm
3527
(define (compile-datum d)
3628
(cond
37-
[(compound? d)
38-
(compile-compound-datum d)]
39-
[(Ref? d)
40-
(seq (Lea rax (Plus (Ref-label d) (Ref-type-tag d))))]
41-
[else
42-
(seq (Mov rax (imm->bits d)))]))
29+
[(string? d) (seq (Lea rax (load-string d)))]
30+
[(symbol? d) (seq (Lea rax (load-symbol d)))]
31+
[(compound? d) (compile-compound-datum d)]
32+
[else (compile-atom d)]))
4333

34+
;; Datum -> Asm
4435
(define (compile-compound-datum d)
4536
(match (compile-quoted d)
4637
[(cons l is)
@@ -52,51 +43,13 @@
5243
;; Datum -> (cons AsmExpr Asm)
5344
(define (compile-quoted c)
5445
(cond
55-
;[(string? c) (compile-datum-string c)]
56-
;[(symbol? c) (compile-datum-symbol (symbol->string c))]
5746
[(vector? c) (compile-datum-vector (vector->list c))]
5847
[(box? c) (compile-datum-box (unbox c))]
5948
[(cons? c) (compile-datum-cons (car c) (cdr c))]
60-
[(Ref? c) (cons (Plus (Ref-label c) (Ref-type-tag c)) '())]
49+
[(symbol? c) (cons (load-symbol c) '())]
50+
[(string? c) (cons (load-string c) '())]
6151
[else (cons (imm->bits c) '())]))
6252

63-
;; String -> (cons AsmExpr Asm)
64-
#;
65-
(define (compile-datum-string c)
66-
(let ((l (gensym 'string)))
67-
(cons (Plus l type-str)
68-
(seq (Label l)
69-
(Dq (string-length c))
70-
(compile-string-chars (string->list c))
71-
(if (odd? (string-length c))
72-
(seq (Dd 0))
73-
(seq))))))
74-
75-
;; String -> (cons AsmExpr Asm)
76-
#;
77-
(define (compile-datum-symbol c)
78-
(let ((l (gensym 'symbol)))
79-
(cons (Plus l type-symb)
80-
(seq (Label l)
81-
(Dq (string-length c))
82-
(compile-string-chars (string->list c))
83-
(if (odd? (string-length c))
84-
(seq (Dd 0))
85-
(seq))))))
86-
87-
;; [Listof Datum] -> (cons AsmExpr Asm)
88-
(define (compile-datum-vector ds)
89-
(match ds
90-
['() (cons type-vect '())]
91-
[_
92-
(let ((l (gensym 'vector))
93-
(cds (map compile-quoted ds)))
94-
(cons (Plus l type-vect)
95-
(seq (Label l)
96-
(Dq (length ds))
97-
(map (λ (cd) (Dq (car cd))) cds)
98-
(append-map cdr cds))))]))
99-
10053
;; Datum -> (cons AsmExpr Asm)
10154
(define (compile-datum-box c)
10255
(match (compile-quoted c)
@@ -121,16 +74,15 @@
12174
is1
12275
is2)))])]))
12376

124-
;; [Listof Char] -> Asm
125-
(define (compile-string-chars cs)
126-
(match cs
127-
['() (seq)]
128-
[(cons c cs)
129-
(seq (Dd (char->integer c))
130-
(compile-string-chars cs))]))
131-
132-
;; (U String Symbol) -> String
133-
(define (to-string s)
134-
(if (symbol? s)
135-
(symbol->string s)
136-
s))
77+
;; [Listof Datum] -> (cons AsmExpr Asm)
78+
(define (compile-datum-vector ds)
79+
(match ds
80+
['() (cons type-vect '())]
81+
[_
82+
(let ((l (gensym 'vector))
83+
(cds (map compile-quoted ds)))
84+
(cons (Plus l type-vect)
85+
(seq (Label l)
86+
(Dq (length ds))
87+
(map (λ (cd) (Dq (car cd))) cds)
88+
(append-map cdr cds))))]))
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#lang racket
2+
(provide (all-defined-out))
3+
(require "ast.rkt"
4+
"types.rkt"
5+
"fv.rkt"
6+
"utils.rkt"
7+
"compile-expr.rkt"
8+
a86/ast)
9+
10+
;; [Listof Defn] -> [Listof Id]
11+
(define (define-ids ds)
12+
(match ds
13+
['() '()]
14+
[(cons (Defn f xs e) ds)
15+
(cons f (define-ids ds))]))
16+
17+
;; [Listof Defn] -> Asm
18+
(define (compile-defines ds)
19+
(match ds
20+
['() (seq)]
21+
[(cons d ds)
22+
(seq (compile-define d)
23+
(compile-defines ds))]))
24+
25+
;; Defn -> Asm
26+
(define (compile-define d)
27+
(match d
28+
[(Defn f xs e)
29+
(compile-lambda-define (Lam f xs e))]))
30+
31+
;; Defns -> Asm
32+
;; Compile the closures for ds and push them on the stack
33+
(define (compile-defines-values ds)
34+
(seq (alloc-defines ds 0)
35+
(init-defines ds (reverse (define-ids ds)) 8)
36+
(add-rbx-defines ds 0)))
37+
38+
;; Defns Int -> Asm
39+
;; Allocate closures for ds at given offset, but don't write environment yet
40+
(define (alloc-defines ds off)
41+
(match ds
42+
['() (seq)]
43+
[(cons (Defn f xs e) ds)
44+
(let ((fvs (fv (Lam f xs e))))
45+
(seq (Lea rax (symbol->label f))
46+
(Mov (Offset rbx off) rax)
47+
(Mov rax rbx)
48+
(Add rax off)
49+
(Or rax type-proc)
50+
(Push rax)
51+
(alloc-defines ds (+ off (* 8 (add1 (length fvs)))))))]))
52+
53+
;; Defns CEnv Int -> Asm
54+
;; Initialize the environment for each closure for ds at given offset
55+
(define (init-defines ds c off)
56+
(match ds
57+
['() (seq)]
58+
[(cons (Defn f xs e) ds)
59+
(let ((fvs (fv (Lam f xs e))))
60+
(seq (free-vars-to-heap fvs c off)
61+
(init-defines ds c (+ off (* 8 (add1 (length fvs)))))))]))
62+
63+
;; Defns Int -> Asm
64+
;; Compute adjustment to rbx for allocation of all ds
65+
(define (add-rbx-defines ds n)
66+
(match ds
67+
['() (seq (Add rbx (* n 8)))]
68+
[(cons (Defn f xs e) ds)
69+
(add-rbx-defines ds (+ n (add1 (length (fv (Lam f xs e))))))]))

0 commit comments

Comments
 (0)