| 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 |
(defun not-a-closure-p (fname) |
| 13 |
(declare (si::c-local)) |
| 14 |
(not (and (fboundp fname) |
| 15 |
(nth-value 1 (function-lambda-expression (fdefinition fname)))))) |
| 16 |
|
| 17 |
(defun function-form-p (form) |
| 18 |
(declare (si::c-local)) |
| 19 |
(if (and (consp form) |
| 20 |
(eq (first form) 'CL:FUNCTION)) |
| 21 |
(prog1 t |
| 22 |
(check-args-number 'CL:FUNCTION (rest form) 1 1)) |
| 23 |
nil)) |
| 24 |
|
| 25 |
(defun lambda-form-p (form) |
| 26 |
(if (function-form-p form) |
| 27 |
(let ((fval (second form))) |
| 28 |
(and (consp fval) |
| 29 |
(member (car fval) '(CL:LAMBDA EXT:LAMBDA-BLOCK)) |
| 30 |
t)) |
| 31 |
nil)) |
| 32 |
|
| 33 |
(defun unoptimized-funcall (fun arguments) |
| 34 |
(let ((fun-form (c1expr fun)) |
| 35 |
(fun-args (c1args* arguments))) |
| 36 |
(make-c1form* 'FCALL |
| 37 |
:sp-change t |
| 38 |
:side-effects t |
| 39 |
:args fun-form fun-args nil :unknown))) |
| 40 |
|
| 41 |
(defun optimized-lambda-call (lambda-form arguments apply-p) |
| 42 |
(multiple-value-bind (bindings body) |
| 43 |
(transform-funcall/apply-into-let* lambda-form arguments apply-p) |
| 44 |
`(let* ,bindings ,@body))) |
| 45 |
|
| 46 |
(defun try-optimized-lambda-call (fun args apply-p) |
| 47 |
(if (lambda-form-p fun) |
| 48 |
(optimized-lambda-call (second fun) args apply-p) |
| 49 |
nil)) |
| 50 |
|
| 51 |
(defun try-macro-expression-call (fun args) |
| 52 |
(unless (consp fun) |
| 53 |
(return-from try-macro-expression-call nil)) |
| 54 |
(let* ((name (first fun)) |
| 55 |
(fd (and (symbolp name) |
| 56 |
;; We do not want to macroexpand 'CL:THE |
| 57 |
(not (eq name 'CL:THE)) |
| 58 |
(cmp-macro-function name)))) |
| 59 |
(if fd |
| 60 |
(c1funcall (list* (cmp-expand-macro fd fun) args)) |
| 61 |
nil))) |
| 62 |
|
| 63 |
(defun try-function-special-call (fun args) |
| 64 |
(unless (function-form-p fun) |
| 65 |
(return-from try-function-special-call nil)) |
| 66 |
(let ((fname (second fun))) |
| 67 |
(if (si:valid-function-name-p fname) |
| 68 |
(c1call fname args nil) |
| 69 |
(cmperr "Malformed function name: ~A." fname)))) |
| 70 |
|
| 71 |
(defun c1funcall (args) |
| 72 |
(check-args-number 'CL:FUNCALL args 1) |
| 73 |
(destructuring-bind (fun . arguments) args |
| 74 |
(or ;; (FUNCALL (LAMBDA ...) ...) or (FUNCALL (EXT:LAMBDA-BLOCK ...) ...) |
| 75 |
;; (FUNCALL #'(LAMBDA ...) ...) or (FUNCALL #'(EXT:LAMBDA-BLOCK ...) ...) |
| 76 |
(try-optimized-lambda-call fun arguments nil) |
| 77 |
;; (FUNCALL macro-expression ...) |
| 78 |
(try-macro-expression-call fun arguments) |
| 79 |
;; (FUNCALL (FUNCTION function-name) ...) |
| 80 |
(try-function-special-call fun arguments) |
| 81 |
;; (FUNCALL lisp-expression ...) or (FUNCALL atomic-expression ...) |
| 82 |
(unoptimized-funcall fun arguments)))) |
| 83 |
|
| 84 |
;;; Further optimization opportunities: |
| 85 |
;;; - expand macros |
| 86 |
;;; - expand compiler macros |
| 87 |
;;; - rely on the function proclamations |
| 88 |
;;; - rely on the inferred argument type |
| 89 |
(defun try-spill-values-funcall (fun arguments) |
| 90 |
(flet ((values-form (arg) |
| 91 |
(setq arg (chk-symbol-macrolet arg)) |
| 92 |
(if (or (atom arg) |
| 93 |
(eq (car arg) 'CL:VALUES)) |
| 94 |
arg |
| 95 |
(return-from try-spill-values-funcall nil)))) |
| 96 |
(loop for arg in arguments |
| 97 |
for val = (values-form arg) |
| 98 |
if (atom val) |
| 99 |
collect val into values |
| 100 |
else |
| 101 |
append (cdr val) into values |
| 102 |
finally (return (c1funcall (list* fun values)))))) |
| 103 |
|
| 104 |
(defun try-macro-expression-mvc (fun arguments) |
| 105 |
(unless (consp fun) |
| 106 |
(return-from try-macro-expression-mvc nil)) |
| 107 |
(let* ((name (first fun)) |
| 108 |
(fd (and (symbolp name) |
| 109 |
;; We do not want to macroexpand 'CL:THE |
| 110 |
(not (eq name 'CL:THE)) |
| 111 |
(cmp-macro-function name)))) |
| 112 |
(if fd |
| 113 |
(c1multiple-value-call (list* (cmp-expand-macro fd fun) arguments)) |
| 114 |
nil))) |
| 115 |
|
| 116 |
(defun try-function-special-mvc (fun arguments) |
| 117 |
(unless (function-form-p fun) |
| 118 |
(return-from try-function-special-mvc nil)) |
| 119 |
(let ((fname (second fun))) |
| 120 |
(if (si:valid-function-name-p fname) |
| 121 |
(ext:if-let ((funob (local-function-ref fname))) |
| 122 |
(make-c1form* 'MCALL |
| 123 |
:sp-change t ; conservative estimate |
| 124 |
:side-effects t ; conservative estimate |
| 125 |
:args (c1expr fun) (c1args* arguments) funob :local) |
| 126 |
(make-c1form* 'MCALL |
| 127 |
:sp-change (function-may-change-sp fname) |
| 128 |
:side-effects (function-may-have-side-effects fname) |
| 129 |
:args (c1expr fun) (c1args* arguments) fname :global)) |
| 130 |
(if (lambda-form-p fun) |
| 131 |
nil |
| 132 |
(cmperr "Malformed function name: ~A." fname))))) |
| 133 |
|
| 134 |
(defun unoptimized-mvc (fun arguments) |
| 135 |
(let ((fun-form (c1expr fun)) |
| 136 |
(fun-args (c1args* arguments))) |
| 137 |
(make-c1form* 'MCALL |
| 138 |
:sp-change t |
| 139 |
:side-effects t |
| 140 |
:args fun-form fun-args nil :unknown))) |
| 141 |
|
| 142 |
(defun c1multiple-value-call (args &aux forms) |
| 143 |
(check-args-number 'CL:MULTIPLE-VALUE-CALL args 1) |
| 144 |
(destructuring-bind (fun . arguments) args |
| 145 |
(or ;; (M-V-C expression (VALUES A B) (VALUES X Y Z) ...) |
| 146 |
;; => (FUNCALL expression A B X Y Z ...) |
| 147 |
(try-spill-values-funcall fun arguments) |
| 148 |
;; (M-V-C macro-expression ...) |
| 149 |
(try-macro-expression-mvc fun arguments) |
| 150 |
;; (M-V-C (FUNCTION function-name) ...) |
| 151 |
(try-function-special-mvc fun arguments) |
| 152 |
;; (M-V-C lisp-expression ...) or (M-V-C atomic-expression ...) |
| 153 |
(unoptimized-mvc fun arguments)))) |
| 154 |
|
| 155 |
(defun c1apply (args) |
| 156 |
(check-args-number 'CL:APPLY args 2) |
| 157 |
(flet ((default-apply (fun arguments) |
| 158 |
(let ((form (c1funcall (list* '(function CL:APPLY) fun arguments)))) |
| 159 |
(when (function-form-p fun) |
| 160 |
(let* ((fname (second fun)) |
| 161 |
(type (get-return-type fname))) |
| 162 |
(when type |
| 163 |
(setf (c1form-type form) type)))) |
| 164 |
form)) |
| 165 |
(last-argument-is-frame-p (arguments) |
| 166 |
;; Uses frames instead of lists as last argument. ;; gross hack! |
| 167 |
(eql (first (last arguments)) 'clos:.combined-method-args.))) |
| 168 |
(destructuring-bind (fun . arguments) args |
| 169 |
(if (last-argument-is-frame-p arguments) |
| 170 |
(default-apply fun arguments) |
| 171 |
(or (try-optimized-lambda-call fun arguments t) |
| 172 |
(default-apply fun arguments)))))) |
| 173 |
|
| 174 |
(defun c1call (fname args macros-allowed &aux fd success can-inline) |
| 175 |
(add-fname fname) |
| 176 |
(cond ((setq fd (local-function-ref fname)) |
| 177 |
(c1call-local fname fd args)) |
| 178 |
((and macros-allowed ; macrolet |
| 179 |
(setq fd (cmp-env-search-macro fname))) |
| 180 |
(cmp-expand-macro fd (list* fname args))) |
| 181 |
((and (setq can-inline (inline-possible fname)) |
| 182 |
(setq fd (cmp-compiler-macro-function fname)) |
| 183 |
(progn |
| 184 |
(multiple-value-setq (fd success) |
| 185 |
(cmp-expand-compiler-macro fd fname args)) |
| 186 |
success)) |
| 187 |
fd) |
| 188 |
((and can-inline |
| 189 |
(progn |
| 190 |
(multiple-value-setq (fd success) |
| 191 |
(clos-compiler-macro-expand fname args)) |
| 192 |
success)) |
| 193 |
fd) |
| 194 |
((and macros-allowed ; global macro |
| 195 |
(setq fd (macro-function fname))) |
| 196 |
(cmp-expand-macro fd (list* fname args))) |
| 197 |
((and (setq can-inline (declared-inline-p fname)) |
| 198 |
(plusp *inline-max-depth*) |
| 199 |
(<= (cmp-env-optimization 'space) 1)) |
| 200 |
(cond ((and (setq fd (find fname *global-funs* :key #'fun-name :test #'same-fname-p)) |
| 201 |
(not (fun-closure fd))) |
| 202 |
(cmpnote "Inlining ~a" fname) |
| 203 |
(inline-local (fun-lambda-expression fd) fd args)) |
| 204 |
((and (function-form-p can-inline) |
| 205 |
(not-a-closure-p fname)) |
| 206 |
(let ((*inline-max-depth* (1- *inline-max-depth*))) |
| 207 |
(cmpnote "Inlining ~a" fname) |
| 208 |
`(funcall ,can-inline ,@args))) |
| 209 |
(t (c1call-global fname args)))) |
| 210 |
(t (c1call-global fname args)))) |
| 211 |
|
| 212 |
(defun inline-local (lambda fun args) |
| 213 |
(declare (si::c-local)) |
| 214 |
(let ((*inline-max-depth* (1- *inline-max-depth*)) |
| 215 |
(setjmps *setjmps*) |
| 216 |
(*cmp-env* (cmp-env-copy))) |
| 217 |
;; To inline the function, we transform it into a let* statement. |
| 218 |
(multiple-value-bind (bindings body) |
| 219 |
(transform-funcall/apply-into-let* lambda args nil) |
| 220 |
(multiple-value-bind (let-vars let-inits specials other-decls body) |
| 221 |
(process-let-bindings 'LET* bindings body) |
| 222 |
;; We have to compile the function body in the same |
| 223 |
;; environment in which the function was defined to get |
| 224 |
;; inlining of closures right. |
| 225 |
(let ((*cmp-env* (cmp-env-copy (fun-cmp-env fun)))) |
| 226 |
(mapc #'cmp-env-register-var let-vars) |
| 227 |
(process-let-body 'LET* let-vars let-inits specials other-decls body setjmps)))))) |
| 228 |
|
| 229 |
(defun c1call-local (fname fun args) |
| 230 |
(declare (si::c-local)) |
| 231 |
(let ((lambda (fun-lambda-expression fun))) |
| 232 |
(when (and lambda |
| 233 |
(declared-inline-p fname) |
| 234 |
(plusp *inline-max-depth*)) |
| 235 |
(return-from c1call-local (inline-local lambda fun args)))) |
| 236 |
(make-c1form* 'FCALL |
| 237 |
:sp-change t ; conservative estimate |
| 238 |
:side-effects t ; conservative estimate |
| 239 |
:args nil (c1args* args) fun :local)) |
| 240 |
|
| 241 |
(defun c1call-global (fname args) |
| 242 |
(let ((forms (c1args* args))) |
| 243 |
;; If all arguments are constants, try to precompute the function value. We |
| 244 |
;; abort when the function signals an error or the value is not printable. |
| 245 |
(ext:when-let ((value (c1call-constant-fold fname forms))) |
| 246 |
(return-from c1call-global value)) |
| 247 |
;; Otherwise emit a global function call. |
| 248 |
(make-c1form* 'FCALL |
| 249 |
:sp-change (function-may-change-sp fname) |
| 250 |
:side-effects (function-may-have-side-effects fname) |
| 251 |
:args (c1expr `(function ,fname)) forms fname :global))) |
| 252 |
|
| 253 |
(defun c1call-constant-fold (fname forms) |
| 254 |
(when (and (si:get-sysprop fname 'pure) |
| 255 |
(policy-evaluate-forms) |
| 256 |
(inline-possible fname)) |
| 257 |
(handler-case |
| 258 |
(loop with all-values = '() |
| 259 |
with constant-p |
| 260 |
with v |
| 261 |
for form in forms |
| 262 |
do (if (multiple-value-setq (constant-p v) |
| 263 |
(c1form-constant-p form)) |
| 264 |
(push v all-values) |
| 265 |
(return nil)) |
| 266 |
finally |
| 267 |
(return |
| 268 |
(let ((results (multiple-value-list (apply fname (nreverse all-values))))) |
| 269 |
(if (endp (rest results)) |
| 270 |
(c1constant-value (first results)) |
| 271 |
(let ((results (mapcar #'c1constant-value results))) |
| 272 |
(when (every #'identity results) |
| 273 |
(make-c1form* 'CL:VALUES :args results))))))) |
| 274 |
(error (c) |
| 275 |
(cmpdebug "Can't constant-fold ~s ~s: ~a~%" fname forms c))))) |
| 276 |
|
| 277 |
;;; Transform a (funcall lambda-form arguments) or (apply lambda-form |
| 278 |
;;; arguments) expression into an equivalent let* statement. Returns |
| 279 |
;;; the bindings and body as two values. |
| 280 |
(defun transform-funcall/apply-into-let* (lambda-form arguments apply-p |
| 281 |
&aux apply-list apply-var |
| 282 |
let-vars extra-stmts all-keys) |
| 283 |
(when (eq (first lambda-form) 'ext:lambda-block) |
| 284 |
(setf lambda-form (macroexpand-1 lambda-form))) |
| 285 |
(multiple-value-bind (requireds optionals rest key-flag keywords |
| 286 |
allow-other-keys aux-vars) |
| 287 |
(cmp-process-lambda-list (second lambda-form)) |
| 288 |
(when apply-p |
| 289 |
(setf apply-list (first (last arguments)) |
| 290 |
apply-var (gensym) |
| 291 |
arguments (butlast arguments))) |
| 292 |
;; 1. check number of arguments |
| 293 |
(let* ((n-args-wanted-min (first requireds)) |
| 294 |
(n-args-wanted-max (if (or key-flag allow-other-keys rest) |
| 295 |
call-arguments-limit |
| 296 |
(+ (first requireds) (first optionals)))) |
| 297 |
(apply-constant-args-p (and apply-p (constantp apply-list) |
| 298 |
(listp (ext:constant-form-value apply-list)))) |
| 299 |
(n-args-got-min (if apply-constant-args-p |
| 300 |
(+ (length arguments) |
| 301 |
(length (ext:constant-form-value apply-list))) |
| 302 |
(length arguments))) |
| 303 |
(n-args-got-max (cond ((and apply-p (not apply-constant-args-p)) |
| 304 |
nil) ; unknown maximum number of arguments |
| 305 |
(t n-args-got-min)))) |
| 306 |
(when (and n-args-got-max (< n-args-got-max n-args-wanted-min)) |
| 307 |
(too-few-args lambda-form n-args-wanted-min n-args-got-min)) |
| 308 |
(when (> n-args-got-min n-args-wanted-max) |
| 309 |
(too-many-args lambda-form n-args-wanted-max n-args-got-max))) |
| 310 |
;; 2. construct forms to evaluate arguments in order |
| 311 |
(setf arguments (copy-list arguments)) |
| 312 |
(do ((scan arguments (cdr scan))) |
| 313 |
((endp scan)) |
| 314 |
(let ((form (first scan))) |
| 315 |
(unless (constantp form) |
| 316 |
(let ((aux-var (gensym))) |
| 317 |
(push `(,aux-var ,form) let-vars) |
| 318 |
(setf (car scan) aux-var))))) |
| 319 |
(when apply-var |
| 320 |
(push `(,apply-var ,apply-list) let-vars)) |
| 321 |
;; 3. process required parameters |
| 322 |
(dolist (i (cdr requireds)) |
| 323 |
(push (list i |
| 324 |
(cond (arguments |
| 325 |
(pop arguments)) |
| 326 |
(apply-p |
| 327 |
`(if ,apply-var |
| 328 |
(pop ,apply-var) |
| 329 |
(si::dm-too-few-arguments nil))))) |
| 330 |
let-vars)) |
| 331 |
;; 4. process optional parameters |
| 332 |
(do ((scan (cdr optionals) (cdddr scan))) |
| 333 |
((endp scan)) |
| 334 |
(let ((opt-var (first scan)) |
| 335 |
(opt-flag (third scan)) |
| 336 |
(opt-value (second scan))) |
| 337 |
(cond (arguments |
| 338 |
(setf let-vars |
| 339 |
(list* `(,opt-var ,(pop arguments)) |
| 340 |
`(,opt-flag t) |
| 341 |
let-vars))) |
| 342 |
(apply-p |
| 343 |
(setf let-vars |
| 344 |
(list* `(,opt-var (if ,apply-var |
| 345 |
(pop ,apply-var) |
| 346 |
,opt-value)) |
| 347 |
`(,opt-flag ,apply-var) |
| 348 |
let-vars))) |
| 349 |
(t |
| 350 |
(setf let-vars |
| 351 |
(list* `(,opt-var ,opt-value) |
| 352 |
`(,opt-flag nil) |
| 353 |
let-vars)))))) |
| 354 |
;; 5. process rest parameter |
| 355 |
(when (or key-flag allow-other-keys) |
| 356 |
(unless rest |
| 357 |
(setf rest (gensym)))) |
| 358 |
(cond (rest |
| 359 |
(push `(,rest ,(if arguments |
| 360 |
(if apply-p |
| 361 |
`(list* ,@arguments ,apply-var) |
| 362 |
`(list ,@arguments)) |
| 363 |
(if apply-p apply-var nil))) |
| 364 |
let-vars)) |
| 365 |
(apply-p |
| 366 |
(push `(when ,apply-var |
| 367 |
(si::dm-too-many-arguments nil)) |
| 368 |
extra-stmts))) |
| 369 |
;; 6. process keyword parameters |
| 370 |
(do ((scan (cdr keywords) (cddddr scan))) |
| 371 |
((endp scan)) |
| 372 |
(let ((keyword (first scan)) |
| 373 |
(key-var (second scan)) |
| 374 |
(key-value (third scan)) |
| 375 |
(key-flag (or (fourth scan) (gensym)))) |
| 376 |
(push keyword all-keys) |
| 377 |
(setf let-vars |
| 378 |
(list* |
| 379 |
`(,key-var (if (eq ,key-flag 'si::missing-keyword) |
| 380 |
(prog1 ,key-value |
| 381 |
,(when (fourth scan) `(setf ,key-flag nil))) |
| 382 |
(prog1 ,key-flag |
| 383 |
,(when (fourth scan) `(setf ,key-flag t))))) |
| 384 |
`(,key-flag (si::search-keyword ,rest ,keyword)) |
| 385 |
let-vars)))) |
| 386 |
(when (and key-flag (not allow-other-keys)) |
| 387 |
(push `(si::check-keyword ,rest ',all-keys) extra-stmts)) |
| 388 |
;; 7. construct body |
| 389 |
(pop aux-vars) |
| 390 |
(loop while aux-vars |
| 391 |
do (push (list (pop aux-vars) (pop aux-vars)) let-vars)) |
| 392 |
(values (nreverse (delete-if-not #'first let-vars)) |
| 393 |
`(,@(and apply-var `((declare (ignorable ,apply-var)))) |
| 394 |
,@(multiple-value-bind (decl body) |
| 395 |
(si::find-declarations (cddr lambda-form)) |
| 396 |
(append decl extra-stmts body)))))) |
| 397 |
|
| 398 |
(defun optimize-funcall/apply-lambda (lambda-form arguments apply-p) |
| 399 |
(multiple-value-bind (bindings body) |
| 400 |
(transform-funcall/apply-into-let* lambda-form arguments apply-p) |
| 401 |
`(let* ,bindings ,@body))) |