c64-livecoding/wip-duuqnd/user-side-compiler/middle/structure.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

38 lines
1.4 KiB
Common Lisp

(in-package #:user-side-compiler)
(defclass ir-program ()
((%start :accessor start :initarg :start)
(%variables :accessor variables :initform (make-hash-table))))
(defclass iblock ()
((%program :accessor program :initarg :program)
(%start :accessor start :initarg :start :type ir-inst)
(%end :accessor end :initarg :end :type ir-terminator)
(%next :accessor next :type (or null iblock) :initform nil)
(%prev :accessor prev :type (or null iblock) :initform nil)
(%name :accessor name :initarg :name :initform nil)))
(defmethod find-variable (reference (search-from ir-program))
(multiple-value-bind (var existsp)
(gethash reference (variables search-from))
(if existsp
var
(setf (gethash reference (variables search-from))
(make-instance 'ir-variable :name (name reference))))))
(defmethod find-variable (reference (search-from iblock))
(find-variable reference (program search-from)))
(defmethod find-variable (reference (search-from ir-inst))
(find-variable reference (iblock search-from)))
(defmethod find-variable (reference (search-from builder))
(find-variable reference (iblock search-from)))
(defmethod print-object ((object iblock) stream)
(print-unreadable-object (object stream :type t :identity t)
(unless (null (name object))
(format stream "~A" (name object)))))
(defun successors (iblock)
(destinations (end iblock)))