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 lowtag)
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 lea alloc-tn (make-ea :byte :base rsp-tn :disp lowtag))
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 lowtag)
154 (inst lea temp-reg-tn (make-ea :qword
155 :disp (make-fixup "alloc_tramp" :foreign)))
156 (inst call temp-reg-tn)
159 (inst lea alloc-tn (make-ea :byte :base alloc-tn :disp lowtag)))
162 (defun allocation (alloc-tn size &optional ignored dynamic-extent lowtag)
163 (declare (ignore ignored))
165 (allocation-dynamic-extent alloc-tn size lowtag)
166 (return-from allocation (values)))
167 (let ((NOT-INLINE (gen-label))
170 (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
171 ;; thread->alloc_region.free_pointer
175 :base thread-base-tn :scale 1
176 :disp (* n-word-bytes thread-alloc-region-slot))
180 (make-fixup "boxed_region" :foreign)))
181 ;; thread->alloc_region.end_addr
185 :base thread-base-tn :scale 1
186 :disp (* n-word-bytes (1+ thread-alloc-region-slot)))
190 (make-fixup "boxed_region" :foreign 8))))
192 (allocation-tramp alloc-tn size lowtag))
194 (inst mov temp-reg-tn free-pointer)
196 (if (location= alloc-tn size)
197 (inst add alloc-tn temp-reg-tn)
199 (make-ea :qword :base temp-reg-tn :index size)))
201 (make-ea :qword :base temp-reg-tn :disp size)))
202 (inst cmp end-addr alloc-tn)
203 (inst jmp :be NOT-INLINE)
204 (inst mov free-pointer alloc-tn)
206 (inst lea alloc-tn (make-ea :byte :base temp-reg-tn :disp lowtag))
207 (inst mov alloc-tn temp-reg-tn))
209 (assemble (*elsewhere*)
210 (emit-label NOT-INLINE)
211 (cond ((numberp size)
212 (allocation-tramp alloc-tn size lowtag))
214 (inst sub alloc-tn free-pointer)
215 (allocation-tramp alloc-tn alloc-tn lowtag)))
219 ;;; Allocate an other-pointer object of fixed SIZE with a single word
220 ;;; header having the specified WIDETAG value. The result is placed in
222 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline stack-allocate-p)
225 (bug "empty &body in WITH-FIXED-ALLOCATION"))
226 (once-only ((result-tn result-tn) (size size) (stack-allocate-p stack-allocate-p))
227 `(maybe-pseudo-atomic ,stack-allocate-p
228 (allocation ,result-tn (pad-data-block ,size) ,inline ,stack-allocate-p
229 other-pointer-lowtag)
230 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
231 ,result-tn 0 other-pointer-lowtag)
235 (defun emit-error-break (vop kind code values)
238 (inst int 3) ; i386 breakpoint instruction
239 ;; On Darwin, we need to use #x0b0f instead of int3 in order
240 ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
241 ;; doesn't seem to be reliably firing SIGTRAP
242 ;; handlers. Hopefully this will be fixed by Apple at a
243 ;; later date. Do the same on x86-64 as we do on x86 until this gets
247 ;; The return PC points here; note the location for the debugger.
249 (note-this-location vop :internal-error))
250 (inst byte kind) ; eg trap_Xyyy
251 (with-adjustable-vector (vector) ; interr arguments
252 (write-var-integer code vector)
254 ;; classic CMU CL comment:
255 ;; zzzzz jrd here. tn-offset is zero for constant
257 (write-var-integer (make-sc-offset (sc-number (tn-sc tn))
258 (or (tn-offset tn) 0))
260 (inst byte (length vector))
261 (dotimes (i (length vector))
262 (inst byte (aref vector i))))))
264 (defun error-call (vop error-code &rest values)
266 "Cause an error. ERROR-CODE is the error to cause."
267 (emit-error-break vop error-trap (error-number-or-lose error-code) values))
269 (defun generate-error-code (vop error-code &rest values)
271 "Generate-Error-Code Error-code Value*
272 Emit code for an error with the specified Error-Code and context Values."
273 (assemble (*elsewhere*)
274 (let ((start-lab (gen-label)))
275 (emit-label start-lab)
276 (emit-error-break vop error-trap (error-number-or-lose error-code) values)
282 ;;; This is used to wrap operations which leave untagged memory lying
283 ;;; around. It's an operation which the AOP weenies would describe as
284 ;;; having "cross-cutting concerns", meaning it appears all over the
285 ;;; place and there's no logical single place to attach documentation.
286 ;;; grep (mostly in src/runtime) is your friend
288 (defmacro maybe-pseudo-atomic (not-really-p &body body)
291 (pseudo-atomic ,@body)))
294 (defmacro pseudo-atomic (&rest forms)
295 (with-unique-names (label)
296 `(let ((,label (gen-label)))
297 (inst or (make-ea :byte
299 :disp (* 8 thread-pseudo-atomic-bits-slot))
302 (inst xor (make-ea :byte
304 :disp (* 8 thread-pseudo-atomic-bits-slot))
307 ;; if PAI was set, interrupts were disabled at the same
308 ;; time using the process signal mask.
309 (inst break pending-interrupt-trap)
310 (emit-label ,label))))
314 (defmacro pseudo-atomic (&rest forms)
315 (with-unique-names (label)
316 `(let ((,label (gen-label)))
317 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
318 ;; something. (perhaps SVLB, for static variable low byte)
319 (inst or (make-ea :byte :disp (+ nil-value
320 (static-symbol-offset
321 '*pseudo-atomic-bits*)
322 (ash symbol-value-slot word-shift)
323 (- other-pointer-lowtag)))
326 (inst xor (make-ea :byte :disp (+ nil-value
327 (static-symbol-offset
328 '*pseudo-atomic-bits*)
329 (ash symbol-value-slot word-shift)
330 (- other-pointer-lowtag)))
333 ;; if PAI was set, interrupts were disabled at the same time
334 ;; using the process signal mask.
335 (inst break pending-interrupt-trap)
336 (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 ;; PINS are dx-allocated in case the compiler for some
527 ;; unfathomable reason decides to allocate value-cells
528 ;; for them -- since we have DX value-cells on x86oid
529 ;; platforms this still forces them on the stack.
530 (dx-let ,(mapcar #'list pins objects)
531 (multiple-value-prog1 (,wpo)
532 ;; TOUCH-OBJECT has a VOP with an empty body: compiler
533 ;; thinks we're using the argument and doesn't flush
534 ;; the variable, but we don't have to pay any extra
535 ;; beyond that -- and MULTIPLE-VALUE-PROG1 keeps them
536 ;; live till the body has finished. *whew*
537 ,@(mapcar (lambda (pin)
538 `(touch-object ,pin))