c64-livecoding/wip-duuqnd/user-side-compiler/middle/data.lisp
John Lorentzson 9e643e6c6d Add compiler middle stage
The compiler middle stage takes high level nodes and produces code in
an intermediate representation more closely resembling assembly code.

Optimizations and the tools for making those are also included. It's
significantly easier to optimize IR than syntax trees or assembly.

Several things need cleaning up, in particular there are things in
jigs.lisp that really should be documented tools, not
jigs (specifically the compilation setup and finalization).
2025-06-26 13:41:43 +02:00

57 lines
1.5 KiB
Common Lisp

(in-package #:user-side-compiler)
(defclass ir-data ()
((%definition :accessor definition :initarg :definition)))
(defclass ir-result (ir-data)
((%user :accessor user :initarg :user :initform nil)))
(defmethod users ((data ir-result))
(if (user data)
(list (user data))
nil))
(defmethod add-user ((data ir-result) user)
(assert (null (user data)))
(setf (user data) user))
(defmethod remove-user ((data ir-result) user)
(assert (eql user (user data)))
(setf (user data) nil))
(defclass ir-reusable (ir-data)
((%users :accessor users :initarg :users :initform '())))
(defmethod add-user ((data ir-reusable) user)
(pushnew user (users data)))
(defmethod remove-user ((data ir-reusable) user)
(setf (users data) (remove user (users data))))
(defclass ir-variable (ir-reusable)
((%name :accessor name :initarg :name)
(%writers :accessor writers :initform '())))
(tlk:define-simple-print-object (ir-variable %name))
(defclass ir-constant (ir-reusable)
((%definition :accessor definition :initarg :definition)
(%value :accessor value :initarg :value)))
(defmethod add-user ((data number) user))
(defmethod remove-user ((data number) user))
(tlk:define-simple-print-object (ir-constant %value))
(defmethod ir-constant-p ((obj ir-data))
(typep (definition obj) 'ir-getconst))
(defmethod ir-constant-p ((obj ir-constant))
t)
(defmethod ir-constant-value ((obj ir-result))
(assert (ir-constant-p obj))
(first (inputs (definition obj))))
(defmethod ir-constant-value ((obj ir-constant))
(value obj))