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 :qword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
51 (defmacro make-ea-for-object-slot-half (ptr slot lowtag)
52 `(make-ea :dword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
54 (defmacro loadw (value ptr &optional (slot 0) (lowtag 0))
55 `(inst mov ,value (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
57 (defmacro storew (value ptr &optional (slot 0) (lowtag 0))
58 (once-only ((value value))
59 `(cond ((and (integerp ,value)
61 '(or (signed-byte 32) (unsigned-byte 32)))))
62 (multiple-value-bind (lo hi) (dwords-for-quad ,value)
63 (inst mov (make-ea-for-object-slot-half
64 ,ptr ,slot ,lowtag) lo)
65 (inst mov (make-ea-for-object-slot-half
66 ,ptr (+ ,slot 1/2) ,lowtag) hi)))
68 (inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) ,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 ;;;; macros to generate useful values
78 (defmacro load-symbol (reg symbol)
79 `(inst mov ,reg (+ nil-value (static-symbol-offset ,symbol))))
81 (defmacro load-symbol-value (reg symbol)
85 (static-symbol-offset ',symbol)
86 (ash symbol-value-slot word-shift)
87 (- other-pointer-lowtag)))))
89 (defmacro store-symbol-value (reg symbol)
93 (static-symbol-offset ',symbol)
94 (ash symbol-value-slot word-shift)
95 (- other-pointer-lowtag)))
99 (defmacro load-tl-symbol-value (reg symbol)
104 (static-symbol-offset ',symbol)
105 (ash symbol-tls-index-slot word-shift)
106 (- other-pointer-lowtag))))
107 (inst fs-segment-prefix)
108 (inst mov ,reg (make-ea :qword :scale 1 :index ,reg))))
110 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
113 (defmacro store-tl-symbol-value (reg symbol temp)
118 (static-symbol-offset ',symbol)
119 (ash symbol-tls-index-slot word-shift)
120 (- other-pointer-lowtag))))
121 (inst fs-segment-prefix)
122 (inst mov (make-ea :qword :scale 1 :index ,temp) ,reg)))
124 (defmacro store-tl-symbol-value (reg symbol temp)
125 (declare (ignore temp))
126 `(store-symbol-value ,reg ,symbol))
128 (defmacro load-type (target source &optional (offset 0))
130 "Loads the type bits of a pointer into target independent of
131 byte-ordering issues."
132 (once-only ((n-target target)
135 (ecase *backend-byte-order*
138 (make-ea :byte :base ,n-source :disp ,n-offset)))
141 (make-ea :byte :base ,n-source :disp (+ ,n-offset 4)))))))
143 ;;;; allocation helpers
145 ;;; All allocation is done by calls to assembler routines that
146 ;;; eventually invoke the C alloc() function.
148 ;;; Emit code to allocate an object with a size in bytes given by
149 ;;; Size. The size may be an integer of a TN. If Inline is a VOP
150 ;;; node-var then it is used to make an appropriate speed vs size
153 ;;; This macro should only be used inside a pseudo-atomic section,
154 ;;; which should also cover subsequent initialization of the
156 (defun allocation-tramp (alloc-tn size &optional ignored)
157 (declare (ignore ignored))
159 (inst lea r13-tn (make-ea :qword
160 :disp (make-fixup (extern-alien-name "alloc_tramp")
166 (defun allocation (alloc-tn size &optional ignored)
167 (declare (ignore ignored))
168 (let ((not-inline (gen-label))
171 (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
173 (make-ea :qword :disp
174 #!+sb-thread (* n-word-bytes thread-alloc-region-slot)
175 #!-sb-thread (make-fixup (extern-alien-name "boxed_region")
177 :scale 1)) ; thread->alloc_region.free_pointer
179 (make-ea :qword :disp
180 #!+sb-thread (* n-word-bytes (1+ thread-alloc-region-slot))
181 #!-sb-thread (make-fixup (extern-alien-name "boxed_region")
183 :scale 1))) ; thread->alloc_region.end_addr
185 (allocation-tramp alloc-tn size))
187 (unless (and (tn-p size) (location= alloc-tn size))
188 (inst mov alloc-tn size))
189 #!+sb-thread (inst fs-segment-prefix)
190 (inst add alloc-tn free-pointer)
191 #!+sb-thread (inst fs-segment-prefix)
192 (inst cmp end-addr alloc-tn)
193 (inst jmp :be NOT-INLINE)
194 #!+sb-thread (inst fs-segment-prefix)
195 (inst xchg free-pointer alloc-tn)
197 (assemble (*elsewhere*)
198 (emit-label NOT-INLINE)
199 (cond ((numberp size)
200 (allocation-tramp alloc-tn size))
202 (inst sub alloc-tn free-pointer)
203 (allocation-tramp alloc-tn alloc-tn)))
208 (defun allocation (alloc-tn size &optional ignored)
209 (declare (ignore ignored))
211 (inst lea r13-tn (make-ea :qword
212 :disp (make-fixup (extern-alien-name "alloc_tramp")
218 ;;; Allocate an other-pointer object of fixed SIZE with a single word
219 ;;; header having the specified WIDETAG value. The result is placed in
221 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline)
224 (allocation ,result-tn (pad-data-block ,size) ,inline)
225 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
228 (make-ea :qword :base ,result-tn :disp other-pointer-lowtag))
232 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
233 (defun emit-error-break (vop kind code values)
234 (let ((vector (gensym)))
235 `((inst int 3) ; i386 breakpoint instruction
236 ;; The return PC points here; note the location for the debugger.
239 (note-this-location vop :internal-error)))
240 (inst byte ,kind) ; eg trap_Xyyy
241 (with-adjustable-vector (,vector) ; interr arguments
242 (write-var-integer (error-number-or-lose ',code) ,vector)
243 ,@(mapcar (lambda (tn)
245 ;; classic CMU CL comment:
246 ;; zzzzz jrd here. tn-offset is zero for constant
248 (write-var-integer (make-sc-offset (sc-number
254 (inst byte (length ,vector))
255 (dotimes (i (length ,vector))
256 (inst byte (aref ,vector i))))))))
258 (defmacro error-call (vop error-code &rest values)
260 "Cause an error. ERROR-CODE is the error to cause."
262 (emit-error-break vop error-trap error-code values)))
264 (defmacro generate-error-code (vop error-code &rest values)
266 "Generate-Error-Code Error-code Value*
267 Emit code for an error with the specified Error-Code and context Values."
268 `(assemble (*elsewhere*)
269 (let ((start-lab (gen-label)))
270 (emit-label start-lab)
271 (error-call ,vop ,error-code ,@values)
277 ;;; This is used to wrap operations which leave untagged memory lying
278 ;;; around. It's an operation which the AOP weenies would describe as
279 ;;; having "cross-cutting concerns", meaning it appears all over the
280 ;;; place and there's no logical single place to attach documentation.
281 ;;; grep (mostly in src/runtime) is your friend
283 ;;; FIXME: *PSEUDO-ATOMIC-FOO* could be made into *PSEUDO-ATOMIC-BITS*,
284 ;;; set with a single operation and cleared with SHR *PSEUDO-ATOMIC-BITS*,-2;
285 ;;; the ATOMIC bit is bit 0, the INTERRUPTED bit is bit 1, and you check
286 ;;; the C flag after the shift to see whether you were interrupted.
288 (defmacro pseudo-atomic (&rest forms)
289 (with-unique-names (label)
290 `(let ((,label (gen-label)))
291 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
292 ;; something. (perhaps SVLB, for static variable low byte)
293 (inst mov (make-ea :byte :disp (+ nil-value
294 (static-symbol-offset
295 '*pseudo-atomic-interrupted*)
296 (ash symbol-value-slot word-shift)
297 ;; FIXME: Use mask, not minus, to
298 ;; take out type bits.
299 (- other-pointer-lowtag)))
301 (inst mov (make-ea :byte :disp (+ nil-value
302 (static-symbol-offset
303 '*pseudo-atomic-atomic*)
304 (ash symbol-value-slot word-shift)
305 (- other-pointer-lowtag)))
308 (inst mov (make-ea :byte :disp (+ nil-value
309 (static-symbol-offset
310 '*pseudo-atomic-atomic*)
311 (ash symbol-value-slot word-shift)
312 (- other-pointer-lowtag)))
314 ;; KLUDGE: Is there any requirement for interrupts to be
315 ;; handled in order? It seems as though an interrupt coming
316 ;; in at this point will be executed before any pending interrupts.
317 ;; Or do incoming interrupts check to see whether any interrupts
318 ;; are pending? I wish I could find the documentation for
319 ;; pseudo-atomics.. -- WHN 19991130
320 (inst cmp (make-ea :byte
322 (static-symbol-offset
323 '*pseudo-atomic-interrupted*)
324 (ash symbol-value-slot word-shift)
325 (- other-pointer-lowtag)))
327 (inst jmp :eq ,label)
328 ;; if PAI was set, interrupts were disabled at the same time
329 ;; using the process signal mask.
330 (inst break pending-interrupt-trap)
331 (emit-label ,label))))
335 ;;;; indexed references
337 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
341 `((:translate ,translate)))
343 (:args (object :scs (descriptor-reg))
344 (index :scs (any-reg)))
345 (:arg-types ,type tagged-num)
346 (:results (value :scs ,scs))
347 (:result-types ,el-type)
348 (:generator 3 ; pw was 5
349 (inst mov value (make-ea :qword :base object :index index
350 :disp (- (* ,offset n-word-bytes)
352 (define-vop (,(symbolicate name "-C"))
354 `((:translate ,translate)))
356 (:args (object :scs (descriptor-reg)))
359 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
361 (:results (value :scs ,scs))
362 (:result-types ,el-type)
363 (:generator 2 ; pw was 5
364 (inst mov value (make-ea :qword :base object
365 :disp (- (* (+ ,offset index) n-word-bytes)
368 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
372 `((:translate ,translate)))
374 (:args (object :scs (descriptor-reg))
375 (index :scs (any-reg))
376 (value :scs ,scs :target result))
377 (:arg-types ,type tagged-num ,el-type)
378 (:results (result :scs ,scs))
379 (:result-types ,el-type)
380 (:generator 4 ; was 5
381 (inst mov (make-ea :qword :base object :index index
382 :disp (- (* ,offset n-word-bytes) ,lowtag))
384 (move result value)))
385 (define-vop (,(symbolicate name "-C"))
387 `((:translate ,translate)))
389 (:args (object :scs (descriptor-reg))
390 (value :scs ,scs :target result))
393 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
396 (:results (result :scs ,scs))
397 (:result-types ,el-type)
398 (:generator 3 ; was 5
399 (inst mov (make-ea :qword :base object
400 :disp (- (* (+ ,offset index) n-word-bytes)
403 (move result value)))))
405 ;;; helper for alien stuff.
406 (defmacro with-pinned-objects ((&rest objects) &body body)
407 "Arrange with the garbage collector that the pages occupied by
408 OBJECTS will not be moved in memory for the duration of BODY.
409 Useful for e.g. foreign calls where another thread may trigger
411 `(multiple-value-prog1
413 ,@(loop for p in objects
414 collect `(push-word-on-c-stack
415 (int-sap (sb!kernel:get-lisp-obj-address ,p))))
417 ;; If the body returned normally, we should restore the stack pointer
418 ;; for the benefit of any following code in the same function. If
419 ;; there's a non-local exit in the body, sp is garbage anyway and
420 ;; will get set appropriately from {a, the} frame pointer before it's
422 (pop-words-from-c-stack ,(length objects))))