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