| 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 c2locals (c1form funs body labels ;; labels is T when deriving from labels |
| 13 |
&aux |
| 14 |
(*env* *env*) |
| 15 |
(*inline-blocks* 0) |
| 16 |
(*env-lvl* *env-lvl*)) |
| 17 |
(declare (ignore c1form labels)) |
| 18 |
;; create location for each function which is returned, either in lexical: |
| 19 |
(loop with env-grows = nil |
| 20 |
with closed-vars = '() |
| 21 |
for fun in funs |
| 22 |
for var = (fun-var fun) |
| 23 |
when (plusp (var-ref var)) |
| 24 |
do (case (var-kind var) |
| 25 |
((lexical closure) |
| 26 |
(push var closed-vars) |
| 27 |
(unless env-grows |
| 28 |
(setq env-grows (var-ref-ccb var)))) |
| 29 |
(otherwise |
| 30 |
(maybe-open-inline-block) |
| 31 |
(bind (next-lcl) var) |
| 32 |
(wt-nl "cl_object " *volatile* var ";"))) |
| 33 |
finally |
| 34 |
;; if we have closed variables |
| 35 |
(when (env-grows env-grows) |
| 36 |
(maybe-open-inline-block) |
| 37 |
(let ((env-lvl *env-lvl*)) |
| 38 |
(wt "cl_object " *volatile* "env" (incf *env-lvl*) " = env" env-lvl ";"))) |
| 39 |
;; bind closed locations because of possible circularities |
| 40 |
(loop for var in closed-vars |
| 41 |
do (bind *vv-nil* var))) |
| 42 |
;; create the functions: |
| 43 |
(map nil #'update-function-env funs) |
| 44 |
;; - then assign to it |
| 45 |
(loop for fun in funs |
| 46 |
for var = (fun-var fun) |
| 47 |
when (plusp (var-ref var)) |
| 48 |
do (set-var (list 'MAKE-CCLOSURE fun) var)) |
| 49 |
(c2expr body) |
| 50 |
(close-inline-blocks)) |
| 51 |
|
| 52 |
(defun update-function-env (fun) |
| 53 |
(declare (type fun fun)) |
| 54 |
(case (fun-closure fun) |
| 55 |
(CLOSURE |
| 56 |
(setf (fun-level fun) 0 |
| 57 |
(fun-env fun) *env*)) |
| 58 |
(LEXICAL |
| 59 |
;; Only increase the lexical level if there have been some |
| 60 |
;; new variables created. This way, the same lexical environment |
| 61 |
;; can be propagated through nested FLET/LABELS. |
| 62 |
(setf (fun-level fun) (if (plusp *lex*) (1+ *level*) *level*) |
| 63 |
(fun-env fun) 0)) |
| 64 |
(otherwise |
| 65 |
(setf (fun-level fun) 0 |
| 66 |
(fun-env fun) 0))) |
| 67 |
(register-function fun)) |
| 68 |
|
| 69 |
#| Steps: |
| 70 |
1. defun creates declarations for requireds + va_alist |
| 71 |
2. c2lambda-expr adds declarations for: |
| 72 |
unboxed requireds |
| 73 |
lexical optionals (+ supplied-p), rest, keywords (+ supplied-p) |
| 74 |
Lexical optionals and keywords can be unboxed if: |
| 75 |
a. there is more then one reference in the body |
| 76 |
b. they are not referenced in closures |
| 77 |
3. binding is performed for: |
| 78 |
special or unboxed requireds |
| 79 |
optionals, rest, keywords |
| 80 |
4. the function name is optionally pushed onto the IHS when |
| 81 |
the caller asks for it. |
| 82 |
|# |
| 83 |
|
| 84 |
(defun c2lambda-expr |
| 85 |
(lambda-list body cfun fname description use-narg required-lcls closure-type |
| 86 |
optional-type-check-forms keyword-type-check-forms |
| 87 |
c-compatible-variadic-signature |
| 88 |
&aux (requireds (first lambda-list)) |
| 89 |
(optionals (second lambda-list)) |
| 90 |
(rest (third lambda-list)) rest-loc |
| 91 |
(key-flag (fourth lambda-list)) |
| 92 |
(keywords (fifth lambda-list)) |
| 93 |
(allow-other-keys (sixth lambda-list)) |
| 94 |
(nreq (length requireds)) |
| 95 |
(nopt (/ (length optionals) 3)) |
| 96 |
(nkey (/ (length keywords) 4)) |
| 97 |
(varargs (or optionals rest key-flag allow-other-keys |
| 98 |
(and c-compatible-variadic-signature requireds))) |
| 99 |
(fname-in-ihs-p (or (policy-debug-variable-bindings) |
| 100 |
(and (policy-debug-ihs-frame) |
| 101 |
(or description fname)))) |
| 102 |
simple-varargs |
| 103 |
(*permanent-data* t) |
| 104 |
(*unwind-exit* *unwind-exit*) |
| 105 |
(*env* *env*) |
| 106 |
(*inline-blocks* 0) |
| 107 |
(last-arg)) |
| 108 |
(declare (fixnum nreq nkey)) |
| 109 |
|
| 110 |
(if (and fname ;; named function |
| 111 |
;; no required appears in closure, |
| 112 |
(dolist (var requireds t) |
| 113 |
(declare (type var var)) |
| 114 |
(when (var-ref-ccb var) (return nil))) |
| 115 |
(null optionals) ;; no optionals, |
| 116 |
(null rest) ;; no rest parameter, and |
| 117 |
(null key-flag)) ;; no keywords. |
| 118 |
(setf *tail-recursion-info* (cons *tail-recursion-info* requireds)) |
| 119 |
(setf *tail-recursion-info* nil)) |
| 120 |
|
| 121 |
;; check number of arguments |
| 122 |
(wt-maybe-check-num-arguments use-narg |
| 123 |
nreq |
| 124 |
(if (or rest key-flag allow-other-keys) |
| 125 |
nil |
| 126 |
(+ nreq nopt)) |
| 127 |
fname) |
| 128 |
|
| 129 |
;; If the number of required arguments exceeds the number of variables we |
| 130 |
;; want to pass on the C stack, we pass some of the arguments to the list |
| 131 |
;; of optionals, which will eventually get passed in the lisp stack. |
| 132 |
(when (> nreq si::c-arguments-limit) |
| 133 |
(setf nopt (+ nopt (- nreq si::c-arguments-limit)) |
| 134 |
nreq si::c-arguments-limit) |
| 135 |
(setf optionals (nconc (loop for var in (subseq requireds si::c-arguments-limit) |
| 136 |
nconc (list var (c1nil) NIL)) |
| 137 |
optionals) |
| 138 |
requireds (subseq requireds 0 si::c-arguments-limit) |
| 139 |
varargs t)) |
| 140 |
|
| 141 |
;; For each variable, set its var-loc. |
| 142 |
;; For optional and keyword parameters, and lexical variables which |
| 143 |
;; can be unboxed, this will be a new LCL. |
| 144 |
;; The bind step later will assign to such variable. |
| 145 |
(labels ((wt-decl (var) |
| 146 |
(let ((lcl (next-lcl (var-name var)))) |
| 147 |
(wt-nl) |
| 148 |
(wt (host-type->c-name (var-host-type var)) " " *volatile* lcl ";") |
| 149 |
lcl)) |
| 150 |
(do-decl (var) |
| 151 |
(when (local-var-p var) ; no LCL needed for SPECIAL or LEX |
| 152 |
(setf (var-loc var) (wt-decl var))))) |
| 153 |
;; Declare unboxed required arguments |
| 154 |
(loop for var in requireds |
| 155 |
when (or (unboxed-var-p var) c-compatible-variadic-signature) |
| 156 |
do (do-decl var)) |
| 157 |
;; dont create rest or varargs if not used |
| 158 |
(when (and rest (< (var-ref rest) 1) |
| 159 |
(not (eq (var-kind rest) 'SPECIAL))) |
| 160 |
(setq rest nil |
| 161 |
varargs (or varargs optionals key-flag allow-other-keys))) |
| 162 |
;; Declare &optional variables |
| 163 |
(do ((opt optionals (cdddr opt))) |
| 164 |
((endp opt)) |
| 165 |
(do-decl (first opt)) |
| 166 |
(when (third opt) (do-decl (third opt)))) |
| 167 |
;; Declare &rest variables |
| 168 |
(when rest (setq rest-loc (wt-decl rest))) |
| 169 |
;; Declare &key variables |
| 170 |
(do ((key keywords (cddddr key))) |
| 171 |
((endp key)) |
| 172 |
(do-decl (second key)) |
| 173 |
(when (fourth key) (do-decl (fourth key))))) |
| 174 |
|
| 175 |
;; Declare and assign the variable arguments pointer |
| 176 |
(when varargs |
| 177 |
(flet ((last-variable () |
| 178 |
(cond ((and required-lcls (not c-compatible-variadic-signature)) |
| 179 |
(first (last required-lcls))) |
| 180 |
((eq closure-type 'LEXICAL) |
| 181 |
(format nil "lex~D" (1- *level*))) |
| 182 |
(t "narg")))) |
| 183 |
(if (setq simple-varargs (and (not (or rest key-flag allow-other-keys)) |
| 184 |
(<= (+ nreq nopt) si::c-arguments-limit))) |
| 185 |
(wt-nl "va_list args; va_start(args," |
| 186 |
(last-variable) |
| 187 |
");") |
| 188 |
(wt-nl "ecl_va_list args; ecl_va_start(args," |
| 189 |
(last-variable) ",narg," |
| 190 |
(if c-compatible-variadic-signature |
| 191 |
0 |
| 192 |
nreq) |
| 193 |
");")) |
| 194 |
(when c-compatible-variadic-signature |
| 195 |
(setf required-lcls |
| 196 |
(loop repeat nreq |
| 197 |
collect (if simple-varargs 'VA-ARG 'CL-VA-ARG)))))) |
| 198 |
|
| 199 |
;; Bind required arguments. Produces C statements for unboxed |
| 200 |
;; variables, which is why it is done after all declarations. |
| 201 |
(mapc #'bind required-lcls requireds) |
| 202 |
|
| 203 |
(when fname-in-ihs-p |
| 204 |
(let ((fname (get-object (or description fname) :test #'equal))) |
| 205 |
(open-inline-block) |
| 206 |
(setf *ihs-used-p* t) |
| 207 |
(push 'IHS *unwind-exit*) |
| 208 |
(when (policy-debug-variable-bindings) |
| 209 |
(build-debug-lexical-env (reverse requireds) t)) |
| 210 |
(wt-nl "ecl_ihs_push(cl_env_copy,&ihs," fname ",_ecl_debug_env);"))) |
| 211 |
|
| 212 |
;; Bind optional parameters as long as there remain arguments. |
| 213 |
(when optionals |
| 214 |
;; When binding optional values, we use two calls to BIND. This means |
| 215 |
;; 'BDS-BIND is pushed twice on *unwind-exit*, which results in two calls |
| 216 |
;; to bds_unwind1(), which is wrong. A simple fix is to save *unwind-exit* |
| 217 |
;; which is what we do here. |
| 218 |
(let ((va-arg-loc (if simple-varargs 'VA-ARG 'CL-VA-ARG))) |
| 219 |
;; counter for optionals |
| 220 |
(with-lexical-scope () |
| 221 |
(wt-nl "int i = " nreq ";") |
| 222 |
(do ((opt optionals (cdddr opt)) |
| 223 |
(type-check optional-type-check-forms (cdr type-check))) |
| 224 |
((endp opt)) |
| 225 |
(wt-nl "if (i >= narg) {") |
| 226 |
(let ((*opened-c-braces* (1+ *opened-c-braces*))) |
| 227 |
(bind-init (second opt) (first opt)) |
| 228 |
(when (third opt) |
| 229 |
(bind *vv-nil* (third opt)))) |
| 230 |
(wt-nl "} else {") |
| 231 |
(let ((*opened-c-braces* (1+ *opened-c-braces*)) |
| 232 |
(*unwind-exit* *unwind-exit*)) |
| 233 |
(wt-nl "i++;") |
| 234 |
(bind va-arg-loc (first opt)) |
| 235 |
(if (car type-check) |
| 236 |
(c2expr* (car type-check))) |
| 237 |
(when (third opt) |
| 238 |
(bind *vv-t* (third opt)))) |
| 239 |
(wt-nl "}"))))) |
| 240 |
|
| 241 |
(when (or rest key-flag allow-other-keys) |
| 242 |
(cond ((not (or key-flag allow-other-keys)) |
| 243 |
(wt-nl rest-loc " = cl_grab_rest_args(args);")) |
| 244 |
(t |
| 245 |
(cond (keywords |
| 246 |
(wt-nl-open-brace) ;; Brace [1] |
| 247 |
(wt-nl "cl_object keyvars[" (* 2 nkey) "];") |
| 248 |
(wt-nl "cl_parse_key(args," nkey "," cfun "keys,keyvars")) |
| 249 |
(t |
| 250 |
(wt-nl "cl_parse_key(args,0,NULL,NULL"))) |
| 251 |
;; This explicit coercion is required to remove the "volatile" |
| 252 |
;; declaration on some variables. |
| 253 |
(if rest (wt ",(cl_object*)&" rest-loc) (wt ",NULL")) |
| 254 |
(wt (if allow-other-keys ",TRUE);" ",FALSE);")))) |
| 255 |
(when rest |
| 256 |
(bind rest-loc rest))) |
| 257 |
|
| 258 |
(when varargs |
| 259 |
(wt-nl (if simple-varargs "va_end(args);" "ecl_va_end(args);"))) |
| 260 |
|
| 261 |
;;; Bind keywords. |
| 262 |
(do ((kwd keywords (cddddr kwd)) |
| 263 |
(all-kwd nil) |
| 264 |
(KEYVARS[i] `(KEYVARS 0)) |
| 265 |
(i 0 (1+ i)) |
| 266 |
(type-check keyword-type-check-forms (cdr type-check))) |
| 267 |
((endp kwd) |
| 268 |
(when all-kwd |
| 269 |
(wt-nl-h "#define " cfun "keys (&" (add-keywords (nreverse all-kwd)) ")") |
| 270 |
(wt-nl-close-brace))) ;; Matches [1] |
| 271 |
(declare (fixnum i)) |
| 272 |
(push (first kwd) all-kwd) |
| 273 |
(let ((key (first kwd)) |
| 274 |
(var (second kwd)) |
| 275 |
(init (third kwd)) |
| 276 |
(flag (fourth kwd))) |
| 277 |
(cond ((and (eq (c1form-name init) 'LOCATION) |
| 278 |
(eq (c1form-arg 0 init) *vv-nil*)) |
| 279 |
;; no initform |
| 280 |
;; ECL_NIL has been set in keyvars if keyword parameter is not supplied. |
| 281 |
(setf (second KEYVARS[i]) i) |
| 282 |
(bind KEYVARS[i] var) |
| 283 |
(when (car type-check) |
| 284 |
(c2expr* (car type-check)))) |
| 285 |
(t |
| 286 |
;; with initform |
| 287 |
(setf (second KEYVARS[i]) (+ nkey i)) |
| 288 |
(wt-nl "if (Null(") (wt-loc KEYVARS[i]) (wt ")) {") |
| 289 |
(let ((*unwind-exit* *unwind-exit*) |
| 290 |
(*opened-c-braces* (1+ *opened-c-braces*))) |
| 291 |
(bind-init init var)) |
| 292 |
(wt-nl "} else {") |
| 293 |
(let ((*opened-c-braces* (1+ *opened-c-braces*))) |
| 294 |
(setf (second KEYVARS[i]) i) |
| 295 |
(bind KEYVARS[i] var) |
| 296 |
(when (car type-check) |
| 297 |
(c2expr* (car type-check)))) |
| 298 |
(wt-nl "}"))) |
| 299 |
(when flag |
| 300 |
(setf (second KEYVARS[i]) (+ nkey i)) |
| 301 |
(bind KEYVARS[i] flag)))) |
| 302 |
(when *tail-recursion-info* |
| 303 |
(setf *tail-recursion-mark* (next-label t)) |
| 304 |
(push *tail-recursion-mark* *unwind-exit*) |
| 305 |
(wt-label *tail-recursion-mark*)) |
| 306 |
;;; Now the parameters are ready, after all! |
| 307 |
(c2expr body) |
| 308 |
(close-inline-blocks)) |
| 309 |
|
| 310 |
(defun wt-maybe-check-num-arguments (use-narg minarg maxarg fname) |
| 311 |
(when (and (policy-check-nargs) use-narg) |
| 312 |
(flet ((wrong-num-arguments () |
| 313 |
(if fname |
| 314 |
(wt " FEwrong_num_arguments(" (get-object fname :test #'equal) ");") |
| 315 |
(wt " FEwrong_num_arguments_anonym();")))) |
| 316 |
(if (and maxarg (= minarg maxarg)) |
| 317 |
(progn (wt-nl "if (ecl_unlikely(narg!=" minarg "))") |
| 318 |
(wrong-num-arguments)) |
| 319 |
(progn |
| 320 |
(when (plusp minarg) |
| 321 |
(wt-nl "if (ecl_unlikely(narg<" minarg "))") |
| 322 |
(wrong-num-arguments)) |
| 323 |
(when maxarg |
| 324 |
(wt-nl "if (ecl_unlikely(narg>" maxarg "))") |
| 325 |
(wrong-num-arguments))))) |
| 326 |
(open-inline-block))) |