Fix make-array transforms.
[sbcl.git] / code / mips-vm.lisp
1 (in-package "SB!VM")
2 \f
3 (define-alien-type os-context-t (struct os-context-t-struct))
4 \f
5 ;;;; MACHINE-TYPE and MACHINE-VERSION
6
7 (defun machine-type ()
8   "Returns a string describing the type of the local machine."
9   "MIPS")
10
11 ;;; support for CL:MACHINE-VERSION defined OAOO elsewhere
12 (defun get-machine-version ()
13   #!+little-endian "little-endian"
14   #!-little-endian "big-endian")
15 \f
16 ;;;; FIXUP-CODE-OBJECT
17
18 (defun fixup-code-object (code offset value kind)
19   (unless (zerop (rem offset n-word-bytes))
20     (error "Unaligned instruction?  offset=#x~X." offset))
21   (sb!sys:without-gcing
22    (let ((sap (truly-the system-area-pointer
23                          (%primitive sb!c::code-instructions code))))
24      (ecase kind
25        (:jump
26         (aver (zerop (ash value -28)))
27         (setf (ldb (byte 26 0) (sap-ref-32 sap offset))
28               (ash value -2)))
29        (:lui
30         (setf (sap-ref-16 sap
31                           #!+little-endian offset
32                           #!-little-endian (+ offset 2))
33               (+ (ash value -16)
34                  (if (logbitp 15 value) 1 0))))
35        (:addi
36         (setf (sap-ref-16 sap
37                           #!+little-endian offset
38                           #!-little-endian (+ offset 2))
39               (ldb (byte 16 0) value)))))))
40
41 \f
42 (define-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-int)
43   (context (* os-context-t)))
44
45 (defun context-pc (context)
46   (declare (type (alien (* os-context-t)) context))
47   ;; KLUDGE: this sucks, and furthermore will break on either of (a)
48   ;; porting back to IRIX or (b) running on proper 64-bit support.
49   ;; Linux on the MIPS defines its registers in the sigcontext as
50   ;; 64-bit quantities ("unsigned long long"), presumably to be
51   ;; binary-compatible with 64-bit mode.  Since there appears not to
52   ;; be ALIEN support for 64-bit return values, we have to do the
53   ;; hacky pointer arithmetic thing.  -- CSR, 2002-09-01
54   (int-sap (deref (context-pc-addr context)
55                   #!-little-endian 1
56                   #!+little-endian 0)))
57
58 (define-alien-routine ("os_context_register_addr" context-register-addr)
59   (* unsigned-int)
60   (context (* os-context-t))
61   (index int))
62
63 (define-alien-routine ("os_context_bd_cause" context-bd-cause-int)
64     (unsigned 32)
65   (context (* os-context-t)))
66
67 ;;; FIXME: Should this and CONTEXT-PC be INLINE to reduce consing?
68 ;;; (Are they used in anything time-critical, or just the debugger?)
69 (defun context-register (context index)
70   (declare (type (alien (* os-context-t)) context))
71   (deref (context-register-addr context index)
72          #!-little-endian 1
73          #!+little-endian 0))
74
75 (defun %set-context-register (context index new)
76   (declare (type (alien (* os-context-t)) context))
77   (setf (deref (context-register-addr context index)
78                #!-little-endian 1
79                #!+little-endian 0)
80         new))
81
82 #!+linux
83 ;;; For now.
84 (defun context-floating-point-modes (context)
85   (declare (ignore context))
86   (warn "stub CONTEXT-FLOATING-POINT-MODES")
87   0)
88
89 ;;;; Internal-error-arguments.
90
91 ;;; INTERNAL-ERROR-ARGUMENTS -- interface.
92 ;;;
93 ;;; Given the sigcontext, extract the internal error arguments from the
94 ;;; instruction stream.
95 ;;;
96 (defun internal-error-args (context)
97   (declare (type (alien (* os-context-t)) context))
98   (/show0 "entering INTERNAL-ERROR-ARGS, CONTEXT=..")
99   (/hexstr context)
100   (let ((pc (context-pc context))
101         (cause (context-bd-cause-int context)))
102     (declare (type system-area-pointer pc))
103     (/show0 "got PC=..")
104     (/hexstr (sap-int pc))
105     ;; KLUDGE: This exposure of the branch delay mechanism hurts.
106     (when (logbitp 31 cause)
107       (setf pc (sap+ pc 4)))
108     (when (= (sap-ref-8 pc 4) 255)
109       (setf pc (sap+ pc 1)))
110     (/show0 "now PC=..")
111     (/hexstr (sap-int pc))
112     (let* ((length (sap-ref-8 pc 4))
113            (vector (make-array length :element-type '(unsigned-byte 8))))
114       (declare (type (unsigned-byte 8) length)
115                (type (simple-array (unsigned-byte 8) (*)) vector))
116       (/show0 "LENGTH,VECTOR,ERROR-NUMBER=..")
117       (/hexstr length)
118       (/hexstr vector)
119       (copy-ub8-from-system-area pc 5 vector 0 length)
120       (let* ((index 0)
121              (error-number (sb!c:read-var-integer vector index)))
122         (/hexstr error-number)
123         (collect ((sc-offsets))
124          (loop
125           (/show0 "INDEX=..")
126           (/hexstr index)
127           (when (>= index length)
128             (return))
129           (sc-offsets (sb!c:read-var-integer vector index)))
130          (values error-number (sc-offsets)))))))
131
132
133
134
135