0.8.21.21: fix & share EXTERN-ALIEN-NAME logic (fixes bug #373)
[sbcl.git] / src / compiler / sparc / vm.lisp
1 ;;;; miscellaneous VM definition noise for the Sparc
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 ;;;; Additional constants
15
16 ;;; NUMBER-STACK-DISPLACEMENT
17 ;;;
18 ;;; The number of bytes reserved above the number stack pointer.  These
19 ;;; slots are required by architecture for a place to spill register windows.
20 ;;;
21 ;;; FIXME: Where is this used?
22 (def!constant number-stack-displacement
23   (* 16 n-word-bytes))
24
25 ;;;; Define the registers
26
27 (eval-when (:compile-toplevel :load-toplevel :execute)
28   (defvar *register-names* (make-array 32 :initial-element nil)))
29
30 (macrolet ((defreg (name offset)
31                (let ((offset-sym (symbolicate name "-OFFSET")))
32                  `(eval-when (:compile-toplevel :load-toplevel :execute)
33                    (def!constant ,offset-sym ,offset)
34                    (setf (svref *register-names* ,offset-sym)
35                         ,(symbol-name name)))))
36
37            (defregset (name &rest regs)
38                 `(eval-when (:compile-toplevel :load-toplevel :execute)
39                   (defparameter ,name
40                     (list ,@(mapcar (lambda (name)
41                                       (symbolicate name "-OFFSET"))
42                                     regs))))))
43   ;; c.f. src/runtime/sparc-lispregs.h
44
45   ;; Globals.  These are difficult to extract from a sigcontext.
46   (defreg zero 0)                               ; %g0
47   (defreg alloc 1)                              ; %g1
48   (defreg null 2)                               ; %g2
49   (defreg csp 3)                                ; %g3
50   (defreg cfp 4)                                ; %g4
51   (defreg bsp 5)                                ; %g5
52   ;; %g6 and %g7 are supposed to be reserved for the system.
53
54   ;; Outs.  These get clobbered when we call into C.
55   (defreg nl0 8)                                ; %o0
56   (defreg nl1 9)                                ; %o1
57   (defreg nl2 10)                               ; %o2
58   (defreg nl3 11)                               ; %o3
59   (defreg nl4 12)                               ; %o4
60   (defreg nl5 13)                               ; %o5
61   (defreg nsp 14)                               ; %o6
62   (defreg nargs 15)                             ; %o7
63
64   ;; Locals.  These are preserved when we call into C.
65   (defreg a0 16)                                ; %l0
66   (defreg a1 17)                                ; %l1
67   (defreg a2 18)                                ; %l2
68   (defreg a3 19)                                ; %l3
69   (defreg a4 20)                                ; %l4
70   (defreg a5 21)                                ; %l5
71   (defreg ocfp 22)                              ; %l6
72   (defreg lra 23)                               ; %l7
73
74   ;; Ins.  These are preserved just like locals.
75   (defreg cname 24)                             ; %i0
76   (defreg lexenv 25)                            ; %i1
77   (defreg l0 26)                                ; %i2
78   (defreg nfp 27)                               ; %i3
79   (defreg cfunc 28)                             ; %i4
80   (defreg code 29)                              ; %i5
81   ;; we can't touch reg 30 if we ever want to return
82   (defreg lip 31)                               ; %i7
83
84   (defregset non-descriptor-regs
85       nl0 nl1 nl2 nl3 nl4 nl5 cfunc nargs nfp)
86   
87   (defregset descriptor-regs
88       a0 a1 a2 a3 a4 a5 ocfp lra cname lexenv l0)
89
90   (defregset *register-arg-offsets*
91       a0 a1 a2 a3 a4 a5))
92 \f
93 ;;;; SB and SC definition
94
95 (define-storage-base registers :finite :size 32)
96 (define-storage-base float-registers :finite :size 64)
97 (define-storage-base control-stack :unbounded :size 8)
98 (define-storage-base non-descriptor-stack :unbounded :size 0)
99 (define-storage-base constant :non-packed)
100 (define-storage-base immediate-constant :non-packed)
101
102 ;;; handy macro so we don't have to keep changing all the numbers
103 ;;; whenever we insert a new storage class
104 (defmacro !define-storage-classes (&rest classes)
105   (do ((forms (list 'progn)
106               (let* ((class (car classes))
107                      (sc-name (car class))
108                      (constant-name (intern (concatenate 'simple-string
109                                                          (string sc-name)
110                                                          "-SC-NUMBER"))))
111                 (list* `(define-storage-class ,sc-name ,index
112                           ,@(cdr class))
113                        `(def!constant ,constant-name ,index)
114                        ;; (The CMU CL version of this macro did
115                        ;;   `(EXPORT ',CONSTANT-NAME)
116                        ;; here, but in SBCL we try to have package
117                        ;; structure described statically in one
118                        ;; master source file, instead of building it
119                        ;; dynamically by letting all the system code
120                        ;; modify it as the system boots.)
121                        forms)))
122        (index 0 (1+ index))
123        (classes classes (cdr classes)))
124       ((null classes)
125        (nreverse forms))))
126
127 ;;; see comment in ../x86/vm.lisp.  The value of 7 was taken from
128 ;;; vm:catch-block-size in a cmucl that I happened to have around
129 ;;; and seems to be working so far    -dan
130 ;;;
131 ;;; arbitrarily taken for alpha, too. - Christophe
132 (def!constant kludge-nondeterministic-catch-block-size 7)
133
134 (!define-storage-classes
135
136   ;; non-immediate constants in the constant pool
137   (constant constant)
138
139   ;; ZERO and NULL are in registers.
140   (zero immediate-constant)
141   (null immediate-constant)
142
143   ;; Anything else that can be an immediate.
144   (immediate immediate-constant)
145
146   ;;
147   ;; the stacks
148   ;;
149
150   ;; The control stack.  (Scanned by GC)
151   (control-stack control-stack)
152
153   ;; The non-descriptor stacks.
154   (signed-stack non-descriptor-stack) ; (signed-byte 32)
155   (unsigned-stack non-descriptor-stack) ; (unsigned-byte 32)
156   (character-stack non-descriptor-stack) ; non-descriptor characters.
157   (sap-stack non-descriptor-stack) ; System area pointers.
158   (single-stack non-descriptor-stack) ; single-floats
159   (double-stack non-descriptor-stack
160                 :element-size 2 :alignment 2) ; double floats.
161   #!+long-float
162   (long-stack non-descriptor-stack :element-size 4 :alignment 4) ; long floats.
163   ;; complex-single-floats
164   (complex-single-stack non-descriptor-stack :element-size 2)
165   ;; complex-double-floats.
166   (complex-double-stack non-descriptor-stack :element-size 4 :alignment 2)
167   #!+long-float
168   ;; complex-long-floats.
169   (complex-long-stack non-descriptor-stack :element-size 8 :alignment 4)
170
171
172   ;; **** Things that can go in the integer registers.
173
174   ;; Immediate descriptor objects.  Don't have to be seen by GC, but nothing
175   ;; bad will happen if they are.  (fixnums, characters, header values, etc).
176   (any-reg
177    registers
178    :locations #.(append non-descriptor-regs descriptor-regs)
179    :constant-scs (zero immediate)
180    :save-p t
181    :alternate-scs (control-stack))
182
183   ;; Pointer descriptor objects.  Must be seen by GC.
184   (descriptor-reg registers
185    :locations #.descriptor-regs
186    :constant-scs (constant null immediate)
187    :save-p t
188    :alternate-scs (control-stack))
189
190   ;; Non-Descriptor characters
191   (character-reg registers
192    :locations #.non-descriptor-regs
193    :constant-scs (immediate)
194    :save-p t
195    :alternate-scs (character-stack))
196
197   ;; Non-Descriptor SAP's (arbitrary pointers into address space)
198   (sap-reg registers
199    :locations #.non-descriptor-regs
200    :constant-scs (immediate)
201    :save-p t
202    :alternate-scs (sap-stack))
203
204   ;; Non-Descriptor (signed or unsigned) numbers.
205   (signed-reg registers
206    :locations #.non-descriptor-regs
207    :constant-scs (zero immediate)
208    :save-p t
209    :alternate-scs (signed-stack))
210   (unsigned-reg registers
211    :locations #.non-descriptor-regs
212    :constant-scs (zero immediate)
213    :save-p t
214    :alternate-scs (unsigned-stack))
215
216   ;; Random objects that must not be seen by GC.  Used only as temporaries.
217   (non-descriptor-reg registers
218    :locations #.non-descriptor-regs)
219
220   ;; Pointers to the interior of objects.  Used only as an temporary.
221   (interior-reg registers
222    :locations (#.lip-offset))
223
224
225   ;; **** Things that can go in the floating point registers.
226
227   ;; Non-Descriptor single-floats.
228   (single-reg float-registers
229    :locations #.(loop for i from 0 to 31 collect i)
230    :reserve-locations (28 29 30 31)
231    :constant-scs ()
232    :save-p t
233    :alternate-scs (single-stack))
234
235   ;; Non-Descriptor double-floats.
236   (double-reg float-registers
237    :locations #.(loop for i from 0 to #!-sparc-64 31 #!+sparc-64 63
238                       by 2 collect i)
239    :element-size 2 :alignment 2
240    :reserve-locations (28 30)
241    :constant-scs ()
242    :save-p t
243    :alternate-scs (double-stack))
244
245   ;; Non-Descriptor double-floats.
246   #!+long-float
247   (long-reg float-registers
248    :locations #.(loop for i from 0 to #!-sparc-64 31 #!+sparc-64 63
249                       by 4 collect i)
250    :element-size 4 :alignment 4
251    :reserve-locations (28)
252    :constant-scs ()
253    :save-p t
254    :alternate-scs (long-stack))
255
256   (complex-single-reg float-registers
257    :locations #.(loop for i from 0 to 31 by 2 collect i)
258    :element-size 2 :alignment 2
259    :reserve-locations (28 30)
260    :constant-scs ()
261    :save-p t
262    :alternate-scs (complex-single-stack))
263
264   (complex-double-reg float-registers
265    :locations #.(loop for i from 0 to #!-sparc-64 31 #!+sparc-64 63
266                       by 4 collect i)
267    :element-size 4 :alignment 4
268    :reserve-locations (28)
269    :constant-scs ()
270    :save-p t
271    :alternate-scs (complex-double-stack))
272
273   #!+long-float
274   (complex-long-reg float-registers
275    :locations #.(loop for i from 0 to #!-sparc-64 31 #!+sparc-64 63
276                       by 8 collect i)
277    :element-size 8 :alignment 8
278    :constant-scs ()
279    :save-p t
280    :alternate-scs (complex-long-stack))
281
282
283   ;; A catch or unwind block.
284   (catch-block control-stack :element-size kludge-nondeterministic-catch-block-size))
285 \f
286 ;;;; Make some miscellaneous TNs for important registers.
287 (macrolet ((defregtn (name sc)
288                (let ((offset-sym (symbolicate name "-OFFSET"))
289                      (tn-sym (symbolicate name "-TN")))
290                  `(defparameter ,tn-sym
291                    (make-random-tn :kind :normal
292                     :sc (sc-or-lose ',sc)
293                     :offset ,offset-sym)))))
294   (defregtn zero any-reg)
295   (defregtn null descriptor-reg)
296   (defregtn code descriptor-reg)
297   (defregtn alloc any-reg)
298   
299   (defregtn nargs any-reg)
300   (defregtn bsp any-reg)
301   (defregtn csp any-reg)
302   (defregtn cfp any-reg)
303   (defregtn ocfp any-reg)
304   (defregtn nsp any-reg))
305 \f
306 ;;; If VALUE can be represented as an immediate constant, then return the
307 ;;; appropriate SC number, otherwise return NIL.
308 (!def-vm-support-routine immediate-constant-sc (value)
309   (typecase value
310     ((integer 0 0)
311      (sc-number-or-lose 'zero))
312     (null
313      (sc-number-or-lose 'null))
314     ((or (integer #.sb!xc:most-negative-fixnum #.sb!xc:most-positive-fixnum)
315          system-area-pointer character)
316      (sc-number-or-lose 'immediate))
317     (symbol
318      (if (static-symbol-p value)
319          (sc-number-or-lose 'immediate)
320          nil))))
321 \f
322 ;;;; function call parameters
323
324 ;;; the SC numbers for register and stack arguments/return values.
325 (def!constant register-arg-scn (meta-sc-number-or-lose 'descriptor-reg))
326 (def!constant immediate-arg-scn (meta-sc-number-or-lose 'any-reg))
327 (def!constant control-stack-arg-scn (meta-sc-number-or-lose 'control-stack))
328
329 (eval-when (:compile-toplevel :load-toplevel :execute)
330
331   ;; offsets of special stack frame locations
332   (def!constant ocfp-save-offset 0)
333   (def!constant lra-save-offset 1)
334   (def!constant nfp-save-offset 2)
335
336   ;; the number of arguments/return values passed in registers.
337   (def!constant register-arg-count 6)
338
339   ;; names to use for the argument registers.
340   (defparameter register-arg-names '(a0 a1 a2 a3 a4 a5))
341 ) ; EVAL-WHEN
342
343
344 ;;; a list of TN's describing the register arguments
345 (defparameter *register-arg-tns*
346   (mapcar (lambda (n)
347             (make-random-tn :kind :normal
348                               :sc (sc-or-lose 'descriptor-reg)
349                               :offset n))
350           *register-arg-offsets*))
351
352 ;;; This is used by the debugger.
353 (def!constant single-value-return-byte-offset 8)
354 \f
355 ;;; This function is called by debug output routines that want a
356 ;;; pretty name for a TN's location. It returns a thing that can be
357 ;;; printed with PRINC.
358 (!def-vm-support-routine location-print-name (tn)
359   (declare (type tn tn)) ; FIXME: commented out on alpha
360   (let ((sb (sb-name (sc-sb (tn-sc tn))))
361         (offset (tn-offset tn)))
362     (ecase sb
363       (registers (or (svref *register-names* offset)
364                      (format nil "R~D" offset)))
365       (float-registers (format nil "F~D" offset))
366       (control-stack (format nil "CS~D" offset))
367       (non-descriptor-stack (format nil "NS~D" offset))
368       (constant (format nil "Const~D" offset))
369       (immediate-constant "Immed"))))
370