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)
24 (inst movss ,n-dst ,n-src))
26 (inst movsd ,n-dst ,n-src))
28 (inst mov ,n-dst ,n-src))))))
30 (defmacro make-ea-for-object-slot (ptr slot lowtag)
31 `(make-ea :qword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
32 (defmacro make-ea-for-object-slot-half (ptr slot lowtag)
33 `(make-ea :dword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
35 (defmacro loadw (value ptr &optional (slot 0) (lowtag 0))
36 `(inst mov ,value (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
38 (defmacro storew (value ptr &optional (slot 0) (lowtag 0))
39 (once-only ((value value))
40 `(cond ((and (integerp ,value)
41 (not (typep ,value '(signed-byte 32))))
42 (multiple-value-bind (lo hi) (dwords-for-quad ,value)
43 (inst mov (make-ea-for-object-slot-half
44 ,ptr ,slot ,lowtag) lo)
45 (inst mov (make-ea-for-object-slot-half
46 ,ptr (+ ,slot 1/2) ,lowtag) hi)))
48 (inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) ,value)))))
50 (defmacro pushw (ptr &optional (slot 0) (lowtag 0))
51 `(inst push (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
53 (defmacro popw (ptr &optional (slot 0) (lowtag 0))
54 `(inst pop (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
56 ;;;; macros to generate useful values
58 (defmacro load-symbol (reg symbol)
59 `(inst mov ,reg (+ nil-value (static-symbol-offset ,symbol))))
61 (defmacro make-ea-for-symbol-value (symbol)
64 (static-symbol-offset ',symbol)
65 (ash symbol-value-slot word-shift)
66 (- other-pointer-lowtag))))
68 (defmacro load-symbol-value (reg symbol)
69 `(inst mov ,reg (make-ea-for-symbol-value ,symbol)))
71 (defmacro store-symbol-value (reg symbol)
72 `(inst mov (make-ea-for-symbol-value ,symbol) ,reg))
75 (defmacro make-ea-for-symbol-tls-index (symbol)
78 (static-symbol-offset ',symbol)
79 (ash symbol-tls-index-slot word-shift)
80 (- other-pointer-lowtag))))
83 (defmacro load-tl-symbol-value (reg symbol)
85 (inst mov ,reg (make-ea-for-symbol-tls-index ,symbol))
86 (inst mov ,reg (make-ea :qword :base thread-base-tn :scale 1 :index ,reg))))
88 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
91 (defmacro store-tl-symbol-value (reg symbol temp)
93 (inst mov ,temp (make-ea-for-symbol-tls-index ,symbol))
94 (inst mov (make-ea :qword :base thread-base-tn :scale 1 :index ,temp) ,reg)))
96 (defmacro store-tl-symbol-value (reg symbol temp)
97 (declare (ignore temp))
98 `(store-symbol-value ,reg ,symbol))
100 (defmacro load-binding-stack-pointer (reg)
102 `(inst mov ,reg (make-ea :qword :base thread-base-tn
103 :disp (* 8 thread-binding-stack-pointer-slot)))
105 `(load-symbol-value ,reg *binding-stack-pointer*))
107 (defmacro store-binding-stack-pointer (reg)
109 `(inst mov (make-ea :qword :base thread-base-tn
110 :disp (* 8 thread-binding-stack-pointer-slot))
113 `(store-symbol-value ,reg *binding-stack-pointer*))
115 (defmacro load-type (target source &optional (offset 0))
117 "Loads the type bits of a pointer into target independent of
118 byte-ordering issues."
119 (once-only ((n-target target)
122 (ecase *backend-byte-order*
125 (make-ea :byte :base ,n-source :disp ,n-offset)))
128 (make-ea :byte :base ,n-source
129 :disp (+ ,n-offset (1- n-word-bytes))))))))
131 ;;;; allocation helpers
133 ;;; All allocation is done by calls to assembler routines that
134 ;;; eventually invoke the C alloc() function.
136 ;;; Emit code to allocate an object with a size in bytes given by
137 ;;; Size. The size may be an integer of a TN. If Inline is a VOP
138 ;;; node-var then it is used to make an appropriate speed vs size
141 (defun allocation-dynamic-extent (alloc-tn size)
142 (inst sub rsp-tn size)
143 ;; see comment in x86/macros.lisp implementation of this
144 (inst and rsp-tn #.(lognot lowtag-mask))
145 (aver (not (location= alloc-tn rsp-tn)))
146 (inst mov alloc-tn rsp-tn)
149 ;;; This macro should only be used inside a pseudo-atomic section,
150 ;;; which should also cover subsequent initialization of the
152 (defun allocation-tramp (alloc-tn size &optional ignored)
153 (declare (ignore ignored))
155 (inst lea temp-reg-tn (make-ea :qword
156 :disp (make-fixup "alloc_tramp" :foreign)))
157 (inst call temp-reg-tn)
161 (defun allocation (alloc-tn size &optional ignored dynamic-extent)
162 (declare (ignore ignored))
164 (allocation-dynamic-extent alloc-tn size)
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))
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)
204 (inst mov alloc-tn temp-reg-tn)
206 (assemble (*elsewhere*)
207 (emit-label NOT-INLINE)
208 (cond ((numberp size)
209 (allocation-tramp alloc-tn size))
211 (inst sub alloc-tn free-pointer)
212 (allocation-tramp alloc-tn alloc-tn)))
217 (defun allocation (alloc-tn size &optional ignored)
218 (declare (ignore ignored))
220 (inst lea temp-reg-tn (make-ea :qword
221 :disp (make-fixup "alloc_tramp" :foreign)))
222 (inst call temp-reg-tn)
226 ;;; Allocate an other-pointer object of fixed SIZE with a single word
227 ;;; header having the specified WIDETAG value. The result is placed in
229 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline stack-allocate-p)
232 (bug "empty &body in WITH-FIXED-ALLOCATION"))
233 (once-only ((result-tn result-tn) (size size) (stack-allocate-p stack-allocate-p))
234 `(maybe-pseudo-atomic ,stack-allocate-p
235 (allocation ,result-tn (pad-data-block ,size) ,inline ,stack-allocate-p)
236 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
239 (make-ea :qword :base ,result-tn :disp other-pointer-lowtag))
243 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
244 (defun emit-error-break (vop kind code values)
245 (let ((vector (gensym)))
247 #!-darwin (inst int 3) ; i386 breakpoint instruction
248 ;; On Darwin, we need to use #x0b0f instead of int3 in order
249 ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
250 ;; doesn't seem to be reliably firing SIGTRAP
251 ;; handlers. Hopefully this will be fixed by Apple at a
252 ;; later date. Do the same on x86-64 as we do on x86 until this gets
254 #!+darwin (inst word #x0b0f))
256 ;; The return PC points here; note the location for the debugger.
259 (note-this-location vop :internal-error)))
260 (inst byte ,kind) ; eg trap_Xyyy
261 (with-adjustable-vector (,vector) ; interr arguments
262 (write-var-integer (error-number-or-lose ',code) ,vector)
263 ,@(mapcar (lambda (tn)
265 ;; classic CMU CL comment:
266 ;; zzzzz jrd here. tn-offset is zero for constant
268 (write-var-integer (make-sc-offset (sc-number
274 (inst byte (length ,vector))
275 (dotimes (i (length ,vector))
276 (inst byte (aref ,vector i))))))))
278 (defmacro error-call (vop error-code &rest values)
280 "Cause an error. ERROR-CODE is the error to cause."
282 (emit-error-break vop error-trap error-code values)))
284 (defmacro generate-error-code (vop error-code &rest values)
286 "Generate-Error-Code Error-code Value*
287 Emit code for an error with the specified Error-Code and context Values."
288 `(assemble (*elsewhere*)
289 (let ((start-lab (gen-label)))
290 (emit-label start-lab)
291 (error-call ,vop ,error-code ,@values)
297 ;;; This is used to wrap operations which leave untagged memory lying
298 ;;; around. It's an operation which the AOP weenies would describe as
299 ;;; having "cross-cutting concerns", meaning it appears all over the
300 ;;; place and there's no logical single place to attach documentation.
301 ;;; grep (mostly in src/runtime) is your friend
303 ;;; FIXME: THIS NAME IS BACKWARDS!
304 (defmacro maybe-pseudo-atomic (really-p &body body)
307 (pseudo-atomic ,@body)))
310 (defmacro pseudo-atomic (&rest forms)
311 (with-unique-names (label)
312 `(let ((,label (gen-label)))
313 (inst or (make-ea :byte
315 :disp (* 8 thread-pseudo-atomic-bits-slot))
318 (inst xor (make-ea :byte
320 :disp (* 8 thread-pseudo-atomic-bits-slot))
323 ;; if PAI was set, interrupts were disabled at the same
324 ;; time using the process signal mask.
325 (inst break pending-interrupt-trap)
326 (emit-label ,label))))
330 (defmacro pseudo-atomic (&rest forms)
331 (with-unique-names (label)
332 `(let ((,label (gen-label)))
333 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
334 ;; something. (perhaps SVLB, for static variable low byte)
335 (inst or (make-ea :byte :disp (+ nil-value
336 (static-symbol-offset
337 '*pseudo-atomic-bits*)
338 (ash symbol-value-slot word-shift)
339 (- other-pointer-lowtag)))
342 (inst xor (make-ea :byte :disp (+ nil-value
343 (static-symbol-offset
344 '*pseudo-atomic-bits*)
345 (ash symbol-value-slot word-shift)
346 (- other-pointer-lowtag)))
349 ;; if PAI was set, interrupts were disabled at the same time
350 ;; using the process signal mask.
351 (inst break pending-interrupt-trap)
352 (emit-label ,label))))
356 ;;;; indexed references
358 (defmacro define-full-compare-and-swap
359 (name type offset lowtag scs el-type &optional translate)
362 ,@(when translate `((:translate ,translate)))
364 (:args (object :scs (descriptor-reg) :to :eval)
365 (index :scs (any-reg) :to :result)
366 (old-value :scs ,scs :target rax)
367 (new-value :scs ,scs))
368 (:arg-types ,type tagged-num ,el-type ,el-type)
369 (:temporary (:sc descriptor-reg :offset rax-offset
370 :from (:argument 2) :to :result :target value) rax)
371 (:results (value :scs ,scs))
372 (:result-types ,el-type)
377 (inst cmpxchg (make-ea :qword :base object :index index
378 :disp (- (* ,offset n-word-bytes) ,lowtag))
382 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
386 `((:translate ,translate)))
388 (:args (object :scs (descriptor-reg))
389 (index :scs (any-reg)))
390 (:arg-types ,type tagged-num)
391 (:results (value :scs ,scs))
392 (:result-types ,el-type)
393 (:generator 3 ; pw was 5
394 (inst mov value (make-ea :qword :base object :index index
395 :disp (- (* ,offset n-word-bytes)
397 (define-vop (,(symbolicate name "-C"))
399 `((:translate ,translate)))
401 (:args (object :scs (descriptor-reg)))
404 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
406 (:results (value :scs ,scs))
407 (:result-types ,el-type)
408 (:generator 2 ; pw was 5
409 (inst mov value (make-ea :qword :base object
410 :disp (- (* (+ ,offset index) n-word-bytes)
413 (defmacro define-full-reffer+offset (name type offset lowtag scs el-type &optional translate)
417 `((:translate ,translate)))
419 (:args (object :scs (descriptor-reg))
420 (index :scs (any-reg)))
422 (:arg-types ,type tagged-num
423 (:constant (constant-displacement other-pointer-lowtag
424 n-word-bytes vector-data-offset)))
425 (:results (value :scs ,scs))
426 (:result-types ,el-type)
427 (:generator 3 ; pw was 5
428 (inst mov value (make-ea :qword :base object :index index
429 :disp (- (* (+ ,offset offset) n-word-bytes)
431 (define-vop (,(symbolicate name "-C"))
433 `((:translate ,translate)))
435 (:args (object :scs (descriptor-reg)))
438 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
440 (:constant (constant-displacement other-pointer-lowtag
441 n-word-bytes vector-data-offset)))
442 (:results (value :scs ,scs))
443 (:result-types ,el-type)
444 (:generator 2 ; pw was 5
445 (inst mov value (make-ea :qword :base object
446 :disp (- (* (+ ,offset index offset) n-word-bytes)
449 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
453 `((:translate ,translate)))
455 (:args (object :scs (descriptor-reg))
456 (index :scs (any-reg))
457 (value :scs ,scs :target result))
458 (:arg-types ,type tagged-num ,el-type)
459 (:results (result :scs ,scs))
460 (:result-types ,el-type)
461 (:generator 4 ; was 5
462 (inst mov (make-ea :qword :base object :index index
463 :disp (- (* ,offset n-word-bytes) ,lowtag))
465 (move result value)))
466 (define-vop (,(symbolicate name "-C"))
468 `((:translate ,translate)))
470 (:args (object :scs (descriptor-reg))
471 (value :scs ,scs :target result))
474 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
477 (:results (result :scs ,scs))
478 (:result-types ,el-type)
479 (:generator 3 ; was 5
480 (inst mov (make-ea :qword :base object
481 :disp (- (* (+ ,offset index) n-word-bytes)
484 (move result value)))))
486 (defmacro define-full-setter+offset (name type offset lowtag scs el-type &optional translate)
490 `((:translate ,translate)))
492 (:args (object :scs (descriptor-reg))
493 (index :scs (any-reg))
494 (value :scs ,scs :target result))
496 (:arg-types ,type tagged-num
497 (:constant (constant-displacement other-pointer-lowtag
501 (:results (result :scs ,scs))
502 (:result-types ,el-type)
503 (:generator 4 ; was 5
504 (inst mov (make-ea :qword :base object :index index
505 :disp (- (* (+ ,offset offset) n-word-bytes) ,lowtag))
507 (move result value)))
508 (define-vop (,(symbolicate name "-C"))
510 `((:translate ,translate)))
512 (:args (object :scs (descriptor-reg))
513 (value :scs ,scs :target result))
516 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
518 (:constant (constant-displacement other-pointer-lowtag
522 (:results (result :scs ,scs))
523 (:result-types ,el-type)
524 (:generator 3 ; was 5
525 (inst mov (make-ea :qword :base object
526 :disp (- (* (+ ,offset index offset) n-word-bytes)
529 (move result value)))))
531 ;;; helper for alien stuff.
533 (def!macro with-pinned-objects ((&rest objects) &body body)
534 "Arrange with the garbage collector that the pages occupied by
535 OBJECTS will not be moved in memory for the duration of BODY.
536 Useful for e.g. foreign calls where another thread may trigger
539 (let ((pins (make-gensym-list (length objects)))
540 (wpo (block-gensym "WPO")))
541 ;; BODY is stuffed in a function to preserve the lexical
543 `(flet ((,wpo () (progn ,@body)))
544 ;; PINS are dx-allocated in case the compiler for some
545 ;; unfathomable reason decides to allocate value-cells
546 ;; for them -- since we have DX value-cells on x86oid
547 ;; platforms this still forces them on the stack.
548 (dx-let ,(mapcar #'list pins objects)
549 (multiple-value-prog1 (,wpo)
550 ;; TOUCH-OBJECT has a VOP with an empty body: compiler
551 ;; thinks we're using the argument and doesn't flush
552 ;; the variable, but we don't have to pay any extra
553 ;; beyond that -- and MULTIPLE-VALUE-PROG1 keeps them
554 ;; live till the body has finished. *whew*
555 ,@(mapcar (lambda (pin)
556 `(touch-object ,pin))