1.0.13.44: bug #414 has disappeared
[sbcl/simd.git] / src / compiler / backend.lisp
blobaefdae6c0895c33043c1a24e0460e493e2e9f190
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.
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.
22 (in-package "SB!C")
24 ;;;; miscellaneous backend properties
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*))
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*))
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*))
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*))
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*))
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*))
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*))
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*))
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*))
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*))
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.
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*))
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*))
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*))
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*))
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*))
137 ;;;; VM support routines
139 ;;; FIXME: Do we need this kind of indirection for the VM support
140 ;;; routines any more?
142 ;;; forward declaration
143 (defvar *backend-support-routines*)
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))))
165 (def-vm-support-routines
167 ;; from vm.lisp
168 immediate-constant-sc
169 location-print-name
170 combination-implementation-style
172 ;; from primtype.lisp
173 primitive-type-of
174 primitive-type
176 ;; from c-call.lisp
177 make-call-out-tns
179 ;; from call.lisp
180 standard-arg-location
181 make-return-pc-passing-location
182 make-old-fp-passing-location
183 make-old-fp-save-location
184 make-return-pc-save-location
185 make-arg-count-location
186 make-nfp-tn
187 make-stack-pointer-tn
188 make-number-stack-pointer-tn
189 make-unknown-values-locations
190 select-component-format
192 ;; from nlx.lisp
193 make-nlx-sp-tn
194 make-dynamic-state-tns
195 make-nlx-entry-arg-start-location
197 ;; from support.lisp
198 generate-call-sequence
199 generate-return-sequence
201 ;; for use with scheduler
202 emit-nop
203 location-number))
205 (defprinter (vm-support-routines))
207 (defmacro !def-vm-support-routine (name ll &body body)
208 (unless (member (intern (string name) (find-package "SB!C"))
209 *vm-support-routines*)
210 (warn "unknown VM support routine: ~A" name))
211 (let ((local-name (symbolicate "IMPL-OF-VM-SUPPORT-ROUTINE-" name)))
212 `(progn
213 (defun ,local-name ,ll ,@body)
214 (setf (,(intern (concatenate 'simple-string
215 "VM-SUPPORT-ROUTINES-"
216 (string name))
217 (find-package "SB!C"))
218 *backend-support-routines*)
219 #',local-name))))
221 ;;; the VM support routines
222 (defvar *backend-support-routines* (make-vm-support-routines))
223 (declaim (type vm-support-routines *backend-support-routines*))
225 ;;;; This is a prototype interface to support Christophe Rhodes' new
226 ;;;; (sbcl-0.pre7.57) VOP :GUARD clauses for implementations which
227 ;;;; depend on CPU variants, e.g. the differences between I486,
228 ;;;; Pentium, and Pentium Pro, or the differences between different
229 ;;;; SPARC versions.
231 ;;;; Christophe Rhodes' longer explanation (cut and pasted
232 ;;;; from CLiki SBCL internals site 2001-10-12):
234 In CMUCL, the :guard argument to VOPs provided a way of disallowing
235 the use of a particular VOP in compiled code. As an example, from the
236 SPARC code in CMUCL,
238 (DEFINE-VOP? (FAST-V8-TRUNCATE/SIGNED=>SIGNED? FAST-SAFE-ARITH-OP?)
239 (:TRANSLATE TRUNCATE?)
241 (:GUARD (OR (BACKEND-FEATUREP :SPARC-V8)
242 (AND (BACKEND-FEATUREP :SPARC-V9)
243 (NOT (BACKEND-FEATUREP :SPARC-64)))))
244 ...)
246 and at the IR2 translation stage, the function #'`(LAMBDA () ,GUARD) would be called.
248 Until SBCL-0.7pre57, this is translated as
249 (:GUARD #!+(OR :SPARC-V8 (AND :SPARC-V9 (NOT :SPARC-64))) T
250 #!-(OR :SPARC-V8 (AND :SPARC-V9 (NOT :SPARC-64))) NIL)
251 which means that whether this VOP will ever be used is determined at
252 compiler compile-time depending on the contents of
253 *SHEBANG-FEATURES*?.
255 As of SBCL-0.7pre57, a new special variable,
256 SB-C:*BACKEND-SUBFEATURES*?, is introduced. As of that version, only
257 VOPs translating %log1p? query it, and :PENTIUM-STYLE-FYL2XP1 is the
258 only useful value to be pushed onto that list, for x86. This is not
259 yet an ideal interface, but it does allow for compile-time
260 conditionalization.
263 ;;; The default value of NIL means use only unguarded VOPs. The
264 ;;; initial value is customizeable via
265 ;;; customize-backend-subfeatures.lisp
266 (defvar *backend-subfeatures* '#.sb-cold:*shebang-backend-subfeatures*)
268 ;;; possible *BACKEND-SUBFEATURES* values:
270 ;;; :PENTIUM-STYLE-FYL2XP1 is a useful value for x86 SBCLs to have on
271 ;;; SB-C:*BACKEND-SUBFEATURES*?; it enables the use of the
272 ;;; %flog1p-pentium? VOP rather than the %flog1p? VOP, which is a few
273 ;;; instructions longer.