spirv: emit.lisp

building SPIR-V modules at run time: sections, ids, instruction words.

Other definitions

(defmacro define-spv-module) source
(defmacro define-spv-module ()
  `(defstruct (spv-module (:conc-name spvm-))
     ,@(mapcan (lambda (s)
                 (let ((name (intern (symbol-name (first s)))) (cap (second s)))
                   `((,name (make-array ,cap :element-type '(unsigned-byte 32) :initial-element 0)
                            :type (simple-array (unsigned-byte 32) (,cap)))
                     (,(intern (concatenate 'string (symbol-name (first s)) "-N")) 0
                      :type (unsigned-byte 32)))))
               *spv-sections*)
     (bound 1 :type (unsigned-byte 32))
     ;; set when a section was full: the module is then incomplete
     (overflow 0 :type (unsigned-byte 1))))
(defmacro spv-words-capacity) source
(defmacro spv-words-capacity () (spv-total-words))
(defun make-spv-words) source
(defun make-spv-words ()
  "A buffer for SPV-FINISH"
  (make-array (spv-words-capacity) :element-type '(unsigned-byte 32) :initial-element 0))
(defun spv-id) source
(defun spv-id (m)
  "A new result id"
  (declare (type spv-module m))
  (let ((id (spvm-bound m)))
    (setf (spvm-bound m) (1+ id))
    id))
(defmacro spv-put) source
(defmacro spv-put (m sec word)
  (let ((mm (gensym "M")) (i (gensym "I"))
        (arr (spv-sec-sym sec "")) (n (spv-sec-sym sec "-N")))
    `(let* ((,mm ,m) (,i (,n ,mm)))
       (if (< ,i ,(spv-sec-cap sec))
           (progn (setf (aref (,arr ,mm) ,i) (ldb (byte 32 0) ,word))
                  (setf (,n ,mm) (1+ ,i)))
           (setf (spvm-overflow ,mm) 1))
       nil)))
(defmacro spv-op-in) source
(defmacro spv-op-in (m sec op &rest args)
  (let* ((inst (spv-find-inst op))
         (mm (gensym "M")) (id (gensym "ID"))
         (newp (member :new args))
         (temps nil)
         (args (mapcar (lambda (a)
                         (flet ((hoist (form)
                                  (let ((g (gensym "OPND"))) (push (list g form) temps) g)))
                           (cond ((eq a :new) id)
                                 ((or (atom a) (spv-quoted-p a)) a)
                                 ((eq (car a) :u64)
                                  (if (atom (second a)) a (list :u64 (hoist (second a)))))
                                 (t (hoist a)))))
                       args)))
    (multiple-value-bind (words result) (spv-encode-args inst args)
      `(let* ((,mm ,m)
              ,@(reverse temps)
              ,@(and newp `((,id (spv-id ,mm)))))
         (spv-put ,mm ,sec ,(logior (ash (1+ (length words)) 16) (spvi-opcode inst)))
         ,@(mapcar (lambda (w) `(spv-put ,mm ,sec ,w)) words)
         ,(or result 0)))))
(defmacro spv-op) source
(defmacro spv-op (m op &rest args)
  `(spv-op-in ,m ,(spv-section-of (spv-find-inst op)) ,op ,@args))
(defmacro spv-open) source
(defmacro spv-open (m sec op)
  (let ((mm (gensym "M")) (at (gensym "AT")))
    `(let* ((,mm ,m) (,at (,(spv-sec-sym sec "-N") ,mm)))
       (spv-put ,mm ,sec ,(spvi-opcode (spv-find-inst op)))
       ,at)))
(defmacro spv-close) source
(defmacro spv-close (m sec at)
  (let ((mm (gensym "M")) (a (gensym "A")) (arr (spv-sec-sym sec "")) (n (spv-sec-sym sec "-N")))
    `(let* ((,mm ,m) (,a ,at))
       (when (< ,a ,(spv-sec-cap sec))
         (setf (aref (,arr ,mm) ,a)
               (logior (ash (- (,n ,mm) ,a) 16) (ldb (byte 16 0) (aref (,arr ,mm) ,a)))))
       nil)))
(defmacro define-spv-finish) source
(defmacro define-spv-finish ()
  `(defun spv-finish (m out version)
     (declare (type spv-module m)
              (type (simple-array (unsigned-byte 32) (,(spv-total-words))) out)
              (type (unsigned-byte 32) version))
     (setf (aref out 0) ,(getf *spv-grammar* :magic)
           (aref out 1) version
           (aref out 2) 0             ; generator: none registered
           (aref out 3) (spvm-bound m)
           (aref out 4) 0)            ; schema
     (let ((n 5))
       (declare (type (unsigned-byte 32) n))
       ,@(mapcar (lambda (s)
                   (let ((arr (spv-sec-sym (first s) "")) (cnt (spv-sec-sym (first s) "-N")))
                     `(dotimes (i (,cnt m))
                        (setf (aref out n) (aref (,arr m) i))
                        (setq n (1+ n)))))
                 *spv-sections*)
       (if (= (spvm-overflow m) 1) 0 n))))

Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.