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)
24 (inst movss ,n-dst ,n-src))
26 (inst movsd ,n-dst ,n-src))
28 (inst mov ,n-dst ,n-src))))))
30 (defmacro make-ea-for-object-slot (ptr slot lowtag)
31 `(make-ea :qword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
32 (defmacro make-ea-for-object-slot-half (ptr slot lowtag)
33 `(make-ea :dword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
35 (defmacro loadw (value ptr &optional (slot 0) (lowtag 0))
36 `(inst mov ,value (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
38 (defmacro storew (value ptr &optional (slot 0) (lowtag 0))
39 (once-only ((value value))
40 `(cond ((and (integerp ,value)
41 (not (typep ,value '(signed-byte 32))))
42 (multiple-value-bind (lo hi) (dwords-for-quad ,value)
43 (inst mov (make-ea-for-object-slot-half
44 ,ptr ,slot ,lowtag) lo)
45 (inst mov (make-ea-for-object-slot-half
46 ,ptr (+ ,slot 1/2) ,lowtag) hi)))
48 (inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) ,value)))))
50 (defmacro pushw (ptr &optional (slot 0) (lowtag 0))
51 `(inst push (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
53 (defmacro popw (ptr &optional (slot 0) (lowtag 0))
54 `(inst pop (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
56 ;;;; macros to generate useful values
58 (defmacro load-symbol (reg symbol)
59 `(inst mov ,reg (+ nil-value (static-symbol-offset ,symbol))))
61 (defmacro make-ea-for-symbol-value (symbol)
64 (static-symbol-offset ',symbol)
65 (ash symbol-value-slot word-shift)
66 (- other-pointer-lowtag))))
68 (defmacro load-symbol-value (reg symbol)
69 `(inst mov ,reg (make-ea-for-symbol-value ,symbol)))
71 (defmacro store-symbol-value (reg symbol)
72 `(inst mov (make-ea-for-symbol-value ,symbol) ,reg))
75 (defmacro make-ea-for-symbol-tls-index (symbol)
78 (static-symbol-offset ',symbol)
79 (ash symbol-tls-index-slot word-shift)
80 (- other-pointer-lowtag))))
83 (defmacro load-tl-symbol-value (reg symbol)
85 (inst mov ,reg (make-ea-for-symbol-tls-index ,symbol))
86 (inst mov ,reg (make-ea :qword :base thread-base-tn :scale 1 :index ,reg))))
88 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
91 (defmacro store-tl-symbol-value (reg symbol temp)
93 (inst mov ,temp (make-ea-for-symbol-tls-index ,symbol))
94 (inst mov (make-ea :qword :base thread-base-tn :scale 1 :index ,temp) ,reg)))
96 (defmacro store-tl-symbol-value (reg symbol temp)
97 (declare (ignore temp))
98 `(store-symbol-value ,reg ,symbol))
100 (defmacro load-binding-stack-pointer (reg)
102 `(inst mov ,reg (make-ea :qword :base thread-base-tn
103 :disp (* 8 thread-binding-stack-pointer-slot)))
105 `(load-symbol-value ,reg *binding-stack-pointer*))
107 (defmacro store-binding-stack-pointer (reg)
109 `(inst mov (make-ea :qword :base thread-base-tn
110 :disp (* 8 thread-binding-stack-pointer-slot))
113 `(store-symbol-value ,reg *binding-stack-pointer*))
115 (defmacro load-type (target source &optional (offset 0))
117 "Loads the type bits of a pointer into target independent of
118 byte-ordering issues."
119 (once-only ((n-target target)
122 (ecase *backend-byte-order*
125 (make-ea :byte :base ,n-source :disp ,n-offset)))
128 (make-ea :byte :base ,n-source
129 :disp (+ ,n-offset (1- n-word-bytes))))))))
131 ;;;; allocation helpers
133 ;;; All allocation is done by calls to assembler routines that
134 ;;; eventually invoke the C alloc() function.
136 ;;; Emit code to allocate an object with a size in bytes given by
137 ;;; Size. The size may be an integer of a TN. If Inline is a VOP
138 ;;; node-var then it is used to make an appropriate speed vs size
141 (defun allocation-dynamic-extent (alloc-tn size)
142 (inst sub rsp-tn size)
143 ;; see comment in x86/macros.lisp implementation of this
144 (inst and rsp-tn #.(lognot lowtag-mask))
145 (aver (not (location= alloc-tn rsp-tn)))
146 (inst mov alloc-tn rsp-tn)
149 ;;; This macro should only be used inside a pseudo-atomic section,
150 ;;; which should also cover subsequent initialization of the
152 (defun allocation-tramp (alloc-tn size &optional ignored)
153 (declare (ignore ignored))
155 (inst lea temp-reg-tn (make-ea :qword
156 :disp (make-fixup "alloc_tramp" :foreign)))
157 (inst call temp-reg-tn)
161 (defun allocation (alloc-tn size &optional ignored dynamic-extent)
162 (declare (ignore ignored))
164 (allocation-dynamic-extent alloc-tn size)
165 (return-from allocation (values)))
166 (let ((NOT-INLINE (gen-label))
169 (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
170 ;; thread->alloc_region.free_pointer
174 :base thread-base-tn :scale 1
175 :disp (* n-word-bytes thread-alloc-region-slot))
179 (make-fixup "boxed_region" :foreign)))
180 ;; thread->alloc_region.end_addr
184 :base thread-base-tn :scale 1
185 :disp (* n-word-bytes (1+ thread-alloc-region-slot)))
189 (make-fixup "boxed_region" :foreign 8))))
191 (allocation-tramp alloc-tn size))
193 (inst mov temp-reg-tn free-pointer)
195 (if (location= alloc-tn size)
196 (inst add alloc-tn temp-reg-tn)
198 (make-ea :qword :base temp-reg-tn :index size)))
200 (make-ea :qword :base temp-reg-tn :disp size)))
201 (inst cmp end-addr alloc-tn)
202 (inst jmp :be NOT-INLINE)
203 (inst mov free-pointer alloc-tn)
204 (inst mov alloc-tn temp-reg-tn)
206 (assemble (*elsewhere*)
207 (emit-label NOT-INLINE)
208 (cond ((numberp size)
209 (allocation-tramp alloc-tn size))
211 (inst sub alloc-tn free-pointer)
212 (allocation-tramp alloc-tn alloc-tn)))
216 ;;; Allocate an other-pointer object of fixed SIZE with a single word
217 ;;; header having the specified WIDETAG value. The result is placed in
219 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline stack-allocate-p)
222 (bug "empty &body in WITH-FIXED-ALLOCATION"))
223 (once-only ((result-tn result-tn) (size size) (stack-allocate-p stack-allocate-p))
224 `(maybe-pseudo-atomic ,stack-allocate-p
225 (allocation ,result-tn (pad-data-block ,size) ,inline ,stack-allocate-p)
226 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
229 (make-ea :qword :base ,result-tn :disp other-pointer-lowtag))
233 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
234 (defun emit-error-break (vop kind code values)
235 (let ((vector (gensym)))
237 #!-darwin (inst int 3) ; i386 breakpoint instruction
238 ;; On Darwin, we need to use #x0b0f instead of int3 in order
239 ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
240 ;; doesn't seem to be reliably firing SIGTRAP
241 ;; handlers. Hopefully this will be fixed by Apple at a
242 ;; later date. Do the same on x86-64 as we do on x86 until this gets
244 #!+darwin (inst word #x0b0f))
246 ;; The return PC points here; note the location for the debugger.
249 (note-this-location vop :internal-error)))
250 (inst byte ,kind) ; eg trap_Xyyy
251 (with-adjustable-vector (,vector) ; interr arguments
252 (write-var-integer (error-number-or-lose ',code) ,vector)
253 ,@(mapcar (lambda (tn)
255 ;; classic CMU CL comment:
256 ;; zzzzz jrd here. tn-offset is zero for constant
258 (write-var-integer (make-sc-offset (sc-number
264 (inst byte (length ,vector))
265 (dotimes (i (length ,vector))
266 (inst byte (aref ,vector i))))))))
268 (defmacro error-call (vop error-code &rest values)
270 "Cause an error. ERROR-CODE is the error to cause."
272 (emit-error-break vop error-trap error-code values)))
274 (defmacro generate-error-code (vop error-code &rest values)
276 "Generate-Error-Code Error-code Value*
277 Emit code for an error with the specified Error-Code and context Values."
278 `(assemble (*elsewhere*)
279 (let ((start-lab (gen-label)))
280 (emit-label start-lab)
281 (error-call ,vop ,error-code ,@values)
287 ;;; This is used to wrap operations which leave untagged memory lying
288 ;;; around. It's an operation which the AOP weenies would describe as
289 ;;; having "cross-cutting concerns", meaning it appears all over the
290 ;;; place and there's no logical single place to attach documentation.
291 ;;; grep (mostly in src/runtime) is your friend
293 ;;; FIXME: THIS NAME IS BACKWARDS!
294 (defmacro maybe-pseudo-atomic (really-p &body body)
297 (pseudo-atomic ,@body)))
300 (defmacro pseudo-atomic (&rest forms)
301 (with-unique-names (label)
302 `(let ((,label (gen-label)))
303 (inst or (make-ea :byte
305 :disp (* 8 thread-pseudo-atomic-bits-slot))
308 (inst xor (make-ea :byte
310 :disp (* 8 thread-pseudo-atomic-bits-slot))
313 ;; if PAI was set, interrupts were disabled at the same
314 ;; time using the process signal mask.
315 (inst break pending-interrupt-trap)
316 (emit-label ,label))))
320 (defmacro pseudo-atomic (&rest forms)
321 (with-unique-names (label)
322 `(let ((,label (gen-label)))
323 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
324 ;; something. (perhaps SVLB, for static variable low byte)
325 (inst or (make-ea :byte :disp (+ nil-value
326 (static-symbol-offset
327 '*pseudo-atomic-bits*)
328 (ash symbol-value-slot word-shift)
329 (- other-pointer-lowtag)))
332 (inst xor (make-ea :byte :disp (+ nil-value
333 (static-symbol-offset
334 '*pseudo-atomic-bits*)
335 (ash symbol-value-slot word-shift)
336 (- other-pointer-lowtag)))
339 ;; if PAI was set, interrupts were disabled at the same time
340 ;; using the process signal mask.
341 (inst break pending-interrupt-trap)
342 (emit-label ,label))))
346 ;;;; indexed references
348 (defmacro define-full-compare-and-swap
349 (name type offset lowtag scs el-type &optional translate)
352 ,@(when translate `((:translate ,translate)))
354 (:args (object :scs (descriptor-reg) :to :eval)
355 (index :scs (any-reg) :to :result)
356 (old-value :scs ,scs :target rax)
357 (new-value :scs ,scs))
358 (:arg-types ,type tagged-num ,el-type ,el-type)
359 (:temporary (:sc descriptor-reg :offset rax-offset
360 :from (:argument 2) :to :result :target value) rax)
361 (:results (value :scs ,scs))
362 (:result-types ,el-type)
367 (inst cmpxchg (make-ea :qword :base object :index index
368 :disp (- (* ,offset n-word-bytes) ,lowtag))
372 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
376 `((:translate ,translate)))
378 (:args (object :scs (descriptor-reg))
379 (index :scs (any-reg)))
380 (:arg-types ,type tagged-num)
381 (:results (value :scs ,scs))
382 (:result-types ,el-type)
383 (:generator 3 ; pw was 5
384 (inst mov value (make-ea :qword :base object :index index
385 :disp (- (* ,offset n-word-bytes)
387 (define-vop (,(symbolicate name "-C"))
389 `((:translate ,translate)))
391 (:args (object :scs (descriptor-reg)))
394 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
396 (:results (value :scs ,scs))
397 (:result-types ,el-type)
398 (:generator 2 ; pw was 5
399 (inst mov value (make-ea :qword :base object
400 :disp (- (* (+ ,offset index) n-word-bytes)
403 (defmacro define-full-reffer+offset (name type offset lowtag scs el-type &optional translate)
407 `((:translate ,translate)))
409 (:args (object :scs (descriptor-reg))
410 (index :scs (any-reg)))
412 (:arg-types ,type tagged-num
413 (:constant (constant-displacement other-pointer-lowtag
414 n-word-bytes vector-data-offset)))
415 (:results (value :scs ,scs))
416 (:result-types ,el-type)
417 (:generator 3 ; pw was 5
418 (inst mov value (make-ea :qword :base object :index index
419 :disp (- (* (+ ,offset offset) n-word-bytes)
421 (define-vop (,(symbolicate name "-C"))
423 `((:translate ,translate)))
425 (:args (object :scs (descriptor-reg)))
428 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
430 (:constant (constant-displacement other-pointer-lowtag
431 n-word-bytes vector-data-offset)))
432 (:results (value :scs ,scs))
433 (:result-types ,el-type)
434 (:generator 2 ; pw was 5
435 (inst mov value (make-ea :qword :base object
436 :disp (- (* (+ ,offset index offset) n-word-bytes)
439 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
443 `((:translate ,translate)))
445 (:args (object :scs (descriptor-reg))
446 (index :scs (any-reg))
447 (value :scs ,scs :target result))
448 (:arg-types ,type tagged-num ,el-type)
449 (:results (result :scs ,scs))
450 (:result-types ,el-type)
451 (:generator 4 ; was 5
452 (inst mov (make-ea :qword :base object :index index
453 :disp (- (* ,offset n-word-bytes) ,lowtag))
455 (move result value)))
456 (define-vop (,(symbolicate name "-C"))
458 `((:translate ,translate)))
460 (:args (object :scs (descriptor-reg))
461 (value :scs ,scs :target result))
464 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
467 (:results (result :scs ,scs))
468 (:result-types ,el-type)
469 (:generator 3 ; was 5
470 (inst mov (make-ea :qword :base object
471 :disp (- (* (+ ,offset index) n-word-bytes)
474 (move result value)))))
476 (defmacro define-full-setter+offset (name type offset lowtag scs el-type &optional translate)
480 `((:translate ,translate)))
482 (:args (object :scs (descriptor-reg))
483 (index :scs (any-reg))
484 (value :scs ,scs :target result))
486 (:arg-types ,type tagged-num
487 (:constant (constant-displacement other-pointer-lowtag
491 (:results (result :scs ,scs))
492 (:result-types ,el-type)
493 (:generator 4 ; was 5
494 (inst mov (make-ea :qword :base object :index index
495 :disp (- (* (+ ,offset offset) n-word-bytes) ,lowtag))
497 (move result value)))
498 (define-vop (,(symbolicate name "-C"))
500 `((:translate ,translate)))
502 (:args (object :scs (descriptor-reg))
503 (value :scs ,scs :target result))
506 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
508 (:constant (constant-displacement other-pointer-lowtag
512 (:results (result :scs ,scs))
513 (:result-types ,el-type)
514 (:generator 3 ; was 5
515 (inst mov (make-ea :qword :base object
516 :disp (- (* (+ ,offset index offset) n-word-bytes)
519 (move result value)))))
521 ;;; helper for alien stuff.
523 (def!macro with-pinned-objects ((&rest objects) &body body)
524 "Arrange with the garbage collector that the pages occupied by
525 OBJECTS will not be moved in memory for the duration of BODY.
526 Useful for e.g. foreign calls where another thread may trigger
529 (let ((pins (make-gensym-list (length objects)))
530 (wpo (block-gensym "WPO")))
531 ;; BODY is stuffed in a function to preserve the lexical
533 `(flet ((,wpo () (progn ,@body)))
534 ;; PINS are dx-allocated in case the compiler for some
535 ;; unfathomable reason decides to allocate value-cells
536 ;; for them -- since we have DX value-cells on x86oid
537 ;; platforms this still forces them on the stack.
538 (dx-let ,(mapcar #'list pins objects)
539 (multiple-value-prog1 (,wpo)
540 ;; TOUCH-OBJECT has a VOP with an empty body: compiler
541 ;; thinks we're using the argument and doesn't flush
542 ;; the variable, but we don't have to pay any extra
543 ;; beyond that -- and MULTIPLE-VALUE-PROG1 keeps them
544 ;; live till the body has finished. *whew*
545 ,@(mapcar (lambda (pin)
546 `(touch-object ,pin))