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 lea temp-reg-tn (make-ea :qword
172 :disp (make-fixup "alloc_tramp" :foreign)))
173 (inst call temp-reg-tn)
176 (inst lea alloc-tn (make-ea :byte :base alloc-tn :disp lowtag)))
179 (defun allocation (alloc-tn size &optional ignored dynamic-extent lowtag)
180 (declare (ignore ignored))
182 (allocation-dynamic-extent alloc-tn size lowtag)
183 (return-from allocation (values)))
184 (let ((NOT-INLINE (gen-label))
187 (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
188 ;; thread->alloc_region.free_pointer
192 :base thread-base-tn :scale 1
193 :disp (* n-word-bytes thread-alloc-region-slot))
197 (make-fixup "boxed_region" :foreign)))
198 ;; thread->alloc_region.end_addr
202 :base thread-base-tn :scale 1
203 :disp (* n-word-bytes (1+ thread-alloc-region-slot)))
207 (make-fixup "boxed_region" :foreign 8))))
209 (allocation-tramp alloc-tn size lowtag))
211 (inst mov temp-reg-tn free-pointer)
213 (if (location= alloc-tn size)
214 (inst add alloc-tn temp-reg-tn)
216 (make-ea :qword :base temp-reg-tn :index size)))
218 (make-ea :qword :base temp-reg-tn :disp size)))
219 (inst cmp end-addr alloc-tn)
220 (inst jmp :be NOT-INLINE)
221 (inst mov free-pointer alloc-tn)
223 (inst lea alloc-tn (make-ea :byte :base temp-reg-tn :disp lowtag))
224 (inst mov alloc-tn temp-reg-tn))
226 (assemble (*elsewhere*)
227 (emit-label NOT-INLINE)
228 (cond ((numberp size)
229 (allocation-tramp alloc-tn size lowtag))
231 (inst sub alloc-tn free-pointer)
232 (allocation-tramp alloc-tn alloc-tn lowtag)))
236 ;;; Allocate an other-pointer object of fixed SIZE with a single word
237 ;;; header having the specified WIDETAG value. The result is placed in
239 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline stack-allocate-p)
242 (bug "empty &body in WITH-FIXED-ALLOCATION"))
243 (once-only ((result-tn result-tn) (size size) (stack-allocate-p stack-allocate-p))
244 `(maybe-pseudo-atomic ,stack-allocate-p
245 (allocation ,result-tn (pad-data-block ,size) ,inline ,stack-allocate-p
246 other-pointer-lowtag)
247 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
248 ,result-tn 0 other-pointer-lowtag)
252 (defun emit-error-break (vop kind code values)
255 (inst int 3) ; i386 breakpoint instruction
256 ;; On Darwin, we need to use #x0b0f instead of int3 in order
257 ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
258 ;; doesn't seem to be reliably firing SIGTRAP
259 ;; handlers. Hopefully this will be fixed by Apple at a
260 ;; later date. Do the same on x86-64 as we do on x86 until this gets
264 ;; The return PC points here; note the location for the debugger.
266 (note-this-location vop :internal-error))
267 (inst byte kind) ; eg trap_Xyyy
268 (with-adjustable-vector (vector) ; interr arguments
269 (write-var-integer code vector)
271 ;; classic CMU CL comment:
272 ;; zzzzz jrd here. tn-offset is zero for constant
274 (write-var-integer (make-sc-offset (sc-number (tn-sc tn))
275 (or (tn-offset tn) 0))
277 (inst byte (length vector))
278 (dotimes (i (length vector))
279 (inst byte (aref vector i))))))
281 (defun error-call (vop error-code &rest values)
283 "Cause an error. ERROR-CODE is the error to cause."
284 (emit-error-break vop error-trap (error-number-or-lose error-code) values))
286 (defun generate-error-code (vop error-code &rest values)
288 "Generate-Error-Code Error-code Value*
289 Emit code for an error with the specified Error-Code and context Values."
290 (assemble (*elsewhere*)
291 (let ((start-lab (gen-label)))
292 (emit-label start-lab)
293 (emit-error-break vop error-trap (error-number-or-lose error-code) values)
299 ;;; This is used to wrap operations which leave untagged memory lying
300 ;;; around. It's an operation which the AOP weenies would describe as
301 ;;; having "cross-cutting concerns", meaning it appears all over the
302 ;;; place and there's no logical single place to attach documentation.
303 ;;; grep (mostly in src/runtime) is your friend
305 (defmacro maybe-pseudo-atomic (not-really-p &body body)
308 (pseudo-atomic ,@body)))
310 ;;; Unsafely clear pa flags so that the image can properly lose in a
313 (defmacro %clear-pseudo-atomic ()
314 '(inst mov (make-ea :qword :base thread-base-tn
315 :disp (* n-word-bytes thread-pseudo-atomic-bits-slot))
319 (defun emit-safepoint ()
320 (inst test al-tn (make-ea :byte :disp sb!vm::gc-safepoint-page-addr)))
323 (defmacro pseudo-atomic (&rest forms)
324 #!+sb-safepoint-strictly
325 `(progn ,@forms (emit-safepoint))
326 #!-sb-safepoint-strictly
327 (with-unique-names (label)
328 `(let ((,label (gen-label)))
329 (inst mov (make-ea :qword
331 :disp (* n-word-bytes thread-pseudo-atomic-bits-slot))
334 (inst xor (make-ea :qword
336 :disp (* n-word-bytes thread-pseudo-atomic-bits-slot))
339 ;; if PAI was set, interrupts were disabled at the same time
340 ;; using the process signal mask.
341 (inst break pending-interrupt-trap)
344 ;; In this case, when allocation thinks a GC should be done, it
345 ;; does not mark PA as interrupted, but schedules a safepoint
346 ;; trap instead. Let's take the opportunity to trigger that
347 ;; safepoint right now.
352 (defmacro pseudo-atomic (&rest forms)
353 (with-unique-names (label)
354 `(let ((,label (gen-label)))
355 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
356 ;; something. (perhaps SVLB, for static variable low byte)
357 (inst mov (make-ea :qword :disp (+ nil-value
358 (static-symbol-offset
359 '*pseudo-atomic-bits*)
360 (ash symbol-value-slot word-shift)
361 (- other-pointer-lowtag)))
364 (inst xor (make-ea :qword :disp (+ nil-value
365 (static-symbol-offset
366 '*pseudo-atomic-bits*)
367 (ash symbol-value-slot word-shift)
368 (- other-pointer-lowtag)))
371 ;; if PAI was set, interrupts were disabled at the same time
372 ;; using the process signal mask.
373 (inst break pending-interrupt-trap)
374 (emit-label ,label))))
376 ;;;; indexed references
378 (defmacro define-full-compare-and-swap
379 (name type offset lowtag scs el-type &optional translate)
382 ,@(when translate `((:translate ,translate)))
384 (:args (object :scs (descriptor-reg) :to :eval)
385 (index :scs (any-reg) :to :result)
386 (old-value :scs ,scs :target rax)
387 (new-value :scs ,scs))
388 (:arg-types ,type tagged-num ,el-type ,el-type)
389 (:temporary (:sc descriptor-reg :offset rax-offset
390 :from (:argument 2) :to :result :target value) rax)
391 (:results (value :scs ,scs))
392 (:result-types ,el-type)
395 (inst cmpxchg (make-ea :qword :base object :index index
396 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
397 :disp (- (* ,offset n-word-bytes) ,lowtag))
401 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
405 `((:translate ,translate)))
407 (:args (object :scs (descriptor-reg))
408 (index :scs (any-reg)))
409 (:arg-types ,type tagged-num)
410 (:results (value :scs ,scs))
411 (:result-types ,el-type)
412 (:generator 3 ; pw was 5
413 (inst mov value (make-ea :qword :base object :index index
414 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
415 :disp (- (* ,offset n-word-bytes)
417 (define-vop (,(symbolicate name "-C"))
419 `((:translate ,translate)))
421 (:args (object :scs (descriptor-reg)))
424 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
426 (:results (value :scs ,scs))
427 (:result-types ,el-type)
428 (:generator 2 ; pw was 5
429 (inst mov value (make-ea :qword :base object
430 :disp (- (* (+ ,offset index) n-word-bytes)
433 (defmacro define-full-reffer+offset (name type offset lowtag scs el-type &optional translate)
437 `((:translate ,translate)))
439 (:args (object :scs (descriptor-reg))
440 (index :scs (any-reg)))
442 (:arg-types ,type tagged-num
443 (:constant (constant-displacement other-pointer-lowtag
444 n-word-bytes vector-data-offset)))
445 (:results (value :scs ,scs))
446 (:result-types ,el-type)
447 (:generator 3 ; pw was 5
448 (inst mov value (make-ea :qword :base object :index index
449 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
450 :disp (- (* (+ ,offset offset) n-word-bytes)
452 (define-vop (,(symbolicate name "-C"))
454 `((:translate ,translate)))
456 (:args (object :scs (descriptor-reg)))
459 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
461 (:constant (constant-displacement other-pointer-lowtag
462 n-word-bytes vector-data-offset)))
463 (:results (value :scs ,scs))
464 (:result-types ,el-type)
465 (:generator 2 ; pw was 5
466 (inst mov value (make-ea :qword :base object
467 :disp (- (* (+ ,offset index offset) n-word-bytes)
470 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
474 `((:translate ,translate)))
476 (:args (object :scs (descriptor-reg))
477 (index :scs (any-reg))
478 (value :scs ,scs :target result))
479 (:arg-types ,type tagged-num ,el-type)
480 (:results (result :scs ,scs))
481 (:result-types ,el-type)
482 (:generator 4 ; was 5
483 (inst mov (make-ea :qword :base object :index index
484 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
485 :disp (- (* ,offset n-word-bytes) ,lowtag))
487 (move result value)))
488 (define-vop (,(symbolicate name "-C"))
490 `((:translate ,translate)))
492 (:args (object :scs (descriptor-reg))
493 (value :scs ,scs :target result))
496 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
499 (:results (result :scs ,scs))
500 (:result-types ,el-type)
501 (:generator 3 ; was 5
502 (inst mov (make-ea :qword :base object
503 :disp (- (* (+ ,offset index) n-word-bytes)
506 (move result value)))))
508 (defmacro define-full-setter+offset (name type offset lowtag scs el-type &optional translate)
512 `((:translate ,translate)))
514 (:args (object :scs (descriptor-reg))
515 (index :scs (any-reg))
516 (value :scs ,scs :target result))
518 (:arg-types ,type tagged-num
519 (:constant (constant-displacement other-pointer-lowtag
523 (:results (result :scs ,scs))
524 (:result-types ,el-type)
525 (:generator 4 ; was 5
526 (inst mov (make-ea :qword :base object :index index
527 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
528 :disp (- (* (+ ,offset offset) n-word-bytes) ,lowtag))
530 (move result value)))
531 (define-vop (,(symbolicate name "-C"))
533 `((:translate ,translate)))
535 (:args (object :scs (descriptor-reg))
536 (value :scs ,scs :target result))
539 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
541 (:constant (constant-displacement other-pointer-lowtag
545 (:results (result :scs ,scs))
546 (:result-types ,el-type)
547 (:generator 3 ; was 5
548 (inst mov (make-ea :qword :base object
549 :disp (- (* (+ ,offset index offset) n-word-bytes)
552 (move result value)))))
554 ;;; helper for alien stuff.
556 (def!macro with-pinned-objects ((&rest objects) &body body)
557 "Arrange with the garbage collector that the pages occupied by
558 OBJECTS will not be moved in memory for the duration of BODY.
559 Useful for e.g. foreign calls where another thread may trigger
562 (let ((pins (make-gensym-list (length objects)))
563 (wpo (sb!xc:gensym "WITH-PINNED-OBJECTS-THUNK")))
564 ;; BODY is stuffed in a function to preserve the lexical
566 `(flet ((,wpo () (progn ,@body)))
567 (declare (muffle-conditions compiler-note))
568 ;; PINS are dx-allocated in case the compiler for some
569 ;; unfathomable reason decides to allocate value-cells
570 ;; for them -- since we have DX value-cells on x86oid
571 ;; platforms this still forces them on the stack.
572 (dx-let ,(mapcar #'list pins objects)
573 (multiple-value-prog1 (,wpo)
574 ;; TOUCH-OBJECT has a VOP with an empty body: compiler
575 ;; thinks we're using the argument and doesn't flush
576 ;; the variable, but we don't have to pay any extra
577 ;; beyond that -- and MULTIPLE-VALUE-PROG1 keeps them
578 ;; live till the body has finished. *whew*
579 ,@(mapcar (lambda (pin)
580 `(touch-object ,pin))