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 ;;; This macro should only be used inside a pseudo-atomic section,
156 ;;; which should also cover subsequent initialization of the
158 (defun allocation (alloc-tn size &optional inline)
159 ;; FIXME: since it appears that inline allocation is gone, we should
160 ;; remove the INLINE parameter and *MAYBE-USE-INLINE-ALLOCATION*
161 (declare (ignore inline))
162 (flet ((load-size (dst-tn size)
163 (unless (and (tn-p size) (location= alloc-tn size))
164 (inst mov dst-tn size))))
165 (let ((alloc-tn-offset (tn-offset alloc-tn)))
166 ;; C call to allocate via dispatch routines. Each
167 ;; destination has a special entry point. The size may be a
168 ;; register or a constant.
169 (ecase alloc-tn-offset
172 (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_eax")
174 (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_eax")
177 (load-size eax-tn size)
178 (inst call (make-fixup (extern-alien-name "alloc_to_eax")
182 (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_ecx")
184 (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_ecx")
187 (load-size ecx-tn size)
188 (inst call (make-fixup (extern-alien-name "alloc_to_ecx")
192 (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_edx")
194 (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_edx")
197 (load-size edx-tn size)
198 (inst call (make-fixup (extern-alien-name "alloc_to_edx")
202 (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_ebx")
204 (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_ebx")
207 (load-size ebx-tn size)
208 (inst call (make-fixup (extern-alien-name "alloc_to_ebx")
212 (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_esi")
214 (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_esi")
217 (load-size esi-tn size)
218 (inst call (make-fixup (extern-alien-name "alloc_to_esi")
222 (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_edi")
224 (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_edi")
227 (load-size edi-tn size)
228 (inst call (make-fixup (extern-alien-name "alloc_to_edi")
232 ;;; Allocate an other-pointer object of fixed SIZE with a single word
233 ;;; header having the specified WIDETAG value. The result is placed in
235 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline)
238 (allocation ,result-tn (pad-data-block ,size) ,inline)
239 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
242 (make-ea :byte :base ,result-tn :disp other-pointer-lowtag))
246 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
247 (defun emit-error-break (vop kind code values)
248 (let ((vector (gensym)))
249 `((inst int 3) ; i386 breakpoint instruction
250 ;; The return PC points here; note the location for the debugger.
253 (note-this-location vop :internal-error)))
254 (inst byte ,kind) ; eg trap_Xyyy
255 (with-adjustable-vector (,vector) ; interr arguments
256 (write-var-integer (error-number-or-lose ',code) ,vector)
257 ,@(mapcar (lambda (tn)
259 ;; classic CMU CL comment:
260 ;; zzzzz jrd here. tn-offset is zero for constant
262 (write-var-integer (make-sc-offset (sc-number
268 (inst byte (length ,vector))
269 (dotimes (i (length ,vector))
270 (inst byte (aref ,vector i))))))))
272 (defmacro error-call (vop error-code &rest values)
274 "Cause an error. ERROR-CODE is the error to cause."
276 (emit-error-break vop error-trap error-code values)))
278 (defmacro generate-error-code (vop error-code &rest values)
280 "Generate-Error-Code Error-code Value*
281 Emit code for an error with the specified Error-Code and context Values."
282 `(assemble (*elsewhere*)
283 (let ((start-lab (gen-label)))
284 (emit-label start-lab)
285 (error-call ,vop ,error-code ,@values)
291 ;;; This is used to wrap operations which leave untagged memory lying
292 ;;; around. It's an operation which the AOP weenies would describe as
293 ;;; having "cross-cutting concerns", meaning it appears all over the
294 ;;; place and there's no logical single place to attach documentation.
295 ;;; grep (mostly in src/runtime) is your friend
297 ;;; FIXME: *PSEUDO-ATOMIC-FOO* could be made into *PSEUDO-ATOMIC-BITS*,
298 ;;; set with a single operation and cleared with SHR *PSEUDO-ATOMIC-BITS*,-2;
299 ;;; the ATOMIC bit is bit 0, the INTERRUPTED bit is bit 1, and you check
300 ;;; the C flag after the shift to see whether you were interrupted.
303 (defmacro pseudo-atomic (&rest forms)
304 (with-unique-names (label)
305 `(let ((,label (gen-label)))
306 (inst fs-segment-prefix)
307 (inst mov (make-ea :byte
308 :disp (* 4 thread-pseudo-atomic-interrupted-slot)) 0)
309 (inst fs-segment-prefix)
310 (inst mov (make-ea :byte :disp (* 4 thread-pseudo-atomic-atomic-slot)) 1)
312 (inst fs-segment-prefix)
313 (inst mov (make-ea :byte :disp (* 4 thread-pseudo-atomic-atomic-slot)) 0)
314 (inst fs-segment-prefix)
315 (inst cmp (make-ea :byte
316 :disp (* 4 thread-pseudo-atomic-interrupted-slot)) 0)
317 (inst jmp :eq ,label)
318 ;; if PAI was set, interrupts were disabled at the same time
319 ;; using the process signal mask.
320 (inst break pending-interrupt-trap)
321 (emit-label ,label))))
324 (defmacro pseudo-atomic (&rest forms)
325 (with-unique-names (label)
326 `(let ((,label (gen-label)))
327 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
328 ;; something. (perhaps SVLB, for static variable low byte)
329 (inst mov (make-ea :byte :disp (+ nil-value
330 (static-symbol-offset
331 '*pseudo-atomic-interrupted*)
332 (ash symbol-value-slot word-shift)
333 ;; FIXME: Use mask, not minus, to
334 ;; take out type bits.
335 (- other-pointer-lowtag)))
337 (inst mov (make-ea :byte :disp (+ nil-value
338 (static-symbol-offset
339 '*pseudo-atomic-atomic*)
340 (ash symbol-value-slot word-shift)
341 (- other-pointer-lowtag)))
344 (inst mov (make-ea :byte :disp (+ nil-value
345 (static-symbol-offset
346 '*pseudo-atomic-atomic*)
347 (ash symbol-value-slot word-shift)
348 (- other-pointer-lowtag)))
350 ;; KLUDGE: Is there any requirement for interrupts to be
351 ;; handled in order? It seems as though an interrupt coming
352 ;; in at this point will be executed before any pending interrupts.
353 ;; Or do incoming interrupts check to see whether any interrupts
354 ;; are pending? I wish I could find the documentation for
355 ;; pseudo-atomics.. -- WHN 19991130
356 (inst cmp (make-ea :byte
358 (static-symbol-offset
359 '*pseudo-atomic-interrupted*)
360 (ash symbol-value-slot word-shift)
361 (- other-pointer-lowtag)))
363 (inst jmp :eq ,label)
364 ;; if PAI was set, interrupts were disabled at the same time
365 ;; using the process signal mask.
366 (inst break pending-interrupt-trap)
367 (emit-label ,label))))
371 ;;;; indexed references
373 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
377 `((:translate ,translate)))
379 (:args (object :scs (descriptor-reg))
380 (index :scs (any-reg)))
381 (:arg-types ,type tagged-num)
382 (:results (value :scs ,scs))
383 (:result-types ,el-type)
384 (:generator 3 ; pw was 5
385 (inst mov value (make-ea :dword :base object :index index
386 :disp (- (* ,offset n-word-bytes)
388 (define-vop (,(symbolicate name "-C"))
390 `((:translate ,translate)))
392 (:args (object :scs (descriptor-reg)))
394 (:arg-types ,type (:constant (signed-byte 30)))
395 (:results (value :scs ,scs))
396 (:result-types ,el-type)
397 (:generator 2 ; pw was 5
398 (inst mov value (make-ea :dword :base object
399 :disp (- (* (+ ,offset index) n-word-bytes)
402 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
406 `((:translate ,translate)))
408 (:args (object :scs (descriptor-reg))
409 (index :scs (any-reg))
410 (value :scs ,scs :target result))
411 (:arg-types ,type tagged-num ,el-type)
412 (:results (result :scs ,scs))
413 (:result-types ,el-type)
414 (:generator 4 ; was 5
415 (inst mov (make-ea :dword :base object :index index
416 :disp (- (* ,offset n-word-bytes) ,lowtag))
418 (move result value)))
419 (define-vop (,(symbolicate name "-C"))
421 `((:translate ,translate)))
423 (:args (object :scs (descriptor-reg))
424 (value :scs ,scs :target result))
426 (:arg-types ,type (:constant (signed-byte 30)) ,el-type)
427 (:results (result :scs ,scs))
428 (:result-types ,el-type)
429 (:generator 3 ; was 5
430 (inst mov (make-ea :dword :base object
431 :disp (- (* (+ ,offset index) n-word-bytes)
434 (move result value)))))
436 ;;; helper for alien stuff.
437 (defmacro with-pinned-objects ((&rest objects) &body body)
438 "Arrange with the garbage collector that the pages occupied by
439 OBJECTS will not be moved in memory for the duration of BODY.
440 Useful for e.g. foreign calls where another thread may trigger
442 `(multiple-value-prog1
444 ,@(loop for p in objects
445 collect `(push-word-on-c-stack
446 (int-sap (sb!kernel:get-lisp-obj-address ,p))))
448 ;; If the body returned normally, we should restore the stack pointer
449 ;; for the benefit of any following code in the same function. If
450 ;; there's a non-local exit in the body, sp is garbage anyway and
451 ;; will get set appropriately from {a, the} frame pointer before it's
453 (pop-words-from-c-stack ,(length objects))))