0.pre8.90:
[sbcl/lichteblau.git] / src / compiler / x86 / cell.lisp
blobe35ae15155795a1430f72059a14076b2232d1f2b
1 ;;;; various primitive memory access VOPs for the x86 VM
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.
12 (in-package "SB!VM")
14 ;;;; data object ref/set stuff
16 (define-vop (slot)
17 (:args (object :scs (descriptor-reg)))
18 (:info name offset lowtag)
19 (:ignore name)
20 (:results (result :scs (descriptor-reg any-reg)))
21 (:generator 1
22 (loadw result object offset lowtag)))
24 (define-vop (set-slot)
25 (:args (object :scs (descriptor-reg))
26 (value :scs (descriptor-reg any-reg immediate)))
27 (:info name offset lowtag)
28 (:ignore name)
29 (:results)
30 (:generator 1
31 (if (sc-is value immediate)
32 (let ((val (tn-value value)))
33 (etypecase val
34 (integer
35 (inst mov
36 (make-ea :dword :base object
37 :disp (- (* offset n-word-bytes) lowtag))
38 (fixnumize val)))
39 (symbol
40 (inst mov
41 (make-ea :dword :base object
42 :disp (- (* offset n-word-bytes) lowtag))
43 (+ nil-value (static-symbol-offset val))))
44 (character
45 (inst mov
46 (make-ea :dword :base object
47 :disp (- (* offset n-word-bytes) lowtag))
48 (logior (ash (char-code val) n-widetag-bits)
49 base-char-widetag)))))
50 ;; Else, value not immediate.
51 (storew value object offset lowtag))))
55 ;;;; symbol hacking VOPs
57 ;;; these next two cf the sparc version, by jrd.
58 ;;; FIXME: Deref this ^ reference.
61 ;;; The compiler likes to be able to directly SET symbols.
62 #!+sb-thread
63 (define-vop (set)
64 (:args (symbol :scs (descriptor-reg))
65 (value :scs (descriptor-reg any-reg)))
66 (:translate sb!kernel:%set-symbol-value)
67 (:temporary (:sc descriptor-reg ) tls)
68 ;;(:policy :fast-safe)
69 (:generator 4
70 (let ((global-val (gen-label))
71 (done (gen-label)))
72 (loadw tls symbol symbol-tls-index-slot other-pointer-lowtag)
73 (inst or tls tls)
74 (inst jmp :z global-val)
75 (inst fs-segment-prefix)
76 (inst cmp (make-ea :dword :scale 1 :index tls) unbound-marker-widetag)
77 (inst jmp :z global-val)
78 (inst fs-segment-prefix)
79 (inst mov (make-ea :dword :scale 1 :index tls) value)
80 (inst jmp done)
81 (emit-label global-val)
82 (storew value symbol symbol-value-slot other-pointer-lowtag)
83 (emit-label done))))
85 ;; unithreaded it's a lot simpler ...
86 #!-sb-thread
87 (define-vop (set cell-set)
88 (:variant symbol-value-slot other-pointer-lowtag))
90 ;;; Do a cell ref with an error check for being unbound.
91 ;;; XXX stil used? I can't see where -dan
92 (define-vop (checked-cell-ref)
93 (:args (object :scs (descriptor-reg) :target obj-temp))
94 (:results (value :scs (descriptor-reg any-reg)))
95 (:policy :fast-safe)
96 (:vop-var vop)
97 (:save-p :compute-only)
98 (:temporary (:sc descriptor-reg :from (:argument 0)) obj-temp))
100 ;;; With Symbol-Value, we check that the value isn't the trap object. So
101 ;;; Symbol-Value of NIL is NIL.
102 #!+sb-thread
103 (define-vop (symbol-value)
104 (:translate symbol-value)
105 (:policy :fast-safe)
106 (:args (object :scs (descriptor-reg) :to (:result 1)))
107 (:results (value :scs (descriptor-reg any-reg)))
108 (:vop-var vop)
109 (:save-p :compute-only)
110 (:generator 9
111 (let* ((err-lab (generate-error-code vop unbound-symbol-error object))
112 (ret-lab (gen-label)))
113 (loadw value object symbol-tls-index-slot other-pointer-lowtag)
114 (inst fs-segment-prefix)
115 (inst mov value (make-ea :dword :index value :scale 1))
116 (inst cmp value unbound-marker-widetag)
117 (inst jmp :ne ret-lab)
118 (loadw value object symbol-value-slot other-pointer-lowtag)
119 (inst cmp value unbound-marker-widetag)
120 (inst jmp :e err-lab)
121 (emit-label ret-lab))))
123 #!+sb-thread
124 (define-vop (fast-symbol-value symbol-value)
125 ;; KLUDGE: not really fast, in fact, because we're going to have to
126 ;; do a full lookup of the thread-local area anyway. But half of
127 ;; the meaning of FAST-SYMBOL-VALUE is "do not signal an error if
128 ;; unbound", which is used in the implementation of COPY-SYMBOL. --
129 ;; CSR, 2003-04-22
130 (:policy :fast)
131 (:translate symbol-value)
132 (:generator 8
133 (let ((ret-lab (gen-label)))
134 (loadw value object symbol-tls-index-slot other-pointer-lowtag)
135 (inst fs-segment-prefix)
136 (inst mov value (make-ea :dword :index value :scale 1))
137 (inst cmp value unbound-marker-widetag)
138 (inst jmp :ne ret-lab)
139 (loadw value object symbol-value-slot other-pointer-lowtag)
140 (emit-label ret-lab))))
142 #!-sb-thread
143 (define-vop (symbol-value)
144 (:translate symbol-value)
145 (:policy :fast-safe)
146 (:args (object :scs (descriptor-reg) :to (:result 1)))
147 (:results (value :scs (descriptor-reg any-reg)))
148 (:vop-var vop)
149 (:save-p :compute-only)
150 (:generator 9
151 (let ((err-lab (generate-error-code vop unbound-symbol-error object)))
152 (loadw value object symbol-value-slot other-pointer-lowtag)
153 (inst cmp value unbound-marker-widetag)
154 (inst jmp :e err-lab))))
156 #!-sb-thread
157 (define-vop (fast-symbol-value cell-ref)
158 (:variant symbol-value-slot other-pointer-lowtag)
159 (:policy :fast)
160 (:translate symbol-value))
162 (defknown locked-symbol-global-value-add (symbol fixnum) fixnum ())
164 (define-vop (locked-symbol-global-value-add)
165 (:args (object :scs (descriptor-reg) :to :result)
166 (value :scs (any-reg) :target result))
167 (:arg-types * tagged-num)
168 (:results (result :scs (any-reg) :from (:argument 1)))
169 (:policy :fast)
170 (:translate locked-symbol-global-value-add)
171 (:result-types tagged-num)
172 (:policy :fast-safe)
173 (:generator 4
174 (move result value)
175 (inst lock)
176 (inst add (make-ea :dword :base object
177 :disp (- (* symbol-value-slot n-word-bytes)
178 other-pointer-lowtag))
179 value)))
181 #!+sb-thread
182 (define-vop (boundp)
183 (:translate boundp)
184 (:policy :fast-safe)
185 (:args (object :scs (descriptor-reg)))
186 (:conditional)
187 (:info target not-p)
188 (:temporary (:sc descriptor-reg #+nil(:from (:argument 0))) value)
189 (:generator 9
190 (if not-p
191 (let ((not-target (gen-label)))
192 (loadw value object symbol-value-slot other-pointer-lowtag)
193 (inst cmp value unbound-marker-widetag)
194 (inst jmp :ne not-target)
195 (loadw value object symbol-tls-index-slot other-pointer-lowtag)
196 (inst fs-segment-prefix)
197 (inst cmp (make-ea :dword :index value :scale 1) unbound-marker-widetag)
198 (inst jmp :e target)
199 (emit-label not-target))
200 (progn
201 (loadw value object symbol-value-slot other-pointer-lowtag)
202 (inst cmp value unbound-marker-widetag)
203 (inst jmp :ne target)
204 (loadw value object symbol-tls-index-slot other-pointer-lowtag)
205 (inst fs-segment-prefix)
206 (inst cmp (make-ea :dword :index value :scale 1) unbound-marker-widetag)
207 (inst jmp :ne target)))))
209 #!-sb-thread
210 (define-vop (boundp)
211 (:translate boundp)
212 (:policy :fast-safe)
213 (:args (object :scs (descriptor-reg)))
214 (:conditional)
215 (:info target not-p)
216 (:temporary (:sc descriptor-reg :from (:argument 0)) value)
217 (:generator 9
218 (loadw value object symbol-value-slot other-pointer-lowtag)
219 (inst cmp value unbound-marker-widetag)
220 (inst jmp (if not-p :e :ne) target)))
223 (define-vop (symbol-hash)
224 (:policy :fast-safe)
225 (:translate symbol-hash)
226 (:args (symbol :scs (descriptor-reg)))
227 (:results (res :scs (any-reg)))
228 (:result-types positive-fixnum)
229 (:generator 2
230 ;; The symbol-hash slot of NIL holds NIL because it is also the
231 ;; cdr slot, so we have to strip off the two low bits to make sure
232 ;; it is a fixnum. The lowtag selection magic that is required to
233 ;; ensure this is explained in the comment in objdef.lisp
234 (loadw res symbol symbol-hash-slot other-pointer-lowtag)
235 (inst and res (lognot #b11))))
237 ;;;; fdefinition (FDEFN) objects
239 (define-vop (fdefn-fun cell-ref) ; /pfw - alpha
240 (:variant fdefn-fun-slot other-pointer-lowtag))
242 (define-vop (safe-fdefn-fun)
243 (:args (object :scs (descriptor-reg) :to (:result 1)))
244 (:results (value :scs (descriptor-reg any-reg)))
245 (:vop-var vop)
246 (:save-p :compute-only)
247 (:generator 10
248 (loadw value object fdefn-fun-slot other-pointer-lowtag)
249 (inst cmp value nil-value)
250 (let ((err-lab (generate-error-code vop undefined-fun-error object)))
251 (inst jmp :e err-lab))))
253 (define-vop (set-fdefn-fun)
254 (:policy :fast-safe)
255 (:translate (setf fdefn-fun))
256 (:args (function :scs (descriptor-reg) :target result)
257 (fdefn :scs (descriptor-reg)))
258 (:temporary (:sc unsigned-reg) raw)
259 (:temporary (:sc byte-reg) type)
260 (:results (result :scs (descriptor-reg)))
261 (:generator 38
262 (load-type type function (- fun-pointer-lowtag))
263 (inst lea raw
264 (make-ea :byte :base function
265 :disp (- (* simple-fun-code-offset n-word-bytes)
266 fun-pointer-lowtag)))
267 (inst cmp type simple-fun-header-widetag)
268 (inst jmp :e normal-fn)
269 (inst lea raw (make-fixup (extern-alien-name "closure_tramp") :foreign))
270 NORMAL-FN
271 (storew function fdefn fdefn-fun-slot other-pointer-lowtag)
272 (storew raw fdefn fdefn-raw-addr-slot other-pointer-lowtag)
273 (move result function)))
275 (define-vop (fdefn-makunbound)
276 (:policy :fast-safe)
277 (:translate fdefn-makunbound)
278 (:args (fdefn :scs (descriptor-reg) :target result))
279 (:results (result :scs (descriptor-reg)))
280 (:generator 38
281 (storew nil-value fdefn fdefn-fun-slot other-pointer-lowtag)
282 (storew (make-fixup (extern-alien-name "undefined_tramp") :foreign)
283 fdefn fdefn-raw-addr-slot other-pointer-lowtag)
284 (move result fdefn)))
286 ;;;; binding and unbinding
288 ;;; BIND -- Establish VAL as a binding for SYMBOL. Save the old value and
289 ;;; the symbol on the binding stack and stuff the new value into the
290 ;;; symbol.
292 #!+sb-thread
293 (define-vop (bind)
294 (:args (val :scs (any-reg descriptor-reg))
295 (symbol :scs (descriptor-reg)))
296 (:temporary (:sc unsigned-reg) tls-index temp bsp)
297 (:generator 5
298 (let ((tls-index-valid (gen-label)))
299 (load-tl-symbol-value bsp *binding-stack-pointer*)
300 (loadw tls-index symbol symbol-tls-index-slot other-pointer-lowtag)
301 (inst add bsp (* binding-size n-word-bytes))
302 (store-tl-symbol-value bsp *binding-stack-pointer* temp)
304 (inst or tls-index tls-index)
305 (inst jmp :ne tls-index-valid)
306 ;; allocate a new tls-index
307 (load-symbol-value tls-index *free-tls-index*)
308 (inst add tls-index 4) ;XXX surely we can do this more
309 (store-symbol-value tls-index *free-tls-index*) ;succintly
310 (inst sub tls-index 4)
311 (storew tls-index symbol symbol-tls-index-slot other-pointer-lowtag)
312 (emit-label tls-index-valid)
313 (inst fs-segment-prefix)
314 (inst mov temp (make-ea :dword :scale 1 :index tls-index))
315 (storew temp bsp (- binding-value-slot binding-size))
316 (storew symbol bsp (- binding-symbol-slot binding-size))
317 (inst fs-segment-prefix)
318 (inst mov (make-ea :dword :scale 1 :index tls-index) val))))
320 #!-sb-thread
321 (define-vop (bind)
322 (:args (val :scs (any-reg descriptor-reg))
323 (symbol :scs (descriptor-reg)))
324 (:temporary (:sc unsigned-reg) temp bsp)
325 (:generator 5
326 (load-symbol-value bsp *binding-stack-pointer*)
327 (loadw temp symbol symbol-value-slot other-pointer-lowtag)
328 (inst add bsp (* binding-size n-word-bytes))
329 (store-symbol-value bsp *binding-stack-pointer*)
330 (storew temp bsp (- binding-value-slot binding-size))
331 (storew symbol bsp (- binding-symbol-slot binding-size))
332 (storew val symbol symbol-value-slot other-pointer-lowtag)))
335 #!+sb-thread
336 (define-vop (unbind)
337 ;; four temporaries?
338 (:temporary (:sc unsigned-reg) symbol value bsp tls-index)
339 (:generator 0
340 (load-tl-symbol-value bsp *binding-stack-pointer*)
341 (loadw symbol bsp (- binding-symbol-slot binding-size))
342 (loadw value bsp (- binding-value-slot binding-size))
344 (loadw tls-index symbol symbol-tls-index-slot other-pointer-lowtag)
345 (inst fs-segment-prefix)
346 (inst mov (make-ea :dword :scale 1 :index tls-index) value)
348 (storew 0 bsp (- binding-symbol-slot binding-size))
349 (inst sub bsp (* binding-size n-word-bytes))
350 ;; we're done with value, so we can use it as a temp here
351 (store-tl-symbol-value bsp *binding-stack-pointer* value)))
353 #!-sb-thread
354 (define-vop (unbind)
355 (:temporary (:sc unsigned-reg) symbol value bsp)
356 (:generator 0
357 (load-symbol-value bsp *binding-stack-pointer*)
358 (loadw symbol bsp (- binding-symbol-slot binding-size))
359 (loadw value bsp (- binding-value-slot binding-size))
360 (storew value symbol symbol-value-slot other-pointer-lowtag)
361 (storew 0 bsp (- binding-symbol-slot binding-size))
362 (inst sub bsp (* binding-size n-word-bytes))
363 (store-symbol-value bsp *binding-stack-pointer*)))
366 (define-vop (unbind-to-here)
367 (:args (where :scs (descriptor-reg any-reg)))
368 (:temporary (:sc unsigned-reg) symbol value bsp #!+sb-thread tls-index)
369 (:generator 0
370 (load-tl-symbol-value bsp *binding-stack-pointer*)
371 (inst cmp where bsp)
372 (inst jmp :e done)
374 LOOP
375 (loadw symbol bsp (- binding-symbol-slot binding-size))
376 (inst or symbol symbol)
377 (inst jmp :z skip)
378 (loadw value bsp (- binding-value-slot binding-size))
379 #!-sb-thread (storew value symbol symbol-value-slot other-pointer-lowtag)
381 #!+sb-thread (loadw
382 tls-index symbol symbol-tls-index-slot other-pointer-lowtag)
383 #!+sb-thread (inst fs-segment-prefix)
384 #!+sb-thread (inst mov (make-ea :dword :scale 1 :index tls-index) value)
385 (storew 0 bsp (- binding-symbol-slot binding-size))
387 SKIP
388 (inst sub bsp (* binding-size n-word-bytes))
389 (inst cmp where bsp)
390 (inst jmp :ne loop)
391 ;; we're done with value, so can use it as a temporary
392 (store-tl-symbol-value bsp *binding-stack-pointer* value)
394 DONE))
398 ;;;; closure indexing
400 (define-full-reffer closure-index-ref *
401 closure-info-offset fun-pointer-lowtag
402 (any-reg descriptor-reg) * %closure-index-ref)
404 (define-full-setter set-funcallable-instance-info *
405 funcallable-instance-info-offset fun-pointer-lowtag
406 (any-reg descriptor-reg) * %set-funcallable-instance-info)
408 (define-full-reffer funcallable-instance-info *
409 funcallable-instance-info-offset fun-pointer-lowtag
410 (descriptor-reg any-reg) * %funcallable-instance-info)
412 (define-vop (funcallable-instance-lexenv cell-ref)
413 (:variant funcallable-instance-lexenv-slot fun-pointer-lowtag))
415 (define-vop (closure-ref slot-ref)
416 (:variant closure-info-offset fun-pointer-lowtag))
418 (define-vop (closure-init slot-set)
419 (:variant closure-info-offset fun-pointer-lowtag))
421 ;;;; value cell hackery
423 (define-vop (value-cell-ref cell-ref)
424 (:variant value-cell-value-slot other-pointer-lowtag))
426 (define-vop (value-cell-set cell-set)
427 (:variant value-cell-value-slot other-pointer-lowtag))
429 ;;;; structure hackery
431 (define-vop (instance-length)
432 (:policy :fast-safe)
433 (:translate %instance-length)
434 (:args (struct :scs (descriptor-reg)))
435 (:results (res :scs (unsigned-reg)))
436 (:result-types positive-fixnum)
437 (:generator 4
438 (loadw res struct 0 instance-pointer-lowtag)
439 (inst shr res n-widetag-bits)))
441 (define-vop (instance-ref slot-ref)
442 (:variant instance-slots-offset instance-pointer-lowtag)
443 (:policy :fast-safe)
444 (:translate %instance-ref)
445 (:arg-types instance (:constant index)))
447 (define-vop (instance-set slot-set)
448 (:policy :fast-safe)
449 (:translate %instance-set)
450 (:variant instance-slots-offset instance-pointer-lowtag)
451 (:arg-types instance (:constant index) *))
453 (define-full-reffer instance-index-ref * instance-slots-offset
454 instance-pointer-lowtag (any-reg descriptor-reg) * %instance-ref)
456 (define-full-setter instance-index-set * instance-slots-offset
457 instance-pointer-lowtag (any-reg descriptor-reg) * %instance-set)
460 (defknown %instance-set-conditional (instance index t t) t
461 (unsafe))
463 (define-vop (instance-set-conditional)
464 (:translate %instance-set-conditional)
465 (:args (object :scs (descriptor-reg) :to :eval)
466 (slot :scs (any-reg) :to :result)
467 (old-value :scs (descriptor-reg any-reg) :target eax)
468 (new-value :scs (descriptor-reg any-reg)))
469 (:arg-types instance positive-fixnum * *)
470 (:temporary (:sc descriptor-reg :offset eax-offset
471 :from (:argument 2) :to :result :target result) eax)
472 (:results (result :scs (descriptor-reg any-reg)))
473 ;(:guard (backend-featurep :i486))
474 (:policy :fast-safe)
475 (:generator 5
476 (move eax old-value)
477 (inst lock)
478 (inst cmpxchg (make-ea :dword :base object :index slot :scale 1
479 :disp (- (* instance-slots-offset n-word-bytes)
480 instance-pointer-lowtag))
481 new-value)
482 (move result eax)))
486 ;;;; code object frobbing
488 (define-full-reffer code-header-ref * 0 other-pointer-lowtag
489 (any-reg descriptor-reg) * code-header-ref)
491 (define-full-setter code-header-set * 0 other-pointer-lowtag
492 (any-reg descriptor-reg) * code-header-set)