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)
22 (inst mov ,n-dst ,n-src))))
24 (defmacro make-ea-for-object-slot (ptr slot lowtag)
25 `(make-ea :qword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
26 (defmacro make-ea-for-object-slot-half (ptr slot lowtag)
27 `(make-ea :dword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
29 (defmacro loadw (value ptr &optional (slot 0) (lowtag 0))
30 `(inst mov ,value (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
32 (defmacro storew (value ptr &optional (slot 0) (lowtag 0))
33 (once-only ((value value))
34 `(cond ((and (integerp ,value)
35 (not (typep ,value '(signed-byte 32))))
36 (multiple-value-bind (lo hi) (dwords-for-quad ,value)
37 (inst mov (make-ea-for-object-slot-half
38 ,ptr ,slot ,lowtag) lo)
39 (inst mov (make-ea-for-object-slot-half
40 ,ptr (+ ,slot 1/2) ,lowtag) hi)))
42 (inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) ,value)))))
44 (defmacro pushw (ptr &optional (slot 0) (lowtag 0))
45 `(inst push (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
47 (defmacro popw (ptr &optional (slot 0) (lowtag 0))
48 `(inst pop (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
50 ;;;; macros to generate useful values
52 (defmacro load-symbol (reg symbol)
53 `(inst mov ,reg (+ nil-value (static-symbol-offset ,symbol))))
55 (defmacro load-symbol-value (reg symbol)
59 (static-symbol-offset ',symbol)
60 (ash symbol-value-slot word-shift)
61 (- other-pointer-lowtag)))))
63 (defmacro store-symbol-value (reg symbol)
67 (static-symbol-offset ',symbol)
68 (ash symbol-value-slot word-shift)
69 (- other-pointer-lowtag)))
73 (defmacro load-tl-symbol-value (reg symbol)
78 (static-symbol-offset ',symbol)
79 (ash symbol-tls-index-slot word-shift)
80 (- other-pointer-lowtag))))
81 (inst fs-segment-prefix)
82 (inst mov ,reg (make-ea :qword :scale 1 :index ,reg))))
84 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
87 (defmacro store-tl-symbol-value (reg symbol temp)
92 (static-symbol-offset ',symbol)
93 (ash symbol-tls-index-slot word-shift)
94 (- other-pointer-lowtag))))
95 (inst fs-segment-prefix)
96 (inst mov (make-ea :qword :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-type (target source &optional (offset 0))
104 "Loads the type bits of a pointer into target independent of
105 byte-ordering issues."
106 (once-only ((n-target target)
109 (ecase *backend-byte-order*
112 (make-ea :byte :base ,n-source :disp ,n-offset)))
115 (make-ea :byte :base ,n-source :disp (+ ,n-offset 4)))))))
117 ;;;; allocation helpers
119 ;;; All allocation is done by calls to assembler routines that
120 ;;; eventually invoke the C alloc() function.
122 ;;; Emit code to allocate an object with a size in bytes given by
123 ;;; Size. The size may be an integer of a TN. If Inline is a VOP
124 ;;; node-var then it is used to make an appropriate speed vs size
127 (defun allocation-dynamic-extent (alloc-tn size)
128 (inst sub rsp-tn size)
129 ;; see comment in x86/macros.lisp implementation of this
130 (inst and rsp-tn #.(lognot lowtag-mask))
131 (aver (not (location= alloc-tn rsp-tn)))
132 (inst mov alloc-tn rsp-tn)
135 ;;; This macro should only be used inside a pseudo-atomic section,
136 ;;; which should also cover subsequent initialization of the
138 (defun allocation-tramp (alloc-tn size &optional ignored)
139 (declare (ignore ignored))
141 (inst lea r13-tn (make-ea :qword
142 :disp (make-fixup "alloc_tramp" :foreign)))
147 (defun allocation (alloc-tn size &optional ignored dynamic-extent)
148 (declare (ignore ignored))
150 (allocation-dynamic-extent alloc-tn size)
151 (return-from allocation (values)))
152 (let ((NOT-INLINE (gen-label))
155 (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
157 (make-ea :qword :disp
158 #!+sb-thread (* n-word-bytes thread-alloc-region-slot)
159 #!-sb-thread (make-fixup "boxed_region" :foreign)
160 :scale 1)) ; thread->alloc_region.free_pointer
162 (make-ea :qword :disp
163 #!+sb-thread (* n-word-bytes (1+ thread-alloc-region-slot))
164 #!-sb-thread (make-fixup "boxed_region" :foreign 8)
165 :scale 1))) ; thread->alloc_region.end_addr
167 (allocation-tramp alloc-tn size))
169 (unless (and (tn-p size) (location= alloc-tn size))
170 (inst mov alloc-tn size))
171 #!+sb-thread (inst fs-segment-prefix)
172 (inst add alloc-tn free-pointer)
173 #!+sb-thread (inst fs-segment-prefix)
174 (inst cmp end-addr alloc-tn)
175 (inst jmp :be NOT-INLINE)
176 #!+sb-thread (inst fs-segment-prefix)
177 (inst xchg free-pointer alloc-tn)
179 (assemble (*elsewhere*)
180 (emit-label NOT-INLINE)
181 (cond ((numberp size)
182 (allocation-tramp alloc-tn size))
184 (inst sub alloc-tn free-pointer)
185 (allocation-tramp alloc-tn alloc-tn)))
190 (defun allocation (alloc-tn size &optional ignored)
191 (declare (ignore ignored))
193 (inst lea r13-tn (make-ea :qword
194 :disp (make-fixup "alloc_tramp" :foreign)))
199 ;;; Allocate an other-pointer object of fixed SIZE with a single word
200 ;;; header having the specified WIDETAG value. The result is placed in
202 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline)
205 (bug "empty &body in WITH-FIXED-ALLOCATION"))
206 (once-only ((result-tn result-tn) (size size))
208 (allocation ,result-tn (pad-data-block ,size) ,inline)
209 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
212 (make-ea :qword :base ,result-tn :disp other-pointer-lowtag))
216 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
217 (defun emit-error-break (vop kind code values)
218 (let ((vector (gensym)))
219 `((inst int 3) ; i386 breakpoint instruction
220 ;; The return PC points here; note the location for the debugger.
223 (note-this-location vop :internal-error)))
224 (inst byte ,kind) ; eg trap_Xyyy
225 (with-adjustable-vector (,vector) ; interr arguments
226 (write-var-integer (error-number-or-lose ',code) ,vector)
227 ,@(mapcar (lambda (tn)
229 ;; classic CMU CL comment:
230 ;; zzzzz jrd here. tn-offset is zero for constant
232 (write-var-integer (make-sc-offset (sc-number
238 (inst byte (length ,vector))
239 (dotimes (i (length ,vector))
240 (inst byte (aref ,vector i))))))))
242 (defmacro error-call (vop error-code &rest values)
244 "Cause an error. ERROR-CODE is the error to cause."
246 (emit-error-break vop error-trap error-code values)))
248 (defmacro generate-error-code (vop error-code &rest values)
250 "Generate-Error-Code Error-code Value*
251 Emit code for an error with the specified Error-Code and context Values."
252 `(assemble (*elsewhere*)
253 (let ((start-lab (gen-label)))
254 (emit-label start-lab)
255 (error-call ,vop ,error-code ,@values)
261 ;;; This is used to wrap operations which leave untagged memory lying
262 ;;; around. It's an operation which the AOP weenies would describe as
263 ;;; having "cross-cutting concerns", meaning it appears all over the
264 ;;; place and there's no logical single place to attach documentation.
265 ;;; grep (mostly in src/runtime) is your friend
267 ;;; FIXME: *PSEUDO-ATOMIC-FOO* could be made into *PSEUDO-ATOMIC-BITS*,
268 ;;; set with a single operation and cleared with SHR *PSEUDO-ATOMIC-BITS*,-2;
269 ;;; the ATOMIC bit is bit 0, the INTERRUPTED bit is bit 1, and you check
270 ;;; the C flag after the shift to see whether you were interrupted.
272 ;;; FIXME: THIS NAME IS BACKWARDS!
273 (defmacro maybe-pseudo-atomic (really-p &body body)
276 (pseudo-atomic ,@body)))
278 (defmacro pseudo-atomic (&rest forms)
279 (with-unique-names (label)
280 `(let ((,label (gen-label)))
281 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
282 ;; something. (perhaps SVLB, for static variable low byte)
283 (inst mov (make-ea :byte :disp (+ nil-value
284 (static-symbol-offset
285 '*pseudo-atomic-interrupted*)
286 (ash symbol-value-slot word-shift)
287 ;; FIXME: Use mask, not minus, to
288 ;; take out type bits.
289 (- other-pointer-lowtag)))
291 (inst mov (make-ea :byte :disp (+ nil-value
292 (static-symbol-offset
293 '*pseudo-atomic-atomic*)
294 (ash symbol-value-slot word-shift)
295 (- other-pointer-lowtag)))
298 (inst mov (make-ea :byte :disp (+ nil-value
299 (static-symbol-offset
300 '*pseudo-atomic-atomic*)
301 (ash symbol-value-slot word-shift)
302 (- other-pointer-lowtag)))
304 ;; KLUDGE: Is there any requirement for interrupts to be
305 ;; handled in order? It seems as though an interrupt coming
306 ;; in at this point will be executed before any pending interrupts.
307 ;; Or do incoming interrupts check to see whether any interrupts
308 ;; are pending? I wish I could find the documentation for
309 ;; pseudo-atomics.. -- WHN 19991130
310 (inst cmp (make-ea :byte
312 (static-symbol-offset
313 '*pseudo-atomic-interrupted*)
314 (ash symbol-value-slot word-shift)
315 (- other-pointer-lowtag)))
317 (inst jmp :eq ,label)
318 ;; if PAI was set, interrupts were disabled at the same time
319 ;; using the process signal mask.
320 (inst break pending-interrupt-trap)
321 (emit-label ,label))))
325 ;;;; indexed references
327 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
331 `((:translate ,translate)))
333 (:args (object :scs (descriptor-reg))
334 (index :scs (any-reg)))
335 (:arg-types ,type tagged-num)
336 (:results (value :scs ,scs))
337 (:result-types ,el-type)
338 (:generator 3 ; pw was 5
339 (inst mov value (make-ea :qword :base object :index index
340 :disp (- (* ,offset n-word-bytes)
342 (define-vop (,(symbolicate name "-C"))
344 `((:translate ,translate)))
346 (:args (object :scs (descriptor-reg)))
349 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
351 (:results (value :scs ,scs))
352 (:result-types ,el-type)
353 (:generator 2 ; pw was 5
354 (inst mov value (make-ea :qword :base object
355 :disp (- (* (+ ,offset index) n-word-bytes)
358 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
362 `((:translate ,translate)))
364 (:args (object :scs (descriptor-reg))
365 (index :scs (any-reg))
366 (value :scs ,scs :target result))
367 (:arg-types ,type tagged-num ,el-type)
368 (:results (result :scs ,scs))
369 (:result-types ,el-type)
370 (:generator 4 ; was 5
371 (inst mov (make-ea :qword :base object :index index
372 :disp (- (* ,offset n-word-bytes) ,lowtag))
374 (move result value)))
375 (define-vop (,(symbolicate name "-C"))
377 `((:translate ,translate)))
379 (:args (object :scs (descriptor-reg))
380 (value :scs ,scs :target result))
383 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
386 (:results (result :scs ,scs))
387 (:result-types ,el-type)
388 (:generator 3 ; was 5
389 (inst mov (make-ea :qword :base object
390 :disp (- (* (+ ,offset index) n-word-bytes)
393 (move result value)))))
395 ;;; helper for alien stuff.
396 (defmacro with-pinned-objects ((&rest objects) &body body)
397 "Arrange with the garbage collector that the pages occupied by
398 OBJECTS will not be moved in memory for the duration of BODY.
399 Useful for e.g. foreign calls where another thread may trigger
401 `(multiple-value-prog1
403 ,@(loop for p in objects
404 collect `(push-word-on-c-stack
405 (int-sap (sb!kernel:get-lisp-obj-address ,p))))
407 ;; If the body returned normally, we should restore the stack pointer
408 ;; for the benefit of any following code in the same function. If
409 ;; there's a non-local exit in the body, sp is garbage anyway and
410 ;; will get set appropriately from {a, the} frame pointer before it's
412 (pop-words-from-c-stack ,(length objects))))