| 1 |
|
| 2 |
;;; Global variables, flag definitions and utilities. |
| 3 |
|
| 4 |
(in-package "COMPILER") |
| 5 |
|
| 6 |
;;; *inline-blocks* holds the number of C blocks opened for declaring temps for |
| 7 |
;;; intermediate results of the evaluation of inlined function calls. |
| 8 |
|
| 9 |
(defvar *inline-blocks* 0) |
| 10 |
(defvar *opened-c-braces* 0) |
| 11 |
|
| 12 |
(defvar *emitted-functions* nil) |
| 13 |
(defvar *inline-information* nil) |
| 14 |
|
| 15 |
;;; Compiled code uses the following kinds of variables: |
| 16 |
;;; 1. Vi, declared explicitely, either unboxed or not (*lcl*, next-lcl) |
| 17 |
;;; 2. Ti, declared collectively, of type object, may be reused (*temp*, next-temp) |
| 18 |
;;; 4. lexi[j], for lexical variables in local functions |
| 19 |
;;; 5. CLVi, for lexical variables in closures |
| 20 |
|
| 21 |
(defvar *lcl* 0) ; number of local variables |
| 22 |
|
| 23 |
(defvar *temp* 0) ; number of temporary variables |
| 24 |
(defvar *max-temp* 0) ; maximum *temp* reached |
| 25 |
|
| 26 |
(defvar *level* 0) ; nesting level for local functions |
| 27 |
|
| 28 |
(defvar *lex* 0) ; number of lexical variables in local functions |
| 29 |
(defvar *max-lex* 0) ; maximum *lex* reached |
| 30 |
|
| 31 |
(defvar *env* 0) ; number of variables in current form |
| 32 |
(defvar *max-env* 0) ; maximum *env* in whole function |
| 33 |
(defvar *env-lvl* 0) ; number of levels of environments |
| 34 |
(defvar *aux-closure* nil) ; stack allocated closure needed for indirect calls |
| 35 |
(defvar *ihs-used-p* nil) ; function must be registered in IHS? |
| 36 |
|
| 37 |
(defvar *next-cfun* 0) ; holds the last cfun used. |
| 38 |
|
| 39 |
;;; *tail-recursion-info* holds NIL, if tail recursion is impossible. |
| 40 |
;;; If possible, *tail-recursion-info* holds |
| 41 |
;;; ( c1-lambda-form required-arg .... required-arg ), |
| 42 |
;;; where each required-arg is a var-object. |
| 43 |
(defvar *tail-recursion-info* nil) |
| 44 |
(defvar *tail-recursion-mark* nil) |
| 45 |
|
| 46 |
;;; --cmpexit.lsp-- |
| 47 |
;;; |
| 48 |
;;; *exit* holds an 'exit', which is |
| 49 |
;; LABEL instance or LEAVE. |
| 50 |
;;; *unwind-exit* holds a list consisting of: |
| 51 |
;; LABEL instance, LEAVE, FRAME, JUMP, BDS-BIND (each pushed for a single |
| 52 |
;; special binding), or a LCL (which holds the bind stack pointer used to |
| 53 |
;; unbind). |
| 54 |
;;; |
| 55 |
|
| 56 |
(defvar *exit*) |
| 57 |
(defvar *unwind-exit*) |
| 58 |
|
| 59 |
;;; Destination of output of different forms. |
| 60 |
;;; |
| 61 |
;;; Valid *DESTINATION* locations are: |
| 62 |
;;; |
| 63 |
;;; var-object Variable |
| 64 |
;;; loc-object VV Location |
| 65 |
;;; TRASH Value may be thrown away. |
| 66 |
;;; LEAVE Object returned from current function. |
| 67 |
;;; VALUEZ Values vector. |
| 68 |
;;; VALUE0 |
| 69 |
;;; ( VALUE i ) Nth value |
| 70 |
;;; ( BIND var alternative ) Alternative is optional |
| 71 |
;;; ( JUMP-TRUE label ) |
| 72 |
;;; ( JUMP-FALSE label ) |
| 73 |
|
| 74 |
(defvar *destination*) |
| 75 |
|
| 76 |
(defun tmp-destination (loc) |
| 77 |
(case loc |
| 78 |
(VALUEZ 'VALUEZ) |
| 79 |
(TRASH 'TRASH) |
| 80 |
(T 'LEAVE))) |
| 81 |
|
| 82 |
;;; C forms to find out (SETF fname) locations |
| 83 |
(defvar *setf-definitions*) ; holds { name fun-vv name-vv }* |
| 84 |
(defvar *global-cfuns-array*) ; holds { fun-vv fname-loc fun }* |
| 85 |
|
| 86 |
;;; T/NIL flag to determine whether one may generate lisp constant values as C |
| 87 |
;;; structs. |
| 88 |
(defvar *use-static-constants-p* |
| 89 |
#+ecl-min t #-ecl-min nil) |
| 90 |
|
| 91 |
;;; Constants that can be built as C values. |
| 92 |
(defvar *static-constants*) ; holds { ( object c-variable constant ) }* |
| 93 |
|
| 94 |
;;; Pairs for inlining constants. |
| 95 |
(defvar *optimizable-constants*) ; holds { (value . c1form) }* |
| 96 |
|
| 97 |
|
| 98 |
;;; Permanent objects are stored in VV[] that is available for the whole module |
| 99 |
;;; during the whole lifetime of the FASL. Temporary objects are stored in |
| 100 |
;;; VVtemp[] that is allocated in the initialization function and released after |
| 101 |
;;; the FASL is initialized. |
| 102 |
|
| 103 |
(defvar *permanent-objects*) ; holds { vv-record }* |
| 104 |
(defvar *temporary-objects*) ; holds { vv-record }* |
| 105 |
|
| 106 |
;;; ---------------------------------------------------------------------- |
| 107 |
;;; CONVENIENCE FUNCTIONS / MACROS |
| 108 |
;;; |
| 109 |
|
| 110 |
(defmacro with-cxx-env (() &body body) |
| 111 |
`(let ((*opened-c-braces* 0) |
| 112 |
(*inline-blocks* 0) |
| 113 |
(*next-cfun* 0) |
| 114 |
(*inline-information* |
| 115 |
(ext:if-let ((r (machine-inline-information *machine*))) |
| 116 |
(si:copy-hash-table r) |
| 117 |
(make-inline-information *machine*))) |
| 118 |
(*setf-definitions* nil) |
| 119 |
(*global-cfuns-array* nil) |
| 120 |
(*static-constants* nil) |
| 121 |
(*optimizable-constants* (make-optimizable-constants *machine*)) |
| 122 |
(*permanent-objects* (make-array 128 :adjustable t :fill-pointer 0)) |
| 123 |
(*temporary-objects* (make-array 128 :adjustable t :fill-pointer 0)) |
| 124 |
(*compiler-declared-globals* (make-hash-table))) |
| 125 |
,@body)) |
| 126 |
|
| 127 |
;;; Block IR creation environment. |
| 128 |
;;; FIXME Still mixed with CXX bits. Clean this up while separating the backend. |
| 129 |
(defmacro with-bir-env ((&key env level volatile) &body body) |
| 130 |
`(let* ((*lcl* 0) |
| 131 |
(*temp* 0) |
| 132 |
(*max-temp* 0) |
| 133 |
(*lex* 0) |
| 134 |
(*max-lex* 0) |
| 135 |
(*env-lvl* 0) |
| 136 |
(*env* ,env) |
| 137 |
(*max-env* *env*) |
| 138 |
(*level* ,level) |
| 139 |
(*last-label* 0) |
| 140 |
(*volatile* ,volatile) |
| 141 |
;; |
| 142 |
(*ihs-used-p* nil) |
| 143 |
(*aux-closure* nil) |
| 144 |
;; |
| 145 |
(*exit* 'LEAVE) |
| 146 |
(*unwind-exit* '(LEAVE)) |
| 147 |
(*destination* *exit*)) |
| 148 |
,@body)) |
| 149 |
|
| 150 |
(defun-cached env-var-name (n) eql |
| 151 |
(format nil "env~D" n)) |
| 152 |
|
| 153 |
(defun-cached lex-env-var-name (n) eql |
| 154 |
(format nil "lex~D" n)) |
| 155 |
|
| 156 |
(defun next-lcl (&optional name) |
| 157 |
(list 'LCL (incf *lcl*) T |
| 158 |
(if (and name (symbol-package name)) |
| 159 |
(lisp-to-c-name name) |
| 160 |
""))) |
| 161 |
|
| 162 |
(defun next-temp () |
| 163 |
(prog1 *temp* |
| 164 |
(incf *temp*) |
| 165 |
(setq *max-temp* (max *temp* *max-temp*)))) |
| 166 |
|
| 167 |
(defun next-lex () |
| 168 |
(prog1 (cons *level* *lex*) |
| 169 |
(incf *lex*) |
| 170 |
(setq *max-lex* (max *lex* *max-lex*)))) |
| 171 |
|
| 172 |
(defun next-env () |
| 173 |
(prog1 *env* |
| 174 |
(incf *env*) |
| 175 |
(setq *max-env* (max *env* *max-env*)))) |
| 176 |
|
| 177 |
(defun env-grows (possibily) |
| 178 |
;; if additional closure variables are introduced and this is not |
| 179 |
;; last form, we must use a new env. |
| 180 |
(and possibily |
| 181 |
(plusp *env*) |
| 182 |
(dolist (exit *unwind-exit*) |
| 183 |
(case exit |
| 184 |
(LEAVE (return NIL)) |
| 185 |
(BDS-BIND) |
| 186 |
(t (return T)))))) |
| 187 |
|
| 188 |
(defun next-cfun (&optional (prefix "L~D~A") (lisp-name nil)) |
| 189 |
(let ((code (incf *next-cfun*))) |
| 190 |
(format nil prefix code (lisp-to-c-name lisp-name)))) |
| 191 |
|
| 192 |
;;; The macro WITH-INLINE-BLOCKS is used by callers who may optionally need to |
| 193 |
;;; introduce inner lexical scope to create variables. Most notably it is used |
| 194 |
;;; for temporary variables that are bound to local evaluation results. |
| 195 |
(defmacro with-inline-blocks (() &body body) |
| 196 |
`(let ((*inline-blocks* 0) |
| 197 |
(*temp* *temp*) |
| 198 |
(*lcl* *lcl*)) |
| 199 |
,@body |
| 200 |
(close-inline-blocks))) |
| 201 |
|
| 202 |
(defmacro with-lexical-scope (() &body body) |
| 203 |
`(with-inline-blocks () |
| 204 |
(open-inline-block) |
| 205 |
,@body)) |
| 206 |
|
| 207 |
|
| 208 |
;;; *LAST-LABEL* holds the label# of the last used label. This is used by the |
| 209 |
;;; code generator to avoid duplicated labels in the same scope. |
| 210 |
|
| 211 |
(defvar *last-label* 0) |
| 212 |
|
| 213 |
;;; LABEL represents a destination for a possible control transfer. An unique ID |
| 214 |
;;; is assigned to ensure that there are no two labels of the same name. DENV |
| 215 |
;;; captures the dynamic environment of the label, so when we jump to the label |
| 216 |
;;; we may unwind the dynamic state (see the exit manager). USED-P is a flag is |
| 217 |
;;; set to T when the code "jumps" to the label. -- jd 2023-11-25 |
| 218 |
(defstruct (label (:predicate labelp)) |
| 219 |
id |
| 220 |
denv |
| 221 |
used-p) |
| 222 |
|
| 223 |
(defun next-label (used-p) |
| 224 |
(make-label :id (incf *last-label*) :denv *unwind-exit* :used-p used-p)) |
| 225 |
|
| 226 |
;;; This macro binds VAR to a label where forms may exit or jump. |
| 227 |
;;; LABEL may be supplied to reuse a label when it exists. |
| 228 |
(defmacro with-exit-label ((var &optional exit) &body body) |
| 229 |
(ext:with-gensyms (reuse label) |
| 230 |
`(let* ((,label ,exit) |
| 231 |
(,reuse (labelp ,label)) |
| 232 |
(,var (if ,reuse ,label (next-label nil))) |
| 233 |
(*unwind-exit* (adjoin ,var *unwind-exit*))) |
| 234 |
,@body |
| 235 |
(unless ,reuse |
| 236 |
(wt-label ,var))))) |
| 237 |
|
| 238 |
;;; This macro estabilishes a frame to handle dynamic escapes like GO, THROW and |
| 239 |
;;; RETURN-FROM to intercept the control and eval UNWIND-PROTECT cleanup forms. |
| 240 |
;;; ecl_frs_pop is emited by the exit manager or the caller. -- jd 2023-11-19 |
| 241 |
(defmacro with-unwind-frame ((tag) handler-form &body body) |
| 242 |
`(with-lexical-scope () |
| 243 |
(let ((*unwind-exit* (list* 'FRAME *unwind-exit*))) |
| 244 |
(wt-nl "ecl_frs_push(cl_env_copy," ,tag ");") |
| 245 |
(wt-nl "if (__ecl_frs_push_result!=0) {") |
| 246 |
,handler-form |
| 247 |
,@(when body |
| 248 |
`((wt-nl "} else {") |
| 249 |
,@body)) |
| 250 |
(wt-nl "}")))) |
| 251 |
|
| 252 |
(defmacro with-stack-frame ((var &optional loc) &body body) |
| 253 |
(ext:with-gensyms (hlp) |
| 254 |
`(with-lexical-scope () |
| 255 |
(let* ((,var ,(or loc "_ecl_inner_frame")) |
| 256 |
(,hlp "_ecl_inner_frame_aux") |
| 257 |
(*unwind-exit* (list* (list 'STACK ,var) *unwind-exit*))) |
| 258 |
(wt-nl "struct ecl_stack_frame " ,hlp ";") |
| 259 |
(wt-nl *volatile* "cl_object " ,var |
| 260 |
"=ecl_stack_frame_open(cl_env_copy,(cl_object)&" ,hlp ",0);") |
| 261 |
,@body)))) |