1.0.19.1: DERIVE-TYPE optimizer for %%PRIMITIVE
[sbcl.git] / src / code / mips-vm.lisp
1 ;;; This file contains the MIPS specific runtime stuff.
2 ;;;
3 (in-package "SB!VM")
4
5 \f
6 (define-alien-type os-context-t (struct os-context-t-struct))
7 (define-alien-type os-context-register-t unsigned-long-long)
8
9 \f
10 ;;;; MACHINE-TYPE and MACHINE-VERSION
11
12 (defun machine-type ()
13   "Returns a string describing the type of the local machine."
14   "MIPS")
15
16 ;;; support for CL:MACHINE-VERSION defined OAOO elsewhere
17 (defun get-machine-version ()
18   #!+little-endian "little-endian"
19   #!-little-endian "big-endian")
20
21 \f
22 ;;;; FIXUP-CODE-OBJECT
23
24 (defun fixup-code-object (code offset value kind)
25   (declare (type index offset))
26   (unless (zerop (rem offset n-word-bytes))
27     (error "Unaligned instruction?  offset=#x~X." offset))
28   (sb!sys:without-gcing
29    (let ((sap (%primitive sb!c::code-instructions code)))
30      (ecase kind
31        (:jump
32         (aver (zerop (ash value -28)))
33         (setf (ldb (byte 26 0) (sap-ref-32 sap offset))
34               (ash value -2)))
35        (:lui
36         (setf (ldb (byte 16 0) (sap-ref-32 sap offset))
37               (ash (1+ (ash value -15)) -1)))
38        (:addi
39         (setf (ldb (byte 16 0) (sap-ref-32 sap offset))
40               (ldb (byte 16 0) value)))))))
41
42 \f
43 (define-alien-routine ("os_context_pc_addr" context-pc-addr)
44     (* os-context-register-t)
45   (context (* os-context-t) :in))
46
47 (defun context-pc (context)
48   (declare (type (alien (* os-context-t)) context))
49   (int-sap (deref (context-pc-addr context))))
50
51 (define-alien-routine ("os_context_register_addr" context-register-addr)
52     (* os-context-register-t)
53   (context (* os-context-t) :in)
54   (index int :in))
55
56 (define-alien-routine ("os_context_bd_cause" context-bd-cause-int)
57     unsigned-int
58   (context (* os-context-t) :in))
59
60 ;;; FIXME: Should this and CONTEXT-PC be INLINE to reduce consing?
61 ;;; (Are they used in anything time-critical, or just the debugger?)
62 (defun context-register (context index)
63   (declare (type (alien (* os-context-t)) context))
64   (let ((addr (context-register-addr context index)))
65     (declare (type (alien (* os-context-register-t)) addr))
66     (deref addr)))
67
68 (defun %set-context-register (context index new)
69   (declare (type (alien (* os-context-t)) context))
70   (let ((addr (context-register-addr context index)))
71     (declare (type (alien (* os-context-register-t)) addr))
72     (setf (deref addr) new)))
73
74 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
75 ;;; register. FORMAT is the type of float to return.
76
77 ;;; FIXME: Whether COERCE actually knows how to make a float out of a
78 ;;; long is another question. This stuff still needs testing.
79 (define-alien-routine ("os_context_fpregister_addr" context-float-register-addr)
80     (* os-context-register-t)
81   (context (* os-context-t) :in)
82   (index int :in))
83
84 (defun context-float-register (context index format)
85   (declare (type (alien (* os-context-t)) context))
86   (let ((addr (context-float-register-addr context index)))
87     (declare (type (alien (* os-context-register-t)) addr))
88     (coerce (deref addr) format)))
89
90 (defun %set-context-float-register (context index format new)
91   (declare (type (alien (* os-context-t)) context))
92   (let ((addr (context-float-register-addr context index)))
93     (declare (type (alien (* os-context-register-t)) addr))
94     (setf (deref addr) (coerce new format))))
95
96 (define-alien-routine
97     ("arch_get_fp_control" floating-point-modes) unsigned-int)
98
99 (define-alien-routine
100     ("arch_set_fp_control" %floating-point-modes-setter) void (fp unsigned-int :in))
101
102 (defun (setf floating-point-modes) (val) (%floating-point-modes-setter val))
103
104 ;;; Given a signal context, return the floating point modes word in
105 ;;; the same format as returned by FLOATING-POINT-MODES.
106 (define-alien-routine ("os_context_fp_control" context-floating-point-modes)
107     unsigned-int
108   (context (* os-context-t) :in))
109
110 ;;;; Internal-error-arguments.
111
112 ;;; INTERNAL-ERROR-ARGUMENTS -- interface.
113 ;;;
114 ;;; Given the sigcontext, extract the internal error arguments from the
115 ;;; instruction stream.  This is e.g.
116 ;;; 4       23      254     206     1       0       0       0
117 ;;; |       ~~~~~~~~~~~~~~~~~~~~~~~~~
118 ;;; length         data              (everything is an octet)
119 ;;; (pc + 4)
120 (defun internal-error-args (context)
121   (declare (type (alien (* os-context-t)) context))
122   (/show0 "entering INTERNAL-ERROR-ARGS, CONTEXT=..")
123   (/hexstr context)
124   (let ((pc (context-pc context))
125         (cause (context-bd-cause-int context)))
126     (declare (type system-area-pointer pc))
127     (multiple-value-bind (error-number length sc-offsets)
128         ;; KLUDGE: This exposure of the branch delay mechanism hurts.
129         (snarf-error-junk pc (if (logbitp 31 cause) 8 4))
130       (declare (ignore length))
131       (values error-number sc-offsets))))