0.6.8.17:
[sbcl.git] / src / compiler / x86 / vm.lisp
1 ;;;; miscellaneous VM definition noise for the x86
2
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.
11
12 (in-package "SB!VM")
13
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-type () '(unsigned-byte 32))
17 ;;; FIXME: This should just named be SAP-INT, not SAP-INT-TYPE. And
18 ;;; grep for SAPINT in the code and replace it with SAP-INT as
19 ;;; appropriate.
20 \f
21 ;;;; register specs
22
23 (eval-when (:compile-toplevel :load-toplevel :execute)
24   (defvar *byte-register-names* (make-array 8 :initial-element nil))
25   (defvar *word-register-names* (make-array 16 :initial-element nil))
26   (defvar *dword-register-names* (make-array 16 :initial-element nil))
27   (defvar *float-register-names* (make-array 8 :initial-element nil)))
28
29 (macrolet ((defreg (name offset size)
30              (let ((offset-sym (symbolicate name "-OFFSET"))
31                    (names-vector (symbolicate "*" size "-REGISTER-NAMES*")))
32                `(progn
33                   (defconstant ,offset-sym ,offset)
34                   (setf (svref ,names-vector ,offset-sym)
35                         ,(symbol-name name)))))
36            ;; FIXME: It looks to me as though DEFREGSET should also
37            ;; define the related *FOO-REGISTER-NAMES* variable.
38            (defregset (name &rest regs)
39              `(eval-when (:compile-toplevel :load-toplevel :execute)
40                 (defparameter ,name
41                   (list ,@(mapcar (lambda (name)
42                                     (symbolicate name "-OFFSET"))
43                                   regs))))))
44
45   ;; byte registers
46   ;;
47   ;; Note: the encoding here is different than that used by the chip.
48   ;; We use this encoding so that the compiler thinks that AX (and
49   ;; EAX) overlap AL and AH instead of AL and CL.
50   (defreg al 0 :byte)
51   (defreg ah 1 :byte)
52   (defreg cl 2 :byte)
53   (defreg ch 3 :byte)
54   (defreg dl 4 :byte)
55   (defreg dh 5 :byte)
56   (defreg bl 6 :byte)
57   (defreg bh 7 :byte)
58   (defregset *byte-regs* al ah cl ch dl dh bl bh)
59
60   ;; word registers
61   (defreg ax 0 :word)
62   (defreg cx 2 :word)
63   (defreg dx 4 :word)
64   (defreg bx 6 :word)
65   (defreg sp 8 :word)
66   (defreg bp 10 :word)
67   (defreg si 12 :word)
68   (defreg di 14 :word)
69   (defregset *word-regs* ax cx dx bx si di)
70
71   ;; double word registers
72   (defreg eax 0 :dword)
73   (defreg ecx 2 :dword)
74   (defreg edx 4 :dword)
75   (defreg ebx 6 :dword)
76   (defreg esp 8 :dword)
77   (defreg ebp 10 :dword)
78   (defreg esi 12 :dword)
79   (defreg edi 14 :dword)
80   (defregset *dword-regs* eax ecx edx ebx esi edi)
81
82   ;; floating point registers
83   (defreg fr0 0 :float)
84   (defreg fr1 1 :float)
85   (defreg fr2 2 :float)
86   (defreg fr3 3 :float)
87   (defreg fr4 4 :float)
88   (defreg fr5 5 :float)
89   (defreg fr6 6 :float)
90   (defreg fr7 7 :float)
91   (defregset *float-regs* fr0 fr1 fr2 fr3 fr4 fr5 fr6 fr7)
92
93   ;; registers used to pass arguments
94   ;;
95   ;; the number of arguments/return values passed in registers
96   (defconstant  register-arg-count 3)
97   ;; names and offsets for registers used to pass arguments
98   (eval-when (:compile-toplevel :load-toplevel :execute)
99     (defparameter *register-arg-names* '(edx edi esi)))
100   (defregset    *register-arg-offsets* edx edi esi))
101 \f
102 ;;;; SB definitions
103
104 ;;; Despite the fact that there are only 8 different registers, we consider
105 ;;; them 16 in order to describe the overlap of byte registers. The only
106 ;;; thing we need to represent is what registers overlap. Therefore, we
107 ;;; consider bytes to take one unit, and words or dwords to take two. We
108 ;;; don't need to tell the difference between words and dwords, because
109 ;;; you can't put two words in a dword register.
110 (define-storage-base registers :finite :size 16)
111
112 ;;; jrd changed this from size 1 to size 8. It doesn't seem to make much
113 ;;; sense to use the 387's idea of a stack; 8 separate registers is easier
114 ;;; to deal with.
115 ;;; the old way:
116 ;;;   (define-storage-base float-registers :finite :size 1)
117 ;;; the new way:
118 (define-storage-base float-registers :finite :size 8)
119
120 (define-storage-base stack :unbounded :size 8)
121 (define-storage-base constant :non-packed)
122 (define-storage-base immediate-constant :non-packed)
123 (define-storage-base noise :unbounded :size 2)
124 \f
125 ;;;; SC definitions
126
127 ;;; a handy macro so we don't have to keep changing all the numbers whenever
128 ;;; we insert a new storage class
129 ;;;
130 ;;; FIXME: This macro is not needed in the runtime target.
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 `(defconstant ,constant-name ,index))
140           (incf index))))
141     `(progn
142        ,@(forms))))
143
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.
159 ;;;
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 (defconstant sb!vm::kludge-nondeterministic-catch-block-size 6)
163
164 (define-storage-classes
165
166   ;; non-immediate contstants in the constant pool
167   (constant constant)
168
169   ;; some FP constants can be generated in the i387 silicon
170   (fp-constant immediate-constant)
171
172   (immediate immediate-constant)
173
174   ;;
175   ;; the stacks
176   ;;
177   
178   ;; the control stack
179   (control-stack stack)                 ; may be pointers, scanned by GC
180
181   ;; the non-descriptor stacks
182   (signed-stack stack)                  ; (signed-byte 32)
183   (unsigned-stack stack)                ; (unsigned-byte 32)
184   (base-char-stack stack)               ; non-descriptor characters.
185   (sap-stack stack)                     ; System area pointers.
186   (single-stack stack)                  ; single-floats
187   (double-stack stack :element-size 2)  ; double-floats.
188   #!+long-float
189   (long-stack stack :element-size 3)    ; long-floats.
190   (complex-single-stack stack :element-size 2)  ; complex-single-floats
191   (complex-double-stack stack :element-size 4)  ; complex-double-floats
192   #!+long-float
193   (complex-long-stack stack :element-size 6)    ; complex-long-floats
194
195   ;;
196   ;; magic SCs
197   ;;
198
199   (ignore-me noise)
200
201   ;;
202   ;; things that can go in the integer registers
203   ;;
204
205   ;; On the X86, we don't have to distinguish between descriptor and
206   ;; non-descriptor registers, because of the conservative GC.
207   ;; Therefore, we use different scs only to distinguish between
208   ;; descriptor and non-descriptor values and to specify size.
209
210   ;; immediate descriptor objects. Don't have to be seen by GC, but nothing
211   ;; bad will happen if they are. (fixnums, characters, header values, etc).
212   (any-reg registers
213            :locations #.*dword-regs*
214            :element-size 2
215 ;          :reserve-locations (#.eax-offset)
216            :constant-scs (immediate)
217            :save-p t
218            :alternate-scs (control-stack))
219
220   ;; pointer descriptor objects -- must be seen by GC
221   (descriptor-reg registers
222                   :locations #.*dword-regs*
223                   :element-size 2
224 ;                 :reserve-locations (#.eax-offset)
225                   :constant-scs (constant immediate)
226                   :save-p t
227                   :alternate-scs (control-stack))
228
229   ;; non-descriptor characters
230   (base-char-reg registers
231                  :locations #.*byte-regs*
232                  :reserve-locations (#.ah-offset #.al-offset)
233                  :constant-scs (immediate)
234                  :save-p t
235                  :alternate-scs (base-char-stack))
236
237   ;; non-descriptor SAPs (arbitrary pointers into address space)
238   (sap-reg registers
239            :locations #.*dword-regs*
240            :element-size 2
241 ;          :reserve-locations (#.eax-offset)
242            :constant-scs (immediate)
243            :save-p t
244            :alternate-scs (sap-stack))
245
246   ;; non-descriptor (signed or unsigned) numbers
247   (signed-reg registers
248               :locations #.*dword-regs*
249               :element-size 2
250 ;             :reserve-locations (#.eax-offset)
251               :constant-scs (immediate)
252               :save-p t
253               :alternate-scs (signed-stack))
254   (unsigned-reg registers
255                 :locations #.*dword-regs*
256                 :element-size 2
257 ;               :reserve-locations (#.eax-offset)
258                 :constant-scs (immediate)
259                 :save-p t
260                 :alternate-scs (unsigned-stack))
261
262   ;; miscellaneous objects that must not be seen by GC. Used only as
263   ;; temporaries.
264   (word-reg registers
265             :locations #.*word-regs*
266             :element-size 2
267 ;           :reserve-locations (#.ax-offset)
268             )
269   (byte-reg registers
270             :locations #.*byte-regs*
271 ;           :reserve-locations (#.al-offset #.ah-offset)
272             )
273
274   ;; that can go in the floating point registers
275
276   ;; non-descriptor SINGLE-FLOATs
277   (single-reg float-registers
278               :locations (0 1 2 3 4 5 6 7)
279               :constant-scs (fp-constant)
280               :save-p t
281               :alternate-scs (single-stack))
282
283   ;; non-descriptor DOUBLE-FLOATs
284   (double-reg float-registers
285               :locations (0 1 2 3 4 5 6 7)
286               :constant-scs (fp-constant)
287               :save-p t
288               :alternate-scs (double-stack))
289
290   ;; non-descriptor LONG-FLOATs
291   #!+long-float
292   (long-reg float-registers
293             :locations (0 1 2 3 4 5 6 7)
294             :constant-scs (fp-constant)
295             :save-p t
296             :alternate-scs (long-stack))
297
298   (complex-single-reg float-registers
299                       :locations (0 2 4 6)
300                       :element-size 2
301                       :constant-scs ()
302                       :save-p t
303                       :alternate-scs (complex-single-stack))
304
305   (complex-double-reg float-registers
306                       :locations (0 2 4 6)
307                       :element-size 2
308                       :constant-scs ()
309                       :save-p t
310                       :alternate-scs (complex-double-stack))
311
312   #!+long-float
313   (complex-long-reg float-registers
314                     :locations (0 2 4 6)
315                     :element-size 2
316                     :constant-scs ()
317                     :save-p t
318                     :alternate-scs (complex-long-stack))
319
320   ;; a catch or unwind block
321   (catch-block stack
322                :element-size sb!vm::kludge-nondeterministic-catch-block-size))
323
324 (eval-when (:compile-toplevel :load-toplevel :execute)
325 (defparameter *byte-sc-names* '(base-char-reg byte-reg base-char-stack))
326 (defparameter *word-sc-names* '(word-reg))
327 (defparameter *dword-sc-names*
328   '(any-reg descriptor-reg sap-reg signed-reg unsigned-reg control-stack
329     signed-stack unsigned-stack sap-stack single-stack constant))
330 ;;; added by jrd. I guess the right thing to do is to treat floats
331 ;;; as a separate size...
332 ;;;
333 ;;; These are used to (at least) determine operand size.
334 (defparameter *float-sc-names* '(single-reg))
335 (defparameter *double-sc-names* '(double-reg double-stack))
336 ) ; EVAL-WHEN
337 \f
338 ;;;; miscellaneous TNs for the various registers
339
340 (macrolet ((def-misc-reg-tns (sc-name &rest reg-names)
341              (collect ((forms))
342                       (dolist (reg-name reg-names)
343                         (let ((tn-name (symbolicate reg-name "-TN"))
344                               (offset-name (symbolicate reg-name "-OFFSET")))
345                           ;; FIXME: It'd be good to have the special
346                           ;; variables here be named with the *FOO*
347                           ;; convention.
348                           (forms `(defparameter ,tn-name
349                                     (make-random-tn :kind :normal
350                                                     :sc (sc-or-lose ',sc-name)
351                                                     :offset
352                                                     ,offset-name)))))
353                       `(progn ,@(forms)))))
354
355   (def-misc-reg-tns unsigned-reg eax ebx ecx edx ebp esp edi esi)
356   (def-misc-reg-tns word-reg ax bx cx dx bp sp di si)
357   (def-misc-reg-tns byte-reg al ah bl bh cl ch dl dh)
358   (def-misc-reg-tns single-reg fr0 fr1 fr2 fr3 fr4 fr5 fr6 fr7))
359
360 ;;; TNs for registers used to pass arguments
361 (defparameter *register-arg-tns*
362   (mapcar (lambda (register-arg-name)
363             (symbol-value (symbolicate register-arg-name "-TN")))
364           *register-arg-names*))
365
366 ;;; FIXME: doesn't seem to be used in SBCL
367 #|
368 ;;; added by pw
369 (defparameter fp-constant-tn
370   (make-random-tn :kind :normal
371                   :sc (sc-or-lose 'fp-constant)
372                   :offset 31))          ; Offset doesn't get used.
373 |#
374 \f
375 ;;; IMMEDIATE-CONSTANT-SC
376 ;;;
377 ;;; If value can be represented as an immediate constant, then return
378 ;;; the appropriate SC number, otherwise return NIL.
379 (!def-vm-support-routine immediate-constant-sc (value)
380   (typecase value
381     ((or fixnum #-sb-xc-host system-area-pointer character)
382      (sc-number-or-lose 'immediate))
383     (symbol
384      (when (static-symbol-p value)
385        (sc-number-or-lose 'immediate)))
386     (single-float
387      (when (or (eql value 0f0) (eql value 1f0))
388        (sc-number-or-lose 'fp-constant)))
389     (double-float
390      (when (or (eql value 0d0) (eql value 1d0))
391        (sc-number-or-lose 'fp-constant)))
392     #!+long-float
393     (long-float
394      (when (or (eql value 0l0) (eql value 1l0)
395                (eql value pi)
396                (eql value (log 10l0 2l0))
397                (eql value (log 2.718281828459045235360287471352662L0 2l0))
398                (eql value (log 2l0 10l0))
399                (eql value (log 2l0 2.718281828459045235360287471352662L0)))
400        (sc-number-or-lose 'fp-constant)))))
401 \f
402 ;;;; miscellaneous function call parameters
403
404 ;;; offsets of special stack frame locations
405 (defconstant ocfp-save-offset 0)
406 (defconstant return-pc-save-offset 1)
407 (defconstant code-save-offset 2)
408
409 ;;; FIXME: This is a bad comment (changed since when?) and there are others
410 ;;; like it in this file. It'd be nice to clarify them. Failing that deleting
411 ;;; them or flagging them with KLUDGE might be better than nothing.
412 ;;;
413 ;;; names of these things seem to have changed. these aliases by jrd
414 (defconstant lra-save-offset return-pc-save-offset)
415
416 (defconstant cfp-offset ebp-offset)     ; pfw - needed by stuff in /code
417                                         ; related to signal context stuff
418
419 ;;; SINGLE-VALUE-RETURN-BYTE-OFFSET
420 ;;;
421 ;;; This is used by the debugger.
422 (defconstant single-value-return-byte-offset 2)
423 \f
424 ;;; This function is called by debug output routines that want a pretty name
425 ;;; for a TN's location. It returns a thing that can be printed with PRINC.
426 (!def-vm-support-routine location-print-name (tn)
427   (declare (type tn tn))
428   (let* ((sc (tn-sc tn))
429          (sb (sb-name (sc-sb sc)))
430          (offset (tn-offset tn)))
431     (ecase sb
432       (registers
433        (let* ((sc-name (sc-name sc))
434               (name-vec (cond ((member sc-name *byte-sc-names*)
435                                *byte-register-names*)
436                               ((member sc-name *word-sc-names*)
437                                *word-register-names*)
438                               ((member sc-name *dword-sc-names*)
439                                *dword-register-names*))))
440          (or (and name-vec
441                   (< -1 offset (length name-vec))
442                   (svref name-vec offset))
443              ;; FIXME: Shouldn't this be an ERROR?
444              (format nil "<unknown reg: off=~D, sc=~A>" offset sc-name))))
445       (float-registers (format nil "FR~D" offset))
446       (stack (format nil "S~D" offset))
447       (constant (format nil "Const~D" offset))
448       (immediate-constant "Immed")
449       (noise (symbol-name (sc-name sc))))))
450 ;;; FIXME: Could this, and everything that uses it, be made #!+SB-SHOW?
451 \f
452 ;;; The loader uses this to convert alien names to the form they need in
453 ;;; the symbol table (for example, prepending an underscore).
454 (defun extern-alien-name (name)
455   (declare (type simple-string name))
456   name)