1 ;;;; miscellaneous VM definition noise for the x86
3 ;;;; This software is part of the SBCL system. See the README file for
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.
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))
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*")))
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)
42 (list ,@(mapcar (lambda (name)
43 (symbolicate name "-OFFSET"))
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.
59 (defregset *byte-regs* al ah cl ch dl dh bl bh)
70 (defregset *word-regs* ax cx dx bx si di)
72 ;; double word registers
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
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))
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
117 ;;; (define-storage-base float-registers :finite :size 1)
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)
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)
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
139 (forms `(def!constant ,constant-name ,index))
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
164 #!-win32 5 #!+win32 7))
166 (!define-storage-classes
168 ;; non-immediate constants in the constant pool
171 ;; some FP constants can be generated in the i387 silicon
172 (fp-constant immediate-constant)
173 (fp-single-immediate immediate-constant)
174 (fp-double-immediate immediate-constant)
175 (immediate immediate-constant)
182 (control-stack stack) ; may be pointers, scanned by GC
184 ;; the non-descriptor stacks
185 (signed-stack stack) ; (signed-byte 32)
186 (unsigned-stack stack) ; (unsigned-byte 32)
187 (character-stack stack) ; non-descriptor characters.
188 (sap-stack stack) ; System area pointers.
189 (single-stack stack) ; single-floats
190 (double-stack stack :element-size 2) ; double-floats.
192 (long-stack stack :element-size 3) ; long-floats.
193 (complex-single-stack stack :element-size 2) ; complex-single-floats
194 (complex-double-stack stack :element-size 4) ; complex-double-floats
196 (complex-long-stack stack :element-size 6) ; complex-long-floats
205 ;; things that can go in the integer registers
208 ;; On the X86, we don't have to distinguish between descriptor and
209 ;; non-descriptor registers, because of the conservative GC.
210 ;; Therefore, we use different scs only to distinguish between
211 ;; descriptor and non-descriptor values and to specify size.
213 ;; immediate descriptor objects. Don't have to be seen by GC, but nothing
214 ;; bad will happen if they are. (fixnums, characters, header values, etc).
216 :locations #.*dword-regs*
218 ; :reserve-locations (#.eax-offset)
219 :constant-scs (immediate)
221 :alternate-scs (control-stack))
223 ;; pointer descriptor objects -- must be seen by GC
224 (descriptor-reg registers
225 :locations #.*dword-regs*
227 ; :reserve-locations (#.eax-offset)
228 :constant-scs (constant immediate)
230 :alternate-scs (control-stack))
232 ;; non-descriptor characters
233 (character-reg registers
234 :locations #!-sb-unicode #.*byte-regs*
235 #!+sb-unicode #.*dword-regs*
236 #!+sb-unicode #!+sb-unicode
238 #!-sb-unicode #!-sb-unicode
239 :reserve-locations (#.ah-offset #.al-offset)
240 :constant-scs (immediate)
242 :alternate-scs (character-stack))
244 ;; non-descriptor SAPs (arbitrary pointers into address space)
246 :locations #.*dword-regs*
248 ; :reserve-locations (#.eax-offset)
249 :constant-scs (immediate)
251 :alternate-scs (sap-stack))
253 ;; non-descriptor (signed or unsigned) numbers
254 (signed-reg registers
255 :locations #.*dword-regs*
257 ; :reserve-locations (#.eax-offset)
258 :constant-scs (immediate)
260 :alternate-scs (signed-stack))
261 (unsigned-reg registers
262 :locations #.*dword-regs*
264 ; :reserve-locations (#.eax-offset)
265 :constant-scs (immediate)
267 :alternate-scs (unsigned-stack))
269 ;; miscellaneous objects that must not be seen by GC. Used only as
272 :locations #.*word-regs*
274 ; :reserve-locations (#.ax-offset)
277 :locations #.*byte-regs*
278 ; :reserve-locations (#.al-offset #.ah-offset)
281 ;; that can go in the floating point registers
283 ;; non-descriptor SINGLE-FLOATs
284 (single-reg float-registers
285 :locations (0 1 2 3 4 5 6 7)
286 :constant-scs (fp-constant fp-single-immediate)
288 :alternate-scs (single-stack))
290 ;; non-descriptor DOUBLE-FLOATs
291 (double-reg float-registers
292 :locations (0 1 2 3 4 5 6 7)
293 :constant-scs (fp-constant fp-double-immediate)
295 :alternate-scs (double-stack))
297 ;; non-descriptor LONG-FLOATs
299 (long-reg float-registers
300 :locations (0 1 2 3 4 5 6 7)
301 :constant-scs (fp-constant)
303 :alternate-scs (long-stack))
305 (complex-single-reg float-registers
310 :alternate-scs (complex-single-stack))
312 (complex-double-reg float-registers
317 :alternate-scs (complex-double-stack))
320 (complex-long-reg float-registers
325 :alternate-scs (complex-long-stack))
327 ;; a catch or unwind block
328 (catch-block stack :element-size kludge-nondeterministic-catch-block-size))
330 (eval-when (:compile-toplevel :load-toplevel :execute)
331 (defparameter *byte-sc-names*
332 '(#!-sb-unicode character-reg byte-reg #!-sb-unicode character-stack))
333 (defparameter *word-sc-names* '(word-reg))
334 (defparameter *dword-sc-names*
335 '(any-reg descriptor-reg sap-reg signed-reg unsigned-reg control-stack
336 signed-stack unsigned-stack sap-stack single-stack
337 #!+sb-unicode character-reg #!+sb-unicode character-stack constant))
338 ;;; added by jrd. I guess the right thing to do is to treat floats
339 ;;; as a separate size...
341 ;;; These are used to (at least) determine operand size.
342 (defparameter *float-sc-names* '(single-reg))
343 (defparameter *double-sc-names* '(double-reg double-stack))
346 ;;;; miscellaneous TNs for the various registers
348 (macrolet ((def-misc-reg-tns (sc-name &rest reg-names)
350 (dolist (reg-name reg-names)
351 (let ((tn-name (symbolicate reg-name "-TN"))
352 (offset-name (symbolicate reg-name "-OFFSET")))
353 ;; FIXME: It'd be good to have the special
354 ;; variables here be named with the *FOO*
356 (forms `(defparameter ,tn-name
357 (make-random-tn :kind :normal
358 :sc (sc-or-lose ',sc-name)
361 `(progn ,@(forms)))))
363 (def-misc-reg-tns unsigned-reg eax ebx ecx edx ebp esp edi esi)
364 (def-misc-reg-tns word-reg ax bx cx dx bp sp di si)
365 (def-misc-reg-tns byte-reg al ah bl bh cl ch dl dh)
366 (def-misc-reg-tns single-reg fr0 fr1 fr2 fr3 fr4 fr5 fr6 fr7))
368 ;;; TNs for registers used to pass arguments
369 (defparameter *register-arg-tns*
370 (mapcar (lambda (register-arg-name)
371 (symbol-value (symbolicate register-arg-name "-TN")))
372 *register-arg-names*))
374 ;;; FIXME: doesn't seem to be used in SBCL
377 (defparameter fp-constant-tn
378 (make-random-tn :kind :normal
379 :sc (sc-or-lose 'fp-constant)
380 :offset 31)) ; Offset doesn't get used.
383 ;;; If value can be represented as an immediate constant, then return
384 ;;; the appropriate SC number, otherwise return NIL.
385 (!def-vm-support-routine immediate-constant-sc (value)
387 ((or (integer #.sb!xc:most-negative-fixnum #.sb!xc:most-positive-fixnum)
389 (sc-number-or-lose 'immediate))
391 (when (static-symbol-p value)
392 (sc-number-or-lose 'immediate)))
395 ((0f0 1f0) (sc-number-or-lose 'fp-constant))
396 (t (sc-number-or-lose 'fp-single-immediate))))
399 ((0d0 1d0) (sc-number-or-lose 'fp-constant))
400 (t (sc-number-or-lose 'fp-double-immediate))))
403 (when (or (eql value 0l0) (eql value 1l0)
405 (eql value (log 10l0 2l0))
406 (eql value (log 2.718281828459045235360287471352662L0 2l0))
407 (eql value (log 2l0 10l0))
408 (eql value (log 2l0 2.718281828459045235360287471352662L0)))
409 (sc-number-or-lose 'fp-constant)))))
411 ;; For an immediate TN, return its value encoded for use as a literal.
412 ;; For any other TN, return the TN. Only works for FIXNUMs,
413 ;; STATIC-SYMBOLs, and CHARACTERS (FLOATs and SAPs are handled
415 (defun encode-value-if-immediate (tn)
416 (if (sc-is tn immediate)
417 (let ((val (tn-value tn)))
419 (integer (fixnumize val))
420 (symbol (+ nil-value (static-symbol-offset val)))
421 (character (logior (ash (char-code val) n-widetag-bits)
422 character-widetag))))
425 ;;;; miscellaneous function call parameters
427 ;;; Offsets of special stack frame locations relative to EBP.
429 ;;; Consider the standard prologue PUSH EBP; MOV EBP, ESP: the return
430 ;;; address is at EBP+4, the old control stack frame pointer is at
431 ;;; EBP, the magic 3rd slot is at EBP-4. Then come the locals from
433 (def!constant return-pc-save-offset 0)
434 (def!constant ocfp-save-offset 1)
435 ;;; Let SP be the stack pointer before CALLing, and FP is the frame
436 ;;; pointer after the standard prologue. SP +
437 ;;; FRAME-WORD-OFFSET(SP->FP-OFFSET + I) = FP + FRAME-WORD-OFFSET(I).
438 (def!constant sp->fp-offset 2)
440 (declaim (inline frame-word-offset))
441 (defun frame-word-offset (index)
444 (declaim (inline frame-byte-offset))
445 (defun frame-byte-offset (index)
446 (* (frame-word-offset index) n-word-bytes))
448 ;;; FIXME: This is a bad comment (changed since when?) and there are others
449 ;;; like it in this file. It'd be nice to clarify them. Failing that deleting
450 ;;; them or flagging them with KLUDGE might be better than nothing.
452 ;;; names of these things seem to have changed. these aliases by jrd
453 (def!constant lra-save-offset return-pc-save-offset)
455 (def!constant cfp-offset ebp-offset) ; pfw - needed by stuff in /code
456 ; related to signal context stuff
458 ;;; This is used by the debugger.
459 (def!constant single-value-return-byte-offset 2)
461 ;;; This function is called by debug output routines that want a pretty name
462 ;;; for a TN's location. It returns a thing that can be printed with PRINC.
463 (!def-vm-support-routine location-print-name (tn)
464 (declare (type tn tn))
465 (let* ((sc (tn-sc tn))
466 (sb (sb-name (sc-sb sc)))
467 (offset (tn-offset tn)))
470 (let* ((sc-name (sc-name sc))
471 (name-vec (cond ((member sc-name *byte-sc-names*)
472 *byte-register-names*)
473 ((member sc-name *word-sc-names*)
474 *word-register-names*)
475 ((member sc-name *dword-sc-names*)
476 *dword-register-names*))))
478 (< -1 offset (length name-vec))
479 (svref name-vec offset))
480 ;; FIXME: Shouldn't this be an ERROR?
481 (format nil "<unknown reg: off=~W, sc=~A>" offset sc-name))))
482 (float-registers (format nil "FR~D" offset))
483 (stack (format nil "S~D" offset))
484 (constant (format nil "Const~D" offset))
485 (immediate-constant "Immed")
486 (noise (symbol-name (sc-name sc))))))
487 ;;; FIXME: Could this, and everything that uses it, be made #!+SB-SHOW?
489 (!def-vm-support-routine combination-implementation-style (node)
490 (declare (type sb!c::combination node))
491 (flet ((valid-funtype (args result)
492 (sb!c::valid-fun-use node
493 (sb!c::specifier-type
494 `(function ,args ,result)))))
495 (case (sb!c::combination-fun-source-name node)
498 ((valid-funtype '(fixnum fixnum) '*)
499 (values :direct nil))
500 ((valid-funtype '((signed-byte 32) (signed-byte 32)) '*)
501 (values :direct nil))
502 ((valid-funtype '((unsigned-byte 32) (unsigned-byte 32)) '*)
503 (values :direct nil))
504 (t (values :default nil))))
507 ((and (valid-funtype '((integer 0 29) fixnum) '*)
508 (sb!c::constant-lvar-p (first (sb!c::basic-combination-args node))))
509 (values :transform '(lambda (index integer)
510 (%logbitp integer index))))
511 ((valid-funtype '((integer 0 31) (signed-byte 32)) '*)
512 (values :transform '(lambda (index integer)
513 (%logbitp integer index))))
514 ((valid-funtype '((integer 0 31) (unsigned-byte 32)) '*)
515 (values :transform '(lambda (index integer)
516 (%logbitp integer index))))
517 (t (values :default nil))))
518 (t (values :default nil)))))