1 ;;;; a bunch of handy macros for the x86
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 (defmacro move (dst src)
18 "Move SRC into DST unless they are location=."
19 (once-only ((n-dst dst)
21 `(unless (location= ,n-dst ,n-src)
23 ((single-reg complex-single-reg)
24 (aver (xmm-register-p ,n-src))
25 (inst movaps ,n-dst ,n-src))
26 ((double-reg complex-double-reg)
27 (aver (xmm-register-p ,n-src))
28 (inst movapd ,n-dst ,n-src))
30 (inst mov ,n-dst ,n-src))))))
32 (defmacro make-ea-for-object-slot (ptr slot lowtag)
33 `(make-ea :qword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
34 (defmacro make-ea-for-object-slot-half (ptr slot lowtag)
35 `(make-ea :dword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
37 (defmacro loadw (value ptr &optional (slot 0) (lowtag 0))
38 `(inst mov ,value (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
40 (defmacro storew (value ptr &optional (slot 0) (lowtag 0))
41 (once-only ((value value))
42 `(cond ((and (integerp ,value)
43 (not (typep ,value '(signed-byte 32))))
44 (inst mov temp-reg-tn ,value)
45 (inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) temp-reg-tn))
47 (inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) ,value)))))
49 (defmacro pushw (ptr &optional (slot 0) (lowtag 0))
50 `(inst push (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
52 (defmacro popw (ptr &optional (slot 0) (lowtag 0))
53 `(inst pop (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
55 ;;;; macros to generate useful values
57 (defmacro load-symbol (reg symbol)
58 `(inst mov ,reg (+ nil-value (static-symbol-offset ,symbol))))
60 (defmacro make-ea-for-symbol-value (symbol)
63 (static-symbol-offset ',symbol)
64 (ash symbol-value-slot word-shift)
65 (- other-pointer-lowtag))))
67 (defmacro load-symbol-value (reg symbol)
68 `(inst mov ,reg (make-ea-for-symbol-value ,symbol)))
70 (defmacro store-symbol-value (reg symbol)
71 `(inst mov (make-ea-for-symbol-value ,symbol) ,reg))
74 (defmacro make-ea-for-symbol-tls-index (symbol)
77 (static-symbol-offset ',symbol)
78 (ash symbol-tls-index-slot word-shift)
79 (- other-pointer-lowtag))))
82 (defmacro load-tl-symbol-value (reg symbol)
84 (inst mov ,reg (make-ea-for-symbol-tls-index ,symbol))
85 (inst mov ,reg (make-ea :qword :base thread-base-tn :scale 1 :index ,reg))))
87 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
90 (defmacro store-tl-symbol-value (reg symbol temp)
92 (inst mov ,temp (make-ea-for-symbol-tls-index ,symbol))
93 (inst mov (make-ea :qword :base thread-base-tn :scale 1 :index ,temp) ,reg)))
95 (defmacro store-tl-symbol-value (reg symbol temp)
96 (declare (ignore temp))
97 `(store-symbol-value ,reg ,symbol))
99 (defmacro load-binding-stack-pointer (reg)
101 `(inst mov ,reg (make-ea :qword :base thread-base-tn
102 :disp (* 8 thread-binding-stack-pointer-slot)))
104 `(load-symbol-value ,reg *binding-stack-pointer*))
106 (defmacro store-binding-stack-pointer (reg)
108 `(inst mov (make-ea :qword :base thread-base-tn
109 :disp (* 8 thread-binding-stack-pointer-slot))
112 `(store-symbol-value ,reg *binding-stack-pointer*))
114 (defmacro load-type (target source &optional (offset 0))
116 "Loads the type bits of a pointer into target independent of
117 byte-ordering issues."
118 (once-only ((n-target target)
121 (ecase *backend-byte-order*
124 (make-ea :byte :base ,n-source :disp ,n-offset)))
127 (make-ea :byte :base ,n-source
128 :disp (+ ,n-offset (1- n-word-bytes))))))))
130 ;;;; allocation helpers
132 ;;; All allocation is done by calls to assembler routines that
133 ;;; eventually invoke the C alloc() function.
135 ;;; Emit code to allocate an object with a size in bytes given by
136 ;;; Size. The size may be an integer of a TN. If Inline is a VOP
137 ;;; node-var then it is used to make an appropriate speed vs size
140 (defun allocation-dynamic-extent (alloc-tn size lowtag)
141 (inst sub rsp-tn size)
142 ;; see comment in x86/macros.lisp implementation of this
143 (inst and rsp-tn #.(lognot lowtag-mask))
144 (aver (not (location= alloc-tn rsp-tn)))
145 (inst lea alloc-tn (make-ea :byte :base rsp-tn :disp lowtag))
148 ;;; This macro should only be used inside a pseudo-atomic section,
149 ;;; which should also cover subsequent initialization of the
151 (defun allocation-tramp (alloc-tn size lowtag)
153 (inst lea temp-reg-tn (make-ea :qword
154 :disp (make-fixup "alloc_tramp" :foreign)))
155 (inst call temp-reg-tn)
158 (inst lea alloc-tn (make-ea :byte :base alloc-tn :disp lowtag)))
161 (defun allocation (alloc-tn size &optional ignored dynamic-extent lowtag)
162 (declare (ignore ignored))
164 (allocation-dynamic-extent alloc-tn size lowtag)
165 (return-from allocation (values)))
166 (let ((NOT-INLINE (gen-label))
169 (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
170 ;; thread->alloc_region.free_pointer
174 :base thread-base-tn :scale 1
175 :disp (* n-word-bytes thread-alloc-region-slot))
179 (make-fixup "boxed_region" :foreign)))
180 ;; thread->alloc_region.end_addr
184 :base thread-base-tn :scale 1
185 :disp (* n-word-bytes (1+ thread-alloc-region-slot)))
189 (make-fixup "boxed_region" :foreign 8))))
191 (allocation-tramp alloc-tn size lowtag))
193 (inst mov temp-reg-tn free-pointer)
195 (if (location= alloc-tn size)
196 (inst add alloc-tn temp-reg-tn)
198 (make-ea :qword :base temp-reg-tn :index size)))
200 (make-ea :qword :base temp-reg-tn :disp size)))
201 (inst cmp end-addr alloc-tn)
202 (inst jmp :be NOT-INLINE)
203 (inst mov free-pointer alloc-tn)
205 (inst lea alloc-tn (make-ea :byte :base temp-reg-tn :disp lowtag))
206 (inst mov alloc-tn temp-reg-tn))
208 (assemble (*elsewhere*)
209 (emit-label NOT-INLINE)
210 (cond ((numberp size)
211 (allocation-tramp alloc-tn size lowtag))
213 (inst sub alloc-tn free-pointer)
214 (allocation-tramp alloc-tn alloc-tn lowtag)))
218 ;;; Allocate an other-pointer object of fixed SIZE with a single word
219 ;;; header having the specified WIDETAG value. The result is placed in
221 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline stack-allocate-p)
224 (bug "empty &body in WITH-FIXED-ALLOCATION"))
225 (once-only ((result-tn result-tn) (size size) (stack-allocate-p stack-allocate-p))
226 `(maybe-pseudo-atomic ,stack-allocate-p
227 (allocation ,result-tn (pad-data-block ,size) ,inline ,stack-allocate-p
228 other-pointer-lowtag)
229 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
230 ,result-tn 0 other-pointer-lowtag)
234 (defun emit-error-break (vop kind code values)
237 (inst int 3) ; i386 breakpoint instruction
238 ;; On Darwin, we need to use #x0b0f instead of int3 in order
239 ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
240 ;; doesn't seem to be reliably firing SIGTRAP
241 ;; handlers. Hopefully this will be fixed by Apple at a
242 ;; later date. Do the same on x86-64 as we do on x86 until this gets
246 ;; The return PC points here; note the location for the debugger.
248 (note-this-location vop :internal-error))
249 (inst byte kind) ; eg trap_Xyyy
250 (with-adjustable-vector (vector) ; interr arguments
251 (write-var-integer code vector)
253 ;; classic CMU CL comment:
254 ;; zzzzz jrd here. tn-offset is zero for constant
256 (write-var-integer (make-sc-offset (sc-number (tn-sc tn))
257 (or (tn-offset tn) 0))
259 (inst byte (length vector))
260 (dotimes (i (length vector))
261 (inst byte (aref vector i))))))
263 (defun error-call (vop error-code &rest values)
265 "Cause an error. ERROR-CODE is the error to cause."
266 (emit-error-break vop error-trap (error-number-or-lose error-code) values))
268 (defun generate-error-code (vop error-code &rest values)
270 "Generate-Error-Code Error-code Value*
271 Emit code for an error with the specified Error-Code and context Values."
272 (assemble (*elsewhere*)
273 (let ((start-lab (gen-label)))
274 (emit-label start-lab)
275 (emit-error-break vop error-trap (error-number-or-lose error-code) values)
281 ;;; This is used to wrap operations which leave untagged memory lying
282 ;;; around. It's an operation which the AOP weenies would describe as
283 ;;; having "cross-cutting concerns", meaning it appears all over the
284 ;;; place and there's no logical single place to attach documentation.
285 ;;; grep (mostly in src/runtime) is your friend
287 (defmacro maybe-pseudo-atomic (not-really-p &body body)
290 (pseudo-atomic ,@body)))
292 ;;; Unsafely clear pa flags so that the image can properly lose in a
295 (defmacro %clear-pseudo-atomic ()
296 '(inst mov (make-ea :qword :base thread-base-tn
297 :disp (* 8 thread-pseudo-atomic-bits-slot))
301 (defmacro pseudo-atomic (&rest forms)
302 (with-unique-names (label)
303 `(let ((,label (gen-label)))
304 (inst mov (make-ea :qword
306 :disp (* 8 thread-pseudo-atomic-bits-slot))
309 (inst xor (make-ea :qword
311 :disp (* 8 thread-pseudo-atomic-bits-slot))
314 ;; if PAI was set, interrupts were disabled at the same time
315 ;; using the process signal mask.
316 (inst break pending-interrupt-trap)
317 (emit-label ,label))))
321 (defmacro pseudo-atomic (&rest forms)
322 (with-unique-names (label)
323 `(let ((,label (gen-label)))
324 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
325 ;; something. (perhaps SVLB, for static variable low byte)
326 (inst mov (make-ea :qword :disp (+ nil-value
327 (static-symbol-offset
328 '*pseudo-atomic-bits*)
329 (ash symbol-value-slot word-shift)
330 (- other-pointer-lowtag)))
333 (inst xor (make-ea :qword :disp (+ nil-value
334 (static-symbol-offset
335 '*pseudo-atomic-bits*)
336 (ash symbol-value-slot word-shift)
337 (- other-pointer-lowtag)))
340 ;; if PAI was set, interrupts were disabled at the same time
341 ;; using the process signal mask.
342 (inst break pending-interrupt-trap)
343 (emit-label ,label))))
345 ;;;; indexed references
347 (defmacro define-full-compare-and-swap
348 (name type offset lowtag scs el-type &optional translate)
351 ,@(when translate `((:translate ,translate)))
353 (:args (object :scs (descriptor-reg) :to :eval)
354 (index :scs (any-reg) :to :result)
355 (old-value :scs ,scs :target rax)
356 (new-value :scs ,scs))
357 (:arg-types ,type tagged-num ,el-type ,el-type)
358 (:temporary (:sc descriptor-reg :offset rax-offset
359 :from (:argument 2) :to :result :target value) rax)
360 (:results (value :scs ,scs))
361 (:result-types ,el-type)
364 (inst cmpxchg (make-ea :qword :base object :index index
365 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
366 :disp (- (* ,offset n-word-bytes) ,lowtag))
370 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
374 `((:translate ,translate)))
376 (:args (object :scs (descriptor-reg))
377 (index :scs (any-reg)))
378 (:arg-types ,type tagged-num)
379 (:results (value :scs ,scs))
380 (:result-types ,el-type)
381 (:generator 3 ; pw was 5
382 (inst mov value (make-ea :qword :base object :index index
383 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
384 :disp (- (* ,offset n-word-bytes)
386 (define-vop (,(symbolicate name "-C"))
388 `((:translate ,translate)))
390 (:args (object :scs (descriptor-reg)))
393 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
395 (:results (value :scs ,scs))
396 (:result-types ,el-type)
397 (:generator 2 ; pw was 5
398 (inst mov value (make-ea :qword :base object
399 :disp (- (* (+ ,offset index) n-word-bytes)
402 (defmacro define-full-reffer+offset (name type offset lowtag scs el-type &optional translate)
406 `((:translate ,translate)))
408 (:args (object :scs (descriptor-reg))
409 (index :scs (any-reg)))
411 (:arg-types ,type tagged-num
412 (:constant (constant-displacement other-pointer-lowtag
413 n-word-bytes vector-data-offset)))
414 (:results (value :scs ,scs))
415 (:result-types ,el-type)
416 (:generator 3 ; pw was 5
417 (inst mov value (make-ea :qword :base object :index index
418 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
419 :disp (- (* (+ ,offset offset) n-word-bytes)
421 (define-vop (,(symbolicate name "-C"))
423 `((:translate ,translate)))
425 (:args (object :scs (descriptor-reg)))
428 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
430 (:constant (constant-displacement other-pointer-lowtag
431 n-word-bytes vector-data-offset)))
432 (:results (value :scs ,scs))
433 (:result-types ,el-type)
434 (:generator 2 ; pw was 5
435 (inst mov value (make-ea :qword :base object
436 :disp (- (* (+ ,offset index offset) n-word-bytes)
439 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
443 `((:translate ,translate)))
445 (:args (object :scs (descriptor-reg))
446 (index :scs (any-reg))
447 (value :scs ,scs :target result))
448 (:arg-types ,type tagged-num ,el-type)
449 (:results (result :scs ,scs))
450 (:result-types ,el-type)
451 (:generator 4 ; was 5
452 (inst mov (make-ea :qword :base object :index index
453 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
454 :disp (- (* ,offset n-word-bytes) ,lowtag))
456 (move result value)))
457 (define-vop (,(symbolicate name "-C"))
459 `((:translate ,translate)))
461 (:args (object :scs (descriptor-reg))
462 (value :scs ,scs :target result))
465 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
468 (:results (result :scs ,scs))
469 (:result-types ,el-type)
470 (:generator 3 ; was 5
471 (inst mov (make-ea :qword :base object
472 :disp (- (* (+ ,offset index) n-word-bytes)
475 (move result value)))))
477 (defmacro define-full-setter+offset (name type offset lowtag scs el-type &optional translate)
481 `((:translate ,translate)))
483 (:args (object :scs (descriptor-reg))
484 (index :scs (any-reg))
485 (value :scs ,scs :target result))
487 (:arg-types ,type tagged-num
488 (:constant (constant-displacement other-pointer-lowtag
492 (:results (result :scs ,scs))
493 (:result-types ,el-type)
494 (:generator 4 ; was 5
495 (inst mov (make-ea :qword :base object :index index
496 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
497 :disp (- (* (+ ,offset offset) n-word-bytes) ,lowtag))
499 (move result value)))
500 (define-vop (,(symbolicate name "-C"))
502 `((:translate ,translate)))
504 (:args (object :scs (descriptor-reg))
505 (value :scs ,scs :target result))
508 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
510 (:constant (constant-displacement other-pointer-lowtag
514 (:results (result :scs ,scs))
515 (:result-types ,el-type)
516 (:generator 3 ; was 5
517 (inst mov (make-ea :qword :base object
518 :disp (- (* (+ ,offset index offset) n-word-bytes)
521 (move result value)))))
523 ;;; helper for alien stuff.
525 (def!macro with-pinned-objects ((&rest objects) &body body)
526 "Arrange with the garbage collector that the pages occupied by
527 OBJECTS will not be moved in memory for the duration of BODY.
528 Useful for e.g. foreign calls where another thread may trigger
531 (let ((pins (make-gensym-list (length objects)))
532 (wpo (gensym "WITH-PINNED-OBJECTS-THUNK")))
533 ;; BODY is stuffed in a function to preserve the lexical
535 `(flet ((,wpo () (progn ,@body)))
536 (declare (muffle-conditions compiler-note))
537 ;; PINS are dx-allocated in case the compiler for some
538 ;; unfathomable reason decides to allocate value-cells
539 ;; for them -- since we have DX value-cells on x86oid
540 ;; platforms this still forces them on the stack.
541 (dx-let ,(mapcar #'list pins objects)
542 (multiple-value-prog1 (,wpo)
543 ;; TOUCH-OBJECT has a VOP with an empty body: compiler
544 ;; thinks we're using the argument and doesn't flush
545 ;; the variable, but we don't have to pay any extra
546 ;; beyond that -- and MULTIPLE-VALUE-PROG1 keeps them
547 ;; live till the body has finished. *whew*
548 ,@(mapcar (lambda (pin)
549 `(touch-object ,pin))