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