Skip to content

Commit a51ca1e

Browse files
committed
Remove nasm cruft, including file and variable names
This also removes the nasm check in `interp.rkt`, which was causing problems on systems that should have been able to execute a86.
1 parent 4247ea4 commit a51ca1e

File tree

7 files changed

+113
-119
lines changed

7 files changed

+113
-119
lines changed

a86/ast.rkt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
(λ (a x n)
1919
(match x
2020
[(? symbol?)
21-
(unless (nasm-label? x)
22-
(error n "label names must conform to nasm restrictions"))
21+
(unless (asm-label? x)
22+
(error n "label names must conform to restrictions"))
2323
(values a ($ x))]
2424
[($ _)
2525
(values a x)]
@@ -38,7 +38,7 @@
3838
(match x
3939
[(? offset?) (values a (exp-normalize x))]
4040
[(? register?) (values a x)]
41-
[(? nasm-label? x) (values a ($ x))]
41+
[(? asm-label? x) (values a ($ x))]
4242
[($ _) (values a x)]
4343
[_
4444
(error n "expects label, register, or offset; given ~v" x)])))
@@ -187,7 +187,7 @@
187187
[(list (? exp-unop?) (? exp?)) #t]
188188
[(list (? exp-binop?) (? exp?) (? exp?)) #t]
189189
[($ _) #t]
190-
[(? nasm-label?) #t]
190+
[(? asm-label?) #t]
191191
[(? 64-bit-integer?) #t]
192192
[_ #f]))
193193

@@ -236,7 +236,7 @@
236236
(define (exp-normalize x)
237237
(match x
238238
[(? register?) x]
239-
[(? nasm-label?) ($ x)]
239+
[(? asm-label?) ($ x)]
240240
[(? Mem?) x]
241241
;[(Offset e1) (Offset (exp-normalize e1))]
242242
[(Plus e1 e2)
@@ -259,7 +259,7 @@
259259
(provide label?)
260260
(define (label? x)
261261
(and (symbol? x)
262-
(nasm-label? x)
262+
(asm-label? x)
263263
(not (register? x))))
264264

265265
(provide (struct-out $))
@@ -270,8 +270,8 @@
270270
(λ (x n)
271271
(unless (symbol? x)
272272
(error n "expects symbol; given ~v" x))
273-
(unless (nasm-label? x)
274-
(error n "label names must conform to nasm restrictions"))
273+
(unless (asm-label? x)
274+
(error n "label names must conform to restrictions"))
275275
(values x))
276276

277277
#;#;#;#:methods gen:equal+hash
@@ -638,7 +638,7 @@
638638
(exp? (Plus-e1 x))
639639
(exp? (Plus-e2 x)))
640640
(register? x)
641-
(nasm-label? x)
641+
(asm-label? x)
642642
($? x)
643643
(integer? x)))
644644

@@ -746,7 +746,7 @@
746746

747747
;; (U Instruction Asm) ... -> Asm
748748
;; Construct a "program", does some global well-formedness checking to help
749-
;; prevent confusing error messages as the nasm level
749+
;; prevent confusing error messages as the assembler level
750750
(define (prog . xs)
751751
(let ((p (apply seq xs)))
752752
(check-unique-label-decls p)
@@ -787,7 +787,7 @@
787787
(extern-decls asm)]))
788788

789789
;; Any -> Boolean
790-
(define (nasm-label? s)
790+
(define (asm-label? s)
791791
(and (symbol? s)
792792
(regexp-match #rx"^[a-zA-Z._?][a-zA-Z0-9_$#@~.?]*$" (symbol->string s))))
793793

@@ -848,7 +848,7 @@
848848
;; Symbol to Label
849849

850850
;; Symbol -> Label
851-
;; Produce a symbol that is a valid Nasm label
851+
;; Produce a symbol that is a valid assembly label
852852
;; Guarantees that (eq? s1 s2) <=> (eq? (symbol->label s1) (symbol->label s1))
853853
(provide symbol->label)
854854
(define (symbol->label s)

a86/check-assembler.rkt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#lang racket
2+
3+
(provide check-clang-available
4+
clang-version
5+
clang-version-16+?)
6+
7+
(require racket/gui/dynamic)
8+
9+
(define clang-missing-msg
10+
#<<HERE
11+
clang not found: either you have not installed clang
12+
or it cannot be found in your PATH: ~a.~a
13+
HERE
14+
)
15+
16+
(define finder-launch-msg
17+
#<<HERE
18+
19+
20+
It appears you launched DrRacket using Finder, which
21+
does not properly set up the PATH environment variable.
22+
Try launching DrRacket from the command line using the
23+
'drracket' command.
24+
HERE
25+
)
26+
27+
(define (macos?)
28+
(eq? 'macosx (system-type 'os)))
29+
30+
;; This will lie if you happen to run drracket from /, but that's OK.
31+
(define (launched-with-finder?)
32+
(equal? (string->path "/") (find-system-path 'orig-dir)))
33+
34+
(define (drracket?)
35+
(gui-available?))
36+
37+
;; -> [Maybe String]
38+
(define (clang-version-string)
39+
(parameterize ([current-output-port (open-output-string)]
40+
[current-error-port (open-output-string)])
41+
(and (system (format "~aclang -v 2>&1"
42+
(if (macos?)
43+
"arch -x86_64 "
44+
"")))
45+
(get-output-string (current-output-port)))))
46+
47+
(define (clang-version-16+?)
48+
(match (clang-version)
49+
[(list major _ _)
50+
(>= major 16)]
51+
[_ #f]))
52+
53+
;; -> [Maybe (list Natural Natural Natural)]
54+
(define (clang-version)
55+
(match (clang-version-string)
56+
[#f #f]
57+
[(regexp #rx"version ([0-9]+)\\.([0-9]+)\\.([0-9]+) "
58+
(list _
59+
(app string->number major)
60+
(app string->number minor)
61+
(app string->number patch)))
62+
(list major minor patch)]))
63+
64+
;; -> Void
65+
(define (check-clang-available)
66+
(define v (clang-version))
67+
(unless v
68+
(error (format clang-missing-msg
69+
(getenv "PATH")
70+
(if (and (drracket?) (macos?) (launched-with-finder?)) finder-launch-msg ""))))
71+
(unless (clang-version-16+?)
72+
(eprintf "clang 16.0.0 or later is recommended; some features may not work as expected.\n")))

a86/check-nasm.rkt

Lines changed: 0 additions & 78 deletions
This file was deleted.

a86/interp.rkt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
(define-logger a86)
1111

12-
(require "printer.rkt" "ast.rkt" "callback.rkt" "check-nasm.rkt"
12+
(require "printer.rkt" "ast.rkt" "callback.rkt" "check-assembler.rkt"
1313
(rename-in ffi/unsafe [-> _->]))
1414
(require (submod "printer.rkt" private))
1515

16-
;; Check NASM availability when required to fail fast.
17-
(check-nasm-available)
16+
;; Check clang availability when required to fail fast.
17+
(check-clang-available)
1818

1919
;; Bail out if we're not on an x86_64 Racket.
2020
(unless (eq? 'x86_64 (system-type 'arch))
@@ -84,7 +84,7 @@
8484

8585
(log-a86-info (~v a))
8686

87-
(define t.s (make-temporary-file "nasm~a.s"))
87+
(define t.s (make-temporary-file "clang-~a.s"))
8888
(define t.o (path-replace-extension t.s #".o"))
8989
(define t.so (path-replace-extension t.s #".so"))
9090
(define t.in (path-replace-extension t.s #".in"))
@@ -123,7 +123,7 @@
123123
(debug-transform a*)
124124
a*)))))
125125

126-
(nasm t.s t.o)
126+
(clang t.s t.o)
127127
(ld t.o t.so)
128128

129129
(define libt.so (ffi-lib t.so))
@@ -206,24 +206,24 @@
206206
(add-between (map (lambda (s) (string-append "\"" s "\"")) xs)
207207
" ")))
208208

209-
;;; Utilities for calling nasm and linker with informative error messages
209+
;;; Utilities for calling clang and linker with informative error messages
210210

211-
(struct exn:nasm exn:fail:user ())
212-
(define nasm-msg
211+
(struct exn:clang exn:fail:user ())
212+
(define assembly-error-msg
213213
(string-append
214214
"assembly error: make sure to use `prog` to construct an assembly program\n"
215215
"if you did and still get this error; please share with course staff."))
216216

217-
(define (nasm:error msg)
218-
(raise (exn:nasm (format "~a\n\n~a~a" nasm-msg msg (nasm-offending-line msg))
219-
(current-continuation-marks))))
217+
(define (clang:error msg)
218+
(raise (exn:clang (format "~a\n\n~a~a" assembly-error-msg msg (assembly-offending-line msg))
219+
(current-continuation-marks))))
220220

221-
(define (nasm-offending-line msg)
221+
(define (assembly-offending-line msg)
222222
(match (regexp-match
223223
"(.*):([0-9]+): error: " msg)
224224
[(list _ (app string->path file) (app string->number line))
225225
(format
226-
"\nnasm offending line: ~a"
226+
"\noffending line: ~a"
227227
(with-input-from-file file
228228
(thunk
229229
(let loop ([l (read-line)]
@@ -234,8 +234,8 @@
234234
[_ ""]))
235235

236236

237-
;; run nasm on t.s to create t.o
238-
(define (nasm t.s t.o)
237+
;; run clang on t.s to create t.o
238+
(define (clang t.s t.o)
239239
(define err-port (open-output-string))
240240
(define fmt (if (eq? (system-type 'os) 'macosx) 'macho64 'elf64))
241241
(define prefix
@@ -245,7 +245,7 @@
245245

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

250250
(struct exn:ld exn:fail:user ())
251251
(define (ld:error msg)

0 commit comments

Comments
 (0)