1 ;;;; a bunch of handy macros for x86-64
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 ;;; This used to be a macro (and still is on the other platforms) but
17 ;;; the support for SC-dependent move instructions needed here makes
18 ;;; that expand into so large an expression that the resulting code
19 ;;; bloat is not justifiable.
22 "Move SRC into DST unless they are location=."
23 (unless (location= dst src)
25 ((single-reg complex-single-reg)
26 (aver (xmm-register-p src))
27 (inst movaps dst src))
28 ((double-reg complex-double-reg)
29 (aver (xmm-register-p src))
30 (inst movapd dst src))
32 ((int-sse-reg sse-reg)
33 (aver (xmm-register-p src))
34 (inst movdqa dst src))
36 ((single-sse-reg double-sse-reg)
37 (aver (xmm-register-p src))
38 (inst movaps dst src))
40 (inst mov dst src)))))
42 (defmacro make-ea-for-object-slot (ptr slot lowtag)
43 `(make-ea :qword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
44 (defmacro make-ea-for-object-slot-half (ptr slot lowtag)
45 `(make-ea :dword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
47 (defmacro loadw (value ptr &optional (slot 0) (lowtag 0))
48 `(inst mov ,value (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
50 (defmacro storew (value ptr &optional (slot 0) (lowtag 0))
51 (once-only ((value value))
52 `(cond ((and (integerp ,value)
53 (not (typep ,value '(signed-byte 32))))
54 (inst mov temp-reg-tn ,value)
55 (inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) temp-reg-tn))
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 (defun call-indirect (offset)
68 (inst call (make-ea :qword :disp offset)))
70 (inst mov temp-reg-tn offset)
71 (inst call (make-ea :qword :base temp-reg-tn)))))
73 ;;;; macros to generate useful values
75 (defmacro load-symbol (reg symbol)
76 `(inst mov ,reg (+ nil-value (static-symbol-offset ,symbol))))
78 (defmacro make-ea-for-symbol-value (symbol)
81 (static-symbol-offset ',symbol)
82 (ash symbol-value-slot word-shift)
83 (- other-pointer-lowtag))))
85 (defmacro load-symbol-value (reg symbol)
86 `(inst mov ,reg (make-ea-for-symbol-value ,symbol)))
88 (defmacro store-symbol-value (reg symbol)
89 `(inst mov (make-ea-for-symbol-value ,symbol) ,reg))
92 (defmacro make-ea-for-symbol-tls-index (symbol)
95 (static-symbol-offset ',symbol)
96 (ash symbol-tls-index-slot word-shift)
97 (- other-pointer-lowtag))))
100 (defmacro load-tl-symbol-value (reg symbol)
102 (inst mov ,reg (make-ea-for-symbol-tls-index ,symbol))
103 (inst mov ,reg (make-ea :qword :base thread-base-tn :scale 1 :index ,reg))))
105 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
108 (defmacro store-tl-symbol-value (reg symbol temp)
110 (inst mov ,temp (make-ea-for-symbol-tls-index ,symbol))
111 (inst mov (make-ea :qword :base thread-base-tn :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-binding-stack-pointer (reg)
119 `(inst mov ,reg (make-ea :qword :base thread-base-tn
120 :disp (* n-word-bytes thread-binding-stack-pointer-slot)))
122 `(load-symbol-value ,reg *binding-stack-pointer*))
124 (defmacro store-binding-stack-pointer (reg)
126 `(inst mov (make-ea :qword :base thread-base-tn
127 :disp (* n-word-bytes thread-binding-stack-pointer-slot))
130 `(store-symbol-value ,reg *binding-stack-pointer*))
132 (defmacro load-type (target source &optional (offset 0))
134 "Loads the type bits of a pointer into target independent of
135 byte-ordering issues."
136 (once-only ((n-target target)
139 (ecase *backend-byte-order*
141 `(inst movzx ,n-target
142 (make-ea :byte :base ,n-source :disp ,n-offset)))
144 `(inst movzx ,n-target
145 (make-ea :byte :base ,n-source
146 :disp (+ ,n-offset (1- n-word-bytes))))))))
148 ;;;; allocation helpers
150 ;;; All allocation is done by calls to assembler routines that
151 ;;; eventually invoke the C alloc() function.
153 ;;; Emit code to allocate an object with a size in bytes given by
154 ;;; Size. The size may be an integer of a TN. If Inline is a VOP
155 ;;; node-var then it is used to make an appropriate speed vs size
158 (defun allocation-dynamic-extent (alloc-tn size lowtag)
159 (inst sub rsp-tn size)
160 ;; see comment in x86/macros.lisp implementation of this
161 (inst and rsp-tn #.(lognot lowtag-mask))
162 (aver (not (location= alloc-tn rsp-tn)))
163 (inst lea alloc-tn (make-ea :byte :base rsp-tn :disp lowtag))
166 ;;; This macro should only be used inside a pseudo-atomic section,
167 ;;; which should also cover subsequent initialization of the
169 (defun allocation-tramp (alloc-tn size lowtag)
171 (inst mov alloc-tn (make-fixup "alloc_tramp" :foreign))
175 (inst lea alloc-tn (make-ea :byte :base alloc-tn :disp lowtag)))
178 (defun allocation (alloc-tn size &optional ignored dynamic-extent lowtag)
179 (declare (ignore ignored))
181 (allocation-dynamic-extent alloc-tn size lowtag)
182 (return-from allocation (values)))
183 (let ((NOT-INLINE (gen-label))
186 (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
187 ;; thread->alloc_region.free_pointer
191 :base thread-base-tn :scale 1
192 :disp (* n-word-bytes thread-alloc-region-slot))
196 (make-fixup "boxed_region" :foreign)))
197 ;; thread->alloc_region.end_addr
201 :base thread-base-tn :scale 1
202 :disp (* n-word-bytes (1+ thread-alloc-region-slot)))
206 (make-fixup "boxed_region" :foreign 8))))
208 (allocation-tramp alloc-tn size lowtag))
210 (inst mov temp-reg-tn free-pointer)
212 (if (location= alloc-tn size)
213 (inst add alloc-tn temp-reg-tn)
215 (make-ea :qword :base temp-reg-tn :index size)))
217 (make-ea :qword :base temp-reg-tn :disp size)))
218 (inst cmp end-addr alloc-tn)
219 (inst jmp :be NOT-INLINE)
220 (inst mov free-pointer alloc-tn)
222 (inst lea alloc-tn (make-ea :byte :base temp-reg-tn :disp lowtag))
223 (inst mov alloc-tn temp-reg-tn))
225 (assemble (*elsewhere*)
226 (emit-label NOT-INLINE)
227 (cond ((numberp size)
228 (allocation-tramp alloc-tn size lowtag))
230 (inst sub alloc-tn free-pointer)
231 (allocation-tramp alloc-tn alloc-tn lowtag)))
235 ;;; Allocate an other-pointer object of fixed SIZE with a single word
236 ;;; header having the specified WIDETAG value. The result is placed in
238 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline stack-allocate-p)
241 (bug "empty &body in WITH-FIXED-ALLOCATION"))
242 (once-only ((result-tn result-tn) (size size) (stack-allocate-p stack-allocate-p))
243 `(maybe-pseudo-atomic ,stack-allocate-p
244 (allocation ,result-tn (pad-data-block ,size) ,inline ,stack-allocate-p
245 other-pointer-lowtag)
246 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
247 ,result-tn 0 other-pointer-lowtag)
251 (defun emit-error-break (vop kind code values)
254 (inst int 3) ; i386 breakpoint instruction
255 ;; On Darwin, we need to use #x0b0f instead of int3 in order
256 ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
257 ;; doesn't seem to be reliably firing SIGTRAP
258 ;; handlers. Hopefully this will be fixed by Apple at a
259 ;; later date. Do the same on x86-64 as we do on x86 until this gets
263 ;; The return PC points here; note the location for the debugger.
265 (note-this-location vop :internal-error))
266 (inst byte kind) ; eg trap_Xyyy
267 (with-adjustable-vector (vector) ; interr arguments
268 (write-var-integer code vector)
270 ;; classic CMU CL comment:
271 ;; zzzzz jrd here. tn-offset is zero for constant
273 (write-var-integer (make-sc-offset (sc-number (tn-sc tn))
274 (or (tn-offset tn) 0))
276 (inst byte (length vector))
277 (dotimes (i (length vector))
278 (inst byte (aref vector i))))))
280 (defun error-call (vop error-code &rest values)
282 "Cause an error. ERROR-CODE is the error to cause."
283 (emit-error-break vop error-trap (error-number-or-lose error-code) values))
285 (defun generate-error-code (vop error-code &rest values)
287 "Generate-Error-Code Error-code Value*
288 Emit code for an error with the specified Error-Code and context Values."
289 (assemble (*elsewhere*)
290 (let ((start-lab (gen-label)))
291 (emit-label start-lab)
292 (emit-error-break vop error-trap (error-number-or-lose error-code) values)
298 ;;; This is used to wrap operations which leave untagged memory lying
299 ;;; around. It's an operation which the AOP weenies would describe as
300 ;;; having "cross-cutting concerns", meaning it appears all over the
301 ;;; place and there's no logical single place to attach documentation.
302 ;;; grep (mostly in src/runtime) is your friend
304 (defmacro maybe-pseudo-atomic (not-really-p &body body)
307 (pseudo-atomic ,@body)))
309 ;;; Unsafely clear pa flags so that the image can properly lose in a
312 (defmacro %clear-pseudo-atomic ()
313 '(inst mov (make-ea :qword :base thread-base-tn
314 :disp (* n-word-bytes thread-pseudo-atomic-bits-slot))
318 (defun emit-safepoint ()
319 (inst test al-tn (make-ea :byte :disp sb!vm::gc-safepoint-page-addr)))
322 (defmacro pseudo-atomic (&rest forms)
323 #!+sb-safepoint-strictly
324 `(progn ,@forms (emit-safepoint))
325 #!-sb-safepoint-strictly
326 (with-unique-names (label)
327 `(let ((,label (gen-label)))
328 (inst mov (make-ea :qword
330 :disp (* n-word-bytes thread-pseudo-atomic-bits-slot))
333 (inst xor (make-ea :qword
335 :disp (* n-word-bytes thread-pseudo-atomic-bits-slot))
338 ;; if PAI was set, interrupts were disabled at the same time
339 ;; using the process signal mask.
340 (inst break pending-interrupt-trap)
343 ;; In this case, when allocation thinks a GC should be done, it
344 ;; does not mark PA as interrupted, but schedules a safepoint
345 ;; trap instead. Let's take the opportunity to trigger that
346 ;; safepoint right now.
351 (defmacro pseudo-atomic (&rest forms)
352 (with-unique-names (label)
353 `(let ((,label (gen-label)))
354 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
355 ;; something. (perhaps SVLB, for static variable low byte)
356 (inst mov (make-ea :qword :disp (+ nil-value
357 (static-symbol-offset
358 '*pseudo-atomic-bits*)
359 (ash symbol-value-slot word-shift)
360 (- other-pointer-lowtag)))
363 (inst xor (make-ea :qword :disp (+ nil-value
364 (static-symbol-offset
365 '*pseudo-atomic-bits*)
366 (ash symbol-value-slot word-shift)
367 (- other-pointer-lowtag)))
370 ;; if PAI was set, interrupts were disabled at the same time
371 ;; using the process signal mask.
372 (inst break pending-interrupt-trap)
373 (emit-label ,label))))
375 ;;;; indexed references
377 (defmacro define-full-compare-and-swap
378 (name type offset lowtag scs el-type &optional translate)
381 ,@(when translate `((:translate ,translate)))
383 (:args (object :scs (descriptor-reg) :to :eval)
384 (index :scs (any-reg) :to :result)
385 (old-value :scs ,scs :target rax)
386 (new-value :scs ,scs))
387 (:arg-types ,type tagged-num ,el-type ,el-type)
388 (:temporary (:sc descriptor-reg :offset rax-offset
389 :from (:argument 2) :to :result :target value) rax)
390 (:results (value :scs ,scs))
391 (:result-types ,el-type)
394 (inst cmpxchg (make-ea :qword :base object :index index
395 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
396 :disp (- (* ,offset n-word-bytes) ,lowtag))
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 :qword :base object :index index
413 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
414 :disp (- (* ,offset n-word-bytes)
416 (define-vop (,(symbolicate name "-C"))
418 `((:translate ,translate)))
420 (:args (object :scs (descriptor-reg)))
423 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
425 (:results (value :scs ,scs))
426 (:result-types ,el-type)
427 (:generator 2 ; pw was 5
428 (inst mov value (make-ea :qword :base object
429 :disp (- (* (+ ,offset index) n-word-bytes)
432 (defmacro define-full-reffer+offset (name type offset lowtag scs el-type &optional translate)
436 `((:translate ,translate)))
438 (:args (object :scs (descriptor-reg))
439 (index :scs (any-reg)))
441 (:arg-types ,type tagged-num
442 (:constant (constant-displacement other-pointer-lowtag
443 n-word-bytes vector-data-offset)))
444 (:results (value :scs ,scs))
445 (:result-types ,el-type)
446 (:generator 3 ; pw was 5
447 (inst mov value (make-ea :qword :base object :index index
448 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
449 :disp (- (* (+ ,offset offset) n-word-bytes)
451 (define-vop (,(symbolicate name "-C"))
453 `((:translate ,translate)))
455 (:args (object :scs (descriptor-reg)))
458 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
460 (:constant (constant-displacement other-pointer-lowtag
461 n-word-bytes vector-data-offset)))
462 (:results (value :scs ,scs))
463 (:result-types ,el-type)
464 (:generator 2 ; pw was 5
465 (inst mov value (make-ea :qword :base object
466 :disp (- (* (+ ,offset index offset) n-word-bytes)
469 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
473 `((:translate ,translate)))
475 (:args (object :scs (descriptor-reg))
476 (index :scs (any-reg))
477 (value :scs ,scs :target result))
478 (:arg-types ,type tagged-num ,el-type)
479 (:results (result :scs ,scs))
480 (:result-types ,el-type)
481 (:generator 4 ; was 5
482 (inst mov (make-ea :qword :base object :index index
483 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
484 :disp (- (* ,offset n-word-bytes) ,lowtag))
486 (move result value)))
487 (define-vop (,(symbolicate name "-C"))
489 `((:translate ,translate)))
491 (:args (object :scs (descriptor-reg))
492 (value :scs ,scs :target result))
495 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
498 (:results (result :scs ,scs))
499 (:result-types ,el-type)
500 (:generator 3 ; was 5
501 (inst mov (make-ea :qword :base object
502 :disp (- (* (+ ,offset index) n-word-bytes)
505 (move result value)))))
507 (defmacro define-full-setter+offset (name type offset lowtag scs el-type &optional translate)
511 `((:translate ,translate)))
513 (:args (object :scs (descriptor-reg))
514 (index :scs (any-reg))
515 (value :scs ,scs :target result))
517 (:arg-types ,type tagged-num
518 (:constant (constant-displacement other-pointer-lowtag
522 (:results (result :scs ,scs))
523 (:result-types ,el-type)
524 (:generator 4 ; was 5
525 (inst mov (make-ea :qword :base object :index index
526 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
527 :disp (- (* (+ ,offset offset) n-word-bytes) ,lowtag))
529 (move result value)))
530 (define-vop (,(symbolicate name "-C"))
532 `((:translate ,translate)))
534 (:args (object :scs (descriptor-reg))
535 (value :scs ,scs :target result))
538 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
540 (:constant (constant-displacement other-pointer-lowtag
544 (:results (result :scs ,scs))
545 (:result-types ,el-type)
546 (:generator 3 ; was 5
547 (inst mov (make-ea :qword :base object
548 :disp (- (* (+ ,offset index offset) n-word-bytes)
551 (move result value)))))
553 ;;; helper for alien stuff.
555 (def!macro with-pinned-objects ((&rest objects) &body body)
556 "Arrange with the garbage collector that the pages occupied by
557 OBJECTS will not be moved in memory for the duration of BODY.
558 Useful for e.g. foreign calls where another thread may trigger
561 (let ((pins (make-gensym-list (length objects)))
562 (wpo (sb!xc:gensym "WITH-PINNED-OBJECTS-THUNK")))
563 ;; BODY is stuffed in a function to preserve the lexical
565 `(flet ((,wpo () (progn ,@body)))
566 (declare (muffle-conditions compiler-note))
567 ;; PINS are dx-allocated in case the compiler for some
568 ;; unfathomable reason decides to allocate value-cells
569 ;; for them -- since we have DX value-cells on x86oid
570 ;; platforms this still forces them on the stack.
571 (dx-let ,(mapcar #'list pins objects)
572 (multiple-value-prog1 (,wpo)
573 ;; TOUCH-OBJECT has a VOP with an empty body: compiler
574 ;; thinks we're using the argument and doesn't flush
575 ;; the variable, but we don't have to pay any extra
576 ;; beyond that -- and MULTIPLE-VALUE-PROG1 keeps them
577 ;; live till the body has finished. *whew*
578 ,@(mapcar (lambda (pin)
579 `(touch-object ,pin))