Initial revision
[sb-simd.git] / sbcl-src / src-093 / compiler / x86 / vm.lisp
blob833dd739f00fd23a928d1bbd1acd522f0d997462
1 ;;;; miscellaneous VM definition noise for the x86
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!VM")
14 ;;; the size of an INTEGER representation of a SYSTEM-AREA-POINTER, i.e.
15 ;;; size of a native memory address
16 (deftype sap-int () '(unsigned-byte 32))
18 ;;;; register specs
20 (eval-when (:compile-toplevel :load-toplevel :execute)
21 (defvar *byte-register-names* (make-array 8 :initial-element nil))
22 (defvar *word-register-names* (make-array 16 :initial-element nil))
23 (defvar *dword-register-names* (make-array 16 :initial-element nil))
24 (defvar *float-register-names* (make-array 8 :initial-element nil)))
26 (macrolet ((defreg (name offset size)
27 (let ((offset-sym (symbolicate name "-OFFSET"))
28 (names-vector (symbolicate "*" size "-REGISTER-NAMES*")))
29 `(progn
30 (eval-when (:compile-toplevel :load-toplevel :execute)
31 ;; EVAL-WHEN is necessary because stuff like #.EAX-OFFSET
32 ;; (in the same file) depends on compile-time evaluation
33 ;; of the DEFCONSTANT. -- AL 20010224
34 (def!constant ,offset-sym ,offset))
35 (setf (svref ,names-vector ,offset-sym)
36 ,(symbol-name name)))))
37 ;; FIXME: It looks to me as though DEFREGSET should also
38 ;; define the related *FOO-REGISTER-NAMES* variable.
39 (defregset (name &rest regs)
40 `(eval-when (:compile-toplevel :load-toplevel :execute)
41 (defparameter ,name
42 (list ,@(mapcar (lambda (name)
43 (symbolicate name "-OFFSET"))
44 regs))))))
46 ;; byte registers
48 ;; Note: the encoding here is different than that used by the chip.
49 ;; We use this encoding so that the compiler thinks that AX (and
50 ;; EAX) overlap AL and AH instead of AL and CL.
51 (defreg al 0 :byte)
52 (defreg ah 1 :byte)
53 (defreg cl 2 :byte)
54 (defreg ch 3 :byte)
55 (defreg dl 4 :byte)
56 (defreg dh 5 :byte)
57 (defreg bl 6 :byte)
58 (defreg bh 7 :byte)
59 (defregset *byte-regs* al ah cl ch dl dh bl bh)
61 ;; word registers
62 (defreg ax 0 :word)
63 (defreg cx 2 :word)
64 (defreg dx 4 :word)
65 (defreg bx 6 :word)
66 (defreg sp 8 :word)
67 (defreg bp 10 :word)
68 (defreg si 12 :word)
69 (defreg di 14 :word)
70 (defregset *word-regs* ax cx dx bx si di)
72 ;; double word registers
73 (defreg eax 0 :dword)
74 (defreg ecx 2 :dword)
75 (defreg edx 4 :dword)
76 (defreg ebx 6 :dword)
77 (defreg esp 8 :dword)
78 (defreg ebp 10 :dword)
79 (defreg esi 12 :dword)
80 (defreg edi 14 :dword)
81 (defregset *dword-regs* eax ecx edx ebx esi edi)
83 ;; floating point registers
84 (defreg fr0 0 :float)
85 (defreg fr1 1 :float)
86 (defreg fr2 2 :float)
87 (defreg fr3 3 :float)
88 (defreg fr4 4 :float)
89 (defreg fr5 5 :float)
90 (defreg fr6 6 :float)
91 (defreg fr7 7 :float)
92 (defregset *float-regs* fr0 fr1 fr2 fr3 fr4 fr5 fr6 fr7)
94 ;; registers used to pass arguments
96 ;; the number of arguments/return values passed in registers
97 (def!constant register-arg-count 3)
98 ;; names and offsets for registers used to pass arguments
99 (eval-when (:compile-toplevel :load-toplevel :execute)
100 (defparameter *register-arg-names* '(edx edi esi)))
101 (defregset *register-arg-offsets* edx edi esi))
103 ;;;; SB definitions
105 ;;; Despite the fact that there are only 8 different registers, we consider
106 ;;; them 16 in order to describe the overlap of byte registers. The only
107 ;;; thing we need to represent is what registers overlap. Therefore, we
108 ;;; consider bytes to take one unit, and words or dwords to take two. We
109 ;;; don't need to tell the difference between words and dwords, because
110 ;;; you can't put two words in a dword register.
111 (define-storage-base registers :finite :size 16)
113 ;;; jrd changed this from size 1 to size 8. It doesn't seem to make much
114 ;;; sense to use the 387's idea of a stack; 8 separate registers is easier
115 ;;; to deal with.
116 ;;; the old way:
117 ;;; (define-storage-base float-registers :finite :size 1)
118 ;;; the new way:
119 (define-storage-base float-registers :finite :size 8)
121 (define-storage-base stack :unbounded :size 8)
122 (define-storage-base constant :non-packed)
123 (define-storage-base immediate-constant :non-packed)
124 (define-storage-base noise :unbounded :size 2)
126 ;;;; SC definitions
128 ;;; a handy macro so we don't have to keep changing all the numbers whenever
129 ;;; we insert a new storage class
131 (defmacro !define-storage-classes (&rest classes)
132 (collect ((forms))
133 (let ((index 0))
134 (dolist (class classes)
135 (let* ((sc-name (car class))
136 (constant-name (symbolicate sc-name "-SC-NUMBER")))
137 (forms `(define-storage-class ,sc-name ,index
138 ,@(cdr class)))
139 (forms `(def!constant ,constant-name ,index))
140 (incf index))))
141 `(progn
142 ,@(forms))))
144 ;;; The DEFINE-STORAGE-CLASS call for CATCH-BLOCK refers to the size
145 ;;; of CATCH-BLOCK. The size of CATCH-BLOCK isn't calculated until
146 ;;; later in the build process, and the calculation is entangled with
147 ;;; code which has lots of predependencies, including dependencies on
148 ;;; the prior call of DEFINE-STORAGE-CLASS. The proper way to
149 ;;; unscramble this would be to untangle the code, so that the code
150 ;;; which calculates the size of CATCH-BLOCK can be separated from the
151 ;;; other lots-of-dependencies code, so that the code which calculates
152 ;;; the size of CATCH-BLOCK can be executed early, so that this value
153 ;;; is known properly at this point in compilation. However, that
154 ;;; would be a lot of editing of code that I (WHN 19990131) can't test
155 ;;; until the project is complete. So instead, I set the correct value
156 ;;; by hand here (a sort of nondeterministic guess of the right
157 ;;; answer:-) and add an assertion later, after the value is
158 ;;; calculated, that the original guess was correct.
160 ;;; (What a KLUDGE! Anyone who wants to come in and clean up this mess
161 ;;; has my gratitude.) (FIXME: Maybe this should be me..)
162 (eval-when (:compile-toplevel :load-toplevel :execute)
163 (def!constant kludge-nondeterministic-catch-block-size 6))
165 (!define-storage-classes
167 ;; non-immediate constants in the constant pool
168 (constant constant)
170 ;; some FP constants can be generated in the i387 silicon
171 (fp-constant immediate-constant)
173 (immediate immediate-constant)
176 ;; the stacks
179 ;; the control stack
180 (control-stack stack) ; may be pointers, scanned by GC
182 ;; the non-descriptor stacks
183 (signed-stack stack) ; (signed-byte 32)
184 (unsigned-stack stack) ; (unsigned-byte 32)
185 (character-stack stack) ; non-descriptor characters.
186 (sap-stack stack) ; System area pointers.
187 (single-stack stack) ; single-floats
188 (double-stack stack :element-size 2) ; double-floats.
189 #!+long-float
190 (long-stack stack :element-size 3) ; long-floats.
191 (complex-single-stack stack :element-size 2) ; complex-single-floats
192 (complex-double-stack stack :element-size 4) ; complex-double-floats
193 #!+long-float
194 (complex-long-stack stack :element-size 6) ; complex-long-floats
197 ;; magic SCs
200 (ignore-me noise)
203 ;; things that can go in the integer registers
206 ;; On the X86, we don't have to distinguish between descriptor and
207 ;; non-descriptor registers, because of the conservative GC.
208 ;; Therefore, we use different scs only to distinguish between
209 ;; descriptor and non-descriptor values and to specify size.
211 ;; immediate descriptor objects. Don't have to be seen by GC, but nothing
212 ;; bad will happen if they are. (fixnums, characters, header values, etc).
213 (any-reg registers
214 :locations #.*dword-regs*
215 :element-size 2
216 ; :reserve-locations (#.eax-offset)
217 :constant-scs (immediate)
218 :save-p t
219 :alternate-scs (control-stack))
221 ;; pointer descriptor objects -- must be seen by GC
222 (descriptor-reg registers
223 :locations #.*dword-regs*
224 :element-size 2
225 ; :reserve-locations (#.eax-offset)
226 :constant-scs (constant immediate)
227 :save-p t
228 :alternate-scs (control-stack))
230 ;; non-descriptor characters
231 (character-reg registers
232 :locations #!-sb-unicode #.*byte-regs*
233 #!+sb-unicode #.*dword-regs*
234 #!-sb-unicode #!-sb-unicode
235 :reserve-locations (#.ah-offset #.al-offset)
236 :constant-scs (immediate)
237 :save-p t
238 :alternate-scs (character-stack))
240 ;; non-descriptor SAPs (arbitrary pointers into address space)
241 (sap-reg registers
242 :locations #.*dword-regs*
243 :element-size 2
244 ; :reserve-locations (#.eax-offset)
245 :constant-scs (immediate)
246 :save-p t
247 :alternate-scs (sap-stack))
249 ;; non-descriptor (signed or unsigned) numbers
250 (signed-reg registers
251 :locations #.*dword-regs*
252 :element-size 2
253 ; :reserve-locations (#.eax-offset)
254 :constant-scs (immediate)
255 :save-p t
256 :alternate-scs (signed-stack))
257 (unsigned-reg registers
258 :locations #.*dword-regs*
259 :element-size 2
260 ; :reserve-locations (#.eax-offset)
261 :constant-scs (immediate)
262 :save-p t
263 :alternate-scs (unsigned-stack))
265 ;; miscellaneous objects that must not be seen by GC. Used only as
266 ;; temporaries.
267 (word-reg registers
268 :locations #.*word-regs*
269 :element-size 2
270 ; :reserve-locations (#.ax-offset)
272 (byte-reg registers
273 :locations #.*byte-regs*
274 ; :reserve-locations (#.al-offset #.ah-offset)
277 ;; that can go in the floating point registers
279 ;; non-descriptor SINGLE-FLOATs
280 (single-reg float-registers
281 :locations (0 1 2 3 4 5 6 7)
282 :constant-scs (fp-constant)
283 :save-p t
284 :alternate-scs (single-stack))
286 ;; non-descriptor DOUBLE-FLOATs
287 (double-reg float-registers
288 :locations (0 1 2 3 4 5 6 7)
289 :constant-scs (fp-constant)
290 :save-p t
291 :alternate-scs (double-stack))
293 ;; non-descriptor LONG-FLOATs
294 #!+long-float
295 (long-reg float-registers
296 :locations (0 1 2 3 4 5 6 7)
297 :constant-scs (fp-constant)
298 :save-p t
299 :alternate-scs (long-stack))
301 (complex-single-reg float-registers
302 :locations (0 2 4 6)
303 :element-size 2
304 :constant-scs ()
305 :save-p t
306 :alternate-scs (complex-single-stack))
308 (complex-double-reg float-registers
309 :locations (0 2 4 6)
310 :element-size 2
311 :constant-scs ()
312 :save-p t
313 :alternate-scs (complex-double-stack))
315 #!+long-float
316 (complex-long-reg float-registers
317 :locations (0 2 4 6)
318 :element-size 2
319 :constant-scs ()
320 :save-p t
321 :alternate-scs (complex-long-stack))
323 ;; a catch or unwind block
324 (catch-block stack :element-size kludge-nondeterministic-catch-block-size))
326 (eval-when (:compile-toplevel :load-toplevel :execute)
327 (defparameter *byte-sc-names*
328 '(#!-sb-unicode character-reg byte-reg #!-sb-unicode character-stack))
329 (defparameter *word-sc-names* '(word-reg))
330 (defparameter *dword-sc-names*
331 '(any-reg descriptor-reg sap-reg signed-reg unsigned-reg control-stack
332 signed-stack unsigned-stack sap-stack single-stack
333 #!+sb-unicode character-reg #!+sb-unicode character-stack constant))
334 ;;; added by jrd. I guess the right thing to do is to treat floats
335 ;;; as a separate size...
337 ;;; These are used to (at least) determine operand size.
338 (defparameter *float-sc-names* '(single-reg))
339 (defparameter *double-sc-names* '(double-reg double-stack))
340 ) ; EVAL-WHEN
342 ;;;; miscellaneous TNs for the various registers
344 (macrolet ((def-misc-reg-tns (sc-name &rest reg-names)
345 (collect ((forms))
346 (dolist (reg-name reg-names)
347 (let ((tn-name (symbolicate reg-name "-TN"))
348 (offset-name (symbolicate reg-name "-OFFSET")))
349 ;; FIXME: It'd be good to have the special
350 ;; variables here be named with the *FOO*
351 ;; convention.
352 (forms `(defparameter ,tn-name
353 (make-random-tn :kind :normal
354 :sc (sc-or-lose ',sc-name)
355 :offset
356 ,offset-name)))))
357 `(progn ,@(forms)))))
359 (def-misc-reg-tns unsigned-reg eax ebx ecx edx ebp esp edi esi)
360 (def-misc-reg-tns word-reg ax bx cx dx bp sp di si)
361 (def-misc-reg-tns byte-reg al ah bl bh cl ch dl dh)
362 (def-misc-reg-tns single-reg fr0 fr1 fr2 fr3 fr4 fr5 fr6 fr7))
364 ;;; TNs for registers used to pass arguments
365 (defparameter *register-arg-tns*
366 (mapcar (lambda (register-arg-name)
367 (symbol-value (symbolicate register-arg-name "-TN")))
368 *register-arg-names*))
370 ;;; FIXME: doesn't seem to be used in SBCL
372 ;;; added by pw
373 (defparameter fp-constant-tn
374 (make-random-tn :kind :normal
375 :sc (sc-or-lose 'fp-constant)
376 :offset 31)) ; Offset doesn't get used.
379 ;;; If value can be represented as an immediate constant, then return
380 ;;; the appropriate SC number, otherwise return NIL.
381 (!def-vm-support-routine immediate-constant-sc (value)
382 (typecase value
383 ((or (integer #.sb!xc:most-negative-fixnum #.sb!xc:most-positive-fixnum)
384 #-sb-xc-host system-area-pointer character)
385 (sc-number-or-lose 'immediate))
386 (symbol
387 (when (static-symbol-p value)
388 (sc-number-or-lose 'immediate)))
389 (single-float
390 (when (or (eql value 0f0) (eql value 1f0))
391 (sc-number-or-lose 'fp-constant)))
392 (double-float
393 (when (or (eql value 0d0) (eql value 1d0))
394 (sc-number-or-lose 'fp-constant)))
395 #!+long-float
396 (long-float
397 (when (or (eql value 0l0) (eql value 1l0)
398 (eql value pi)
399 (eql value (log 10l0 2l0))
400 (eql value (log 2.718281828459045235360287471352662L0 2l0))
401 (eql value (log 2l0 10l0))
402 (eql value (log 2l0 2.718281828459045235360287471352662L0)))
403 (sc-number-or-lose 'fp-constant)))))
405 ;;;; miscellaneous function call parameters
407 ;;; offsets of special stack frame locations
408 (def!constant ocfp-save-offset 0)
409 (def!constant return-pc-save-offset 1)
410 (def!constant code-save-offset 2)
412 ;;; FIXME: This is a bad comment (changed since when?) and there are others
413 ;;; like it in this file. It'd be nice to clarify them. Failing that deleting
414 ;;; them or flagging them with KLUDGE might be better than nothing.
416 ;;; names of these things seem to have changed. these aliases by jrd
417 (def!constant lra-save-offset return-pc-save-offset)
419 (def!constant cfp-offset ebp-offset) ; pfw - needed by stuff in /code
420 ; related to signal context stuff
422 ;;; This is used by the debugger.
423 (def!constant single-value-return-byte-offset 2)
425 ;;; This function is called by debug output routines that want a pretty name
426 ;;; for a TN's location. It returns a thing that can be printed with PRINC.
427 (!def-vm-support-routine location-print-name (tn)
428 (declare (type tn tn))
429 (let* ((sc (tn-sc tn))
430 (sb (sb-name (sc-sb sc)))
431 (offset (tn-offset tn)))
432 (ecase sb
433 (registers
434 (let* ((sc-name (sc-name sc))
435 (name-vec (cond ((member sc-name *byte-sc-names*)
436 *byte-register-names*)
437 ((member sc-name *word-sc-names*)
438 *word-register-names*)
439 ((member sc-name *dword-sc-names*)
440 *dword-register-names*))))
441 (or (and name-vec
442 (< -1 offset (length name-vec))
443 (svref name-vec offset))
444 ;; FIXME: Shouldn't this be an ERROR?
445 (format nil "<unknown reg: off=~W, sc=~A>" offset sc-name))))
446 (float-registers (format nil "FR~D" offset))
447 (stack (format nil "S~D" offset))
448 (constant (format nil "Const~D" offset))
449 (immediate-constant "Immed")
450 (noise (symbol-name (sc-name sc))))))
451 ;;; FIXME: Could this, and everything that uses it, be made #!+SB-SHOW?