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 (inst mov dst src)))))
34 (defmacro make-ea-for-object-slot (ptr slot lowtag)
35 `(make-ea :qword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
36 (defmacro make-ea-for-object-slot-half (ptr slot lowtag)
37 `(make-ea :dword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
39 (defmacro loadw (value ptr &optional (slot 0) (lowtag 0))
40 `(inst mov ,value (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
42 (defmacro storew (value ptr &optional (slot 0) (lowtag 0))
43 (once-only ((value value))
44 `(cond ((and (integerp ,value)
45 (not (typep ,value '(signed-byte 32))))
46 (inst mov temp-reg-tn ,value)
47 (inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) temp-reg-tn))
49 (inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) ,value)))))
51 (defmacro pushw (ptr &optional (slot 0) (lowtag 0))
52 `(inst push (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
54 (defmacro popw (ptr &optional (slot 0) (lowtag 0))
55 `(inst pop (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
57 ;;;; macros to generate useful values
59 (defmacro load-symbol (reg symbol)
60 `(inst mov ,reg (+ nil-value (static-symbol-offset ,symbol))))
62 (defmacro make-ea-for-symbol-value (symbol)
65 (static-symbol-offset ',symbol)
66 (ash symbol-value-slot word-shift)
67 (- other-pointer-lowtag))))
69 (defmacro load-symbol-value (reg symbol)
70 `(inst mov ,reg (make-ea-for-symbol-value ,symbol)))
72 (defmacro store-symbol-value (reg symbol)
73 `(inst mov (make-ea-for-symbol-value ,symbol) ,reg))
76 (defmacro make-ea-for-symbol-tls-index (symbol)
79 (static-symbol-offset ',symbol)
80 (ash symbol-tls-index-slot word-shift)
81 (- other-pointer-lowtag))))
84 (defmacro load-tl-symbol-value (reg symbol)
86 (inst mov ,reg (make-ea-for-symbol-tls-index ,symbol))
87 (inst mov ,reg (make-ea :qword :base thread-base-tn :scale 1 :index ,reg))))
89 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
92 (defmacro store-tl-symbol-value (reg symbol temp)
94 (inst mov ,temp (make-ea-for-symbol-tls-index ,symbol))
95 (inst mov (make-ea :qword :base thread-base-tn :scale 1 :index ,temp) ,reg)))
97 (defmacro store-tl-symbol-value (reg symbol temp)
98 (declare (ignore temp))
99 `(store-symbol-value ,reg ,symbol))
101 (defmacro load-binding-stack-pointer (reg)
103 `(inst mov ,reg (make-ea :qword :base thread-base-tn
104 :disp (* n-word-bytes thread-binding-stack-pointer-slot)))
106 `(load-symbol-value ,reg *binding-stack-pointer*))
108 (defmacro store-binding-stack-pointer (reg)
110 `(inst mov (make-ea :qword :base thread-base-tn
111 :disp (* n-word-bytes thread-binding-stack-pointer-slot))
114 `(store-symbol-value ,reg *binding-stack-pointer*))
116 (defmacro load-type (target source &optional (offset 0))
118 "Loads the type bits of a pointer into target independent of
119 byte-ordering issues."
120 (once-only ((n-target target)
123 (ecase *backend-byte-order*
125 `(inst movzx ,n-target
126 (make-ea :byte :base ,n-source :disp ,n-offset)))
128 `(inst movzx ,n-target
129 (make-ea :byte :base ,n-source
130 :disp (+ ,n-offset (1- n-word-bytes))))))))
132 ;;;; allocation helpers
134 ;;; All allocation is done by calls to assembler routines that
135 ;;; eventually invoke the C alloc() function.
137 ;;; Emit code to allocate an object with a size in bytes given by
138 ;;; Size. The size may be an integer of a TN. If Inline is a VOP
139 ;;; node-var then it is used to make an appropriate speed vs size
142 (defun allocation-dynamic-extent (alloc-tn size lowtag)
143 (inst sub rsp-tn size)
144 ;; see comment in x86/macros.lisp implementation of this
145 (inst and rsp-tn #.(lognot lowtag-mask))
146 (aver (not (location= alloc-tn rsp-tn)))
147 (inst lea alloc-tn (make-ea :byte :base rsp-tn :disp lowtag))
150 ;;; This macro should only be used inside a pseudo-atomic section,
151 ;;; which should also cover subsequent initialization of the
153 (defun allocation-tramp (alloc-tn size lowtag)
155 (inst lea temp-reg-tn (make-ea :qword
156 :disp (make-fixup "alloc_tramp" :foreign)))
157 (inst call temp-reg-tn)
160 (inst lea alloc-tn (make-ea :byte :base alloc-tn :disp lowtag)))
163 (defun allocation (alloc-tn size &optional ignored dynamic-extent lowtag)
164 (declare (ignore ignored))
166 (allocation-dynamic-extent alloc-tn size lowtag)
167 (return-from allocation (values)))
168 (let ((NOT-INLINE (gen-label))
171 (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
172 ;; thread->alloc_region.free_pointer
176 :base thread-base-tn :scale 1
177 :disp (* n-word-bytes thread-alloc-region-slot))
181 (make-fixup "boxed_region" :foreign)))
182 ;; thread->alloc_region.end_addr
186 :base thread-base-tn :scale 1
187 :disp (* n-word-bytes (1+ thread-alloc-region-slot)))
191 (make-fixup "boxed_region" :foreign 8))))
193 (allocation-tramp alloc-tn size lowtag))
195 (inst mov temp-reg-tn free-pointer)
197 (if (location= alloc-tn size)
198 (inst add alloc-tn temp-reg-tn)
200 (make-ea :qword :base temp-reg-tn :index size)))
202 (make-ea :qword :base temp-reg-tn :disp size)))
203 (inst cmp end-addr alloc-tn)
204 (inst jmp :be NOT-INLINE)
205 (inst mov free-pointer alloc-tn)
207 (inst lea alloc-tn (make-ea :byte :base temp-reg-tn :disp lowtag))
208 (inst mov alloc-tn temp-reg-tn))
210 (assemble (*elsewhere*)
211 (emit-label NOT-INLINE)
212 (cond ((numberp size)
213 (allocation-tramp alloc-tn size lowtag))
215 (inst sub alloc-tn free-pointer)
216 (allocation-tramp alloc-tn alloc-tn lowtag)))
220 ;;; Allocate an other-pointer object of fixed SIZE with a single word
221 ;;; header having the specified WIDETAG value. The result is placed in
223 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline stack-allocate-p)
226 (bug "empty &body in WITH-FIXED-ALLOCATION"))
227 (once-only ((result-tn result-tn) (size size) (stack-allocate-p stack-allocate-p))
228 `(maybe-pseudo-atomic ,stack-allocate-p
229 (allocation ,result-tn (pad-data-block ,size) ,inline ,stack-allocate-p
230 other-pointer-lowtag)
231 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
232 ,result-tn 0 other-pointer-lowtag)
236 (defun emit-error-break (vop kind code values)
239 (inst int 3) ; i386 breakpoint instruction
240 ;; On Darwin, we need to use #x0b0f instead of int3 in order
241 ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
242 ;; doesn't seem to be reliably firing SIGTRAP
243 ;; handlers. Hopefully this will be fixed by Apple at a
244 ;; later date. Do the same on x86-64 as we do on x86 until this gets
248 ;; The return PC points here; note the location for the debugger.
250 (note-this-location vop :internal-error))
251 (inst byte kind) ; eg trap_Xyyy
252 (with-adjustable-vector (vector) ; interr arguments
253 (write-var-integer code vector)
255 ;; classic CMU CL comment:
256 ;; zzzzz jrd here. tn-offset is zero for constant
258 (write-var-integer (make-sc-offset (sc-number (tn-sc tn))
259 (or (tn-offset tn) 0))
261 (inst byte (length vector))
262 (dotimes (i (length vector))
263 (inst byte (aref vector i))))))
265 (defun error-call (vop error-code &rest values)
267 "Cause an error. ERROR-CODE is the error to cause."
268 (emit-error-break vop error-trap (error-number-or-lose error-code) values))
270 (defun generate-error-code (vop error-code &rest values)
272 "Generate-Error-Code Error-code Value*
273 Emit code for an error with the specified Error-Code and context Values."
274 (assemble (*elsewhere*)
275 (let ((start-lab (gen-label)))
276 (emit-label start-lab)
277 (emit-error-break vop error-trap (error-number-or-lose error-code) values)
283 ;;; This is used to wrap operations which leave untagged memory lying
284 ;;; around. It's an operation which the AOP weenies would describe as
285 ;;; having "cross-cutting concerns", meaning it appears all over the
286 ;;; place and there's no logical single place to attach documentation.
287 ;;; grep (mostly in src/runtime) is your friend
289 (defmacro maybe-pseudo-atomic (not-really-p &body body)
292 (pseudo-atomic ,@body)))
294 ;;; Unsafely clear pa flags so that the image can properly lose in a
297 (defmacro %clear-pseudo-atomic ()
298 '(inst mov (make-ea :qword :base thread-base-tn
299 :disp (* n-word-bytes thread-pseudo-atomic-bits-slot))
303 (defun emit-safepoint ()
304 (inst test al-tn (make-ea :byte :disp sb!vm::gc-safepoint-page-addr)))
307 (defmacro pseudo-atomic (&rest forms)
308 #!+sb-safepoint-strictly
309 `(progn ,@forms (emit-safepoint))
310 #!-sb-safepoint-strictly
311 (with-unique-names (label)
312 `(let ((,label (gen-label)))
313 (inst mov (make-ea :qword
315 :disp (* n-word-bytes thread-pseudo-atomic-bits-slot))
318 (inst xor (make-ea :qword
320 :disp (* n-word-bytes thread-pseudo-atomic-bits-slot))
323 ;; if PAI was set, interrupts were disabled at the same time
324 ;; using the process signal mask.
325 (inst break pending-interrupt-trap)
328 ;; In this case, when allocation thinks a GC should be done, it
329 ;; does not mark PA as interrupted, but schedules a safepoint
330 ;; trap instead. Let's take the opportunity to trigger that
331 ;; safepoint right now.
336 (defmacro pseudo-atomic (&rest forms)
337 (with-unique-names (label)
338 `(let ((,label (gen-label)))
339 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
340 ;; something. (perhaps SVLB, for static variable low byte)
341 (inst mov (make-ea :qword :disp (+ nil-value
342 (static-symbol-offset
343 '*pseudo-atomic-bits*)
344 (ash symbol-value-slot word-shift)
345 (- other-pointer-lowtag)))
348 (inst xor (make-ea :qword :disp (+ nil-value
349 (static-symbol-offset
350 '*pseudo-atomic-bits*)
351 (ash symbol-value-slot word-shift)
352 (- other-pointer-lowtag)))
355 ;; if PAI was set, interrupts were disabled at the same time
356 ;; using the process signal mask.
357 (inst break pending-interrupt-trap)
358 (emit-label ,label))))
360 ;;;; indexed references
362 (defmacro define-full-compare-and-swap
363 (name type offset lowtag scs el-type &optional translate)
366 ,@(when translate `((:translate ,translate)))
368 (:args (object :scs (descriptor-reg) :to :eval)
369 (index :scs (any-reg) :to :result)
370 (old-value :scs ,scs :target rax)
371 (new-value :scs ,scs))
372 (:arg-types ,type tagged-num ,el-type ,el-type)
373 (:temporary (:sc descriptor-reg :offset rax-offset
374 :from (:argument 2) :to :result :target value) rax)
375 (:results (value :scs ,scs))
376 (:result-types ,el-type)
379 (inst cmpxchg (make-ea :qword :base object :index index
380 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
381 :disp (- (* ,offset n-word-bytes) ,lowtag))
385 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
389 `((:translate ,translate)))
391 (:args (object :scs (descriptor-reg))
392 (index :scs (any-reg)))
393 (:arg-types ,type tagged-num)
394 (:results (value :scs ,scs))
395 (:result-types ,el-type)
396 (:generator 3 ; pw was 5
397 (inst mov value (make-ea :qword :base object :index index
398 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
399 :disp (- (* ,offset n-word-bytes)
401 (define-vop (,(symbolicate name "-C"))
403 `((:translate ,translate)))
405 (:args (object :scs (descriptor-reg)))
408 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
410 (:results (value :scs ,scs))
411 (:result-types ,el-type)
412 (:generator 2 ; pw was 5
413 (inst mov value (make-ea :qword :base object
414 :disp (- (* (+ ,offset index) n-word-bytes)
417 (defmacro define-full-reffer+offset (name type offset lowtag scs el-type &optional translate)
421 `((:translate ,translate)))
423 (:args (object :scs (descriptor-reg))
424 (index :scs (any-reg)))
426 (:arg-types ,type tagged-num
427 (:constant (constant-displacement other-pointer-lowtag
428 n-word-bytes vector-data-offset)))
429 (:results (value :scs ,scs))
430 (:result-types ,el-type)
431 (:generator 3 ; pw was 5
432 (inst mov value (make-ea :qword :base object :index index
433 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
434 :disp (- (* (+ ,offset offset) n-word-bytes)
436 (define-vop (,(symbolicate name "-C"))
438 `((:translate ,translate)))
440 (:args (object :scs (descriptor-reg)))
443 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
445 (:constant (constant-displacement other-pointer-lowtag
446 n-word-bytes vector-data-offset)))
447 (:results (value :scs ,scs))
448 (:result-types ,el-type)
449 (:generator 2 ; pw was 5
450 (inst mov value (make-ea :qword :base object
451 :disp (- (* (+ ,offset index offset) n-word-bytes)
454 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
458 `((:translate ,translate)))
460 (:args (object :scs (descriptor-reg))
461 (index :scs (any-reg))
462 (value :scs ,scs :target result))
463 (:arg-types ,type tagged-num ,el-type)
464 (:results (result :scs ,scs))
465 (:result-types ,el-type)
466 (:generator 4 ; was 5
467 (inst mov (make-ea :qword :base object :index index
468 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
469 :disp (- (* ,offset n-word-bytes) ,lowtag))
471 (move result value)))
472 (define-vop (,(symbolicate name "-C"))
474 `((:translate ,translate)))
476 (:args (object :scs (descriptor-reg))
477 (value :scs ,scs :target result))
480 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
483 (:results (result :scs ,scs))
484 (:result-types ,el-type)
485 (:generator 3 ; was 5
486 (inst mov (make-ea :qword :base object
487 :disp (- (* (+ ,offset index) n-word-bytes)
490 (move result value)))))
492 (defmacro define-full-setter+offset (name type offset lowtag scs el-type &optional translate)
496 `((:translate ,translate)))
498 (:args (object :scs (descriptor-reg))
499 (index :scs (any-reg))
500 (value :scs ,scs :target result))
502 (:arg-types ,type tagged-num
503 (:constant (constant-displacement other-pointer-lowtag
507 (:results (result :scs ,scs))
508 (:result-types ,el-type)
509 (:generator 4 ; was 5
510 (inst mov (make-ea :qword :base object :index index
511 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
512 :disp (- (* (+ ,offset offset) n-word-bytes) ,lowtag))
514 (move result value)))
515 (define-vop (,(symbolicate name "-C"))
517 `((:translate ,translate)))
519 (:args (object :scs (descriptor-reg))
520 (value :scs ,scs :target result))
523 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
525 (:constant (constant-displacement other-pointer-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 offset) n-word-bytes)
536 (move result value)))))
538 ;;; helper for alien stuff.
540 (def!macro with-pinned-objects ((&rest objects) &body body)
541 "Arrange with the garbage collector that the pages occupied by
542 OBJECTS will not be moved in memory for the duration of BODY.
543 Useful for e.g. foreign calls where another thread may trigger
546 (let ((pins (make-gensym-list (length objects)))
547 (wpo (sb!xc:gensym "WITH-PINNED-OBJECTS-THUNK")))
548 ;; BODY is stuffed in a function to preserve the lexical
550 `(flet ((,wpo () (progn ,@body)))
551 (declare (muffle-conditions compiler-note))
552 ;; PINS are dx-allocated in case the compiler for some
553 ;; unfathomable reason decides to allocate value-cells
554 ;; for them -- since we have DX value-cells on x86oid
555 ;; platforms this still forces them on the stack.
556 (dx-let ,(mapcar #'list pins objects)
557 (multiple-value-prog1 (,wpo)
558 ;; TOUCH-OBJECT has a VOP with an empty body: compiler
559 ;; thinks we're using the argument and doesn't flush
560 ;; the variable, but we don't have to pay any extra
561 ;; beyond that -- and MULTIPLE-VALUE-PROG1 keeps them
562 ;; live till the body has finished. *whew*
563 ,@(mapcar (lambda (pin)
564 `(touch-object ,pin))