| 1 |
;;;; |
| 2 |
;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya |
| 3 |
;;;; Copyright (c) 1990, Giuseppe Attardi |
| 4 |
;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll |
| 5 |
;;;; Copyright (c) 2021, Daniel Kochmański |
| 6 |
;;;; |
| 7 |
;;;; See file 'LICENSE' for the copyright details. |
| 8 |
;;;; |
| 9 |
|
| 10 |
(in-package #:compiler) |
| 11 |
|
| 12 |
;; should check whether a form before var causes a side-effect |
| 13 |
;; exactly one occurrence of var is present in forms |
| 14 |
(defun replaceable (var form) |
| 15 |
(labels ((abort-on-side-effects (form) |
| 16 |
(if (eq (c1form-name form) 'VARIABLE) |
| 17 |
(when (eq var (first (c1form-args form))) |
| 18 |
(return-from replaceable t)) |
| 19 |
(when (c1form-side-effects form) |
| 20 |
(return-from replaceable nil))))) |
| 21 |
(traverse-c1form-tree form #'abort-on-side-effects) |
| 22 |
(baboon :format-control "In REPLACEABLE, variable ~A not found. Form:~%~A" |
| 23 |
:format-arguments (list (var-name var) *current-form*)))) |
| 24 |
|
| 25 |
(defun nsubst-var (var form) |
| 26 |
(when (var-set-nodes var) |
| 27 |
(baboon :format-control "Cannot replace a variable that is to be changed")) |
| 28 |
(when (var-functions-reading var) |
| 29 |
(baboon :format-control "Cannot replace a variable that forms part of a closure")) |
| 30 |
(dolist (where (var-read-forms var)) |
| 31 |
(unless (and (eql (c1form-name where) 'VARIABLE) |
| 32 |
(eql (c1form-arg 0 where) var)) |
| 33 |
(baboon :format-control "VAR-READ-NODES are only C1FORMS of type VAR")) |
| 34 |
(delete-from-read-nodes var where) |
| 35 |
(c1form-replace-with where form)) |
| 36 |
(setf (var-ignorable var) 0)) |
| 37 |
|
| 38 |
;;; |
| 39 |
|
| 40 |
(defun make-var (&rest args) |
| 41 |
(let ((var (apply #'%make-var args))) |
| 42 |
(unless (member (var-kind var) '(SPECIAL GLOBAL)) |
| 43 |
(when *current-function* |
| 44 |
(push var (fun-local-vars *current-function*)))) |
| 45 |
var)) |
| 46 |
|
| 47 |
(defun make-lcl-var (&key host-type (type 'T)) |
| 48 |
(unless host-type |
| 49 |
(setq host-type (if type (lisp-type->host-type type) :object))) |
| 50 |
(unless type |
| 51 |
(setq type 'T)) |
| 52 |
(make-var :kind host-type :type type :loc (next-lcl))) |
| 53 |
|
| 54 |
(defun make-global-var (name &key |
| 55 |
(type (or (si:get-sysprop name 'CMP-TYPE) t)) |
| 56 |
(kind 'GLOBAL) |
| 57 |
(warn nil)) |
| 58 |
(let ((var (make-var :name name :kind kind :type type :loc (add-symbol name)))) |
| 59 |
(when warn |
| 60 |
(unless (or (constantp name) |
| 61 |
(special-variable-p name) |
| 62 |
(member name *undefined-vars*)) |
| 63 |
(undefined-variable name) |
| 64 |
(push name *undefined-vars*))) |
| 65 |
var)) |
| 66 |
|
| 67 |
(defun make-temp-var (&optional (type 'T)) |
| 68 |
(make-var :kind :object :type type :loc `(TEMP ,(next-temp)))) |
| 69 |
|
| 70 |
(defun var-referenced-in-form-list (var form-list) |
| 71 |
(loop for f in form-list |
| 72 |
thereis (var-referenced-in-form var f))) |
| 73 |
|
| 74 |
(defun var-changed-in-form-list (var form-list) |
| 75 |
(loop for f in form-list |
| 76 |
thereis (var-changed-in-form var f))) |
| 77 |
|
| 78 |
;;; FIXME! VAR-REFERENCED-IN-FORM and VAR-CHANGED-IN-FORM are too |
| 79 |
;;; pessimistic. One should check whether the functions reading/setting the |
| 80 |
;;; variable are actually called from the given node. The problem arises when |
| 81 |
;;; we create a closure of a function, as in |
| 82 |
;;; |
| 83 |
;;; (let* ((a 1) (b #'(lambda () (incf a)))) ...) |
| 84 |
;;; |
| 85 |
;;; To know whether A is changed or read, we would have to track where B is |
| 86 |
;;; actually used. |
| 87 |
|
| 88 |
(defun var-referenced-in-form (var form) |
| 89 |
(declare (type var var)) |
| 90 |
(or (find-form-in-node-list form (var-read-nodes var)) |
| 91 |
(var-functions-reading var))) |
| 92 |
|
| 93 |
(defun var-changed-in-form (var form) |
| 94 |
(declare (type var var)) |
| 95 |
(or (find-form-in-node-list form (var-set-nodes var)) |
| 96 |
(let ((kind (var-kind var))) |
| 97 |
(if (or (eq kind 'SPECIAL) (eq kind 'GLOBAL)) |
| 98 |
(c1form-sp-change form) |
| 99 |
(var-functions-setting var))))) |
| 100 |
|
| 101 |
(defun update-variable-type (var orig-type) |
| 102 |
;; FIXME! Refuse to update type of variables that are modified |
| 103 |
(when (var-set-nodes var) |
| 104 |
(return-from update-variable-type)) |
| 105 |
(let ((type (type-and (var-type var) orig-type))) |
| 106 |
(if (null type) |
| 107 |
(cmpwarn "Variable assigned a value incompatible with its type declaration.~%Variable: ~A~%Expected type: ~A~%Value type: ~A" |
| 108 |
(var-name var) |
| 109 |
(var-type var) |
| 110 |
orig-type) |
| 111 |
(loop for form in (var-read-forms var) |
| 112 |
when (and (eq (c1form-name form) 'VARIABLE) |
| 113 |
(eq var (c1form-arg 0 form))) |
| 114 |
do (setf (c1form-type form) (type-and type (c1form-primary-type form))) |
| 115 |
finally (setf (var-type var) type))))) |
| 116 |
|
| 117 |
(defun var-read-forms (var) |
| 118 |
(mapcar #'first (var-read-nodes var))) |
| 119 |
|
| 120 |
(defun assert-var-ref-value (var) |
| 121 |
(when *debug-compiler* |
| 122 |
(unless (let ((ref (var-ref var))) |
| 123 |
(or (> ref (/ most-positive-fixnum 2)) |
| 124 |
(= (var-ref var) (+ (length (var-read-nodes var)) |
| 125 |
(length (var-set-nodes var)))))) |
| 126 |
(baboon :format-control "Number of references in VAR ~A unequal to references list" |
| 127 |
:format-arguments (list var))))) |
| 128 |
|
| 129 |
(defun assert-var-not-ignored (var) |
| 130 |
(when (let ((x (var-ignorable var))) (and x (minusp x))) |
| 131 |
(cmpwarn-style "Variable ~A, declared as IGNORE, found in a lisp form." |
| 132 |
(var-name var)) |
| 133 |
(setf (var-ignorable var) nil))) |
| 134 |
|
| 135 |
(defun delete-from-read-nodes (var form) |
| 136 |
(assert-var-ref-value var) |
| 137 |
(setf (var-ref var) (1- (var-ref var)) |
| 138 |
(var-read-nodes var) (delete-form-from-node-list form (var-read-nodes var)))) |
| 139 |
|
| 140 |
(defun add-to-read-nodes (var form) |
| 141 |
(assert-var-ref-value var) |
| 142 |
(assert-var-not-ignored var) |
| 143 |
(setf (var-ref var) (1+ (var-ref var)) |
| 144 |
(var-read-nodes var) (add-form-to-node-list form (var-read-nodes var))) |
| 145 |
(when *current-function* |
| 146 |
(unless (eq *current-function* (var-function var)) |
| 147 |
(pushnew *current-function* (var-functions-reading var)) |
| 148 |
(pushnew var (fun-referenced-vars *current-function*)))) |
| 149 |
form) |
| 150 |
|
| 151 |
(defun add-to-set-nodes (var form) |
| 152 |
(assert-var-ref-value var) |
| 153 |
(assert-var-not-ignored var) |
| 154 |
(setf (var-ref var) (1+ (var-ref var)) |
| 155 |
(var-set-nodes var) (add-form-to-node-list form (var-set-nodes var))) |
| 156 |
;;(push form (var-read-nodes var)) |
| 157 |
(when *current-function* |
| 158 |
(unless (eq *current-function* (var-function var)) |
| 159 |
(pushnew *current-function* (var-functions-setting var)) |
| 160 |
(pushnew var (fun-referenced-vars *current-function*)))) |
| 161 |
form) |
| 162 |
|
| 163 |
(defun add-to-set-nodes-of-var-list (var-list form) |
| 164 |
(dolist (v var-list) |
| 165 |
(add-to-set-nodes v form)) |
| 166 |
form) |
| 167 |
|
| 168 |
(defun var-host-type (var) |
| 169 |
(case (var-kind var) |
| 170 |
((LEXICAL CLOSURE SPECIAL GLOBAL) :object) |
| 171 |
(t (var-kind var)))) |
| 172 |
|
| 173 |
(defun check-vref (var) |
| 174 |
(when (eq (var-kind var) 'LEXICAL) |
| 175 |
(when (and (zerop (var-ref var)) ;;; This field may be -1 (IGNORE). Beppe |
| 176 |
(not (var-ignorable var))) |
| 177 |
(cmpwarn-style "The variable ~s is not used." (var-name var))) |
| 178 |
(when (not (var-ref-clb var)) |
| 179 |
;; if the variable can be stored locally, set it var-kind to its type |
| 180 |
(setf (var-kind var) |
| 181 |
(if (plusp (var-ref var)) |
| 182 |
(lisp-type->host-type (var-type var)) |
| 183 |
:OBJECT))))) |
| 184 |
|
| 185 |
(defun unboxed-var-p (var) |
| 186 |
(not (eq (var-host-type var) :object))) |
| 187 |
|
| 188 |
(defun local-var-p (var) |
| 189 |
(and (not (member (var-kind var) '(LEXICAL CLOSURE SPECIAL GLOBAL))) |
| 190 |
(var-kind var))) |
| 191 |
|
| 192 |
(defun global-var-p (var) |
| 193 |
(let ((kind (var-kind var))) |
| 194 |
(or (eq kind 'global) |
| 195 |
(eq kind 'special)))) |
| 196 |
|
| 197 |
(defun useful-var-p (var) |
| 198 |
(or (plusp (var-ref var)) |
| 199 |
(global-var-p var))) |