Skip to content

Commit e710060

Browse files
authored
Fixes #11. (#24)
1 parent be5e008 commit e710060

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

a86/interp.rkt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
(define fclose
4141
(get-ffi-obj "fclose" (ffi-lib #f) (_fun _pointer _-> _void)))
4242

43-
(define fmt (if (eq? (system-type 'os) 'macosx) 'macho64 'elf64))
44-
4543
;; WARNING: The heap is re-used, so make sure you're done with it
4644
;; before calling asm-interp again
4745
(define *heap*
@@ -232,8 +230,14 @@
232230
;; run nasm on t.s to create t.o
233231
(define (nasm t.s t.o)
234232
(define err-port (open-output-string))
233+
(define fmt (if (eq? (system-type 'os) 'macosx) 'macho64 'elf64))
234+
(define prefix
235+
(if (eq? (system-type 'os) 'macosx)
236+
"'_'"
237+
"''"))
238+
235239
(unless (parameterize ((current-error-port err-port))
236-
(system (format "nasm -f ~a ~a -o ~a" fmt t.s t.o)))
240+
(system (format "nasm -f ~a --gprefix ~a ~a -o ~a" fmt prefix t.s t.o)))
237241
(nasm:error (get-output-string err-port))))
238242

239243
(struct exn:ld exn:fail:user ())

a86/printer.rkt

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,17 @@
2525
(define current-extern-labels (make-parameter '()))
2626

2727
;; Label -> String
28-
;; prefix with _ for Mac
29-
(define label-symbol->string
30-
(match (system-type 'os)
31-
['macosx
32-
(λ (s) (string-append "$_" (symbol->string s)))]
33-
[_
34-
(λ (s)
35-
(if (and (current-shared?) (memq s (current-extern-labels)))
36-
; hack for ELF64 shared libraries in service of
37-
; calling external functions in asm-interp
38-
(string-append "$" (symbol->string s) " wrt ..plt")
39-
(string-append "$" (symbol->string s))))]))
40-
41-
(define extern-label-decl-symbol->string
42-
(match (system-type 'os)
43-
['macosx
44-
(λ (s) (string-append "$_" (symbol->string s)))]
45-
[_
46-
(λ (s)
47-
(string-append "$" (symbol->string s)))]))
28+
(define (label-symbol->string s)
29+
;; This should maybe be handled specially in the printing of Call rather
30+
;; than in every label...
31+
(if (and (eq? (system-type 'os) 'unix) (current-shared?) (memq s (current-extern-labels)))
32+
; hack for ELF64 shared libraries in service of
33+
; calling external functions in asm-interp
34+
(string-append "$" (symbol->string s) " wrt ..plt")
35+
(string-append "$" (symbol->string s))))
36+
37+
(define (extern-label-decl-symbol->string s)
38+
(string-append "$" (symbol->string s)))
4839

4940
;; Instruction -> String
5041
(define (common-instruction->string i)

a86/test/interp.rkt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#lang racket
2+
(require rackunit "../ast.rkt" "../interp.rkt")
3+
4+
(check-equal?
5+
(asm-interp (prog (Global 'entry)
6+
(Label 'entry)
7+
(Mov 'rax 42)
8+
(Ret)))
9+
42)

0 commit comments

Comments
 (0)