Skip to content

Commit 4247ea4

Browse files
committed
updates a86 printer to use intel syntax
1 parent 5bc7bc0 commit 4247ea4

File tree

2 files changed

+54
-25
lines changed

2 files changed

+54
-25
lines changed

a86/interp.rkt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,11 @@
240240
(define fmt (if (eq? (system-type 'os) 'macosx) 'macho64 'elf64))
241241
(define prefix
242242
(if (eq? (system-type 'os) 'macosx)
243-
"'_'"
244-
"''"))
243+
"arch -x86_64"
244+
""))
245245

246246
(unless (parameterize ((current-error-port err-port))
247-
(system (format "nasm -f ~a --gprefix ~a ~a -o ~a" fmt prefix t.s t.o)))
247+
(system (format "~a clang -c ~a -o ~a" prefix t.s t.o)))
248248
(nasm:error (get-output-string err-port))))
249249

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

a86/printer.rkt

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,45 @@
1818

1919
(define (comment->string c)
2020
(match c
21-
[(% s) (string-append (make-string 32 #\space) "; " s)]
22-
[(%% s) (string-append tab ";; " s)]
23-
[(%%% s) (string-append ";;; " s)]))
21+
[(% s) (string-append (make-string 32 #\space) "# " s)]
22+
[(%% s) (string-append tab "## " s)]
23+
[(%%% s) (string-append "### " s)]))
2424

2525
(define current-extern-labels (make-parameter '()))
2626

2727
;; Label -> String
28-
(define (label-symbol->string s)
28+
;; prefix with _ for Mac
29+
(define label-symbol->string
30+
(match (system-type 'os)
31+
['macosx (λ (s) (string-append "_" (symbol->string s)))]
32+
[_ (λ (s) (symbol->string s))]))
33+
34+
;(if (and (current-shared?) (memq s (current-extern-labels)))
35+
; hack for ELF64 shared libraries in service of
36+
; calling external functions in asm-interp
37+
;(string-append "$" (symbol->string s) " wrt ..plt")
38+
;(symbol->string s)))]))
39+
40+
(define (label-symbol->string_old s)
2941
;; This should maybe be handled specially in the printing of Call rather
3042
;; than in every label...
3143
(if (and (eq? (system-type 'os) 'unix) (current-shared?) (memq s (current-extern-labels)))
3244
; hack for ELF64 shared libraries in service of
3345
; calling external functions in asm-interp
34-
(string-append "$" (symbol->string s) " wrt ..plt")
35-
(string-append "$" (symbol->string s))))
46+
;(string-append "$" (symbol->string s) " wrt ..plt")
47+
(symbol->string s)
48+
(string-append "_" (symbol->string s))))
49+
50+
51+
(define extern-label-decl-symbol->string-new
52+
(match (system-type 'os)
53+
['macosx (λ (s) (string-append "_" (symbol->string s)))]
54+
[_ (λ (s)
55+
(symbol->string s))]))
56+
3657

3758
(define (extern-label-decl-symbol->string s)
38-
(string-append "$" (symbol->string s)))
59+
(string-append "" (symbol->string s)))
3960

4061
;; Instruction -> String
4162
(define (common-instruction->string i)
@@ -76,6 +97,8 @@
7697
(match e
7798
[(? register?) (symbol->string e)]
7899
[(? Mem?) (string-append "[" (mem->string e) "]")]
100+
[(Offset (? register? r)) (string-append "[" (symbol->string r) "]")]
101+
[(Offset ($ (? label? r))) (string-append "[" (label-symbol->string r) " + rip]")]
79102
[(Offset e)
80103
(string-append "[" (exp->string e) "]")]
81104
[_ (exp->string e)]))
@@ -95,48 +118,54 @@
95118

96119
(define (text-section n)
97120
(match (system-type 'os)
98-
['macosx (format "section __TEXT,~a align=16" n)]
99-
[_ (format "section ~a progbits alloc exec nowrite align=16" n)]))
121+
['macosx (format ".section __TEXT,~a \n\t .p2align 4" n)]
122+
[_ (format ".section ~a progbits alloc exec nowrite align=16" n)]))
100123

101124
(define (data-section n)
102125
(match (system-type 'os)
103-
['macosx (format "section __DATA,~a align=8" n)]
104-
[_ (format "section ~a progbits alloc noexec write align=8" n)]))
126+
['macosx (format ".section __DATA,~a \n\t .p2align 3" n)]
127+
[_ (format ".section ~a progbits alloc noexec write align=8" n)]))
105128

106129
;; Instruction -> String
107130
(define (simple-instr->string i)
108131
(match i
109-
[(Text) (string-append tab "section .text")]
132+
[(Text) (string-append tab ".text")]
110133
[(Text n) (string-append tab (text-section n))]
111-
[(Data) (string-append tab "section .data align=8")] ; 8-byte aligned data
134+
[(Data) (string-append tab ".data\n\t .p2align 3")] ; 8-byte aligned data
112135
[(Data n) (string-append tab (data-section n))]
113-
[(Extern ($ l)) (string-append tab "extern " (extern-label-decl-symbol->string l))]
136+
[(Extern ($ l)) (string-append tab ".extern " (extern-label-decl-symbol->string l))]
137+
[(Global ($ l)) (string-append tab ".global " (label-symbol->string l))]
138+
;;[(Label ($ l)) (string-append "_" (symbol->string l) ":")]
114139
[(Label ($ l)) (string-append (label-symbol->string l) ":")]
115140
[(Lea d (? Mem? m))
116141
(string-append tab "lea "
117-
(arg->string d) ", [rel "
142+
(arg->string d) ", [rip + "
118143
(mem->string m) "]")]
119144
[(Lea d e)
120145
(string-append tab "lea "
121-
(arg->string d) ", [rel "
146+
(arg->string d) ", [rip + "
122147
(arg->string e) "]")]
123148
[(Equ x c)
124149
(string-append tab
125150
(symbol->string x)
126151
" equ "
127152
(number->string c))]
128153
[(Dq (? Mem? m))
129-
(string-append tab "dq " (mem->string m))]
154+
(string-append tab ".quad " (mem->string m))]
155+
[(Dq m)
156+
(string-append tab ".quad " (number->string m))]
130157
[(Dd (? Mem? m))
131-
(string-append tab "dd " (mem->string m))]
158+
(string-append tab ".long " (mem->string m))]
159+
[(Dd m)
160+
(string-append tab ".long " (number->string m))]
132161
[(Db (? bytes? bs))
133-
(apply string-append tab "db " (add-between (map number->string (bytes->list bs)) ", "))]
162+
(apply string-append tab ".byte " (add-between (map number->string (bytes->list bs)) ", "))]
134163
[_ (common-instruction->string i)]))
135164

136165
(define (line-comment i s)
137166
(let ((i-str (simple-instr->string i)))
138167
(let ((pad (make-string (max 1 (- 32 (string-length i-str))) #\space)))
139-
(string-append i-str pad "; " s))))
168+
(string-append i-str pad "# " s))))
140169

141170
;; [Listof Instr] -> Void
142171
(define (instrs-display a)
@@ -173,8 +202,8 @@
173202
(begin
174203
(write-string (string-append
175204
; tab "global " (label-symbol->string g) "\n"
176-
tab "default rel\n"
177-
tab "section .text\n"))
205+
tab ".intel_syntax noprefix\n"
206+
tab ".text\n"))
178207
(instrs-display a))]
179208
[_
180209
(instrs-display a)

0 commit comments

Comments
 (0)