|
| 1 | +#lang racket |
| 2 | +(provide/contract |
| 3 | + [current-objs (parameter/c (listof path-string?))] |
| 4 | + [asm-interp (-> (listof instruction?) any/c)] |
| 5 | + [asm-interp/io (-> (listof instruction?) string? any/c)]) |
| 6 | + |
| 7 | +(require "printer.rkt" "ast.rkt" "callback.rkt" |
| 8 | + (rename-in ffi/unsafe [-> _->])) |
| 9 | + |
| 10 | + |
| 11 | +;; Assembly code is linked with object files in this parameter |
| 12 | +(define current-objs |
| 13 | + (make-parameter '())) |
| 14 | + |
| 15 | +;; Asm -> Value |
| 16 | +;; Interpret (by assemblying, linking, and loading) x86-64 code |
| 17 | +;; Assume: entry point is "entry" |
| 18 | +(define (asm-interp a) |
| 19 | + (asm-interp/io a #f)) |
| 20 | + |
| 21 | +(define fopen |
| 22 | + (get-ffi-obj "fopen" (ffi-lib #f) (_fun _path _string/utf-8 _-> _pointer))) |
| 23 | + |
| 24 | +(define fflush |
| 25 | + (get-ffi-obj "fflush" (ffi-lib #f) (_fun _pointer _-> _void))) |
| 26 | + |
| 27 | +(define fclose |
| 28 | + (get-ffi-obj "fclose" (ffi-lib #f) (_fun _pointer _-> _void))) |
| 29 | + |
| 30 | +(define fmt (if (eq? (system-type 'os) 'macosx) 'macho64 'elf64)) |
| 31 | + |
| 32 | +;; Asm String -> (cons Value String) |
| 33 | +;; Like asm-interp, but uses given string for input and returns |
| 34 | +;; result with string output |
| 35 | +(define (asm-interp/io a input) |
| 36 | + (define t.s (make-temporary-file "nasm~a.s")) |
| 37 | + (define t.o (path-replace-extension t.s #".o")) |
| 38 | + (define t.so (path-replace-extension t.s #".so")) |
| 39 | + (define t.in (path-replace-extension t.s #".in")) |
| 40 | + (define t.out (path-replace-extension t.s #".out")) |
| 41 | + |
| 42 | + (with-output-to-file t.s |
| 43 | + #:exists 'truncate |
| 44 | + (λ () |
| 45 | + ; (parameterize ((current-shared? #t)) |
| 46 | + (displayln (asm-string a)))) |
| 47 | + |
| 48 | + (nasm t.s t.o) |
| 49 | + (ld t.o t.so) |
| 50 | + |
| 51 | + (define libt.so (ffi-lib t.so)) |
| 52 | + |
| 53 | + (define init-label |
| 54 | + (match (findf Label? a) |
| 55 | + [(Label l) l] |
| 56 | + [_ (error "no initial label found")])) |
| 57 | + |
| 58 | + (define entry |
| 59 | + (get-ffi-obj init-label libt.so (_fun _pointer _-> _int64))) |
| 60 | + |
| 61 | + ;; install our own `error_handler` procedure to prevent `exit` calls |
| 62 | + ;; from interpreted code bringing down the parent process. All of |
| 63 | + ;; these hooks into the runtime need a better API and documentation, |
| 64 | + ;; but this is a rough hack to make Extort work for now. |
| 65 | + (when (ffi-obj-ref "error_handler" libt.so (thunk #f)) |
| 66 | + (set-ffi-obj! "error_handler" libt.so _pointer |
| 67 | + (function-ptr (λ () (raise 'err)) (_fun _-> _void)))) |
| 68 | + |
| 69 | + |
| 70 | + (define current-heap #f) |
| 71 | + |
| 72 | + ;; allocate a heap |
| 73 | + (when (ffi-obj-ref "heap" libt.so (thunk #f)) |
| 74 | + (set! current-heap (make-c-parameter "heap" libt.so _pointer)) |
| 75 | + |
| 76 | + (if (ffi-obj-ref "from" libt.so (thunk #f)) |
| 77 | + (begin |
| 78 | + (current-heap |
| 79 | + ; IMPROVE ME: hard-coded heap size |
| 80 | + (malloc _int64 20000 'raw)) |
| 81 | + (set-ffi-obj! "from" libt.so _pointer (current-heap)) |
| 82 | + (set-ffi-obj! "to" libt.so _pointer (ptr-add (current-heap) 10000 _int64)) |
| 83 | + (set-ffi-obj! "types" libt.so _pointer (malloc _int32 10000))) |
| 84 | + (current-heap |
| 85 | + ; IMPROVE ME: hard-coded heap size |
| 86 | + (malloc _int64 10000 'raw)))) |
| 87 | + |
| 88 | + (delete-file t.s) |
| 89 | + (delete-file t.o) |
| 90 | + (delete-file t.so) |
| 91 | + (if input |
| 92 | + (let () |
| 93 | + (unless (and (ffi-obj-ref "in" libt.so (thunk #f)) |
| 94 | + (ffi-obj-ref "out" libt.so (thunk #f))) |
| 95 | + (error "asm-interp/io: running in IO mode without IO linkage")) |
| 96 | + |
| 97 | + (with-output-to-file t.in #:exists 'truncate |
| 98 | + (thunk (display input))) |
| 99 | + |
| 100 | + (define current-in |
| 101 | + (make-c-parameter "in" libt.so _pointer)) |
| 102 | + (define current-out |
| 103 | + (make-c-parameter "out" libt.so _pointer)) |
| 104 | + |
| 105 | + (current-in (fopen t.in "r")) |
| 106 | + (current-out (fopen t.out "w")) |
| 107 | + |
| 108 | + (define result |
| 109 | + (begin0 |
| 110 | + (with-handlers ((symbol? identity)) |
| 111 | + (guard-foreign-escape |
| 112 | + (if current-heap |
| 113 | + (cons (current-heap) (entry (current-heap))) |
| 114 | + (entry #f)))) |
| 115 | + #; |
| 116 | + (when current-heap |
| 117 | + (free (current-heap))))) |
| 118 | + |
| 119 | + (fflush (current-out)) |
| 120 | + (fclose (current-in)) |
| 121 | + (fclose (current-out)) |
| 122 | + |
| 123 | + (define output (file->string t.out)) |
| 124 | + (delete-file t.in) |
| 125 | + (delete-file t.out) |
| 126 | + (cons result output)) |
| 127 | + |
| 128 | + (begin0 |
| 129 | + (with-handlers ((symbol? identity)) |
| 130 | + (guard-foreign-escape |
| 131 | + (if current-heap |
| 132 | + (cons (current-heap) (entry (current-heap))) |
| 133 | + (entry #f)))) |
| 134 | + #; |
| 135 | + (when current-heap |
| 136 | + (free (current-heap)))))) |
| 137 | + |
| 138 | + |
| 139 | +(define (string-splice xs) |
| 140 | + (apply string-append |
| 141 | + (add-between (map (lambda (s) (string-append "\"" s "\"")) xs) |
| 142 | + " "))) |
| 143 | + |
| 144 | +;;; Utilities for calling nasm and linker with informative error messages |
| 145 | + |
| 146 | +(struct exn:nasm exn:fail:user ()) |
| 147 | +(define nasm-msg |
| 148 | + (string-append |
| 149 | + "assembly error: make sure to use `prog` to construct an assembly program\n" |
| 150 | + "if you did and still get this error; please share with course staff.")) |
| 151 | + |
| 152 | +(define (nasm:error msg) |
| 153 | + (raise (exn:nasm (format "~a\n\n~a" nasm-msg msg) |
| 154 | + (current-continuation-marks)))) |
| 155 | + |
| 156 | +;; run nasm on t.s to create t.o |
| 157 | +(define (nasm t.s t.o) |
| 158 | + (define err-port (open-output-string)) |
| 159 | + (unless (parameterize ((current-error-port err-port)) |
| 160 | + (system (format "nasm -f ~a ~a -o ~a" fmt t.s t.o))) |
| 161 | + (nasm:error (get-output-string err-port)))) |
| 162 | + |
| 163 | +(struct exn:ld exn:fail:user ()) |
| 164 | +(define (ld:error msg) |
| 165 | + (raise (exn:ld (format "link error: ~a" msg) |
| 166 | + (current-continuation-marks)))) |
| 167 | + |
| 168 | +(define (ld:undef-symbol s) |
| 169 | + (ld:error |
| 170 | + (string-append |
| 171 | + (format "symbol ~a not defined in linked objects: ~a\n" s (current-objs)) |
| 172 | + "use `current-objs` to link in object containing symbol definition."))) |
| 173 | + |
| 174 | +;; link together t.o with current-objs to create shared t.so |
| 175 | +(define (ld t.o t.so) |
| 176 | + (define err-port (open-output-string)) |
| 177 | + (define objs (string-splice (current-objs))) |
| 178 | + (define -z-defs-maybe |
| 179 | + (if (eq? (system-type 'os) 'macosx) |
| 180 | + "" |
| 181 | + "-z defs ")) |
| 182 | + (unless (parameterize ((current-error-port err-port)) |
| 183 | + (system (format "gcc ~a-v -shared ~a ~a -o ~a" |
| 184 | + -z-defs-maybe |
| 185 | + t.o objs t.so))) |
| 186 | + (define err-msg |
| 187 | + (get-output-string err-port)) |
| 188 | + (match (or (regexp-match #rx"Undefined.*\"(.*)\"" err-msg) ; mac |
| 189 | + (regexp-match #rx"undefined reference to `(.*)'" err-msg)) ; linux |
| 190 | + [(list _ symbol) (ld:undef-symbol symbol)] |
| 191 | + [_ (ld:error (format "unknown link error.\n\n~a" err-msg))]))) |
0 commit comments