4dbb9cb22fc36304f1b2caa404fb49a6b40df398
[sbcl.git] / src / compiler / backend.lisp
1 ;;;; This file contains backend-specific data. The original intent, in
2 ;;;; CMU CL, was to allow compilation using different backends, as a
3 ;;;; way of mutating a running CMU CL into a hybrid system which could
4 ;;;; emit code for a different architecture. In SBCL, this is not
5 ;;;; needed, since we have a cross-compiler which runs as an ordinary
6 ;;;; Lisp program under SBCL or other Lisps. However, it still seems
7 ;;;; reasonable to have all backendish things here in a single file.
8 ;;;;
9 ;;;; FIXME: Perhaps someday the vmdef.lisp and/or meta-vmdef.lisp stuff can
10 ;;;; merged into this file, and/or the metaness can go away or at least be
11 ;;;; radically simplified.
12
13 ;;;; This software is part of the SBCL system. See the README file for
14 ;;;; more information.
15 ;;;;
16 ;;;; This software is derived from the CMU CL system, which was
17 ;;;; written at Carnegie Mellon University and released into the
18 ;;;; public domain. The software is in the public domain and is
19 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
20 ;;;; files for more information.
21
22 (in-package "SB!C")
23 \f
24 ;;;; miscellaneous backend properties
25
26 ;;; the number of references that a TN must have to offset the
27 ;;; overhead of saving the TN across a call
28 (defvar *backend-register-save-penalty* 0)
29 (declaim (type index *backend-register-save-penalty*))
30
31 ;;; the byte order of the target machine. :BIG-ENDIAN has the MSB first (e.g.
32 ;;; IBM RT), :LITTLE-ENDIAN has the MSB last (e.g. DEC VAX).
33 (defvar *backend-byte-order*)
34 (declaim (type (member nil :little-endian :big-endian) *backend-byte-order*))
35
36 ;;; translation from SC numbers to SC info structures. SC numbers are always
37 ;;; used instead of names at run time, so changing this vector changes all the
38 ;;; references.
39 (defvar *backend-sc-numbers* (make-array sc-number-limit :initial-element nil))
40 (declaim (type sc-vector *backend-sc-numbers*))
41
42 ;;; a list of all the SBs defined, so that we can easily iterate over them
43 (defvar *backend-sb-list* ())
44 (declaim (type list *backend-sb-list*))
45
46 ;;; translation from template names to template structures
47 (defvar *backend-template-names* (make-hash-table :test 'eq))
48 (declaim (type hash-table *backend-template-names*))
49
50 ;;; hashtables mapping from SC and SB names to the corresponding structures
51 ;;;
52 ;;; CMU CL comment:
53 ;;;   The META versions are only used at meta-compile and load times,
54 ;;;   so the defining macros can change these at meta-compile time
55 ;;;   without breaking the compiler.
56 ;;; FIXME: Couldn't the META versions go away in SBCL now that we don't
57 ;;; have to worry about metacompiling and breaking the compiler?
58 (defvar *backend-sc-names* (make-hash-table :test 'eq))
59 (defvar *backend-sb-names* (make-hash-table :test 'eq))
60 (defvar *backend-meta-sc-names* (make-hash-table :test 'eq))
61 (defvar *backend-meta-sb-names* (make-hash-table :test 'eq))
62 (declaim (type hash-table
63                *backend-sc-names*
64                *backend-sb-names*
65                *backend-meta-sc-names*
66                *backend-meta-sb-names*))
67
68
69 ;;; like *SC-NUMBERS*, but updated at meta-compile time
70 ;;;
71 ;;; FIXME: As per *BACKEND-META-SC-NAMES* and *BACKEND-META-SB-NAMES*,
72 ;;; couldn't we get rid of this in SBCL?
73 (defvar *backend-meta-sc-numbers*
74   (make-array sc-number-limit :initial-element nil))
75 (declaim (type sc-vector *backend-meta-sc-numbers*))
76
77 ;;; translations from primitive type names to the corresponding
78 ;;; primitive-type structure.
79 (defvar *backend-primitive-type-names*
80   (make-hash-table :test 'eq))
81 (declaim (type hash-table *backend-primitive-type-names*))
82
83 ;;; This establishes a convenient handle on primitive type unions, or
84 ;;; whatever. These names can only be used as the :ARG-TYPES or
85 ;;; :RESULT-TYPES for VOPs and can map to anything else that can be
86 ;;; used as :ARG-TYPES or :RESULT-TYPES (e.g. :OR, :CONSTANT).
87 (defvar *backend-primitive-type-aliases* (make-hash-table :test 'eq))
88 (declaim (type hash-table *backend-primitive-type-aliases*))
89
90 ;;; meta-compile time translation from names to primitive types
91 ;;;
92 ;;; FIXME: As per *BACKEND-META-SC-NAMES* and *BACKEND-META-SB-NAMES*,
93 ;;; couldn't we get rid of this in SBCL?
94 (defvar *backend-meta-primitive-type-names* (make-hash-table :test 'eq))
95 (declaim (type hash-table *meta-primitive-type-names*))
96
97 ;;; The primitive type T is somewhat magical, in that it is the only
98 ;;; primitive type that overlaps with other primitive types. An object
99 ;;; of primitive-type T is in the canonical descriptor (boxed or pointer)
100 ;;; representation.
101 ;;;
102 ;;; The T primitive-type is kept in this variable so that people who
103 ;;; have to special-case it can get at it conveniently. This variable
104 ;;; has to be set by the machine-specific VM definition, since the
105 ;;; !DEF-PRIMITIVE-TYPE for T must specify the SCs that boxed objects
106 ;;; can be allocated in.
107 (defvar *backend-t-primitive-type*)
108 (declaim (type primitive-type *backend-t-primitive-type*))
109
110 ;;; a hashtable translating from VOP names to the corresponding VOP-PARSE
111 ;;; structures. This information is only used at meta-compile time.
112 (defvar *backend-parsed-vops* (make-hash-table :test 'eq))
113 (declaim (type hash-table *backend-parsed-vops*))
114
115 ;;; support for the assembler
116 (defvar *backend-instruction-formats* (make-hash-table :test 'eq))
117 (defvar *backend-instruction-flavors* (make-hash-table :test 'equal))
118 (defvar *backend-special-arg-types* (make-hash-table :test 'eq))
119 (declaim (type hash-table
120                *backend-instruction-formats*
121                *backend-instruction-flavors*
122                *backend-special-arg-types*))
123
124 ;;; mappings between CTYPE structures and the corresponding predicate.
125 ;;; The type->predicate mapping is implemented as an alist because
126 ;;; there is no such thing as a TYPE= hash table.
127 (defvar *backend-predicate-types* (make-hash-table :test 'eq))
128 (defvar *backend-type-predicates* nil)
129 (declaim (type hash-table *backend-predicate-types*))
130 (declaim (type list *backend-type-predicates*))
131
132 ;;; a vector of the internal errors defined for this backend, or NIL if
133 ;;; they haven't been installed yet
134 (defvar *backend-internal-errors* nil)
135 (declaim (type (or simple-vector null) *backend-internal-errors*))
136 \f
137 ;;;; VM support routines
138
139 ;;; FIXME: Do we need this kind of indirection for the VM support
140 ;;; routines any more?
141
142 ;;; forward declaration
143 (defvar *backend-support-routines*)
144
145 (macrolet ((def-vm-support-routines (&rest routines)
146              `(progn
147                 (eval-when (:compile-toplevel :load-toplevel :execute)
148                   (defparameter *vm-support-routines* ',routines))
149                 (defstruct (vm-support-routines (:copier nil))
150                   ,@(mapcar (lambda (routine)
151                               `(,routine nil :type (or function null)))
152                             routines))
153                 ,@(mapcar
154                    (lambda (name)
155                      `(defun ,name (&rest args)
156                         (apply (or (,(symbolicate "VM-SUPPORT-ROUTINES-"
157                                                   name)
158                                     *backend-support-routines*)
159                                    (error "machine-specific support ~S ~
160                                            routine undefined"
161                                           ',name))
162                                args)))
163                    routines))))
164
165   (def-vm-support-routines
166
167     ;; from vm.lisp
168     immediate-constant-sc
169     location-print-name
170     combination-implementation-style
171     boxed-immediate-sc-p
172
173     ;; from primtype.lisp
174     primitive-type-of
175     primitive-type
176
177     ;; from c-call.lisp
178     make-call-out-tns
179
180     ;; from call.lisp
181     standard-arg-location
182     make-return-pc-passing-location
183     make-old-fp-passing-location
184     make-old-fp-save-location
185     make-return-pc-save-location
186     make-arg-count-location
187     make-nfp-tn
188     make-stack-pointer-tn
189     make-number-stack-pointer-tn
190     make-unknown-values-locations
191     select-component-format
192
193     ;; from nlx.lisp
194     make-nlx-sp-tn
195     make-dynamic-state-tns
196     make-nlx-entry-arg-start-location
197
198     ;; from pred.lisp
199     convert-conditional-move-p
200
201     ;; from support.lisp
202     generate-call-sequence
203     generate-return-sequence
204
205     ;; for use with scheduler
206     emit-nop
207     location-number))
208
209 (defprinter (vm-support-routines))
210
211 (defmacro !def-vm-support-routine (name ll &body body)
212   (unless (member (intern (string name) (find-package "SB!C"))
213                   *vm-support-routines*)
214     (warn "unknown VM support routine: ~A" name))
215   (let ((local-name (symbolicate "IMPL-OF-VM-SUPPORT-ROUTINE-" name)))
216     `(progn
217        (defun ,local-name ,ll ,@body)
218        (setf (,(intern (concatenate 'simple-string
219                                     "VM-SUPPORT-ROUTINES-"
220                                     (string name))
221                        (find-package "SB!C"))
222               *backend-support-routines*)
223              #',local-name))))
224
225 ;;; the VM support routines
226 (defvar *backend-support-routines* (make-vm-support-routines))
227 (declaim (type vm-support-routines *backend-support-routines*))
228 \f
229 ;;;; This is a prototype interface to support Christophe Rhodes' new
230 ;;;; (sbcl-0.pre7.57) VOP :GUARD clauses for implementations which
231 ;;;; depend on CPU variants, e.g. the differences between I486,
232 ;;;; Pentium, and Pentium Pro, or the differences between different
233 ;;;; SPARC versions.
234
235 ;;;; Christophe Rhodes' longer explanation (cut and pasted
236 ;;;; from CLiki SBCL internals site 2001-10-12):
237 #|
238 In CMUCL, the :guard argument to VOPs provided a way of disallowing
239 the use of a particular VOP in compiled code. As an example, from the
240 SPARC code in CMUCL,
241
242 (DEFINE-VOP? (FAST-V8-TRUNCATE/SIGNED=>SIGNED? FAST-SAFE-ARITH-OP?)
243   (:TRANSLATE TRUNCATE?)
244   ...
245   (:GUARD (OR (BACKEND-FEATUREP :SPARC-V8)
246               (AND (BACKEND-FEATUREP :SPARC-V9)
247                    (NOT (BACKEND-FEATUREP :SPARC-64)))))
248   ...)
249
250 and at the IR2 translation stage, the function #'`(LAMBDA () ,GUARD) would be called.
251
252 Until SBCL-0.7pre57, this is translated as
253   (:GUARD #!+(OR :SPARC-V8 (AND :SPARC-V9 (NOT :SPARC-64))) T
254           #!-(OR :SPARC-V8 (AND :SPARC-V9 (NOT :SPARC-64))) NIL)
255 which means that whether this VOP will ever be used is determined at
256 compiler compile-time depending on the contents of
257 *SHEBANG-FEATURES*?.
258
259 As of SBCL-0.7pre57, a new special variable,
260 SB-C:*BACKEND-SUBFEATURES*?, is introduced. As of that version, only
261 VOPs translating %log1p? query it, and :PENTIUM-STYLE-FYL2XP1 is the
262 only useful value to be pushed onto that list, for x86. This is not
263 yet an ideal interface, but it does allow for compile-time
264 conditionalization.
265 |#
266
267 ;;; The default value of NIL means use only unguarded VOPs. The
268 ;;; initial value is customizeable via
269 ;;; customize-backend-subfeatures.lisp
270 (defvar *backend-subfeatures* '#.sb-cold:*shebang-backend-subfeatures*)
271
272 ;;; possible *BACKEND-SUBFEATURES* values:
273 ;;;
274 ;;; :PENTIUM-STYLE-FYL2XP1 is a useful value for x86 SBCLs to have on
275 ;;; SB-C:*BACKEND-SUBFEATURES*?; it enables the use of the
276 ;;; %flog1p-pentium? VOP rather than the %flog1p? VOP, which is a few
277 ;;; instructions longer.