| 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) 2023, Daniel Kochmański |
| 6 |
;;;; |
| 7 |
;;;; See the file 'LICENSE' for the copyright details. |
| 8 |
;;;; |
| 9 |
|
| 10 |
(in-package #:compiler) |
| 11 |
|
| 12 |
;;; FIXME functions declared as SI::C-LOCAL can't be called from the stack |
| 13 |
;;; because they are not installed in the environment. That means that if we |
| 14 |
;;; have such function and call it with too many arguments it will be |
| 15 |
;;; "undefined". This is a defect (but not a regression). -- jd 2023-06-16 |
| 16 |
|
| 17 |
;;; |
| 18 |
;;; c2fcall: |
| 19 |
;;; |
| 20 |
;;; FUN: the function to be called |
| 21 |
;;; ARGS: the list of arguments |
| 22 |
;;; FUN-VAL depends on the particular call type |
| 23 |
;;; :LOCAL structure FUN [see cmprefs.lsp] |
| 24 |
;;; :GLOBAL function name [symbol or (SETF symbol)] |
| 25 |
;;; :UNKNOWN the value NIL |
| 26 |
;;; CALL-TYPE: (member :LOCAL :GLOBAL :UKNOWN) |
| 27 |
;;; |
| 28 |
(defun c2fcall (c1form fun args fun-val call-type) |
| 29 |
(if (> (length args) si:c-arguments-limit) |
| 30 |
(ecase call-type |
| 31 |
(:local (c2call-local-stack c1form fun-val args)) |
| 32 |
((:global :unknown) (c2call-global-stack c1form fun args nil))) |
| 33 |
(with-inline-blocks () |
| 34 |
(ecase call-type |
| 35 |
(:local (c2call-local c1form fun-val args)) |
| 36 |
(:global (c2call-global c1form fun-val args)) |
| 37 |
(:unknown (c2call-unknown c1form fun args)))))) |
| 38 |
|
| 39 |
(defun c2mcall (c1form form args fun-val call-type) |
| 40 |
(declare (ignore fun-val call-type)) |
| 41 |
(c2call-global-stack c1form form args t)) |
| 42 |
|
| 43 |
;;; |
| 44 |
;;; c2call-global-stack: |
| 45 |
;;; |
| 46 |
;;; This is the most generic way of calling functions. First we push them on |
| 47 |
;;; the stack, and then we apply from the stack frame. Other variants call |
| 48 |
;;; inline-args and put results directly in the function call. |
| 49 |
;;; |
| 50 |
(defun c2call-global-stack (c1form form args values-p) |
| 51 |
(declare (ignore c1form)) |
| 52 |
(with-stack-frame (frame) |
| 53 |
(let ((loc (emit-inline-form form args))) |
| 54 |
(let ((*destination* (if values-p 'VALUEZ 'LEAVE))) |
| 55 |
(dolist (arg args) |
| 56 |
(c2expr* arg) |
| 57 |
(if values-p |
| 58 |
(wt-nl "ecl_stack_frame_push_values(" frame ");") |
| 59 |
(wt-nl "ecl_stack_frame_push(" frame ",value0);")))) |
| 60 |
(unwind-exit (call-global-stack-loc nil loc))))) |
| 61 |
|
| 62 |
;;; |
| 63 |
;;; c2call-local-stack: |
| 64 |
;;; |
| 65 |
;;; First push the arguments on the stack and then call the function |
| 66 |
;;; directly. This is used for calls to local functions that take |
| 67 |
;;; more arguments than C-ARGUMENTS-LIMIT. |
| 68 |
;;; |
| 69 |
(defun c2call-local-stack (c1form fun args) |
| 70 |
(declare (type fun fun)) |
| 71 |
(with-stack-frame (frame) |
| 72 |
(let ((evaluated-args (mapcar #'(lambda (arg) |
| 73 |
(let ((*destination* (make-temp-var t))) |
| 74 |
(c2expr* arg) |
| 75 |
(wt-nl "ecl_stack_frame_push(" frame "," *destination* ");") |
| 76 |
*destination*)) |
| 77 |
args))) |
| 78 |
(unwind-exit (call-local-stack-loc fun |
| 79 |
evaluated-args |
| 80 |
(c1form-primary-type c1form)))))) |
| 81 |
|
| 82 |
;;; |
| 83 |
;;; c2call-global: |
| 84 |
;;; |
| 85 |
;;; LOC: the location of the function object or NIL |
| 86 |
;;; ARGS: the list of arguments |
| 87 |
;;; |
| 88 |
(defun c2call-global (c1form fname args) |
| 89 |
(let ((fun (find fname *global-funs* :key #'fun-name :test #'same-fname-p))) |
| 90 |
(when (and fun (c2try-tail-recursive-call fun args)) |
| 91 |
(return-from c2call-global)) |
| 92 |
(unwind-exit (call-global-loc fname fun |
| 93 |
(inline-args args) |
| 94 |
(type-and (c1form-primary-type c1form) |
| 95 |
(loc-lisp-type *destination*)))))) |
| 96 |
|
| 97 |
;;; |
| 98 |
;;; c2call-local: |
| 99 |
;;; |
| 100 |
;;; FUN: the function object |
| 101 |
;;; ARGS: the list of arguments |
| 102 |
;;; |
| 103 |
(defun c2call-local (c1form fun args) |
| 104 |
(declare (type fun fun)) |
| 105 |
(unless (c2try-tail-recursive-call fun args) |
| 106 |
(unwind-exit (call-loc (fun-name fun) fun |
| 107 |
(inline-args args) |
| 108 |
(c1form-primary-type c1form))))) |
| 109 |
|
| 110 |
(defun c2call-unknown (c1form form args) |
| 111 |
(declare (ignore c1form)) |
| 112 |
(let* ((form-type (c1form-primary-type form)) |
| 113 |
(function-p (and (subtypep form-type 'function *cmp-env*) |
| 114 |
(policy-assume-right-type))) |
| 115 |
(loc (emit-inline-form form args)) |
| 116 |
(args (inline-args args))) |
| 117 |
(unwind-exit (call-unknown-global-loc loc args function-p)))) |
| 118 |
|
| 119 |
;;; Tail-recursion optimization for a function F is possible only if |
| 120 |
;;; 1. F receives only required parameters, and |
| 121 |
;;; 2. no required parameter of F is enclosed in a closure. |
| 122 |
;;; |
| 123 |
;;; A recursive call (F e1 ... en) may be replaced by a loop only if |
| 124 |
;;; 1. F is not declared as NOTINLINE, |
| 125 |
;;; 2. n is equal to the number of required parameters of F, |
| 126 |
;;; 3. the form is a normal function call (i.e. args are not ARGS-PUSHED), |
| 127 |
;;; 4. (F e1 ... en) is not surrounded by a form that causes dynamic |
| 128 |
;;; binding (such as LET, LET*, PROGV), |
| 129 |
;;; 5. (F e1 ... en) is not surrounded by a form that that pushes a frame |
| 130 |
;;; onto the frame-stack (such as BLOCK and TAGBODY whose tags are |
| 131 |
;;; enclosed in a closure, and CATCH), |
| 132 |
|
| 133 |
(defun tail-recursion-possible () |
| 134 |
(dolist (ue *unwind-exit* |
| 135 |
(baboon :format-control "tail-recursion-possible: should never return.")) |
| 136 |
(cond ((eq ue *tail-recursion-mark*) |
| 137 |
(return t)) |
| 138 |
((or (eq ue 'BDS-BIND) (eq ue 'FRAME)) |
| 139 |
(return nil)) |
| 140 |
((or (consp ue) (labelp ue) (eq ue 'IHS-ENV))) |
| 141 |
(t (baboon :format-control "tail-recursion-possible: unexpected situation."))))) |
| 142 |
|
| 143 |
(defun c2try-tail-recursive-call (fun args) |
| 144 |
(when (and *tail-recursion-info* |
| 145 |
(eq fun (first *tail-recursion-info*)) |
| 146 |
(eq *exit* 'LEAVE) |
| 147 |
(tail-recursion-possible) |
| 148 |
(inline-possible (fun-name fun)) |
| 149 |
(= (length args) (length (rest *tail-recursion-info*)))) |
| 150 |
(with-exit-label (*exit*) |
| 151 |
(let ((*destination* 'TRASH)) |
| 152 |
;; We do not provide any C2FORM. |
| 153 |
(c2psetq nil (cdr *tail-recursion-info*) args))) |
| 154 |
(unwind-jump *tail-recursion-mark*) |
| 155 |
(cmpdebug "Tail-recursive call of ~s was replaced by iteration." (fun-name fun)) |
| 156 |
t)) |
| 157 |
|
| 158 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 159 |
;;; |
| 160 |
;;; CALL LOCATIONS |
| 161 |
;;; |
| 162 |
|
| 163 |
;;; |
| 164 |
;;; call-global-stack-loc |
| 165 |
;;; |
| 166 |
;;; FNAME: the name of the function or NIL |
| 167 |
;;; LOC: the location containing function |
| 168 |
;;; |
| 169 |
(defun call-global-stack-loc (fname loc) |
| 170 |
`(CALL-GLOBAL-STACK ,loc ,fname)) |
| 171 |
|
| 172 |
;;; |
| 173 |
;;; call-local-stack-loc |
| 174 |
;;; |
| 175 |
;;; FUN: a function object |
| 176 |
;;; ARGS: a list of INLINED-ARGs |
| 177 |
;;; TYPE: the type to which the output is coerced |
| 178 |
;;; |
| 179 |
(defun call-local-stack-loc (fun args type) |
| 180 |
`(CALL-LOCAL-STACK ,fun ,(coerce-args args) ,type)) |
| 181 |
|
| 182 |
;;; |
| 183 |
;;; call-loc |
| 184 |
;;; |
| 185 |
;;; FNAME: the name of the function |
| 186 |
;;; FUN: a function object |
| 187 |
;;; ARGS: a list of INLINED-ARGs |
| 188 |
;;; TYPE: the type to which the output is coerced |
| 189 |
;;; |
| 190 |
(defun call-loc (fname fun args type) |
| 191 |
(declare (ignore fname)) |
| 192 |
`(CALL-NORMAL ,fun ,(coerce-args args) ,type)) |
| 193 |
|
| 194 |
;;; |
| 195 |
;;; call-global: |
| 196 |
;;; FNAME: the name of the function |
| 197 |
;;; FUN: either a function object or NIL |
| 198 |
;;; ARGS: a list of typed locs with arguments |
| 199 |
;;; TYPE: the type to which the output is coerced |
| 200 |
;;; |
| 201 |
(defun call-global-loc (fname fun args type) |
| 202 |
|
| 203 |
;; Check whether it is a global function that we cannot call directly. |
| 204 |
(when (not (inline-possible fname)) |
| 205 |
(return-from call-global-loc |
| 206 |
(call-unknown-global-fun fname args))) |
| 207 |
|
| 208 |
;; Try with a function that has a C-INLINE expansion |
| 209 |
(ext:when-let ((inline-loc (apply-inliner fname type args))) |
| 210 |
(return-from call-global-loc inline-loc)) |
| 211 |
|
| 212 |
;; Call to a function defined in the same file. Direct calls are only emitted |
| 213 |
;; for low or neutral values of DEBUG, that is DEBUG < 2. |
| 214 |
(when (and fun (policy-use-direct-C-call)) |
| 215 |
(return-from call-global-loc |
| 216 |
(call-loc fname fun args type))) |
| 217 |
|
| 218 |
;; Call to a global (SETF ...) function |
| 219 |
(when (not (symbolp fname)) |
| 220 |
(return-from call-global-loc |
| 221 |
(call-unknown-global-fun fname args))) |
| 222 |
|
| 223 |
;; Call to a function whose C language function name is known because it |
| 224 |
;; belongs to the runtime. |
| 225 |
(multiple-value-bind (found fd minarg maxarg) |
| 226 |
(si:mangle-name fname t) |
| 227 |
(when found |
| 228 |
(return-from call-global-loc |
| 229 |
(call-exported-function-loc fname args fd minarg maxarg t type)))) |
| 230 |
|
| 231 |
;; Call to a function whose C language function name is known because it has |
| 232 |
;; been proclaimed so. |
| 233 |
(when (policy-use-direct-C-call) |
| 234 |
(ext:when-let ((fd (si:get-sysprop fname 'Lfun))) |
| 235 |
(multiple-value-bind (minarg maxarg found) (get-proclaimed-narg fname) |
| 236 |
(unless found |
| 237 |
;; Without knowing the number of arguments we cannot call the C |
| 238 |
;; function. When compiling ECL itself, we get this information |
| 239 |
;; through si::mangle-name from symbols_list.h for core functions |
| 240 |
;; defined in Lisp code. |
| 241 |
#+ecl-min |
| 242 |
(let (ignored) |
| 243 |
(multiple-value-setq (found ignored minarg maxarg) |
| 244 |
(si:mangle-name fname))) |
| 245 |
(unless found |
| 246 |
(cmperr "Can not call the function ~A using its exported C name ~A ~ |
| 247 |
because its function type has not been proclaimed." |
| 248 |
fname fd))) |
| 249 |
(return-from call-global-loc |
| 250 |
(call-exported-function-loc fname args fd minarg maxarg |
| 251 |
(si:mangle-name fname) type))))) |
| 252 |
|
| 253 |
(call-unknown-global-fun fname args)) |
| 254 |
|
| 255 |
(defun call-exported-function-loc (fname args fun-c-name minarg maxarg in-core type) |
| 256 |
(unless in-core |
| 257 |
;; We only write declarations for functions which are not in lisp_external.h |
| 258 |
(multiple-value-bind (val declared) |
| 259 |
(gethash fun-c-name *compiler-declared-globals*) |
| 260 |
(declare (ignore val)) |
| 261 |
(unless declared |
| 262 |
(if (= maxarg minarg) |
| 263 |
(progn |
| 264 |
(wt-nl-h "extern cl_object " fun-c-name "(") |
| 265 |
(dotimes (i maxarg) |
| 266 |
(when (> i 0) (wt-h1 ",")) |
| 267 |
(wt-h1 "cl_object")) |
| 268 |
(wt-h1 ");")) |
| 269 |
(progn |
| 270 |
(wt-nl-h "extern cl_object " fun-c-name "(cl_narg") |
| 271 |
(dotimes (i (min minarg si:c-arguments-limit)) |
| 272 |
(wt-h1 ",cl_object")) |
| 273 |
(wt-h1 ",...);"))) |
| 274 |
(setf (gethash fun-c-name *compiler-declared-globals*) 1)))) |
| 275 |
(let ((fun (make-fun :name fname :global t :cfun fun-c-name :lambda 'NIL |
| 276 |
:minarg minarg :maxarg maxarg))) |
| 277 |
(call-loc fname fun args type))) |
| 278 |
|
| 279 |
;;; |
| 280 |
;;; call-unknown-global-loc |
| 281 |
;;; |
| 282 |
;;; LOC: the location containing the function or NIL |
| 283 |
;;; ARGS: a list of INLINED-ARGs |
| 284 |
;;; FUNCTION-P: true when we can assume that LOC is the function |
| 285 |
;;; |
| 286 |
(defun call-unknown-global-loc (loc args function-p) |
| 287 |
`(CALL-INDIRECT ,loc ,(coerce-args args) nil ,function-p)) |
| 288 |
|
| 289 |
;;; |
| 290 |
;;; call-unknown-global-fun |
| 291 |
;;; |
| 292 |
;;; FNAME: the name of the global function |
| 293 |
;;; ARGS: a list of INLINED-ARGs |
| 294 |
;;; |
| 295 |
(defun call-unknown-global-fun (fname args) |
| 296 |
`(CALL-INDIRECT (FDEFINITION ,fname) ,(coerce-args args) ,fname t)) |
| 297 |
|
| 298 |
#+ (or) |
| 299 |
;;; This version is correct but unnecessarily slow - it goes through |
| 300 |
;;; ecl_function_dispatch. wt-fdefinition handles all proper names. |
| 301 |
(defun call-unknown-global-fun (fname args) |
| 302 |
`(CALL-INDIRECT ,(get-object fname :test #'equal) ,(coerce-args args) ,fname nil)) |