|
1 | 1 | #lang racket |
2 | | -(provide compile-datum compile-literals) |
| 2 | +(provide compile-datum) |
3 | 3 | (require "types.rkt" |
4 | | - "intern.rkt" |
| 4 | + "utils.rkt" |
5 | 5 | a86/ast) |
6 | 6 |
|
7 | 7 | ;; Registers used |
8 | 8 | (define rax 'rax) ; return |
9 | 9 |
|
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)) |
17 | 12 |
|
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)) |
26 | 15 |
|
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))) |
33 | 25 |
|
34 | 26 | ;; Datum -> Asm |
35 | 27 | (define (compile-datum d) |
36 | 28 | (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)])) |
43 | 33 |
|
| 34 | +;; Datum -> Asm |
44 | 35 | (define (compile-compound-datum d) |
45 | 36 | (match (compile-quoted d) |
46 | 37 | [(cons l is) |
|
52 | 43 | ;; Datum -> (cons AsmExpr Asm) |
53 | 44 | (define (compile-quoted c) |
54 | 45 | (cond |
55 | | - ;[(string? c) (compile-datum-string c)] |
56 | | - ;[(symbol? c) (compile-datum-symbol (symbol->string c))] |
57 | 46 | [(vector? c) (compile-datum-vector (vector->list c))] |
58 | 47 | [(box? c) (compile-datum-box (unbox c))] |
59 | 48 | [(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) '())] |
61 | 51 | [else (cons (imm->bits c) '())])) |
62 | 52 |
|
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 | | - |
100 | 53 | ;; Datum -> (cons AsmExpr Asm) |
101 | 54 | (define (compile-datum-box c) |
102 | 55 | (match (compile-quoted c) |
|
121 | 74 | is1 |
122 | 75 | is2)))])])) |
123 | 76 |
|
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))))])) |
0 commit comments