| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 2009, Juan Jose Garcia-Ripoll |
| 6 |
;;;; |
| 7 |
;;;; See file 'LICENSE' for the copyright details. |
| 8 |
|
| 9 |
;;;; |
| 10 |
;;;; CMPFORM -- Internal representation of Lisp forms |
| 11 |
;;;; |
| 12 |
|
| 13 |
(in-package "COMPILER") |
| 14 |
|
| 15 |
;;; |
| 16 |
;;; ALL C1FORMS: Intermediate language used by the compiler |
| 17 |
;;; |
| 18 |
;;; body = (c1form*) |
| 19 |
;;; tag-body = ({c1form | tag}*) |
| 20 |
;;; return-type = {CLB | CCB | UNWIND-PROTECT} |
| 21 |
;;; *value = c1form |
| 22 |
;;; lambda-list = (requireds optionals rest key-flag keywords allow-other-keys) |
| 23 |
;;; |
| 24 |
|
| 25 |
(defun make-c1form (name subform &rest args) |
| 26 |
(let ((form (do-make-c1form :name name :args args |
| 27 |
:type (info-type subform) |
| 28 |
:sp-change (info-sp-change subform) |
| 29 |
:volatile (info-volatile subform) |
| 30 |
:form *current-form* |
| 31 |
:toplevel-form *current-toplevel-form* |
| 32 |
:file *compile-file-truename* |
| 33 |
:file-position *compile-file-position*))) |
| 34 |
(c1form-add-info form args) |
| 35 |
form)) |
| 36 |
|
| 37 |
(defun make-c1form* (name &rest args) |
| 38 |
(let ((info-args '()) |
| 39 |
(form-args '())) |
| 40 |
(do ((l args (cdr l))) |
| 41 |
((endp l)) |
| 42 |
(let ((key (first l))) |
| 43 |
(cond ((not (keywordp key)) |
| 44 |
(baboon :format-control "make-c1form*: ~s is not a keyword." |
| 45 |
:format-arguments (list key))) |
| 46 |
((eq key ':args) |
| 47 |
(setf form-args (rest l)) |
| 48 |
(return)) |
| 49 |
(t |
| 50 |
(setf info-args (list* key (second l) info-args) |
| 51 |
l (cdr l)))))) |
| 52 |
(let ((form (apply #'do-make-c1form :name name :args form-args |
| 53 |
:form *current-form* |
| 54 |
:toplevel-form *current-toplevel-form* |
| 55 |
:file *compile-file-truename* |
| 56 |
:file-position *compile-file-position* |
| 57 |
info-args))) |
| 58 |
(c1form-add-info form form-args) |
| 59 |
form))) |
| 60 |
|
| 61 |
(defun c1form-add-info-loop (form dependents) |
| 62 |
(loop with subform |
| 63 |
while (consp dependents) |
| 64 |
when (c1form-p (setf subform (pop dependents))) |
| 65 |
do (progn |
| 66 |
(when (c1form-sp-change subform) |
| 67 |
(setf (c1form-sp-change form) t |
| 68 |
(c1form-side-effects form) t)) |
| 69 |
(when (c1form-side-effects subform) |
| 70 |
(setf (c1form-side-effects form) t)) |
| 71 |
(unless (eq (c1form-name subform) 'LOCATION) |
| 72 |
(when (rest (c1form-parents subform)) |
| 73 |
(error "Running twice through same form")) |
| 74 |
(setf (c1form-parents subform) |
| 75 |
(nconc (c1form-parents subform) |
| 76 |
(c1form-parents form))))) |
| 77 |
when (consp subform) |
| 78 |
do (c1form-add-info-loop form subform))) |
| 79 |
|
| 80 |
(defun c1form-add-info (form dependents) |
| 81 |
(let ((record (gethash (c1form-name form) +c1-form-hash+))) |
| 82 |
(unless record |
| 83 |
(error "Internal error: unknown C1FORM name ~A" |
| 84 |
(c1form-name form))) |
| 85 |
(let ((length (first record)) |
| 86 |
(sp-change (c1form-sp-change form)) |
| 87 |
(side-effects (second record))) |
| 88 |
(setf (c1form-side-effects form) |
| 89 |
(or (c1form-side-effects form) sp-change side-effects) |
| 90 |
(c1form-parents form) |
| 91 |
(list form)) |
| 92 |
(unless (or (null length) (= length (length (c1form-args form)))) |
| 93 |
(error "Internal error: illegal number of arguments in ~A" form)))) |
| 94 |
(c1form-add-info-loop form dependents)) |
| 95 |
|
| 96 |
(defmacro c1form-arg (nth form) |
| 97 |
(case nth |
| 98 |
(0 `(first (c1form-args ,form))) |
| 99 |
(1 `(second (c1form-args ,form))) |
| 100 |
(otherwise `(nth ,nth (c1form-args ,form))))) |
| 101 |
|
| 102 |
(defun c1form-volatile* (form) |
| 103 |
(if (c1form-volatile form) "volatile " "")) |
| 104 |
|
| 105 |
(defun c1form-primary-type (form) |
| 106 |
(values-type-primary-type (c1form-type form))) |
| 107 |
|
| 108 |
(defun location-primary-type (form) |
| 109 |
(c1form-primary-type form)) |
| 110 |
|
| 111 |
(defun find-form-in-node-list (form list) |
| 112 |
(let ((v1 (loop with form-parents = (c1form-parents form) |
| 113 |
for presumed-child-parents in list |
| 114 |
thereis (tailp form-parents presumed-child-parents))) |
| 115 |
(v2 (loop for presumed-child-parents in list |
| 116 |
thereis (member form presumed-child-parents :test #'eq)))) |
| 117 |
(unless (eq (and v1 t) (and v2 t)) |
| 118 |
(baboon :format-control "Mismatch between FIND-FORM-IN-NODE-LISTs")) |
| 119 |
v1)) |
| 120 |
|
| 121 |
(defun add-form-to-node-list (form list) |
| 122 |
(list* (c1form-parents form) list)) |
| 123 |
|
| 124 |
(defun delete-form-from-node-list (form list) |
| 125 |
(let ((parents (c1form-parents form))) |
| 126 |
(unless (member parents list) |
| 127 |
(baboon :format-control "Unable to find C1FORM~%~4I~A~%in node list~%~4I~A" |
| 128 |
:format-arguments (list form list))) |
| 129 |
(delete parents list))) |
| 130 |
|
| 131 |
(defun traverse-c1form-tree (tree function) |
| 132 |
(cond ((consp tree) |
| 133 |
(loop for f in tree |
| 134 |
do (traverse-c1form-tree f function))) |
| 135 |
((c1form-p tree) |
| 136 |
(loop for f in (c1form-args tree) |
| 137 |
do (traverse-c1form-tree f function)) |
| 138 |
(funcall function tree)))) |
| 139 |
|
| 140 |
(defun c1form-pure-p (form) |
| 141 |
(third (gethash (c1form-name form) +c1-form-hash+))) |
| 142 |
|
| 143 |
(defun c1form-movable-p (form) |
| 144 |
(flet ((abort-on-not-pure (form) |
| 145 |
(let ((name (c1form-name form))) |
| 146 |
(cond ((eq name 'VARIABLE) |
| 147 |
(let ((var (c1form-arg 0 form))) |
| 148 |
(when (or (global-var-p var) |
| 149 |
(var-set-nodes var)) |
| 150 |
(return-from c1form-movable-p nil)))) |
| 151 |
((or (c1form-side-effects form) |
| 152 |
(not (c1form-pure-p form))) |
| 153 |
(return-from c1form-movable-p nil)))))) |
| 154 |
(abort-on-not-pure form))) |
| 155 |
|
| 156 |
(defun c1form-unmodified-p (form rest-form) |
| 157 |
(flet ((abort-on-not-pure (form) |
| 158 |
(let ((name (c1form-name form))) |
| 159 |
(cond ((eq name 'VARIABLE) |
| 160 |
(let ((var (c1form-arg 0 form))) |
| 161 |
(when (var-changed-in-form-list var rest-form) |
| 162 |
(return-from c1form-unmodified-p nil)))) |
| 163 |
((or (c1form-side-effects form) |
| 164 |
(not (c1form-pure-p form))) |
| 165 |
(return-from c1form-unmodified-p nil)))))) |
| 166 |
(traverse-c1form-tree form #'abort-on-not-pure) |
| 167 |
t)) |
| 168 |
|
| 169 |
(defun c1form-values-number (form) |
| 170 |
(if (fourth (gethash (c1form-name form) +c1-form-hash+)) |
| 171 |
(values 1 1) |
| 172 |
(values-number-from-type (c1form-type form)))) |
| 173 |
|
| 174 |
(defun c1form-single-valued-p (form) |
| 175 |
(or (fourth (gethash (c1form-name form) +c1-form-hash+)) |
| 176 |
(= (nth-value 1 (c1form-values-number form)) 1))) |
| 177 |
|
| 178 |
(defmacro with-c1form-env ((form value) &rest body) |
| 179 |
`(let* ((,form ,value) |
| 180 |
(*current-c1form* ,form) |
| 181 |
(*compile-file-truename* (c1form-file ,form)) |
| 182 |
(*compile-file-position* (c1form-file-position ,form)) |
| 183 |
(*current-toplevel-form* (c1form-toplevel-form ,form)) |
| 184 |
(*current-form* (c1form-form ,form)) |
| 185 |
(*cmp-env* (c1form-env ,form))) |
| 186 |
,@body)) |
| 187 |
|
| 188 |
(defun relocate-parents-list (dest new-fields) |
| 189 |
(let* ((old (c1form-parents dest)) |
| 190 |
(first-cons (or (c1form-parents new-fields) old))) |
| 191 |
(setf (car first-cons) dest |
| 192 |
(cdr first-cons) (rest old) |
| 193 |
(c1form-parents new-fields) nil |
| 194 |
(c1form-parents dest) first-cons))) |
| 195 |
|
| 196 |
(defun c1form-replace-with (dest new-fields) |
| 197 |
;; Side effects might have to be propagated to the parents |
| 198 |
;; but currently we do not allow moving forms with side effects |
| 199 |
(when (c1form-side-effects new-fields) |
| 200 |
(baboon :format-control "Attempted to move a form with side-effects")) |
| 201 |
;; The following protocol is only valid for VAR references. |
| 202 |
(unless (eq (c1form-name dest) 'VARIABLE) |
| 203 |
(baboon :format-control "Cannot replace forms other than VARs:~%~4I~A" |
| 204 |
:format-arguments (list dest))) |
| 205 |
;; We have to relocate the children nodes of NEW-FIELDS in |
| 206 |
;; the new branch. This implies rewriting the parents chain, |
| 207 |
;; but only for non-location nodes (these are reused). The only |
| 208 |
;; exceptions are forms that can be fully replaced |
| 209 |
(case (c1form-name new-fields) |
| 210 |
(LOCATION) |
| 211 |
(VARIABLE |
| 212 |
(let ((var (c1form-arg 0 new-fields))) |
| 213 |
;; If this is the first time we replace a reference with this one |
| 214 |
;; then we have to remove it from the read nodes of the variable |
| 215 |
(when (c1form-parents new-fields) |
| 216 |
(delete-from-read-nodes var new-fields)) |
| 217 |
;; ... and then add the new node |
| 218 |
(relocate-parents-list dest new-fields) |
| 219 |
(add-to-read-nodes var dest))) |
| 220 |
(t |
| 221 |
(relocate-parents-list dest new-fields))) |
| 222 |
;; Remaining flags are just copied |
| 223 |
(setf (c1form-name dest) (c1form-name new-fields) |
| 224 |
(c1form-local-vars dest) (c1form-local-vars new-fields) |
| 225 |
(c1form-type dest) (values-type-and (c1form-type new-fields) |
| 226 |
(c1form-type dest)) |
| 227 |
(c1form-sp-change dest) (c1form-sp-change new-fields) |
| 228 |
(c1form-side-effects dest) (c1form-side-effects new-fields) |
| 229 |
(c1form-volatile dest) (c1form-volatile new-fields) |
| 230 |
(c1form-args dest) (c1form-args new-fields) |
| 231 |
(c1form-env dest) (c1form-env new-fields) |
| 232 |
(c1form-form dest) (c1form-form new-fields) |
| 233 |
(c1form-toplevel-form dest) (c1form-toplevel-form new-fields) |
| 234 |
(c1form-file dest) (c1form-file new-fields) |
| 235 |
(c1form-file-position dest) (c1form-file-position new-fields))) |
| 236 |
|
| 237 |
;; should check whether a form before var causes a side-effect |
| 238 |
;; exactly one occurrence of var is present in forms |
| 239 |
(defun delete-c1forms (form) |
| 240 |
(flet ((eliminate-references (form) |
| 241 |
(when (eq (c1form-name form) 'VARIABLE) |
| 242 |
(let ((var (c1form-arg 0 form))) |
| 243 |
(when var |
| 244 |
(delete-from-read-nodes var form)))))) |
| 245 |
(traverse-c1form-tree form #'eliminate-references))) |
| 246 |
|
| 247 |
(defun c1form-constant-p (form) |
| 248 |
(when (eq (c1form-name form) 'LOCATION) |
| 249 |
(loc-immediate-value-p (c1form-arg 0 form)))) |