| 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 |
(defun register-function (fun) |
| 13 |
(push fun *functions*)) |
| 14 |
|
| 15 |
(defun child-function-p (presumed-parent fun) |
| 16 |
(declare (optimize speed)) |
| 17 |
(loop for real-parent = (fun-parent fun) |
| 18 |
while real-parent |
| 19 |
do (if (eq real-parent presumed-parent) |
| 20 |
(return t) |
| 21 |
(setf fun real-parent)))) |
| 22 |
|
| 23 |
(defun compute-closure-type (fun) |
| 24 |
(declare (si::c-local)) |
| 25 |
(let ((lexical-closure-p nil)) |
| 26 |
;; it will have a full closure if it refers external non-global variables |
| 27 |
(dolist (var (fun-referenced-vars fun)) |
| 28 |
(cond ((global-var-p var)) |
| 29 |
;; ...across CB |
| 30 |
((ref-ref-ccb var) |
| 31 |
(return-from compute-closure-type 'CLOSURE)) |
| 32 |
(t |
| 33 |
(setf lexical-closure-p t)))) |
| 34 |
;; ...or if it directly calls a function |
| 35 |
(dolist (f (fun-referenced-funs fun)) |
| 36 |
(unless (child-function-p fun f) |
| 37 |
;; .. which has a full closure |
| 38 |
(case (fun-closure f) |
| 39 |
(CLOSURE (return-from compute-closure-type 'CLOSURE)) |
| 40 |
(LEXICAL (setf lexical-closure-p t))))) |
| 41 |
;; ...or the function itself is referred across CB |
| 42 |
(when lexical-closure-p |
| 43 |
(if (or (fun-ref-ccb fun) |
| 44 |
(and (fun-var fun) |
| 45 |
(plusp (var-ref (fun-var fun))))) |
| 46 |
'CLOSURE |
| 47 |
'LEXICAL)))) |
| 48 |
|
| 49 |
(defun update-fun-closure-type-many (function-list) |
| 50 |
(do ((finish nil t) |
| 51 |
(recompute nil)) |
| 52 |
(finish |
| 53 |
recompute) |
| 54 |
(dolist (f function-list) |
| 55 |
(when (update-fun-closure-type f) |
| 56 |
(setf recompute t finish nil))))) |
| 57 |
|
| 58 |
(defun prepend-new (l1 l2) |
| 59 |
(loop for f in l1 |
| 60 |
do (pushnew f l2)) |
| 61 |
l2) |
| 62 |
|
| 63 |
(defun update-fun-closure-type (fun) |
| 64 |
(let ((old-type (fun-closure fun))) |
| 65 |
(when (eq old-type 'closure) |
| 66 |
(return-from update-fun-closure-type nil)) |
| 67 |
;; This recursive algorithm is guaranteed to stop when functions |
| 68 |
;; do not change. |
| 69 |
(let ((new-type (compute-closure-type fun)) |
| 70 |
to-be-updated) |
| 71 |
;; Same type |
| 72 |
(when (eq new-type old-type) |
| 73 |
(return-from update-fun-closure-type nil)) |
| 74 |
(when (fun-global fun) |
| 75 |
(cmpnote "Function ~A is global but is closed over some variables.~%~{~A ~}" |
| 76 |
(fun-name fun) (mapcar #'var-name (fun-referenced-vars fun)))) |
| 77 |
(setf to-be-updated (append (fun-child-funs fun) (fun-referencing-funs fun))) |
| 78 |
(setf (fun-closure fun) new-type) |
| 79 |
;; All external, non-global variables become of type closure |
| 80 |
(when (eq new-type 'CLOSURE) |
| 81 |
(dolist (var (fun-referenced-vars fun)) |
| 82 |
(unless (or (global-var-p var) |
| 83 |
(eq (var-kind var) new-type)) |
| 84 |
(setf (var-ref-clb var) nil |
| 85 |
(var-ref-ccb var) t |
| 86 |
(var-kind var) 'CLOSURE |
| 87 |
to-be-updated |
| 88 |
(prepend-new (var-functions-reading var) |
| 89 |
(prepend-new (var-functions-setting var) |
| 90 |
to-be-updated))))) |
| 91 |
(dolist (f (fun-referenced-funs fun)) |
| 92 |
(setf (fun-ref-ccb f) t))) |
| 93 |
;; If the status of some of the children changes, we have |
| 94 |
;; to recompute the closure type. |
| 95 |
(when (update-fun-closure-type-many to-be-updated) |
| 96 |
(update-fun-closure-type fun)) |
| 97 |
t))) |
| 98 |
|
| 99 |
(defun local-function-ref (fname &optional build-object) |
| 100 |
(multiple-value-bind (fun cfb unw) |
| 101 |
(cmp-env-search-function fname) |
| 102 |
(declare (ignore unw)) |
| 103 |
(when fun |
| 104 |
(when (functionp fun) |
| 105 |
(when build-object |
| 106 |
;; Macro definition appears in #'.... This should not happen. |
| 107 |
(cmperr "The name of a macro ~A was found in special form FUNCTION." fname)) |
| 108 |
(return-from local-function-ref nil)) |
| 109 |
(incf (fun-ref fun)) |
| 110 |
(if build-object |
| 111 |
(setf (fun-ref-ccb fun) t) |
| 112 |
(let ((caller *current-function*)) |
| 113 |
(when (and caller |
| 114 |
(not (member fun (fun-referenced-funs caller) :test #'eq))) |
| 115 |
(push fun (fun-referenced-funs caller)) |
| 116 |
(push caller (fun-referencing-funs fun))))) |
| 117 |
;; we introduce a variable to hold the funob |
| 118 |
(let ((var (fun-var fun))) |
| 119 |
(when (and cfb build-object) |
| 120 |
(setf (var-ref-clb var) t) |
| 121 |
(when (not (eq (var-kind var) 'CLOSURE)) |
| 122 |
(setf (var-kind var) 'LEXICAL))))) |
| 123 |
fun)) |
| 124 |
|
| 125 |
(defun fun-needs-narg (fun) |
| 126 |
(not (fun-fixed-narg fun))) |
| 127 |
|
| 128 |
(defun fun-fixed-narg (fun) |
| 129 |
"Returns true if the function has a fixed number of arguments and it is not a closure. |
| 130 |
The function thus belongs to the type of functions that ecl_make_cfun accepts." |
| 131 |
(let (narg) |
| 132 |
(and (not (eq (fun-closure fun) 'CLOSURE)) |
| 133 |
(= (fun-minarg fun) (setf narg (fun-maxarg fun))) |
| 134 |
(<= narg si:c-arguments-limit) |
| 135 |
narg))) |
| 136 |
|
| 137 |
(defun add-to-fun-referenced-vars (fun var-list) |
| 138 |
(loop with new-vars = (fun-referenced-vars fun) |
| 139 |
with locals = (fun-local-vars fun) |
| 140 |
with change = nil |
| 141 |
for v in var-list |
| 142 |
when (and (not (member v locals :test #'eq)) |
| 143 |
(not (member v new-vars :test #'eq))) |
| 144 |
do (setf change t new-vars (cons v new-vars)) |
| 145 |
finally (when change |
| 146 |
(setf (fun-referenced-vars fun) new-vars) |
| 147 |
(return t)))) |
| 148 |
|
| 149 |
(defun add-to-fun-referenced-funs (fun fun-list) |
| 150 |
(loop with new-funs = (fun-referenced-funs fun) |
| 151 |
with change = nil |
| 152 |
for f in fun-list |
| 153 |
when (and (not (eq fun f)) |
| 154 |
(not (member f new-funs :test #'eq)) |
| 155 |
(not (child-function-p fun f))) |
| 156 |
do (setf change t |
| 157 |
new-funs (cons f new-funs) |
| 158 |
(fun-referencing-funs f) (cons fun (fun-referencing-funs f))) |
| 159 |
finally (when change |
| 160 |
(setf (fun-referenced-funs fun) new-funs) |
| 161 |
(return t)))) |
| 162 |
|
| 163 |
;;; searches for a (FUNCTION-BLOCK-NAME ...) declaration |
| 164 |
(defun function-block-name-declaration (declarations) |
| 165 |
(loop for i in declarations |
| 166 |
do (when (and (consp i) (eql (car i) 'si:function-block-name)) |
| 167 |
(let ((name (second i)) |
| 168 |
(rest (cddr i))) |
| 169 |
(unless (and (symbolp name) (null rest)) |
| 170 |
(cmperr "Invalid ~s declaration:~%~s" 'si:function-block-name i)) |
| 171 |
(return name))))) |
| 172 |
|
| 173 |
(defun exported-fname (name) |
| 174 |
(let (cname) |
| 175 |
(if (and (symbolp name) (setf cname (si:get-sysprop name 'Lfun))) |
| 176 |
(values cname t) |
| 177 |
(values (next-cfun "L~D~A" name) nil)))) |
| 178 |
|
| 179 |
(defun function-may-have-side-effects (fname) |
| 180 |
(not (si:get-sysprop fname 'no-side-effects))) |
| 181 |
|
| 182 |
(defun function-may-change-sp (fname) |
| 183 |
(not (or (si:get-sysprop fname 'no-side-effects) |
| 184 |
(si:get-sysprop fname 'no-sp-change)))) |