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 :disp (+ ,n-offset 4)))))))
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)
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 mov alloc-tn rsp-tn)
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 &optional ignored)
152 (declare (ignore ignored))
154 (inst lea temp-reg-tn (make-ea :qword
155 :disp (make-fixup "alloc_tramp" :foreign)))
156 (inst call temp-reg-tn)
160 (defun allocation (alloc-tn size &optional ignored dynamic-extent)
161 (declare (ignore ignored))
163 (allocation-dynamic-extent alloc-tn size)
164 (return-from allocation (values)))
165 (let ((NOT-INLINE (gen-label))
168 (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
169 ;; thread->alloc_region.free_pointer
173 :base thread-base-tn :scale 1
174 :disp (* n-word-bytes thread-alloc-region-slot))
178 (make-fixup "boxed_region" :foreign)))
179 ;; thread->alloc_region.end_addr
183 :base thread-base-tn :scale 1
184 :disp (* n-word-bytes (1+ thread-alloc-region-slot)))
188 (make-fixup "boxed_region" :foreign 8))))
190 (allocation-tramp alloc-tn size))
192 (inst mov temp-reg-tn free-pointer)
194 (if (location= alloc-tn size)
195 (inst add alloc-tn temp-reg-tn)
197 (make-ea :qword :base temp-reg-tn :index size)))
199 (make-ea :qword :base temp-reg-tn :disp size)))
200 (inst cmp end-addr alloc-tn)
201 (inst jmp :be NOT-INLINE)
202 (inst mov free-pointer alloc-tn)
203 (inst mov alloc-tn temp-reg-tn)
205 (assemble (*elsewhere*)
206 (emit-label NOT-INLINE)
207 (cond ((numberp size)
208 (allocation-tramp alloc-tn size))
210 (inst sub alloc-tn free-pointer)
211 (allocation-tramp alloc-tn alloc-tn)))
216 (defun allocation (alloc-tn size &optional ignored)
217 (declare (ignore ignored))
219 (inst lea temp-reg-tn (make-ea :qword
220 :disp (make-fixup "alloc_tramp" :foreign)))
221 (inst call temp-reg-tn)
225 ;;; Allocate an other-pointer object of fixed SIZE with a single word
226 ;;; header having the specified WIDETAG value. The result is placed in
228 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline)
231 (bug "empty &body in WITH-FIXED-ALLOCATION"))
232 (once-only ((result-tn result-tn) (size size))
234 (allocation ,result-tn (pad-data-block ,size) ,inline)
235 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
238 (make-ea :qword :base ,result-tn :disp other-pointer-lowtag))
242 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
243 (defun emit-error-break (vop kind code values)
244 (let ((vector (gensym)))
246 #!-darwin (inst int 3) ; i386 breakpoint instruction
247 ;; On Darwin, we need to use #x0b0f instead of int3 in order
248 ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
249 ;; doesn't seem to be reliably firing SIGTRAP
250 ;; handlers. Hopefully this will be fixed by Apple at a
251 ;; later date. Do the same on x86-64 as we do on x86 until this gets
253 #!+darwin (inst word #x0b0f))
255 ;; The return PC points here; note the location for the debugger.
258 (note-this-location vop :internal-error)))
259 (inst byte ,kind) ; eg trap_Xyyy
260 (with-adjustable-vector (,vector) ; interr arguments
261 (write-var-integer (error-number-or-lose ',code) ,vector)
262 ,@(mapcar (lambda (tn)
264 ;; classic CMU CL comment:
265 ;; zzzzz jrd here. tn-offset is zero for constant
267 (write-var-integer (make-sc-offset (sc-number
273 (inst byte (length ,vector))
274 (dotimes (i (length ,vector))
275 (inst byte (aref ,vector i))))))))
277 (defmacro error-call (vop error-code &rest values)
279 "Cause an error. ERROR-CODE is the error to cause."
281 (emit-error-break vop error-trap error-code values)))
283 (defmacro generate-error-code (vop error-code &rest values)
285 "Generate-Error-Code Error-code Value*
286 Emit code for an error with the specified Error-Code and context Values."
287 `(assemble (*elsewhere*)
288 (let ((start-lab (gen-label)))
289 (emit-label start-lab)
290 (error-call ,vop ,error-code ,@values)
296 ;;; This is used to wrap operations which leave untagged memory lying
297 ;;; around. It's an operation which the AOP weenies would describe as
298 ;;; having "cross-cutting concerns", meaning it appears all over the
299 ;;; place and there's no logical single place to attach documentation.
300 ;;; grep (mostly in src/runtime) is your friend
302 ;;; FIXME: THIS NAME IS BACKWARDS!
303 (defmacro maybe-pseudo-atomic (really-p &body body)
306 (pseudo-atomic ,@body)))
309 (defmacro pseudo-atomic (&rest forms)
310 (with-unique-names (label)
311 `(let ((,label (gen-label)))
312 (inst or (make-ea :byte
314 :disp (* 8 thread-pseudo-atomic-bits-slot))
317 (inst xor (make-ea :byte
319 :disp (* 8 thread-pseudo-atomic-bits-slot))
322 ;; if PAI was set, interrupts were disabled at the same
323 ;; time using the process signal mask.
324 (inst break pending-interrupt-trap)
325 (emit-label ,label))))
329 (defmacro pseudo-atomic (&rest forms)
330 (with-unique-names (label)
331 `(let ((,label (gen-label)))
332 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
333 ;; something. (perhaps SVLB, for static variable low byte)
334 (inst or (make-ea :byte :disp (+ nil-value
335 (static-symbol-offset
336 '*pseudo-atomic-bits*)
337 (ash symbol-value-slot word-shift)
338 (- other-pointer-lowtag)))
341 (inst xor (make-ea :byte :disp (+ nil-value
342 (static-symbol-offset
343 '*pseudo-atomic-bits*)
344 (ash symbol-value-slot word-shift)
345 (- other-pointer-lowtag)))
348 ;; if PAI was set, interrupts were disabled at the same time
349 ;; using the process signal mask.
350 (inst break pending-interrupt-trap)
351 (emit-label ,label))))
355 ;;;; indexed references
357 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
361 `((:translate ,translate)))
363 (:args (object :scs (descriptor-reg))
364 (index :scs (any-reg)))
365 (:arg-types ,type tagged-num)
366 (:results (value :scs ,scs))
367 (:result-types ,el-type)
368 (:generator 3 ; pw was 5
369 (inst mov value (make-ea :qword :base object :index index
370 :disp (- (* ,offset n-word-bytes)
372 (define-vop (,(symbolicate name "-C"))
374 `((:translate ,translate)))
376 (:args (object :scs (descriptor-reg)))
379 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
381 (:results (value :scs ,scs))
382 (:result-types ,el-type)
383 (:generator 2 ; pw was 5
384 (inst mov value (make-ea :qword :base object
385 :disp (- (* (+ ,offset index) n-word-bytes)
388 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
392 `((:translate ,translate)))
394 (:args (object :scs (descriptor-reg))
395 (index :scs (any-reg))
396 (value :scs ,scs :target result))
397 (:arg-types ,type tagged-num ,el-type)
398 (:results (result :scs ,scs))
399 (:result-types ,el-type)
400 (:generator 4 ; was 5
401 (inst mov (make-ea :qword :base object :index index
402 :disp (- (* ,offset n-word-bytes) ,lowtag))
404 (move result value)))
405 (define-vop (,(symbolicate name "-C"))
407 `((:translate ,translate)))
409 (:args (object :scs (descriptor-reg))
410 (value :scs ,scs :target result))
413 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
416 (:results (result :scs ,scs))
417 (:result-types ,el-type)
418 (:generator 3 ; was 5
419 (inst mov (make-ea :qword :base object
420 :disp (- (* (+ ,offset index) n-word-bytes)
423 (move result value)))))
425 ;;; helper for alien stuff.
426 (def!macro with-pinned-objects ((&rest objects) &body body)
427 "Arrange with the garbage collector that the pages occupied by
428 OBJECTS will not be moved in memory for the duration of BODY.
429 Useful for e.g. foreign calls where another thread may trigger
431 `(multiple-value-prog1
433 ,@(loop for p in objects
434 collect `(push-word-on-c-stack
435 (int-sap (sb!kernel:get-lisp-obj-address ,p))))
437 ;; If the body returned normally, we should restore the stack pointer
438 ;; for the benefit of any following code in the same function. If
439 ;; there's a non-local exit in the body, sp is garbage anyway and
440 ;; will get set appropriately from {a, the} frame pointer before it's
442 (pop-words-from-c-stack ,(length objects))))