Eliminate style-warning about undefined type GLOBAL-VAR
[sbcl.git] / src / compiler / vop.lisp
blobc08d599faf23c84333060ff7f325c03bfaa08c85
1 ;;;; structures for the second (virtual machine) intermediate
2 ;;;; representation in the compiler, IR2
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!C")
15 ;;; the largest number of TNs whose liveness changes that we can have
16 ;;; in any block
17 (def!constant local-tn-limit 64)
19 (deftype local-tn-number () `(integer 0 (,local-tn-limit)))
20 (deftype local-tn-count () `(integer 0 ,local-tn-limit))
21 (deftype local-tn-vector () `(simple-vector ,local-tn-limit))
22 (deftype local-tn-bit-vector () `(simple-bit-vector ,local-tn-limit))
24 ;;; type of an SC number
25 (deftype sc-number () `(integer 0 (,sc-number-limit)))
27 ;;; types for vectors indexed by SC numbers
28 (deftype sc-vector () `(simple-vector ,sc-number-limit))
29 (deftype sc-bit-vector () `(simple-bit-vector ,sc-number-limit))
31 ;;; the different policies we can use to determine the coding strategy
32 (deftype ltn-policy ()
33 '(member :safe :small :fast :fast-safe))
35 ;;;; PRIMITIVE-TYPEs
37 ;;; A PRIMITIVE-TYPE is used to represent the aspects of type
38 ;;; interesting to the VM. Selection of IR2 translation templates is
39 ;;; done on the basis of the primitive types of the operands, and the
40 ;;; primitive type of a value is used to constrain the possible
41 ;;; representations of that value.
42 (defstruct (primitive-type (:copier nil))
43 ;; the name of this PRIMITIVE-TYPE
44 (name nil :type symbol :read-only t)
45 ;; a list of the SC numbers for all the SCs that a TN of this type
46 ;; can be allocated in
47 (scs nil :type list :read-only t)
48 ;; the Lisp type equivalent to this type. If this type could never be
49 ;; returned by PRIMITIVE-TYPE, then this is the NIL (or empty) type.
50 ;; TYPE-SPECIFIER is too general - this doesn't allow CLASS/CLASSOID.
51 (specifier (missing-arg) :type (or symbol list) :read-only t)
52 ;; the template used to check that an object is of this type. This is a
53 ;; template of one argument and one result, both of primitive-type T. If
54 ;; the argument is of the correct type, then it is delivered into the
55 ;; result. If the type is incorrect, then an error is signalled.
56 (check nil :type (or template null) :read-only nil))
58 (defprinter (primitive-type)
59 name)
61 ;;;; IR1 annotations used for IR2 conversion
63 ;;; BLOCK-INFO
64 ;;; Holds the IR2-BLOCK structure. If there are overflow blocks,
65 ;;; then this points to the first IR2-BLOCK. The BLOCK-INFO of the
66 ;;; dummy component head and tail are dummy IR2 blocks that begin
67 ;;; and end the emission order thread.
68 ;;;
69 ;;; COMPONENT-INFO
70 ;;; Holds the IR2-COMPONENT structure.
71 ;;;
72 ;;; LVAR-INFO
73 ;;; Holds the IR2-LVAR structure. LVARs whose values aren't used
74 ;;; won't have any. XXX
75 ;;;
76 ;;; CLEANUP-INFO
77 ;;; If non-null, then a TN in which the affected dynamic
78 ;;; environment pointer should be saved after the binding is
79 ;;; instantiated.
80 ;;;
81 ;;; PHYSENV-INFO
82 ;;; Holds the IR2-PHYSENV structure.
83 ;;;
84 ;;; TAIL-SET-INFO
85 ;;; Holds the RETURN-INFO structure.
86 ;;;
87 ;;; NLX-INFO-INFO
88 ;;; Holds the IR2-NLX-INFO structure.
89 ;;;
90 ;;; LEAF-INFO
91 ;;; If a non-set lexical variable, the TN that holds the value in
92 ;;; the home environment. If a constant, then the corresponding
93 ;;; constant TN. If an XEP lambda, then the corresponding
94 ;;; Entry-Info structure.
95 ;;;
96 ;;; BASIC-COMBINATION-INFO
97 ;;; The template chosen by LTN, or
98 ;;; :FULL if this is definitely a full call.
99 ;;; :FUNNY if this is an oddball thing with IR2-convert.
100 ;;; :LOCAL if this is a local call.
102 ;;; NODE-TAIL-P
103 ;;; After LTN analysis, this is true only in combination nodes that are
104 ;;; truly tail recursive.
106 ;;; An IR2-BLOCK holds information about a block that is used during
107 ;;; and after IR2 conversion. It is stored in the BLOCK-INFO slot for
108 ;;; the associated block.
109 (defstruct (ir2-block (:include block-annotation)
110 (:constructor make-ir2-block (block))
111 (:copier nil))
112 ;; the IR2-BLOCK's number, which differs from BLOCK's BLOCK-NUMBER
113 ;; if any blocks are split. This is assigned by lifetime analysis.
114 (number nil :type (or index null))
115 ;; information about unknown-values LVARs that is used by stack
116 ;; analysis to do stack simulation. An UNKNOWN-VALUES LVAR is PUSHED
117 ;; if its DEST is in another block. Similarly, a LVAR is POPPED if
118 ;; its DEST is in this block but has its uses elsewhere. The LVARs
119 ;; are in the order that are pushed/popped in the block. Note that
120 ;; the args to a single MV-COMBINATION appear reversed in POPPED,
121 ;; since we must effectively pop the last argument first. All pops
122 ;; must come before all pushes (although internal MV uses may be
123 ;; interleaved.) POPPED is computed by LTN, and PUSHED is computed
124 ;; by stack analysis.
125 (pushed () :type list)
126 (popped () :type list)
127 ;; the result of stack analysis: lists of all the unknown-values
128 ;; LVARs on the stack at the block start and end, topmost LVAR
129 ;; first.
130 (start-stack () :type list)
131 (end-stack () :type list)
132 ;; the first and last VOP in this block. If there are none, both
133 ;; slots are null.
134 (start-vop nil :type (or vop null))
135 (last-vop nil :type (or vop null))
136 ;; the number of local TNs actually allocated
137 (local-tn-count 0 :type local-tn-count)
138 ;; a vector that maps local TN numbers to TNs. Some entries may be
139 ;; NIL, indicating that that number is unused. (This allows us to
140 ;; delete local conflict information without compressing the LTN
141 ;; numbers.)
143 ;; If an entry is :MORE, then this block contains only a single VOP.
144 ;; This VOP has so many more arguments and/or results that they
145 ;; cannot all be assigned distinct LTN numbers. In this case, we
146 ;; assign all the more args one LTN number, and all the more results
147 ;; another LTN number. We can do this, since more operands are
148 ;; referenced simultaneously as far as conflict analysis is
149 ;; concerned. Note that all these :MORE TNs will be global TNs.
150 (local-tns (make-array local-tn-limit) :type local-tn-vector)
151 ;; Bit-vectors used during lifetime analysis to keep track of
152 ;; references to local TNs. When indexed by the LTN number, the
153 ;; index for a TN is non-zero in WRITTEN if it is ever written in
154 ;; the block, and in LIVE-OUT if the first reference is a read.
155 (written (make-array local-tn-limit :element-type 'bit
156 :initial-element 0)
157 :type local-tn-bit-vector)
158 (live-out (make-array local-tn-limit :element-type 'bit)
159 :type local-tn-bit-vector)
160 ;; This is similar to the above, but is updated by lifetime flow
161 ;; analysis to have a 1 for LTN numbers of TNs live at the end of
162 ;; the block. This takes into account all TNs that aren't :LIVE.
163 (live-in (make-array local-tn-limit :element-type 'bit :initial-element 0)
164 :type local-tn-bit-vector)
165 ;; a thread running through the global-conflicts structures for this
166 ;; block, sorted by TN number
167 (global-tns nil :type (or global-conflicts null))
168 ;; the assembler label that points to the beginning of the code for
169 ;; this block, or NIL when we haven't assigned a label yet
170 (%label nil)
171 ;; the assembler label that points to the trampoline for this block,
172 ;; or NIL if unassigned yet. Only meaningful for local call targets.
173 (%trampoline-label nil)
174 ;; T if the preceding block assumes it can drop thru to %label
175 (dropped-thru-to nil)
176 ;; list of LOCATION-INFO structures describing all the interesting
177 ;; (to the debugger) locations in this block
178 (locations nil :type list))
180 (defprinter (ir2-block)
181 (pushed :test pushed)
182 (popped :test popped)
183 (start-vop :test start-vop)
184 (last-vop :test last-vop)
185 (local-tn-count :test (not (zerop local-tn-count)))
186 (%label :test %label))
188 ;;; An IR2-LVAR structure is used to annotate LVARs that are used as a
189 ;;; function result LVARs or that receive MVs.
190 (defstruct (ir2-lvar
191 (:constructor make-ir2-lvar (primitive-type))
192 (:copier nil))
193 ;; If this is :DELAYED, then this is a single value LVAR for which
194 ;; the evaluation of the use is to be postponed until the evaluation
195 ;; of destination. This can be done for ref nodes or predicates
196 ;; whose destination is an IF.
198 ;; If this is :FIXED, then this LVAR has a fixed number of values,
199 ;; with the TNs in LOCS.
201 ;; If this is :UNKNOWN, then this is an unknown-values LVAR, using
202 ;; the passing locations in LOCS.
204 ;; If this is :UNUSED, then this LVAR should never actually be used
205 ;; as the destination of a value: it is only used tail-recursively.
206 (kind :fixed :type (member :delayed :fixed :unknown :unused))
207 ;; The primitive-type of the first value of this LVAR. This is
208 ;; primarily for internal use during LTN, but it also records the
209 ;; type restriction on delayed references. In multiple-value
210 ;; contexts, this is null to indicate that it is meaningless. This
211 ;; is always (primitive-type (lvar-type cont)), which may be more
212 ;; restrictive than the tn-primitive-type of the value TN. This is
213 ;; becase the value TN must hold any possible type that could be
214 ;; computed (before type checking.) XXX
215 (primitive-type nil :type (or primitive-type null))
216 ;; Locations used to hold the values of the LVAR. If the number of
217 ;; values if fixed, then there is one TN per value. If the number of
218 ;; values is unknown, then this is a two-list of TNs holding the
219 ;; start of the values glob and the number of values. Note that
220 ;; since type checking is the responsibility of the values receiver,
221 ;; these TNs primitive type is only based on the proven type
222 ;; information.
223 (locs nil :type list)
224 (stack-pointer nil :type (or tn null)))
226 (defprinter (ir2-lvar)
227 kind
228 primitive-type
229 locs)
231 ;;; An IR2-COMPONENT serves mostly to accumulate non-code information
232 ;;; about the component being compiled.
233 (defstruct (ir2-component (:copier nil))
234 ;; the counter used to allocate global TN numbers
235 (global-tn-counter 0 :type index)
236 ;; NORMAL-TNS is the head of the list of all the normal TNs that
237 ;; need to be packed, linked through the Next slot. We place TNs on
238 ;; this list when we allocate them so that Pack can find them.
240 ;; RESTRICTED-TNS are TNs that must be packed within a finite SC. We
241 ;; pack these TNs first to ensure that the restrictions will be
242 ;; satisfied (if possible).
244 ;; WIRED-TNs are TNs that must be packed at a specific location. The
245 ;; SC and OFFSET are already filled in.
247 ;; CONSTANT-TNs are non-packed TNs that represent constants.
248 (normal-tns nil :type (or tn null))
249 (restricted-tns nil :type (or tn null))
250 (wired-tns nil :type (or tn null))
251 (constant-tns nil :type (or tn null))
252 ;; a list of all the :COMPONENT TNs (live throughout the component).
253 ;; These TNs will also appear in the {NORMAL,RESTRICTED,WIRED} TNs
254 ;; as appropriate to their location.
255 (component-tns () :type list)
256 ;; If this component has a NFP, then this is it.
257 (nfp nil :type (or tn null))
258 ;; a list of the explicitly specified save TNs (kind
259 ;; :SPECIFIED-SAVE). These TNs will also appear in the
260 ;; {NORMAL,RESTRICTED,WIRED} TNs as appropriate to their location.
261 (specified-save-tns () :type list)
262 ;; a list of all the blocks whose IR2-BLOCK has a non-null value for
263 ;; POPPED. This slot is initialized by LTN-ANALYZE as an input to
264 ;; STACK-ANALYZE.
265 (values-receivers nil :type list)
266 ;; an adjustable vector that records all the constants in the
267 ;; constant pool. A non-immediate :CONSTANT TN with offset 0 refers
268 ;; to the constant in element 0, etc. Normal constants are
269 ;; represented by the placing the CONSTANT leaf in this vector. A
270 ;; load-time constant is distinguished by being a cons (KIND .
271 ;; WHAT). KIND is a keyword indicating how the constant is computed,
272 ;; and WHAT is some context.
274 ;; These load-time constants are recognized:
276 ;; (:entry . <function>)
277 ;; Is replaced by the code pointer for the specified function.
278 ;; This is how compiled code (including DEFUN) gets its hands on
279 ;; a function. <function> is the XEP lambda for the called
280 ;; function; its LEAF-INFO should be an ENTRY-INFO structure.
282 ;; (:label . <label>)
283 ;; Is replaced with the byte offset of that label from the start
284 ;; of the code vector (including the header length.)
286 ;; A null entry in this vector is a placeholder for implementation
287 ;; overhead that is eventually stuffed in somehow.
288 (constants (make-array 10 :fill-pointer 0 :adjustable t) :type vector)
289 ;; some kind of info about the component's run-time representation.
290 ;; This is filled in by the VM supplied SELECT-COMPONENT-FORMAT function.
291 format
292 ;; a list of the ENTRY-INFO structures describing all of the entries
293 ;; into this component. Filled in by entry analysis.
294 (entries nil :type list)
295 ;; head of the list of :ALIAS TNs in this component, threaded by TN-NEXT
296 (alias-tns nil :type (or tn null))
297 ;; SPILLED-VOPS is a hashtable translating from "interesting" VOPs
298 ;; to a list of the TNs spilled at that VOP. This is used when
299 ;; computing debug info so that we don't consider the TN's value to
300 ;; be valid when it is in fact somewhere else. SPILLED-TNS has T for
301 ;; every "interesting" TN that is ever spilled, providing a
302 ;; representation that is more convenient some places.
303 (spilled-vops (make-hash-table :test 'eq) :type hash-table)
304 (spilled-tns (make-hash-table :test 'eq) :type hash-table)
305 ;; dynamic vop count info. This is needed by both ir2-convert and
306 ;; setup-dynamic-count-info. (But only if we are generating code to
307 ;; collect dynamic statistics.)
308 #!+sb-dyncount
309 (dyncount-info nil :type (or null dyncount-info)))
311 ;;; An ENTRY-INFO condenses all the information that the dumper needs
312 ;;; to create each XEP's function entry data structure. ENTRY-INFO
313 ;;; structures are sometimes created before they are initialized,
314 ;;; since IR2 conversion may need to compile a forward reference. In
315 ;;; this case the slots aren't actually initialized until entry
316 ;;; analysis runs.
317 (defstruct (entry-info (:copier nil))
318 ;; TN, containing closure (if needed) for this function in the home
319 ;; environment.
320 (closure-tn nil :type (or null tn))
321 ;; a label pointing to the entry vector for this function, or NIL
322 ;; before ENTRY-ANALYZE runs
323 (offset nil :type (or label null))
324 ;; If this function was defined using DEFUN, then this is the name
325 ;; of the function, a symbol or (SETF <symbol>). Otherwise, this is
326 ;; some string that is intended to be informative.
327 (name "<not computed>" :type (or simple-string list symbol))
328 ;; the argument list that the function was defined with.
329 (arguments nil :type list)
330 ;; a function type specifier representing the arguments and results
331 ;; of this function
332 (type 'function :type (or list (member function)))
333 ;; docstring and/or xref information for the XEP
334 (info nil :type (or null simple-vector string (cons string simple-vector))))
336 ;;; An IR2-PHYSENV is used to annotate non-LET LAMBDAs with their
337 ;;; passing locations. It is stored in the PHYSENV-INFO.
338 (defstruct (ir2-physenv (:copier nil))
339 ;; TN info for closed-over things within the function: an alist
340 ;; mapping from NLX-INFOs and LAMBDA-VARs to TNs holding the
341 ;; corresponding thing within this function
343 ;; Elements of this list have a one-to-one correspondence with
344 ;; elements of the PHYSENV-CLOSURE list of the PHYSENV object that
345 ;; links to us.
346 (closure (missing-arg) :type list :read-only t)
347 ;; the TNs that hold the OLD-FP and RETURN-PC within the function.
348 ;; We always save these so that the debugger can do a backtrace,
349 ;; even if the function has no return (and thus never uses them).
350 ;; Null only temporarily.
351 (old-fp nil :type (or tn null))
352 (return-pc nil :type (or tn null))
353 ;; The passing location for the RETURN-PC. The return PC is treated
354 ;; differently from the other arguments, since in some
355 ;; implementations we may use a call instruction that requires the
356 ;; return PC to be passed in a particular place.
357 (return-pc-pass (missing-arg) :type tn :read-only t)
358 ;; True if this function has a frame on the number stack. This is
359 ;; set by representation selection whenever it is possible that some
360 ;; function in our tail set will make use of the number stack.
361 (number-stack-p nil :type boolean)
362 ;; a list of all the :ENVIRONMENT TNs live in this environment
363 (live-tns nil :type list)
364 ;; a list of all the :DEBUG-ENVIRONMENT TNs live in this environment
365 (debug-live-tns nil :type list)
366 ;; a label that marks the start of elsewhere code for this function,
367 ;; or null until this label is assigned by codegen. Used for
368 ;; maintaining the debug source map.
369 (elsewhere-start nil :type (or label null))
370 ;; a label that marks the first location in this function at which
371 ;; the environment is properly initialized, i.e. arguments moved
372 ;; from their passing locations, etc. This is the start of the
373 ;; function as far as the debugger is concerned.
374 (environment-start nil :type (or label null))
375 (closure-save-tn nil :type (or tn null))
376 #!+unwind-to-frame-and-call-vop
377 (bsp-save-tn nil :type (or tn null)))
379 (defprinter (ir2-physenv)
380 closure
381 old-fp
382 return-pc
383 return-pc-pass
384 closure-save-tn)
386 ;;; A RETURN-INFO is used by GTN to represent the return strategy and
387 ;;; locations for all the functions in a given TAIL-SET. It is stored
388 ;;; in the TAIL-SET-INFO.
389 (defstruct (return-info (:copier nil))
390 ;; The return convention used:
391 ;; -- If :UNKNOWN, we use the standard return convention.
392 ;; -- If :FIXED, we use the known-values convention.
393 (kind (missing-arg) :type (member :fixed :unknown))
394 ;; the number of values returned, or :UNKNOWN if we don't know.
395 ;; COUNT may be known when KIND is :UNKNOWN, since we may choose the
396 ;; standard return convention for other reasons.
397 (count (missing-arg) :type (or index (member :unknown)))
398 ;; If count isn't :UNKNOWN, then this is a list of the
399 ;; primitive-types of each value.
400 (types () :type list)
401 ;; If kind is :FIXED, then this is the list of the TNs that we
402 ;; return the values in.
403 (locations () :type list))
404 (defprinter (return-info)
405 kind
406 count
407 types
408 locations)
410 (defstruct (ir2-nlx-info (:copier nil))
411 ;; If the kind is :ENTRY (a lexical exit), then in the home
412 ;; environment, this holds a VALUE-CELL object containing the unwind
413 ;; block pointer. In the other cases nobody directly references the
414 ;; unwind-block, so we leave this slot null.
415 (home nil :type (or tn null))
416 ;; the saved control stack pointer
417 (save-sp (missing-arg) :type tn)
418 ;; the list of dynamic state save TNs
419 (dynamic-state (list* (make-stack-pointer-tn)
420 (make-dynamic-state-tns))
421 :type list)
422 ;; the target label for NLX entry
423 (target (gen-label) :type label))
424 (defprinter (ir2-nlx-info)
425 home
426 save-sp
427 dynamic-state)
429 ;;;; VOPs and templates
431 ;;; A VOP is a Virtual Operation. It represents an operation and the
432 ;;; operands to the operation.
433 (def!struct (vop (:constructor make-vop (block node info args results))
434 (:copier nil))
435 ;; VOP-INFO structure containing static info about the operation
436 (info nil :type (or vop-info null))
437 ;; the IR2-BLOCK this VOP is in
438 (block (missing-arg) :type ir2-block)
439 ;; VOPs evaluated after and before this one. Null at the
440 ;; beginning/end of the block, and temporarily during IR2
441 ;; translation.
442 (next nil :type (or vop null))
443 (prev nil :type (or vop null))
444 ;; heads of the TN-REF lists for operand TNs, linked using the
445 ;; ACROSS slot
446 (args nil :type (or tn-ref null))
447 (results nil :type (or tn-ref null))
448 ;; head of the list of write refs for each explicitly allocated
449 ;; temporary, linked together using the ACROSS slot
450 (temps nil :type (or tn-ref null))
451 ;; head of the list of all TN-REFs for references in this VOP,
452 ;; linked by the NEXT-REF slot. There will be one entry for each
453 ;; operand and two (a read and a write) for each temporary.
454 (refs nil :type (or tn-ref null))
455 ;; stuff that is passed uninterpreted from IR2 conversion to
456 ;; codegen. The meaning of this slot is totally dependent on the VOP.
457 codegen-info
458 ;; the node that generated this VOP, for keeping track of debug info
459 (node nil :type (or node null))
460 ;; LOCAL-TN-BIT-VECTOR representing the set of TNs live after args
461 ;; are read and before results are written. This is only filled in
462 ;; when VOP-INFO-SAVE-P is non-null.
463 (save-set nil :type (or local-tn-bit-vector null)))
464 (defprinter (vop)
465 (info :prin1 (vop-info-name info))
466 args
467 results
468 (codegen-info :test codegen-info))
470 ;;; A TN-REF object contains information about a particular reference
471 ;;; to a TN. The information in TN-REFs largely determines how TNs are
472 ;;; packed.
473 (def!struct (tn-ref (:constructor make-tn-ref (tn write-p))
474 (:copier nil))
475 ;; the TN referenced
476 (tn (missing-arg) :type tn)
477 ;; Is this is a write reference? (as opposed to a read reference)
478 (write-p nil :type boolean)
479 ;; the link for a list running through all TN-REFs for this TN of
480 ;; the same kind (read or write)
481 (next nil :type (or tn-ref null))
482 ;; the VOP where the reference happens, or NIL temporarily
483 (vop nil :type (or vop null))
484 ;; the link for a list of all TN-REFs in VOP, in reverse order of
485 ;; reference
486 (next-ref nil :type (or tn-ref null))
487 ;; the link for a list of the TN-REFs in VOP of the same kind
488 ;; (argument, result, temp)
489 (across nil :type (or tn-ref null))
490 ;; If true, this is a TN-REF also in VOP whose TN we would like
491 ;; packed in the same location as our TN. Read and write refs are
492 ;; always paired: TARGET in the read points to the write, and
493 ;; vice-versa.
494 (target nil :type (or null tn-ref))
495 ;; the load TN allocated for this operand, if any
496 (load-tn nil :type (or tn null)))
497 (defprinter (tn-ref)
499 write-p
500 (vop :test vop :prin1 (vop-info-name (vop-info vop))))
502 ;;; A TEMPLATE object represents a particular IR2 coding strategy for
503 ;;; a known function.
504 (def!struct (template (:constructor nil)
505 #-sb-xc-host (:pure t))
506 ;; the symbol name of this VOP. This is used when printing the VOP
507 ;; and is also used to provide a handle for definition and
508 ;; translation.
509 (name nil :type symbol)
510 ;; the arg/result type restrictions. We compute this from the
511 ;; PRIMITIVE-TYPE restrictions to make life easier for IR1 phases
512 ;; that need to anticipate LTN's template selection.
513 (type (missing-arg) :type ctype)
514 ;; lists of restrictions on the argument and result types. A
515 ;; restriction may take several forms:
516 ;; -- The restriction * is no restriction at all.
517 ;; -- A restriction (:OR <primitive-type>*) means that the operand
518 ;; must have one of the specified primitive types.
519 ;; -- A restriction (:CONSTANT <predicate> <type-spec>) means that the
520 ;; argument (not a result) must be a compile-time constant that
521 ;; satisfies the specified predicate function. In this case, the
522 ;; constant value will be passed as an info argument rather than
523 ;; as a normal argument. <type-spec> is a Lisp type specifier for
524 ;; the type tested by the predicate, used when we want to represent
525 ;; the type constraint as a Lisp function type.
527 ;; If RESULT-TYPES is :CONDITIONAL, then this is an IF-FOO style
528 ;; conditional that yields its result as a control transfer. The
529 ;; emit function takes two info arguments: the target label and a
530 ;; boolean flag indicating whether to negate the sense of the test.
532 ;; If RESULT-TYPES is a cons whose car is :CONDITIONAL, then this is
533 ;; a flag-setting VOP. The rest is a list of condition descriptors to
534 ;; be interpreted by the BRANCH-IF VOP (see $ARCH/pred.lisp).
535 (arg-types nil :type list)
536 (result-types nil :type (or list (member :conditional) (cons (eql :conditional))))
537 ;; the primitive type restriction applied to each extra argument or
538 ;; result following the fixed operands. If NIL, no extra
539 ;; args/results are allowed. Otherwise, either * or a (:OR ...) list
540 ;; as described for the {ARG,RESULT}-TYPES.
541 (more-args-type nil :type (or (member nil *) cons))
542 (more-results-type nil :type (or (member nil *) cons))
543 ;; If true, this is a function that is called with no arguments to
544 ;; see whether this template can be emitted. This is used to
545 ;; conditionally compile for different target hardware
546 ;; configuarations (e.g. FP hardware.)
547 (guard nil :type (or function null))
548 ;; the policy under which this template is the best translation.
549 ;; Note that LTN might use this template under other policies if it
550 ;; can't figure out anything better to do.
551 (ltn-policy (missing-arg) :type ltn-policy)
552 ;; the base cost for this template, given optimistic assumptions
553 ;; such as no operand loading, etc.
554 (cost (missing-arg) :type index)
555 ;; If true, then this is a short noun-like phrase describing what
556 ;; this VOP "does", i.e. the implementation strategy. This is for
557 ;; use in efficiency notes.
558 (note nil :type (or string null))
559 ;; the number of trailing arguments to VOP or %PRIMITIVE that we
560 ;; bundle into a list and pass into the emit function. This provides
561 ;; a way to pass uninterpreted stuff directly to the code generator.
562 (info-arg-count 0 :type index))
563 (defprinter (template)
564 name
565 arg-types
566 result-types
567 (more-args-type :test more-args-type :prin1 more-args-type)
568 (more-results-type :test more-results-type :prin1 more-results-type)
569 ltn-policy
570 cost
571 (note :test note)
572 (info-arg-count :test (not (zerop info-arg-count))))
574 ;;; A VOP-INFO object holds the constant information for a given
575 ;;; virtual operation. We include TEMPLATE so that functions with a
576 ;;; direct VOP equivalent can be translated easily.
577 (def!struct (vop-info
578 (:include template)
579 (:make-load-form-fun ignore-it))
580 ;; side effects of this VOP and side effects that affect the value
581 ;; of this VOP
582 (effects (missing-arg) :type attributes)
583 (affected (missing-arg) :type attributes)
584 ;; If true, causes special casing of TNs live after this VOP that
585 ;; aren't results:
586 ;; -- If T, all such TNs that are allocated in a SC with a defined
587 ;; save-sc will be saved in a TN in the save SC before the VOP
588 ;; and restored after the VOP. This is used by call VOPs. A bit
589 ;; vector representing the live TNs is stored in the VOP-SAVE-SET.
590 ;; -- If :FORCE-TO-STACK, all such TNs will made into :ENVIRONMENT TNs
591 ;; and forced to be allocated in SCs without any save-sc. This is
592 ;; used by NLX entry vops.
593 ;; -- If :COMPUTE-ONLY, just compute the save set, don't do any saving.
594 ;; This is used to get the live variables for debug info.
595 (save-p nil :type (member t nil :force-to-stack :compute-only))
596 ;; info for automatic emission of move-arg VOPs by representation
597 ;; selection. If NIL, then do nothing special. If non-null, then
598 ;; there must be a more arg. Each more arg is moved to its passing
599 ;; location using the appropriate representation-specific MOVE-ARG
600 ;; VOP. The first (fixed) argument must be the control-stack frame
601 ;; pointer for the frame to move into. The first info arg is the
602 ;; list of passing locations.
604 ;; Additional constraints depend on the value:
606 ;; :FULL-CALL
607 ;; None.
609 ;; :LOCAL-CALL
610 ;; The second (fixed) arg is the NFP for the called function (from
611 ;; ALLOCATE-FRAME.)
613 ;; :KNOWN-RETURN
614 ;; If needed, the old NFP is computed using COMPUTE-OLD-NFP.
615 (move-args nil :type (member nil :full-call :local-call :known-return))
616 ;; a list of sc-vectors representing the loading costs of each fixed
617 ;; argument and result
618 (arg-costs nil :type list)
619 (result-costs nil :type list)
620 ;; if true, SC-VECTORs representing the loading costs for any more
621 ;; args and results
622 (more-arg-costs nil :type (or sc-vector null))
623 (more-result-costs nil :type (or sc-vector null))
624 ;; lists of SC-VECTORs mapping each SC to the SCs that we can load
625 ;; into. If a SC is directly acceptable to the VOP, then the entry
626 ;; is T. Otherwise, it is a list of the SC numbers of all the SCs
627 ;; that we can load into. This list will be empty if there is no
628 ;; load function which loads from that SC to an SC allowed by the
629 ;; operand SC restriction.
630 (arg-load-scs nil :type list)
631 (result-load-scs nil :type list)
632 ;; a function that emits assembly code for a use of this VOP when it
633 ;; is called with the VOP structure. This is null if this VOP has no
634 ;; specified generator (i.e. if it exists only to be inherited by
635 ;; other VOPs).
636 (generator-function nil :type (or function null))
637 ;; a list of things that are used to parameterize an inherited
638 ;; generator. This allows the same generator function to be used for
639 ;; a group of VOPs with similar implementations.
640 (variant nil :type list)
641 ;; the number of arguments and results. Each regular arg/result
642 ;; counts as one, and all the more args/results together count as 1.
643 (num-args 0 :type index)
644 (num-results 0 :type index)
645 ;; a vector of the temporaries the vop needs. See EMIT-VOP
646 ;; in vmdef for information on how the temps are encoded.
647 (temps nil :type (or null (simple-array (unsigned-byte 16) 1)))
648 ;; the order all the refs for this vop should be put in. Each
649 ;; operand is assigned a number in the following ordering: args,
650 ;; more-args, results, more-results, temps. This vector represents
651 ;; the order the operands should be put into in the next-ref link.
652 (ref-ordering nil :type (or null (simple-array (unsigned-byte 8) 1)))
653 ;; a vector of the various targets that should be done. Each element
654 ;; encodes the source ref (shifted 8, it is also encoded in
655 ;; MAX-VOP-TN-REFS) and the dest ref index.
656 (targets nil :type (or null (simple-array (unsigned-byte 16) 1))))
658 ;;;; SBs and SCs
660 ;;; copied from docs/internals/retargeting.tex by WHN 19990707:
662 ;;; A Storage Base represents a physical storage resource such as a
663 ;;; register set or stack frame. Storage bases for non-global
664 ;;; resources such as the stack are relativized by the environment
665 ;;; that the TN is allocated in. Packing conflict information is kept
666 ;;; in the storage base, but non-packed storage resources such as
667 ;;; closure environments also have storage bases.
669 ;;; Some storage bases:
670 ;;; General purpose registers
671 ;;; Floating point registers
672 ;;; Boxed (control) stack environment
673 ;;; Unboxed (number) stack environment
674 ;;; Closure environment
676 ;;; A storage class is a potentially arbitrary set of the elements in
677 ;;; a storage base. Although conceptually there may be a hierarchy of
678 ;;; storage classes such as "all registers", "boxed registers", "boxed
679 ;;; scratch registers", this doesn't exist at the implementation
680 ;;; level. Such things can be done by specifying storage classes whose
681 ;;; locations overlap. A TN shouldn't have lots of overlapping SC's as
682 ;;; legal SC's, since time would be wasted repeatedly attempting to
683 ;;; pack in the same locations.
685 ;;; ...
687 ;;; Some SCs:
688 ;;; Reg: any register (immediate objects)
689 ;;; Save-Reg: a boxed register near r15 (registers easily saved in a call)
690 ;;; Boxed-Reg: any boxed register (any boxed object)
691 ;;; Unboxed-Reg: any unboxed register (any unboxed object)
692 ;;; Float-Reg, Double-Float-Reg: float in FP register.
693 ;;; Stack: boxed object on the stack (on control stack)
694 ;;; Word: any 32bit unboxed object on nstack.
695 ;;; Double: any 64bit unboxed object on nstack.
697 ;;; The SB structure represents the global information associated with
698 ;;; a storage base.
699 (def!struct (sb (:make-load-form-fun just-dump-it-normally))
700 ;; name, for printing and reference
701 (name nil :type symbol)
702 ;; the kind of storage base (which determines the packing
703 ;; algorithm)
704 (kind :non-packed :type (member :finite :unbounded :non-packed))
705 ;; the number of elements in the SB. If finite, this is the total
706 ;; size. If unbounded, this is the size that the SB is initially
707 ;; allocated at.
708 (size 0 :type index))
709 (defprinter (sb)
710 name)
712 ;;; A FINITE-SB holds information needed by the packing algorithm for
713 ;;; finite SBs.
714 (def!struct (finite-sb (:include sb))
715 ;; the minimum number of location by which to grow this SB
716 ;; if it is :unbounded
717 (size-increment 1 :type index)
718 ;; current-size must always be a multiple of this. It is assumed
719 ;; to be a power of two.
720 (size-alignment 1 :type index)
721 ;; the number of locations currently allocated in this SB
722 (current-size 0 :type index)
723 ;; the last location packed in, used by pack to scatter TNs to
724 ;; prevent a few locations from getting all the TNs, and thus
725 ;; getting overcrowded, reducing the possibilities for targeting.
726 (last-offset 0 :type index)
727 ;; a vector containing, for each location in this SB, a vector
728 ;; indexed by IR2 block numbers, holding local conflict bit vectors.
729 ;; A TN must not be packed in a given location within a particular
730 ;; block if the LTN number for that TN in that block corresponds to
731 ;; a set bit in the bit-vector.
732 (conflicts '#() :type simple-vector)
733 ;; a vector containing, for each location in this SB, a bit-vector
734 ;; indexed by IR2 block numbers. If the bit corresponding to a block
735 ;; is set, then the location is in use somewhere in the block, and
736 ;; thus has a conflict for always-live TNs.
737 (always-live '#() :type simple-vector)
738 (always-live-count '#() :type simple-vector)
739 ;; a vector containing the TN currently live in each location in the
740 ;; SB, or NIL if the location is unused. This is used during load-tn pack.
741 (live-tns '#() :type simple-vector)
742 ;; the number of blocks for which the ALWAYS-LIVE and CONFLICTS
743 ;; might not be virgin, and thus must be reinitialized when PACK
744 ;; starts. Less then the length of those vectors when not all of the
745 ;; length was used on the previously packed component.
746 (last-block-count 0 :type index))
748 ;;; the SC structure holds the storage base that storage is allocated
749 ;;; in and information used to select locations within the SB
750 (def!struct (sc (:copier nil))
751 ;; name, for printing and reference
752 (name nil :type symbol)
753 ;; the number used to index SC cost vectors
754 (number 0 :type sc-number)
755 ;; the storage base that this SC allocates storage from
756 (sb nil :type (or sb null))
757 ;; the size of elements in this SC, in units of locations in the SB
758 (element-size 0 :type index)
759 ;; if our SB is finite, a list of the locations in this SC
760 (locations nil :type list)
761 ;; a list of the alternate (save) SCs for this SC
762 (alternate-scs nil :type list)
763 ;; a list of the constant SCs that can me moved into this SC
764 (constant-scs nil :type list)
765 ;; true if the values in this SC needs to be saved across calls
766 (save-p nil :type boolean)
767 ;; vectors mapping from SC numbers to information about how to load
768 ;; from the index SC to this one. MOVE-FUNS holds the names of
769 ;; the functions used to do loading, and LOAD-COSTS holds the cost
770 ;; of the corresponding move functions. If loading is impossible,
771 ;; then the entries are NIL. LOAD-COSTS is initialized to have a 0
772 ;; for this SC.
773 (move-funs (make-array sc-number-limit :initial-element nil)
774 :type sc-vector)
775 (load-costs (make-array sc-number-limit :initial-element nil)
776 :type sc-vector)
777 ;; a vector mapping from SC numbers to possibly
778 ;; representation-specific move and coerce VOPs. Each entry is a
779 ;; list of VOP-INFOs for VOPs that move/coerce an object in the
780 ;; index SC's representation into this SC's representation. This
781 ;; vector is filled out with entries for all SCs that can somehow be
782 ;; coerced into this SC, not just those VOPs defined to directly
783 ;; move into this SC (i.e. it allows for operand loading on the move
784 ;; VOP's operands.)
786 ;; When there are multiple applicable VOPs, the template arg and
787 ;; result type restrictions are used to determine which one to use.
788 ;; The list is sorted by increasing cost, so the first applicable
789 ;; VOP should be used.
791 ;; Move (or move-arg) VOPs with descriptor results shouldn't have
792 ;; TNs wired in the standard argument registers, since there may
793 ;; already be live TNs wired in those locations holding the values
794 ;; that we are setting up for unknown-values return.
795 (move-vops (make-array sc-number-limit :initial-element nil)
796 :type sc-vector)
797 ;; the costs corresponding to the MOVE-VOPS. Separate because this
798 ;; info is needed at meta-compile time, while the MOVE-VOPs don't
799 ;; exist till load time. If no move is defined, then the entry is
800 ;; NIL.
801 (move-costs (make-array sc-number-limit :initial-element nil)
802 :type sc-vector)
803 ;; similar to Move-VOPs, except that we only ever use the entries
804 ;; for this SC and its alternates, since we never combine complex
805 ;; representation conversion with argument passing.
806 (move-arg-vops (make-array sc-number-limit :initial-element nil)
807 :type sc-vector)
808 ;; true if this SC or one of its alternates in in the NUMBER-STACK SB.
809 (number-stack-p nil :type boolean)
810 ;; alignment restriction. The offset must be an even multiple of this.
811 ;; this must be a power of two.
812 (alignment 1 :type (and index (integer 1)))
813 ;; a list of locations that we avoid packing in during normal
814 ;; register allocation to ensure that these locations will be free
815 ;; for operand loading. This prevents load-TN packing from thrashing
816 ;; by spilling a lot.
817 (reserve-locations nil :type list))
818 (defprinter (sc)
819 name)
821 ;;;; TNs
823 (def!struct (tn (:include sset-element)
824 (:constructor make-random-tn)
825 (:constructor make-tn (number kind primitive-type sc))
826 (:copier nil))
827 ;; The kind of TN this is:
829 ;; :NORMAL
830 ;; A normal, non-constant TN, representing a variable or temporary.
831 ;; Lifetime information is computed so that packing can be done.
833 ;; :ENVIRONMENT
834 ;; A TN that has hidden references (debugger or NLX), and thus must be
835 ;; allocated for the duration of the environment it is referenced in.
837 ;; :DEBUG-ENVIRONMENT
838 ;; Like :ENVIRONMENT, but is used for TNs that we want to be able to
839 ;; target to/from and that don't absolutely have to be live
840 ;; everywhere. These TNs are live in all blocks in the environment
841 ;; that don't reference this TN.
843 ;; :COMPONENT
844 ;; A TN that implicitly conflicts with all other TNs. No conflict
845 ;; info is computed.
847 ;; :SAVE
848 ;; :SAVE-ONCE
849 ;; A TN used for saving a :NORMAL TN across function calls. The
850 ;; lifetime information slots are unitialized: get the original
851 ;; TN out of the SAVE-TN slot and use it for conflicts. SAVE-ONCE
852 ;; is like :SAVE, except that it is only save once at the single
853 ;; writer of the original TN.
855 ;; :SPECIFIED-SAVE
856 ;; A TN that was explicitly specified as the save TN for another TN.
857 ;; When we actually get around to doing the saving, this will be
858 ;; changed to :SAVE or :SAVE-ONCE.
860 ;; :LOAD
861 ;; A load-TN used to compute an argument or result that is
862 ;; restricted to some finite SB. Load TNs don't have any conflict
863 ;; information. Load TN pack uses a special local conflict
864 ;; determination method.
866 ;; :CONSTANT
867 ;; Represents a constant, with TN-LEAF a CONSTANT leaf. Lifetime
868 ;; information isn't computed, since the value isn't allocated by
869 ;; pack, but is instead generated as a load at each use. Since
870 ;; lifetime analysis isn't done on :CONSTANT TNs, they don't have
871 ;; LOCAL-NUMBERs and similar stuff.
873 ;; :ALIAS
874 ;; A special kind of TN used to represent initialization of local
875 ;; call arguments in the caller. It provides another name for the
876 ;; argument TN so that lifetime analysis doesn't get confused by
877 ;; self-recursive calls. Lifetime analysis treats this the same
878 ;; as :NORMAL, but then at the end merges the conflict info into
879 ;; the original TN and replaces all uses of the alias with the
880 ;; original TN. SAVE-TN holds the aliased TN.
881 (kind (missing-arg)
882 :type (member :normal :environment :debug-environment
883 :save :save-once :specified-save :load :constant
884 :component :alias))
885 ;; the primitive-type for this TN's value. Null in restricted or
886 ;; wired TNs.
887 (primitive-type nil :type (or primitive-type null))
888 ;; If this TN represents a variable or constant, then this is the
889 ;; corresponding LEAF.
890 (leaf nil :type (or leaf null))
891 ;; thread that links TNs together so that we can find them
892 (next nil :type (or tn null))
893 ;; head of TN-REF lists for reads and writes of this TN
894 (reads nil :type (or tn-ref null))
895 (writes nil :type (or tn-ref null))
896 ;; a link we use when building various temporary TN lists
897 (next* nil :type (or tn null))
898 ;; some block that contains a reference to this TN, or NIL if we
899 ;; haven't seen any reference yet. If the TN is local, then this is
900 ;; the block it is local to.
901 (local nil :type (or ir2-block null))
902 ;; If a local TN, the block relative number for this TN. Global TNs
903 ;; whose liveness changes within a block are also assigned a local
904 ;; number during the conflicts analysis of that block. If the TN has
905 ;; no local number within the block, then this is NIL.
906 (local-number nil :type (or local-tn-number null))
907 ;; If this object is a local TN, this slot is a bit-vector with 1
908 ;; for the local-number of every TN that we conflict with.
909 (local-conflicts (make-array local-tn-limit
910 :element-type 'bit
911 :initial-element 0)
912 :type local-tn-bit-vector)
913 ;; head of the list of GLOBAL-CONFLICTS structures for a global TN.
914 ;; This list is sorted by block number (i.e. reverse DFO), allowing
915 ;; the intersection between the lifetimes for two global TNs to be
916 ;; easily found. If null, then this TN is a local TN.
917 ;; KLUDGE: The defstructs for TN and GLOBAL-CONFLICTS are mutually
918 ;; referential, and absent a block-compilation feature, one or the other
919 ;; of the structures can't inline type checks for its referent.
920 ;; Stating this type as (OR NULL GLOBAL-CONFLICTS) instead of the reverse
921 ;; avoids a forward-reference that would crash cold-init.
922 ;; But since TYPEP short-circuits, the constructor is happy with NIL.
923 ;; [See the comments in LAYOUT-OF for further detail]
924 (global-conflicts nil :type (or null global-conflicts))
925 ;; During lifetime analysis, this is used as a pointer into the
926 ;; conflicts chain, for scanning through blocks in reverse DFO.
927 (current-conflict nil)
928 ;; In a :SAVE TN, this is the TN saved. In a :NORMAL or :ENVIRONMENT
929 ;; TN, this is the associated save TN. In TNs with no save TN, this
930 ;; is null.
931 (save-tn nil :type (or tn null))
932 ;; After pack, the SC we packed into. Beforehand, the SC we want to
933 ;; pack into, or null if we don't know.
934 (sc nil :type (or sc null))
935 ;; the offset within the SB that this TN is packed into. This is what
936 ;; indicates that the TN is packed
937 (offset nil :type (or index null))
938 ;; some kind of info about how important this TN is
939 (cost 0 :type fixnum)
940 ;; If a :ENVIRONMENT or :DEBUG-ENVIRONMENT TN, this is the
941 ;; physical environment that the TN is live throughout.
942 (physenv nil :type (or physenv null))
943 ;; The depth of the deepest loop that this TN is used in.
944 (loop-depth 0 :type fixnum))
945 (def!method print-object ((tn tn) stream)
946 (print-unreadable-object (tn stream :type t)
947 ;; KLUDGE: The distinction between PRINT-TN and PRINT-OBJECT on TN is
948 ;; not very mnemonic. -- WHN 20000124
949 (print-tn-guts tn stream)))
951 ;;; The GLOBAL-CONFLICTS structure represents the conflicts for global
952 ;;; TNs. Each global TN has a list of these structures, one for each
953 ;;; block that it is live in. In addition to representing the result of
954 ;;; lifetime analysis, the global conflicts structure is used during
955 ;;; lifetime analysis to represent the set of TNs live at the start of
956 ;;; the IR2 block.
957 (defstruct (global-conflicts
958 (:constructor make-global-conflicts (kind tn block number))
959 (:copier nil))
960 ;; the IR2-BLOCK that this structure represents the conflicts for
961 (block (missing-arg) :type ir2-block)
962 ;; thread running through all the GLOBAL-CONFLICTSs for BLOCK. This
963 ;; thread is sorted by TN number
964 (next-blockwise nil :type (or global-conflicts null))
965 ;; the way that TN is used by BLOCK
967 ;; :READ
968 ;; The TN is read before it is written. It starts the block live,
969 ;; but is written within the block.
971 ;; :WRITE
972 ;; The TN is written before any read. It starts the block dead,
973 ;; and need not have a read within the block.
975 ;; :READ-ONLY
976 ;; The TN is read, but never written. It starts the block live,
977 ;; and is not killed by the block. Lifetime analysis will promote
978 ;; :READ-ONLY TNs to :LIVE if they are live at the block end.
980 ;; :LIVE
981 ;; The TN is not referenced. It is live everywhere in the block.
982 (kind :read-only :type (member :read :write :read-only :live))
983 ;; a local conflicts vector representing conflicts with TNs live in
984 ;; BLOCK. The index for the local TN number of each TN we conflict
985 ;; with in this block is 1. To find the full conflict set, the :LIVE
986 ;; TNs for BLOCK must also be included. This slot is not meaningful
987 ;; when KIND is :LIVE.
988 (conflicts (make-array local-tn-limit
989 :element-type 'bit
990 :initial-element 0)
991 :type local-tn-bit-vector)
992 ;; the TN we are recording conflicts for.
993 (tn (missing-arg) :type tn)
994 ;; thread through all the GLOBAL-CONFLICTSs for TN
995 (next-tnwise nil :type (or global-conflicts null))
996 ;; TN's local TN number in BLOCK. :LIVE TNs don't have local numbers.
997 (number nil :type (or local-tn-number null)))
998 (defprinter (global-conflicts)
1000 block
1001 kind
1002 (number :test number))