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 (defmacro storew
(value ptr
&optional
(slot 0) (lowtag 0))
53 (once-only ((value value
))
54 `(cond ((and (integerp ,value
)
55 (not (typep ,value
'(signed-byte 32))))
56 (inst mov temp-reg-tn
,value
)
57 (inst mov
(make-ea-for-object-slot ,ptr
,slot
,lowtag
) temp-reg-tn
))
59 (inst mov
(make-ea-for-object-slot ,ptr
,slot
,lowtag
) ,value
)))))
61 (defmacro pushw
(ptr &optional
(slot 0) (lowtag 0))
62 `(inst push
(make-ea-for-object-slot ,ptr
,slot
,lowtag
)))
64 (defmacro popw
(ptr &optional
(slot 0) (lowtag 0))
65 `(inst pop
(make-ea-for-object-slot ,ptr
,slot
,lowtag
)))
67 (defun call-indirect (offset)
70 (inst call
(make-ea :qword
:disp offset
)))
72 (inst mov temp-reg-tn offset
)
73 (inst call
(make-ea :qword
:base temp-reg-tn
)))))
75 ;;;; macros to generate useful values
77 (defmacro load-symbol
(reg symbol
)
78 `(inst mov
,reg
(+ nil-value
(static-symbol-offset ,symbol
))))
80 (defmacro make-ea-for-symbol-value
(symbol)
83 (static-symbol-offset ',symbol
)
84 (ash symbol-value-slot word-shift
)
85 (- other-pointer-lowtag
))))
87 (defmacro load-symbol-value
(reg symbol
)
88 `(inst mov
,reg
(make-ea-for-symbol-value ,symbol
)))
90 (defmacro store-symbol-value
(reg symbol
)
91 `(inst mov
(make-ea-for-symbol-value ,symbol
) ,reg
))
93 ;; Return the effective address of the value slot of static SYMBOL.
94 (defun static-symbol-value-ea (symbol)
97 (static-symbol-offset symbol
)
98 (ash symbol-value-slot word-shift
)
99 (- other-pointer-lowtag
))))
103 ;; Return an EA for the TLS of SYMBOL, or die.
104 (defun symbol-known-tls-cell (symbol)
105 (let ((index (info :variable
:wired-tls symbol
)))
106 (aver (integerp index
))
107 (make-ea :qword
:base thread-base-tn
:disp index
)))
109 ;; LOAD/STORE-TL-SYMBOL-VALUE macros are ad-hoc (ugly) emulations
110 ;; of (INFO :VARIABLE :WIRED-TLS) = :ALWAYS-THREAD-LOCAL
111 (defmacro load-tl-symbol-value
(reg symbol
)
112 `(inst mov
,reg
(symbol-known-tls-cell ',symbol
)))
114 (defmacro store-tl-symbol-value
(reg symbol
)
115 `(inst mov
(symbol-known-tls-cell ',symbol
) ,reg
)))
119 (defmacro load-tl-symbol-value
(reg symbol
)
120 `(load-symbol-value ,reg
,symbol
))
121 (defmacro store-tl-symbol-value
(reg symbol
)
122 `(store-symbol-value ,reg
,symbol
)))
124 (defmacro load-binding-stack-pointer
(reg)
125 #!+sb-thread
`(inst mov
,reg
(symbol-known-tls-cell '*binding-stack-pointer
*))
126 #!-sb-thread
`(load-symbol-value ,reg
*binding-stack-pointer
*))
128 (defmacro store-binding-stack-pointer
(reg)
129 #!+sb-thread
`(inst mov
(symbol-known-tls-cell '*binding-stack-pointer
*) ,reg
)
130 #!-sb-thread
`(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 ;; However that comment seems inapplicable here because:
162 ;; - PAD-DATA-BLOCK quite clearly enforces double-word alignment,
163 ;; contradicting "... unfortunately not enforced by ..."
164 ;; - It's not the job of WITH-FIXED-ALLOCATION to realign anything.
165 ;; - The real issue is that it's not obvious that the stack is
166 ;; 16-byte-aligned at *all* times. Maybe it is, maybe it isn't.
167 (inst and rsp-tn
#.
(lognot lowtag-mask
))
168 (aver (not (location= alloc-tn rsp-tn
)))
169 (inst lea alloc-tn
(make-ea :byte
:base rsp-tn
:disp lowtag
))
172 ;;; This macro should only be used inside a pseudo-atomic section,
173 ;;; which should also cover subsequent initialization of the
175 (defun allocation-tramp (alloc-tn size lowtag
)
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 alloc-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 ;; Kludge: this is supposed to be
223 ;; (>= size (extern-alien "large_object_size" long))
224 ;; but that won't cross-compile. So, a little OAOOM...
225 (>= size
(* 4 (max *backend-page-bytes
* gencgc-card-bytes
226 gencgc-alloc-granularity
)))))
227 (allocation-tramp alloc-tn size lowtag
))
229 (inst mov temp-reg-tn free-pointer
)
231 (if (location= alloc-tn size
)
232 (inst add alloc-tn temp-reg-tn
)
234 (make-ea :qword
:base temp-reg-tn
:index size
))))
235 ((typep size
'(signed-byte 31))
237 (make-ea :qword
:base temp-reg-tn
:disp size
)))
238 (t ; a doozy - 'disp' in an EA is too small for this size
239 (inst mov alloc-tn temp-reg-tn
)
240 (inst add alloc-tn
(constantize size
))))
241 (inst cmp alloc-tn end-addr
)
242 (inst jmp
:a NOT-INLINE
)
243 (inst mov free-pointer alloc-tn
)
245 (inst lea alloc-tn
(make-ea :byte
:base temp-reg-tn
:disp lowtag
))
246 (inst mov alloc-tn temp-reg-tn
))
248 (assemble (*elsewhere
*)
249 (emit-label NOT-INLINE
)
250 (cond ((numberp size
)
251 (allocation-tramp alloc-tn size lowtag
))
253 (inst sub alloc-tn free-pointer
)
254 (allocation-tramp alloc-tn alloc-tn lowtag
)))
258 ;;; Allocate an other-pointer object of fixed SIZE with a single word
259 ;;; header having the specified WIDETAG value. The result is placed in
261 (defmacro with-fixed-allocation
((result-tn widetag size
&optional inline stack-allocate-p
)
264 (bug "empty &body in WITH-FIXED-ALLOCATION"))
265 (once-only ((result-tn result-tn
) (size size
) (stack-allocate-p stack-allocate-p
))
266 `(maybe-pseudo-atomic ,stack-allocate-p
267 (allocation ,result-tn
(pad-data-block ,size
) ,inline
,stack-allocate-p
268 other-pointer-lowtag
)
269 (storew (logior (ash (1- ,size
) n-widetag-bits
) ,widetag
)
270 ,result-tn
0 other-pointer-lowtag
)
274 (defun emit-error-break (vop kind code values
)
277 (inst int
3) ; i386 breakpoint instruction
278 ;; On Darwin, we need to use #x0b0f instead of int3 in order
279 ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
280 ;; doesn't seem to be reliably firing SIGTRAP
281 ;; handlers. Hopefully this will be fixed by Apple at a
282 ;; later date. Do the same on x86-64 as we do on x86 until this gets
286 ;; The return PC points here; note the location for the debugger.
288 (note-this-location vop
:internal-error
))
289 (inst byte kind
) ; eg trap_Xyyy
291 (#.invalid-arg-count-trap
) ; there is no "payload" in this trap kind
293 (with-adjustable-vector (vector) ; interr arguments
294 (write-var-integer code vector
)
296 ;; classic CMU CL comment:
297 ;; zzzzz jrd here. tn-offset is zero for constant
299 (write-var-integer (make-sc-offset (sc-number (tn-sc tn
))
300 (or (tn-offset tn
) 0))
302 (inst byte
(length vector
))
303 (dotimes (i (length vector
))
304 (inst byte
(aref vector i
))))))))
306 (defun error-call (vop error-code
&rest values
)
308 "Cause an error. ERROR-CODE is the error to cause."
309 (emit-error-break vop error-trap
(error-number-or-lose error-code
) values
))
311 (defun generate-error-code (vop error-code
&rest values
)
313 "Generate-Error-Code Error-code Value*
314 Emit code for an error with the specified Error-Code and context Values."
315 (assemble (*elsewhere
*)
316 (let ((start-lab (gen-label)))
317 (emit-label start-lab
)
318 (emit-error-break vop
319 (case error-code
; should be named ERROR-SYMBOL really
320 (invalid-arg-count-error invalid-arg-count-trap
)
322 (error-number-or-lose error-code
)
329 ;;; This is used to wrap operations which leave untagged memory lying
330 ;;; around. It's an operation which the AOP weenies would describe as
331 ;;; having "cross-cutting concerns", meaning it appears all over the
332 ;;; place and there's no logical single place to attach documentation.
333 ;;; grep (mostly in src/runtime) is your friend
335 (defmacro maybe-pseudo-atomic
(not-really-p &body body
)
338 (pseudo-atomic ,@body
)))
340 ;;; Unsafely clear pa flags so that the image can properly lose in a
343 (defmacro %clear-pseudo-atomic
()
344 '(inst mov
(make-ea :qword
:base thread-base-tn
345 :disp
(* n-word-bytes thread-pseudo-atomic-bits-slot
))
349 (defun emit-safepoint ()
350 (inst test al-tn
(make-ea :byte
:disp gc-safepoint-page-addr
)))
353 (defmacro pseudo-atomic
(&rest forms
)
354 #!+sb-safepoint-strictly
355 `(progn ,@forms
(emit-safepoint))
356 #!-sb-safepoint-strictly
357 (with-unique-names (label)
358 `(let ((,label
(gen-label)))
359 (inst mov
(make-ea :qword
361 :disp
(* n-word-bytes thread-pseudo-atomic-bits-slot
))
364 (inst xor
(make-ea :qword
366 :disp
(* n-word-bytes thread-pseudo-atomic-bits-slot
))
369 ;; if PAI was set, interrupts were disabled at the same time
370 ;; using the process signal mask.
371 (inst break pending-interrupt-trap
)
374 ;; In this case, when allocation thinks a GC should be done, it
375 ;; does not mark PA as interrupted, but schedules a safepoint
376 ;; trap instead. Let's take the opportunity to trigger that
377 ;; safepoint right now.
382 (defmacro pseudo-atomic
(&rest forms
)
383 (with-unique-names (label)
384 `(let ((,label
(gen-label)))
385 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
386 ;; something. (perhaps SVLB, for static variable low byte)
387 (inst mov
(make-ea :qword
:disp
(+ nil-value
388 (static-symbol-offset
389 '*pseudo-atomic-bits
*)
390 (ash symbol-value-slot word-shift
)
391 (- other-pointer-lowtag
)))
394 (inst xor
(make-ea :qword
:disp
(+ nil-value
395 (static-symbol-offset
396 '*pseudo-atomic-bits
*)
397 (ash symbol-value-slot word-shift
)
398 (- other-pointer-lowtag
)))
401 ;; if PAI was set, interrupts were disabled at the same time
402 ;; using the process signal mask.
403 (inst break pending-interrupt-trap
)
404 (emit-label ,label
))))
406 ;;;; indexed references
408 (defmacro define-full-compare-and-swap
409 (name type offset lowtag scs el-type
&optional translate
)
412 ,@(when translate
`((:translate
,translate
)))
414 (:args
(object :scs
(descriptor-reg) :to
:eval
)
415 (index :scs
(any-reg) :to
:result
)
416 (old-value :scs
,scs
:target rax
)
417 (new-value :scs
,scs
))
418 (:arg-types
,type tagged-num
,el-type
,el-type
)
419 (:temporary
(:sc descriptor-reg
:offset rax-offset
420 :from
(:argument
2) :to
:result
:target value
) rax
)
421 (:results
(value :scs
,scs
))
422 (:result-types
,el-type
)
425 (inst cmpxchg
(make-ea :qword
:base object
:index index
426 :scale
(ash 1 (- word-shift n-fixnum-tag-bits
))
427 :disp
(- (* ,offset n-word-bytes
) ,lowtag
))
431 (defmacro define-full-reffer
(name type offset lowtag scs el-type
&optional translate
)
435 `((:translate
,translate
)))
437 (:args
(object :scs
(descriptor-reg))
438 (index :scs
(any-reg)))
439 (:arg-types
,type tagged-num
)
440 (:results
(value :scs
,scs
))
441 (:result-types
,el-type
)
442 (:generator
3 ; pw was 5
443 (inst mov value
(make-ea :qword
:base object
:index index
444 :scale
(ash 1 (- word-shift n-fixnum-tag-bits
))
445 :disp
(- (* ,offset n-word-bytes
)
447 (define-vop (,(symbolicate name
"-C"))
449 `((:translate
,translate
)))
451 (:args
(object :scs
(descriptor-reg)))
454 (:constant
(load/store-index
,n-word-bytes
,(eval lowtag
)
456 (:results
(value :scs
,scs
))
457 (:result-types
,el-type
)
458 (:generator
2 ; pw was 5
459 (inst mov value
(make-ea :qword
:base object
460 :disp
(- (* (+ ,offset index
) n-word-bytes
)
463 (defmacro define-full-reffer
+offset
(name type offset lowtag scs el-type
&optional translate
)
467 `((:translate
,translate
)))
469 (:args
(object :scs
(descriptor-reg))
470 (index :scs
(any-reg)))
472 (:arg-types
,type tagged-num
473 (:constant
(constant-displacement other-pointer-lowtag
474 n-word-bytes vector-data-offset
)))
475 (:results
(value :scs
,scs
))
476 (:result-types
,el-type
)
477 (:generator
3 ; pw was 5
478 (inst mov value
(make-ea :qword
:base object
:index index
479 :scale
(ash 1 (- word-shift n-fixnum-tag-bits
))
480 :disp
(- (* (+ ,offset offset
) n-word-bytes
)
482 (define-vop (,(symbolicate name
"-C"))
484 `((:translate
,translate
)))
486 (:args
(object :scs
(descriptor-reg)))
489 (:constant
(load/store-index
,n-word-bytes
,(eval lowtag
)
491 (:constant
(constant-displacement other-pointer-lowtag
492 n-word-bytes vector-data-offset
)))
493 (:results
(value :scs
,scs
))
494 (:result-types
,el-type
)
495 (:generator
2 ; pw was 5
496 (inst mov value
(make-ea :qword
:base object
497 :disp
(- (* (+ ,offset index offset
) n-word-bytes
)
500 (defmacro define-full-setter
(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
))
509 (:arg-types
,type tagged-num
,el-type
)
510 (:results
(result :scs
,scs
))
511 (:result-types
,el-type
)
512 (:generator
4 ; was 5
513 (inst mov
(make-ea :qword
:base object
:index index
514 :scale
(ash 1 (- word-shift n-fixnum-tag-bits
))
515 :disp
(- (* ,offset n-word-bytes
) ,lowtag
))
517 (move result value
)))
518 (define-vop (,(symbolicate name
"-C"))
520 `((:translate
,translate
)))
522 (:args
(object :scs
(descriptor-reg))
523 (value :scs
,scs
:target result
))
526 (:constant
(load/store-index
,n-word-bytes
,(eval lowtag
)
529 (:results
(result :scs
,scs
))
530 (:result-types
,el-type
)
531 (:generator
3 ; was 5
532 (inst mov
(make-ea :qword
:base object
533 :disp
(- (* (+ ,offset index
) n-word-bytes
)
536 (move result value
)))))
538 (defmacro define-full-setter
+offset
(name type offset lowtag scs el-type
&optional translate
)
542 `((:translate
,translate
)))
544 (:args
(object :scs
(descriptor-reg))
545 (index :scs
(any-reg))
546 (value :scs
,scs
:target result
))
548 (:arg-types
,type tagged-num
549 (:constant
(constant-displacement other-pointer-lowtag
553 (:results
(result :scs
,scs
))
554 (:result-types
,el-type
)
555 (:generator
4 ; was 5
556 (inst mov
(make-ea :qword
:base object
:index index
557 :scale
(ash 1 (- word-shift n-fixnum-tag-bits
))
558 :disp
(- (* (+ ,offset offset
) n-word-bytes
) ,lowtag
))
560 (move result value
)))
561 (define-vop (,(symbolicate name
"-C"))
563 `((:translate
,translate
)))
565 (:args
(object :scs
(descriptor-reg))
566 (value :scs
,scs
:target result
))
569 (:constant
(load/store-index
,n-word-bytes
,(eval lowtag
)
571 (:constant
(constant-displacement other-pointer-lowtag
575 (:results
(result :scs
,scs
))
576 (:result-types
,el-type
)
577 (:generator
3 ; was 5
578 (inst mov
(make-ea :qword
:base object
579 :disp
(- (* (+ ,offset index offset
) n-word-bytes
)
582 (move result value
)))))
584 ;;; helper for alien stuff.
586 (def!macro with-pinned-objects
((&rest objects
) &body body
)
588 "Arrange with the garbage collector that the pages occupied by
589 OBJECTS will not be moved in memory for the duration of BODY.
590 Useful for e.g. foreign calls where another thread may trigger
593 (let ((pins (make-gensym-list (length objects
)))
594 (wpo (sb!xc
:gensym
"WITH-PINNED-OBJECTS-THUNK")))
595 ;; BODY is stuffed in a function to preserve the lexical
597 `(flet ((,wpo
() (progn ,@body
)))
598 (declare (muffle-conditions compiler-note
))
599 ;; PINS are dx-allocated in case the compiler for some
600 ;; unfathomable reason decides to allocate value-cells
601 ;; for them -- since we have DX value-cells on x86oid
602 ;; platforms this still forces them on the stack.
603 (dx-let ,(mapcar #'list pins objects
)
604 (multiple-value-prog1 (,wpo
)
605 ;; TOUCH-OBJECT has a VOP with an empty body: compiler
606 ;; thinks we're using the argument and doesn't flush
607 ;; the variable, but we don't have to pay any extra
608 ;; beyond that -- and MULTIPLE-VALUE-PROG1 keeps them
609 ;; live till the body has finished. *whew*
610 ,@(mapcar (lambda (pin)
611 `(touch-object ,pin
))