1 ;;;; a bunch of handy macros for the x86
3 ;;;; This software is part of the SBCL system. See the README file for
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.
14 ;;; We can load/store into fp registers through the top of stack
15 ;;; %st(0) (fr0 here). Loads imply a push to an empty register which
16 ;;; then changes all the reg numbers. These macros help manage that.
18 ;;; Use this when we don't have to load anything. It preserves old tos
19 ;;; value, but probably destroys tn with operation.
20 (defmacro with-tn
@fp-top
((tn) &body body
)
22 (unless (zerop (tn-offset ,tn
))
25 (unless (zerop (tn-offset ,tn
))
28 ;;; Use this to prepare for load of new value from memory. This
29 ;;; changes the register numbering so the next instruction had better
30 ;;; be a FP load from memory; a register load from another register
31 ;;; will probably be loading the wrong register!
32 (defmacro with-empty-tn
@fp-top
((tn) &body body
)
36 (unless (zerop (tn-offset ,tn
))
37 (inst fxch
,tn
)))) ; save into new dest and restore st(0)
39 ;;;; instruction-like macros
41 (defmacro move
(dst src
)
43 "Move SRC into DST unless they are location=."
44 (once-only ((n-dst dst
)
46 `(unless (location= ,n-dst
,n-src
)
47 (inst mov
,n-dst
,n-src
))))
49 (defmacro align-stack-pointer
(tn)
50 #!-darwin
(declare (ignore tn
))
53 `(inst and
,tn
#xfffffff0
))
55 (defmacro make-ea-for-object-slot
(ptr slot lowtag
&optional
(size :dword
))
56 `(make-ea ,size
:base
,ptr
:disp
(- (* ,slot n-word-bytes
) ,lowtag
)))
58 (defmacro loadw
(value ptr
&optional
(slot 0) (lowtag 0))
59 `(inst mov
,value
(make-ea-for-object-slot ,ptr
,slot
,lowtag
)))
61 (defmacro storew
(value ptr
&optional
(slot 0) (lowtag 0))
62 (once-only ((value value
))
63 `(inst mov
(make-ea-for-object-slot ,ptr
,slot
,lowtag
) ,value
)))
65 ;;; A handy macro for storing widetags.
66 (defmacro storeb
(value ptr
&optional
(slot 0) (lowtag 0))
67 (once-only ((value value
))
68 `(inst mov
(make-ea-for-object-slot ,ptr
,slot
,lowtag
:byte
) ,value
)))
70 (defmacro pushw
(ptr &optional
(slot 0) (lowtag 0))
71 `(inst push
(make-ea-for-object-slot ,ptr
,slot
,lowtag
)))
73 (defmacro popw
(ptr &optional
(slot 0) (lowtag 0))
74 `(inst pop
(make-ea-for-object-slot ,ptr
,slot
,lowtag
)))
76 (defmacro make-ea-for-vector-data
(object &key
(size :dword
) (offset 0)
77 index
(scale (ash (width-bits size
) -
3)))
78 `(make-ea ,size
:base
,object
:index
,index
:scale
,scale
79 :disp
(- (+ (* vector-data-offset n-word-bytes
)
81 other-pointer-lowtag
)))
83 ;;;; macros to generate useful values
85 (defmacro load-symbol
(reg symbol
)
86 `(inst mov
,reg
(+ nil-value
(static-symbol-offset ,symbol
))))
88 (defmacro make-ea-for-symbol-value
(symbol &optional
(width :dword
))
89 (declare (type symbol symbol
))
92 (static-symbol-offset ',symbol
)
93 (ash symbol-value-slot word-shift
)
94 (- other-pointer-lowtag
))))
96 (defmacro load-symbol-value
(reg symbol
)
97 `(inst mov
,reg
(make-ea-for-symbol-value ,symbol
)))
99 (defmacro store-symbol-value
(reg symbol
)
100 `(inst mov
(make-ea-for-symbol-value ,symbol
) ,reg
))
103 (defmacro make-ea-for-symbol-tls-index
(symbol)
104 (declare (type symbol symbol
))
107 (static-symbol-offset ',symbol
)
108 (ash symbol-tls-index-slot word-shift
)
109 (- other-pointer-lowtag
))))
112 (defmacro load-tl-symbol-value
(reg symbol
)
114 (inst mov
,reg
(make-ea-for-symbol-tls-index ,symbol
))
115 (inst fs-segment-prefix
)
116 (inst mov
,reg
(make-ea :dword
:base
,reg
))))
118 (defmacro load-tl-symbol-value
(reg symbol
) `(load-symbol-value ,reg
,symbol
))
121 (defmacro store-tl-symbol-value
(reg symbol temp
)
123 (inst mov
,temp
(make-ea-for-symbol-tls-index ,symbol
))
124 (inst fs-segment-prefix
)
125 (inst mov
(make-ea :dword
:base
,temp
) ,reg
)))
127 (defmacro store-tl-symbol-value
(reg symbol temp
)
128 (declare (ignore temp
))
129 `(store-symbol-value ,reg
,symbol
))
131 (defmacro load-binding-stack-pointer
(reg)
134 (inst fs-segment-prefix
)
135 (inst mov
,reg
(make-ea :dword
136 :disp
(* 4 thread-binding-stack-pointer-slot
))))
138 `(load-symbol-value ,reg
*binding-stack-pointer
*))
140 (defmacro store-binding-stack-pointer
(reg)
143 (inst fs-segment-prefix
)
144 (inst mov
(make-ea :dword
145 :disp
(* 4 thread-binding-stack-pointer-slot
))
148 `(store-symbol-value ,reg
*binding-stack-pointer
*))
150 (defmacro load-type
(target source
&optional
(offset 0))
152 "Loads the type bits of a pointer into target independent of
153 byte-ordering issues."
154 (once-only ((n-target target
)
157 (ecase *backend-byte-order
*
160 (make-ea :byte
:base
,n-source
:disp
,n-offset
)))
163 (make-ea :byte
:base
,n-source
164 :disp
(+ ,n-offset
(1- n-word-bytes
))))))))
166 ;;;; allocation helpers
168 ;;; Allocation within alloc_region (which is thread local) can be done
169 ;;; inline. If the alloc_region is overflown allocation is done by
170 ;;; calling the C alloc() function.
172 ;;; C calls for allocation don't /seem/ to make an awful lot of
173 ;;; difference to speed. On pure consing it's about a 25%
174 ;;; gain. Guessing from historical context, it looks like inline
175 ;;; allocation was introduced before pseudo-atomic, at which time all
176 ;;; calls to alloc() would have needed a syscall to mask signals for
177 ;;; the duration. Now we have pseudoatomic there's no need for that
180 (defun allocation-dynamic-extent (alloc-tn size
)
181 (inst sub esp-tn size
)
182 ;; FIXME: SIZE _should_ be double-word aligned (suggested but
183 ;; unfortunately not enforced by PAD-DATA-BLOCK and
184 ;; WITH-FIXED-ALLOCATION), so that ESP is always divisible by 8 (for
185 ;; 32-bit lispobjs). In that case, this AND instruction is
186 ;; unneccessary and could be removed. If not, explain why. -- CSR,
188 (inst and esp-tn
(lognot lowtag-mask
))
189 (aver (not (location= alloc-tn esp-tn
)))
190 (inst mov alloc-tn esp-tn
)
193 (defun allocation-notinline (alloc-tn size
)
194 (let* ((alloc-tn-offset (tn-offset alloc-tn
))
195 ;; C call to allocate via dispatch routines. Each
196 ;; destination has a special entry point. The size may be a
197 ;; register or a constant.
198 (tn-text (ecase alloc-tn-offset
204 (#.edi-offset
"edi")))
205 (size-text (case size
(8 "8_") (16 "16_") (t ""))))
206 (unless (or (eql size
8) (eql size
16))
207 (unless (and (tn-p size
) (location= alloc-tn size
))
208 (inst mov alloc-tn size
)))
209 (inst call
(make-fixup (concatenate 'string
214 (defun allocation-inline (alloc-tn size
)
215 (let ((ok (gen-label))
218 (make-ea :dword
:disp
219 #!+sb-thread
(* n-word-bytes thread-alloc-region-slot
)
220 #!-sb-thread
(make-fixup "boxed_region" :foreign
)
221 :scale
1)) ; thread->alloc_region.free_pointer
223 (make-ea :dword
:disp
224 #!+sb-thread
(* n-word-bytes
(1+ thread-alloc-region-slot
))
225 #!-sb-thread
(make-fixup "boxed_region" :foreign
4)
226 :scale
1))) ; thread->alloc_region.end_addr
227 (unless (and (tn-p size
) (location= alloc-tn size
))
228 (inst mov alloc-tn size
))
229 #!+sb-thread
(inst fs-segment-prefix
)
230 (inst add alloc-tn free-pointer
)
231 #!+sb-thread
(inst fs-segment-prefix
)
232 (inst cmp alloc-tn end-addr
)
234 (let ((dst (ecase (tn-offset alloc-tn
)
235 (#.eax-offset
"alloc_overflow_eax")
236 (#.ecx-offset
"alloc_overflow_ecx")
237 (#.edx-offset
"alloc_overflow_edx")
238 (#.ebx-offset
"alloc_overflow_ebx")
239 (#.esi-offset
"alloc_overflow_esi")
240 (#.edi-offset
"alloc_overflow_edi"))))
241 (inst call
(make-fixup dst
:foreign
)))
242 (inst jmp-short done
)
244 ;; Swap ALLOC-TN and FREE-POINTER
245 (cond ((and (tn-p size
) (location= alloc-tn size
))
246 ;; XCHG is extremely slow, use the xor swap trick
247 #!+sb-thread
(inst fs-segment-prefix
)
248 (inst xor alloc-tn free-pointer
)
249 #!+sb-thread
(inst fs-segment-prefix
)
250 (inst xor free-pointer alloc-tn
)
251 #!+sb-thread
(inst fs-segment-prefix
)
252 (inst xor alloc-tn free-pointer
))
254 ;; It's easier if SIZE is still available.
255 #!+sb-thread
(inst fs-segment-prefix
)
256 (inst mov free-pointer alloc-tn
)
257 (inst sub alloc-tn size
)))
262 ;;; Emit code to allocate an object with a size in bytes given by
263 ;;; SIZE. The size may be an integer or a TN. If Inline is a VOP
264 ;;; node-var then it is used to make an appropriate speed vs size
267 ;;; Allocation should only be used inside a pseudo-atomic section, which
268 ;;; should also cover subsequent initialization of the object.
270 ;;; (FIXME: so why aren't we asserting this?)
272 (defun allocation (alloc-tn size
&optional inline dynamic-extent
)
274 (dynamic-extent (allocation-dynamic-extent alloc-tn size
))
275 ((or (null inline
) (policy inline
(>= speed space
)))
276 (allocation-inline alloc-tn size
))
277 (t (allocation-notinline alloc-tn size
)))
280 ;;; Allocate an other-pointer object of fixed SIZE with a single word
281 ;;; header having the specified WIDETAG value. The result is placed in
283 (defmacro with-fixed-allocation
((result-tn widetag size
&optional inline stack-allocate-p
)
286 (bug "empty &body in WITH-FIXED-ALLOCATION"))
287 (once-only ((result-tn result-tn
) (size size
) (stack-allocate-p stack-allocate-p
))
288 `(maybe-pseudo-atomic ,stack-allocate-p
289 (allocation ,result-tn
(pad-data-block ,size
) ,inline
,stack-allocate-p
)
290 (storew (logior (ash (1- ,size
) n-widetag-bits
) ,widetag
)
293 (make-ea :byte
:base
,result-tn
:disp other-pointer-lowtag
))
297 (eval-when (#-sb-xc
:compile-toplevel
:load-toplevel
:execute
)
298 (defun emit-error-break (vop kind code values
)
299 (let ((vector (gensym)))
301 #-darwin
(inst int
3) ; i386 breakpoint instruction
303 ;; On Darwin, we need to use #x0b0f instead of int3 in order
304 ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
305 ;; doesn't seem to be reliably firing SIGTRAP
306 ;; handlers. Hopefully this will be fixed by Apple at a
308 #+darwin
(inst word
#x0b0f
))
309 ;; The return PC points here; note the location for the debugger.
312 (note-this-location vop
:internal-error
)))
313 (inst byte
,kind
) ; eg trap_Xyyy
314 (with-adjustable-vector (,vector
) ; interr arguments
315 (write-var-integer (error-number-or-lose ',code
) ,vector
)
316 ,@(mapcar (lambda (tn)
318 ;; classic CMU CL comment:
319 ;; zzzzz jrd here. tn-offset is zero for constant
321 (write-var-integer (make-sc-offset (sc-number
327 (inst byte
(length ,vector
))
328 (dotimes (i (length ,vector
))
329 (inst byte
(aref ,vector i
))))))))
331 (defmacro error-call
(vop error-code
&rest values
)
333 "Cause an error. ERROR-CODE is the error to cause."
335 (emit-error-break vop error-trap error-code values
)))
337 (defmacro generate-error-code
(vop error-code
&rest values
)
339 "Generate-Error-Code Error-code Value*
340 Emit code for an error with the specified Error-Code and context Values."
341 `(assemble (*elsewhere
*)
342 (let ((start-lab (gen-label)))
343 (emit-label start-lab
)
344 (error-call ,vop
,error-code
,@values
)
350 ;;; This is used to wrap operations which leave untagged memory lying
351 ;;; around. It's an operation which the AOP weenies would describe as
352 ;;; having "cross-cutting concerns", meaning it appears all over the
353 ;;; place and there's no logical single place to attach documentation.
354 ;;; grep (mostly in src/runtime) is your friend
356 ;;; KLUDGE: since the stack on the x86 is treated conservatively, it
357 ;;; does not matter whether a signal occurs during construction of a
358 ;;; dynamic-extent object, as the half-finished construction of the
359 ;;; object will not cause any difficulty. We can therefore elide
360 (defmacro maybe-pseudo-atomic
(really-p &body forms
)
363 (pseudo-atomic ,@forms
)))
366 (defmacro pseudo-atomic
(&rest forms
)
367 (with-unique-names (label)
368 `(let ((,label
(gen-label)))
369 (inst fs-segment-prefix
)
370 (inst or
(make-ea :byte
:disp
(* 4 thread-pseudo-atomic-bits-slot
))
373 (inst fs-segment-prefix
)
374 (inst xor
(make-ea :byte
:disp
(* 4 thread-pseudo-atomic-bits-slot
))
377 ;; if PAI was set, interrupts were disabled at the same
378 ;; time using the process signal mask.
379 (inst break pending-interrupt-trap
)
380 (emit-label ,label
))))
383 (defmacro pseudo-atomic
(&rest forms
)
384 (with-unique-names (label)
385 `(let ((,label
(gen-label)))
386 (inst or
(make-ea-for-symbol-value *pseudo-atomic-bits
* :byte
)
389 (inst xor
(make-ea-for-symbol-value *pseudo-atomic-bits
* :byte
)
392 ;; if PAI was set, interrupts were disabled at the same
393 ;; time using the process signal mask.
394 (inst break pending-interrupt-trap
)
395 (emit-label ,label
))))
397 ;;;; indexed references
399 (defmacro define-full-compare-and-swap
400 (name type offset lowtag scs el-type
&optional translate
)
403 ,@(when translate
`((:translate
,translate
)))
405 (:args
(object :scs
(descriptor-reg) :to
:eval
)
406 (index :scs
(any-reg immediate unsigned-reg
) :to
:result
)
407 (old-value :scs
,scs
:target eax
)
408 (new-value :scs
,scs
))
409 (:arg-types
,type tagged-num
,el-type
,el-type
)
410 (:temporary
(:sc descriptor-reg
:offset eax-offset
411 :from
(:argument
2) :to
:result
:target value
) eax
)
412 (:results
(value :scs
,scs
))
413 (:result-types
,el-type
)
418 (let ((ea (sc-case index
420 (make-ea :dword
:base object
421 :disp
(- (* (+ ,offset
(tn-value index
))
425 (make-ea :dword
:base object
:index index
:scale
4
426 :disp
(- (* ,offset n-word-bytes
)
429 (make-ea :dword
:base object
:index index
430 :disp
(- (* ,offset n-word-bytes
)
432 (inst cmpxchg ea new-value
))
435 (defmacro define-full-reffer
(name type offset lowtag scs el-type
&optional translate
)
439 `((:translate
,translate
)))
441 (:args
(object :scs
(descriptor-reg))
442 (index :scs
(any-reg immediate unsigned-reg
)))
443 (:arg-types
,type tagged-num
)
444 (:results
(value :scs
,scs
))
445 (:result-types
,el-type
)
446 (:generator
3 ; pw was 5
449 (inst mov value
(make-ea :dword
:base object
450 :disp
(- (* (+ ,offset
(tn-value index
))
454 (inst mov value
(make-ea :dword
:base object
:index index
:scale
4
455 :disp
(- (* ,offset n-word-bytes
)
458 (inst mov value
(make-ea :dword
:base object
:index index
459 :disp
(- (* ,offset n-word-bytes
)
462 (defmacro define-full-reffer
+offset
(name type offset lowtag scs el-type
&optional translate
)
466 `((:translate
,translate
)))
468 (:args
(object :scs
(descriptor-reg))
469 (index :scs
(any-reg immediate unsigned-reg
)))
470 (:arg-types
,type tagged-num
471 (:constant
(constant-displacement ,lowtag sb
!vm
:n-word-bytes
,offset
)))
473 (:results
(value :scs
,scs
))
474 (:result-types
,el-type
)
475 (:generator
3 ; pw was 5
478 (inst mov value
(make-ea :dword
:base object
479 :disp
(- (* (+ ,offset
485 (inst mov value
(make-ea :dword
:base object
:index index
:scale
4
486 :disp
(- (* (+ ,offset offset
)
490 (inst mov value
(make-ea :dword
:base object
:index index
491 :disp
(- (* (+ ,offset offset
)
495 (defmacro define-full-setter
(name type offset lowtag scs el-type
&optional translate
)
499 `((:translate
,translate
)))
501 (:args
(object :scs
(descriptor-reg))
502 (index :scs
(any-reg immediate
))
503 (value :scs
,scs
:target result
))
504 (:arg-types
,type tagged-num
,el-type
)
505 (:results
(result :scs
,scs
))
506 (:result-types
,el-type
)
507 (:generator
4 ; was 5
510 (inst mov
(make-ea :dword
:base object
511 :disp
(- (* (+ ,offset
(tn-value index
))
516 (inst mov
(make-ea :dword
:base object
:index index
517 :disp
(- (* ,offset n-word-bytes
) ,lowtag
))
519 (move result value
)))))
521 (defmacro define-full-setter
+offset
(name type offset lowtag scs el-type
&optional translate
)
525 `((:translate
,translate
)))
527 (:args
(object :scs
(descriptor-reg))
528 (index :scs
(any-reg immediate
))
529 (value :scs
,scs
:target result
))
531 (:arg-types
,type tagged-num
532 (:constant
(constant-displacement ,lowtag sb
!vm
:n-word-bytes
,offset
)) ,el-type
)
533 (:results
(result :scs
,scs
))
534 (:result-types
,el-type
)
535 (:generator
4 ; was 5
538 (inst mov
(make-ea :dword
:base object
539 :disp
(- (* (+ ,offset
(tn-value index
) offset
)
544 (inst mov
(make-ea :dword
:base object
:index index
545 :disp
(- (* (+ ,offset offset
)
546 n-word-bytes
) ,lowtag
))
548 (move result value
)))))
550 ;;; helper for alien stuff.
551 (def!macro with-pinned-objects
((&rest objects
) &body body
)
552 "Arrange with the garbage collector that the pages occupied by
553 OBJECTS will not be moved in memory for the duration of BODY.
554 Useful for e.g. foreign calls where another thread may trigger
556 `(multiple-value-prog1
558 ,@(loop for p in objects
560 ;; There is no race here wrt to gc, because at every
561 ;; point during the execution there is a reference to
562 ;; P on the stack or in a register.
563 `(push-word-on-c-stack
564 (int-sap (sb!kernel
:get-lisp-obj-address
,p
))))
566 ;; If the body returned normally, we should restore the stack pointer
567 ;; for the benefit of any following code in the same function. If
568 ;; there's a non-local exit in the body, sp is garbage anyway and
569 ;; will get set appropriately from {a, the} frame pointer before it's
571 (pop-words-from-c-stack ,(length objects
))))