;;;; Random Common Lisp code useful to use here and there.
(defmacro with-gensyms ((&rest vars) &body body)
- `(let ,(mapcar (lambda (var) `(,var (gensym ,(string var)))) vars)
+ `(let ,(mapcar (lambda (var) `(,var (gensym ,(concatenate 'string (string var) "-")))) vars)
,@body))
(defun singlep (x)
(assert (singlep x))
(first x))
+(defun generic-printer (x stream)
+ (print-unreadable-object (x stream :type t :identity t)))
+
+;;; A generic counter mechanism. IDs are used generally for debugging
+;;; purposes. You can bind *counter-alist* to NIL to reset the
+;;; counters in a dynamic extent.
+(defvar *counter-alist* nil)
+(defun generate-id (class)
+ (let ((e (assoc class *counter-alist*)))
+ (if e
+ (incf (cdr e))
+ (prog1 1
+ (push (cons class 1) *counter-alist*)))))
+
;;;; Intermediate representation structures
;;;;
;;; A lambda expression. Why do we name it `functional'? Well,
;;; function is reserved by the ANSI, isn't it?
-(defstruct (functional (:include leaf))
+(defstruct (functional (:include leaf) (:print-object generic-printer))
;; The symbol which names this function in the source code or null
;; if we do not know or it is an anonymous function.
name
arguments
return-lvar
- entry-point)
+ component)
;;; An abstract place where the result of a computation is stored and
;;; it can be referenced from other nodes, so lvars are responsible
;;; for keeping the necessary information of the nested structure of
;;; the code in this plain representation.
(defstruct lvar
- (id (gensym "$")))
+ (id (generate-id 'lvar)))
;;; A base structure for every single computation. Most of the
;;; computations are valued.
-(defstruct node
+(defstruct (node (:print-object generic-printer))
;; The next and the prev slots are the next nodes and the previous
;; node in the basic block sequence respectively.
next prev
variable
value)
-;;; Call the lvar FUNCTION with a list of lvars as ARGUMENTS.
-(defstruct (call (:include node))
- function
+;;; A base node to function calls with a list of lvar as ARGUMENTS.
+(defstruct (combination (:include node) (:constructor))
arguments)
+;;; A function call to the ordinary Lisp function in the lvar FUNCTION.
+(defstruct (call (:include combination))
+ function)
+
+;;; A function call to the primitive FUNCTION.
+(defstruct (primitive-call (:include combination))
+ function)
+
+
;;; A conditional branch. If the LVAR is not NIL, then we will jump to
;;; the basic block CONSEQUENT, jumping to ALTERNATIVE otherwise. By
;;; definition, a conditional must appear at the end of a basic block.
alternative)
+
;;; Blocks are `basic block`. Basic blocks are organized as a control
;;; flow graph with some more information in omponents.
(defstruct (basic-block
(:conc-name "BLOCK-")
(:constructor make-block)
- (:predicate block-p))
- (id (gensym "L"))
- ;; List of successors and predecessors of this basic block.
+ (:predicate block-p)
+ (:print-object generic-printer))
+ (id (generate-id 'basic-block))
+ ;; List of successors and predecessors of this basic block. They are
+ ;; null only for deleted blocks and component's entry and exit.
succ pred
;; The sentinel nodes of the sequence.
- entry exit)
+ entry exit
+ ;; The component where the basic block belongs to.
+ component
+ ;; The order in the reverse post ordering of the blocks.
+ order
+ ;; A bit-vector representing the set of dominators. See the function
+ ;; `compute-dominators' to know how to use it properly.
+ dominators%
+ ;; Arbitrary data which could be necessary to keep during IR
+ ;; processing.
+ data)
;;; Sentinel nodes in the control flow graph of basic blocks.
(defstruct (component-entry (:include basic-block)))
(defstruct (component-exit (:include basic-block)))
-;;; Return a fresh empty basic block.
-(defun make-empty-block ()
- (let ((entry (make-block-entry))
- (exit (make-block-exit)))
- (setf (node-next entry) exit
- (node-prev exit) entry)
- (make-block :entry entry :exit exit)))
-
;;; Return T if B is an empty basic block and NIL otherwise.
(defun empty-block-p (b)
(block-exit-p (node-next (block-entry b))))
+(defun boundary-block-p (block)
+ (or (component-entry-p block)
+ (component-exit-p block)))
+
;;; Iterate across the nodes in a basic block forward.
(defmacro do-nodes
((node block &optional result &key include-sentinel-p) &body body)
(values))
+;;; Components are connected pieces of the control flow graph of
+;;; basic blocks with some additional information. Components have
+;;; well-defined entry and exit nodes. It is the toplevel
+;;; organizational entity in the compiler. The IR translation result
+;;; is accumulated into components incrementally.
+(defstruct (component (:print-object generic-printer))
+ (id (generate-id 'component))
+ name
+ entry
+ exit
+ functions
+ ;; TODO: Replace with a flags slot for indicate what
+ ;; analysis/transformations have been carried out.
+ reverse-post-order-p
+ blocks)
+
+;;; The current component.
+(defvar *component*)
+
+;;; Create a new fresh empty basic block in the current component.
+(defun make-empty-block ()
+ (let ((entry (make-block-entry))
+ (exit (make-block-exit)))
+ (link-nodes entry exit)
+ (let ((block (make-block :entry entry :exit exit :component *component*)))
+ (push block (component-blocks *component*))
+ block)))
+
+;;; Create a new component with an empty basic block, ready to start
+;;; conversion to IR. It returns the component and the basic block as
+;;; multiple values.
+(defun make-empty-component (&optional name)
+ (let ((*component* (make-component :name name)))
+ (let ((entry (make-component-entry :component *component*))
+ (exit (make-component-exit :component *component*))
+ (block (make-empty-block)))
+ (setf (block-succ entry) (list block)
+ (block-pred exit) (list block)
+ (block-succ block) (list exit)
+ (block-pred block) (list entry)
+ (component-entry *component*) entry
+ (component-exit *component*) exit)
+ (values *component* block))))
+
+;;; A few consistency checks in the IR useful for catching bugs.
+(defun check-ir-consistency (&optional (component *component*))
+ (with-simple-restart (continue "Continue execution")
+ (dolist (block (component-blocks component))
+ (dolist (succ (block-succ block))
+ (unless (find block (block-pred succ))
+ (error "The block `~S' does not belong to the predecessors list of the its successor `~S'"
+ block succ))
+ (unless (or (boundary-block-p succ) (find succ (component-blocks component)))
+ (error "Block `~S' is reachable from its predecessor `~S' but it is not in the component `~S'"
+ succ block component)))
+ (dolist (pred (block-pred block))
+ (unless (find block (block-succ pred))
+ (error "The block `~S' does not belong to the successors' list of its predecessor `~S'"
+ block pred))
+ (unless (or (boundary-block-p pred) (find pred (component-blocks component)))
+ (error "Block `~S' is reachable from its sucessor `~S' but it is not in the component `~S'"
+ pred block component))))))
+
+;;; Prepare a new component with a current empty block ready to start
+;;; IR conversion bound in the current cursor. BODY is evaluated and
+;;; the value of the last form is returned.
+(defmacro with-component-compilation ((&optional name) &body body)
+ (with-gensyms (block)
+ `(multiple-value-bind (*component* ,block)
+ (make-empty-component ,name)
+ (let ((*cursor* (cursor :block ,block)))
+ ,@body))))
+
+;;; Call function for each reachable block in component in
+;;; post-order. The consequences are unspecified if a block is
+;;; FUNCTION modifies a block which has not been processed yet.
+(defun map-postorder-blocks (function component)
+ (let ((seen nil))
+ (labels ((compute-from (block)
+ (unless (or (component-exit-p block) (find block seen))
+ (push block seen)
+ (dolist (successor (block-succ block))
+ (unless (component-exit-p block)
+ (compute-from successor)))
+ (funcall function block))))
+ (compute-from (unlist (block-succ (component-entry component))))
+ nil)))
+
+;;; Change all the predecessors of BLOCK to precede NEW-BLOCK
+;;; instead. As consequence, BLOCK becomes unreachable.
+(defun replace-block (block new-block)
+ (let ((predecessors (block-pred block)))
+ (setf (block-pred block) nil)
+ (dolist (pred predecessors)
+ (pushnew pred (block-pred new-block))
+ (setf (block-succ pred) (substitute new-block block (block-succ pred)))
+ (unless (component-entry-p pred)
+ (let ((last-node (node-prev (block-exit pred))))
+ (when (conditional-p last-node)
+ (macrolet ((replacef (place)
+ `(setf ,place (if (eq block ,place) new-block ,place))))
+ (replacef (conditional-consequent last-node))
+ (replacef (conditional-alternative last-node)))))))))
+
+(defun delete-block (block)
+ (when (boundary-block-p block)
+ (error "Cannot delete entry or exit basic blocks."))
+ (unless (singlep (block-succ block))
+ (error "Cannot delete a basic block with multiple successors."))
+ (let ((successor (unlist (block-succ block))))
+ (replace-block block successor)
+ ;; At this point, block is unreachable, however we could have
+ ;; backreferences to it from its successors. Let's get rid of
+ ;; them.
+ (setf (block-pred successor) (remove block (block-pred successor)))
+ (setf (block-succ block) nil)))
+
;;;; Cursors
;;;;
(defun cursor (&key (block (current-block))
(before nil before-p)
(after nil after-p))
- (when (or (component-entry-p block) (component-exit-p block))
+ (when (boundary-block-p block)
(error "Invalid cursor on special entry/exit basic block."))
;; Handle special values :ENTRY and :EXIT.
(flet ((node-designator (x)
;;; Insert NODE at cursor.
(defun insert-node (node &optional (cursor *cursor*))
- ;; After if? wrong!
(link-nodes (node-prev (cursor-next cursor)) node)
(link-nodes node (cursor-next cursor))
t)
(newblock (make-block :entry newentry
:exit exit
:pred (list block)
- :succ (block-succ block))))
+ :succ (block-succ block)
+ :component *component*)))
(insert-node newexit)
(insert-node newentry)
(setf (node-next newexit) nil)
(dolist (succ (block-succ newblock))
(setf (block-pred succ) (substitute newblock block (block-pred succ))))
(set-cursor :block block :before newexit)
+ (push newblock (component-blocks *component*))
newblock))
;;; Split the block at CURSOR if it is in the middle of it. The cursor
(split-block cursor)))
-;;;; Components
-;;;;
-;;;; Components are connected pieces of the control flow graph of
-;;;; basic blocks with some additional information. Components have
-;;;; well-defined entry and exit nodes. It is the toplevel
-;;;; organizational entity in the compiler. The IR translation result
-;;;; is accumulated into components incrementally.
-(defstruct (component #-jscl (:print-object print-component))
- entry
- exit)
-
-;;; Create a new component with an empty basic block, ready to start
-;;; conversion to IR. It returns the component and the basic block as
-;;; multiple values.
-(defun make-empty-component ()
- (let ((entry (make-component-entry))
- (block (make-empty-block))
- (exit (make-component-exit)))
- (setf (block-succ entry) (list block)
- (block-pred exit) (list block)
- (block-succ block) (list exit)
- (block-pred block) (list entry))
- (values (make-component :entry entry :exit exit) block)))
-
-;;; Return the list of blocks in COMPONENT, conveniently sorted.
-(defun component-blocks (component)
- (let ((seen nil)
- (output nil))
- (labels ((compute-rdfo-from (block)
- (unless (or (component-exit-p block) (find block seen))
- (push block seen)
- (dolist (successor (block-succ block))
- (unless (component-exit-p block)
- (compute-rdfo-from successor)))
- (push block output))))
- (compute-rdfo-from (unlist (block-succ (component-entry component))))
- output)))
-
-;;; Iterate across different blocks in COMPONENT.
-(defmacro do-blocks ((block component &optional result) &body body)
- `(dolist (,block (component-blocks ,component) ,result)
- ,@body))
-
-(defmacro do-blocks-backward ((block component &optional result) &body body)
- `(dolist (,block (reverse (component-blocks ,component)) ,result)
- ,@body))
-
-
-;;; A few consistency checks in the IR useful for catching bugs.
-(defun check-ir-consistency (component)
- (with-simple-restart (continue "Continue execution")
- (do-blocks (block component)
- (dolist (succ (block-succ block))
- (unless (find block (block-pred succ))
- (error "The block `~S' does not belong to the predecessors list of the its successor `~S'"
- (block-id block)
- (block-id succ))))
- (dolist (pred (block-pred block))
- (unless (find block (block-succ pred))
- (error "The block `~S' does not belong to the successors' list of its predecessor `~S'"
- (block-id block)
- (block-id pred)))))))
-
-
;;;; Lexical environment
;;;;
;;;; It keeps an association between names and the IR entities. It is
(defstruct binding
name namespace type value)
-(defvar *lexenv*)
+(defvar *lexenv* nil)
(defun find-binding (name namespace)
(find-if (lambda (b)
;;;; that is the `ir-convert' function, which dispatches to IR
;;;; translators. This function ss intended to do the initial
;;;; conversion as well as insert new IR code during optimizations.
-;;;;
-;;;; The function `ir-complete' will coalesce basic blocks in a
-;;;; component to generate proper maximal basic blocks.
-
-;;; The current component. We accumulate the results of the IR
-;;; conversion in this component.
-(defvar *component*)
;;; A alist of IR translator functions.
(defvar *ir-translator* nil)
;;; unique successor, and so it should be when the translator returns.
(defmacro define-ir-translator (name lambda-list &body body)
(check-type name symbol)
- (let ((fname (intern (format nil "IR-CONVERT-~a" (string name))))
- (result (gensym))
- (form (gensym)))
- `(progn
- (defun ,fname (,form ,result)
- (flet ((result-lvar () ,result))
- (declare (ignorable (function result-lvar)))
- (destructuring-bind ,lambda-list ,form
- ,@body)))
- (push (cons ',name #',fname) *ir-translator*))))
+ (let ((fname (intern (format nil "IR-CONVERT-~a" (string name)))))
+ (with-gensyms (result form)
+ `(progn
+ (defun ,fname (,form ,result)
+ (flet ((result-lvar () ,result))
+ (declare (ignorable (function result-lvar)))
+ (destructuring-bind ,lambda-list ,form
+ ,@body)))
+ (push (cons ',name #',fname) *ir-translator*)))))
;;; Return the unique successor of the current block. If it is not
;;; unique signal an error.
(ir-convert-constant form (result-lvar)))
(define-ir-translator setq (variable value)
- (let ((var (make-var :name variable))
- (value-lvar (make-lvar)))
- (ir-convert value value-lvar)
- (let ((assign (make-assignment :variable var :value value-lvar :lvar (result-lvar))))
- (insert-node assign))))
+ (let ((b (find-binding variable 'variable)))
+ (cond
+ (b
+ (let ((var (make-var :name variable))
+ (value-lvar (make-lvar)))
+ (ir-convert value value-lvar)
+ (let ((assign (make-assignment :variable var :value value-lvar :lvar (result-lvar))))
+ (insert-node assign))))
+ (t
+ (ir-convert `(set ',variable ,value) (result-lvar))))))
(define-ir-translator progn (&body body)
(mapc #'ir-convert (butlast body))
(set-cursor :block join-block)))
(define-ir-translator block (name &body body)
- (push-binding name 'block (cons (next-block) (result-lvar)))
- (ir-convert `(progn ,@body) (result-lvar)))
+ (let ((new (split-block)))
+ (push-binding name 'block (cons (next-block) (result-lvar)))
+ (ir-convert `(progn ,@body) (result-lvar))
+ (set-cursor :block new)))
(define-ir-translator return-from (name &optional value)
(let ((binding
;; block in a alist in TAG-BLOCKS.
(let ((*cursor* *cursor*))
(dolist (tag tags)
- (set-cursor :block (split-block))
+ (setq *cursor* (cursor :block (split-block)))
(push-binding tag 'tag (current-block))
(if (assoc tag tag-blocks)
(error "Duplicated tag `~S' in tagbody." tag)
(set-cursor :block dummy))))
+(defun ir-convert-functoid (result name arguments &rest body)
+ (let ((component)
+ (return-lvar (make-lvar)))
+ (with-component-compilation (name)
+ (ir-convert `(progn ,@body) return-lvar)
+ (ir-normalize)
+ (setq component *component*))
+ (let ((functional
+ (make-functional
+ :name name
+ :arguments arguments
+ :component component
+ :return-lvar return-lvar)))
+ (push functional (component-functions *component*))
+ (insert-node (make-ref :leaf functional :lvar result)))))
+
+(define-ir-translator function (name)
+ (if (atom name)
+ (ir-convert `(symbol-function ,name) (result-lvar))
+ (ecase (car name)
+ ((lambda named-lambda)
+ (let ((desc (cdr name)))
+ (when (eq 'lambda (car name))
+ (push nil desc))
+ (apply #'ir-convert-functoid (result-lvar) desc)))
+ (setf))))
+
(defun ir-convert-var (form result)
- (let* ((leaf (make-var :name form)))
- (insert-node (make-ref :leaf leaf :lvar result))))
+ (let ((binds (find-binding form 'variable)))
+ (if binds
+ (insert-node (make-ref :leaf (binding-value binds) :lvar result))
+ (ir-convert `(symbol-value ',form) result))))
(defun ir-convert-call (form result)
(destructuring-bind (function &rest args) form
(let ((func-lvar (make-lvar))
(args-lvars nil))
- (when (symbolp function)
- (ir-convert `(%symbol-function ,function) func-lvar))
+ ;; Argument list
(dolist (arg args)
(let ((arg-lvar (make-lvar)))
(push arg-lvar args-lvars)
(ir-convert arg arg-lvar)))
(setq args-lvars (reverse args-lvars))
- (let ((call (make-call :function func-lvar :arguments args-lvars :lvar result)))
- (insert-node call)))))
-
-;;; Convert the Lisp expression FORM into IR before the NEXT node, it
-;;; may create new basic blocks into the current component. RESULT is
-;;; the lvar representing the result of the computation or null if the
-;;; value should be discarded. The IR is inserted at *CURSOR*.
+ ;; Funcall
+ (if (find-primitive function)
+ (insert-node (make-primitive-call
+ :function (find-primitive function)
+ :arguments args-lvars
+ :lvar result))
+ (progn
+ (ir-convert `(symbol-function ,function) func-lvar)
+ (insert-node (make-call :function func-lvar
+ :arguments args-lvars
+ :lvar result)))))))
+
+;;; Convert the Lisp expression FORM, it may create new basic
+;;; blocks. RESULT is the lvar representing the result of the
+;;; computation or null if the value should be discarded. The IR is
+;;; inserted at *CURSOR*.
(defun ir-convert (form &optional result (*cursor* *cursor*))
;; Rebinding the lexical environment here we make sure that the
;; lexical information introduced by FORM is just available for
(values)))
-;;; Prepare a new component with a current empty block ready to start
-;;; IR conversion bound in the current cursor. BODY is evaluated and
-;;; the value of the last form is returned.
-(defmacro with-component-compilation (&body body)
- (let ((block (gensym)))
- `(multiple-value-bind (*component* ,block)
- (make-empty-component)
- (let ((*cursor* (cursor :block ,block))
- (*lexenv* nil))
- ,@body))))
-
-;;; Change all the predecessors of BLOCK to precede NEW-BLOCK instead.
-(defun replace-block (block new-block)
- (let ((predecessors (block-pred block)))
- (setf (block-pred new-block) (union (block-pred new-block) predecessors))
- (dolist (pred predecessors)
- (setf (block-succ pred) (substitute new-block block (block-succ pred)))
- (unless (component-entry-p pred)
- (let ((last-node (node-prev (block-exit pred))))
- (when (conditional-p last-node)
- (macrolet ((replacef (place)
- `(setf ,place (if (eq block ,place) new-block ,place))))
- (replacef (conditional-consequent last-node))
- (replacef (conditional-alternative last-node)))))))))
-
-(defun delete-empty-block (block)
- (when (or (component-entry-p block) (component-exit-p block))
- (error "Cannot delete entry or exit basic blocks."))
- (unless (empty-block-p block)
- (error "Block `~S' is not empty!" (block-id block)))
- (replace-block block (unlist (block-succ block))))
+;;;; IR Normalization
+;;;;
+;;;; IR as generated by `ir-convert' or after some transformations is
+;;;; not appropiated. Here, we remove unreachable and empty blocks and
+;;;; coallesce blocks when it is possible.
;;; Try to coalesce BLOCK with the successor if it is unique and block
;;; is its unique predecessor.
(when (and (not (component-exit-p succ)) (singlep (block-pred succ)))
(link-nodes (node-prev (block-exit block))
(node-next (block-entry succ)))
+ (setf (block-exit block) (block-exit succ))
(setf (block-succ block) (block-succ succ))
(dolist (next (block-succ succ))
(setf (block-pred next) (substitute block succ (block-pred next))))
+ (setf (block-succ succ) nil
+ (block-pred succ) nil)
t))))
-(defun ir-complete (&optional (component *component*))
- (do-blocks-backward (block component)
- (maybe-coalesce-block block)
- (when (empty-block-p block)
- (delete-empty-block block))))
+;;; Normalize a component. This function must be called after a batch
+;;; of modifications to the flowgraph of the component to make sure it
+;;; is a valid input for the possible optimizations and the backend.
+(defun ir-normalize (&optional (component *component*))
+ ;; Initialize blocks as unreachables and remove empty basic blocks.
+ (dolist (block (component-blocks component))
+ (setf (block-data block) 'unreachable))
+ ;; Coalesce and mark blocks as reachable.
+ (map-postorder-blocks
+ (lambda (block)
+ (maybe-coalesce-block block)
+ (setf (block-data block) 'reachable))
+ component)
+ (let ((block-list nil))
+ (dolist (block (component-blocks component))
+ (cond
+ ;; If the block is unreachable, but it is predeces a reachable
+ ;; one, then break the link between them. So we discard it
+ ;; from the flowgraph.
+ ((eq (block-data block) 'unreachable)
+ (setf (block-succ block) nil)
+ (dolist (succ (block-succ block))
+ (when (eq (block-data succ) 'reachable)
+ (remove block (block-pred succ)))))
+ ;; Delete empty blocks
+ ((empty-block-p block)
+ (delete-block block))
+ ;; The rest of blocks remain in the component.
+ (t
+ (push block block-list))))
+ (setf (component-blocks component) block-list))
+ (check-ir-consistency))
+
+
+;;;; IR Analysis
+;;;;
+;;;; Once IR conversion has been finished. We do some analysis of the
+;;;; component to produce information which is useful for both
+;;;; optimizations and code generation. Indeed, we provide some
+;;;; abstractions to use this information.
+
+(defun compute-reverse-post-order (component)
+ (let ((output nil)
+ (count 0))
+ (flet ((add-block-to-list (block)
+ (push block output)
+ (setf (block-order block) (incf count))))
+ (map-postorder-blocks #'add-block-to-list component))
+ (setf (component-reverse-post-order-p component) t)
+ (setf (component-blocks component) output)))
+
+;;; Iterate across blocks in COMPONENT in reverse post order.
+(defmacro do-blocks-forward ((block component &optional result) &body body)
+ (with-gensyms (g!component)
+ `(let ((,g!component ,component))
+ (dolist (,block (if (component-reverse-post-order-p ,g!component)
+ (component-blocks ,g!component)
+ (error "reverse post order was not computed yet."))
+ ,result)
+ ,@body))))
+
+;;; Iterate across blocks in COMPONENT in post order.
+(defmacro do-blocks-backward ((block component &optional result) &body body)
+ (with-gensyms (g!component)
+ `(let ((,g!component ,component))
+ (dolist (,block (if (component-reverse-post-order-p ,g!component)
+ (reverse (component-blocks ,g!component))
+ (error "reverse post order was not computed yet."))
+ ,result)
+ ,@body))))
+(defun compute-dominators (component)
+ ;; Initialize the dominators of the entry to the component to be
+ ;; empty and the power set of the set of blocks for proper basic
+ ;; blocks in the component.
+ (let ((n (length (component-blocks component))))
+ ;; The component entry special block has not predecessors in the
+ ;; set of (proper) basic blocks.
+ (setf (block-dominators% (component-entry component))
+ (make-array n :element-type 'bit :initial-element 0))
+ (dolist (block (component-blocks component))
+ (setf (block-dominators% block) (make-array n :element-type 'bit :initial-element 1))))
+ ;; Iterate across the blocks in the component removing non domintors
+ ;; until it reaches a fixed point.
+ (do ((i 0 0)
+ (iteration 0 (1+ iteration))
+ (changes t))
+ ((not changes))
+ (setf changes nil)
+ (do-blocks-forward (block component)
+ (let* ((predecessors (block-pred block)))
+ (bit-and (block-dominators% block) (block-dominators% (first predecessors)) t)
+ (dolist (pred (rest predecessors))
+ (bit-and (block-dominators% block) (block-dominators% pred) t))
+ (setf (aref (block-dominators% block) i) 1)
+ (setf changes (or changes (not (equal (block-dominators% block) (block-dominators% block)))))
+ (incf i)))))
+
+;;; Return T if BLOCK1 dominates BLOCK2, else return NIL.
+(defun dominate-p (block1 block2)
+ (let ((order (block-order block1)))
+ (= 1 (aref (block-dominators% block2) order))))
+
+
+;;;; IR Debugging
+;;;;
+;;;; This section provides a function `/print' which write a textual
+;;;; representation of a component to the standard output. Also, a
+;;;; `/ir' macro is provided, which takes a form, convert it to IR and
+;;;; then print the component as above. They are useful commands if
+;;;; you are hacking the front-end of the compiler.
+;;;;
+
+(defun format-block-name (block)
+ (cond
+ ((eq block (unlist (block-succ (component-entry (block-component block)))))
+ (format nil "ENTRY-~a" (component-id (block-component block))))
+ ((component-exit-p block)
+ (format nil "EXIT-~a" (component-id (block-component block))))
+ (t
+ (format nil "BLOCK ~a" (block-id block)))))
-;;; IR Debugging
(defun print-node (node)
(when (node-lvar node)
- (format t "~a = " (lvar-id (node-lvar node))))
+ (format t "$~a = " (lvar-id (node-lvar node))))
(cond
((ref-p node)
(let ((leaf (ref-leaf node)))
((constant-p leaf)
(format t "'~s" (constant-value leaf)))
((functional-p leaf)
- (format t "#<function ~a at ~a>"
- (functional-name leaf)
- (functional-entry-point leaf))))))
+ (format t "#<function ~a>" (functional-name leaf))))))
((assignment-p node)
- (format t "set ~a ~a"
+ (format t "set ~a $~a"
(var-name (assignment-variable node))
(lvar-id (assignment-value node))))
+ ((primitive-call-p node)
+ (format t "primitive ~a" (primitive-name (primitive-call-function node)))
+ (dolist (arg (primitive-call-arguments node))
+ (format t " $~a" (lvar-id arg))))
((call-p node)
- (format t "call ~a" (lvar-id (call-function node)))
+ (format t "call $~a" (lvar-id (call-function node)))
(dolist (arg (call-arguments node))
- (format t " ~a" (lvar-id arg))))
+ (format t " $~a" (lvar-id arg))))
((conditional-p node)
- (format t "if ~a ~a ~a"
+ (format t "if $~a then ~a else ~a~%"
(lvar-id (conditional-test node))
- (block-id (conditional-consequent node))
- (block-id (conditional-alternative node))))
+ (format-block-name (conditional-consequent node))
+ (format-block-name (conditional-alternative node))))
(t
(error "`print-node' does not support printing ~S as a node." node)))
(terpri))
(defun print-block (block)
- (flet ((block-name (block)
- (cond
- ((and (singlep (block-pred block))
- (component-entry-p (unlist (block-pred block))))
- "ENTRY")
- ((component-exit-p block)
- "EXIT")
- (t (string (block-id block))))))
- (format t "BLOCK ~a:~%" (block-name block))
- (do-nodes (node block)
- (print-node node))
- (when (singlep (block-succ block))
- (format t "GO ~a~%" (block-name (first (block-succ block)))))
- (terpri)))
-
-(defun print-component (component &optional (stream *standard-output*))
+ (write-line (format-block-name block))
+ (do-nodes (node block)
+ (print-node node))
+ (when (singlep (block-succ block))
+ (format t "GO ~a~%~%" (format-block-name (unlist (block-succ block))))))
+
+(defun /print (component &optional (stream *standard-output*))
+ (format t ";;; COMPONENT ~a (~a) ~%~%" (component-name component) (component-id component))
(let ((*standard-output* stream))
- (do-blocks (block component)
- (print-block block))))
+ (do-blocks-forward (block component)
+ (print-block block)))
+ (format t ";;; END COMPONENT ~a ~%~%" (component-name component))
+ (let ((*standard-output* stream))
+ (dolist (func (component-functions component))
+ (/print (functional-component func)))))
;;; Translate FORM into IR and print a textual repreresentation of the
;;; component.
-(defun describe-ir (form &optional (complete t))
- (with-component-compilation
- (ir-convert form (make-lvar :id "$out"))
- (when complete (ir-complete))
- (check-ir-consistency *component*)
- (print-component *component*)))
+(defun convert-toplevel-and-print (form)
+ (let ((*counter-alist* nil))
+ (with-component-compilation ('toplevel)
+ (ir-convert form (make-lvar :id "out"))
+ (ir-normalize)
+ (compute-reverse-post-order *component*)
+ (/print *component*)
+ *component*)))
+
+(defmacro /ir (form)
+ `(convert-toplevel-and-print ',form))
+
+
+
+;;;; Primitives
+;;;;
+;;;; Primitive functions are a set of functions provided by the
+;;;; compiler. They cannot usually be written in terms of other
+;;;; functions. When the compiler tries to compile a function call, it
+;;;; looks for a primitive function firstly, and if it is found and
+;;;; the declarations allow it, a primitive call is inserted in the
+;;;; IR. The back-end of the compiler knows how to compile primitive
+;;;; calls.
+;;;;
+
+(defvar *primitive-function-table* nil)
+
+(defstruct primitive
+ name)
+
+(defmacro define-primitive (name args &body body)
+ (declare (ignore args body))
+ `(push (make-primitive :name ',name)
+ *primitive-function-table*))
+
+(defun find-primitive (name)
+ (find name *primitive-function-table* :key #'primitive-name))
+
+(define-primitive symbol-function (symbol))
+(define-primitive symbol-value (symbol))
+(define-primitive set (symbol value))
+(define-primitive fset (symbol value))
+(define-primitive + (&rest numbers))
+(define-primitive - (number &rest other-numbers))
+(define-primitive consp (x))
+(define-primitive cons (x y))
+(define-primitive car (x))
+(define-primitive cdr (x))
;;; compiler.lisp ends here