Skip to content

Commit

Permalink
Fixes #11.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvanhorn committed Nov 4, 2024
1 parent 6de4841 commit 1d15227
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
10 changes: 7 additions & 3 deletions a86/interp.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
(define fclose
(get-ffi-obj "fclose" (ffi-lib #f) (_fun _pointer _-> _void)))

(define fmt (if (eq? (system-type 'os) 'macosx) 'macho64 'elf64))

;; WARNING: The heap is re-used, so make sure you're done with it
;; before calling asm-interp again
(define *heap*
Expand Down Expand Up @@ -208,8 +206,14 @@
;; run nasm on t.s to create t.o
(define (nasm t.s t.o)
(define err-port (open-output-string))
(define fmt (if (eq? (system-type 'os) 'macosx) 'macho64 'elf64))
(define prefix
(if (eq? (system-type 'os) 'macosx)
"'_'"
"''"))

(unless (parameterize ((current-error-port err-port))
(system (format "nasm -f ~a ~a -o ~a" fmt t.s t.o)))
(system (format "nasm -f ~a --gprefix ~a ~a -o ~a" fmt prefix t.s t.o)))
(nasm:error (get-output-string err-port))))

(struct exn:ld exn:fail:user ())
Expand Down
29 changes: 10 additions & 19 deletions a86/printer.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,17 @@
(define current-extern-labels (make-parameter '()))

;; Label -> String
;; prefix with _ for Mac
(define label-symbol->string
(match (system-type 'os)
['macosx
(λ (s) (string-append "$_" (symbol->string s)))]
[_
(λ (s)
(if (and (current-shared?) (memq s (current-extern-labels)))
; hack for ELF64 shared libraries in service of
; calling external functions in asm-interp
(string-append "$" (symbol->string s) " wrt ..plt")
(string-append "$" (symbol->string s))))]))
(define (label-symbol->string s)
;; This should maybe be handled specially in the printing of Call rather
;; than in every label...
(if (and (eq? (system-type 'os) 'unix) (current-shared?) (memq s (current-extern-labels)))
; hack for ELF64 shared libraries in service of
; calling external functions in asm-interp
(string-append "$" (symbol->string s) " wrt ..plt")
(string-append "$" (symbol->string s))))

(define extern-label-decl-symbol->string
(match (system-type 'os)
['macosx
(λ (s) (string-append "$_" (symbol->string s)))]
[_
(λ (s)
(string-append "$" (symbol->string s)))]))
(define (extern-label-decl-symbol->string s)
(string-append "$" (symbol->string s)))

;; Instruction -> String
(define (common-instruction->string i)
Expand Down
9 changes: 9 additions & 0 deletions a86/test/interp.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#lang racket
(require rackunit "../ast.rkt" "../interp.rkt")

(check-equal?
(asm-interp (prog (Global 'entry)
(Label 'entry)
(Mov 'rax 42)
(Ret)))
42)

0 comments on commit 1d15227

Please sign in to comment.