b31d80d9d661f0b6acafc8b97164287990ed370a
[sbcl.git] / src / compiler / mips / system.lisp
1 ;;;; MIPS VM definitions of various system hacking operations
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
14 \f
15 ;;;; Type frobbing VOPs
16
17 (define-vop (lowtag-of)
18   (:translate lowtag-of)
19   (:policy :fast-safe)
20   (:args (object :scs (any-reg descriptor-reg)))
21   (:results (result :scs (unsigned-reg)))
22   (:result-types positive-fixnum)
23   (:generator 1
24     (inst and result object lowtag-mask)))
25
26 (define-vop (widetag-of)
27   (:translate widetag-of)
28   (:policy :fast-safe)
29   (:args (object :scs (descriptor-reg)))
30   (:temporary (:scs (non-descriptor-reg)) ndescr)
31   (:results (result :scs (unsigned-reg)))
32   (:result-types positive-fixnum)
33   (:generator 6
34     ;; Pick off objects with headers.
35     (inst and ndescr object lowtag-mask)
36     (inst xor ndescr other-pointer-lowtag)
37     (inst beq ndescr other-ptr)
38     (inst xor ndescr (logxor other-pointer-lowtag fun-pointer-lowtag))
39     (inst beq ndescr function-ptr)
40
41     ;; Pick off fixnums.
42     (inst and result object 3)
43     (inst beq result done)
44
45     ;; Pick off structure and list pointers.
46     (inst and result object 1)
47     (inst bne result lowtag-only)
48     (inst nop)
49
50       ;; Must be an other immediate.
51     (inst b done)
52     (inst and result object widetag-mask)
53
54     FUNCTION-PTR
55     (load-type result object (- fun-pointer-lowtag))
56     (inst b done)
57     (inst nop)
58
59     LOWTAG-ONLY
60     (inst b done)
61     (inst and result object lowtag-mask)
62
63     OTHER-PTR
64     (load-type result object (- other-pointer-lowtag))
65     (inst nop)
66
67     DONE))
68
69 (define-vop (fun-subtype)
70   (:translate fun-subtype)
71   (:policy :fast-safe)
72   (:args (function :scs (descriptor-reg)))
73   (:results (result :scs (unsigned-reg)))
74   (:result-types positive-fixnum)
75   (:generator 6
76     (load-type result function (- fun-pointer-lowtag))
77     (inst nop)))
78
79 (define-vop (set-fun-subtype)
80   (:translate (setf fun-subtype))
81   (:policy :fast-safe)
82   (:args (type :scs (unsigned-reg) :target result)
83          (function :scs (descriptor-reg)))
84   (:arg-types positive-fixnum *)
85   (:results (result :scs (unsigned-reg)))
86   (:result-types positive-fixnum)
87   (:generator 6
88     (inst sb type function (- fun-pointer-lowtag))
89     (move result type)))
90
91
92 (define-vop (get-header-data)
93   (:translate get-header-data)
94   (:policy :fast-safe)
95   (:args (x :scs (descriptor-reg)))
96   (:results (res :scs (unsigned-reg)))
97   (:result-types positive-fixnum)
98   (:generator 6
99     (loadw res x 0 other-pointer-lowtag)
100     (inst srl res res n-widetag-bits)))
101
102 (define-vop (get-closure-length)
103   (:translate get-closure-length)
104   (:policy :fast-safe)
105   (:args (x :scs (descriptor-reg)))
106   (:results (res :scs (unsigned-reg)))
107   (:result-types positive-fixnum)
108   (:generator 6
109     (loadw res x 0 fun-pointer-lowtag)
110     (inst srl res res n-widetag-bits)))
111
112 (define-vop (set-header-data)
113   (:translate set-header-data)
114   (:policy :fast-safe)
115   (:args (x :scs (descriptor-reg) :target res)
116          (data :scs (any-reg immediate zero)))
117   (:arg-types * positive-fixnum)
118   (:results (res :scs (descriptor-reg)))
119   (:temporary (:scs (non-descriptor-reg)) t1 t2)
120   (:generator 6
121     (loadw t1 x 0 other-pointer-lowtag)
122     (inst and t1 widetag-mask)
123     (sc-case data
124       (any-reg
125        (inst sll t2 data (- n-widetag-bits 2))
126        (inst or t1 t2))
127       (immediate
128        (inst or t1 (ash (tn-value data) n-widetag-bits)))
129       (zero))
130     (storew t1 x 0 other-pointer-lowtag)
131     (move res x)))
132
133 (define-vop (make-fixnum)
134   (:args (ptr :scs (any-reg descriptor-reg)))
135   (:results (res :scs (any-reg descriptor-reg)))
136   (:generator 1
137     ;;
138     ;; Some code (the hash table code) depends on this returning a
139     ;; positive number so make sure it does.
140     (inst sll res ptr 3)
141     (inst srl res res 1)))
142
143 (define-vop (make-other-immediate-type)
144   (:args (val :scs (any-reg descriptor-reg))
145          (type :scs (any-reg descriptor-reg immediate)
146                :target temp))
147   (:results (res :scs (any-reg descriptor-reg)))
148   (:temporary (:scs (non-descriptor-reg)) temp)
149   (:generator 2
150     (sc-case type
151       ((immediate)
152        (inst sll temp val n-widetag-bits)
153        (inst or res temp (tn-value type)))
154       (t
155        (inst sra temp type 2)
156        (inst sll res val (- n-widetag-bits 2))
157        (inst or res res temp)))))
158
159 \f
160 ;;;; Allocation
161
162 (define-vop (dynamic-space-free-pointer)
163   (:results (int :scs (sap-reg)))
164   (:result-types system-area-pointer)
165   (:translate dynamic-space-free-pointer)
166   (:policy :fast-safe)
167   (:generator 1
168     (move int alloc-tn)))
169
170 (define-vop (binding-stack-pointer-sap)
171   (:results (int :scs (sap-reg)))
172   (:result-types system-area-pointer)
173   (:translate binding-stack-pointer-sap)
174   (:policy :fast-safe)
175   (:generator 1
176     (move int bsp-tn)))
177
178 (define-vop (control-stack-pointer-sap)
179   (:results (int :scs (sap-reg)))
180   (:result-types system-area-pointer)
181   (:translate control-stack-pointer-sap)
182   (:policy :fast-safe)
183   (:generator 1
184     (move int csp-tn)))
185
186 \f
187 ;;;; Code object frobbing.
188
189 (define-vop (code-instructions)
190   (:translate code-instructions)
191   (:policy :fast-safe)
192   (:args (code :scs (descriptor-reg)))
193   (:temporary (:scs (non-descriptor-reg)) ndescr)
194   (:results (sap :scs (sap-reg)))
195   (:result-types system-area-pointer)
196   (:generator 10
197     (loadw ndescr code 0 other-pointer-lowtag)
198     (inst srl ndescr n-widetag-bits)
199     (inst sll ndescr word-shift)
200     (inst subu ndescr other-pointer-lowtag)
201     (inst addu sap code ndescr)))
202
203 (define-vop (compute-fun)
204   (:args (code :scs (descriptor-reg))
205          (offset :scs (signed-reg unsigned-reg)))
206   (:arg-types * positive-fixnum)
207   (:results (func :scs (descriptor-reg)))
208   (:temporary (:scs (non-descriptor-reg)) ndescr)
209   (:generator 10
210     (loadw ndescr code 0 other-pointer-lowtag)
211     (inst srl ndescr n-widetag-bits)
212     (inst sll ndescr word-shift)
213     (inst addu ndescr offset)
214     (inst addu ndescr (- fun-pointer-lowtag other-pointer-lowtag))
215     (inst addu func code ndescr)))
216
217 \f
218 ;;;; Other random VOPs.
219
220
221 (defknown sb!unix::receive-pending-interrupt () (values))
222 (define-vop (sb!unix::receive-pending-interrupt)
223   (:policy :fast-safe)
224   (:translate sb!unix::receive-pending-interrupt)
225   (:generator 1
226     (inst break 0 pending-interrupt-trap)))
227
228
229 (define-vop (halt)
230   (:generator 1
231     (inst break 0 halt-trap)))
232
233 \f
234 ;;;; Dynamic vop count collection support
235
236 (define-vop (count-me)
237   (:args (count-vector :scs (descriptor-reg)))
238   (:info index)
239   (:temporary (:scs (non-descriptor-reg)) count)
240   (:generator 1
241     (let ((offset
242            (- (* (+ index vector-data-offset) n-word-bytes) other-pointer-lowtag)))
243       (inst lw count count-vector offset)
244       (inst nop)
245       (inst addu count 1)
246       (inst sw count count-vector offset))))