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