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
)))
46 (defmacro tls-index-of
(sym)
47 `(make-ea :dword
:base
,sym
:disp
(+ 4 (- other-pointer-lowtag
))))
49 (defmacro loadw
(value ptr
&optional
(slot 0) (lowtag 0))
50 `(inst mov
,value
(make-ea-for-object-slot ,ptr
,slot
,lowtag
)))
52 (defun storew (value ptr
&optional
(slot 0) (lowtag 0))
53 (cond ((and (integerp value
)
54 (not (typep value
'(signed-byte 32))))
55 (inst mov temp-reg-tn value
)
56 (inst mov
(make-ea-for-object-slot ptr slot lowtag
) temp-reg-tn
))
58 (inst mov
(make-ea-for-object-slot ptr slot lowtag
) value
))))
60 (defmacro pushw
(ptr &optional
(slot 0) (lowtag 0))
61 `(inst push
(make-ea-for-object-slot ,ptr
,slot
,lowtag
)))
63 (defmacro popw
(ptr &optional
(slot 0) (lowtag 0))
64 `(inst pop
(make-ea-for-object-slot ,ptr
,slot
,lowtag
)))
66 (defun call-indirect (offset)
69 (inst call
(make-ea :qword
:disp offset
)))
71 (inst mov temp-reg-tn offset
)
72 (inst call
(make-ea :qword
:base temp-reg-tn
)))))
74 ;;;; macros to generate useful values
76 (defmacro load-symbol
(reg symbol
)
77 `(inst mov
,reg
(+ nil-value
(static-symbol-offset ,symbol
))))
79 (defmacro make-ea-for-symbol-value
(symbol)
82 (static-symbol-offset ',symbol
)
83 (ash symbol-value-slot word-shift
)
84 (- other-pointer-lowtag
))))
86 (defmacro load-symbol-value
(reg symbol
)
87 `(inst mov
,reg
(make-ea-for-symbol-value ,symbol
)))
89 (defmacro store-symbol-value
(reg symbol
)
90 `(inst mov
(make-ea-for-symbol-value ,symbol
) ,reg
))
92 ;; Return the effective address of the value slot of static SYMBOL.
93 (defun static-symbol-value-ea (symbol)
96 (static-symbol-offset symbol
)
97 (ash symbol-value-slot word-shift
)
98 (- other-pointer-lowtag
))))
102 ;; Return an EA for the TLS of SYMBOL, or die.
103 (defun symbol-known-tls-cell (symbol)
104 (let ((index (info :variable
:wired-tls symbol
)))
105 (aver (integerp index
))
106 (make-ea :qword
:base thread-base-tn
:disp index
)))
108 ;; LOAD/STORE-TL-SYMBOL-VALUE macros are ad-hoc (ugly) emulations
109 ;; of (INFO :VARIABLE :WIRED-TLS) = :ALWAYS-THREAD-LOCAL
110 (defmacro load-tl-symbol-value
(reg symbol
)
111 `(inst mov
,reg
(symbol-known-tls-cell ',symbol
)))
113 (defmacro store-tl-symbol-value
(reg symbol
)
114 `(inst mov
(symbol-known-tls-cell ',symbol
) ,reg
)))
118 (defmacro load-tl-symbol-value
(reg symbol
)
119 `(load-symbol-value ,reg
,symbol
))
120 (defmacro store-tl-symbol-value
(reg symbol
)
121 `(store-symbol-value ,reg
,symbol
)))
123 (defmacro load-binding-stack-pointer
(reg)
124 #!+sb-thread
`(inst mov
,reg
(symbol-known-tls-cell '*binding-stack-pointer
*))
125 #!-sb-thread
`(load-symbol-value ,reg
*binding-stack-pointer
*))
127 (defmacro store-binding-stack-pointer
(reg)
128 #!+sb-thread
`(inst mov
(symbol-known-tls-cell '*binding-stack-pointer
*) ,reg
)
129 #!-sb-thread
`(store-symbol-value ,reg
*binding-stack-pointer
*))
131 (defmacro load-type
(target source
&optional
(offset 0))
133 "Loads the type bits of a pointer into target independent of
134 byte-ordering issues."
135 (once-only ((n-target target
)
138 (ecase *backend-byte-order
*
140 `(inst movzx
,n-target
141 (make-ea :byte
:base
,n-source
:disp
,n-offset
)))
143 `(inst movzx
,n-target
144 (make-ea :byte
:base
,n-source
145 :disp
(+ ,n-offset
(1- n-word-bytes
))))))))
147 ;;;; allocation helpers
149 ;;; All allocation is done by calls to assembler routines that
150 ;;; eventually invoke the C alloc() function.
152 ;;; Emit code to allocate an object with a size in bytes given by
153 ;;; Size. The size may be an integer of a TN. If Inline is a VOP
154 ;;; node-var then it is used to make an appropriate speed vs size
157 (defun allocation-dynamic-extent (alloc-tn size lowtag
)
158 (inst sub rsp-tn size
)
159 ;; see comment in x86/macros.lisp implementation of this
160 ;; However that comment seems inapplicable here because:
161 ;; - PAD-DATA-BLOCK quite clearly enforces double-word alignment,
162 ;; contradicting "... unfortunately not enforced by ..."
163 ;; - It's not the job of WITH-FIXED-ALLOCATION to realign anything.
164 ;; - The real issue is that it's not obvious that the stack is
165 ;; 16-byte-aligned at *all* times. Maybe it is, maybe it isn't.
166 (inst and rsp-tn
#.
(lognot lowtag-mask
))
167 (aver (not (location= alloc-tn rsp-tn
)))
168 (inst lea alloc-tn
(make-ea :byte
:base rsp-tn
:disp lowtag
))
171 ;;; This macro should only be used inside a pseudo-atomic section,
172 ;;; which should also cover subsequent initialization of the
174 (defun allocation-tramp (alloc-tn size lowtag
175 &optional
(result-tn alloc-tn
))
176 (cond ((typep size
'(and integer
(not (signed-byte 32))))
177 ;; MOV accepts large immediate operands, PUSH does not
178 (inst mov alloc-tn size
)
179 (inst push alloc-tn
))
182 (inst mov alloc-tn
(make-fixup "alloc_tramp" :foreign
))
186 (inst or
(reg-in-size result-tn
:byte
) lowtag
))
189 (defun allocation (alloc-tn size
&optional ignored dynamic-extent lowtag
)
190 (declare (ignore ignored
))
192 (allocation-dynamic-extent alloc-tn size lowtag
)
193 (return-from allocation
(values)))
194 (let ((NOT-INLINE (gen-label))
197 (in-elsewhere (eq *elsewhere
* sb
!assem
::**current-segment
**))
198 ;; thread->alloc_region.free_pointer
202 :base thread-base-tn
:scale
1
203 :disp
(* n-word-bytes thread-alloc-region-slot
))
207 (make-fixup "boxed_region" :foreign
)))
208 ;; thread->alloc_region.end_addr
212 :base thread-base-tn
:scale
1
213 :disp
(* n-word-bytes
(1+ thread-alloc-region-slot
)))
217 (make-fixup "boxed_region" :foreign
8))))
218 (cond ((or in-elsewhere
220 ;; large objects will never be made in a per-thread region
222 (>= size large-object-size
)))
223 (allocation-tramp alloc-tn size lowtag
))
225 (inst mov temp-reg-tn free-pointer
)
227 (if (location= alloc-tn size
)
228 (inst add alloc-tn temp-reg-tn
)
230 (make-ea :qword
:base temp-reg-tn
:index size
))))
231 ((typep size
'(signed-byte 31))
233 (make-ea :qword
:base temp-reg-tn
:disp size
)))
234 (t ; a doozy - 'disp' in an EA is too small for this size
235 (inst mov alloc-tn temp-reg-tn
)
236 (inst add alloc-tn
(constantize size
))))
237 (inst cmp alloc-tn end-addr
)
238 (inst jmp
:a NOT-INLINE
)
239 (inst mov free-pointer alloc-tn
)
242 (inst lea alloc-tn
(make-ea :byte
:base temp-reg-tn
:disp lowtag
))
243 (inst mov alloc-tn temp-reg-tn
))
244 (assemble (*elsewhere
*)
245 (emit-label NOT-INLINE
)
246 (cond ((numberp size
)
247 (allocation-tramp alloc-tn size nil temp-reg-tn
))
249 (inst sub alloc-tn free-pointer
)
250 (allocation-tramp alloc-tn alloc-tn nil temp-reg-tn
)))
254 ;;; Allocate an other-pointer object of fixed SIZE with a single word
255 ;;; header having the specified WIDETAG value. The result is placed in
257 (defmacro with-fixed-allocation
((result-tn widetag size
&optional inline stack-allocate-p
)
260 (bug "empty &body in WITH-FIXED-ALLOCATION"))
261 (once-only ((result-tn result-tn
) (size size
) (stack-allocate-p stack-allocate-p
))
262 `(maybe-pseudo-atomic ,stack-allocate-p
263 (allocation ,result-tn
(pad-data-block ,size
) ,inline
,stack-allocate-p
264 other-pointer-lowtag
)
265 (storew (logior (ash (1- ,size
) n-widetag-bits
) ,widetag
)
266 ,result-tn
0 other-pointer-lowtag
)
270 (defun emit-error-break (vop kind code values
)
273 (inst int
3) ; i386 breakpoint instruction
274 ;; On Darwin, we need to use #x0b0f instead of int3 in order
275 ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
276 ;; doesn't seem to be reliably firing SIGTRAP
277 ;; handlers. Hopefully this will be fixed by Apple at a
278 ;; later date. Do the same on x86-64 as we do on x86 until this gets
282 ;; The return PC points here; note the location for the debugger.
284 (note-this-location vop
:internal-error
))
285 (inst byte kind
) ; eg trap_Xyyy
287 (#.invalid-arg-count-trap
) ; there is no "payload" in this trap kind
290 (with-adjustable-vector (vector) ; interr arguments
292 ;; classic CMU CL comment:
293 ;; zzzzz jrd here. tn-offset is zero for constant
296 (make-sc-offset (sc-number (tn-sc tn
)) (or (tn-offset tn
) 0)) vector
))
297 (dotimes (i (length vector
))
298 (inst byte
(aref vector i
))))))))
300 (defun error-call (vop error-code
&rest values
)
302 "Cause an error. ERROR-CODE is the error to cause."
303 (emit-error-break vop error-trap
(error-number-or-lose error-code
) values
))
305 (defun generate-error-code (vop error-code
&rest values
)
307 "Generate-Error-Code Error-code Value*
308 Emit code for an error with the specified Error-Code and context Values."
309 (assemble (*elsewhere
*)
310 (let ((start-lab (gen-label)))
311 (emit-label start-lab
)
312 (emit-error-break vop
313 (case error-code
; should be named ERROR-SYMBOL really
314 (invalid-arg-count-error invalid-arg-count-trap
)
316 (error-number-or-lose error-code
)
323 ;;; This is used to wrap operations which leave untagged memory lying
324 ;;; around. It's an operation which the AOP weenies would describe as
325 ;;; having "cross-cutting concerns", meaning it appears all over the
326 ;;; place and there's no logical single place to attach documentation.
327 ;;; grep (mostly in src/runtime) is your friend
329 (defmacro maybe-pseudo-atomic
(not-really-p &body body
)
332 (pseudo-atomic ,@body
)))
334 ;;; Unsafely clear pa flags so that the image can properly lose in a
337 (defmacro %clear-pseudo-atomic
()
338 '(inst mov
(make-ea :qword
:base thread-base-tn
339 :disp
(* n-word-bytes thread-pseudo-atomic-bits-slot
))
343 (defun emit-safepoint ()
344 (inst test al-tn
(make-ea :byte
:disp gc-safepoint-page-addr
)))
347 (defmacro pseudo-atomic
(&rest forms
)
348 #!+sb-safepoint-strictly
349 `(progn ,@forms
(emit-safepoint))
350 #!-sb-safepoint-strictly
351 (with-unique-names (label)
352 `(let ((,label
(gen-label)))
353 (inst mov
(make-ea :qword
355 :disp
(* n-word-bytes thread-pseudo-atomic-bits-slot
))
358 (inst xor
(make-ea :qword
360 :disp
(* n-word-bytes thread-pseudo-atomic-bits-slot
))
363 ;; if PAI was set, interrupts were disabled at the same time
364 ;; using the process signal mask.
365 (inst break pending-interrupt-trap
)
368 ;; In this case, when allocation thinks a GC should be done, it
369 ;; does not mark PA as interrupted, but schedules a safepoint
370 ;; trap instead. Let's take the opportunity to trigger that
371 ;; safepoint right now.
376 (defmacro pseudo-atomic
(&rest forms
)
377 (with-unique-names (label)
378 `(let ((,label
(gen-label)))
379 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
380 ;; something. (perhaps SVLB, for static variable low byte)
381 (inst mov
(make-ea :qword
:disp
(+ nil-value
382 (static-symbol-offset
383 '*pseudo-atomic-bits
*)
384 (ash symbol-value-slot word-shift
)
385 (- other-pointer-lowtag
)))
388 (inst xor
(make-ea :qword
:disp
(+ nil-value
389 (static-symbol-offset
390 '*pseudo-atomic-bits
*)
391 (ash symbol-value-slot word-shift
)
392 (- other-pointer-lowtag
)))
395 ;; if PAI was set, interrupts were disabled at the same time
396 ;; using the process signal mask.
397 (inst break pending-interrupt-trap
)
398 (emit-label ,label
))))
400 ;;;; indexed references
402 (defmacro define-full-compare-and-swap
403 (name type offset lowtag scs el-type
&optional translate
)
406 ,@(when translate
`((:translate
,translate
)))
408 (:args
(object :scs
(descriptor-reg) :to
:eval
)
409 (index :scs
(any-reg) :to
:result
)
410 (old-value :scs
,scs
:target rax
)
411 (new-value :scs
,scs
))
412 (:arg-types
,type tagged-num
,el-type
,el-type
)
413 (:temporary
(:sc descriptor-reg
:offset rax-offset
414 :from
(:argument
2) :to
:result
:target value
) rax
)
415 (:results
(value :scs
,scs
))
416 (:result-types
,el-type
)
419 (inst cmpxchg
(make-ea :qword
:base object
:index index
420 :scale
(ash 1 (- word-shift n-fixnum-tag-bits
))
421 :disp
(- (* ,offset n-word-bytes
) ,lowtag
))
425 (defmacro define-full-reffer
(name type offset lowtag scs el-type
&optional translate
)
429 `((:translate
,translate
)))
431 (:args
(object :scs
(descriptor-reg))
432 (index :scs
(any-reg)))
433 (:arg-types
,type tagged-num
)
434 (:results
(value :scs
,scs
))
435 (:result-types
,el-type
)
436 (:generator
3 ; pw was 5
437 (inst mov value
(make-ea :qword
:base object
:index index
438 :scale
(ash 1 (- word-shift n-fixnum-tag-bits
))
439 :disp
(- (* ,offset n-word-bytes
)
441 (define-vop (,(symbolicate name
"-C"))
443 `((:translate
,translate
)))
445 (:args
(object :scs
(descriptor-reg)))
448 (:constant
(load/store-index
,n-word-bytes
,(eval lowtag
)
450 (:results
(value :scs
,scs
))
451 (:result-types
,el-type
)
452 (:generator
2 ; pw was 5
453 (inst mov value
(make-ea :qword
:base object
454 :disp
(- (* (+ ,offset index
) n-word-bytes
)
457 (defmacro define-full-reffer
+offset
(name type offset lowtag scs el-type
&optional translate
)
461 `((:translate
,translate
)))
463 (:args
(object :scs
(descriptor-reg))
464 (index :scs
(any-reg)))
466 (:arg-types
,type tagged-num
467 (:constant
(constant-displacement other-pointer-lowtag
468 n-word-bytes vector-data-offset
)))
469 (:results
(value :scs
,scs
))
470 (:result-types
,el-type
)
471 (:generator
3 ; pw was 5
472 (inst mov value
(make-ea :qword
:base object
:index index
473 :scale
(ash 1 (- word-shift n-fixnum-tag-bits
))
474 :disp
(- (* (+ ,offset offset
) n-word-bytes
)
476 (define-vop (,(symbolicate name
"-C"))
478 `((:translate
,translate
)))
480 (:args
(object :scs
(descriptor-reg)))
483 (:constant
(load/store-index
,n-word-bytes
,(eval lowtag
)
485 (:constant
(constant-displacement other-pointer-lowtag
486 n-word-bytes vector-data-offset
)))
487 (:results
(value :scs
,scs
))
488 (:result-types
,el-type
)
489 (:generator
2 ; pw was 5
490 (inst mov value
(make-ea :qword
:base object
491 :disp
(- (* (+ ,offset index offset
) n-word-bytes
)
494 (defmacro define-full-setter
(name type offset lowtag scs el-type
&optional translate
)
498 `((:translate
,translate
)))
500 (:args
(object :scs
(descriptor-reg))
501 (index :scs
(any-reg))
502 (value :scs
,scs
:target result
))
503 (:arg-types
,type tagged-num
,el-type
)
504 (:results
(result :scs
,scs
))
505 (:result-types
,el-type
)
506 (:generator
4 ; was 5
507 (inst mov
(make-ea :qword
:base object
:index index
508 :scale
(ash 1 (- word-shift n-fixnum-tag-bits
))
509 :disp
(- (* ,offset n-word-bytes
) ,lowtag
))
511 (move result value
)))
512 (define-vop (,(symbolicate name
"-C"))
514 `((:translate
,translate
)))
516 (:args
(object :scs
(descriptor-reg))
517 (value :scs
,scs
:target result
))
520 (:constant
(load/store-index
,n-word-bytes
,(eval lowtag
)
523 (:results
(result :scs
,scs
))
524 (:result-types
,el-type
)
525 (:generator
3 ; was 5
526 (inst mov
(make-ea :qword
:base object
527 :disp
(- (* (+ ,offset index
) n-word-bytes
)
530 (move result value
)))))
532 (defmacro define-full-setter
+offset
(name type offset lowtag scs el-type
&optional translate
)
536 `((:translate
,translate
)))
538 (:args
(object :scs
(descriptor-reg))
539 (index :scs
(any-reg))
540 (value :scs
,scs
:target result
))
542 (:arg-types
,type tagged-num
543 (:constant
(constant-displacement other-pointer-lowtag
547 (:results
(result :scs
,scs
))
548 (:result-types
,el-type
)
549 (:generator
4 ; was 5
550 (inst mov
(make-ea :qword
:base object
:index index
551 :scale
(ash 1 (- word-shift n-fixnum-tag-bits
))
552 :disp
(- (* (+ ,offset offset
) n-word-bytes
) ,lowtag
))
554 (move result value
)))
555 (define-vop (,(symbolicate name
"-C"))
557 `((:translate
,translate
)))
559 (:args
(object :scs
(descriptor-reg))
560 (value :scs
,scs
:target result
))
563 (:constant
(load/store-index
,n-word-bytes
,(eval lowtag
)
565 (:constant
(constant-displacement other-pointer-lowtag
569 (:results
(result :scs
,scs
))
570 (:result-types
,el-type
)
571 (:generator
3 ; was 5
572 (inst mov
(make-ea :qword
:base object
573 :disp
(- (* (+ ,offset index offset
) n-word-bytes
)
576 (move result value
)))))
578 ;;; helper for alien stuff.
580 (sb!xc
:defmacro with-pinned-objects
((&rest objects
) &body body
)
582 "Arrange with the garbage collector that the pages occupied by
583 OBJECTS will not be moved in memory for the duration of BODY.
584 Useful for e.g. foreign calls where another thread may trigger
587 (let ((pins (make-gensym-list (length objects
)))
588 (wpo (sb!xc
:gensym
"WITH-PINNED-OBJECTS-THUNK")))
589 ;; BODY is stuffed in a function to preserve the lexical
591 `(flet ((,wpo
() (progn ,@body
)))
592 ;; The cross-compiler prints either "unknown type: COMPILER-NOTE" at
593 ;; each use of W-P-O prior to 'ir1report' being compiled, or else
594 ;; "could not stack allocate". Kill it with fire :-(
595 (declare (muffle-conditions #+sb-xc compiler-note
#-sb-xc t
))
596 ;; PINS are dx-allocated in case the compiler for some
597 ;; unfathomable reason decides to allocate value-cells
598 ;; for them -- since we have DX value-cells on x86oid
599 ;; platforms this still forces them on the stack.
600 (dx-let ,(mapcar #'list pins objects
)
601 (multiple-value-prog1 (,wpo
)
602 ;; TOUCH-OBJECT has a VOP with an empty body: compiler
603 ;; thinks we're using the argument and doesn't flush
604 ;; the variable, but we don't have to pay any extra
605 ;; beyond that -- and MULTIPLE-VALUE-PROG1 keeps them
606 ;; live till the body has finished. *whew*
607 ,@(mapcar (lambda (pin)
608 `(touch-object ,pin
))
612 ;;; Emit the most compact form of the test immediate instruction,
613 ;;; using an 8 bit test when the immediate is only 8 bits and the
614 ;;; value is one of the four low registers (rax, rbx, rcx, rdx) or the
616 (defun emit-optimized-test-inst (x y
)
619 ;; If we knew that the sign bit would not be tested, this could
620 ;; handle (unsigned-byte 8) constants. But since we don't know,
621 ;; we assume that it's not ok to change the test such that the S flag
622 ;; comes out possibly differently.
623 (let ((offset (tn-offset x
)))
624 (cond ((and (sc-is x any-reg descriptor-reg signed-reg unsigned-reg
)
625 (or (= offset rax-offset
) (= offset rbx-offset
)
626 (= offset rcx-offset
) (= offset rdx-offset
)))
627 (inst test
(reg-in-size x
:byte
) y
))
628 ((sc-is x control-stack
)
629 (inst test
(make-ea :byte
:base rbp-tn
630 :disp
(frame-byte-offset offset
))