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 ;;;; instruction-like macros
16 (defmacro move (dst src)
18 "Move SRC into DST unless they are location=."
19 (once-only ((n-dst dst)
21 `(unless (location= ,n-dst ,n-src)
22 (inst mov ,n-dst ,n-src))))
24 (defmacro make-ea-for-object-slot (ptr slot lowtag)
25 `(make-ea :qword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
26 (defmacro make-ea-for-object-slot-half (ptr slot lowtag)
27 `(make-ea :dword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
29 (defmacro loadw (value ptr &optional (slot 0) (lowtag 0))
30 `(inst mov ,value (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
32 (defmacro storew (value ptr &optional (slot 0) (lowtag 0))
33 (once-only ((value value))
34 `(cond ((and (integerp ,value)
35 (not (typep ,value '(signed-byte 32))))
36 (multiple-value-bind (lo hi) (dwords-for-quad ,value)
37 (inst mov (make-ea-for-object-slot-half
38 ,ptr ,slot ,lowtag) lo)
39 (inst mov (make-ea-for-object-slot-half
40 ,ptr (+ ,slot 1/2) ,lowtag) hi)))
42 (inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) ,value)))))
44 (defmacro pushw (ptr &optional (slot 0) (lowtag 0))
45 `(inst push (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
47 (defmacro popw (ptr &optional (slot 0) (lowtag 0))
48 `(inst pop (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
50 ;;;; macros to generate useful values
52 (defmacro load-symbol (reg symbol)
53 `(inst mov ,reg (+ nil-value (static-symbol-offset ,symbol))))
55 (defmacro make-ea-for-symbol-value (symbol)
58 (static-symbol-offset ',symbol)
59 (ash symbol-value-slot word-shift)
60 (- other-pointer-lowtag))))
62 (defmacro load-symbol-value (reg symbol)
63 `(inst mov ,reg (make-ea-for-symbol-value ,symbol)))
65 (defmacro store-symbol-value (reg symbol)
66 `(inst mov (make-ea-for-symbol-value ,symbol) ,reg))
69 (defmacro make-ea-for-symbol-tls-index (symbol)
72 (static-symbol-offset ',symbol)
73 (ash symbol-tls-index-slot word-shift)
74 (- other-pointer-lowtag))))
77 (defmacro load-tl-symbol-value (reg symbol)
79 (inst mov ,reg (make-ea-for-symbol-tls-index ,symbol))
80 (inst mov ,reg (make-ea :qword :base thread-base-tn :scale 1 :index ,reg))))
82 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
85 (defmacro store-tl-symbol-value (reg symbol temp)
87 (inst mov ,temp (make-ea-for-symbol-tls-index ,symbol))
88 (inst mov (make-ea :qword :base thread-base-tn :scale 1 :index ,temp) ,reg)))
90 (defmacro store-tl-symbol-value (reg symbol temp)
91 (declare (ignore temp))
92 `(store-symbol-value ,reg ,symbol))
94 (defmacro load-type (target source &optional (offset 0))
96 "Loads the type bits of a pointer into target independent of
97 byte-ordering issues."
98 (once-only ((n-target target)
101 (ecase *backend-byte-order*
104 (make-ea :byte :base ,n-source :disp ,n-offset)))
107 (make-ea :byte :base ,n-source :disp (+ ,n-offset 4)))))))
109 ;;;; allocation helpers
111 ;;; All allocation is done by calls to assembler routines that
112 ;;; eventually invoke the C alloc() function.
114 ;;; Emit code to allocate an object with a size in bytes given by
115 ;;; Size. The size may be an integer of a TN. If Inline is a VOP
116 ;;; node-var then it is used to make an appropriate speed vs size
119 (defun allocation-dynamic-extent (alloc-tn size)
120 (inst sub rsp-tn size)
121 ;; see comment in x86/macros.lisp implementation of this
122 (inst and rsp-tn #.(lognot lowtag-mask))
123 (aver (not (location= alloc-tn rsp-tn)))
124 (inst mov alloc-tn rsp-tn)
127 ;;; This macro should only be used inside a pseudo-atomic section,
128 ;;; which should also cover subsequent initialization of the
130 (defun allocation-tramp (alloc-tn size &optional ignored)
131 (declare (ignore ignored))
133 (inst lea temp-reg-tn (make-ea :qword
134 :disp (make-fixup "alloc_tramp" :foreign)))
135 (inst call temp-reg-tn)
139 (defun allocation (alloc-tn size &optional ignored dynamic-extent)
140 (declare (ignore ignored))
142 (allocation-dynamic-extent alloc-tn size)
143 (return-from allocation (values)))
144 (let ((NOT-INLINE (gen-label))
147 (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
148 ;; thread->alloc_region.free_pointer
152 :base thread-base-tn :scale 1
153 :disp (* n-word-bytes thread-alloc-region-slot))
157 (make-fixup (extern-alien-name "boxed_region") :foreign)))
158 ;; thread->alloc_region.end_addr
162 :base thread-base-tn :scale 1
163 :disp (* n-word-bytes (1+ thread-alloc-region-slot)))
167 (make-fixup (extern-alien-name "boxed_region") :foreign 8))))
169 (allocation-tramp alloc-tn size))
171 (inst mov temp-reg-tn free-pointer)
172 (unless (and (tn-p size) (location= alloc-tn size))
173 (inst mov alloc-tn size))
174 (inst add alloc-tn temp-reg-tn)
175 (inst cmp end-addr alloc-tn)
176 (inst jmp :be NOT-INLINE)
177 (inst mov free-pointer alloc-tn)
178 (inst mov alloc-tn temp-reg-tn)
180 (assemble (*elsewhere*)
181 (emit-label NOT-INLINE)
182 (cond ((numberp size)
183 (allocation-tramp alloc-tn size))
185 (inst sub alloc-tn free-pointer)
186 (allocation-tramp alloc-tn alloc-tn)))
191 (defun allocation (alloc-tn size &optional ignored)
192 (declare (ignore ignored))
194 (inst lea temp-reg-tn (make-ea :qword
195 :disp (make-fixup "alloc_tramp" :foreign)))
196 (inst call temp-reg-tn)
200 ;;; Allocate an other-pointer object of fixed SIZE with a single word
201 ;;; header having the specified WIDETAG value. The result is placed in
203 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline)
206 (bug "empty &body in WITH-FIXED-ALLOCATION"))
207 (once-only ((result-tn result-tn) (size size))
209 (allocation ,result-tn (pad-data-block ,size) ,inline)
210 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
213 (make-ea :qword :base ,result-tn :disp other-pointer-lowtag))
217 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
218 (defun emit-error-break (vop kind code values)
219 (let ((vector (gensym)))
220 `((inst int 3) ; i386 breakpoint instruction
221 ;; The return PC points here; note the location for the debugger.
224 (note-this-location vop :internal-error)))
225 (inst byte ,kind) ; eg trap_Xyyy
226 (with-adjustable-vector (,vector) ; interr arguments
227 (write-var-integer (error-number-or-lose ',code) ,vector)
228 ,@(mapcar (lambda (tn)
230 ;; classic CMU CL comment:
231 ;; zzzzz jrd here. tn-offset is zero for constant
233 (write-var-integer (make-sc-offset (sc-number
239 (inst byte (length ,vector))
240 (dotimes (i (length ,vector))
241 (inst byte (aref ,vector i))))))))
243 (defmacro error-call (vop error-code &rest values)
245 "Cause an error. ERROR-CODE is the error to cause."
247 (emit-error-break vop error-trap error-code values)))
249 (defmacro generate-error-code (vop error-code &rest values)
251 "Generate-Error-Code Error-code Value*
252 Emit code for an error with the specified Error-Code and context Values."
253 `(assemble (*elsewhere*)
254 (let ((start-lab (gen-label)))
255 (emit-label start-lab)
256 (error-call ,vop ,error-code ,@values)
262 ;;; This is used to wrap operations which leave untagged memory lying
263 ;;; around. It's an operation which the AOP weenies would describe as
264 ;;; having "cross-cutting concerns", meaning it appears all over the
265 ;;; place and there's no logical single place to attach documentation.
266 ;;; grep (mostly in src/runtime) is your friend
268 ;;; FIXME: *PSEUDO-ATOMIC-FOO* could be made into *PSEUDO-ATOMIC-BITS*,
269 ;;; set with a single operation and cleared with SHR *PSEUDO-ATOMIC-BITS*,-2;
270 ;;; the ATOMIC bit is bit 0, the INTERRUPTED bit is bit 1, and you check
271 ;;; the C flag after the shift to see whether you were interrupted.
273 ;;; FIXME: THIS NAME IS BACKWARDS!
274 (defmacro maybe-pseudo-atomic (really-p &body body)
277 (pseudo-atomic ,@body)))
280 (defmacro pseudo-atomic (&rest forms)
281 (with-unique-names (label)
282 `(let ((,label (gen-label)))
283 (inst mov (make-ea :byte
285 :disp (* 8 thread-pseudo-atomic-interrupted-slot)) 0)
286 (inst mov (make-ea :byte
288 :disp (* 8 thread-pseudo-atomic-atomic-slot))
291 (inst mov (make-ea :byte
293 :disp (* 8 thread-pseudo-atomic-atomic-slot)) 0)
294 (inst cmp (make-ea :byte
296 :disp (* 8 thread-pseudo-atomic-interrupted-slot)) 0)
297 (inst jmp :eq ,label)
298 ;; if PAI was set, interrupts were disabled at the same
299 ;; time using the process signal mask.
300 (inst break pending-interrupt-trap)
301 (emit-label ,label))))
305 (defmacro pseudo-atomic (&rest forms)
306 (with-unique-names (label)
307 `(let ((,label (gen-label)))
308 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
309 ;; something. (perhaps SVLB, for static variable low byte)
310 (inst mov (make-ea :byte :disp (+ nil-value
311 (static-symbol-offset
312 '*pseudo-atomic-interrupted*)
313 (ash symbol-value-slot word-shift)
314 ;; FIXME: Use mask, not minus, to
315 ;; take out type bits.
316 (- other-pointer-lowtag)))
318 (inst mov (make-ea :byte :disp (+ nil-value
319 (static-symbol-offset
320 '*pseudo-atomic-atomic*)
321 (ash symbol-value-slot word-shift)
322 (- other-pointer-lowtag)))
325 (inst mov (make-ea :byte :disp (+ nil-value
326 (static-symbol-offset
327 '*pseudo-atomic-atomic*)
328 (ash symbol-value-slot word-shift)
329 (- other-pointer-lowtag)))
331 ;; KLUDGE: Is there any requirement for interrupts to be
332 ;; handled in order? It seems as though an interrupt coming
333 ;; in at this point will be executed before any pending interrupts.
334 ;; Or do incoming interrupts check to see whether any interrupts
335 ;; are pending? I wish I could find the documentation for
336 ;; pseudo-atomics.. -- WHN 19991130
337 (inst cmp (make-ea :byte
339 (static-symbol-offset
340 '*pseudo-atomic-interrupted*)
341 (ash symbol-value-slot word-shift)
342 (- other-pointer-lowtag)))
344 (inst jmp :eq ,label)
345 ;; if PAI was set, interrupts were disabled at the same time
346 ;; using the process signal mask.
347 (inst break pending-interrupt-trap)
348 (emit-label ,label))))
352 ;;;; indexed references
354 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
358 `((:translate ,translate)))
360 (:args (object :scs (descriptor-reg))
361 (index :scs (any-reg)))
362 (:arg-types ,type tagged-num)
363 (:results (value :scs ,scs))
364 (:result-types ,el-type)
365 (:generator 3 ; pw was 5
366 (inst mov value (make-ea :qword :base object :index index
367 :disp (- (* ,offset n-word-bytes)
369 (define-vop (,(symbolicate name "-C"))
371 `((:translate ,translate)))
373 (:args (object :scs (descriptor-reg)))
376 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
378 (:results (value :scs ,scs))
379 (:result-types ,el-type)
380 (:generator 2 ; pw was 5
381 (inst mov value (make-ea :qword :base object
382 :disp (- (* (+ ,offset index) n-word-bytes)
385 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
389 `((:translate ,translate)))
391 (:args (object :scs (descriptor-reg))
392 (index :scs (any-reg))
393 (value :scs ,scs :target result))
394 (:arg-types ,type tagged-num ,el-type)
395 (:results (result :scs ,scs))
396 (:result-types ,el-type)
397 (:generator 4 ; was 5
398 (inst mov (make-ea :qword :base object :index index
399 :disp (- (* ,offset n-word-bytes) ,lowtag))
401 (move result value)))
402 (define-vop (,(symbolicate name "-C"))
404 `((:translate ,translate)))
406 (:args (object :scs (descriptor-reg))
407 (value :scs ,scs :target result))
410 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
413 (:results (result :scs ,scs))
414 (:result-types ,el-type)
415 (:generator 3 ; was 5
416 (inst mov (make-ea :qword :base object
417 :disp (- (* (+ ,offset index) n-word-bytes)
420 (move result value)))))
422 ;;; helper for alien stuff.
423 (defmacro with-pinned-objects ((&rest objects) &body body)
424 "Arrange with the garbage collector that the pages occupied by
425 OBJECTS will not be moved in memory for the duration of BODY.
426 Useful for e.g. foreign calls where another thread may trigger
428 `(multiple-value-prog1
430 ,@(loop for p in objects
431 collect `(push-word-on-c-stack
432 (int-sap (sb!kernel:get-lisp-obj-address ,p))))
434 ;; If the body returned normally, we should restore the stack pointer
435 ;; for the benefit of any following code in the same function. If
436 ;; there's a non-local exit in the body, sp is garbage anyway and
437 ;; will get set appropriately from {a, the} frame pointer before it's
439 (pop-words-from-c-stack ,(length objects))))