run-program: support I/O redirection to binary streams on win32.
[sbcl.git] / src / code / ppc-vm.lisp
blob60e914f2a5592265afe5be7e9f58c40f63d8c349
1 ;;; This file contains the PPC specific runtime stuff.
2 ;;;
3 (in-package "SB!VM")
5 (define-alien-type os-context-t (struct os-context-t-struct))
7 \f
8 ;;;; MACHINE-TYPE
10 (defun machine-type ()
11 "Returns a string describing the type of the local machine."
12 "PowerPC")
14 ;;;; FIXUP-CODE-OBJECT
16 (defun fixup-code-object (code offset fixup kind)
17 (declare (type index offset))
18 (unless (zerop (rem offset n-word-bytes))
19 (error "Unaligned instruction? offset=#x~X." offset))
20 (without-gcing
21 (let ((sap (%primitive code-instructions code)))
22 (ecase kind
23 (:b
24 (error "Can't deal with CALL fixups, yet."))
25 (:ba
26 (setf (ldb (byte 24 2) (sap-ref-32 sap offset))
27 (ash fixup -2)))
28 (:ha
29 (let* ((h (ldb (byte 16 16) fixup))
30 (l (ldb (byte 16 0) fixup)))
31 ; Compensate for possible sign-extension when the low half
32 ; is added to the high. We could avoid this by ORI-ing
33 ; the low half in 32-bit absolute loads, but it'd be
34 ; nice to be able to do:
35 ; lis rX,foo@ha
36 ; lwz rY,foo@l(rX)
37 ; and lwz/stw and friends all use a signed 16-bit offset.
38 (setf (ldb (byte 16 0) (sap-ref-32 sap offset))
39 (if (logbitp 15 l) (ldb (byte 16 0) (1+ h)) h))))
40 (:l
41 (setf (ldb (byte 16 0) (sap-ref-32 sap offset))
42 (ldb (byte 16 0) fixup)))))))
45 ;;;; "Sigcontext" access functions, cut & pasted from x86-vm.lisp then
46 ;;;; hacked for types.
48 (define-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-long)
49 (context (* os-context-t)))
51 (defun context-pc (context)
52 (declare (type (alien (* os-context-t)) context))
53 (int-sap (deref (context-pc-addr context))))
55 (define-alien-routine ("os_context_register_addr" context-register-addr)
56 (* unsigned-long)
57 (context (* os-context-t))
58 (index int))
60 (defun context-register (context index)
61 (declare (type (alien (* os-context-t)) context))
62 (deref (context-register-addr context index)))
64 (define-alien-routine ("os_context_lr_addr" context-lr-addr) (* unsigned-long)
65 (context (* os-context-t)))
67 (defun context-lr (context)
68 (declare (type (alien (* os-context-t)) context))
69 (int-sap (deref (context-lr-addr context))))
71 (defun %set-context-register (context index new)
72 (declare (type (alien (* os-context-t)) context))
73 (setf (deref (context-register-addr context index))
74 new))
75 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
76 ;;; register. FORMAT is the type of float to return.
78 ;;; FIXME: Whether COERCE actually knows how to make a float out of a
79 ;;; long is another question. This stuff still needs testing.
80 #+nil
81 (define-alien-routine ("os_context_fpregister_addr" context-float-register-addr)
82 (* long)
83 (context (* os-context-t))
84 (index int))
85 #+nil
86 (defun context-float-register (context index format)
87 (declare (type (alien (* os-context-t)) context))
88 (coerce (deref (context-float-register-addr context index)) format))
89 #+nil
90 (defun %set-context-float-register (context index format new)
91 (declare (type (alien (* os-context-t)) context))
92 (setf (deref (context-float-register-addr context index))
93 (coerce new format)))
95 ;;; Given a signal context, return the floating point modes word in
96 ;;; the same format as returned by FLOATING-POINT-MODES.
97 ;;;
98 ;;; FIXME: surely this must be accessible somewhere under Darwin? Or
99 ;;; under NetBSD?
100 #!+linux
101 (define-alien-routine ("os_context_fp_control" context-floating-point-modes)
102 (unsigned 32)
103 (context (* os-context-t)))
106 ;;;; INTERNAL-ERROR-ARGS.
108 ;;; GIVEN a (POSIX) signal context, extract the internal error
109 ;;; arguments from the instruction stream. This is e.g.
111 ;;; INTERNAL-ERROR-ARGS -- interface.
113 ;;; Given the sigcontext, extract the internal error arguments from the
114 ;;; instruction stream.
116 (defun internal-error-args (context)
117 (declare (type (alien (* os-context-t)) context))
118 (let* ((pc (context-pc context))
119 (bad-inst (sap-ref-32 pc 0))
120 (op (ldb (byte 16 16) bad-inst))
121 (regnum (ldb (byte 5 0) op)))
122 (declare (type system-area-pointer pc))
123 (cond ((= op (logior (ash 3 10) (ash 6 5)))
124 (args-for-unimp-inst context))
125 #!-precise-arg-count-error
126 ((and (= (ldb (byte 6 10) op) 3)
127 (= (ldb (byte 5 5) op) 24))
128 (let ((prev (sap-ref-32 (int-sap (- (sap-int pc) 4)) 0)))
129 (if (and (= (ldb (byte 6 26) prev) 3)
130 (= (ldb (byte 5 21) prev) 0))
131 (values (ldb (byte 16 0) prev)
132 (list (make-sc-offset any-reg-sc-number
133 (ldb (byte 5 16) prev))))
134 (values #.(error-number-or-lose
135 'invalid-arg-count-error)
136 (list (make-sc-offset any-reg-sc-number regnum))))))
137 #!+precise-arg-count-error
138 ((and (= (ldb (byte 6 10) op) 3) ;; twi
139 (or (= regnum #.(sc-offset-offset arg-count-sc))
140 (= (ldb (byte 5 5) op) 24))) ;; :ne
141 ;; Type errors are encoded as
142 ;; twi 0 value-register error-code
143 ;; twi :ne temp-register x
144 (let ((prev (sap-ref-32 (int-sap (- (sap-int pc) 4)) 0)))
145 (if (and (= (ldb (byte 5 5) op) 24) ;; is the condition :ne?
146 (= (ldb (byte 6 26) prev) 3) ;; is it twi?
147 (= (ldb (byte 5 21) prev) 0)) ;; is it non-trapping?
148 (values (ldb (byte 16 0) prev)
149 (list (make-sc-offset any-reg-sc-number
150 (ldb (byte 5 16) prev))))
151 ;; arg-count errors are encoded as
152 ;; twi {:ne :llt :lgt} nargs arg-count
153 (values #.(error-number-or-lose 'invalid-arg-count-error)
154 '(#.arg-count-sc)))))
156 (values #.(error-number-or-lose 'unknown-error) nil)))))
158 (defun args-for-unimp-inst (context)
159 (declare (type (alien (* os-context-t)) context))
160 (let* ((pc (context-pc context))
161 (length (sap-ref-8 pc 4))
162 (vector (make-array length :element-type '(unsigned-byte 8))))
163 (declare (type system-area-pointer pc)
164 (type (unsigned-byte 8) length)
165 (type (simple-array (unsigned-byte 8) (*)) vector))
166 (copy-ub8-from-system-area pc 5 vector 0 length)
167 (let* ((index 0)
168 (error-number (read-var-integer vector index)))
169 (collect ((sc-offsets))
170 (loop
171 (when (>= index length)
172 (return))
173 (sc-offsets (read-var-integer vector index)))
174 (values error-number (sc-offsets))))))