| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya. |
| 6 |
;;;; Copyright (c) 1990, Giuseppe Attardi. |
| 7 |
;;;; |
| 8 |
;;;; See file 'LICENSE' for the copyright details. |
| 9 |
|
| 10 |
;;;; CMPWT Output routines. |
| 11 |
|
| 12 |
(in-package "COMPILER") |
| 13 |
|
| 14 |
(defun data-permanent-storage-size () |
| 15 |
(length *permanent-objects*)) |
| 16 |
|
| 17 |
(defun data-temporary-storage-size () |
| 18 |
(length *temporary-objects*)) |
| 19 |
|
| 20 |
(defun data-size () |
| 21 |
(+ (data-permanent-storage-size) |
| 22 |
(data-temporary-storage-size))) |
| 23 |
|
| 24 |
(defun data-get-all-objects () |
| 25 |
;; We collect all objects that are to be externalized, but filter out |
| 26 |
;; those which will be created by a lisp form. |
| 27 |
(loop for array in (list *permanent-objects* *temporary-objects*) |
| 28 |
nconc (loop for vv-record across array |
| 29 |
for object = (vv-value vv-record) |
| 30 |
collect (cond ((gethash object *load-objects*) |
| 31 |
0) |
| 32 |
((vv-used-p vv-record) |
| 33 |
object) |
| 34 |
(t |
| 35 |
;; Value optimized away or not used |
| 36 |
0))))) |
| 37 |
|
| 38 |
(defun data-dump-array () |
| 39 |
(cond (si:*compiler-constants* |
| 40 |
(setf si:*compiler-constants* (concatenate 'vector (data-get-all-objects))) |
| 41 |
"") |
| 42 |
#+externalizable |
| 43 |
((plusp (data-size)) |
| 44 |
(let* ((data-vector (concatenate 'vector (data-get-all-objects)))) |
| 45 |
(si::serialize data-vector))) |
| 46 |
#-externalizable |
| 47 |
((plusp (data-size)) |
| 48 |
(let* ((*wt-string-size* 0) |
| 49 |
(*wt-data-column* 80) |
| 50 |
(data (data-get-all-objects)) |
| 51 |
(data-string (si:with-ecl-io-syntax |
| 52 |
(prin1-to-string data))) |
| 53 |
(l (length data-string))) |
| 54 |
(subseq data-string 1 (1- l)))) |
| 55 |
(t |
| 56 |
""))) |
| 57 |
|
| 58 |
#-externalizable |
| 59 |
(defun data-c-dump (filename) |
| 60 |
(labels ((produce-strings () |
| 61 |
;; Only Windows has a size limit in the strings it creates. |
| 62 |
#-windows |
| 63 |
(let ((s (data-dump-array))) |
| 64 |
(when (plusp (length s)) |
| 65 |
(list s))) |
| 66 |
#+windows |
| 67 |
(loop with string = (data-dump-array) |
| 68 |
with max-string-size = 65530 |
| 69 |
with l = (length string) |
| 70 |
for i from 0 below l by max-string-size |
| 71 |
for this-l = (min (- l i) max-string-size) |
| 72 |
collect (make-array this-l :displaced-to string |
| 73 |
:element-type (array-element-type string) |
| 74 |
:displaced-index-offset i))) |
| 75 |
(output-one-c-string (name string stream) |
| 76 |
(let* ((*wt-string-size* 0) |
| 77 |
(*wt-data-column* 80) |
| 78 |
(s (with-output-to-string (stream) |
| 79 |
(wt-filtered-data string stream)))) |
| 80 |
(format stream "static const struct ecl_base_string ~A[] = { |
| 81 |
(int8_t)t_base_string, 0, ecl_aet_bc, 0, |
| 82 |
ECL_NIL, (cl_index)~D, (cl_index)~D, |
| 83 |
(ecl_base_char*)~A };~%" |
| 84 |
name *wt-string-size* *wt-string-size* s) |
| 85 |
name)) |
| 86 |
(output-c-strings (strings stream) |
| 87 |
(format stream |
| 88 |
"~%static const cl_object compiler_data_text[] = {~{~%(cl_object)~A,~}~%NULL};" |
| 89 |
(loop for s in strings |
| 90 |
for i from 1 |
| 91 |
for name = (format nil "compiler_data_text~D" i) |
| 92 |
collect (output-one-c-string name s stream))))) |
| 93 |
(with-open-file (stream filename :direction :output :if-does-not-exist :create |
| 94 |
:if-exists :supersede :external-format :default) |
| 95 |
(let ((strings (produce-strings))) |
| 96 |
(if strings |
| 97 |
(output-c-strings strings stream) |
| 98 |
(princ "#define compiler_data_text NULL" stream)) |
| 99 |
;; Ensure a final newline or some compilers complain |
| 100 |
(terpri stream))))) |
| 101 |
|
| 102 |
#+externalizable |
| 103 |
(defun data-c-dump (filename) |
| 104 |
(with-open-file (stream filename :direction :output :if-does-not-exist :create |
| 105 |
:if-exists :supersede :external-format :default) |
| 106 |
(let ((data (data-dump-array))) |
| 107 |
(if (plusp (length data)) |
| 108 |
(let ((s (with-output-to-string (stream) |
| 109 |
(loop for i below (length data) do |
| 110 |
(princ (elt data i) stream) |
| 111 |
(if (< i (1- (length data))) |
| 112 |
(princ "," stream)))))) |
| 113 |
(format stream "static uint8_t serialization_data[] = {~A};~%" s) |
| 114 |
(format stream "static const struct ecl_vector compiler_data_text1[] = {{ |
| 115 |
(int8_t)t_vector, 0, ecl_aet_b8, 0, |
| 116 |
ECL_NIL, (cl_index)~D, (cl_index)~D, |
| 117 |
{ .b8=serialization_data } }};~%" |
| 118 |
(length data) (length data)) |
| 119 |
(format stream "static const cl_object compiler_data_text[] = { |
| 120 |
(cl_object)compiler_data_text1}; ")) |
| 121 |
(princ "#define compiler_data_text NULL" stream)) |
| 122 |
;; Ensure a final newline or some compilers complain |
| 123 |
(terpri stream)))) |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
;;; ====================================================================== |
| 128 |
;;; |
| 129 |
;;; STATIC CONSTANTS |
| 130 |
;;; |
| 131 |
|
| 132 |
(defun static-base-string-builder (name value stream) |
| 133 |
(format stream "ecl_def_ct_base_string(~A," name) |
| 134 |
(wt-filtered-data value stream :one-liner t) |
| 135 |
(format stream ",~D,static,const);" (length value))) |
| 136 |
|
| 137 |
(defun static-single-float-builder (name value stream) |
| 138 |
(let* ((*read-default-float-format* 'single-float) |
| 139 |
(*print-readably* t)) |
| 140 |
(format stream "ecl_def_ct_single_float(~A,~S,static,const);" |
| 141 |
name value))) |
| 142 |
|
| 143 |
(defun static-double-float-builder (name value stream) |
| 144 |
(let* ((*read-default-float-format* 'double-float) |
| 145 |
(*print-readably* t)) |
| 146 |
(format stream "ecl_def_ct_double_float(~A,~S,static,const);" |
| 147 |
name value))) |
| 148 |
|
| 149 |
(defun static-long-float-builder (name value stream) |
| 150 |
(let* ((*read-default-float-format* 'long-float) |
| 151 |
(*print-readably* t)) |
| 152 |
(format stream "ecl_def_ct_long_float(~A,~SL,static,const);" |
| 153 |
name value))) |
| 154 |
|
| 155 |
(defun static-rational-builder (name value stream) |
| 156 |
(let* ((*read-default-float-format* 'double-float) |
| 157 |
(*print-readably* t)) |
| 158 |
(format stream |
| 159 |
"ecl_def_ct_ratio(~A,ecl_make_fixnum(~D),ecl_make_fixnum(~D),static,const);" |
| 160 |
name (numerator value) (denominator value)))) |
| 161 |
|
| 162 |
(defun static-constant-delegate (name value stream) |
| 163 |
(funcall (static-constant-expression value) |
| 164 |
name value stream)) |
| 165 |
|
| 166 |
(defun static-complex-builder (name value stream) |
| 167 |
(let* ((*read-default-float-format* 'double-float) |
| 168 |
(*print-readably* t) |
| 169 |
(name-real (concatenate 'string name "_real")) |
| 170 |
(name-imag (concatenate 'string name "_imag"))) |
| 171 |
(static-constant-delegate name-real (realpart value) stream) |
| 172 |
(terpri stream) |
| 173 |
(static-constant-delegate name-imag (imagpart value) stream) |
| 174 |
(terpri stream) |
| 175 |
(format stream |
| 176 |
"ecl_def_ct_complex(~A,&~A_data,&~A_data,static,const);" |
| 177 |
name name-real name-imag))) |
| 178 |
|
| 179 |
#+complex-float |
| 180 |
(defun static-csfloat-builder (name value stream) |
| 181 |
(let* ((*read-default-float-format* 'single-float) |
| 182 |
(*print-readably* t)) |
| 183 |
(format stream "ecl_def_ct_csfloat(~A,(~S + I*~S),static,const);" |
| 184 |
name (realpart value) (imagpart value) stream))) |
| 185 |
|
| 186 |
#+complex-float |
| 187 |
(defun static-cdfloat-builder (name value stream) |
| 188 |
(let* ((*read-default-float-format* 'double-float) |
| 189 |
(*print-readably* t)) |
| 190 |
(format stream "ecl_def_ct_cdfloat(~A,(~S + I*~S),static,const);" |
| 191 |
name (realpart value) (imagpart value) stream))) |
| 192 |
|
| 193 |
#+complex-float |
| 194 |
(defun static-clfloat-builder (name value stream) |
| 195 |
(let* ((*read-default-float-format* 'long-float) |
| 196 |
(*print-readably* t)) |
| 197 |
(format stream "ecl_def_ct_clfloat(~A,(~SL + I*~SL),static,const);" |
| 198 |
name (realpart value) (imagpart value) stream))) |
| 199 |
|
| 200 |
#+sse2 |
| 201 |
(defun static-sse-pack-builder (name value stream) |
| 202 |
(let* ((bytes (ext:sse-pack-to-vector value '(unsigned-byte 8))) |
| 203 |
(type-code (nth-value 1 (ext:sse-pack-element-type value)))) |
| 204 |
(format stream |
| 205 |
"ecl_def_ct_sse_pack(~A,~A~{,~A~});" |
| 206 |
name type-code (coerce bytes 'list)))) |
| 207 |
|
| 208 |
(defun static-constant-expression (object) |
| 209 |
(typecase object |
| 210 |
(base-string #'static-base-string-builder) |
| 211 |
(ratio (and (static-constant-expression (numerator object)) |
| 212 |
(static-constant-expression (denominator object)) |
| 213 |
#'static-rational-builder)) |
| 214 |
(single-float (and (not (ext:float-nan-p object)) |
| 215 |
(not (ext:float-infinity-p object)) |
| 216 |
#'static-single-float-builder)) |
| 217 |
(double-float (and (not (ext:float-nan-p object)) |
| 218 |
(not (ext:float-infinity-p object)) |
| 219 |
#'static-double-float-builder)) |
| 220 |
(long-float (and (not (ext:float-nan-p object)) |
| 221 |
(not (ext:float-infinity-p object)) |
| 222 |
#'static-long-float-builder)) |
| 223 |
#+complex-float |
| 224 |
(si:complex-single-float #'static-csfloat-builder) |
| 225 |
#+complex-float |
| 226 |
(si:complex-double-float #'static-cdfloat-builder) |
| 227 |
#+complex-float |
| 228 |
(si:complex-long-float #'static-clfloat-builder) |
| 229 |
(complex (and (static-constant-expression (realpart object)) |
| 230 |
(static-constant-expression (imagpart object)) |
| 231 |
#'static-complex-builder)) |
| 232 |
#+sse2 |
| 233 |
(ext:sse-pack #'static-sse-pack-builder) |
| 234 |
(t nil))) |
| 235 |
|
| 236 |
(defun add-static-constant (object) |
| 237 |
;; FIXME! The MSVC compiler does not allow static initialization of bit |
| 238 |
;; fields. SSE uses always unboxed static constants. No reference is kept to |
| 239 |
;; them -- it is thus safe to use them even on code that might be unloaded. |
| 240 |
;; |
| 241 |
;; NOTE *use-static-constants-p* is T only when :ecl-min is a feature. |
| 242 |
(unless (or #+msvc t |
| 243 |
si:*compiler-constants* |
| 244 |
(and (not *use-static-constants-p*) |
| 245 |
#+sse2 |
| 246 |
(not (typep object 'ext:sse-pack *cmp-env*))) |
| 247 |
(not (listp *static-constants*))) |
| 248 |
(ext:if-let ((record (find object *static-constants* :key #'first :test #'equal))) |
| 249 |
(second record) |
| 250 |
(ext:when-let ((builder (static-constant-expression object))) |
| 251 |
(let* ((c-name (format nil "_ecl_static_~D" (length *static-constants*))) |
| 252 |
(vv (make-vv :location c-name :value object))) |
| 253 |
(push (list object vv builder) *static-constants*) |
| 254 |
vv))))) |
| 255 |
|
| 256 |
|
| 257 |
;;; Inlineable constants |
| 258 |
|
| 259 |
;;; When we inline an immediate value, then sometimes it is expected to be a |
| 260 |
;;; lisp object (:object), and somtimes it is unboxed (:fixnum, :float, ...). |
| 261 |
;;; |
| 262 |
;;; TODO implement handling of unboxed values. |
| 263 |
|
| 264 |
(defun try-value-c-inliner (value) |
| 265 |
(ext:when-let ((x (assoc value *optimizable-constants*))) |
| 266 |
(when (typep value '(or float (complex float)) *cmp-env*) |
| 267 |
(pushnew "#include <float.h>" *clines-string-list*) |
| 268 |
(pushnew "#include <complex.h>" *clines-string-list*)) |
| 269 |
(cdr x))) |
| 270 |
|
| 271 |
(defun try-const-c-inliner (var) |
| 272 |
(check-type var var) |
| 273 |
(let ((name (var-name var))) |
| 274 |
(when (constant-variable-p name) |
| 275 |
(ext:when-let ((x (assoc name *optimizable-constants*))) |
| 276 |
(cdr x))))) |
| 277 |
|
| 278 |
(defun try-inline-core-sym (object) |
| 279 |
(when (symbolp object) |
| 280 |
(multiple-value-bind (foundp cname) |
| 281 |
(si:mangle-name object) |
| 282 |
(when foundp |
| 283 |
(make-vv :value object :location cname))))) |
| 284 |
|
| 285 |
(defun try-immediate-value (value) |
| 286 |
;; FIXME we could inline here also (COMPLEX FLOAT). That requires adding an |
| 287 |
;; emmiter of C complex floats in the function WT1. |
| 288 |
(cond |
| 289 |
((typep value '(or fixnum character float #|#+complex-float (complex float)|#) *cmp-env*) |
| 290 |
(make-vv :value value |
| 291 |
:location nil |
| 292 |
:host-type (lisp-type->host-type (type-of value)))) |
| 293 |
#+sse2 |
| 294 |
((typep value 'ext:sse-pack *cmp-env*) |
| 295 |
(let* ((bytes (ext:sse-pack-to-vector value '(unsigned-byte 8))) |
| 296 |
(elt-type (ext:sse-pack-element-type value))) |
| 297 |
(multiple-value-bind (wrapper rtype) |
| 298 |
(case elt-type |
| 299 |
(cl:single-float (values "_mm_castsi128_ps" :float-sse-pack)) |
| 300 |
(cl:double-float (values "_mm_castsi128_pd" :double-sse-pack)) |
| 301 |
(otherwise (values "" :int-sse-pack))) |
| 302 |
(make-vv :value value |
| 303 |
:location (format nil "~A(_mm_setr_epi8(~{~A~^,~}))" |
| 304 |
wrapper (coerce bytes 'list)) |
| 305 |
:host-type rtype)))) |
| 306 |
(t |
| 307 |
nil))) |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
(defun data-empty-loc* () |
| 312 |
(insert-vv (make-vv :value *empty-loc* :permanent-p t :used-p t))) |
| 313 |
|
| 314 |
;;; VV is emitted depending on the location and the representation type. |
| 315 |
(defun update-vv (target source) |
| 316 |
(assert (eql (vv-value target) (vv-value source))) |
| 317 |
(setf (vv-location target) (vv-location source) |
| 318 |
(vv-host-type target) (vv-host-type source))) |
| 319 |
|
| 320 |
(defun insert-vv (vv) |
| 321 |
(let* ((arr (if (vv-permanent-p vv) |
| 322 |
*permanent-objects* |
| 323 |
*temporary-objects*)) |
| 324 |
(ndx (vector-push-extend vv arr))) |
| 325 |
(setf (vv-location vv) ndx) |
| 326 |
vv)) |
| 327 |
|
| 328 |
(defun search-vv (object &key permanent (errorp t) test) |
| 329 |
(let* ((test (or test (if si:*compiler-constants* 'eq 'equal-with-circularity))) |
| 330 |
(item (if permanent |
| 331 |
(find object *permanent-objects* :test test :key #'vv-value) |
| 332 |
(or (find object *permanent-objects* :test test :key #'vv-value) |
| 333 |
(find object *temporary-objects* :test test :key #'vv-value))))) |
| 334 |
(when (and (null item) errorp) |
| 335 |
(cmperr "Unable to find object ~s." object)) |
| 336 |
item)) |
| 337 |
|
| 338 |
(defun get-object (object &key (permanent t) (errorp t) test) |
| 339 |
(or (search-vv object :permanent permanent :errorp nil :test test) |
| 340 |
(try-inline-core-sym object) |
| 341 |
(and errorp |
| 342 |
(cmperr "Unable to find object ~s." object)))) |
| 343 |
|
| 344 |
;;; The vector *REFERENCED-OBJECTS* contains all referenced objects collected in |
| 345 |
;;; the first pass. The function OPTIMIZE-CXX-DATA is responsible for inlining |
| 346 |
;;; them when possible, and putting them in a temporary/permanent data segments |
| 347 |
;;; otherwise. Objects in data segment require an explicit initialization when |
| 348 |
;;; the the module is loaded. The following inline strategies are tried: |
| 349 |
;;; |
| 350 |
;;; - using a symbol from the core :: ECL_SYM(\"*BREAK-ON-SIGNALS*\",27) |
| 351 |
;;; - using a cvalue inline value :: cl_core.single_float_zero, FLT_MAX |
| 352 |
;;; - using a static constructor :: ecl_def_ct_single_float -> _ecl_static_7 |
| 353 |
;;; - coercing an immediate value :: ecl_make_fixnum(42) |
| 354 |
;;; |
| 355 |
;;; Otherwise the object is put in VV or VVtemp vector, and is created when the |
| 356 |
;;; module is loaded. Then the code refers to such objects as VV[13]. |
| 357 |
;;; |
| 358 |
;;; TODO we could further optimize immediate values by duplicating some entries |
| 359 |
;;; depending on the expected host-type, to avoid unnecessary coercions. |
| 360 |
(defun optimize-cxx-data (objects) |
| 361 |
(flet ((optimize-vv (object) |
| 362 |
(let ((value (vv-value object))) |
| 363 |
(ext:if-let ((vv (and (not (vv-always object)) |
| 364 |
(or (try-inline-core-sym value) |
| 365 |
(try-value-c-inliner value) |
| 366 |
(add-static-constant value) |
| 367 |
(try-immediate-value value))))) |
| 368 |
(update-vv object vv) |
| 369 |
(insert-vv object))))) |
| 370 |
(map nil #'optimize-vv objects))) |
| 371 |
|
| 372 |
(defun add-keywords (keywords) |
| 373 |
;; We have to build, in the vector VV[], a sequence with all the keywords |
| 374 |
;; that this function uses. It does not matter whether each keyword has |
| 375 |
;; appeared separately before, because cl_parse_key() needs the whole |
| 376 |
;; sequence. However, we can reuse keywords sequences from other functions |
| 377 |
;; when they coincide with ours. See C2LAMBDA-EXPR. |
| 378 |
(ext:if-let ((x (search keywords *permanent-objects* |
| 379 |
:test #'(lambda (k record) |
| 380 |
(and (vv-always record) |
| 381 |
(eq k (vv-value record))))))) |
| 382 |
(elt *permanent-objects* x) |
| 383 |
(prog1 (insert-vv (make-vv :value (pop keywords) :always t :used-p t)) |
| 384 |
(dolist (k keywords) |
| 385 |
(insert-vv (make-vv :value k :always t :used-p t)))))) |
| 386 |
|
| 387 |
|
| 388 |
(defun wt-vv-index (index permanent-p) |
| 389 |
(cond ((not (numberp index)) |
| 390 |
(wt index)) |
| 391 |
(permanent-p |
| 392 |
(wt "VV[" index "]")) |
| 393 |
(t |
| 394 |
(wt "VVtemp[" index "]")))) |
| 395 |
|
| 396 |
(defun wt-vv-value (vv value) |
| 397 |
(cond |
| 398 |
((eql value CL:T) (wt "ECL_T")) |
| 399 |
((eql value CL:NIL) (wt "ECL_NIL")) |
| 400 |
((typep value 'fixnum *cmp-env*) (wt-fixnum value vv)) |
| 401 |
((typep value 'character *cmp-env*) (wt-character value vv)) |
| 402 |
((typep value 'float *cmp-env*) (wt-number value vv)) |
| 403 |
((typep value '(complex float) *cmp-env*) (wt-number value vv)) |
| 404 |
(t (baboon "wt-vv-value: ~s is not an immediate value, but has no VV index~%" value)))) |
| 405 |
|
| 406 |
(defun wt-vv (vv-loc) |
| 407 |
(setf (vv-used-p vv-loc) t) |
| 408 |
(let ((index (vv-location vv-loc))) |
| 409 |
(if (null index) |
| 410 |
(wt-vv-value vv-loc (vv-value vv-loc)) |
| 411 |
(wt-vv-index index (vv-permanent-p vv-loc))))) |
| 412 |
|
| 413 |
(defun set-vv-index (loc index permanent-p) |
| 414 |
(wt-nl) (wt-vv-index index permanent-p) (wt "=" (coerce-loc :object loc) ";")) |
| 415 |
|
| 416 |
(defun set-vv (loc vv-loc) |
| 417 |
(setf (vv-used-p vv-loc) t) |
| 418 |
(set-vv-index loc (vv-location vv-loc) (vv-permanent-p vv-loc))) |
| 419 |
|