| 1 | ;;;; driver.lisp -- the command line, and a program's modules made into |
| 2 | ;;;; one executable. |
| 3 | ;;;; |
| 4 | ;;;; raz [-I dir]... [-o exe] [-S] file.raz |
| 5 | ;;;; |
| 6 | ;;;; The compiler reads the main module and, through their imports, every |
| 7 | ;;;; module it needs, Rt always among them. It then compiles the Lisp it |
| 8 | ;;;; made with SBCL, into this very image, and saves the image as the |
| 9 | ;;;; executable, to start at the run time's START (rt.lisp). -S also |
| 10 | ;;;; writes the Lisp to exe.lisp, for whoever wants to read it. |
| 11 | |
| 12 | (in-package :raz) |
| 13 | |
| 14 | (defparameter *version* "raz-sbcl 0.1.0, report 2026-10.23") |
| 15 | |
| 16 | (defun body-order (roots) |
| 17 | "The modules whose bodies run, in order (section 3): each after those |
| 18 | it imports, in the order of its imports." |
| 19 | (let ((seen '()) (order '())) |
| 20 | (labels ((visit (m) |
| 21 | (unless (member m seen) |
| 22 | (push m seen) |
| 23 | (dolist (imp (reverse (module-imports m))) |
| 24 | (let ((target (if (module-generic (import-target imp)) |
| 25 | (import-instance imp) |
| 26 | (import-target imp)))) |
| 27 | (when target (visit target)))) |
| 28 | (push m order)))) |
| 29 | (mapc #'visit roots)) |
| 30 | (nreverse order))) |
| 31 | |
| 32 | (defun all-modules () |
| 33 | "Every module of the program, each generic module's instances in its place." |
| 34 | (let ((ms '())) |
| 35 | (maphash (lambda (name m) (declare (ignore name)) |
| 36 | (if (module-generic m) |
| 37 | (dolist (i (module-instances m)) (push (cdr i) ms)) |
| 38 | (push m ms))) |
| 39 | *modules*) |
| 40 | ms)) |
| 41 | |
| 42 | (defun main-form (main) |
| 43 | "The program's function: every body, then main; its value the status." |
| 44 | (let* ((obj (gethash "main" (module-exports main))) |
| 45 | (call (cond ((null obj) 0) |
| 46 | ((not (eq (obj-kind obj) :proc)) |
| 47 | (fail-tok (obj-tok obj) "main is a procedure")) |
| 48 | ((and (null (sig-params (obj-sig obj))) (null (sig-results (obj-sig obj)))) |
| 49 | `(progn (,(obj-sym obj)) 0)) |
| 50 | ((and (null (sig-params (obj-sig obj))) |
| 51 | (equal (sig-results (obj-sig obj)) (list (basic "INT")))) |
| 52 | `(,(obj-sym obj))) |
| 53 | (t (fail-tok (obj-tok obj) "main is proc main*; or proc main*: INT;"))))) |
| 54 | `(lambda () |
| 55 | ,@(loop for m in (body-order (list (gethash "Rt" *modules*) main)) |
| 56 | when (module-body m) collect `(,(module-body m))) |
| 57 | ,call))) |
| 58 | |
| 59 | (defun compile-forms (forms) |
| 60 | "Compiles FORMS into this image, quietly." |
| 61 | (handler-bind ((warning #'muffle-warning) |
| 62 | (sb-ext:compiler-note #'muffle-warning)) |
| 63 | (let ((*error-output* (make-broadcast-stream))) |
| 64 | (with-compilation-unit () |
| 65 | (dolist (f forms) |
| 66 | (let ((*package* (find-package :raz))) |
| 67 | (funcall (compile nil `(lambda () ,f))))))))) |
| 68 | |
| 69 | (defun compile-program (file out includes dump) |
| 70 | (let* ((path (probe-file file)) |
| 71 | (name (pathname-name file)) |
| 72 | (*search-dirs* (append (list (directory-namestring (or path #p"./"))) |
| 73 | includes |
| 74 | (list (namestring (merge-pathnames "lib/" (directory-namestring sb-ext:*runtime-pathname*)))))) |
| 75 | (*file* file)) |
| 76 | (unless path (error 'raz-error :text (format nil "~a: no such file" file))) |
| 77 | (setf *universe* (make-universe)) |
| 78 | (let* ((none (make-tok :eof nil 1 1)) |
| 79 | (main (progn (load-module "Rt" none) (load-module name none (namestring path)))) |
| 80 | (modules (all-modules))) |
| 81 | (when (module-generic main) |
| 82 | (error 'raz-error :text (format nil "~a: ~a is generic: it is compiled where a module imports it" |
| 83 | file name))) |
| 84 | (when (and (some #'module-static modules) (some #'module-uses-c modules)) |
| 85 | (error 'raz-error |
| 86 | :text (format nil "~a: the program uses C (~{~a~^, ~}), which ~{~a~^, ~} doesn't allow by link static" |
| 87 | file (mapcar #'module-name (remove-if-not #'module-uses-c modules)) |
| 88 | (mapcar #'module-name (remove-if-not #'module-static modules))))) |
| 89 | (let ((forms (reverse *forms*)) (entry (let ((*file* file)) (main-form main)))) |
| 90 | (when dump |
| 91 | (with-open-file (s (format nil "~a.lisp" out) :direction :output :if-exists :supersede) |
| 92 | (let ((*package* (find-package :raz)) (*print-circle* t) (*print-right-margin* 110)) |
| 93 | (dolist (f (append forms (list entry))) (pprint f s) (terpri s))))) |
| 94 | (compile-forms forms) |
| 95 | (setf *main* (let ((*package* (find-package :raz))) (compile nil entry))) |
| 96 | (setf *procs* (let ((v (make-array (1+ (length *proc-values*)) :initial-element nil))) |
| 97 | (loop for (h . sym) in *proc-values* do (setf (svref v h) (symbol-function sym))) |
| 98 | v)) |
| 99 | (setf *data-image* (coerce *data* '(simple-array (unsigned-byte 8) (*)))))))) |
| 100 | |
| 101 | (defun usage () |
| 102 | (format *error-output* "usage: raz [-I dir]... [-o exe] [-S] file.raz~%") |
| 103 | (sb-ext:exit :code 2 :abort t)) |
| 104 | |
| 105 | (defun main () |
| 106 | "The compiler's command line." |
| 107 | (let ((args (rest sb-ext:*posix-argv*)) (includes '()) (out nil) (dump nil) (file nil)) |
| 108 | (loop while args |
| 109 | do (let ((a (pop args))) |
| 110 | (cond ((string= a "-I") (unless args (usage)) (setf includes (append includes (list (pop args))))) |
| 111 | ((string= a "-o") (unless args (usage)) (setf out (pop args))) |
| 112 | ((string= a "-S") (setf dump t)) |
| 113 | ((string= a "-Werror")) ; this compiler gives no warnings |
| 114 | ((string= a "--version") (format t "~a~%" *version*) (sb-ext:exit :code 0)) |
| 115 | ((and (> (length a) 1) (char= (char a 0) #\-)) (usage)) |
| 116 | (file (usage)) |
| 117 | (t (setf file a))))) |
| 118 | (unless file (usage)) |
| 119 | (let ((out (or out (pathname-name file)))) |
| 120 | (handler-case |
| 121 | ;; with RAZ_DEBUG set, the compiler's own mistakes come with a backtrace |
| 122 | (handler-bind ((error (lambda (e) |
| 123 | (when (and (sb-ext:posix-getenv "RAZ_DEBUG") (not (typep e 'raz-error))) |
| 124 | (sb-debug:print-backtrace :count 25) |
| 125 | (format *error-output* "~a~%" e) |
| 126 | (sb-ext:exit :code 3 :abort t))))) |
| 127 | (sb-int:with-float-traps-masked (:overflow :invalid :inexact :divide-by-zero :underflow) |
| 128 | (compile-program file out includes dump))) |
| 129 | (raz-error (e) |
| 130 | (format *error-output* "~a~%" (raz-error-text e)) |
| 131 | (sb-ext:exit :code 1 :abort t)) |
| 132 | (error (e) |
| 133 | ;; a mistake of the compiler's own, not the program's |
| 134 | (format *error-output* "raz: internal error: ~a~%" e) |
| 135 | (sb-ext:exit :code 3 :abort t))) |
| 136 | (sb-ext:save-lisp-and-die out :executable t :toplevel #'start :save-runtime-options t)))) |