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 make-ea-for-object-slot (ptr slot lowtag)
50 `(make-ea :dword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
52 (defmacro loadw (value ptr &optional (slot 0) (lowtag 0))
53 `(inst mov ,value (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
55 (defmacro storew (value ptr &optional (slot 0) (lowtag 0))
56 (once-only ((value value))
57 `(inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) ,value)))
59 (defmacro pushw (ptr &optional (slot 0) (lowtag 0))
60 `(inst push (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
62 (defmacro popw (ptr &optional (slot 0) (lowtag 0))
63 `(inst pop (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
65 ;;;; macros to generate useful values
67 (defmacro load-symbol (reg symbol)
68 `(inst mov ,reg (+ nil-value (static-symbol-offset ,symbol))))
70 (defmacro make-ea-for-symbol-value (symbol)
73 (static-symbol-offset ',symbol)
74 (ash symbol-value-slot word-shift)
75 (- other-pointer-lowtag))))
77 (defmacro load-symbol-value (reg symbol)
78 `(inst mov ,reg (make-ea-for-symbol-value ,symbol)))
80 (defmacro store-symbol-value (reg symbol)
81 `(inst mov (make-ea-for-symbol-value ,symbol) ,reg))
84 (defmacro make-ea-for-symbol-tls-index (symbol)
87 (static-symbol-offset ',symbol)
88 (ash symbol-tls-index-slot word-shift)
89 (- other-pointer-lowtag))))
92 (defmacro load-tl-symbol-value (reg symbol)
94 (inst mov ,reg (make-ea-for-symbol-tls-index ,symbol))
95 (inst fs-segment-prefix)
96 (inst mov ,reg (make-ea :dword :base ,reg))))
98 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
101 (defmacro store-tl-symbol-value (reg symbol temp)
103 (inst mov ,temp (make-ea-for-symbol-tls-index ,symbol))
104 (inst fs-segment-prefix)
105 (inst mov (make-ea :dword :base ,temp) ,reg)))
107 (defmacro store-tl-symbol-value (reg symbol temp)
108 (declare (ignore temp))
109 `(store-symbol-value ,reg ,symbol))
111 (defmacro load-binding-stack-pointer (reg)
114 (inst fs-segment-prefix)
115 (inst mov ,reg (make-ea :dword
116 :disp (* 4 thread-binding-stack-pointer-slot))))
118 `(load-symbol-value ,reg *binding-stack-pointer*))
120 (defmacro store-binding-stack-pointer (reg)
123 (inst fs-segment-prefix)
124 (inst mov (make-ea :dword
125 :disp (* 4 thread-binding-stack-pointer-slot))
128 `(store-symbol-value ,reg *binding-stack-pointer*))
130 (defmacro load-type (target source &optional (offset 0))
132 "Loads the type bits of a pointer into target independent of
133 byte-ordering issues."
134 (once-only ((n-target target)
137 (ecase *backend-byte-order*
140 (make-ea :byte :base ,n-source :disp ,n-offset)))
143 (make-ea :byte :base ,n-source :disp (+ ,n-offset 3)))))))
145 ;;;; allocation helpers
147 ;;; Allocation within alloc_region (which is thread local) can be done
148 ;;; inline. If the alloc_region is overflown allocation is done by
149 ;;; calling the C alloc() function.
151 ;;; C calls for allocation don't /seem/ to make an awful lot of
152 ;;; difference to speed. On pure consing it's about a 25%
153 ;;; gain. Guessing from historical context, it looks like inline
154 ;;; allocation was introduced before pseudo-atomic, at which time all
155 ;;; calls to alloc() would have needed a syscall to mask signals for
156 ;;; the duration. Now we have pseudoatomic there's no need for that
159 (defun allocation-dynamic-extent (alloc-tn size)
160 (inst sub esp-tn size)
161 ;; FIXME: SIZE _should_ be double-word aligned (suggested but
162 ;; unfortunately not enforced by PAD-DATA-BLOCK and
163 ;; WITH-FIXED-ALLOCATION), so that ESP is always divisible by 8 (for
164 ;; 32-bit lispobjs). In that case, this AND instruction is
165 ;; unneccessary and could be removed. If not, explain why. -- CSR,
167 (inst and esp-tn #.(ldb (byte 32 0) (lognot lowtag-mask)))
168 (aver (not (location= alloc-tn esp-tn)))
169 (inst mov alloc-tn esp-tn)
172 (defun allocation-notinline (alloc-tn size)
173 (let* ((alloc-tn-offset (tn-offset alloc-tn))
174 ;; C call to allocate via dispatch routines. Each
175 ;; destination has a special entry point. The size may be a
176 ;; register or a constant.
177 (tn-text (ecase alloc-tn-offset
183 (#.edi-offset "edi")))
184 (size-text (case size (8 "8_") (16 "16_") (t ""))))
185 (unless (or (eql size 8) (eql size 16))
186 (unless (and (tn-p size) (location= alloc-tn size))
187 (inst mov alloc-tn size)))
188 (inst call (make-fixup (concatenate 'string
193 (defun allocation-inline (alloc-tn size)
194 (let ((ok (gen-label))
197 (make-ea :dword :disp
198 #!+sb-thread (* n-word-bytes thread-alloc-region-slot)
199 #!-sb-thread (make-fixup "boxed_region" :foreign)
200 :scale 1)) ; thread->alloc_region.free_pointer
202 (make-ea :dword :disp
203 #!+sb-thread (* n-word-bytes (1+ thread-alloc-region-slot))
204 #!-sb-thread (make-fixup "boxed_region" :foreign 4)
205 :scale 1))) ; thread->alloc_region.end_addr
206 (unless (and (tn-p size) (location= alloc-tn size))
207 (inst mov alloc-tn size))
208 #!+sb-thread (inst fs-segment-prefix)
209 (inst add alloc-tn free-pointer)
210 #!+sb-thread (inst fs-segment-prefix)
211 (inst cmp alloc-tn end-addr)
213 (let ((dst (ecase (tn-offset alloc-tn)
214 (#.eax-offset "alloc_overflow_eax")
215 (#.ecx-offset "alloc_overflow_ecx")
216 (#.edx-offset "alloc_overflow_edx")
217 (#.ebx-offset "alloc_overflow_ebx")
218 (#.esi-offset "alloc_overflow_esi")
219 (#.edi-offset "alloc_overflow_edi"))))
220 (inst call (make-fixup dst :foreign)))
221 (inst jmp-short done)
223 ;; Swap ALLOC-TN and FREE-POINTER
224 (cond ((and (tn-p size) (location= alloc-tn size))
225 ;; XCHG is extremely slow, use the xor swap trick
226 #!+sb-thread (inst fs-segment-prefix)
227 (inst xor alloc-tn free-pointer)
228 #!+sb-thread (inst fs-segment-prefix)
229 (inst xor free-pointer alloc-tn)
230 #!+sb-thread (inst fs-segment-prefix)
231 (inst xor alloc-tn free-pointer))
233 ;; It's easier if SIZE is still available.
234 #!+sb-thread (inst fs-segment-prefix)
235 (inst mov free-pointer alloc-tn)
236 (inst sub alloc-tn size)))
241 ;;; Emit code to allocate an object with a size in bytes given by
242 ;;; SIZE. The size may be an integer or a TN. If Inline is a VOP
243 ;;; node-var then it is used to make an appropriate speed vs size
246 ;;; Allocation should only be used inside a pseudo-atomic section, which
247 ;;; should also cover subsequent initialization of the object.
249 ;;; (FIXME: so why aren't we asserting this?)
251 (defun allocation (alloc-tn size &optional inline dynamic-extent)
253 (dynamic-extent (allocation-dynamic-extent alloc-tn size))
254 ((or (null inline) (policy inline (>= speed space)))
255 (allocation-inline alloc-tn size))
256 (t (allocation-notinline alloc-tn size)))
259 ;;; Allocate an other-pointer object of fixed SIZE with a single word
260 ;;; header having the specified WIDETAG value. The result is placed in
262 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline)
265 (bug "empty &body in WITH-FIXED-ALLOCATION"))
266 (once-only ((result-tn result-tn) (size size))
268 (allocation ,result-tn (pad-data-block ,size) ,inline)
269 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
272 (make-ea :byte :base ,result-tn :disp other-pointer-lowtag))
276 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
277 (defun emit-error-break (vop kind code values)
278 (let ((vector (gensym)))
280 #-darwin (inst int 3) ; i386 breakpoint instruction
282 ;; On Darwin, we need to use #x0b0f instead of int3 in order
283 ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
284 ;; doesn't seem to be reliably firing SIGTRAP
285 ;; handlers. Hopefully this will be fixed by Apple at a
287 #+darwin (inst word #x0b0f))
288 ;; The return PC points here; note the location for the debugger.
291 (note-this-location vop :internal-error)))
292 (inst byte ,kind) ; eg trap_Xyyy
293 (with-adjustable-vector (,vector) ; interr arguments
294 (write-var-integer (error-number-or-lose ',code) ,vector)
295 ,@(mapcar (lambda (tn)
297 ;; classic CMU CL comment:
298 ;; zzzzz jrd here. tn-offset is zero for constant
300 (write-var-integer (make-sc-offset (sc-number
306 (inst byte (length ,vector))
307 (dotimes (i (length ,vector))
308 (inst byte (aref ,vector i))))))))
310 (defmacro error-call (vop error-code &rest values)
312 "Cause an error. ERROR-CODE is the error to cause."
314 (emit-error-break vop error-trap error-code values)))
316 (defmacro generate-error-code (vop error-code &rest values)
318 "Generate-Error-Code Error-code Value*
319 Emit code for an error with the specified Error-Code and context Values."
320 `(assemble (*elsewhere*)
321 (let ((start-lab (gen-label)))
322 (emit-label start-lab)
323 (error-call ,vop ,error-code ,@values)
329 ;;; This is used to wrap operations which leave untagged memory lying
330 ;;; around. It's an operation which the AOP weenies would describe as
331 ;;; having "cross-cutting concerns", meaning it appears all over the
332 ;;; place and there's no logical single place to attach documentation.
333 ;;; grep (mostly in src/runtime) is your friend
335 ;;; FIXME: *PSEUDO-ATOMIC-FOO* could be made into *PSEUDO-ATOMIC-BITS*,
336 ;;; set with a single operation and cleared with SHR *PSEUDO-ATOMIC-BITS*,-2;
337 ;;; the ATOMIC bit is bit 0, the INTERRUPTED bit is bit 1, and you check
338 ;;; the C flag after the shift to see whether you were interrupted.
340 ;;; KLUDGE: since the stack on the x86 is treated conservatively, it
341 ;;; does not matter whether a signal occurs during construction of a
342 ;;; dynamic-extent object, as the half-finished construction of the
343 ;;; object will not cause any difficulty. We can therefore elide
344 (defmacro maybe-pseudo-atomic (really-p &body forms)
347 (pseudo-atomic ,@forms)))
350 (defmacro pseudo-atomic (&rest forms)
351 (with-unique-names (label)
352 `(let ((,label (gen-label)))
353 (inst fs-segment-prefix)
354 (inst mov (make-ea :byte :disp (* 4 thread-pseudo-atomic-atomic-slot))
357 (inst fs-segment-prefix)
358 (inst mov (make-ea :byte :disp (* 4 thread-pseudo-atomic-atomic-slot)) 0)
359 (inst fs-segment-prefix)
360 (inst cmp (make-ea :byte
361 :disp (* 4 thread-pseudo-atomic-interrupted-slot)) 0)
362 (inst jmp :eq ,label)
363 ;; if PAI was set, interrupts were disabled at the same
364 ;; time using the process signal mask.
365 (inst break pending-interrupt-trap)
366 (emit-label ,label))))
369 (defmacro pseudo-atomic (&rest forms)
370 (with-unique-names (label)
371 `(let ((,label (gen-label)))
372 ;; FIXME: The MAKE-EA noise should become a MACROLET macro
373 ;; or something. (perhaps SVLB, for static variable low
375 (inst mov (make-ea :byte :disp (+ nil-value
376 (static-symbol-offset
377 '*pseudo-atomic-atomic*)
378 (ash symbol-value-slot word-shift)
379 (- other-pointer-lowtag)))
382 (inst mov (make-ea :byte :disp (+ nil-value
383 (static-symbol-offset
384 '*pseudo-atomic-atomic*)
385 (ash symbol-value-slot word-shift)
386 (- other-pointer-lowtag)))
388 (inst cmp (make-ea :byte
390 (static-symbol-offset
391 '*pseudo-atomic-interrupted*)
392 (ash symbol-value-slot word-shift)
393 (- other-pointer-lowtag)))
395 (inst jmp :eq ,label)
396 ;; if PAI was set, interrupts were disabled at the same
397 ;; time using the process signal mask.
398 (inst break pending-interrupt-trap)
399 (emit-label ,label))))
401 ;;;; indexed references
403 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
407 `((:translate ,translate)))
409 (:args (object :scs (descriptor-reg))
410 (index :scs (any-reg immediate)))
411 (:arg-types ,type tagged-num)
412 (:results (value :scs ,scs))
413 (:result-types ,el-type)
414 (:generator 3 ; pw was 5
417 (inst mov value (make-ea :dword :base object
418 :disp (- (* (+ ,offset (tn-value index))
422 (inst mov value (make-ea :dword :base object :index index
423 :disp (- (* ,offset n-word-bytes)
426 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
430 `((:translate ,translate)))
432 (:args (object :scs (descriptor-reg))
433 (index :scs (any-reg immediate))
434 (value :scs ,scs :target result))
435 (:arg-types ,type tagged-num ,el-type)
436 (:results (result :scs ,scs))
437 (:result-types ,el-type)
438 (:generator 4 ; was 5
441 (inst mov (make-ea :dword :base object
442 :disp (- (* (+ ,offset (tn-value index))
447 (inst mov (make-ea :dword :base object :index index
448 :disp (- (* ,offset n-word-bytes) ,lowtag))
450 (move result value)))))
452 ;;; helper for alien stuff.
453 (defmacro with-pinned-objects ((&rest objects) &body body)
454 "Arrange with the garbage collector that the pages occupied by
455 OBJECTS will not be moved in memory for the duration of BODY.
456 Useful for e.g. foreign calls where another thread may trigger
458 `(multiple-value-prog1
460 ,@(loop for p in objects
462 ;; There is no race here wrt to gc, because at every
463 ;; point during the execution there is a reference to
464 ;; P on the stack or in a register.
465 `(push-word-on-c-stack
466 (int-sap (sb!kernel:get-lisp-obj-address ,p))))
468 ;; If the body returned normally, we should restore the stack pointer
469 ;; for the benefit of any following code in the same function. If
470 ;; there's a non-local exit in the body, sp is garbage anyway and
471 ;; will get set appropriately from {a, the} frame pointer before it's
473 (pop-words-from-c-stack ,(length objects))))