3 ;; Copyright (C) 2013 David Vazquez
5 ;; JSCL is free software: you can redistribute it and/or
6 ;; modify it under the terms of the GNU General Public License as
7 ;; published by the Free Software Foundation, either version 3 of the
8 ;; License, or (at your option) any later version.
10 ;; JSCL is distributed in the hope that it will be useful, but
11 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with JSCL. If not, see <http://www.gnu.org/licenses/>.
25 ;;;; Random Common Lisp code useful to use here and there.
27 (defmacro with-gensyms ((&rest vars) &body body)
28 `(let ,(mapcar (lambda (var) `(,var (gensym ,(concatenate 'string (string var) "-")))) vars)
32 (and (consp x) (null (cdr x))))
38 (defun generic-printer (x stream)
39 (print-unreadable-object (x stream :type t :identity t)))
41 ;;; A generic counter mechanism. IDs are used generally for debugging
42 ;;; purposes. You can bind *counter-alist* to NIL to reset the
43 ;;; counters in a dynamic extent.
44 (defvar *counter-alist* nil)
45 (defun generate-id (class)
46 (let ((e (assoc class *counter-alist*)))
50 (push (cons class 1) *counter-alist*)))))
53 ;;;; Intermediate representation structures
55 ;;;; This intermediate representation (IR) is a simplified version of
56 ;;;; the first intermediate representation what you will find if you
57 ;;;; have a look to the source code of SBCL. Some terminology is also
58 ;;;; used, but other is changed, so be careful if you assume you know
59 ;;;; what it is because you know the name.
61 ;;;; Computations are represented by `node'. Nodes are grouped
62 ;;;; sequencially into `basic-block'. It is a plain representation
63 ;;;; rather than a nested one. Computations take data and produce a
64 ;;;; value. Both data transfer are represented by `lvar'.
68 ;;; A (lexical) variable. Special variables has not a special
69 ;;; representation in the IR. They are handled by the primitive
70 ;;; functions `%symbol-function' and `%symbol-value'.
71 (defstruct (var (:include leaf))
72 ;; The symbol which names this variable in the source code.
75 ;;; A literal Lisp object. It usually comes from a quoted expression.
76 (defstruct (constant (:include leaf))
80 ;;; A lambda expression. Why do we name it `functional'? Well,
81 ;;; function is reserved by the ANSI, isn't it?
82 (defstruct (functional (:include leaf) (:print-object generic-printer))
83 ;; The symbol which names this function in the source code or null
84 ;; if we do not know or it is an anonymous function.
90 ;;; An abstract place where the result of a computation is stored and
91 ;;; it can be referenced from other nodes, so lvars are responsible
92 ;;; for keeping the necessary information of the nested structure of
93 ;;; the code in this plain representation.
95 (id (generate-id 'lvar)))
97 ;;; A base structure for every single computation. Most of the
98 ;;; computations are valued.
99 (defstruct (node (:print-object generic-printer))
100 ;; The next and the prev slots are the next nodes and the previous
101 ;; node in the basic block sequence respectively.
103 ;; Lvar which stands for the result of the computation of this node.
106 ;;; Sentinel nodes in the basic block sequence of nodes.
107 (defstruct (block-entry (:include node)))
108 (defstruct (block-exit (:include node)))
110 ;;; A reference to a leaf (variable, constant and functions). The
111 ;;; meaning of this node is leaving the leaf into the lvar of the
113 (defstruct (ref (:include node))
116 ;;; An assignation of the LVAR VALUE into the var VARIABLE.
117 (defstruct (assignment (:include node))
121 ;;; A base node to function calls with a list of lvar as ARGUMENTS.
122 (defstruct (combination (:include node) (:constructor))
125 ;;; A function call to the ordinary Lisp function in the lvar FUNCTION.
126 (defstruct (call (:include combination))
129 ;;; A function call to the primitive FUNCTION.
130 (defstruct (primitive-call (:include combination))
134 ;;; A conditional branch. If the LVAR is not NIL, then we will jump to
135 ;;; the basic block CONSEQUENT, jumping to ALTERNATIVE otherwise. By
136 ;;; definition, a conditional must appear at the end of a basic block.
137 (defstruct (conditional (:include node))
145 ;;;; Components are connected pieces of the control flow graph of
146 ;;;; basic blocks with some additional information. Components have
147 ;;;; well-defined entry and exit nodes. It is the toplevel
148 ;;;; organizational entity in the compiler. The IR translation result
149 ;;;; is accumulated into components incrementally.
150 (defstruct (component (:print-object generic-printer))
151 (id (generate-id 'component))
158 ;;; The current component. We accumulate the results of the IR
159 ;;; conversion in this component.
162 ;;; Create a new component with an empty basic block, ready to start
163 ;;; conversion to IR. It returns the component and the basic block as
165 (defun make-empty-component (&optional name)
166 (let ((*component* (make-component :name name)))
167 (let ((entry (make-component-entry))
168 (block (make-empty-block))
169 (exit (make-component-exit)))
170 (setf (block-succ entry) (list block)
171 (block-pred exit) (list block)
172 (block-succ block) (list exit)
173 (block-pred block) (list entry)
174 (component-entry *component*) entry
175 (component-exit *component*) exit)
176 (values *component* block))))
178 ;;; Prepare a new component with a current empty block ready to start
179 ;;; IR conversion bound in the current cursor. BODY is evaluated and
180 ;;; the value of the last form is returned.
181 (defmacro with-component-compilation ((&optional name) &body body)
182 (with-gensyms (block)
183 `(multiple-value-bind (*component* ,block)
184 (make-empty-component ,name)
185 (let ((*cursor* (cursor :block ,block)))
188 ;;; Call function for each block in component in post-order.
189 (defun map-postorder-blocks (function component)
191 (labels ((compute-from (block)
192 (unless (or (component-exit-p block) (find block seen))
194 (dolist (successor (block-succ block))
195 (unless (component-exit-p block)
196 (compute-from successor)))
197 (funcall function block))))
198 (compute-from (unlist (block-succ (component-entry component))))
201 ;;; Iterate across different blocks in COMPONENT.
202 (defmacro do-blocks ((block component &optional result) &body body)
203 `(dolist (,block (or (component-blocks ,component)
204 (error "Component is not normalized."))
208 (defmacro do-blocks-backward ((block component &optional result) &body body)
209 `(dolist (,block (or (reverse (component-blocks ,component))
210 (error "component is not normalized."))
214 ;;; A few consistency checks in the IR useful for catching bugs.
215 (defun check-ir-consistency (&optional (component *component*))
216 (with-simple-restart (continue "Continue execution")
217 (do-blocks (block component)
218 (dolist (succ (block-succ block))
219 (unless (find block (block-pred succ))
220 (error "The block `~S' does not belong to the predecessors list of the its successor `~S'"
223 (unless (or (boundary-block-p succ) (find succ (component-blocks component)))
224 (error "Block `~S' is reachable but it is not in the component `~S'" succ component)))
225 (dolist (pred (block-pred block))
226 (unless (find block (block-succ pred))
227 (error "The block `~S' does not belong to the successors' list of its predecessor `~S'"
230 (unless (or (boundary-block-p pred) (find pred (component-blocks component)))
231 (error "Block `~S' is reachable but it is not in the component `~S'" pred component))))))
234 ;;; Blocks are `basic block`. Basic blocks are organized as a control
235 ;;; flow graph with some more information in omponents.
236 (defstruct (basic-block
237 (:conc-name "BLOCK-")
238 (:constructor make-block)
240 (:print-object generic-printer))
241 (id (generate-id 'basic-block))
242 ;; List of successors and predecessors of this basic block.
244 ;; The sentinel nodes of the sequence.
246 ;; The component where this block belongs
247 (component *component*)
250 ;;; Sentinel nodes in the control flow graph of basic blocks.
251 (defstruct (component-entry (:include basic-block)))
252 (defstruct (component-exit (:include basic-block)))
254 ;;; Return a fresh empty basic block.
255 (defun make-empty-block ()
256 (let ((entry (make-block-entry))
257 (exit (make-block-exit)))
258 (setf (node-next entry) exit
259 (node-prev exit) entry)
260 (make-block :entry entry :exit exit)))
262 ;;; Return T if B is an empty basic block and NIL otherwise.
263 (defun empty-block-p (b)
264 (block-exit-p (node-next (block-entry b))))
266 (defun boundary-block-p (block)
267 (or (component-entry-p block)
268 (component-exit-p block)))
270 ;;; Iterate across the nodes in a basic block forward.
272 ((node block &optional result &key include-sentinel-p) &body body)
273 `(do ((,node ,(if include-sentinel-p
274 `(block-entry ,block)
275 `(node-next (block-entry ,block)))
277 (,(if include-sentinel-p
279 `(block-exit-p ,node))
283 ;;; Iterate across the nodes in a basic block backward.
284 (defmacro do-nodes-backward
285 ((node block &optional result &key include-sentinel-p) &body body)
286 `(do ((,node ,(if include-sentinel-p
288 `(node-prev (block-entry ,block)))
290 (,(if include-sentinel-p
292 `(block-entry-p ,node))
296 ;;; Link FROM and TO nodes together. FROM and TO must belong to the
297 ;;; same basic block and appear in such order. The nodes between FROM
298 ;;; and TO are discarded.
299 (defun link-nodes (from to)
300 (setf (node-next from) to
308 ;;;; A cursor is a point between two nodes in some basic block in the
309 ;;;; IR representation where manipulations can take place, similarly
310 ;;;; to the cursors in text editing.
312 ;;;; Cursors cannot point to special component's entry and exit basic
313 ;;;; blocks or after a conditional node. Conveniently, the `cursor'
314 ;;;; function will signal an error if the cursor is not positioned
315 ;;;; correctly, so the rest of the code does not need to check once
321 ;;; The current cursor. It is the default cursor for many functions
322 ;;; which work on cursors.
325 ;;; Return the current basic block. It is to say, the basic block
326 ;;; where the current cursor is pointint.
327 (defun current-block ()
328 (cursor-block *cursor*))
330 ;;; Create a cursor which points to the basic block BLOCK. If omitted,
331 ;;; then the current block is used.
333 ;;; The keywords AFTER and BEFORE specify the cursor will point after (or
334 ;;; before) that node respectively. If none is specified, the cursor is
335 ;;; created before the exit node in BLOCK. An error is signaled if both
336 ;;; keywords are specified inconsistently, or if the nodes do not belong
339 ;;; AFTER and BEFORE could also be the special values :ENTRY and :EXIT,
340 ;;; which stand for the entry and exit nodes of the block respectively.
341 (defun cursor (&key (block (current-block))
342 (before nil before-p)
344 (when (boundary-block-p block)
345 (error "Invalid cursor on special entry/exit basic block."))
346 ;; Handle special values :ENTRY and :EXIT.
347 (flet ((node-designator (x)
349 (:entry (block-entry block))
350 (:exit (block-exit block))
352 (setq before (node-designator before))
353 (setq after (node-designator after)))
354 (let* ((next (or before (and after (node-next after)) (block-exit block)))
355 (cursor (make-cursor :block block :next next)))
356 (flet ((out-of-range-cursor ()
357 (error "Out of range cursor."))
359 (error "Ambiguous cursor specified between two non-adjacent nodes.")))
360 (when (conditional-p (node-prev next))
361 (error "Invalid cursor after conditional node."))
362 (when (or (null next) (block-entry-p next))
363 (out-of-range-cursor))
364 (when (and before-p after-p (not (eq after before)))
366 (do-nodes-backward (node block (out-of-range-cursor) :include-sentinel-p t)
367 (when (eq next node) (return))))
370 ;;; Accept a cursor specification just as described in `cursor'
371 ;;; describing a position in the IR and modify destructively the
372 ;;; current cursor to point there.
373 (defun set-cursor (&rest cursor-spec)
374 (let ((newcursor (apply #'cursor cursor-spec)))
375 (setf (cursor-block *cursor*) (cursor-block newcursor))
376 (setf (cursor-next *cursor*) (cursor-next newcursor))
379 ;;; Insert NODE at cursor.
380 (defun insert-node (node &optional (cursor *cursor*))
381 (link-nodes (node-prev (cursor-next cursor)) node)
382 (link-nodes node (cursor-next cursor))
385 ;;; Split the block at CURSOR. The cursor will point to the end of the
386 ;;; first basic block. Return the three basic blocks as multiple
388 (defun split-block (&optional (cursor *cursor*))
389 ;; <aaaaa|zzzzz> ==> <aaaaa|>--<zzzzz>
390 (let* ((block (cursor-block cursor))
391 (newexit (make-block-exit))
392 (newentry (make-block-entry))
393 (exit (block-exit block))
394 (newblock (make-block :entry newentry
397 :succ (block-succ block))))
398 (insert-node newexit)
399 (insert-node newentry)
400 (setf (node-next newexit) nil)
401 (setf (node-prev newentry) nil)
402 (setf (block-exit block) newexit)
403 (setf (block-succ block) (list newblock))
404 (dolist (succ (block-succ newblock))
405 (setf (block-pred succ) (substitute newblock block (block-pred succ))))
406 (set-cursor :block block :before newexit)
409 ;;; Split the block at CURSOR if it is in the middle of it. The cursor
410 ;;; will point to the end of the first basic block. Return the three
411 ;;; basic blocks as multiple values.
412 (defun maybe-split-block (&optional (cursor *cursor*))
413 ;; If we are converting IR into the end of the basic block, it's
414 ;; fine, we don't need to do anything.
415 (unless (block-exit-p (cursor-next cursor))
416 (split-block cursor)))
420 ;;;; Lexical environment
422 ;;;; It keeps an association between names and the IR entities. It is
423 ;;;; used to guide the translation from the Lisp source code to the
424 ;;;; intermediate representation.
427 name namespace type value)
429 (defvar *lexenv* nil)
431 (defun find-binding (name namespace)
433 (and (eq (binding-name b) name)
434 (eq (binding-namespace b) namespace)))
437 (defun push-binding (name namespace value &optional type)
438 (push (make-binding :name name
447 ;;;; This code covers the translation from Lisp source code to the
448 ;;;; intermediate representation. The main entry point function to do
449 ;;;; that is the `ir-convert' function, which dispatches to IR
450 ;;;; translators. This function ss intended to do the initial
451 ;;;; conversion as well as insert new IR code during optimizations.
453 ;;;; The function `ir-normalize' will coalesce basic blocks in a
454 ;;;; component to generate proper maximal basic blocks, as well as
455 ;;;; compute reverse depth first ordering on the blocks.
457 ;;; A alist of IR translator functions.
458 (defvar *ir-translator* nil)
460 ;;; Define a IR translator for NAME. LAMBDA-LIST is used to
461 ;;; destructure the arguments of the form. Calling the local function
462 ;;; `result-lvar' you can get the LVAR where the compilation of the
463 ;;; expression should store the result of the evaluation.
465 ;;; The cursor is granted to be at the end of a basic block with a
466 ;;; unique successor, and so it should be when the translator returns.
467 (defmacro define-ir-translator (name lambda-list &body body)
468 (check-type name symbol)
469 (let ((fname (intern (format nil "IR-CONVERT-~a" (string name)))))
470 (with-gensyms (result form)
472 (defun ,fname (,form ,result)
473 (flet ((result-lvar () ,result))
474 (declare (ignorable (function result-lvar)))
475 (destructuring-bind ,lambda-list ,form
477 (push (cons ',name #',fname) *ir-translator*)))))
479 ;;; Return the unique successor of the current block. If it is not
480 ;;; unique signal an error.
482 (unlist (block-succ (current-block))))
484 ;;; Set the next block of the current one.
485 (defun (setf next-block) (new-value)
486 (let ((block (current-block)))
487 (dolist (succ (block-succ block))
488 (setf (block-pred succ) (remove block (block-pred succ))))
489 (setf (block-succ block) (list new-value))
490 (push block (block-pred new-value))
493 (defun ir-convert-constant (form result)
494 (let* ((leaf (make-constant :value form)))
495 (insert-node (make-ref :leaf leaf :lvar result))))
497 (define-ir-translator quote (form)
498 (ir-convert-constant form (result-lvar)))
500 (define-ir-translator setq (variable value)
501 (let ((b (find-binding variable 'variable)))
504 (let ((var (make-var :name variable))
505 (value-lvar (make-lvar)))
506 (ir-convert value value-lvar)
507 (let ((assign (make-assignment :variable var :value value-lvar :lvar (result-lvar))))
508 (insert-node assign))))
510 (ir-convert `(set ',variable ,value) (result-lvar))))))
512 (define-ir-translator progn (&body body)
513 (mapc #'ir-convert (butlast body))
514 (ir-convert (car (last body)) (result-lvar)))
516 (define-ir-translator if (test then &optional else)
517 ;; It is the schema of how the basic blocks will look like
520 ;; <aaaaXX> --< >-- <|> -- <zzzz>
523 ;; Note that is important to leave the cursor in an empty basic
524 ;; block, as zzz could be the exit basic block of the component,
525 ;; which is an invalid position for a cursor.
526 (let ((test-lvar (make-lvar))
527 (then-block (make-empty-block))
528 (else-block (make-empty-block))
529 (join-block (make-empty-block)))
530 (ir-convert test test-lvar)
531 (insert-node (make-conditional :test test-lvar :consequent then-block :alternative else-block))
532 (let* ((block (current-block))
533 (tail-block (next-block)))
534 ;; Link together the different created basic blocks.
535 (setf (block-succ block) (list else-block then-block)
536 (block-pred else-block) (list block)
537 (block-pred then-block) (list block)
538 (block-succ then-block) (list join-block)
539 (block-succ else-block) (list join-block)
540 (block-pred join-block) (list else-block then-block)
541 (block-succ join-block) (list tail-block)
542 (block-pred tail-block) (substitute join-block block (block-pred tail-block))))
543 ;; Convert he consequent and alternative forms and update cursor.
544 (ir-convert then (result-lvar) (cursor :block then-block))
545 (ir-convert else (result-lvar) (cursor :block else-block))
546 (set-cursor :block join-block)))
548 (define-ir-translator block (name &body body)
549 (let ((new (split-block)))
550 (push-binding name 'block (cons (next-block) (result-lvar)))
551 (ir-convert `(progn ,@body) (result-lvar))
552 (set-cursor :block new)))
554 (define-ir-translator return-from (name &optional value)
556 (or (find-binding name 'block)
557 (error "Tried to return from unknown block `~S' name" name))))
558 (destructuring-bind (jump-block . lvar)
559 (binding-value binding)
560 (ir-convert value lvar)
561 (setf (next-block) jump-block)
562 ;; This block is really unreachable, even if the following code
563 ;; is labelled in a tagbody, as tagbody will create a new block
564 ;; for each label. However, we have to leave the cursor
565 ;; somewhere to convert new input.
566 (let ((dummy (make-empty-block)))
567 (set-cursor :block dummy)))))
569 (define-ir-translator tagbody (&rest statements)
571 (or (integerp x) (symbolp x))))
572 (let* ((tags (remove-if-not #'go-tag-p statements))
574 ;; Create a chain of basic blocks for the tags, recording each
575 ;; block in a alist in TAG-BLOCKS.
576 (let ((*cursor* *cursor*))
578 (setq *cursor* (cursor :block (split-block)))
579 (push-binding tag 'tag (current-block))
580 (if (assoc tag tag-blocks)
581 (error "Duplicated tag `~S' in tagbody." tag)
582 (push (cons tag (current-block)) tag-blocks))))
583 ;; Convert the statements into the correct block.
584 (dolist (stmt statements)
586 (set-cursor :block (cdr (assoc stmt tag-blocks)))
587 (ir-convert stmt))))))
589 (define-ir-translator go (label)
591 (or (find-binding label 'tag)
592 (error "Unable to jump to the label `~S'" label))))
593 (setf (next-block) (binding-value tag-binding))
594 ;; Unreachable block.
595 (let ((dummy (make-empty-block)))
596 (set-cursor :block dummy))))
599 (defun ir-convert-functoid (result name arguments &rest body)
601 (return-lvar (make-lvar)))
602 (with-component-compilation (name)
603 (ir-convert `(progn ,@body) return-lvar)
605 (setq component *component*))
611 :return-lvar return-lvar)))
612 (push functional (component-functions *component*))
613 (insert-node (make-ref :leaf functional :lvar result)))))
615 (define-ir-translator function (name)
617 (ir-convert `(symbol-function ,name) (result-lvar))
619 ((lambda named-lambda)
620 (let ((desc (cdr name)))
621 (when (eq 'lambda (car name))
623 (apply #'ir-convert-functoid (result-lvar) desc)))
626 (defun ir-convert-var (form result)
627 (let ((binds (find-binding form 'variable)))
629 (insert-node (make-ref :leaf (binding-value binds) :lvar result))
630 (ir-convert `(symbol-value ',form) result))))
632 (defun ir-convert-call (form result)
633 (destructuring-bind (function &rest args) form
634 (let ((func-lvar (make-lvar))
638 (let ((arg-lvar (make-lvar)))
639 (push arg-lvar args-lvars)
640 (ir-convert arg arg-lvar)))
641 (setq args-lvars (reverse args-lvars))
643 (if (find-primitive function)
644 (insert-node (make-primitive-call
645 :function (find-primitive function)
646 :arguments args-lvars
649 (ir-convert `(symbol-function ,function) func-lvar)
650 (insert-node (make-call :function func-lvar
651 :arguments args-lvars
654 ;;; Convert the Lisp expression FORM, it may create new basic
655 ;;; blocks. RESULT is the lvar representing the result of the
656 ;;; computation or null if the value should be discarded. The IR is
657 ;;; inserted at *CURSOR*.
658 (defun ir-convert (form &optional result (*cursor* *cursor*))
659 ;; Rebinding the lexical environment here we make sure that the
660 ;; lexical information introduced by FORM is just available for
662 (let ((*lexenv* *lexenv*))
663 ;; Possibly create additional blocks in order to make sure the
664 ;; cursor is at end the end of a basic block.
670 (ir-convert-var form result))
672 (ir-convert-constant form result))))
674 (destructuring-bind (op &rest args) form
675 (let ((translator (cdr (assoc op *ir-translator*))))
677 (funcall translator args result)
678 (ir-convert-call form result))))))
682 ;;; Change all the predecessors of BLOCK to precede NEW-BLOCK instead.
683 (defun replace-block (block new-block)
684 (let ((predecessors (block-pred block)))
685 (setf (block-pred new-block) (union (block-pred new-block) predecessors))
686 (dolist (pred predecessors)
687 (setf (block-succ pred) (substitute new-block block (block-succ pred)))
688 (unless (component-entry-p pred)
689 (let ((last-node (node-prev (block-exit pred))))
690 (when (conditional-p last-node)
691 (macrolet ((replacef (place)
692 `(setf ,place (if (eq block ,place) new-block ,place))))
693 (replacef (conditional-consequent last-node))
694 (replacef (conditional-alternative last-node)))))))))
696 (defun delete-block (block)
697 (when (boundary-block-p block)
698 (error "Cannot delete entry or exit basic blocks."))
699 (unless (singlep (block-succ block))
700 (error "Cannot delete a basic block with multiple successors."))
701 (replace-block block (unlist (block-succ block))))
703 ;;; Try to coalesce BLOCK with the successor if it is unique and block
704 ;;; is its unique predecessor.
705 (defun maybe-coalesce-block (block)
706 (when (singlep (block-succ block))
707 (let ((succ (first (block-succ block))))
708 (when (and (not (component-exit-p succ)) (singlep (block-pred succ)))
709 (link-nodes (node-prev (block-exit block))
710 (node-next (block-entry succ)))
711 (setf (block-succ block) (block-succ succ))
712 (dolist (next (block-succ succ))
713 (setf (block-pred next) (substitute block succ (block-pred next))))
716 ;;; Normalize a component. This function must be called after a batch
717 ;;; of modifications to the flowgraph of the component to make sure it
718 ;;; is a valid input for the possible optimizations and the backend.
719 (defun ir-normalize (&optional (component *component*))
720 (flet ((clean-and-coallesce (block)
721 (maybe-coalesce-block block)
722 (when (empty-block-p block)
723 (delete-block block)))
725 (push block (component-blocks *component*))))
726 (map-postorder-blocks #'clean-and-coallesce component)
727 (map-postorder-blocks #'add-to-list component)
728 (check-ir-consistency)))
733 (defun format-block-name (block)
735 ((eq block (unlist (block-succ (component-entry (block-component block)))))
736 (format nil "ENTRY-~a" (component-id (block-component block))))
737 ((component-exit-p block)
738 (format nil "EXIT-~a" (component-id (block-component block))))
740 (format nil "BLOCK ~a" (block-id block)))))
742 (defun print-node (node)
743 (when (node-lvar node)
744 (format t "$~a = " (lvar-id (node-lvar node))))
747 (let ((leaf (ref-leaf node)))
750 (format t "~a" (var-name leaf)))
752 (format t "'~s" (constant-value leaf)))
754 (format t "#<function ~a>" (functional-name leaf))))))
756 (format t "set ~a $~a"
757 (var-name (assignment-variable node))
758 (lvar-id (assignment-value node))))
759 ((primitive-call-p node)
760 (format t "primitive ~a" (primitive-name (primitive-call-function node)))
761 (dolist (arg (primitive-call-arguments node))
762 (format t " $~a" (lvar-id arg))))
764 (format t "call $~a" (lvar-id (call-function node)))
765 (dolist (arg (call-arguments node))
766 (format t " $~a" (lvar-id arg))))
767 ((conditional-p node)
768 (format t "if $~a then ~a else ~a~%"
769 (lvar-id (conditional-test node))
770 (format-block-name (conditional-consequent node))
771 (format-block-name (conditional-alternative node))))
773 (error "`print-node' does not support printing ~S as a node." node)))
776 (defun print-block (block)
777 (write-line (format-block-name block))
778 (do-nodes (node block)
780 (when (singlep (block-succ block))
781 (format t "GO ~a~%~%" (format-block-name (unlist (block-succ block))))))
783 (defun /print (component &optional (stream *standard-output*))
784 (format t ";;; COMPONENT ~a (~a) ~%~%" (component-name component) (component-id component))
785 (let ((*standard-output* stream))
786 (do-blocks (block component)
787 (print-block block)))
788 (format t ";;; END COMPONENT ~a ~%~%" (component-name component))
789 (let ((*standard-output* stream))
790 (dolist (func (component-functions component))
791 (/print (functional-component func)))))
793 ;;; Translate FORM into IR and print a textual repreresentation of the
795 (defun convert-toplevel-and-print (form)
796 (let ((*counter-alist* nil))
797 (with-component-compilation ('toplevel)
798 (ir-convert form (make-lvar :id "out"))
804 `(convert-toplevel-and-print ',form))
810 (defun compute-dominators (component)
811 ;; Initialize the dominators of the entry to the component to be
812 ;; empty and the power set of the set of blocks for proper basic
813 ;; blocks in the component.
814 (let ((n (length (component-blocks component))))
815 ;; The component entry special block has not predecessors in the
816 ;; set of (proper) basic blocks.
817 (setf (block-dominators% (component-entry component))
818 (make-array n :element-type 'bit :initial-element 0))
819 (do-blocks (block component)
820 (setf (block-dominators% block) (make-array n :element-type 'bit :initial-element 1))))
821 ;; Iterate across the blocks in the component removing non domintors
822 ;; until it reaches a fixed point.tpn
827 (do-blocks (block component)
828 (format t "Processing ~a~%" (format-block-name block))
829 (let ((new (reduce #'bit-and (mapcar #'block-dominators% (block-pred block)))))
830 (setf (aref new i) 1)
831 (setf changes (or changes (not (equal new (block-dominators% block)))))
832 (setf (block-dominators% block) new)
839 ;;;; Primitive functions are a set of functions provided by the
840 ;;;; compiler. They cannot usually be written in terms of other
841 ;;;; functions. When the compiler tries to compile a function call, it
842 ;;;; looks for a primitive function firstly, and if it is found and
843 ;;;; the declarations allow it, a primitive call is inserted in the
844 ;;;; IR. The back-end of the compiler knows how to compile primitive
848 (defvar *primitive-function-table* nil)
853 (defmacro define-primitive (name args &body body)
854 (declare (ignore args body))
855 `(push (make-primitive :name ',name)
856 *primitive-function-table*))
858 (defun find-primitive (name)
859 (find name *primitive-function-table* :key #'primitive-name))
861 (define-primitive symbol-function (symbol))
862 (define-primitive symbol-value (symbol))
863 (define-primitive set (symbol value))
864 (define-primitive fset (symbol value))
866 (define-primitive + (&rest numbers))
867 (define-primitive - (number &rest other-numbers))
869 (define-primitive consp (x))
870 (define-primitive cons (x y))
871 (define-primitive car (x))
872 (define-primitive cdr (x))
875 ;;; compiler.lisp ends here