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