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 load-symbol-value (reg symbol)
74 (static-symbol-offset ',symbol)
75 (ash symbol-value-slot word-shift)
76 (- other-pointer-lowtag)))))
78 (defmacro store-symbol-value (reg symbol)
82 (static-symbol-offset ',symbol)
83 (ash symbol-value-slot word-shift)
84 (- other-pointer-lowtag)))
88 (defmacro load-tl-symbol-value (reg symbol)
93 (static-symbol-offset ',symbol)
94 (ash symbol-tls-index-slot word-shift)
95 (- other-pointer-lowtag))))
96 (inst fs-segment-prefix)
97 (inst mov ,reg (make-ea :dword :scale 1 :index ,reg))))
99 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
102 (defmacro store-tl-symbol-value (reg symbol temp)
107 (static-symbol-offset ',symbol)
108 (ash symbol-tls-index-slot word-shift)
109 (- other-pointer-lowtag))))
110 (inst fs-segment-prefix)
111 (inst mov (make-ea :dword :scale 1 :index ,temp) ,reg)))
113 (defmacro store-tl-symbol-value (reg symbol temp)
114 (declare (ignore temp))
115 `(store-symbol-value ,reg ,symbol))
117 (defmacro load-type (target source &optional (offset 0))
119 "Loads the type bits of a pointer into target independent of
120 byte-ordering issues."
121 (once-only ((n-target target)
124 (ecase *backend-byte-order*
127 (make-ea :byte :base ,n-source :disp ,n-offset)))
130 (make-ea :byte :base ,n-source :disp (+ ,n-offset 3)))))))
132 ;;;; allocation helpers
134 ;;; All allocation is done by calls to assembler routines that
135 ;;; eventually invoke the C alloc() function. Once upon a time
136 ;;; (before threads) allocation within an alloc_region could also be
137 ;;; done inline, with the aid of two C symbols storing the current
138 ;;; allocation region boundaries; however, C cymbols are global.
140 ;;; C calls for allocation don't /seem/ to make an awful lot of
141 ;;; difference to speed. Guessing from historical context, it looks
142 ;;; like inline allocation was introduced before pseudo-atomic, at
143 ;;; which time all calls to alloc() would have needed a syscall to
144 ;;; mask signals for the duration. Now we have pseudoatomic there's
145 ;;; no need for that overhead. Still, inline alloc would be a neat
148 (defvar *maybe-use-inline-allocation* t) ; FIXME unused
150 ;;; Emit code to allocate an object with a size in bytes given by
151 ;;; SIZE. The size may be an integer of a TN. If Inline is a VOP
152 ;;; node-var then it is used to make an appropriate speed vs size
155 (defun allocation-dynamic-extent (alloc-tn size)
156 (inst sub esp-tn size)
157 ;; FIXME: SIZE _should_ be double-word aligned (suggested but
158 ;; unfortunately not enforced by PAD-DATA-BLOCK and
159 ;; WITH-FIXED-ALLOCATION), so that ESP is always divisible by 8 (for
160 ;; 32-bit lispobjs). In that case, this AND instruction is
161 ;; unneccessary and could be removed. If not, explain why. -- CSR,
163 (inst and esp-tn #.(ldb (byte 32 0) (lognot lowtag-mask)))
164 (aver (not (location= alloc-tn esp-tn)))
165 (inst mov alloc-tn esp-tn)
168 (defun allocation-notinline (alloc-tn size)
169 (flet ((load-size (dst-tn size)
170 (unless (and (tn-p size) (location= alloc-tn size))
171 (inst mov dst-tn size))))
172 (let ((alloc-tn-offset (tn-offset alloc-tn)))
173 ;; C call to allocate via dispatch routines. Each
174 ;; destination has a special entry point. The size may be a
175 ;; register or a constant.
176 (ecase alloc-tn-offset
179 (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_eax")
181 (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_eax")
184 (load-size eax-tn size)
185 (inst call (make-fixup (extern-alien-name "alloc_to_eax")
189 (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_ecx")
191 (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_ecx")
194 (load-size ecx-tn size)
195 (inst call (make-fixup (extern-alien-name "alloc_to_ecx")
199 (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_edx")
201 (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_edx")
204 (load-size edx-tn size)
205 (inst call (make-fixup (extern-alien-name "alloc_to_edx")
209 (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_ebx")
211 (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_ebx")
214 (load-size ebx-tn size)
215 (inst call (make-fixup (extern-alien-name "alloc_to_ebx")
219 (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_esi")
221 (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_esi")
224 (load-size esi-tn size)
225 (inst call (make-fixup (extern-alien-name "alloc_to_esi")
229 (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_edi")
231 (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_edi")
234 (load-size edi-tn size)
235 (inst call (make-fixup (extern-alien-name "alloc_to_edi")
238 ;;; This macro should only be used inside a pseudo-atomic section,
239 ;;; which should also cover subsequent initialization of the object.
240 ;;; (FIXME: so why aren't we asserting this?)
241 (defun allocation (alloc-tn size &optional inline dynamic-extent)
242 ;; FIXME: since it appears that inline allocation is gone, we should
243 ;; remove the INLINE parameter and *MAYBE-USE-INLINE-ALLOCATION*
244 (declare (ignore inline))
246 (dynamic-extent (allocation-dynamic-extent alloc-tn size))
247 (t (allocation-notinline alloc-tn size)))
250 ;;; Allocate an other-pointer object of fixed SIZE with a single word
251 ;;; header having the specified WIDETAG value. The result is placed in
253 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline)
256 (allocation ,result-tn (pad-data-block ,size) ,inline)
257 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
260 (make-ea :byte :base ,result-tn :disp other-pointer-lowtag))
264 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
265 (defun emit-error-break (vop kind code values)
266 (let ((vector (gensym)))
267 `((inst int 3) ; i386 breakpoint instruction
268 ;; The return PC points here; note the location for the debugger.
271 (note-this-location vop :internal-error)))
272 (inst byte ,kind) ; eg trap_Xyyy
273 (with-adjustable-vector (,vector) ; interr arguments
274 (write-var-integer (error-number-or-lose ',code) ,vector)
275 ,@(mapcar (lambda (tn)
277 ;; classic CMU CL comment:
278 ;; zzzzz jrd here. tn-offset is zero for constant
280 (write-var-integer (make-sc-offset (sc-number
286 (inst byte (length ,vector))
287 (dotimes (i (length ,vector))
288 (inst byte (aref ,vector i))))))))
290 (defmacro error-call (vop error-code &rest values)
292 "Cause an error. ERROR-CODE is the error to cause."
294 (emit-error-break vop error-trap error-code values)))
296 (defmacro generate-error-code (vop error-code &rest values)
298 "Generate-Error-Code Error-code Value*
299 Emit code for an error with the specified Error-Code and context Values."
300 `(assemble (*elsewhere*)
301 (let ((start-lab (gen-label)))
302 (emit-label start-lab)
303 (error-call ,vop ,error-code ,@values)
309 ;;; This is used to wrap operations which leave untagged memory lying
310 ;;; around. It's an operation which the AOP weenies would describe as
311 ;;; having "cross-cutting concerns", meaning it appears all over the
312 ;;; place and there's no logical single place to attach documentation.
313 ;;; grep (mostly in src/runtime) is your friend
315 ;;; FIXME: *PSEUDO-ATOMIC-FOO* could be made into *PSEUDO-ATOMIC-BITS*,
316 ;;; set with a single operation and cleared with SHR *PSEUDO-ATOMIC-BITS*,-2;
317 ;;; the ATOMIC bit is bit 0, the INTERRUPTED bit is bit 1, and you check
318 ;;; the C flag after the shift to see whether you were interrupted.
320 ;;; KLUDGE: since the stack on the x86 is treated conservatively, it
321 ;;; does not matter whether a signal occurs during construction of a
322 ;;; dynamic-extent object, as the half-finished construction of the
323 ;;; object will not cause any difficulty. We can therefore elide
324 (defvar *dynamic-extent* nil)
327 (defmacro pseudo-atomic (&rest forms)
328 (with-unique-names (label)
329 `(if *dynamic-extent* ; I will burn in hell
331 (let ((,label (gen-label)))
332 (inst fs-segment-prefix)
333 (inst mov (make-ea :byte
334 :disp (* 4 thread-pseudo-atomic-interrupted-slot)) 0)
335 (inst fs-segment-prefix)
336 (inst mov (make-ea :byte :disp (* 4 thread-pseudo-atomic-atomic-slot)) 1)
338 (inst fs-segment-prefix)
339 (inst mov (make-ea :byte :disp (* 4 thread-pseudo-atomic-atomic-slot)) 0)
340 (inst fs-segment-prefix)
341 (inst cmp (make-ea :byte
342 :disp (* 4 thread-pseudo-atomic-interrupted-slot)) 0)
343 (inst jmp :eq ,label)
344 ;; if PAI was set, interrupts were disabled at the same
345 ;; time using the process signal mask.
346 (inst break pending-interrupt-trap)
347 (emit-label ,label)))))
350 (defmacro pseudo-atomic (&rest forms)
351 (with-unique-names (label)
352 `(if *dynamic-extent*
354 (let ((,label (gen-label)))
355 ;; FIXME: The MAKE-EA noise should become a MACROLET macro
356 ;; or something. (perhaps SVLB, for static variable low
358 (inst mov (make-ea :byte :disp (+ nil-value
359 (static-symbol-offset
360 '*pseudo-atomic-interrupted*)
361 (ash symbol-value-slot word-shift)
362 ;; FIXME: Use mask, not minus, to
363 ;; take out type bits.
364 (- other-pointer-lowtag)))
366 (inst mov (make-ea :byte :disp (+ nil-value
367 (static-symbol-offset
368 '*pseudo-atomic-atomic*)
369 (ash symbol-value-slot word-shift)
370 (- other-pointer-lowtag)))
373 (inst mov (make-ea :byte :disp (+ nil-value
374 (static-symbol-offset
375 '*pseudo-atomic-atomic*)
376 (ash symbol-value-slot word-shift)
377 (- other-pointer-lowtag)))
379 ;; KLUDGE: Is there any requirement for interrupts to be
380 ;; handled in order? It seems as though an interrupt coming
381 ;; in at this point will be executed before any pending
382 ;; interrupts. Or do incoming interrupts check to see
383 ;; whether any interrupts are pending? I wish I could find
384 ;; the documentation for pseudo-atomics.. -- WHN 19991130
385 (inst cmp (make-ea :byte
387 (static-symbol-offset
388 '*pseudo-atomic-interrupted*)
389 (ash symbol-value-slot word-shift)
390 (- other-pointer-lowtag)))
392 (inst jmp :eq ,label)
393 ;; if PAI was set, interrupts were disabled at the same
394 ;; time using the process signal mask.
395 (inst break pending-interrupt-trap)
396 (emit-label ,label)))))
398 ;;;; indexed references
400 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
404 `((:translate ,translate)))
406 (:args (object :scs (descriptor-reg))
407 (index :scs (any-reg)))
408 (:arg-types ,type tagged-num)
409 (:results (value :scs ,scs))
410 (:result-types ,el-type)
411 (:generator 3 ; pw was 5
412 (inst mov value (make-ea :dword :base object :index index
413 :disp (- (* ,offset n-word-bytes)
415 (define-vop (,(symbolicate name "-C"))
417 `((:translate ,translate)))
419 (:args (object :scs (descriptor-reg)))
421 (:arg-types ,type (:constant (signed-byte 30)))
422 (:results (value :scs ,scs))
423 (:result-types ,el-type)
424 (:generator 2 ; pw was 5
425 (inst mov value (make-ea :dword :base object
426 :disp (- (* (+ ,offset index) n-word-bytes)
429 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
433 `((:translate ,translate)))
435 (:args (object :scs (descriptor-reg))
436 (index :scs (any-reg))
437 (value :scs ,scs :target result))
438 (:arg-types ,type tagged-num ,el-type)
439 (:results (result :scs ,scs))
440 (:result-types ,el-type)
441 (:generator 4 ; was 5
442 (inst mov (make-ea :dword :base object :index index
443 :disp (- (* ,offset n-word-bytes) ,lowtag))
445 (move result value)))
446 (define-vop (,(symbolicate name "-C"))
448 `((:translate ,translate)))
450 (:args (object :scs (descriptor-reg))
451 (value :scs ,scs :target result))
453 (:arg-types ,type (:constant (signed-byte 30)) ,el-type)
454 (:results (result :scs ,scs))
455 (:result-types ,el-type)
456 (:generator 3 ; was 5
457 (inst mov (make-ea :dword :base object
458 :disp (- (* (+ ,offset index) n-word-bytes)
461 (move result value)))))
463 ;;; helper for alien stuff.
464 (defmacro with-pinned-objects ((&rest objects) &body body)
465 "Arrange with the garbage collector that the pages occupied by
466 OBJECTS will not be moved in memory for the duration of BODY.
467 Useful for e.g. foreign calls where another thread may trigger
469 `(multiple-value-prog1
471 ,@(loop for p in objects
472 collect `(push-word-on-c-stack
473 (int-sap (sb!kernel:get-lisp-obj-address ,p))))
475 ;; If the body returned normally, we should restore the stack pointer
476 ;; for the benefit of any following code in the same function. If
477 ;; there's a non-local exit in the body, sp is garbage anyway and
478 ;; will get set appropriately from {a, the} frame pointer before it's
480 (pop-words-from-c-stack ,(length objects))))