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 (multiple-value-bind (lo hi) (dwords-for-quad ,value)
45 (inst mov (make-ea-for-object-slot-half
46 ,ptr ,slot ,lowtag) lo)
47 (inst mov (make-ea-for-object-slot-half
48 ,ptr (+ ,slot 1/2) ,lowtag) hi)))
50 (inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) ,value)))))
52 (defmacro pushw (ptr &optional (slot 0) (lowtag 0))
53 `(inst push (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
55 (defmacro popw (ptr &optional (slot 0) (lowtag 0))
56 `(inst pop (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
58 ;;;; macros to generate useful values
60 (defmacro load-symbol (reg symbol)
61 `(inst mov ,reg (+ nil-value (static-symbol-offset ,symbol))))
63 (defmacro make-ea-for-symbol-value (symbol)
66 (static-symbol-offset ',symbol)
67 (ash symbol-value-slot word-shift)
68 (- other-pointer-lowtag))))
70 (defmacro load-symbol-value (reg symbol)
71 `(inst mov ,reg (make-ea-for-symbol-value ,symbol)))
73 (defmacro store-symbol-value (reg symbol)
74 `(inst mov (make-ea-for-symbol-value ,symbol) ,reg))
77 (defmacro make-ea-for-symbol-tls-index (symbol)
80 (static-symbol-offset ',symbol)
81 (ash symbol-tls-index-slot word-shift)
82 (- other-pointer-lowtag))))
85 (defmacro load-tl-symbol-value (reg symbol)
87 (inst mov ,reg (make-ea-for-symbol-tls-index ,symbol))
88 (inst mov ,reg (make-ea :qword :base thread-base-tn :scale 1 :index ,reg))))
90 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
93 (defmacro store-tl-symbol-value (reg symbol temp)
95 (inst mov ,temp (make-ea-for-symbol-tls-index ,symbol))
96 (inst mov (make-ea :qword :base thread-base-tn :scale 1 :index ,temp) ,reg)))
98 (defmacro store-tl-symbol-value (reg symbol temp)
99 (declare (ignore temp))
100 `(store-symbol-value ,reg ,symbol))
102 (defmacro load-binding-stack-pointer (reg)
104 `(inst mov ,reg (make-ea :qword :base thread-base-tn
105 :disp (* 8 thread-binding-stack-pointer-slot)))
107 `(load-symbol-value ,reg *binding-stack-pointer*))
109 (defmacro store-binding-stack-pointer (reg)
111 `(inst mov (make-ea :qword :base thread-base-tn
112 :disp (* 8 thread-binding-stack-pointer-slot))
115 `(store-symbol-value ,reg *binding-stack-pointer*))
117 (defmacro load-type (target source &optional (offset 0))
119 "Loads the type bits of a pointer into target independent of
120 byte-ordering issues."
121 (once-only ((n-target target)
124 (ecase *backend-byte-order*
127 (make-ea :byte :base ,n-source :disp ,n-offset)))
130 (make-ea :byte :base ,n-source
131 :disp (+ ,n-offset (1- n-word-bytes))))))))
133 ;;;; allocation helpers
135 ;;; All allocation is done by calls to assembler routines that
136 ;;; eventually invoke the C alloc() function.
138 ;;; Emit code to allocate an object with a size in bytes given by
139 ;;; Size. The size may be an integer of a TN. If Inline is a VOP
140 ;;; node-var then it is used to make an appropriate speed vs size
143 (defun allocation-dynamic-extent (alloc-tn size lowtag)
144 (inst sub rsp-tn size)
145 ;; see comment in x86/macros.lisp implementation of this
146 (inst and rsp-tn #.(lognot lowtag-mask))
147 (aver (not (location= alloc-tn rsp-tn)))
148 (inst lea alloc-tn (make-ea :byte :base rsp-tn :disp lowtag))
151 ;;; This macro should only be used inside a pseudo-atomic section,
152 ;;; which should also cover subsequent initialization of the
154 (defun allocation-tramp (alloc-tn size lowtag)
156 (inst lea temp-reg-tn (make-ea :qword
157 :disp (make-fixup "alloc_tramp" :foreign)))
158 (inst call temp-reg-tn)
161 (inst lea alloc-tn (make-ea :byte :base alloc-tn :disp lowtag)))
164 (defun allocation (alloc-tn size &optional ignored dynamic-extent lowtag)
165 (declare (ignore ignored))
167 (allocation-dynamic-extent alloc-tn size lowtag)
168 (return-from allocation (values)))
169 (let ((NOT-INLINE (gen-label))
172 (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
173 ;; thread->alloc_region.free_pointer
177 :base thread-base-tn :scale 1
178 :disp (* n-word-bytes thread-alloc-region-slot))
182 (make-fixup "boxed_region" :foreign)))
183 ;; thread->alloc_region.end_addr
187 :base thread-base-tn :scale 1
188 :disp (* n-word-bytes (1+ thread-alloc-region-slot)))
192 (make-fixup "boxed_region" :foreign 8))))
194 (allocation-tramp alloc-tn size lowtag))
196 (inst mov temp-reg-tn free-pointer)
198 (if (location= alloc-tn size)
199 (inst add alloc-tn temp-reg-tn)
201 (make-ea :qword :base temp-reg-tn :index size)))
203 (make-ea :qword :base temp-reg-tn :disp size)))
204 (inst cmp end-addr alloc-tn)
205 (inst jmp :be NOT-INLINE)
206 (inst mov free-pointer alloc-tn)
208 (inst lea alloc-tn (make-ea :byte :base temp-reg-tn :disp lowtag))
209 (inst mov alloc-tn temp-reg-tn))
211 (assemble (*elsewhere*)
212 (emit-label NOT-INLINE)
213 (cond ((numberp size)
214 (allocation-tramp alloc-tn size lowtag))
216 (inst sub alloc-tn free-pointer)
217 (allocation-tramp alloc-tn alloc-tn lowtag)))
221 ;;; Allocate an other-pointer object of fixed SIZE with a single word
222 ;;; header having the specified WIDETAG value. The result is placed in
224 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline stack-allocate-p)
227 (bug "empty &body in WITH-FIXED-ALLOCATION"))
228 (once-only ((result-tn result-tn) (size size) (stack-allocate-p stack-allocate-p))
229 `(maybe-pseudo-atomic ,stack-allocate-p
230 (allocation ,result-tn (pad-data-block ,size) ,inline ,stack-allocate-p
231 other-pointer-lowtag)
232 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
233 ,result-tn 0 other-pointer-lowtag)
237 (defun emit-error-break (vop kind code values)
240 (inst int 3) ; i386 breakpoint instruction
241 ;; On Darwin, we need to use #x0b0f instead of int3 in order
242 ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
243 ;; doesn't seem to be reliably firing SIGTRAP
244 ;; handlers. Hopefully this will be fixed by Apple at a
245 ;; later date. Do the same on x86-64 as we do on x86 until this gets
249 ;; The return PC points here; note the location for the debugger.
251 (note-this-location vop :internal-error))
252 (inst byte kind) ; eg trap_Xyyy
253 (with-adjustable-vector (vector) ; interr arguments
254 (write-var-integer code vector)
256 ;; classic CMU CL comment:
257 ;; zzzzz jrd here. tn-offset is zero for constant
259 (write-var-integer (make-sc-offset (sc-number (tn-sc tn))
260 (or (tn-offset tn) 0))
262 (inst byte (length vector))
263 (dotimes (i (length vector))
264 (inst byte (aref vector i))))))
266 (defun error-call (vop error-code &rest values)
268 "Cause an error. ERROR-CODE is the error to cause."
269 (emit-error-break vop error-trap (error-number-or-lose error-code) values))
271 (defun generate-error-code (vop error-code &rest values)
273 "Generate-Error-Code Error-code Value*
274 Emit code for an error with the specified Error-Code and context Values."
275 (assemble (*elsewhere*)
276 (let ((start-lab (gen-label)))
277 (emit-label start-lab)
278 (emit-error-break vop error-trap (error-number-or-lose error-code) values)
284 ;;; This is used to wrap operations which leave untagged memory lying
285 ;;; around. It's an operation which the AOP weenies would describe as
286 ;;; having "cross-cutting concerns", meaning it appears all over the
287 ;;; place and there's no logical single place to attach documentation.
288 ;;; grep (mostly in src/runtime) is your friend
290 (defmacro maybe-pseudo-atomic (not-really-p &body body)
293 (pseudo-atomic ,@body)))
296 (defmacro pseudo-atomic (&rest forms)
297 (with-unique-names (label)
298 `(let ((,label (gen-label)))
299 (inst mov (make-ea :qword
301 :disp (* 8 thread-pseudo-atomic-bits-slot))
304 (inst xor (make-ea :qword
306 :disp (* 8 thread-pseudo-atomic-bits-slot))
309 ;; if PAI was set, interrupts were disabled at the same time
310 ;; using the process signal mask.
311 (inst break pending-interrupt-trap)
312 (emit-label ,label))))
316 (defmacro pseudo-atomic (&rest forms)
317 (with-unique-names (label)
318 `(let ((,label (gen-label)))
319 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
320 ;; something. (perhaps SVLB, for static variable low byte)
321 (inst mov (make-ea :qword :disp (+ nil-value
322 (static-symbol-offset
323 '*pseudo-atomic-bits*)
324 (ash symbol-value-slot word-shift)
325 (- other-pointer-lowtag)))
328 (inst xor (make-ea :qword :disp (+ nil-value
329 (static-symbol-offset
330 '*pseudo-atomic-bits*)
331 (ash symbol-value-slot word-shift)
332 (- other-pointer-lowtag)))
335 ;; if PAI was set, interrupts were disabled at the same time
336 ;; using the process signal mask.
337 (inst break pending-interrupt-trap)
338 (emit-label ,label))))
340 ;;;; indexed references
342 (defmacro define-full-compare-and-swap
343 (name type offset lowtag scs el-type &optional translate)
346 ,@(when translate `((:translate ,translate)))
348 (:args (object :scs (descriptor-reg) :to :eval)
349 (index :scs (any-reg) :to :result)
350 (old-value :scs ,scs :target rax)
351 (new-value :scs ,scs))
352 (:arg-types ,type tagged-num ,el-type ,el-type)
353 (:temporary (:sc descriptor-reg :offset rax-offset
354 :from (:argument 2) :to :result :target value) rax)
355 (:results (value :scs ,scs))
356 (:result-types ,el-type)
359 (inst cmpxchg (make-ea :qword :base object :index index
360 :disp (- (* ,offset n-word-bytes) ,lowtag))
364 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
368 `((:translate ,translate)))
370 (:args (object :scs (descriptor-reg))
371 (index :scs (any-reg)))
372 (:arg-types ,type tagged-num)
373 (:results (value :scs ,scs))
374 (:result-types ,el-type)
375 (:generator 3 ; pw was 5
376 (inst mov value (make-ea :qword :base object :index index
377 :disp (- (* ,offset n-word-bytes)
379 (define-vop (,(symbolicate name "-C"))
381 `((:translate ,translate)))
383 (:args (object :scs (descriptor-reg)))
386 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
388 (:results (value :scs ,scs))
389 (:result-types ,el-type)
390 (:generator 2 ; pw was 5
391 (inst mov value (make-ea :qword :base object
392 :disp (- (* (+ ,offset index) n-word-bytes)
395 (defmacro define-full-reffer+offset (name type offset lowtag scs el-type &optional translate)
399 `((:translate ,translate)))
401 (:args (object :scs (descriptor-reg))
402 (index :scs (any-reg)))
404 (:arg-types ,type tagged-num
405 (:constant (constant-displacement other-pointer-lowtag
406 n-word-bytes vector-data-offset)))
407 (:results (value :scs ,scs))
408 (:result-types ,el-type)
409 (:generator 3 ; pw was 5
410 (inst mov value (make-ea :qword :base object :index index
411 :disp (- (* (+ ,offset offset) n-word-bytes)
413 (define-vop (,(symbolicate name "-C"))
415 `((:translate ,translate)))
417 (:args (object :scs (descriptor-reg)))
420 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
422 (:constant (constant-displacement other-pointer-lowtag
423 n-word-bytes vector-data-offset)))
424 (:results (value :scs ,scs))
425 (:result-types ,el-type)
426 (:generator 2 ; pw was 5
427 (inst mov value (make-ea :qword :base object
428 :disp (- (* (+ ,offset index offset) n-word-bytes)
431 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
435 `((:translate ,translate)))
437 (:args (object :scs (descriptor-reg))
438 (index :scs (any-reg))
439 (value :scs ,scs :target result))
440 (:arg-types ,type tagged-num ,el-type)
441 (:results (result :scs ,scs))
442 (:result-types ,el-type)
443 (:generator 4 ; was 5
444 (inst mov (make-ea :qword :base object :index index
445 :disp (- (* ,offset n-word-bytes) ,lowtag))
447 (move result value)))
448 (define-vop (,(symbolicate name "-C"))
450 `((:translate ,translate)))
452 (:args (object :scs (descriptor-reg))
453 (value :scs ,scs :target result))
456 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
459 (:results (result :scs ,scs))
460 (:result-types ,el-type)
461 (:generator 3 ; was 5
462 (inst mov (make-ea :qword :base object
463 :disp (- (* (+ ,offset index) n-word-bytes)
466 (move result value)))))
468 (defmacro define-full-setter+offset (name type offset lowtag scs el-type &optional translate)
472 `((:translate ,translate)))
474 (:args (object :scs (descriptor-reg))
475 (index :scs (any-reg))
476 (value :scs ,scs :target result))
478 (:arg-types ,type tagged-num
479 (:constant (constant-displacement other-pointer-lowtag
483 (:results (result :scs ,scs))
484 (:result-types ,el-type)
485 (:generator 4 ; was 5
486 (inst mov (make-ea :qword :base object :index index
487 :disp (- (* (+ ,offset offset) n-word-bytes) ,lowtag))
489 (move result value)))
490 (define-vop (,(symbolicate name "-C"))
492 `((:translate ,translate)))
494 (:args (object :scs (descriptor-reg))
495 (value :scs ,scs :target result))
498 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
500 (:constant (constant-displacement other-pointer-lowtag
504 (:results (result :scs ,scs))
505 (:result-types ,el-type)
506 (:generator 3 ; was 5
507 (inst mov (make-ea :qword :base object
508 :disp (- (* (+ ,offset index offset) n-word-bytes)
511 (move result value)))))
513 ;;; helper for alien stuff.
515 (def!macro with-pinned-objects ((&rest objects) &body body)
516 "Arrange with the garbage collector that the pages occupied by
517 OBJECTS will not be moved in memory for the duration of BODY.
518 Useful for e.g. foreign calls where another thread may trigger
521 (let ((pins (make-gensym-list (length objects)))
522 (wpo (block-gensym "WPO")))
523 ;; BODY is stuffed in a function to preserve the lexical
525 `(flet ((,wpo () (progn ,@body)))
526 (declare (muffle-conditions compiler-note))
527 ;; PINS are dx-allocated in case the compiler for some
528 ;; unfathomable reason decides to allocate value-cells
529 ;; for them -- since we have DX value-cells on x86oid
530 ;; platforms this still forces them on the stack.
531 (dx-let ,(mapcar #'list pins objects)
532 (multiple-value-prog1 (,wpo)
533 ;; TOUCH-OBJECT has a VOP with an empty body: compiler
534 ;; thinks we're using the argument and doesn't flush
535 ;; the variable, but we don't have to pay any extra
536 ;; beyond that -- and MULTIPLE-VALUE-PROG1 keeps them
537 ;; live till the body has finished. *whew*
538 ,@(mapcar (lambda (pin)
539 `(touch-object ,pin))