| 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) 2010, Juan Jose Garcia-Ripoll |
| 6 |
;;;; Copyright (c) 2023, Daniel Kochmański |
| 7 |
;;;; |
| 8 |
;;;; See the file 'LICENSE' for the copyright details. |
| 9 |
;;;; |
| 10 |
|
| 11 |
;;;; CMPPROP Type propagation. |
| 12 |
|
| 13 |
(in-package "COMPILER") |
| 14 |
|
| 15 |
(eval-when (:compile-toplevel :execute) |
| 16 |
(defconstant +all-c1-forms+ |
| 17 |
'(;; top-level forms |
| 18 |
(ORDINARY c1form :pure) |
| 19 |
(MAKE-FORM vv-loc value-c1form :side-effects) |
| 20 |
(INIT-FORM vv-loc value-c1form :side-effects) |
| 21 |
;; Forms that have different semantics when they are top-level |
| 22 |
(CL:PROGN body :pure) |
| 23 |
(SI:FSET function-object vv-loc macro-p pprint-p lambda-form :side-effects) |
| 24 |
(CL:LOAD-TIME-VALUE dest-loc value-c1form :pure :single-valued) |
| 25 |
(EXT:COMPILER-LET symbols values body) |
| 26 |
;; Places, assignment and binding |
| 27 |
(LOCATION loc :pure :single-valued) |
| 28 |
(VARIABLE var value :single-valued) |
| 29 |
(CL:SETQ var value-c1form :side-effects) |
| 30 |
(CL:PSETQ var-list value-c1form-list :side-effects) |
| 31 |
(CL:PROGV symbols values form :side-effects) |
| 32 |
(CL:LET* vars-list var-init-c1form-list decl-body-c1form :pure) |
| 33 |
(CL:MULTIPLE-VALUE-SETQ vars-list values-c1form-list :side-effects) |
| 34 |
(CL:MULTIPLE-VALUE-BIND vars-list init-c1form body :pure) |
| 35 |
;; Function namespace (should include also FSET) |
| 36 |
(CL:FUNCTION fname :single-valued) |
| 37 |
(LOCALS local-fun-list body labels-p :pure) |
| 38 |
;; Control structures |
| 39 |
(CL:BLOCK blk-var progn-c1form :pure) |
| 40 |
(CL:RETURN-FROM blk-var nonlocal value :side-effects) |
| 41 |
(CL:TAGBODY tag-var tag-body :pure) |
| 42 |
(CL:GO tag-var nonlocal :side-effects) |
| 43 |
(CL:CATCH catch-value body :side-effects) |
| 44 |
(CL:THROW catch-value output-value :side-effects) |
| 45 |
(CL:UNWIND-PROTECT protected-c1form body :side-effects) |
| 46 |
;; |
| 47 |
(CL:IF fmla-c1form true-c1form false-c1form :pure) |
| 48 |
(FMLA-NOT fmla-c1form :pure) |
| 49 |
(FMLA-AND * :pure) |
| 50 |
(FMLA-OR * :pure) |
| 51 |
;; Both nodes FCALL and MCALL are function call variants that implement |
| 52 |
;; semantics of Common Lisp operators FUNCALL and MULTIPLE-VALUE-CALL. |
| 53 |
(FCALL fun-form (arg-value*) fun-val call-type :side-effects) |
| 54 |
(MCALL fun-form (arg-value*) fun-val call-type :side-effects) |
| 55 |
;; Other operators |
| 56 |
(CL:LAMBDA lambda-list doc body-c1form) |
| 57 |
(CL:VALUES values-c1form-list :pure) |
| 58 |
(MV-PROG1 form body :side-effects) |
| 59 |
;; Extensions |
| 60 |
(ext:COMPILER-TYPECASE var expressions) |
| 61 |
(ext:CHECKED-VALUE type value-c1form let-form) |
| 62 |
;; Backend-specific operators |
| 63 |
(FFI:C-INLINE (arg-c1form*) (arg-type-symbol*) output-host-type |
| 64 |
c-expression-string |
| 65 |
side-effects-p one-liner-p) |
| 66 |
(FFI:C-PROGN variables forms)))) |
| 67 |
|
| 68 |
(defconstant +c1-form-hash+ |
| 69 |
#.(loop with hash = (make-hash-table :size 128 :test #'eq) |
| 70 |
for (name . rest) in +all-c1-forms+ |
| 71 |
for length = (if (member '* rest) nil (length rest)) |
| 72 |
for side-effects = (if (member :side-effects rest) |
| 73 |
(progn (and length (decf length)) t) |
| 74 |
nil) |
| 75 |
for movable = (if (member :pure rest) |
| 76 |
(progn (and length (decf length)) t) |
| 77 |
nil) |
| 78 |
for single-valued = (if (member :single-valued rest) |
| 79 |
(progn (and length (decf length)) t) |
| 80 |
nil) |
| 81 |
do (setf (gethash name hash) (list length side-effects movable single-valued)) |
| 82 |
finally (return hash))) |
| 83 |
|
| 84 |
(defconstant +c1-dispatch-alist+ |
| 85 |
'((cl:block . c1block) ; c1special |
| 86 |
(cl:return-from . c1return-from) ; c1special |
| 87 |
(cl:funcall . c1funcall) ; c1 |
| 88 |
(cl:catch . c1catch) ; c1special |
| 89 |
(cl:unwind-protect . c1unwind-protect) ; c1special |
| 90 |
(cl:throw . c1throw) ; c1special |
| 91 |
(ffi:defcallback . c1-defcallback) ; c1 |
| 92 |
(cl:progn . c1progn) ; c1special |
| 93 |
(ext:with-backend . c1with-backend) ; c1special |
| 94 |
(ffi:clines . c1clines) ; c1special |
| 95 |
(ffi:c-inline . c1c-inline) ; c1special |
| 96 |
(ffi:c-progn . c1c-progn) ; c1special |
| 97 |
(cl:flet . c1flet) ; c1special |
| 98 |
(cl:labels . c1labels) ; c1special |
| 99 |
(cl:locally . c1locally) ; c1special |
| 100 |
(cl:macrolet . c1macrolet) ; c1special |
| 101 |
(cl:symbol-macrolet . c1symbol-macrolet) ; c1special |
| 102 |
|
| 103 |
(cl:if . c1if) ; c1special |
| 104 |
(cl:not . c1not) ; c1special |
| 105 |
(cl:and . c1and) ; c1special |
| 106 |
(cl:or . c1or) ; c1special |
| 107 |
|
| 108 |
(cl:let . c1let) ; c1special |
| 109 |
(cl:let* . c1let*) ; c1special |
| 110 |
|
| 111 |
(cl:multiple-value-call . c1multiple-value-call) ; c1special |
| 112 |
(cl:multiple-value-prog1 . c1multiple-value-prog1) ; c1special |
| 113 |
(cl:values . c1values) ; c1 |
| 114 |
(cl:multiple-value-setq . c1multiple-value-setq) ; c1 |
| 115 |
(cl:multiple-value-bind . c1multiple-value-bind) ; c1 |
| 116 |
|
| 117 |
(ext:compiler-typecase . c1compiler-typecase) ; c1special |
| 118 |
(ext:checked-value . c1checked-value) ; c1special |
| 119 |
|
| 120 |
(cl:quote . c1quote) ; c1special |
| 121 |
(cl:function . c1function) ; c1special |
| 122 |
(cl:the . c1the) ; c1special |
| 123 |
(ext:truly-the . c1truly-the) ; c1special |
| 124 |
(cl:eval-when . c1eval-when) ; c1special |
| 125 |
(cl:declare . c1declare) ; c1special |
| 126 |
(ext:compiler-let . c1compiler-let) ; c1special |
| 127 |
|
| 128 |
(cl:tagbody . c1tagbody) ; c1special |
| 129 |
(cl:go . c1go) ; c1special |
| 130 |
|
| 131 |
(cl:setq . c1setq) ; c1special |
| 132 |
(cl:progv . c1progv) ; c1special |
| 133 |
(cl:psetq . c1psetq) ; c1special |
| 134 |
|
| 135 |
(cl:load-time-value . c1load-time-value) ; c1 |
| 136 |
|
| 137 |
(cl:apply . c1apply) ; c1 |
| 138 |
)) |
| 139 |
|
| 140 |
(defconstant +t1-dispatch-alist+ |
| 141 |
'((ext:with-backend . c1with-backend) ; t1 |
| 142 |
|
| 143 |
(cl:defmacro . t1defmacro) |
| 144 |
(ext:compiler-let . c1compiler-let) |
| 145 |
(cl:eval-when . c1eval-when) |
| 146 |
(cl:progn . c1progn) |
| 147 |
(cl:macrolet . c1macrolet) |
| 148 |
(cl:locally . c1locally) |
| 149 |
(cl:symbol-macrolet . c1symbol-macrolet) |
| 150 |
(si:fset . t1fset) |
| 151 |
)) |
| 152 |
|
| 153 |
(defconstant +set-loc-dispatch-alist+ |
| 154 |
'((bind . bind) |
| 155 |
(cl:the . set-the-loc) |
| 156 |
(valuez . set-valuez-loc) |
| 157 |
(value0 . set-value0-loc) |
| 158 |
(leave . set-leave-loc) |
| 159 |
(trash . set-trash-loc) |
| 160 |
(jump-true . set-trash-loc) |
| 161 |
(jump-false . set-trash-loc) |
| 162 |
(ffi-data-ref . wt-ffi-data-set))) |
| 163 |
|
| 164 |
(defconstant +wt-loc-dispatch-alist+ |
| 165 |
'((call-normal . wt-call-normal) |
| 166 |
(call-indirect . wt-call-indirect) |
| 167 |
(call-global-stack . wt-call-global-stack) |
| 168 |
(call-local-stack . wt-call-local-stack) |
| 169 |
(ffi:c-inline . wt-c-inline-loc) |
| 170 |
(coerce-loc . wt-coerce-loc) |
| 171 |
|
| 172 |
(temp . wt-temp) |
| 173 |
(lcl . wt-lcl-loc) |
| 174 |
(value . wt-value) |
| 175 |
(keyvars . wt-keyvars) |
| 176 |
(cl:the . wt-the) |
| 177 |
|
| 178 |
(cl:fdefinition . wt-fdefinition) |
| 179 |
(make-cclosure . wt-make-closure) |
| 180 |
|
| 181 |
(ffi-data-ref . wt-ffi-data-ref) |
| 182 |
|
| 183 |
(frame++ . "ECL_NEW_FRAME_ID(cl_env_copy)") |
| 184 |
(leave . "value0") |
| 185 |
(va-arg . "va_arg(args,cl_object)") |
| 186 |
(cl-va-arg . "ecl_va_arg(args)") |
| 187 |
(valuez . "cl_env_copy->values[0]") |
| 188 |
(value0 . "value0"))) |
| 189 |
|
| 190 |
(defconstant +c2-dispatch-alist+ |
| 191 |
'((cl:block . c2block) |
| 192 |
(cl:return-from . c2return-from) |
| 193 |
(fcall . c2fcall) |
| 194 |
(mcall . c2mcall) |
| 195 |
|
| 196 |
(cl:catch . c2catch) |
| 197 |
(cl:unwind-protect . c2unwind-protect) |
| 198 |
(cl:throw . c2throw) |
| 199 |
(cl:progn . c2progn) |
| 200 |
(ffi:c-inline . c2c-inline) |
| 201 |
(ffi:c-progn . c2c-progn) |
| 202 |
(locals . c2locals) |
| 203 |
|
| 204 |
(cl:if . c2if) |
| 205 |
(fmla-not . c2fmla-not) |
| 206 |
(fmla-and . c2fmla-and) |
| 207 |
(fmla-or . c2fmla-or) |
| 208 |
|
| 209 |
(cl:let* . c2let*) |
| 210 |
|
| 211 |
(cl:values . c2values) |
| 212 |
(cl:multiple-value-setq . c2multiple-value-setq) |
| 213 |
(cl:multiple-value-bind . c2multiple-value-bind) |
| 214 |
|
| 215 |
(cl:function . c2function) |
| 216 |
(ext:compiler-let . c2compiler-let) |
| 217 |
|
| 218 |
(mv-prog1 . c2mv-prog1) |
| 219 |
|
| 220 |
(cl:tagbody . c2tagbody) |
| 221 |
(cl:go . c2go) |
| 222 |
|
| 223 |
(variable . c2variable) |
| 224 |
(location . c2location) |
| 225 |
(cl:setq . c2setq) |
| 226 |
(cl:progv . c2progv) |
| 227 |
(cl:psetq . c2psetq) |
| 228 |
|
| 229 |
(si:fset . c2fset) |
| 230 |
|
| 231 |
(ext:compiler-typecase . c2compiler-typecase) |
| 232 |
(ext:checked-value . c2checked-value) |
| 233 |
)) |
| 234 |
|
| 235 |
(defconstant +t2-dispatch-alist+ |
| 236 |
'((ext:compiler-let . t2compiler-let) |
| 237 |
(cl:progn . t2progn) |
| 238 |
(ordinary . t2ordinary) |
| 239 |
(cl:load-time-value . t2load-time-value) |
| 240 |
(make-form . t2make-form) |
| 241 |
(init-form . t2init-form) |
| 242 |
(si:fset . t2fset) |
| 243 |
)) |
| 244 |
|
| 245 |
(defconstant +p1-dispatch-alist+ |
| 246 |
'((cl:block . p1block) |
| 247 |
(cl:return-from . p1return-from) |
| 248 |
(fcall . p1fcall) |
| 249 |
(mcall . p1mcall) |
| 250 |
(cl:catch . p1catch) |
| 251 |
(cl:throw . p1throw) |
| 252 |
(cl:if . p1if) |
| 253 |
(fmla-not . p1fmla-not) |
| 254 |
(fmla-and . p1fmla-and) |
| 255 |
(fmla-or . p1fmla-or) |
| 256 |
(cl:lambda . p1lambda) |
| 257 |
(cl:let* . p1let*) |
| 258 |
(locals . p1locals) |
| 259 |
(cl:multiple-value-bind . p1multiple-value-bind) |
| 260 |
(cl:multiple-value-setq . p1multiple-value-setq) |
| 261 |
(cl:progn . p1progn) |
| 262 |
(cl:progv . p1progv) |
| 263 |
(cl:setq . p1setq) |
| 264 |
(cl:psetq . p1psetq) |
| 265 |
(cl:tagbody . p1tagbody) |
| 266 |
(cl:go . p1go) |
| 267 |
(cl:unwind-protect . p1unwind-protect) |
| 268 |
(ordinary . p1ordinary) |
| 269 |
(si:fset . p1fset) |
| 270 |
(variable . p1var) |
| 271 |
(cl:values . p1values) |
| 272 |
(location . p1trivial) ;; Some of these can be improved |
| 273 |
(ffi:c-inline . p1trivial) |
| 274 |
(ffi:c-progn . p1trivial) |
| 275 |
(cl:function . p1trivial) |
| 276 |
(cl:load-time-value . p1trivial) |
| 277 |
(make-form . p1trivial) |
| 278 |
(init-form . p1trivial) |
| 279 |
(mv-prog1 . p1mv-prog1) |
| 280 |
|
| 281 |
(ext:compiler-typecase . p1compiler-typecase) |
| 282 |
(ext:checked-value . p1checked-value) |
| 283 |
)) |
| 284 |
|
| 285 |
(defun make-dispatch-table (alist) |
| 286 |
(loop with hash = (make-hash-table :size (max 128 (* 2 (length alist))) |
| 287 |
:test #'eq) |
| 288 |
for (name . function) in alist |
| 289 |
do (setf (gethash name hash) function) |
| 290 |
finally (return hash))) |
| 291 |
|
| 292 |
(defparameter *c1-dispatch-table* (make-dispatch-table +c1-dispatch-alist+)) |
| 293 |
|
| 294 |
(defparameter *t1-dispatch-table* (make-dispatch-table +t1-dispatch-alist+)) |
| 295 |
|
| 296 |
(defparameter *c2-dispatch-table* (make-dispatch-table +c2-dispatch-alist+)) |
| 297 |
|
| 298 |
(defparameter *set-loc-dispatch-table* (make-dispatch-table +set-loc-dispatch-alist+)) |
| 299 |
|
| 300 |
(defparameter *wt-loc-dispatch-table* (make-dispatch-table +wt-loc-dispatch-alist+)) |
| 301 |
|
| 302 |
(defparameter *t2-dispatch-table* (make-dispatch-table +t2-dispatch-alist+)) |
| 303 |
|
| 304 |
(defparameter *p1-dispatch-table* (make-dispatch-table +p1-dispatch-alist+) |
| 305 |
"Dispatch table for type propagators associated to C1FORMs.") |
| 306 |
|
| 307 |
(defparameter *p0-dispatch-table* (make-dispatch-table '()) |
| 308 |
"Type propagators for known functions.") |
| 309 |
|
| 310 |
(defparameter *cinline-dispatch-table* (make-dispatch-table '())) |