| 1 |
(in-package #:compiler) |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
(defun wt-print-header (source) |
| 6 |
(wt-comment-nl "Compiler: ~A ~A" (lisp-implementation-type) (lisp-implementation-version)) |
| 7 |
#-ecl-min |
| 8 |
(multiple-value-bind (second minute hour day month year) |
| 9 |
(get-decoded-time) |
| 10 |
(declare (ignore second)) |
| 11 |
(wt-comment-nl "Date: ~D/~D/~D ~2,'0D:~2,'0D (yyyy/mm/dd)" year month day hour minute) |
| 12 |
(wt-comment-nl "Machine: ~A ~A ~A" (software-type) (software-version) (machine-type))) |
| 13 |
(wt-comment-nl "Source: ~A" source)) |
| 14 |
|
| 15 |
(defun compiler-pass/generate-cxx (c-pathname h-pathname data-pathname init-name source) |
| 16 |
(with-cxx-env () |
| 17 |
;; After this step we still can add new objects, but objects that are |
| 18 |
;; already stored in VV or VVtemp must not change the location. |
| 19 |
(optimize-cxx-data *referenced-objects*) |
| 20 |
(setq *compiler-phase* 't2) |
| 21 |
(with-open-file (*compiler-output1* c-pathname :direction :output |
| 22 |
:if-does-not-exist :create |
| 23 |
:if-exists :supersede) |
| 24 |
(with-open-file (*compiler-output2* h-pathname :direction :output |
| 25 |
:if-does-not-exist :create |
| 26 |
:if-exists :supersede) |
| 27 |
(wt-print-header source) |
| 28 |
(wt-nl1 "#include " *cmpinclude*) |
| 29 |
(ctop-write init-name h-pathname data-pathname) |
| 30 |
(terpri *compiler-output1*) |
| 31 |
(terpri *compiler-output2*))) |
| 32 |
(data-c-dump data-pathname))) |
| 33 |
|
| 34 |
;;;; CMPTOP -- Compiler top-level. |
| 35 |
|
| 36 |
(defun t2expr (form) |
| 37 |
(check-type form c1form) |
| 38 |
(with-c1form-env (form form) |
| 39 |
(let* ((name (c1form-name form)) |
| 40 |
(args (c1form-args form)) |
| 41 |
(dispatch (gethash name *t2-dispatch-table*))) |
| 42 |
(if dispatch |
| 43 |
(apply dispatch form args) |
| 44 |
(cmperr "Unhandled T2FORM found at the toplevel:~%~4I~A" form))))) |
| 45 |
|
| 46 |
(defun t2compiler-let (c1form symbols values body) |
| 47 |
(declare (ignore c1form)) |
| 48 |
(progv symbols values (c2expr body))) |
| 49 |
|
| 50 |
(defun t2progn (c1form args) |
| 51 |
(declare (ignore c1form)) |
| 52 |
(mapc #'t2expr args)) |
| 53 |
|
| 54 |
(defun t2ordinary (c1form form) |
| 55 |
(declare (ignore c1form)) |
| 56 |
(with-exit-label (*exit*) |
| 57 |
(let ((*destination* 'TRASH)) |
| 58 |
(c2expr form)))) |
| 59 |
|
| 60 |
(defun t2load-time-value (c1form vv-loc form) |
| 61 |
(declare (ignore c1form)) |
| 62 |
(with-exit-label (*exit*) |
| 63 |
(let ((*destination* vv-loc)) |
| 64 |
(c2expr form)))) |
| 65 |
|
| 66 |
(defun t2make-form (c1form vv-loc form) |
| 67 |
(declare (ignore c1form)) |
| 68 |
(with-exit-label (*exit*) |
| 69 |
(let ((*destination* vv-loc)) |
| 70 |
(c2expr form)))) |
| 71 |
|
| 72 |
(defun t2init-form (c1form vv-loc form) |
| 73 |
(declare (ignore c1form vv-loc)) |
| 74 |
(with-exit-label (*exit*) |
| 75 |
(let ((*destination* 'TRASH)) |
| 76 |
(c2expr form)))) |
| 77 |
|
| 78 |
(defun t2fset (c1form &rest args) |
| 79 |
(declare (ignore args)) |
| 80 |
(t2ordinary c1form c1form)) |
| 81 |
|
| 82 |
(defun c2fset (c1form fun fname macro-p pprint c1forms) |
| 83 |
(declare (ignore pprint)) |
| 84 |
(when (fun-no-entry fun) |
| 85 |
(wt-nl "(void)0; " (format nil "/* No entry created for ~A */" (fun-name fun))) |
| 86 |
;; FIXME! Look at C2LOCALS! |
| 87 |
(update-function-env fun) |
| 88 |
(return-from c2fset)) |
| 89 |
(if (and (not (fun-closure fun)) |
| 90 |
(eq *destination* 'TRASH)) |
| 91 |
(wt-install-function fname fun macro-p) |
| 92 |
(c2call-global c1form 'SI:FSET c1forms))) |
| 93 |
|
| 94 |
|
| 95 |
(defun emit-functions (*compiler-output1*) |
| 96 |
(declare (si::c-local)) |
| 97 |
;; Local functions and closure functions |
| 98 |
(do ((*compile-time-too* nil) |
| 99 |
(*compile-toplevel* nil) |
| 100 |
(*emitted-functions* nil)) |
| 101 |
;; repeat until t3function generates no more |
| 102 |
((eq *emitted-functions* *functions*)) |
| 103 |
;; scan *functions* backwards |
| 104 |
(do ((lfs *functions* (cdr lfs))) |
| 105 |
((eq (cdr lfs) *emitted-functions*) |
| 106 |
(setq *emitted-functions* lfs) |
| 107 |
(locally (declare (notinline t3function)) |
| 108 |
;; so disassemble can redefine it |
| 109 |
(t3function (first lfs))))))) |
| 110 |
|
| 111 |
(defun ctop-write (init-name h-pathname data-pathname &aux top-output-string) |
| 112 |
(wt-nl "#include \"" (brief-namestring h-pathname) "\"") |
| 113 |
|
| 114 |
;; VV might be needed by functions in CLINES. |
| 115 |
(wt-nl-h "#ifdef ECL_DYNAMIC_VV") |
| 116 |
(wt-nl-h "static cl_object *VV;") |
| 117 |
(wt-nl-h "#else") |
| 118 |
(wt-nl-h "static cl_object VV[VM];") |
| 119 |
(wt-nl-h "#endif") |
| 120 |
(output-clines *compiler-output2*) |
| 121 |
|
| 122 |
(wt-nl-h "#ifdef __cplusplus") |
| 123 |
(wt-nl-h "extern \"C\" {") |
| 124 |
(wt-nl-h "#endif") |
| 125 |
;;; We rebind the output to ensure that the initialization function is |
| 126 |
;;; processed first and added last. |
| 127 |
(let ((output (make-string-output-stream))) |
| 128 |
(t3entry-fun init-name output) |
| 129 |
(emit-functions *compiler-output1*) |
| 130 |
(setq top-output-string (get-output-stream-string output))) |
| 131 |
;; Declarations in h-file. |
| 132 |
(wt-nl-h "static cl_object Cblock;") |
| 133 |
(let ((num-objects (data-size))) |
| 134 |
(if (zerop num-objects) |
| 135 |
(progn |
| 136 |
(wt-nl-h "#undef ECL_DYNAMIC_VV") |
| 137 |
(wt-nl-h "#define compiler_data_text 0") |
| 138 |
(wt-nl-h "#define VM 0") |
| 139 |
(wt-nl-h "#define VMtemp 0") |
| 140 |
(wt-nl-h "#define VV NULL")) |
| 141 |
(progn |
| 142 |
(wt-nl-h "#define VM " (data-permanent-storage-size)) |
| 143 |
(wt-nl-h "#define VMtemp " (data-temporary-storage-size))))) |
| 144 |
|
| 145 |
(wt-nl-h "#define ECL_DEFINE_SETF_FUNCTIONS ") |
| 146 |
(loop for (name setf-vv name-vv) in *setf-definitions* |
| 147 |
do (wt-h #\\ #\Newline setf-vv "=ecl_setf_definition(" name-vv ",ECL_T);")) |
| 148 |
|
| 149 |
(wt-nl-h "#ifdef __cplusplus") |
| 150 |
(wt-nl-h "}") |
| 151 |
(wt-nl-h "#endif") |
| 152 |
|
| 153 |
(unless (null *static-constants*) |
| 154 |
(wt-nl-h "/*") |
| 155 |
(wt-nl-h " * Statically defined constants") |
| 156 |
(wt-nl-h " */") |
| 157 |
(loop for (value vv builder) in (reverse *static-constants*) |
| 158 |
do (terpri *compiler-output2*) |
| 159 |
do (funcall builder (vv-location vv) value *compiler-output2*))) |
| 160 |
|
| 161 |
(output-cfuns *compiler-output2*) |
| 162 |
|
| 163 |
(setq *compiler-phase* 't3) |
| 164 |
|
| 165 |
;;; Callbacks |
| 166 |
(when *callbacks* |
| 167 |
(wt-nl-h "#include <ecl/internal.h>") |
| 168 |
(dolist (x *callbacks*) |
| 169 |
(apply #'t3callback x))) |
| 170 |
|
| 171 |
(wt-nl "#include \"" (brief-namestring data-pathname) "\"") |
| 172 |
(wt-nl "#ifdef __cplusplus") |
| 173 |
(wt-nl "extern \"C\"") |
| 174 |
(wt-nl "#endif") |
| 175 |
(wt-nl top-output-string)) |
| 176 |
|
| 177 |
(defun wt-function-locals (&optional closure-type) |
| 178 |
;; FIXME! Are we careful enough with temporary variables that |
| 179 |
;; we need not make them volatile? |
| 180 |
(when (plusp *max-temp*) |
| 181 |
(wt-nl "cl_object ") |
| 182 |
(dotimes (i *max-temp*) |
| 183 |
(wt "T" i) |
| 184 |
(unless (= (1+ i) *max-temp*) (wt ", "))) |
| 185 |
(wt ";")) |
| 186 |
(when *ihs-used-p* |
| 187 |
(wt-nl "struct ecl_ihs_frame ihs;") |
| 188 |
(wt-nl "const cl_object _ecl_debug_env = ECL_NIL;")) |
| 189 |
;; There should be no need to mark lex as volatile, since we |
| 190 |
;; are going to pass pointers of this array around and the compiler |
| 191 |
;; should definitely keep this in memory. |
| 192 |
(when (plusp *max-lex*) |
| 193 |
(wt-nl "volatile cl_object lex" *level* "[" *max-lex* "];")) |
| 194 |
|
| 195 |
(unless (eq closure-type 'CLOSURE) |
| 196 |
(wt-nl "cl_object " *volatile* "env0 = ECL_NIL;")) |
| 197 |
|
| 198 |
(when (plusp *max-env*) |
| 199 |
;; Closure structure has to be marked volatile or else GCC may |
| 200 |
;; optimize away writes into it because it does not know it shared |
| 201 |
;; with the rest of the world. |
| 202 |
(when *aux-closure* |
| 203 |
(wt-nl "volatile struct ecl_cclosure aux_closure;")) |
| 204 |
|
| 205 |
(wt-nl "cl_object " *volatile*) |
| 206 |
(loop for i from 0 below *max-env* |
| 207 |
for comma = "" then ", " |
| 208 |
do (wt comma "CLV" i) |
| 209 |
finally (wt ";")))) |
| 210 |
|
| 211 |
|
| 212 |
(defun t3entry-fun (name *compiler-output1*) |
| 213 |
(with-bir-env (:env 0 :level 0 :volatile "volatile ") |
| 214 |
(wt-nl "ECL_DLLEXPORT void " name "(cl_object flag)") |
| 215 |
(wt-nl-open-brace) |
| 216 |
(wt-nl "const cl_env_ptr cl_env_copy = ecl_process_env();") |
| 217 |
(wt-nl "cl_object value0;") |
| 218 |
(wt-nl "cl_object *VVtemp;") |
| 219 |
|
| 220 |
(wt-nl "if (flag != OBJNULL){") |
| 221 |
(wt-nl "Cblock = flag;") |
| 222 |
(wt-nl "#ifndef ECL_DYNAMIC_VV") |
| 223 |
(wt-nl "flag->cblock.data = VV;") |
| 224 |
(wt-nl "#endif") |
| 225 |
(when *self-destructing-fasl* |
| 226 |
(wt-nl "flag->cblock.self_destruct=1;")) |
| 227 |
(wt-nl "flag->cblock.data_size = VM;") |
| 228 |
(wt-nl "flag->cblock.temp_data_size = VMtemp;") |
| 229 |
(wt-nl "flag->cblock.data_text = compiler_data_text;") |
| 230 |
(wt-nl "flag->cblock.cfuns_size = compiler_cfuns_size;") |
| 231 |
(wt-nl "flag->cblock.cfuns = compiler_cfuns;") |
| 232 |
(when ext:*source-location* |
| 233 |
(wt-nl "flag->cblock.source = ecl_make_constant_base_string(\"" |
| 234 |
(namestring (car ext:*source-location*)) "\",-1);")) |
| 235 |
(wt-nl "return;}") |
| 236 |
(wt-nl "#ifdef ECL_DYNAMIC_VV") |
| 237 |
(wt-nl "VV = Cblock->cblock.data;") |
| 238 |
(wt-nl "#endif") |
| 239 |
;; With this we ensure creating a constant with the tag |
| 240 |
;; and the initialization file |
| 241 |
(wt-nl "Cblock->cblock.data_text = (const cl_object *)\"" (init-name-tag name) "\";") |
| 242 |
(wt-nl "VVtemp = Cblock->cblock.temp_data;") |
| 243 |
(wt-nl "ECL_DEFINE_SETF_FUNCTIONS") |
| 244 |
;; We save the C body of the statement, indented, just in case we need to |
| 245 |
;; add a {} section with the environment variables. |
| 246 |
(let ((body (let ((*opened-c-braces* (1+ *opened-c-braces*))) |
| 247 |
(with-output-to-string (*compiler-output1*) |
| 248 |
(terpri *compiler-output1*) |
| 249 |
(wt-comment-nl "MAKE-LOAD-FORMs") |
| 250 |
(dolist (form *make-forms*) |
| 251 |
(t2expr form)) |
| 252 |
(wt-comment-nl "TOP-LEVEL-FORMs") |
| 253 |
(dolist (form *top-level-forms*) |
| 254 |
(t2expr form)))))) |
| 255 |
(if (or (plusp *max-lex*) |
| 256 |
(plusp *max-temp*) |
| 257 |
(plusp *max-env*) |
| 258 |
*ihs-used-p*) |
| 259 |
(with-lexical-scope () |
| 260 |
(wt-function-locals) |
| 261 |
(write-sequence body *compiler-output1*)) |
| 262 |
(write-sequence body *compiler-output1*))) |
| 263 |
;; We process top-level forms before functions to update their |
| 264 |
;; environments. Then we emit functions before top level forms. |
| 265 |
(wt-nl-close-many-braces 0))) |
| 266 |
|
| 267 |
(defun t3callback (lisp-name c-name c-name-constant return-type return-type-code |
| 268 |
arg-types arg-type-constants call-type &aux (return-p t)) |
| 269 |
(declare (ignore lisp-name)) |
| 270 |
(with-bir-env (:env 0 :level 0 :volatile "volatile ") |
| 271 |
(when (eql return-type :void) |
| 272 |
(setf return-p nil)) |
| 273 |
(let ((return-type-name (host-type->c-name (ffi::%convert-to-arg-type return-type))) |
| 274 |
(vars (loop for n from 0 below (length arg-types) |
| 275 |
collect (format nil "var~d" n))) |
| 276 |
(fmod (case call-type |
| 277 |
((:cdecl :default) "") |
| 278 |
(:stdcall "__stdcall ") |
| 279 |
(t (cmperr "DEFCALLBACK does not support ~A as calling convention" |
| 280 |
call-type))))) |
| 281 |
(wt-nl-h "static " return-type-name " " fmod c-name "(") |
| 282 |
(wt-nl1 "static " return-type-name " " fmod c-name "(") |
| 283 |
(loop with comma = "" |
| 284 |
for var in vars |
| 285 |
for type in arg-types |
| 286 |
for arg-type-name = (host-type->c-name (ffi::%convert-to-arg-type type)) |
| 287 |
do (wt-h comma arg-type-name " " var) |
| 288 |
(wt comma arg-type-name " " var) |
| 289 |
(setf comma ",")) |
| 290 |
(wt ")") |
| 291 |
(wt-h ");") |
| 292 |
(with-lexical-scope () |
| 293 |
(when return-p |
| 294 |
(wt-nl return-type-name " output;")) |
| 295 |
(wt-nl "const cl_env_ptr cl_env_copy = ecl_process_env();") |
| 296 |
(wt-nl "cl_object aux;") |
| 297 |
(with-stack-frame (frame) |
| 298 |
(loop for var in vars |
| 299 |
and type in arg-types |
| 300 |
and ct in arg-type-constants |
| 301 |
do (wt-nl "ecl_stack_frame_push(" frame "," `(ffi-data-ref ,var ,ct) ");")) |
| 302 |
(wt-nl "aux = ecl_apply_from_stack_frame(" frame "," |
| 303 |
"ecl_fdefinition(" c-name-constant "));") |
| 304 |
;; No UNWIND-EXIT, so we must close the frame manually. |
| 305 |
(wt-nl "ecl_stack_frame_close(" frame ");")) |
| 306 |
(when return-p |
| 307 |
(set-loc `(ffi-data-ref "output" ,return-type-code) "aux") |
| 308 |
(wt-nl "return output;")))))) |
| 309 |
|
| 310 |
(defun t3function (fun) |
| 311 |
(declare (type fun fun)) |
| 312 |
;; Compiler note about compiling this function |
| 313 |
(when *compile-print* |
| 314 |
(ext:when-let ((name (or (fun-name fun) (fun-description fun)))) |
| 315 |
(format t "~&;;; Emitting code for ~s.~%" name))) |
| 316 |
(with-c1form-env (lambda-expr (fun-lambda fun)) |
| 317 |
(let ((*tail-recursion-info* fun) |
| 318 |
(*tail-recursion-mark* nil)) |
| 319 |
(with-bir-env (:env (fun-env fun) |
| 320 |
:level (fun-lexical-levels fun) |
| 321 |
:volatile (c1form-volatile* lambda-expr)) |
| 322 |
(t3function-declaration fun) |
| 323 |
(wt-nl-open-brace) |
| 324 |
(let ((body (t3function-body fun))) |
| 325 |
(wt-function-locals (fun-closure fun)) |
| 326 |
(wt-nl "const cl_env_ptr cl_env_copy = ecl_process_env();") |
| 327 |
(when (eq (fun-closure fun) 'CLOSURE) |
| 328 |
(wt-nl "cl_object " *volatile* "env0 = cl_env_copy->function->cclosure.env;")) |
| 329 |
(wt-nl "cl_object " *volatile* "value0;") |
| 330 |
(when (policy-check-stack-overflow) |
| 331 |
(wt-nl "ecl_cs_check(cl_env_copy,value0);")) |
| 332 |
(when (eq (fun-closure fun) 'CLOSURE) |
| 333 |
(t3function-closure-scan fun)) |
| 334 |
(write-sequence body *compiler-output1*) |
| 335 |
(wt-nl-close-many-braces 0)) |
| 336 |
(when (fun-variadic-entrypoint fun) |
| 337 |
(t3function-variadic-entrypoint fun)))))) |
| 338 |
|
| 339 |
(defun t3function-body (fun) |
| 340 |
(let ((string (make-array 2048 :element-type 'character |
| 341 |
:adjustable t |
| 342 |
:fill-pointer 0))) |
| 343 |
(with-output-to-string (*compiler-output1* string) |
| 344 |
(let ((lambda-expr (fun-lambda fun))) |
| 345 |
(c2lambda-expr (c1form-arg 0 lambda-expr) |
| 346 |
(c1form-arg 2 lambda-expr) |
| 347 |
(fun-cfun fun) |
| 348 |
(fun-name fun) |
| 349 |
(fun-description fun) |
| 350 |
(fun-needs-narg fun) |
| 351 |
(fun-required-lcls fun) |
| 352 |
(fun-closure fun) |
| 353 |
(fun-optional-type-check-forms fun) |
| 354 |
(fun-keyword-type-check-forms fun) |
| 355 |
(fun-c-compatible-variadic-signature fun)))) |
| 356 |
string)) |
| 357 |
|
| 358 |
(defun fun-c-compatible-variadic-signature (fun) |
| 359 |
;; Returns true if we need to generate a signature of the form |
| 360 |
;; `cl_object f(cl_narg narg, ...)` |
| 361 |
#-c-compatible-variadic-dispatch |
| 362 |
nil |
| 363 |
#+c-compatible-variadic-dispatch |
| 364 |
(and (fun-needs-narg fun) |
| 365 |
;; local functions or lexical closures are never called via a |
| 366 |
;; function pointer |
| 367 |
(not (eq (fun-closure fun) 'LEXICAL)) |
| 368 |
(not (fun-no-entry fun)) |
| 369 |
;; exported functions need to keep their signature to not break |
| 370 |
;; the external API |
| 371 |
(not (fun-exported fun)))) |
| 372 |
|
| 373 |
;;; Variadic entrypoints |
| 374 |
;;; |
| 375 |
;;; For functions which only take required arguments, we can generate |
| 376 |
;;; an entrypoint with variadic signature and narg parameter. This |
| 377 |
;;; entrypoint checks the number of arguments and then calls the |
| 378 |
;;; entrypoint without narg parameter. Callers using direct C calls |
| 379 |
;;; can then skip the check for the number of parameters. If no direct |
| 380 |
;;; C calls exist, the C compiler will usually inline the fixed |
| 381 |
;;; entrypoint and thus optimize away the overhead that this method |
| 382 |
;;; incurs over only generating a variadic entrypoint. |
| 383 |
;;; |
| 384 |
(defun fun-variadic-entrypoint (fun) |
| 385 |
(let ((result (fun-variadic-entrypoint-cfun fun))) |
| 386 |
(if (not (eq result :unknown)) |
| 387 |
result |
| 388 |
(setf (fun-variadic-entrypoint-cfun fun) |
| 389 |
(and (policy-inline-nargs-check) |
| 390 |
(not (fun-no-entry fun)) |
| 391 |
(not (fun-needs-narg fun)) |
| 392 |
;; lexical closures will never be called via a |
| 393 |
;; variadic entrypoint, no need to create one |
| 394 |
(not (eq (fun-closure fun) 'lexical)) |
| 395 |
(concatenate 'string (fun-cfun fun) "_va")))))) |
| 396 |
|
| 397 |
(defun t3function-variadic-entrypoint (fun) |
| 398 |
(flet ((wt-return (args) |
| 399 |
(wt-nl "return ") |
| 400 |
(wt-call (fun-cfun fun) args) |
| 401 |
(wt ";") |
| 402 |
(wt-nl-close-many-braces 0))) |
| 403 |
(let ((*volatile* "")) |
| 404 |
(t3function-header fun |
| 405 |
(fun-variadic-entrypoint-cfun fun) |
| 406 |
t |
| 407 |
#+c-compatible-variadic-dispatch t)) |
| 408 |
(wt-nl-open-brace) |
| 409 |
(wt-maybe-check-num-arguments t |
| 410 |
(fun-minarg fun) |
| 411 |
(fun-maxarg fun) |
| 412 |
(fun-name fun)) |
| 413 |
#-c-compatible-variadic-dispatch |
| 414 |
(wt-return (fun-required-lcls fun)) |
| 415 |
#+c-compatible-variadic-dispatch |
| 416 |
(let ((maxargs (min (fun-maxarg fun) (1+ si:c-arguments-limit)))) |
| 417 |
(when (plusp maxargs) |
| 418 |
(wt-nl "cl_object x[" maxargs "];") |
| 419 |
(wt-nl "va_list args; va_start(args,narg);") |
| 420 |
(loop for i below maxargs |
| 421 |
do (wt-nl "x[" i "] = ") (wt-coerce-loc :object 'VA-ARG) (wt ";")) |
| 422 |
(wt-nl "va_end(args);")) |
| 423 |
(let ((args (loop for i below maxargs |
| 424 |
collect (concatenate 'string "x[" (write-to-string i) "]")))) |
| 425 |
(wt-return args))))) |
| 426 |
|
| 427 |
(defun fun-fixed-narg-main-entrypoint (fun) |
| 428 |
"Number of fixed arguments for fun. If both variadic and ordinary |
| 429 |
entrypoints exist, return the number of fixed arguments for the |
| 430 |
variadic entrypoint. This may differ from the number of required |
| 431 |
parameters of the corresponding Lisp function if we are generating a C |
| 432 |
compatible variadic signature." |
| 433 |
#+c-compatible-variadic-dispatch |
| 434 |
(when (or (fun-variadic-entrypoint fun) (fun-c-compatible-variadic-signature fun)) |
| 435 |
(return-from fun-fixed-narg-main-entrypoint 0)) |
| 436 |
(min (fun-minarg fun) si:c-arguments-limit)) |
| 437 |
|
| 438 |
(defun t3function-header (fun cfun needs-narg &optional omit-requireds) |
| 439 |
(let* ((comma "") |
| 440 |
(lambda-expr (fun-lambda fun)) |
| 441 |
(lambda-list (c1form-arg 0 lambda-expr)) |
| 442 |
(requireds (loop |
| 443 |
repeat si::c-arguments-limit |
| 444 |
for arg in (car lambda-list) |
| 445 |
collect (next-lcl (var-name arg))))) |
| 446 |
(setf (fun-required-lcls fun) requireds) |
| 447 |
(cond ((fun-exported fun) |
| 448 |
(wt-nl-h "ECL_DLLEXPORT cl_object " cfun "(") |
| 449 |
(wt-nl "cl_object " cfun "(")) |
| 450 |
(t |
| 451 |
(wt-nl-h "static cl_object " cfun "(") |
| 452 |
(wt-nl "static cl_object " cfun "("))) |
| 453 |
(when needs-narg |
| 454 |
(wt-h *volatile* "cl_narg") |
| 455 |
(wt *volatile* "cl_narg narg") |
| 456 |
(setf comma ", ")) |
| 457 |
(dotimes (n (fun-lexical-levels fun)) |
| 458 |
(wt-h comma "volatile cl_object *") |
| 459 |
(wt comma "volatile cl_object *lex" n) |
| 460 |
(setf comma ", ")) |
| 461 |
(unless omit-requireds |
| 462 |
(loop for lcl in requireds |
| 463 |
do (wt-h comma "cl_object " *volatile*) |
| 464 |
(wt comma "cl_object " *volatile* lcl) |
| 465 |
(setf comma ", "))) |
| 466 |
(when needs-narg |
| 467 |
(wt-h ", ...") |
| 468 |
(wt ", ...")) |
| 469 |
(wt-h ");") |
| 470 |
(wt ")"))) |
| 471 |
|
| 472 |
(defun t3function-declaration (fun) |
| 473 |
(declare (type fun fun)) |
| 474 |
(wt-comment-nl (cond ((fun-global fun) "function definition for ~a") |
| 475 |
((eq (fun-closure fun) 'CLOSURE) "closure ~a") |
| 476 |
(t "local function ~a")) |
| 477 |
(or (fun-name fun) (fun-description fun) 'CLOSURE)) |
| 478 |
(let* ((lambda-expr (fun-lambda fun)) |
| 479 |
(cmp-env (c1form-env lambda-expr))) |
| 480 |
(wt-comment-nl "optimize speed ~D, debug ~D, space ~D, safety ~D " |
| 481 |
(cmp-env-optimization 'speed cmp-env) |
| 482 |
(cmp-env-optimization 'debug cmp-env) |
| 483 |
(cmp-env-optimization 'space cmp-env) |
| 484 |
(cmp-env-optimization 'safety cmp-env)) |
| 485 |
(t3function-header fun (fun-cfun fun) (fun-needs-narg fun) |
| 486 |
(fun-c-compatible-variadic-signature fun))) |
| 487 |
t) |
| 488 |
|
| 489 |
(defun fun-closure-variables (fun) |
| 490 |
(sort (remove-if |
| 491 |
#'(lambda (x) |
| 492 |
(or |
| 493 |
;; non closure variable |
| 494 |
(not (ref-ref-ccb x)) |
| 495 |
;; special variable |
| 496 |
(eq (var-kind x) 'special) |
| 497 |
;; not actually referenced |
| 498 |
(and (not (var-referenced-in-form x (fun-lambda fun))) |
| 499 |
(not (var-changed-in-form x (fun-lambda fun)))) |
| 500 |
;; parameter of this closure |
| 501 |
;; (not yet bound, therefore var-loc is OBJECT) |
| 502 |
(eq (var-loc x) 'OBJECT))) |
| 503 |
(fun-referenced-vars fun)) |
| 504 |
#'> |
| 505 |
:key #'var-loc)) |
| 506 |
|
| 507 |
(defun fun-lexical-levels (fun) |
| 508 |
(if (eq (fun-closure fun) 'LEXICAL) |
| 509 |
(fun-level fun) |
| 510 |
0)) |
| 511 |
|
| 512 |
(defun t3function-closure-scan (fun) |
| 513 |
(let ((clv-used (fun-closure-variables fun))) |
| 514 |
(wt-nl "/* Scanning closure data ... */") |
| 515 |
(do ((n (1- (fun-env fun)) (1- n)) |
| 516 |
(bs clv-used) |
| 517 |
(first t)) |
| 518 |
((or (minusp n) (null bs))) |
| 519 |
(wt-nl "CLV" n) |
| 520 |
(if first |
| 521 |
(progn (wt " = env0;") (setf first nil)) |
| 522 |
(wt " = _ecl_cdr(CLV" (1+ n) ");")) |
| 523 |
(when (= n (var-loc (first bs))) |
| 524 |
(wt-comment (var-name (first clv-used))) |
| 525 |
(pop clv-used))) |
| 526 |
(wt-nl-open-brace) |
| 527 |
(wt " /* ... closure scanning finished */"))) |
| 528 |
|
| 529 |
(defun output-cfuns (stream) |
| 530 |
(let ((n-cfuns (length *global-cfuns-array*))) |
| 531 |
(wt-nl-h "/*") |
| 532 |
(wt-nl-h " * Exported Lisp functions") |
| 533 |
(wt-nl-h " */") |
| 534 |
(wt-nl-h "#define compiler_cfuns_size " n-cfuns) |
| 535 |
(if (zerop n-cfuns) |
| 536 |
(wt-nl-h "#define compiler_cfuns NULL") |
| 537 |
(progn |
| 538 |
(format stream "~%static const struct ecl_cfunfixed compiler_cfuns[] = {~ |
| 539 |
~%~t/*t,m,narg,padding,name=function-location,block=name-location,entry,entry_fixed,file,file_position*/") |
| 540 |
(loop for (loc fname-loc fun) in (nreverse *global-cfuns-array*) |
| 541 |
do (let* ((variadic-entrypoint (fun-variadic-entrypoint-cfun fun)) |
| 542 |
(cfun (or variadic-entrypoint (fun-cfun fun))) |
| 543 |
(needs-narg (or variadic-entrypoint (fun-needs-narg fun))) |
| 544 |
(narg (fun-fixed-narg-main-entrypoint fun))) |
| 545 |
(format stream "~%{~A,0,~D,0,ecl_make_fixnum(~D),ecl_make_fixnum(~D),(cl_objectfn)~A,NULL,ECL_NIL,ecl_make_fixnum(~D)}," |
| 546 |
(if needs-narg "t_cfun" "t_cfunfixed") |
| 547 |
narg |
| 548 |
(vv-location loc) |
| 549 |
(vv-location fname-loc) |
| 550 |
cfun |
| 551 |
(fun-file-position fun)))) |
| 552 |
(format stream "~%};"))))) |
| 553 |
|
| 554 |
(defun wt-install-function (fname fun macro-p) |
| 555 |
(with-inline-blocks () |
| 556 |
(let ((loc (data-empty-loc*))) |
| 557 |
(push (list loc fname fun) *global-cfuns-array*) |
| 558 |
;; FIXME! Look at C2LOCALS! |
| 559 |
(update-function-env fun) |
| 560 |
(if macro-p |
| 561 |
(wt-nl "ecl_cmp_defmacro(" loc ");") |
| 562 |
(wt-nl "ecl_cmp_defun(" loc ");")) |
| 563 |
(wt-comment (loc-immediate-value fname))))) |