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 make-ea-for-symbol-value (symbol)
58 (static-symbol-offset ',symbol)
59 (ash symbol-value-slot word-shift)
60 (- other-pointer-lowtag))))
62 (defmacro load-symbol-value (reg symbol)
63 `(inst mov ,reg (make-ea-for-symbol-value ,symbol)))
65 (defmacro store-symbol-value (reg symbol)
66 `(inst mov (make-ea-for-symbol-value ,symbol) ,reg))
69 (defmacro make-ea-for-symbol-tls-index (symbol)
72 (static-symbol-offset ',symbol)
73 (ash symbol-tls-index-slot word-shift)
74 (- other-pointer-lowtag))))
77 (defmacro load-tl-symbol-value (reg symbol)
79 (inst mov ,reg (make-ea-for-symbol-tls-index ,symbol))
80 (inst mov ,reg (make-ea :qword :base thread-base-tn :scale 1 :index ,reg))))
82 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
85 (defmacro store-tl-symbol-value (reg symbol temp)
87 (inst mov ,temp (make-ea-for-symbol-tls-index ,symbol))
88 (inst mov (make-ea :qword :base thread-base-tn :scale 1 :index ,temp) ,reg)))
90 (defmacro store-tl-symbol-value (reg symbol temp)
91 (declare (ignore temp))
92 `(store-symbol-value ,reg ,symbol))
94 (defmacro load-binding-stack-pointer (reg)
96 `(inst mov ,reg (make-ea :qword :base thread-base-tn
97 :disp (* 8 thread-binding-stack-pointer-slot)))
99 `(load-symbol-value ,reg *binding-stack-pointer*))
101 (defmacro store-binding-stack-pointer (reg)
103 `(inst mov (make-ea :qword :base thread-base-tn
104 :disp (* 8 thread-binding-stack-pointer-slot))
107 `(store-symbol-value ,reg *binding-stack-pointer*))
109 (defmacro load-type (target source &optional (offset 0))
111 "Loads the type bits of a pointer into target independent of
112 byte-ordering issues."
113 (once-only ((n-target target)
116 (ecase *backend-byte-order*
119 (make-ea :byte :base ,n-source :disp ,n-offset)))
122 (make-ea :byte :base ,n-source :disp (+ ,n-offset 4)))))))
124 ;;;; allocation helpers
126 ;;; All allocation is done by calls to assembler routines that
127 ;;; eventually invoke the C alloc() function.
129 ;;; Emit code to allocate an object with a size in bytes given by
130 ;;; Size. The size may be an integer of a TN. If Inline is a VOP
131 ;;; node-var then it is used to make an appropriate speed vs size
134 (defun allocation-dynamic-extent (alloc-tn size)
135 (inst sub rsp-tn size)
136 ;; see comment in x86/macros.lisp implementation of this
137 (inst and rsp-tn #.(lognot lowtag-mask))
138 (aver (not (location= alloc-tn rsp-tn)))
139 (inst mov alloc-tn rsp-tn)
142 ;;; This macro should only be used inside a pseudo-atomic section,
143 ;;; which should also cover subsequent initialization of the
145 (defun allocation-tramp (alloc-tn size &optional ignored)
146 (declare (ignore ignored))
148 (inst lea temp-reg-tn (make-ea :qword
149 :disp (make-fixup "alloc_tramp" :foreign)))
150 (inst call temp-reg-tn)
154 (defun allocation (alloc-tn size &optional ignored dynamic-extent)
155 (declare (ignore ignored))
157 (allocation-dynamic-extent alloc-tn size)
158 (return-from allocation (values)))
159 (let ((NOT-INLINE (gen-label))
162 (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
163 ;; thread->alloc_region.free_pointer
167 :base thread-base-tn :scale 1
168 :disp (* n-word-bytes thread-alloc-region-slot))
172 (make-fixup (extern-alien-name "boxed_region") :foreign)))
173 ;; thread->alloc_region.end_addr
177 :base thread-base-tn :scale 1
178 :disp (* n-word-bytes (1+ thread-alloc-region-slot)))
182 (make-fixup (extern-alien-name "boxed_region") :foreign 8))))
184 (allocation-tramp alloc-tn size))
186 (inst mov temp-reg-tn free-pointer)
188 (if (location= alloc-tn size)
189 (inst add alloc-tn temp-reg-tn)
191 (make-ea :qword :base temp-reg-tn :index size)))
193 (make-ea :qword :base temp-reg-tn :disp size)))
194 (inst cmp end-addr alloc-tn)
195 (inst jmp :be NOT-INLINE)
196 (inst mov free-pointer alloc-tn)
197 (inst mov alloc-tn temp-reg-tn)
199 (assemble (*elsewhere*)
200 (emit-label NOT-INLINE)
201 (cond ((numberp size)
202 (allocation-tramp alloc-tn size))
204 (inst sub alloc-tn free-pointer)
205 (allocation-tramp alloc-tn alloc-tn)))
210 (defun allocation (alloc-tn size &optional ignored)
211 (declare (ignore ignored))
213 (inst lea temp-reg-tn (make-ea :qword
214 :disp (make-fixup "alloc_tramp" :foreign)))
215 (inst call temp-reg-tn)
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)
225 (bug "empty &body in WITH-FIXED-ALLOCATION"))
226 (once-only ((result-tn result-tn) (size size))
228 (allocation ,result-tn (pad-data-block ,size) ,inline)
229 (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
232 (make-ea :qword :base ,result-tn :disp other-pointer-lowtag))
236 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
237 (defun emit-error-break (vop kind code values)
238 (let ((vector (gensym)))
239 `((inst int 3) ; i386 breakpoint instruction
240 ;; The return PC points here; note the location for the debugger.
243 (note-this-location vop :internal-error)))
244 (inst byte ,kind) ; eg trap_Xyyy
245 (with-adjustable-vector (,vector) ; interr arguments
246 (write-var-integer (error-number-or-lose ',code) ,vector)
247 ,@(mapcar (lambda (tn)
249 ;; classic CMU CL comment:
250 ;; zzzzz jrd here. tn-offset is zero for constant
252 (write-var-integer (make-sc-offset (sc-number
258 (inst byte (length ,vector))
259 (dotimes (i (length ,vector))
260 (inst byte (aref ,vector i))))))))
262 (defmacro error-call (vop error-code &rest values)
264 "Cause an error. ERROR-CODE is the error to cause."
266 (emit-error-break vop error-trap error-code values)))
268 (defmacro generate-error-code (vop error-code &rest values)
270 "Generate-Error-Code Error-code Value*
271 Emit code for an error with the specified Error-Code and context Values."
272 `(assemble (*elsewhere*)
273 (let ((start-lab (gen-label)))
274 (emit-label start-lab)
275 (error-call ,vop ,error-code ,@values)
281 ;;; This is used to wrap operations which leave untagged memory lying
282 ;;; around. It's an operation which the AOP weenies would describe as
283 ;;; having "cross-cutting concerns", meaning it appears all over the
284 ;;; place and there's no logical single place to attach documentation.
285 ;;; grep (mostly in src/runtime) is your friend
287 ;;; FIXME: *PSEUDO-ATOMIC-FOO* could be made into *PSEUDO-ATOMIC-BITS*,
288 ;;; set with a single operation and cleared with SHR *PSEUDO-ATOMIC-BITS*,-2;
289 ;;; the ATOMIC bit is bit 0, the INTERRUPTED bit is bit 1, and you check
290 ;;; the C flag after the shift to see whether you were interrupted.
292 ;;; FIXME: THIS NAME IS BACKWARDS!
293 (defmacro maybe-pseudo-atomic (really-p &body body)
296 (pseudo-atomic ,@body)))
299 (defmacro pseudo-atomic (&rest forms)
300 (with-unique-names (label)
301 `(let ((,label (gen-label)))
302 (inst mov (make-ea :byte
304 :disp (* 8 thread-pseudo-atomic-atomic-slot))
307 (inst mov (make-ea :byte
309 :disp (* 8 thread-pseudo-atomic-atomic-slot)) 0)
310 (inst cmp (make-ea :byte
312 :disp (* 8 thread-pseudo-atomic-interrupted-slot)) 0)
313 (inst jmp :eq ,label)
314 ;; if PAI was set, interrupts were disabled at the same
315 ;; time using the process signal mask.
316 (inst break pending-interrupt-trap)
317 (emit-label ,label))))
321 (defmacro pseudo-atomic (&rest forms)
322 (with-unique-names (label)
323 `(let ((,label (gen-label)))
324 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
325 ;; something. (perhaps SVLB, for static variable low byte)
326 (inst mov (make-ea :byte :disp (+ nil-value
327 (static-symbol-offset
328 '*pseudo-atomic-atomic*)
329 (ash symbol-value-slot word-shift)
330 (- other-pointer-lowtag)))
333 (inst mov (make-ea :byte :disp (+ nil-value
334 (static-symbol-offset
335 '*pseudo-atomic-atomic*)
336 (ash symbol-value-slot word-shift)
337 (- other-pointer-lowtag)))
339 (inst cmp (make-ea :byte
341 (static-symbol-offset
342 '*pseudo-atomic-interrupted*)
343 (ash symbol-value-slot word-shift)
344 (- other-pointer-lowtag)))
346 (inst jmp :eq ,label)
347 ;; if PAI was set, interrupts were disabled at the same time
348 ;; using the process signal mask.
349 (inst break pending-interrupt-trap)
350 (emit-label ,label))))
354 ;;;; indexed references
356 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
360 `((:translate ,translate)))
362 (:args (object :scs (descriptor-reg))
363 (index :scs (any-reg)))
364 (:arg-types ,type tagged-num)
365 (:results (value :scs ,scs))
366 (:result-types ,el-type)
367 (:generator 3 ; pw was 5
368 (inst mov value (make-ea :qword :base object :index index
369 :disp (- (* ,offset n-word-bytes)
371 (define-vop (,(symbolicate name "-C"))
373 `((:translate ,translate)))
375 (:args (object :scs (descriptor-reg)))
378 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
380 (:results (value :scs ,scs))
381 (:result-types ,el-type)
382 (:generator 2 ; pw was 5
383 (inst mov value (make-ea :qword :base object
384 :disp (- (* (+ ,offset index) n-word-bytes)
387 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
391 `((:translate ,translate)))
393 (:args (object :scs (descriptor-reg))
394 (index :scs (any-reg))
395 (value :scs ,scs :target result))
396 (:arg-types ,type tagged-num ,el-type)
397 (:results (result :scs ,scs))
398 (:result-types ,el-type)
399 (:generator 4 ; was 5
400 (inst mov (make-ea :qword :base object :index index
401 :disp (- (* ,offset n-word-bytes) ,lowtag))
403 (move result value)))
404 (define-vop (,(symbolicate name "-C"))
406 `((:translate ,translate)))
408 (:args (object :scs (descriptor-reg))
409 (value :scs ,scs :target result))
412 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
415 (:results (result :scs ,scs))
416 (:result-types ,el-type)
417 (:generator 3 ; was 5
418 (inst mov (make-ea :qword :base object
419 :disp (- (* (+ ,offset index) n-word-bytes)
422 (move result value)))))
424 ;;; helper for alien stuff.
425 (defmacro with-pinned-objects ((&rest objects) &body body)
426 "Arrange with the garbage collector that the pages occupied by
427 OBJECTS will not be moved in memory for the duration of BODY.
428 Useful for e.g. foreign calls where another thread may trigger
430 `(multiple-value-prog1
432 ,@(loop for p in objects
433 collect `(push-word-on-c-stack
434 (int-sap (sb!kernel:get-lisp-obj-address ,p))))
436 ;; If the body returned normally, we should restore the stack pointer
437 ;; for the benefit of any following code in the same function. If
438 ;; there's a non-local exit in the body, sp is garbage anyway and
439 ;; will get set appropriately from {a, the} frame pointer before it's
441 (pop-words-from-c-stack ,(length objects))))