|
1 | 1 | #lang racket |
2 | | -(provide asm-string) |
| 2 | +(provide asm-string current-shared?) |
3 | 3 | (require "ast.rkt") |
4 | 4 |
|
| 5 | +(define current-shared? |
| 6 | + (let ((x (box #f))) |
| 7 | + (case-lambda |
| 8 | + [() (unbox x)] |
| 9 | + [(y) (set-box! x y)]))) |
| 10 | + |
5 | 11 | ;; Any -> Boolean |
6 | 12 | (define (reg? x) |
7 | 13 | (register? x)) |
|
17 | 23 | ['macosx |
18 | 24 | (λ (s) (string-append "_" (symbol->string s)))] |
19 | 25 | [_ |
20 | | - symbol->string])) |
| 26 | + (if (current-shared?) |
| 27 | + (λ (s) |
| 28 | + (if (memq s external-labels) |
| 29 | + ; hack for ELF64 shared libraries in service of |
| 30 | + ; calling external functions in asm-interp |
| 31 | + (string-append (symbol->string s) " wrt ..plt") |
| 32 | + (symbol->string s))) |
| 33 | + symbol->string)])) |
21 | 34 |
|
22 | 35 | ;; (U Label Reg) -> String |
23 | 36 | (define (jump-target->string t) |
|
50 | 63 |
|
51 | 64 | (define tab (make-string 8 #\space)) |
52 | 65 |
|
| 66 | + |
| 67 | +(define external-labels (box '())) |
| 68 | + |
53 | 69 | ;; Instruction -> String |
54 | 70 | (define (instr->string i) |
55 | 71 | (match i |
|
58 | 74 | [(Ret) (string-append tab "ret")] |
59 | 75 | [(Label l) (string-append (label-symbol->string l) ":")] |
60 | 76 | [(Global x) (string-append tab "global " (label-symbol->string x))] |
61 | | - [(Extern l) (string-append tab "extern " (label-symbol->string l))] |
| 77 | + [(Extern l) (let ((r (string-append tab "extern " (label-symbol->string l)))) |
| 78 | + (begin |
| 79 | + (set-box! external-labels (cons l (unbox external-labels))) |
| 80 | + r))] |
62 | 81 | [(Mov a1 a2) |
63 | 82 | (string-append tab "mov " |
64 | 83 | (arg->string a1) ", " |
|
154 | 173 | (string-append (instr->string i) "\n" (instrs->string a))])) |
155 | 174 |
|
156 | 175 | ;; Asm -> String |
157 | | -(define (asm-string a) |
158 | | - ;; entry point will be first label |
159 | | - (match (findf Label? a) |
160 | | - [(Label g) |
161 | | - (string-append |
162 | | - tab "global " (label-symbol->string g) "\n" |
163 | | - tab "default rel\n" |
164 | | - tab "section .text\n" |
165 | | - (instrs->string a))] |
166 | | - [_ |
167 | | - (instrs->string a) |
168 | | - #; |
169 | | - (error "program does not have an initial label")])) |
| 176 | +(define (asm-string a) |
| 177 | + (begin |
| 178 | + (set-box! external-labels '()) |
| 179 | + ;; entry point will be first label |
| 180 | + (match (findf Label? a) |
| 181 | + [(Label g) |
| 182 | + (string-append |
| 183 | + tab "global " (label-symbol->string g) "\n" |
| 184 | + tab "default rel\n" |
| 185 | + tab "section .text\n" |
| 186 | + (instrs->string a))] |
| 187 | + [_ |
| 188 | + (instrs->string a) |
| 189 | + #; |
| 190 | + (error "program does not have an initial label")]))) |
0 commit comments