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