| 1 | ;;;; decl.lisp -- modules, imports, generic instances, declarations, |
| 2 | ;;;; types and procedures (report, sections 3, 4, 5, 8 and 9). |
| 3 | |
| 4 | (in-package :raz) |
| 5 | |
| 6 | ;;; The scope around every module (section 2). |
| 7 | |
| 8 | (defparameter *builtins* |
| 9 | '("SIZE" "ADR" "LEN" "NEW" "MSG" "COPY" "SYSCALL" "INC" "DEC" "FENCE" |
| 10 | "CAS" "XCHG" "XADD" "ARGC" "ARGV")) |
| 11 | |
| 12 | (defun make-universe () |
| 13 | (let ((*scope* (make-scope))) |
| 14 | (dolist (ty *basic-types*) |
| 15 | (declare-name (make-obj :name (ty-name ty) :kind :type :type ty))) |
| 16 | (declare-name (make-obj :name "true" :kind :const :type (basic "BOOL") :val t)) |
| 17 | (declare-name (make-obj :name "false" :kind :const :type (basic "BOOL") :val nil)) |
| 18 | (declare-name (make-obj :name "nil" :kind :const :type *nil-type* :val 0)) |
| 19 | (dolist (b *builtins*) (declare-name (make-obj :name b :kind :builtin))) |
| 20 | *scope*)) |
| 21 | |
| 22 | ;;; Names declared at the top of a module may be exported. |
| 23 | |
| 24 | (defun ident-def (top) |
| 25 | "An IdentDef: the name, and whether it is exported (only at the top)." |
| 26 | (let ((tk (tok)) (name (ident))) |
| 27 | (values name (when (accept :*) |
| 28 | (unless top (fail-tok tk "only a module's own names are exported")) |
| 29 | t) |
| 30 | tk))) |
| 31 | |
| 32 | (defun declare-top (obj) |
| 33 | "Declares OBJ in the current scope, and among the module's exports if |
| 34 | it is exported." |
| 35 | (declare-name obj) |
| 36 | (when (obj-exported obj) |
| 37 | (setf (gethash (obj-name obj) (module-exports *module*)) obj)) |
| 38 | obj) |
| 39 | |
| 40 | ;;; Types (section 5). |
| 41 | |
| 42 | (defvar *forward-types* nil |
| 43 | "In a `type` section, the pointers whose base is declared later in it, |
| 44 | (type name tok) for each; elsewhere :none.") |
| 45 | (defvar *type-exported* nil "Whether the type being declared is exported: its case and enumeration names are too.") |
| 46 | (defvar *top* nil "Whether the declarations being read are a module's own.") |
| 47 | |
| 48 | (defun parse-type () |
| 49 | (let ((tk (tok))) |
| 50 | (case (kind) |
| 51 | (:ident (named-type)) |
| 52 | (:|[| (next-tok) |
| 53 | (if (accept :|]|) |
| 54 | (make-slice-type (parse-type)) |
| 55 | (let ((n (constant-int "an array's length"))) |
| 56 | (unless (> n 0) (fail-tok tk "an array's length is greater than 0")) |
| 57 | (expect :|]|) |
| 58 | (check-size (make-array-type n (parse-type)) tk)))) |
| 59 | (:^ (next-tok) (pointer-type-rest)) |
| 60 | (:packed (next-tok) (expect :record) (record-type t tk)) |
| 61 | (:record (next-tok) (record-type nil tk)) |
| 62 | (:variant (next-tok) (variant-type)) |
| 63 | (:|(| (next-tok) (enum-type)) |
| 64 | (:set (next-tok) (expect :of) |
| 65 | (let ((e (named-type))) |
| 66 | (unless (is e :enum) (fail-tok tk "a set is of an enumeration")) |
| 67 | (when (> (length (ty-names e)) 64) (fail-tok tk "a set's enumeration has at most 64 names")) |
| 68 | (make-set-type e))) |
| 69 | (:proc (next-tok) (make-proc-type (signature nil))) |
| 70 | (t (fail-here "a type expected, not ~a" (describe-tok tk)))))) |
| 71 | |
| 72 | (defun check-size (ty tk) |
| 73 | (when (> (ty-size ty) (int-max (basic "INT"))) (fail-tok tk "the type is too big")) |
| 74 | ty) |
| 75 | |
| 76 | (defun named-type () |
| 77 | (multiple-value-bind (obj tk) (qualified-obj) |
| 78 | (unless (eq (obj-kind obj) :type) (fail-tok tk "~a isn't a type" (obj-name obj))) |
| 79 | (obj-type obj))) |
| 80 | |
| 81 | (defun pointer-type-rest () |
| 82 | "A pointer type, after its ^." |
| 83 | (let ((vol (and (accept :volatile) t))) |
| 84 | (if (and (listp *forward-types*) (eq (kind) :ident) (not (eq (kind 1) :|.|)) |
| 85 | (not (lookup (tok-val (tok))))) |
| 86 | ;; a type declared later in this section (section 4) |
| 87 | (let* ((tk (next-tok)) (ty (make-ptr-type (list :forward (tok-val tk)) vol))) |
| 88 | (push (list ty (tok-val tk) tk) *forward-types*) |
| 89 | ty) |
| 90 | (make-ptr-type (parse-type) vol)))) |
| 91 | |
| 92 | (defun resolve-forward-types () |
| 93 | (dolist (f *forward-types*) |
| 94 | (destructuring-bind (ty name tk) f |
| 95 | (let ((obj (lookup name))) |
| 96 | (unless (and obj (eq (obj-kind obj) :type)) |
| 97 | (fail-tok tk "~a isn't declared as a type" name)) |
| 98 | (setf (ty-base ty) (obj-type obj)))))) |
| 99 | |
| 100 | (defun field-list (fields &key exports) |
| 101 | "IdentList : Type, the names added to FIELDS (each name new)." |
| 102 | (let ((names (loop collect (multiple-value-list (if exports (ident-def *top*) (values (ident) nil (tok 0)))) |
| 103 | while (accept :|,|)))) |
| 104 | (expect :|:|) |
| 105 | (let ((ty (parse-type))) |
| 106 | (dolist (n names fields) |
| 107 | (destructuring-bind (name exported tk) n |
| 108 | (when (find name fields :key #'field-name :test #'string=) |
| 109 | (fail-tok tk "the field ~a is declared twice" name)) |
| 110 | (setf fields (append fields (list (make-field :name name :type ty :exported exported))))))))) |
| 111 | |
| 112 | (defun record-type (packed tk) |
| 113 | (let ((fields '())) |
| 114 | (loop until (accept :end) |
| 115 | do (setf fields (field-list fields :exports t)) (expect :|;|)) |
| 116 | (check-size (make-record-type fields packed *module*) tk))) |
| 117 | |
| 118 | (defun declare-member (name kind type val tk) |
| 119 | "A name a type declares in the scope around it: a case or an enumeration's name." |
| 120 | (declare-top (make-obj :name name :kind kind :type type :val val :tok tk |
| 121 | :exported *type-exported* :module *module*))) |
| 122 | |
| 123 | (defun variant-type () |
| 124 | (let ((cases '()) (toks '())) |
| 125 | (loop until (accept :end) |
| 126 | do (let* ((tk (tok)) (name (ident)) (fields '())) |
| 127 | (when (accept :|(|) |
| 128 | (loop (setf fields (field-list fields)) (unless (accept :|;|) (return))) |
| 129 | (expect :|)|)) |
| 130 | (when (find name cases :key #'vcase-name :test #'string=) |
| 131 | (fail-tok tk "the case ~a is declared twice" name)) |
| 132 | (setf cases (append cases (list (make-vcase :name name :index (length cases) :fields fields)))) |
| 133 | (push tk toks)) |
| 134 | (expect :|;|)) |
| 135 | ;; the cases are named in the scope around the type (5.6) |
| 136 | (let ((v (make-variant-type cases *module*))) |
| 137 | (loop for c in cases for tk in (reverse toks) |
| 138 | do (declare-member (vcase-name c) :case v c tk)) |
| 139 | v))) |
| 140 | |
| 141 | (defun enum-type () |
| 142 | (let ((names (loop collect (cons (tok) (ident)) while (accept :|,|)))) |
| 143 | (expect :|)|) |
| 144 | (when (> (length names) 256) (fail-tok (car (first names)) "an enumeration has at most 256 names")) |
| 145 | (let ((ty (make-enum-type (mapcar #'cdr names) *module*))) |
| 146 | (loop for (tk . name) in names for i from 0 |
| 147 | do (declare-member name :const ty i tk)) |
| 148 | ty))) |
| 149 | |
| 150 | (defun signature (extern) |
| 151 | "A Signature: its parameters, `...` for an EXTERN, and its results." |
| 152 | (let ((params '()) (varargs nil)) |
| 153 | (when (accept :|(|) |
| 154 | (unless (eq (kind) :|)|) |
| 155 | (loop |
| 156 | (when (eq (kind) :...) |
| 157 | (unless (and extern params) (fail-here "... is for an extern, after its parameters")) |
| 158 | (next-tok) (setf varargs t) (return)) |
| 159 | (let* ((var (accept :var)) |
| 160 | (names (loop collect (cons (tok) (ident)) while (accept :|,|)))) |
| 161 | (expect :|:|) |
| 162 | (let ((ty (parse-type))) |
| 163 | (dolist (n names) |
| 164 | (when (find (cdr n) params :key #'first :test #'string=) |
| 165 | (fail-tok (car n) "the parameter ~a is declared twice" (cdr n))) |
| 166 | (setf params (append params (list (list (cdr n) ty (and var t)))))))) |
| 167 | (unless (accept :|;|) (return)))) |
| 168 | (expect :|)|)) |
| 169 | (make-sig :params params :varargs varargs |
| 170 | :results (when (accept :|:|) |
| 171 | (if (accept :|(|) |
| 172 | (prog1 (loop collect (parse-type) while (accept :|,|)) (expect :|)|)) |
| 173 | (list (parse-type))))))) |
| 174 | |
| 175 | ;;; Constants (section 4). |
| 176 | |
| 177 | (defun constant-int (what) |
| 178 | (let* ((tk (tok)) (x (need-value (expression)))) |
| 179 | (unless (and (const-p x) (member (ty-kind (item-type x)) '(:int :untyped-int))) |
| 180 | (fail-tok tk "~a is an integer constant" what)) |
| 181 | (item-val x))) |
| 182 | |
| 183 | (defun write-const (addr ty item tk) |
| 184 | "Writes the constant ITEM, as a TY, at ADDR in the data block." |
| 185 | (let ((why (assign-error item ty))) |
| 186 | (when why (fail-tok tk "~a" why))) |
| 187 | (case (item-mode item) |
| 188 | (:literal |
| 189 | (destructuring-bind (kind elems) (item-val item) |
| 190 | (ecase kind |
| 191 | (:empty) |
| 192 | (:array (if (is ty :slice) |
| 193 | (let ((arr (data-alloc (* (length elems) (ty-size (ty-elem ty))) (ty-align (ty-elem ty))))) |
| 194 | (loop for e in elems for i from 0 |
| 195 | do (write-const (+ arr (* i (ty-size (ty-elem ty)))) (ty-elem ty) e tk)) |
| 196 | (data-put-int addr arr 8) (data-put-int (+ addr 8) (length elems) 8)) |
| 197 | (loop for e in elems for i from 0 |
| 198 | do (write-const (+ addr (* i (ty-size (ty-elem ty)))) (ty-elem ty) e tk)))) |
| 199 | (:record (loop for (name . e) in elems |
| 200 | for f = (find-field ty name tk) |
| 201 | do (write-const (+ addr (field-offset f)) (field-type f) e tk))) |
| 202 | (:set (write-const addr ty (const-item ty (build-literal item ty)) tk))))) |
| 203 | (:proc (data-put-int addr (proc-handle (item-obj item)) 8)) |
| 204 | (:const |
| 205 | (let ((v (item-val item)) (from (item-type item))) |
| 206 | (case (ty-kind ty) |
| 207 | ((:int :enum :set :error :ptr :proc) (data-put-int addr v (ty-size ty))) |
| 208 | (:bool (data-put-int addr (if v 1 0) 1)) |
| 209 | (:real (if (= (ty-bits ty) 32) |
| 210 | (data-put-int addr (sb-kernel:single-float-bits (coerce v 'single-float)) 4) |
| 211 | (data-put-int addr (sb-kernel:double-float-bits (coerce v 'double-float)) 8))) |
| 212 | (:slice (data-put-int addr (string-address v) 8) (data-put-int (+ addr 8) (length v) 8)) |
| 213 | (:array (data-put addr v)) |
| 214 | (t (fail-tok tk "~a isn't a constant of ~a" (type-name from) (type-name ty)))))) |
| 215 | (:var |
| 216 | (unless (eq (item-ro item) :const) (fail-tok tk "this isn't a constant")) |
| 217 | (let ((src (item-form item)) (from (item-type item))) |
| 218 | (if (is ty :slice) |
| 219 | (progn (data-put-int addr src 8) (data-put-int (+ addr 8) (ty-len from) 8)) |
| 220 | (let ((off (- src +data-base+))) |
| 221 | (data-put addr (subseq *data* off (+ off (ty-size ty)))))))) |
| 222 | (t (fail-tok tk "this isn't a constant")))) |
| 223 | |
| 224 | (defun const-section () |
| 225 | (loop while (eq (kind) :ident) |
| 226 | do (multiple-value-bind (name exported tk) (ident-def *top*) |
| 227 | (let ((ty (when (accept :|:|) (parse-type)))) |
| 228 | (expect :=) |
| 229 | (let* ((etk (tok)) (x (expression))) |
| 230 | (declare-top |
| 231 | (cond |
| 232 | ((null ty) |
| 233 | (unless (or (const-p x) (and (eq (item-mode x) :var) (eq (item-ro x) :const))) |
| 234 | (fail-tok etk "a constant's value is a constant")) |
| 235 | (make-obj :name name :kind :const :type (item-type x) :tok tk :exported exported |
| 236 | :val (if (const-p x) (item-val x) (item-form x)))) |
| 237 | ((is ty :error) |
| 238 | (unless (and (const-p x) (is (item-type x) :string)) |
| 239 | (fail-tok etk "an ERROR is made by a string")) |
| 240 | (make-obj :name name :kind :const :type ty :tok tk :exported exported |
| 241 | :val (error-value (item-val x)))) |
| 242 | ((member (ty-kind ty) '(:array :record)) |
| 243 | (let ((addr (data-alloc (ty-size ty) (ty-align ty)))) |
| 244 | (write-const addr ty x etk) |
| 245 | (make-obj :name name :kind :const :type ty :tok tk :exported exported :val addr))) |
| 246 | (t (fail-tok tk "a constant with a type is an ERROR, an array or a record"))))))) |
| 247 | (expect :|;|))) |
| 248 | |
| 249 | (defun type-section () |
| 250 | (let ((*forward-types* '())) |
| 251 | (loop while (eq (kind) :ident) |
| 252 | do (multiple-value-bind (name exported tk) (ident-def *top*) |
| 253 | (expect :=) |
| 254 | (let* ((alias (and (eq (kind) :ident) (member (kind (if (eq (kind 1) :|.|) 3 1)) '(:|;|)))) |
| 255 | (ty (let ((*type-exported* exported)) (parse-type)))) |
| 256 | ;; `type A = B` names B's type; anything else makes a new one |
| 257 | (unless (or alias (ty-name ty)) (setf (ty-name ty) name)) |
| 258 | (declare-top (make-obj :name name :kind :type :type ty :tok tk :exported exported)))) |
| 259 | (expect :|;|)) |
| 260 | (resolve-forward-types))) |
| 261 | |
| 262 | (defun var-section () |
| 263 | (loop while (eq (kind) :ident) |
| 264 | do (let ((names (loop collect (multiple-value-list (ident-def *top*)) while (accept :|,|)))) |
| 265 | (expect :|:|) |
| 266 | (let ((ty (parse-type))) |
| 267 | (dolist (n names) |
| 268 | (destructuring-bind (name exported tk) n |
| 269 | (declare-top |
| 270 | (make-obj :name name :kind :var :type ty :tok tk :exported exported |
| 271 | :module (when *top* *module*) |
| 272 | :where (if *top* |
| 273 | (list :global (data-alloc (ty-size ty) (ty-align ty))) |
| 274 | (list :local (alloc-offset (ty-size ty) (ty-align ty))))))))) |
| 275 | (expect :|;|)))) |
| 276 | |
| 277 | (defun alloc-offset (size align) |
| 278 | (third (alloc size align))) |
| 279 | |
| 280 | (defun declarations () |
| 281 | "Const, type and var sections, and at a module's top procedures and externs." |
| 282 | (loop |
| 283 | (case (kind) |
| 284 | (:const (next-tok) (const-section)) |
| 285 | (:type (next-tok) (type-section)) |
| 286 | (:var (next-tok) (var-section)) |
| 287 | (:proc (if *top* (progn (next-tok) (proc-declaration)) (fail-here "procedures don't nest"))) |
| 288 | (:extern (if *top* (progn (next-tok) (extern-declaration)) (fail-here "an extern is a module's"))) |
| 289 | (t (return))))) |
| 290 | |
| 291 | ;;; Procedures (section 8). |
| 292 | |
| 293 | (defun proc-declaration () |
| 294 | (multiple-value-bind (name exported tk) (ident-def t) |
| 295 | (let* ((sig (signature nil)) |
| 296 | (old (gethash name (scope-table *scope*))) |
| 297 | (obj (if (and old (eq (obj-kind old) :proc) (obj-forward old) (not (obj-defined old))) |
| 298 | (progn |
| 299 | (unless (sig-alike-p sig (obj-sig old)) |
| 300 | (fail-tok tk "~a's signature isn't its forward declaration's" name)) |
| 301 | (setf (obj-sig old) sig) |
| 302 | old) |
| 303 | (declare-top (make-obj :name name :kind :proc :sig sig :tok tk :exported exported |
| 304 | :sym (lisp-name name)))))) |
| 305 | (expect :|;|) |
| 306 | (cond ((accept :forward) |
| 307 | (when (eq obj old) (fail-tok tk "~a is declared forward twice" name)) |
| 308 | (setf (obj-forward obj) t)) |
| 309 | (*interface-only* (skip-body) (setf (obj-defined obj) t)) |
| 310 | ((eq (kind) :code) (next-tok) (code-procedure obj)) |
| 311 | (t (procedure-body obj) |
| 312 | (setf (obj-defined obj) t))) |
| 313 | (when (eq (kind) :ident) |
| 314 | (unless (string= (tok-val (tok)) name) |
| 315 | (fail-here "the procedure ends with its own name, ~a" name)) |
| 316 | (next-tok)) |
| 317 | (expect :|;|)))) |
| 318 | |
| 319 | (defun skip-body () |
| 320 | "Reads past a procedure's body unchecked, for a file read only for its |
| 321 | interface: its local declarations and statements, or its code." |
| 322 | (if (accept :code) |
| 323 | (expect :end) |
| 324 | (let ((depth 0)) |
| 325 | (loop (let ((k (kind))) |
| 326 | (next-tok) |
| 327 | (case k |
| 328 | ((:begin :if :while :for :match :record :variant) (incf depth)) |
| 329 | (:end (when (= (decf depth) 0) (return))) |
| 330 | (:eof (fail-here "the procedure never ends")))))))) |
| 331 | |
| 332 | (defun procedure-body (obj) |
| 333 | "Compiles a procedure's declarations and statements into a DEFUN." |
| 334 | (let* ((sig (obj-sig obj)) |
| 335 | (*scope* (make-scope :parent *scope*)) |
| 336 | (*proc* (make-proc :obj obj :sig sig :block (gensym "PROC") :results (sig-results sig))) |
| 337 | (*top* nil) |
| 338 | (psyms '()) (stores '())) |
| 339 | (setf (proc-dests *proc*) (loop for r in (sig-results sig) collect (when (aggregate-p r) (var-sym "D")))) |
| 340 | (dolist (p (sig-params sig)) |
| 341 | (destructuring-bind (name ty var) p |
| 342 | (let ((s (var-sym "P"))) |
| 343 | (push s psyms) |
| 344 | (if var |
| 345 | (let ((off (alloc-offset 8 8))) |
| 346 | (push `(st :u64 (+ fp ,off) ,s) stores) |
| 347 | (declare-name (make-obj :name name :kind :var :type ty :where (list :var-param off) :tok (tok)))) |
| 348 | (let ((off (alloc-offset (ty-size ty) (ty-align ty)))) |
| 349 | (push (if (aggregate-p ty) `(move (+ fp ,off) ,s ,(ty-size ty)) `(st ,(access-kind ty) (+ fp ,off) ,s)) |
| 350 | stores) |
| 351 | (declare-name (make-obj :name name :kind :var :type ty :where (list :local off) :tok (tok)))))))) |
| 352 | (declarations) |
| 353 | (expect :begin) |
| 354 | (let ((body (statements :end))) |
| 355 | (expect :end) |
| 356 | (emit (procedure-defun (obj-sym obj) (reverse psyms) (remove nil (proc-dests *proc*)) |
| 357 | (reverse stores) body (zeros-form (sig-results sig))))))) |
| 358 | |
| 359 | (defun procedure-defun (sym params dests stores body end) |
| 360 | ;; The frame is pushed by moving *SP*, and popped by moving it back as |
| 361 | ;; the procedure returns, by RETURN-FROM or by its end. (Binding *SP* |
| 362 | ;; with LET would be simpler, but each binding takes room on SBCL's |
| 363 | ;; binding stack, which a deep recursion soon fills.) |
| 364 | `(defun ,sym (,@params ,@dests) |
| 365 | (let ((fp *sp*)) |
| 366 | (declare (ignorable fp)) |
| 367 | (enter fp ,(proc-size *proc*)) |
| 368 | (setf *sp* (+ fp ,(proc-size *proc*))) |
| 369 | ,@stores |
| 370 | (multiple-value-prog1 (block ,(proc-block *proc*) ,@body ,end) |
| 371 | (setf *sp* fp))))) |
| 372 | |
| 373 | (defun code-procedure (obj) |
| 374 | "A code procedure (section 8). This implementation's target is SBCL's |
| 375 | Lisp, so its instructions are Lisp forms, one to each `;`, which see the |
| 376 | parameters by their names; the value of the last is the result." |
| 377 | (let ((code (tok)) (sig (obj-sig obj))) |
| 378 | (expect :code) |
| 379 | (when (some #'aggregate-p (sig-results sig)) |
| 380 | (fail-tok code "a code procedure here returns no aggregate")) |
| 381 | (let* ((params (mapcar (lambda (p) (intern (string-upcase (first p)) :raz)) (sig-params sig))) |
| 382 | (forms (loop for (text line col) in (tok-val code) |
| 383 | collect (handler-case (let ((*package* (find-package :raz))) (read-from-string text)) |
| 384 | (error (e) (fail-at line col "the instruction isn't Lisp: ~a" e)))))) |
| 385 | (emit `(defun ,(obj-sym obj) ,params (declare (ignorable ,@params)) ,@forms)) |
| 386 | (setf (obj-defined obj) t (obj-code obj) t)) |
| 387 | (expect :end))) |
| 388 | |
| 389 | ;;; Calling C (section 9). |
| 390 | |
| 391 | (defun extern-declaration () |
| 392 | (setf (module-uses-c *module*) t) |
| 393 | (cond ((accept :proc) |
| 394 | (multiple-value-bind (name exported tk) (ident-def t) |
| 395 | (let ((sig (signature t))) |
| 396 | (dolist (p (sig-params sig)) |
| 397 | (unless (and (not (third p)) (member (ty-kind (second p)) '(:int :bool :real :ptr :proc))) |
| 398 | (fail-tok tk "~a's parameter ~a isn't of a type C takes" name (first p)))) |
| 399 | (when (or (rest (sig-results sig)) |
| 400 | (and (sig-results sig) |
| 401 | (not (member (ty-kind (first (sig-results sig))) '(:int :bool :real :ptr :proc))))) |
| 402 | (fail-tok tk "~a's result isn't of a type C gives" name)) |
| 403 | (let ((obj (declare-top (make-obj :name name :kind :proc :sig sig :tok tk :exported exported |
| 404 | :sym (lisp-name name) :extern name :defined t)))) |
| 405 | (unless (sig-varargs sig) (emit (extern-wrapper obj))))))) |
| 406 | ((accept :var) |
| 407 | (multiple-value-bind (name exported tk) (ident-def t) |
| 408 | (expect :|:|) |
| 409 | (declare-top (make-obj :name name :kind :var :type (parse-type) :tok tk :exported exported |
| 410 | :module *module* :where (list :extern name))))) |
| 411 | (t (fail-here "extern proc or extern var expected"))) |
| 412 | (expect :|;|)) |
| 413 | |
| 414 | ;;; A Lisp function calling a C procedure, for calls and for its value. |
| 415 | (defun extern-wrapper (obj) |
| 416 | (let* ((sig (obj-sig obj)) (result (first (sig-results sig))) |
| 417 | (ps (loop for p in (sig-params sig) collect (var-sym "P")))) |
| 418 | `(defun ,(obj-sym obj) ,ps |
| 419 | ,(c-result-form |
| 420 | result |
| 421 | `(sb-alien:alien-funcall |
| 422 | (sb-alien:extern-alien ,(obj-extern obj) |
| 423 | (function ,(if result (alien-type result) 'sb-alien:void) |
| 424 | ,@(mapcar (lambda (p) (alien-type (second p))) (sig-params sig)))) |
| 425 | ,@(loop for p in (sig-params sig) for s in ps collect (c-value-form (second p) s))))))) |
| 426 | |
| 427 | ;;; Modules (section 3). |
| 428 | |
| 429 | (defvar *search-dirs* '() "Where modules are looked for, in order.") |
| 430 | (defvar *interface-only* nil "Whether the module being read is another target's file, read for its interface.") |
| 431 | (defvar *target-names* '("linux-sbcl" "linux" "sbcl")) |
| 432 | |
| 433 | (defun find-module-file (name tk) |
| 434 | "The file of module NAME: in the first directory that has one, which |
| 435 | must be the only one, or the same file through a symbolic link." |
| 436 | (let ((found '())) |
| 437 | (dolist (dir *search-dirs*) |
| 438 | (let ((f (loop for suffix in (append (mapcar (lambda (s) (format nil ".~a" s)) *target-names*) '("")) |
| 439 | for path = (format nil "~a/~a~a.raz" dir name suffix) |
| 440 | when (probe-file path) return path))) |
| 441 | (when f (push f found)))) |
| 442 | (setf found (nreverse found)) |
| 443 | (unless found (fail-tok tk "the module ~a can't be found" name)) |
| 444 | (unless (every (lambda (f) (equal (truename f) (truename (first found)))) found) |
| 445 | (fail-tok tk "the module ~a has files in more than one directory: ~{~a~^, ~}" name found)) |
| 446 | (namestring (truename (first found))))) |
| 447 | |
| 448 | (defun load-module (name tk &optional file) |
| 449 | "Module NAME, read if it hasn't been; a generic module only as far as |
| 450 | its parameters." |
| 451 | (let ((m (gethash name *modules*))) |
| 452 | (cond ((null m) |
| 453 | (let* ((file (or file (find-module-file name tk))) |
| 454 | (toks (let ((*file* file)) (scan (read-bytes file))))) |
| 455 | (setf m (make-module :name name :file file :toks toks :lisp-name name)) |
| 456 | (setf (gethash name *modules*) m) |
| 457 | (if (and (eq (tok-kind (aref toks 2)) :|(|)) |
| 458 | (read-generic-header m) |
| 459 | (progn (read-module m) (check-other-files m))))) |
| 460 | ((eq (module-state m) :compiling) |
| 461 | (fail-tok tk "the module ~a imports itself" name))) |
| 462 | m)) |
| 463 | |
| 464 | (defmacro with-module ((m) &body body) |
| 465 | "BODY reading module M from its start." |
| 466 | `(let ((*module* ,m) (*toks* (module-toks ,m)) (*pos* 0) (*file* (module-file ,m)) |
| 467 | (*scope* (make-scope :parent *universe*)) (*proc* nil) (*top* t) (*forward-types* :none)) |
| 468 | (setf (module-scope ,m) *scope*) |
| 469 | ,@body)) |
| 470 | |
| 471 | (defun module-header () |
| 472 | "module Name ( and the name's check against the file's." |
| 473 | (expect :module) |
| 474 | (let ((tk (tok)) (name (ident))) |
| 475 | (unless (string= name (module-name *module*)) |
| 476 | (fail-tok tk "the module ~a is in the file of ~a" name (module-name *module*))))) |
| 477 | |
| 478 | (defun read-generic-header (m) |
| 479 | "A generic module's parameters, counted: the module is compiled only for |
| 480 | each list of arguments." |
| 481 | (with-module (m) |
| 482 | (module-header) |
| 483 | (expect :|(|) |
| 484 | (let ((types (loop collect (ident) while (accept :|,|))) (procs '())) |
| 485 | (loop while (accept :|;|) |
| 486 | do (push (ident) procs) (expect :|:|) (expect :proc) |
| 487 | ;; skip the signature: its types are known only in an instance |
| 488 | (let ((depth 0)) |
| 489 | (loop until (and (= depth 0) (member (kind) '(:|;| :|)|))) |
| 490 | do (case (kind) (:|(| (incf depth)) (:|)| (decf depth))) |
| 491 | (next-tok)))) |
| 492 | (setf (module-generic m) (cons types (nreverse procs)) (module-state m) :done)))) |
| 493 | |
| 494 | (defun read-module (m &optional args) |
| 495 | "Reads and compiles module M; for an instance, ARGS are its arguments." |
| 496 | (setf (module-state m) :compiling) |
| 497 | (with-module (m) |
| 498 | (module-header) |
| 499 | (when (accept :|(|) (bind-module-params args)) |
| 500 | (expect :|;|) |
| 501 | (loop while (accept :import) do (import-list)) |
| 502 | (loop while (and (eq (kind) :link)) |
| 503 | do (next-tok) |
| 504 | (cond ((eq (kind) :string) |
| 505 | (let ((lib (sb-ext:octets-to-string (tok-val (next-tok)) :external-format :utf-8))) |
| 506 | (handler-case (sb-alien:load-shared-object lib) |
| 507 | (error () (fail-tok (tok -1) "the library ~a can't be loaded" lib))) |
| 508 | (push lib (module-links m)) |
| 509 | (setf (module-uses-c m) t))) |
| 510 | ((and (eq (kind) :ident) (string= (tok-val (tok)) "static")) |
| 511 | (next-tok) (setf (module-static m) t)) |
| 512 | (t (fail-here "link takes a library's name or static"))) |
| 513 | (expect :|;|)) |
| 514 | (when (and (eq (kind) :ident) (string= (tok-val (tok)) "check")) |
| 515 | (next-tok) |
| 516 | (unless (and (eq (kind) :ident) (string= (tok-val (tok)) "off")) (fail-here "check off expected")) |
| 517 | (next-tok) (expect :|;|) |
| 518 | (setf (module-check-off m) t)) |
| 519 | (declarations) |
| 520 | (when *interface-only* (return-from read-module m)) |
| 521 | (when (eq (kind) :begin) |
| 522 | (next-tok) |
| 523 | (let* ((sym (lisp-name "(body)")) |
| 524 | (*proc* (make-proc :block (gensym "BODY"))) |
| 525 | (*scope* (make-scope :parent *scope*)) |
| 526 | (body (statements :end))) |
| 527 | (emit (procedure-defun sym '() '() '() body nil)) |
| 528 | (setf (module-body m) sym))) |
| 529 | (expect :end) |
| 530 | (let ((tk (tok))) |
| 531 | (unless (equal (ident) (module-name m)) (fail-tok tk "the module ends with its own name, ~a" (module-name m)))) |
| 532 | (expect :|.|) |
| 533 | (unless (eq (kind) :eof) (fail-here "nothing follows the module's end")) |
| 534 | (maphash (lambda (name o) |
| 535 | (when (and (eq (obj-kind o) :proc) (not (obj-defined o))) |
| 536 | (fail-tok (obj-tok o) "~a is declared forward and never defined" name))) |
| 537 | (scope-table *scope*))) |
| 538 | (setf (module-state m) :done) |
| 539 | m) |
| 540 | |
| 541 | (defun import-list () |
| 542 | (loop |
| 543 | (let* ((tk (tok)) (alias (ident)) (name alias) (args nil)) |
| 544 | (when (accept :|:=|) |
| 545 | (setf name (ident)) |
| 546 | (when (eq (kind) :|(|) |
| 547 | ;; the arguments are read where the instance is made |
| 548 | (setf args *pos*) |
| 549 | (let ((depth 0)) |
| 550 | (loop do (case (kind) (:|(| (incf depth)) (:|)| (decf depth)) (:eof (fail-here "the import isn't closed"))) |
| 551 | (next-tok) |
| 552 | until (= depth 0))))) |
| 553 | (when (string= name (module-name *module*)) (fail-tok tk "the module ~a imports itself" name)) |
| 554 | (let* ((target (load-module name tk)) |
| 555 | (imp (make-import :alias alias :target target :args args :tok tk))) |
| 556 | (cond ((and args (not (module-generic target))) |
| 557 | (fail-tok tk "~a isn't generic and takes no arguments" name)) |
| 558 | ((and (module-generic target) (not args)) |
| 559 | (fail-tok tk "~a is generic: give its arguments" name))) |
| 560 | (push imp (module-imports *module*)) |
| 561 | (declare-name (make-obj :name alias :kind :module :val imp :tok tk)))) |
| 562 | (unless (accept :|,|) (return))) |
| 563 | (expect :|;|)) |
| 564 | |
| 565 | (defun import-module (imp tk) |
| 566 | "The module an import stands for: for a generic module, the instance |
| 567 | for its arguments, made now if it hasn't been (section 3)." |
| 568 | (cond ((import-instance imp)) |
| 569 | ((not (module-generic (import-target imp))) (import-target imp)) |
| 570 | (t (setf (import-instance imp) (make-instance-module imp tk))))) |
| 571 | |
| 572 | (defun read-type-args () |
| 573 | "A generic module's arguments, after its (: each a type, or (:proc obj)." |
| 574 | (loop collect (let ((start *pos*)) |
| 575 | (if (eq (kind) :ident) |
| 576 | (let ((obj (qualified-obj))) |
| 577 | (if (eq (obj-kind obj) :proc) |
| 578 | (list :proc obj) |
| 579 | (progn (setf *pos* start) (parse-type)))) |
| 580 | (parse-type))) |
| 581 | while (accept :|,|))) |
| 582 | |
| 583 | (defun same-args-p (a b) |
| 584 | (and (= (length a) (length b)) |
| 585 | (every (lambda (x y) |
| 586 | (if (consp x) |
| 587 | (and (consp y) (eq (second x) (second y))) |
| 588 | (and (ty-p y) (same-type-p x y)))) |
| 589 | a b))) |
| 590 | |
| 591 | (defun make-instance-module (imp tk) |
| 592 | (let* ((g (import-target imp)) |
| 593 | (args (let ((saved *pos*)) |
| 594 | (setf *pos* (1+ (import-args imp))) |
| 595 | (prog1 (read-type-args) (setf *pos* saved)))) |
| 596 | (ntypes (length (car (module-generic g)))) |
| 597 | (nprocs (length (cdr (module-generic g))))) |
| 598 | (unless (and (= (length args) (+ ntypes nprocs)) |
| 599 | (notany #'consp (subseq args 0 (min ntypes (length args)))) |
| 600 | (every #'consp (nthcdr ntypes args))) |
| 601 | (fail-tok tk "~a takes ~d types and ~d procedures" (module-name g) ntypes nprocs)) |
| 602 | (let ((old (assoc args (module-instances g) :test #'same-args-p))) |
| 603 | (if old |
| 604 | (cdr old) |
| 605 | (let ((m (make-module :name (module-name g) :file (module-file g) :toks (module-toks g) |
| 606 | :lisp-name (format nil "~a(~d)" (module-name g) (1+ (length (module-instances g)))) |
| 607 | :args args))) |
| 608 | (push (cons args m) (module-instances g)) |
| 609 | (read-module m args)))))) |
| 610 | |
| 611 | (defun bind-module-params (args) |
| 612 | "An instance's parameters: each type parameter names its argument, and |
| 613 | each procedure parameter its procedure, whose signature must be the |
| 614 | parameter's with the types put in." |
| 615 | (let ((types (loop collect (cons (tok) (ident)) while (accept :|,|)))) |
| 616 | (loop for (tk . name) in types for a in args |
| 617 | do (declare-name (make-obj :name name :kind :type :type a :tok tk))) |
| 618 | (let ((procs (nthcdr (length types) args))) |
| 619 | (loop while (accept :|;|) |
| 620 | do (let ((tk (tok)) (name (ident))) |
| 621 | (expect :|:|) (expect :proc) |
| 622 | (let ((sig (signature nil)) (actual (second (pop procs)))) |
| 623 | (unless (sig-alike-p sig (obj-sig actual)) |
| 624 | (fail-tok tk "~a's signature isn't the parameter ~a's" (obj-name actual) name)) |
| 625 | (setf (gethash name (scope-table *scope*)) actual))))) |
| 626 | (expect :|)|))) |
| 627 | |
| 628 | ;;; One interface to a module's files (section 3): each file of the |
| 629 | ;;; module beside the one taken, Name.*.raz and Name.raz, is read for its |
| 630 | ;;; declarations alone, and must export what the one taken does. |
| 631 | |
| 632 | (defun other-files (m) |
| 633 | (let* ((file (module-file m)) (dir (directory-namestring file))) |
| 634 | (remove-if (lambda (f) (equal (namestring f) file)) |
| 635 | (append (directory (format nil "~a~a.*.raz" dir (module-name m))) |
| 636 | (directory (format nil "~a~a.raz" dir (module-name m))))))) |
| 637 | |
| 638 | (defun check-other-files (m) |
| 639 | (loop for f in (other-files m) for i from 1 |
| 640 | do (let* ((file (namestring f)) |
| 641 | (other (make-module :name (module-name m) :file file |
| 642 | :toks (let ((*file* file)) (scan (read-bytes file))) |
| 643 | :lisp-name (format nil "~a{~d}" (module-name m) i)))) |
| 644 | (let ((*interface-only* t)) (read-module other)) |
| 645 | (compare-interfaces m other)))) |
| 646 | |
| 647 | (defun iface-type-p (a b) |
| 648 | "Whether types A and B, of two files of one module, are built alike, a |
| 649 | name in one standing for the same name in the other." |
| 650 | (cond ((eq a b) t) |
| 651 | ((or (ty-name a) (ty-name b)) |
| 652 | (and (ty-name a) (ty-name b) (string= (ty-name a) (ty-name b)) |
| 653 | (eq (ty-kind a) (ty-kind b)))) |
| 654 | ((not (eq (ty-kind a) (ty-kind b))) nil) |
| 655 | (t (case (ty-kind a) |
| 656 | (:ptr (and (eq (ty-vol a) (ty-vol b)) (iface-type-p (ptr-base a) (ptr-base b)))) |
| 657 | (:array (and (= (ty-len a) (ty-len b)) (iface-type-p (ty-elem a) (ty-elem b)))) |
| 658 | (:slice (iface-type-p (ty-elem a) (ty-elem b))) |
| 659 | (:set (iface-type-p (ty-enum a) (ty-enum b))) |
| 660 | (:proc (iface-sig-p (ty-sig a) (ty-sig b))) |
| 661 | (t (iface-shape-p a b)))))) |
| 662 | |
| 663 | (defun iface-fields-p (as bs) |
| 664 | "The exported fields of two records, or the fields of two cases, alike." |
| 665 | (and (= (length as) (length bs)) |
| 666 | (every (lambda (f g) (and (string= (field-name f) (field-name g)) |
| 667 | (iface-type-p (field-type f) (field-type g)))) |
| 668 | as bs))) |
| 669 | |
| 670 | (defun iface-shape-p (a b) |
| 671 | "What a record, variant or enumeration shows another module, alike: |
| 672 | its exported fields, its cases, its names." |
| 673 | (ecase (ty-kind a) |
| 674 | (:record (iface-fields-p (remove-if-not #'field-exported (ty-fields a)) |
| 675 | (remove-if-not #'field-exported (ty-fields b)))) |
| 676 | (:variant (and (= (length (ty-cases a)) (length (ty-cases b))) |
| 677 | (every (lambda (c d) (and (string= (vcase-name c) (vcase-name d)) |
| 678 | (iface-fields-p (vcase-fields c) (vcase-fields d)))) |
| 679 | (ty-cases a) (ty-cases b)))) |
| 680 | (:enum (equal (ty-names a) (ty-names b))) |
| 681 | ((:int :bool :real :error) t))) |
| 682 | |
| 683 | (defun iface-sig-p (a b) |
| 684 | (and (= (length (sig-params a)) (length (sig-params b))) |
| 685 | (= (length (sig-results a)) (length (sig-results b))) |
| 686 | (eq (sig-varargs a) (sig-varargs b)) |
| 687 | (every (lambda (p q) (and (eq (third p) (third q)) (iface-type-p (second p) (second q)))) |
| 688 | (sig-params a) (sig-params b)) |
| 689 | (every #'iface-type-p (sig-results a) (sig-results b)))) |
| 690 | |
| 691 | (defun compare-interfaces (m other) |
| 692 | (flet ((differ (name file) |
| 693 | (error 'raz-error :text (format nil "~a: ~a isn't exported as in ~a" file name (module-file m))))) |
| 694 | (maphash (lambda (name o) |
| 695 | (let ((p (gethash name (module-exports other)))) |
| 696 | (unless (and p (eq (obj-kind o) (obj-kind p)) |
| 697 | (ecase (obj-kind o) |
| 698 | ((:const :var :case) (iface-type-p (obj-type o) (obj-type p))) |
| 699 | (:type (and (eq (ty-kind (obj-type o)) (ty-kind (obj-type p))) |
| 700 | (if (member (ty-kind (obj-type o)) '(:record :variant :enum)) |
| 701 | (iface-shape-p (obj-type o) (obj-type p)) |
| 702 | (iface-type-p (obj-type o) (obj-type p))))) |
| 703 | (:proc (iface-sig-p (obj-sig o) (obj-sig p))))) |
| 704 | (differ name (module-file other))))) |
| 705 | (module-exports m)) |
| 706 | (maphash (lambda (name p) (declare (ignore p)) |
| 707 | (unless (gethash name (module-exports m)) (differ name (module-file other)))) |
| 708 | (module-exports other)))) |