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