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 mov
,reg
(make-ea :qword
:base thread-base-tn
:scale
1 :index
,reg
))))
83 (defmacro load-tl-symbol-value
(reg symbol
) `(load-symbol-value ,reg
,symbol
))
86 (defmacro store-tl-symbol-value
(reg symbol temp
)
91 (static-symbol-offset ',symbol
)
92 (ash symbol-tls-index-slot word-shift
)
93 (- other-pointer-lowtag
))))
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-type
(target source
&optional
(offset 0))
102 "Loads the type bits of a pointer into target independent of
103 byte-ordering issues."
104 (once-only ((n-target target
)
107 (ecase *backend-byte-order
*
110 (make-ea :byte
:base
,n-source
:disp
,n-offset
)))
113 (make-ea :byte
:base
,n-source
:disp
(+ ,n-offset
4)))))))
115 ;;;; allocation helpers
117 ;;; All allocation is done by calls to assembler routines that
118 ;;; eventually invoke the C alloc() function.
120 ;;; Emit code to allocate an object with a size in bytes given by
121 ;;; Size. The size may be an integer of a TN. If Inline is a VOP
122 ;;; node-var then it is used to make an appropriate speed vs size
125 (defun allocation-dynamic-extent (alloc-tn size
)
126 (inst sub rsp-tn size
)
127 ;; see comment in x86/macros.lisp implementation of this
128 (inst and rsp-tn
#.
(lognot lowtag-mask
))
129 (aver (not (location= alloc-tn rsp-tn
)))
130 (inst mov alloc-tn rsp-tn
)
133 ;;; This macro should only be used inside a pseudo-atomic section,
134 ;;; which should also cover subsequent initialization of the
136 (defun allocation-tramp (alloc-tn size
&optional ignored
)
137 (declare (ignore ignored
))
139 (inst lea r13-tn
(make-ea :qword
140 :disp
(make-fixup "alloc_tramp" :foreign
)))
145 (defun allocation (alloc-tn size
&optional ignored dynamic-extent
)
146 (declare (ignore ignored
))
148 (allocation-dynamic-extent alloc-tn size
)
149 (return-from allocation
(values)))
150 (let ((NOT-INLINE (gen-label))
153 (in-elsewhere (eq *elsewhere
* sb
!assem
::**current-segment
**))
154 ;; thread->alloc_region.free_pointer
158 :base thread-base-tn
:scale
1
159 :disp
(* n-word-bytes thread-alloc-region-slot
))
163 (make-fixup (extern-alien-name "boxed_region") :foreign
)))
164 ;; thread->alloc_region.end_addr
168 :base thread-base-tn
:scale
1
169 :disp
(* n-word-bytes
(1+ thread-alloc-region-slot
)))
173 (make-fixup (extern-alien-name "boxed_region") :foreign
8))))
175 (allocation-tramp alloc-tn size
))
177 (unless (and (tn-p size
) (location= alloc-tn size
))
178 (inst mov alloc-tn size
))
179 (inst add alloc-tn free-pointer
)
180 (inst cmp end-addr alloc-tn
)
181 (inst jmp
:be NOT-INLINE
)
182 (inst xchg free-pointer alloc-tn
)
184 (assemble (*elsewhere
*)
185 (emit-label NOT-INLINE
)
186 (cond ((numberp size
)
187 (allocation-tramp alloc-tn size
))
189 (inst sub alloc-tn free-pointer
)
190 (allocation-tramp alloc-tn alloc-tn
)))
195 (defun allocation (alloc-tn size
&optional ignored
)
196 (declare (ignore ignored
))
198 (inst lea r13-tn
(make-ea :qword
199 :disp
(make-fixup "alloc_tramp" :foreign
)))
204 ;;; Allocate an other-pointer object of fixed SIZE with a single word
205 ;;; header having the specified WIDETAG value. The result is placed in
207 (defmacro with-fixed-allocation
((result-tn widetag size
&optional inline
)
210 (bug "empty &body in WITH-FIXED-ALLOCATION"))
211 (once-only ((result-tn result-tn
) (size size
))
213 (allocation ,result-tn
(pad-data-block ,size
) ,inline
)
214 (storew (logior (ash (1- ,size
) n-widetag-bits
) ,widetag
)
217 (make-ea :qword
:base
,result-tn
:disp other-pointer-lowtag
))
221 (eval-when (#-sb-xc
:compile-toplevel
:load-toplevel
:execute
)
222 (defun emit-error-break (vop kind code values
)
223 (let ((vector (gensym)))
224 `((inst int
3) ; i386 breakpoint instruction
225 ;; The return PC points here; note the location for the debugger.
228 (note-this-location vop
:internal-error
)))
229 (inst byte
,kind
) ; eg trap_Xyyy
230 (with-adjustable-vector (,vector
) ; interr arguments
231 (write-var-integer (error-number-or-lose ',code
) ,vector
)
232 ,@(mapcar (lambda (tn)
234 ;; classic CMU CL comment:
235 ;; zzzzz jrd here. tn-offset is zero for constant
237 (write-var-integer (make-sc-offset (sc-number
243 (inst byte
(length ,vector
))
244 (dotimes (i (length ,vector
))
245 (inst byte
(aref ,vector i
))))))))
247 (defmacro error-call
(vop error-code
&rest values
)
249 "Cause an error. ERROR-CODE is the error to cause."
251 (emit-error-break vop error-trap error-code values
)))
253 (defmacro generate-error-code
(vop error-code
&rest values
)
255 "Generate-Error-Code Error-code Value*
256 Emit code for an error with the specified Error-Code and context Values."
257 `(assemble (*elsewhere
*)
258 (let ((start-lab (gen-label)))
259 (emit-label start-lab
)
260 (error-call ,vop
,error-code
,@values
)
266 ;;; This is used to wrap operations which leave untagged memory lying
267 ;;; around. It's an operation which the AOP weenies would describe as
268 ;;; having "cross-cutting concerns", meaning it appears all over the
269 ;;; place and there's no logical single place to attach documentation.
270 ;;; grep (mostly in src/runtime) is your friend
272 ;;; FIXME: *PSEUDO-ATOMIC-FOO* could be made into *PSEUDO-ATOMIC-BITS*,
273 ;;; set with a single operation and cleared with SHR *PSEUDO-ATOMIC-BITS*,-2;
274 ;;; the ATOMIC bit is bit 0, the INTERRUPTED bit is bit 1, and you check
275 ;;; the C flag after the shift to see whether you were interrupted.
277 ;;; FIXME: THIS NAME IS BACKWARDS!
278 (defmacro maybe-pseudo-atomic
(really-p &body body
)
281 (pseudo-atomic ,@body
)))
284 (defmacro pseudo-atomic
(&rest forms
)
285 (with-unique-names (label)
286 `(let ((,label
(gen-label)))
287 (inst mov
(make-ea :byte
289 :disp
(* 8 thread-pseudo-atomic-interrupted-slot
)) 0)
290 (inst mov
(make-ea :byte
292 :disp
(* 8 thread-pseudo-atomic-atomic-slot
))
295 (inst mov
(make-ea :byte
297 :disp
(* 8 thread-pseudo-atomic-atomic-slot
)) 0)
298 (inst cmp
(make-ea :byte
300 :disp
(* 8 thread-pseudo-atomic-interrupted-slot
)) 0)
301 (inst jmp
:eq
,label
)
302 ;; if PAI was set, interrupts were disabled at the same
303 ;; time using the process signal mask.
304 (inst break pending-interrupt-trap
)
305 (emit-label ,label
))))
309 (defmacro pseudo-atomic
(&rest forms
)
310 (with-unique-names (label)
311 `(let ((,label
(gen-label)))
312 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
313 ;; something. (perhaps SVLB, for static variable low byte)
314 (inst mov
(make-ea :byte
:disp
(+ nil-value
315 (static-symbol-offset
316 '*pseudo-atomic-interrupted
*)
317 (ash symbol-value-slot word-shift
)
318 ;; FIXME: Use mask, not minus, to
319 ;; take out type bits.
320 (- other-pointer-lowtag
)))
322 (inst mov
(make-ea :byte
:disp
(+ nil-value
323 (static-symbol-offset
324 '*pseudo-atomic-atomic
*)
325 (ash symbol-value-slot word-shift
)
326 (- other-pointer-lowtag
)))
329 (inst mov
(make-ea :byte
:disp
(+ nil-value
330 (static-symbol-offset
331 '*pseudo-atomic-atomic
*)
332 (ash symbol-value-slot word-shift
)
333 (- other-pointer-lowtag
)))
335 ;; KLUDGE: Is there any requirement for interrupts to be
336 ;; handled in order? It seems as though an interrupt coming
337 ;; in at this point will be executed before any pending interrupts.
338 ;; Or do incoming interrupts check to see whether any interrupts
339 ;; are pending? I wish I could find the documentation for
340 ;; pseudo-atomics.. -- WHN 19991130
341 (inst cmp
(make-ea :byte
343 (static-symbol-offset
344 '*pseudo-atomic-interrupted
*)
345 (ash symbol-value-slot word-shift
)
346 (- other-pointer-lowtag
)))
348 (inst jmp
:eq
,label
)
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-reffer
(name type offset lowtag scs el-type
&optional translate
)
362 `((:translate
,translate
)))
364 (:args
(object :scs
(descriptor-reg))
365 (index :scs
(any-reg)))
366 (:arg-types
,type tagged-num
)
367 (:results
(value :scs
,scs
))
368 (:result-types
,el-type
)
369 (:generator
3 ; pw was 5
370 (inst mov value
(make-ea :qword
:base object
:index index
371 :disp
(- (* ,offset n-word-bytes
)
373 (define-vop (,(symbolicate name
"-C"))
375 `((:translate
,translate
)))
377 (:args
(object :scs
(descriptor-reg)))
380 (:constant
(load/store-index
,n-word-bytes
,(eval lowtag
)
382 (:results
(value :scs
,scs
))
383 (:result-types
,el-type
)
384 (:generator
2 ; pw was 5
385 (inst mov value
(make-ea :qword
:base object
386 :disp
(- (* (+ ,offset index
) n-word-bytes
)
389 (defmacro define-full-setter
(name type offset lowtag scs el-type
&optional translate
)
393 `((:translate
,translate
)))
395 (:args
(object :scs
(descriptor-reg))
396 (index :scs
(any-reg))
397 (value :scs
,scs
:target result
))
398 (:arg-types
,type tagged-num
,el-type
)
399 (:results
(result :scs
,scs
))
400 (:result-types
,el-type
)
401 (:generator
4 ; was 5
402 (inst mov
(make-ea :qword
:base object
:index index
403 :disp
(- (* ,offset n-word-bytes
) ,lowtag
))
405 (move result value
)))
406 (define-vop (,(symbolicate name
"-C"))
408 `((:translate
,translate
)))
410 (:args
(object :scs
(descriptor-reg))
411 (value :scs
,scs
:target result
))
414 (:constant
(load/store-index
,n-word-bytes
,(eval lowtag
)
417 (:results
(result :scs
,scs
))
418 (:result-types
,el-type
)
419 (:generator
3 ; was 5
420 (inst mov
(make-ea :qword
:base object
421 :disp
(- (* (+ ,offset index
) n-word-bytes
)
424 (move result value
)))))
426 ;;; helper for alien stuff.
427 (defmacro with-pinned-objects
((&rest objects
) &body body
)
428 "Arrange with the garbage collector that the pages occupied by
429 OBJECTS will not be moved in memory for the duration of BODY.
430 Useful for e.g. foreign calls where another thread may trigger
432 `(multiple-value-prog1
434 ,@(loop for p in objects
435 collect
`(push-word-on-c-stack
436 (int-sap (sb!kernel
:get-lisp-obj-address
,p
))))
438 ;; If the body returned normally, we should restore the stack pointer
439 ;; for the benefit of any following code in the same function. If
440 ;; there's a non-local exit in the body, sp is garbage anyway and
441 ;; will get set appropriately from {a, the} frame pointer before it's
443 (pop-words-from-c-stack ,(length objects
))))