rsx: defrsx.lisp

the RSX as an ISA: DEFMETHOD-RSX, the method table, RSX enumerations.

Other definitions

(defun rsx-sem-body) source
(defun rsx-sem-body (name)
  "the semantics of method NAME as DEFMETHOD-RSX recorded it: (params . body)"
  (let ((m (rsx-find-method name)))
    (cons (rmi-params m) (rmi-body m))))
(defun rsx-writes) source
(defun rsx-writes (name)
  "the (group field) pairs of the abstract state that method NAME assigns"
  (rmi-writes (rsx-find-method name)))
(defmacro reg) source
(defmacro reg (name &optional (i 0))
  "the register of method NAME (element I) of the machine RSX"
  (let ((m (rsx-find-method name)))
    `(aref (rsx-regs rsx) ,(if (integerp i)
                               (progn (unless (< -1 i (rmi-count m)) (error "~A has no element ~D" name i))
                                      (rsx-word-index m i))
                               `(+ ,(ash (rmi-base m) -2) (* ,i ,(ash (rmi-stride m) -2)))))))
(defmacro defmethod-rsx) source
(defmacro defmethod-rsx (name address params &body body)
  (destructuring-bind (base count stride) (rsx-address-spec address)
    (let* ((doc (and (stringp (car body)) (cdr body) (pop body)))
           (fn (rsx-fn-name name))
           (value (first params))
           (index (or (second params) (intern "INDEX" :isa.rsx))))
      (when (and (> count 1) (null (second params)))
        (error "~A is arrayed: name its index" name))
      `(progn
         (eval-when (:compile-toplevel :load-toplevel :execute)
           (rsx-register-method ',name ,base ,count ,stride ',params ',body ,doc ',fn))
         (defun ,fn (rsx ,value ,index)
           ,@(and doc (list doc))
           (declare (type rsx rsx) (type u32 ,value) (type u16 ,index) (ignorable rsx ,value ,index))
           (let ((gfx (rsx-gfx rsx)))
             (declare (ignorable gfx))
             ,@body)
           nil)
         ',name))))
(defmacro defmethods-rsx) source
(defmacro defmethods-rsx (methods params &body body)
  "one body for several methods: METHODS is ((name address) ...)"
  `(progn ,@(mapcar (lambda (m) `(defmethod-rsx ,(first m) ,(second m) ,params ,@body)) methods)))
(defmacro define-rsx-registers) source
(defmacro define-rsx-registers (&rest methods)
  "methods that only store their value: (name address [doc]) ..., address as for DEFMETHOD-RSX"
  `(eval-when (:compile-toplevel :load-toplevel :execute)
     ,@(mapcar (lambda (m)
                 (destructuring-bind (base count stride) (rsx-address-spec (second m))
                   `(rsx-register-method ',(first m) ,base ,count ,stride nil nil ,(third m) nil)))
               methods)))
(defmacro defrsx-derive) source
(defmacro defrsx-derive (name params &body body)
  "a helper that derives abstract state from registers, shared by methods: (NAME rsx args...).
   PARAMS are ((arg type) ...). Its writes count as the writes of every method that calls it."
  (let ((doc (and (stringp (car body)) (cdr body) (pop body))))
    `(progn
       (eval-when (:compile-toplevel :load-toplevel :execute)
         (setq *rsx-helpers* (cons (cons ',name (rsx-collect-writes ',body))
                                   (remove ',name *rsx-helpers* :key #'car))))
       (defun ,name (rsx ,@(mapcar #'first params))
         ,@(and doc (list doc))
         (declare (type rsx rsx) ,@(mapcar (lambda (p) `(type ,(second p) ,(first p))) params))
         (let ((gfx (rsx-gfx rsx)))
           (declare (ignorable gfx))
           ,@body)
         nil))))
(defmacro define-rsx-enum) source
(defmacro define-rsx-enum (kind &rest pairs)
  (let ((map nil))
    (dolist (p pairs)
      (let ((code (gfx-enum-code kind (first p))))
        (dolist (v (rest p)) (push (cons v code) map))))
    `(eval-when (:compile-toplevel :load-toplevel :execute)
       (setq *rsx-enum-maps* (cons '(,kind ,@(nreverse map)) (remove ',kind *rsx-enum-maps* :key #'car)))
       ',kind)))
(defmacro rsx->gfx) source
(defmacro rsx->gfx (kind x)
  "the canonical code of the RSX value X of enumeration KIND (0 for a value the RSX does not define)"
  (let ((m (or (cdr (assoc (symbol-name kind) *rsx-enum-maps* :key #'symbol-name :test #'string=))
               (error "no RSX map for ~S" kind))))
    `(case ,x ,@(mapcar (lambda (p) `(,(car p) ,(cdr p))) m) (otherwise 0))))
(defparameter *rsx-memory-fields*) source
(defparameter *rsx-memory-fields*
  '(((vertex-buffer address) (vertex-buffer buffer) (vertex-buffer offset))
    ((index address) (index buffer) (index offset))
    ((attachment address) (attachment view))
    ((attachment pitch) (attachment view))
    ((depth-attachment address) (depth-attachment view) (depth-attachment stencil-view))
    ((depth-attachment pitch) (depth-attachment view) (depth-attachment stencil-view))))
(defun rsx-host-fields) source
(defun rsx-host-fields (fields)
  "FIELDS ((group field) ...) with each guest-memory field replaced by the host fields it resolves to"
  (let ((out nil))
    (dolist (f fields (nreverse out))
      (let ((m (assoc (mapcar #'symbol-name f) *rsx-memory-fields*
                      :key (lambda (k) (mapcar #'symbol-name k)) :test #'equal)))
        (dolist (h (if m (cdr m) (list f)))
          (pushnew h out :test (lambda (a b) (equal (mapcar #'symbol-name a) (mapcar #'symbol-name b)))))))))

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