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 ;;; This macro should only be used inside a pseudo-atomic section,
128 ;;; which should also cover subsequent initialization of the
130 (defun allocation-tramp (alloc-tn size &optional ignored)
131 (declare (ignore ignored))
133 (inst lea r13-tn (make-ea :qword
134 :disp (make-fixup "alloc_tramp" :foreign)))
139 (defun allocation (alloc-tn size &optional ignored)
140 (declare (ignore ignored))
141 (let ((NOT-INLINE (gen-label))
144 (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
146 (make-ea :qword :disp
147 #!+sb-thread (* n-word-bytes thread-alloc-region-slot)
148 #!-sb-thread (make-fixup "boxed_region" :foreign)
149 :scale 1)) ; thread->alloc_region.free_pointer
151 (make-ea :qword :disp
152 #!+sb-thread (* n-word-bytes (1+ thread-alloc-region-slot))
153 #!-sb-thread (make-fixup "boxed_region" :foreign 8)
154 :scale 1))) ; thread->alloc_region.end_addr
156 (allocation-tramp alloc-tn size))
158 (unless (and (tn-p size) (location= alloc-tn size))
159 (inst mov alloc-tn size))
160 #!+sb-thread (inst fs-segment-prefix)
161 (inst add alloc-tn free-pointer)
162 #!+sb-thread (inst fs-segment-prefix)
163 (inst cmp end-addr alloc-tn)
164 (inst jmp :be NOT-INLINE)
165 #!+sb-thread (inst fs-segment-prefix)
166 (inst xchg free-pointer alloc-tn)
168 (assemble (*elsewhere*)
169 (emit-label NOT-INLINE)
170 (cond ((numberp size)
171 (allocation-tramp alloc-tn size))
173 (inst sub alloc-tn free-pointer)
174 (allocation-tramp alloc-tn alloc-tn)))
179 (defun allocation (alloc-tn size &optional ignored)
180 (declare (ignore ignored))
182 (inst lea r13-tn (make-ea :qword
183 :disp (make-fixup "alloc_tramp" :foreign)))
188 ;;; Allocate an other-pointer object of fixed SIZE with a single word
189 ;;; header having the specified WIDETAG value. The result is placed in
191 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline)
194 (bug "empty &body in WITH-FIXED-ALLOCATION"))
195 (once-only ((result-tn result-tn) (size size))
197 (allocation ,result-tn (pad-data-block ,size) ,inline)
198 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
201 (make-ea :qword :base ,result-tn :disp other-pointer-lowtag))
205 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
206 (defun emit-error-break (vop kind code values)
207 (let ((vector (gensym)))
208 `((inst int 3) ; i386 breakpoint instruction
209 ;; The return PC points here; note the location for the debugger.
212 (note-this-location vop :internal-error)))
213 (inst byte ,kind) ; eg trap_Xyyy
214 (with-adjustable-vector (,vector) ; interr arguments
215 (write-var-integer (error-number-or-lose ',code) ,vector)
216 ,@(mapcar (lambda (tn)
218 ;; classic CMU CL comment:
219 ;; zzzzz jrd here. tn-offset is zero for constant
221 (write-var-integer (make-sc-offset (sc-number
227 (inst byte (length ,vector))
228 (dotimes (i (length ,vector))
229 (inst byte (aref ,vector i))))))))
231 (defmacro error-call (vop error-code &rest values)
233 "Cause an error. ERROR-CODE is the error to cause."
235 (emit-error-break vop error-trap error-code values)))
237 (defmacro generate-error-code (vop error-code &rest values)
239 "Generate-Error-Code Error-code Value*
240 Emit code for an error with the specified Error-Code and context Values."
241 `(assemble (*elsewhere*)
242 (let ((start-lab (gen-label)))
243 (emit-label start-lab)
244 (error-call ,vop ,error-code ,@values)
250 ;;; This is used to wrap operations which leave untagged memory lying
251 ;;; around. It's an operation which the AOP weenies would describe as
252 ;;; having "cross-cutting concerns", meaning it appears all over the
253 ;;; place and there's no logical single place to attach documentation.
254 ;;; grep (mostly in src/runtime) is your friend
256 ;;; FIXME: *PSEUDO-ATOMIC-FOO* could be made into *PSEUDO-ATOMIC-BITS*,
257 ;;; set with a single operation and cleared with SHR *PSEUDO-ATOMIC-BITS*,-2;
258 ;;; the ATOMIC bit is bit 0, the INTERRUPTED bit is bit 1, and you check
259 ;;; the C flag after the shift to see whether you were interrupted.
261 (defmacro pseudo-atomic (&rest forms)
262 (with-unique-names (label)
263 `(let ((,label (gen-label)))
264 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
265 ;; something. (perhaps SVLB, for static variable low byte)
266 (inst mov (make-ea :byte :disp (+ nil-value
267 (static-symbol-offset
268 '*pseudo-atomic-interrupted*)
269 (ash symbol-value-slot word-shift)
270 ;; FIXME: Use mask, not minus, to
271 ;; take out type bits.
272 (- other-pointer-lowtag)))
274 (inst mov (make-ea :byte :disp (+ nil-value
275 (static-symbol-offset
276 '*pseudo-atomic-atomic*)
277 (ash symbol-value-slot word-shift)
278 (- other-pointer-lowtag)))
281 (inst mov (make-ea :byte :disp (+ nil-value
282 (static-symbol-offset
283 '*pseudo-atomic-atomic*)
284 (ash symbol-value-slot word-shift)
285 (- other-pointer-lowtag)))
287 ;; KLUDGE: Is there any requirement for interrupts to be
288 ;; handled in order? It seems as though an interrupt coming
289 ;; in at this point will be executed before any pending interrupts.
290 ;; Or do incoming interrupts check to see whether any interrupts
291 ;; are pending? I wish I could find the documentation for
292 ;; pseudo-atomics.. -- WHN 19991130
293 (inst cmp (make-ea :byte
295 (static-symbol-offset
296 '*pseudo-atomic-interrupted*)
297 (ash symbol-value-slot word-shift)
298 (- other-pointer-lowtag)))
300 (inst jmp :eq ,label)
301 ;; if PAI was set, interrupts were disabled at the same time
302 ;; using the process signal mask.
303 (inst break pending-interrupt-trap)
304 (emit-label ,label))))
308 ;;;; indexed references
310 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
314 `((:translate ,translate)))
316 (:args (object :scs (descriptor-reg))
317 (index :scs (any-reg)))
318 (:arg-types ,type tagged-num)
319 (:results (value :scs ,scs))
320 (:result-types ,el-type)
321 (:generator 3 ; pw was 5
322 (inst mov value (make-ea :qword :base object :index index
323 :disp (- (* ,offset n-word-bytes)
325 (define-vop (,(symbolicate name "-C"))
327 `((:translate ,translate)))
329 (:args (object :scs (descriptor-reg)))
332 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
334 (:results (value :scs ,scs))
335 (:result-types ,el-type)
336 (:generator 2 ; pw was 5
337 (inst mov value (make-ea :qword :base object
338 :disp (- (* (+ ,offset index) n-word-bytes)
341 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
345 `((:translate ,translate)))
347 (:args (object :scs (descriptor-reg))
348 (index :scs (any-reg))
349 (value :scs ,scs :target result))
350 (:arg-types ,type tagged-num ,el-type)
351 (:results (result :scs ,scs))
352 (:result-types ,el-type)
353 (:generator 4 ; was 5
354 (inst mov (make-ea :qword :base object :index index
355 :disp (- (* ,offset n-word-bytes) ,lowtag))
357 (move result value)))
358 (define-vop (,(symbolicate name "-C"))
360 `((:translate ,translate)))
362 (:args (object :scs (descriptor-reg))
363 (value :scs ,scs :target result))
366 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
369 (:results (result :scs ,scs))
370 (:result-types ,el-type)
371 (:generator 3 ; was 5
372 (inst mov (make-ea :qword :base object
373 :disp (- (* (+ ,offset index) n-word-bytes)
376 (move result value)))))
378 ;;; helper for alien stuff.
379 (defmacro with-pinned-objects ((&rest objects) &body body)
380 "Arrange with the garbage collector that the pages occupied by
381 OBJECTS will not be moved in memory for the duration of BODY.
382 Useful for e.g. foreign calls where another thread may trigger
384 `(multiple-value-prog1
386 ,@(loop for p in objects
387 collect `(push-word-on-c-stack
388 (int-sap (sb!kernel:get-lisp-obj-address ,p))))
390 ;; If the body returned normally, we should restore the stack pointer
391 ;; for the benefit of any following code in the same function. If
392 ;; there's a non-local exit in the body, sp is garbage anyway and
393 ;; will get set appropriately from {a, the} frame pointer before it's
395 (pop-words-from-c-stack ,(length objects))))