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 (extern-alien-name "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 (extern-alien-name "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)))
245 `((inst int 3) ; i386 breakpoint instruction
246 ;; The return PC points here; note the location for the debugger.
249 (note-this-location vop :internal-error)))
250 (inst byte ,kind) ; eg trap_Xyyy
251 (with-adjustable-vector (,vector) ; interr arguments
252 (write-var-integer (error-number-or-lose ',code) ,vector)
253 ,@(mapcar (lambda (tn)
255 ;; classic CMU CL comment:
256 ;; zzzzz jrd here. tn-offset is zero for constant
258 (write-var-integer (make-sc-offset (sc-number
264 (inst byte (length ,vector))
265 (dotimes (i (length ,vector))
266 (inst byte (aref ,vector i))))))))
268 (defmacro error-call (vop error-code &rest values)
270 "Cause an error. ERROR-CODE is the error to cause."
272 (emit-error-break vop error-trap error-code values)))
274 (defmacro generate-error-code (vop error-code &rest values)
276 "Generate-Error-Code Error-code Value*
277 Emit code for an error with the specified Error-Code and context Values."
278 `(assemble (*elsewhere*)
279 (let ((start-lab (gen-label)))
280 (emit-label start-lab)
281 (error-call ,vop ,error-code ,@values)
287 ;;; This is used to wrap operations which leave untagged memory lying
288 ;;; around. It's an operation which the AOP weenies would describe as
289 ;;; having "cross-cutting concerns", meaning it appears all over the
290 ;;; place and there's no logical single place to attach documentation.
291 ;;; grep (mostly in src/runtime) is your friend
293 ;;; FIXME: THIS NAME IS BACKWARDS!
294 (defmacro maybe-pseudo-atomic (really-p &body body)
297 (pseudo-atomic ,@body)))
300 (defmacro pseudo-atomic (&rest forms)
301 (with-unique-names (label)
302 `(let ((,label (gen-label)))
303 (inst or (make-ea :byte
305 :disp (* 8 thread-pseudo-atomic-bits-slot))
308 (inst xor (make-ea :byte
310 :disp (* 8 thread-pseudo-atomic-bits-slot))
313 ;; if PAI was set, interrupts were disabled at the same
314 ;; time using the process signal mask.
315 (inst break pending-interrupt-trap)
316 (emit-label ,label))))
320 (defmacro pseudo-atomic (&rest forms)
321 (with-unique-names (label)
322 `(let ((,label (gen-label)))
323 ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
324 ;; something. (perhaps SVLB, for static variable low byte)
325 (inst or (make-ea :byte :disp (+ nil-value
326 (static-symbol-offset
327 '*pseudo-atomic-bits*)
328 (ash symbol-value-slot word-shift)
329 (- other-pointer-lowtag)))
332 (inst xor (make-ea :byte :disp (+ nil-value
333 (static-symbol-offset
334 '*pseudo-atomic-bits*)
335 (ash symbol-value-slot word-shift)
336 (- other-pointer-lowtag)))
339 ;; if PAI was set, interrupts were disabled at the same time
340 ;; using the process signal mask.
341 (inst break pending-interrupt-trap)
342 (emit-label ,label))))
346 ;;;; indexed references
348 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
352 `((:translate ,translate)))
354 (:args (object :scs (descriptor-reg))
355 (index :scs (any-reg)))
356 (:arg-types ,type tagged-num)
357 (:results (value :scs ,scs))
358 (:result-types ,el-type)
359 (:generator 3 ; pw was 5
360 (inst mov value (make-ea :qword :base object :index index
361 :disp (- (* ,offset n-word-bytes)
363 (define-vop (,(symbolicate name "-C"))
365 `((:translate ,translate)))
367 (:args (object :scs (descriptor-reg)))
370 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
372 (:results (value :scs ,scs))
373 (:result-types ,el-type)
374 (:generator 2 ; pw was 5
375 (inst mov value (make-ea :qword :base object
376 :disp (- (* (+ ,offset index) n-word-bytes)
379 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
383 `((:translate ,translate)))
385 (:args (object :scs (descriptor-reg))
386 (index :scs (any-reg))
387 (value :scs ,scs :target result))
388 (:arg-types ,type tagged-num ,el-type)
389 (:results (result :scs ,scs))
390 (:result-types ,el-type)
391 (:generator 4 ; was 5
392 (inst mov (make-ea :qword :base object :index index
393 :disp (- (* ,offset n-word-bytes) ,lowtag))
395 (move result value)))
396 (define-vop (,(symbolicate name "-C"))
398 `((:translate ,translate)))
400 (:args (object :scs (descriptor-reg))
401 (value :scs ,scs :target result))
404 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
407 (:results (result :scs ,scs))
408 (:result-types ,el-type)
409 (:generator 3 ; was 5
410 (inst mov (make-ea :qword :base object
411 :disp (- (* (+ ,offset index) n-word-bytes)
414 (move result value)))))
416 ;;; helper for alien stuff.
417 (defmacro with-pinned-objects ((&rest objects) &body body)
418 "Arrange with the garbage collector that the pages occupied by
419 OBJECTS will not be moved in memory for the duration of BODY.
420 Useful for e.g. foreign calls where another thread may trigger
422 `(multiple-value-prog1
424 ,@(loop for p in objects
425 collect `(push-word-on-c-stack
426 (int-sap (sb!kernel:get-lisp-obj-address ,p))))
428 ;; If the body returned normally, we should restore the stack pointer
429 ;; for the benefit of any following code in the same function. If
430 ;; there's a non-local exit in the body, sp is garbage anyway and
431 ;; will get set appropriately from {a, the} frame pointer before it's
433 (pop-words-from-c-stack ,(length objects))))