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 ;;;; macros to generate useful values
67 (defmacro load-symbol (reg symbol)
68 `(inst mov ,reg (+ nil-value (static-symbol-offset ,symbol))))
70 (defmacro make-ea-for-symbol-value (symbol)
73 (static-symbol-offset ',symbol)
74 (ash symbol-value-slot word-shift)
75 (- other-pointer-lowtag))))
77 (defmacro load-symbol-value (reg symbol)
78 `(inst mov ,reg (make-ea-for-symbol-value ,symbol)))
80 (defmacro store-symbol-value (reg symbol)
81 `(inst mov (make-ea-for-symbol-value ,symbol) ,reg))
84 (defmacro make-ea-for-symbol-tls-index (symbol)
87 (static-symbol-offset ',symbol)
88 (ash symbol-tls-index-slot word-shift)
89 (- other-pointer-lowtag))))
92 (defmacro load-tl-symbol-value (reg symbol)
94 (inst mov ,reg (make-ea-for-symbol-tls-index ,symbol))
95 (inst mov ,reg (make-ea :qword :base thread-base-tn :scale 1 :index ,reg))))
97 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
100 (defmacro store-tl-symbol-value (reg symbol temp)
102 (inst mov ,temp (make-ea-for-symbol-tls-index ,symbol))
103 (inst mov (make-ea :qword :base thread-base-tn :scale 1 :index ,temp) ,reg)))
105 (defmacro store-tl-symbol-value (reg symbol temp)
106 (declare (ignore temp))
107 `(store-symbol-value ,reg ,symbol))
109 (defmacro load-binding-stack-pointer (reg)
111 `(inst mov ,reg (make-ea :qword :base thread-base-tn
112 :disp (* n-word-bytes thread-binding-stack-pointer-slot)))
114 `(load-symbol-value ,reg *binding-stack-pointer*))
116 (defmacro store-binding-stack-pointer (reg)
118 `(inst mov (make-ea :qword :base thread-base-tn
119 :disp (* n-word-bytes thread-binding-stack-pointer-slot))
122 `(store-symbol-value ,reg *binding-stack-pointer*))
124 (defmacro load-type (target source &optional (offset 0))
126 "Loads the type bits of a pointer into target independent of
127 byte-ordering issues."
128 (once-only ((n-target target)
131 (ecase *backend-byte-order*
133 `(inst movzx ,n-target
134 (make-ea :byte :base ,n-source :disp ,n-offset)))
136 `(inst movzx ,n-target
137 (make-ea :byte :base ,n-source
138 :disp (+ ,n-offset (1- n-word-bytes))))))))
140 ;;;; allocation helpers
142 ;;; All allocation is done by calls to assembler routines that
143 ;;; eventually invoke the C alloc() function.
145 ;;; Emit code to allocate an object with a size in bytes given by
146 ;;; Size. The size may be an integer of a TN. If Inline is a VOP
147 ;;; node-var then it is used to make an appropriate speed vs size
150 (defun allocation-dynamic-extent (alloc-tn size lowtag)
151 (inst sub rsp-tn size)
152 ;; see comment in x86/macros.lisp implementation of this
153 (inst and rsp-tn #.(lognot lowtag-mask))
154 (aver (not (location= alloc-tn rsp-tn)))
155 (inst lea alloc-tn (make-ea :byte :base rsp-tn :disp lowtag))
158 ;;; This macro should only be used inside a pseudo-atomic section,
159 ;;; which should also cover subsequent initialization of the
161 (defun allocation-tramp (alloc-tn size lowtag)
163 (inst lea temp-reg-tn (make-ea :qword
164 :disp (make-fixup "alloc_tramp" :foreign)))
165 (inst call temp-reg-tn)
168 (inst lea alloc-tn (make-ea :byte :base alloc-tn :disp lowtag)))
171 (defun allocation (alloc-tn size &optional ignored dynamic-extent lowtag)
172 (declare (ignore ignored))
174 (allocation-dynamic-extent alloc-tn size lowtag)
175 (return-from allocation (values)))
176 (let ((NOT-INLINE (gen-label))
179 (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
180 ;; thread->alloc_region.free_pointer
184 :base thread-base-tn :scale 1
185 :disp (* n-word-bytes thread-alloc-region-slot))
189 (make-fixup "boxed_region" :foreign)))
190 ;; thread->alloc_region.end_addr
194 :base thread-base-tn :scale 1
195 :disp (* n-word-bytes (1+ thread-alloc-region-slot)))
199 (make-fixup "boxed_region" :foreign 8))))
201 (allocation-tramp alloc-tn size lowtag))
203 (inst mov temp-reg-tn free-pointer)
205 (if (location= alloc-tn size)
206 (inst add alloc-tn temp-reg-tn)
208 (make-ea :qword :base temp-reg-tn :index size)))
210 (make-ea :qword :base temp-reg-tn :disp size)))
211 (inst cmp end-addr alloc-tn)
212 (inst jmp :be NOT-INLINE)
213 (inst mov free-pointer alloc-tn)
215 (inst lea alloc-tn (make-ea :byte :base temp-reg-tn :disp lowtag))
216 (inst mov alloc-tn temp-reg-tn))
218 (assemble (*elsewhere*)
219 (emit-label NOT-INLINE)
220 (cond ((numberp size)
221 (allocation-tramp alloc-tn size lowtag))
223 (inst sub alloc-tn free-pointer)
224 (allocation-tramp alloc-tn alloc-tn lowtag)))
228 ;;; Allocate an other-pointer object of fixed SIZE with a single word
229 ;;; header having the specified WIDETAG value. The result is placed in
231 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline stack-allocate-p)
234 (bug "empty &body in WITH-FIXED-ALLOCATION"))
235 (once-only ((result-tn result-tn) (size size) (stack-allocate-p stack-allocate-p))
236 `(maybe-pseudo-atomic ,stack-allocate-p
237 (allocation ,result-tn (pad-data-block ,size) ,inline ,stack-allocate-p
238 other-pointer-lowtag)
239 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
240 ,result-tn 0 other-pointer-lowtag)
244 (defun emit-error-break (vop kind code values)
247 (inst int 3) ; i386 breakpoint instruction
248 ;; On Darwin, we need to use #x0b0f instead of int3 in order
249 ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
250 ;; doesn't seem to be reliably firing SIGTRAP
251 ;; handlers. Hopefully this will be fixed by Apple at a
252 ;; later date. Do the same on x86-64 as we do on x86 until this gets
256 ;; The return PC points here; note the location for the debugger.
258 (note-this-location vop :internal-error))
259 (inst byte kind) ; eg trap_Xyyy
260 (with-adjustable-vector (vector) ; interr arguments
261 (write-var-integer code vector)
263 ;; classic CMU CL comment:
264 ;; zzzzz jrd here. tn-offset is zero for constant
266 (write-var-integer (make-sc-offset (sc-number (tn-sc tn))
267 (or (tn-offset tn) 0))
269 (inst byte (length vector))
270 (dotimes (i (length vector))
271 (inst byte (aref vector i))))))
273 (defun error-call (vop error-code &rest values)
275 "Cause an error. ERROR-CODE is the error to cause."
276 (emit-error-break vop error-trap (error-number-or-lose error-code) values))
278 (defun generate-error-code (vop error-code &rest values)
280 "Generate-Error-Code Error-code Value*
281 Emit code for an error with the specified Error-Code and context Values."
282 (assemble (*elsewhere*)
283 (let ((start-lab (gen-label)))
284 (emit-label start-lab)
285 (emit-error-break vop error-trap (error-number-or-lose error-code) values)
291 ;;; This is used to wrap operations which leave untagged memory lying
292 ;;; around. It's an operation which the AOP weenies would describe as
293 ;;; having "cross-cutting concerns", meaning it appears all over the
294 ;;; place and there's no logical single place to attach documentation.
295 ;;; grep (mostly in src/runtime) is your friend
297 (defmacro maybe-pseudo-atomic (not-really-p &body body)
300 (pseudo-atomic ,@body)))
302 ;;; Unsafely clear pa flags so that the image can properly lose in a
305 (defmacro %clear-pseudo-atomic ()
306 '(inst mov (make-ea :qword :base thread-base-tn
307 :disp (* n-word-bytes thread-pseudo-atomic-bits-slot))
311 (defun emit-safepoint ()
312 (inst test al-tn (make-ea :byte :disp sb!vm::gc-safepoint-page-addr)))
315 (defmacro pseudo-atomic (&rest forms)
316 #!+sb-safepoint-strictly
317 `(progn ,@forms (emit-safepoint))
318 #!-sb-safepoint-strictly
319 (with-unique-names (label)
320 `(let ((,label (gen-label)))
321 (inst mov (make-ea :qword
323 :disp (* n-word-bytes thread-pseudo-atomic-bits-slot))
326 (inst xor (make-ea :qword
328 :disp (* n-word-bytes thread-pseudo-atomic-bits-slot))
331 ;; if PAI was set, interrupts were disabled at the same time
332 ;; using the process signal mask.
333 (inst break pending-interrupt-trap)
336 ;; In this case, when allocation thinks a GC should be done, it
337 ;; does not mark PA as interrupted, but schedules a safepoint
338 ;; trap instead. Let's take the opportunity to trigger that
339 ;; safepoint right now.
344 (defmacro pseudo-atomic (&rest forms)
345 (with-unique-names (label)
346 `(let ((,label (gen-label)))
347 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
348 ;; something. (perhaps SVLB, for static variable low byte)
349 (inst mov (make-ea :qword :disp (+ nil-value
350 (static-symbol-offset
351 '*pseudo-atomic-bits*)
352 (ash symbol-value-slot word-shift)
353 (- other-pointer-lowtag)))
356 (inst xor (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 ;; if PAI was set, interrupts were disabled at the same time
364 ;; using the process signal mask.
365 (inst break pending-interrupt-trap)
366 (emit-label ,label))))
368 ;;;; indexed references
370 (defmacro define-full-compare-and-swap
371 (name type offset lowtag scs el-type &optional translate)
374 ,@(when translate `((:translate ,translate)))
376 (:args (object :scs (descriptor-reg) :to :eval)
377 (index :scs (any-reg) :to :result)
378 (old-value :scs ,scs :target rax)
379 (new-value :scs ,scs))
380 (:arg-types ,type tagged-num ,el-type ,el-type)
381 (:temporary (:sc descriptor-reg :offset rax-offset
382 :from (:argument 2) :to :result :target value) rax)
383 (:results (value :scs ,scs))
384 (:result-types ,el-type)
387 (inst cmpxchg (make-ea :qword :base object :index index
388 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
389 :disp (- (* ,offset n-word-bytes) ,lowtag))
393 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
397 `((:translate ,translate)))
399 (:args (object :scs (descriptor-reg))
400 (index :scs (any-reg)))
401 (:arg-types ,type tagged-num)
402 (:results (value :scs ,scs))
403 (:result-types ,el-type)
404 (:generator 3 ; pw was 5
405 (inst mov value (make-ea :qword :base object :index index
406 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
407 :disp (- (* ,offset n-word-bytes)
409 (define-vop (,(symbolicate name "-C"))
411 `((:translate ,translate)))
413 (:args (object :scs (descriptor-reg)))
416 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
418 (:results (value :scs ,scs))
419 (:result-types ,el-type)
420 (:generator 2 ; pw was 5
421 (inst mov value (make-ea :qword :base object
422 :disp (- (* (+ ,offset index) n-word-bytes)
425 (defmacro define-full-reffer+offset (name type offset lowtag scs el-type &optional translate)
429 `((:translate ,translate)))
431 (:args (object :scs (descriptor-reg))
432 (index :scs (any-reg)))
434 (:arg-types ,type tagged-num
435 (:constant (constant-displacement other-pointer-lowtag
436 n-word-bytes vector-data-offset)))
437 (:results (value :scs ,scs))
438 (:result-types ,el-type)
439 (:generator 3 ; pw was 5
440 (inst mov value (make-ea :qword :base object :index index
441 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
442 :disp (- (* (+ ,offset offset) n-word-bytes)
444 (define-vop (,(symbolicate name "-C"))
446 `((:translate ,translate)))
448 (:args (object :scs (descriptor-reg)))
451 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
453 (:constant (constant-displacement other-pointer-lowtag
454 n-word-bytes vector-data-offset)))
455 (:results (value :scs ,scs))
456 (:result-types ,el-type)
457 (:generator 2 ; pw was 5
458 (inst mov value (make-ea :qword :base object
459 :disp (- (* (+ ,offset index offset) n-word-bytes)
462 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
466 `((:translate ,translate)))
468 (:args (object :scs (descriptor-reg))
469 (index :scs (any-reg))
470 (value :scs ,scs :target result))
471 (:arg-types ,type tagged-num ,el-type)
472 (:results (result :scs ,scs))
473 (:result-types ,el-type)
474 (:generator 4 ; was 5
475 (inst mov (make-ea :qword :base object :index index
476 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
477 :disp (- (* ,offset n-word-bytes) ,lowtag))
479 (move result value)))
480 (define-vop (,(symbolicate name "-C"))
482 `((:translate ,translate)))
484 (:args (object :scs (descriptor-reg))
485 (value :scs ,scs :target result))
488 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
491 (:results (result :scs ,scs))
492 (:result-types ,el-type)
493 (:generator 3 ; was 5
494 (inst mov (make-ea :qword :base object
495 :disp (- (* (+ ,offset index) n-word-bytes)
498 (move result value)))))
500 (defmacro define-full-setter+offset (name type offset lowtag scs el-type &optional translate)
504 `((:translate ,translate)))
506 (:args (object :scs (descriptor-reg))
507 (index :scs (any-reg))
508 (value :scs ,scs :target result))
510 (:arg-types ,type tagged-num
511 (:constant (constant-displacement other-pointer-lowtag
515 (:results (result :scs ,scs))
516 (:result-types ,el-type)
517 (:generator 4 ; was 5
518 (inst mov (make-ea :qword :base object :index index
519 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
520 :disp (- (* (+ ,offset offset) n-word-bytes) ,lowtag))
522 (move result value)))
523 (define-vop (,(symbolicate name "-C"))
525 `((:translate ,translate)))
527 (:args (object :scs (descriptor-reg))
528 (value :scs ,scs :target result))
531 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
533 (:constant (constant-displacement other-pointer-lowtag
537 (:results (result :scs ,scs))
538 (:result-types ,el-type)
539 (:generator 3 ; was 5
540 (inst mov (make-ea :qword :base object
541 :disp (- (* (+ ,offset index offset) n-word-bytes)
544 (move result value)))))
546 ;;; helper for alien stuff.
548 (def!macro with-pinned-objects ((&rest objects) &body body)
549 "Arrange with the garbage collector that the pages occupied by
550 OBJECTS will not be moved in memory for the duration of BODY.
551 Useful for e.g. foreign calls where another thread may trigger
554 (let ((pins (make-gensym-list (length objects)))
555 (wpo (sb!xc:gensym "WITH-PINNED-OBJECTS-THUNK")))
556 ;; BODY is stuffed in a function to preserve the lexical
558 `(flet ((,wpo () (progn ,@body)))
559 (declare (muffle-conditions compiler-note))
560 ;; PINS are dx-allocated in case the compiler for some
561 ;; unfathomable reason decides to allocate value-cells
562 ;; for them -- since we have DX value-cells on x86oid
563 ;; platforms this still forces them on the stack.
564 (dx-let ,(mapcar #'list pins objects)
565 (multiple-value-prog1 (,wpo)
566 ;; TOUCH-OBJECT has a VOP with an empty body: compiler
567 ;; thinks we're using the argument and doesn't flush
568 ;; the variable, but we don't have to pay any extra
569 ;; beyond that -- and MULTIPLE-VALUE-PROG1 keeps them
570 ;; live till the body has finished. *whew*
571 ,@(mapcar (lambda (pin)
572 `(touch-object ,pin))