0.9.4.8:
[sbcl.git] / src / compiler / x86-64 / macros.lisp
1 ;;;; a bunch of handy macros for the x86
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!VM")
13
14 ;;;; instruction-like macros
15
16 (defmacro move (dst src)
17   #!+sb-doc
18   "Move SRC into DST unless they are location=."
19   (once-only ((n-dst dst)
20               (n-src src))
21     `(unless (location= ,n-dst ,n-src)
22        (inst mov ,n-dst ,n-src))))
23
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)))
28
29 (defmacro loadw (value ptr &optional (slot 0) (lowtag 0))
30   `(inst mov ,value (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
31
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)))
41            (t
42             (inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) ,value)))))
43
44 (defmacro pushw (ptr &optional (slot 0) (lowtag 0))
45   `(inst push (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
46
47 (defmacro popw (ptr &optional (slot 0) (lowtag 0))
48   `(inst pop (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
49 \f
50 ;;;; macros to generate useful values
51
52 (defmacro load-symbol (reg symbol)
53   `(inst mov ,reg (+ nil-value (static-symbol-offset ,symbol))))
54
55 (defmacro make-ea-for-symbol-value (symbol)
56   `(make-ea :qword
57     :disp (+ nil-value
58            (static-symbol-offset ',symbol)
59            (ash symbol-value-slot word-shift)
60            (- other-pointer-lowtag))))
61
62 (defmacro load-symbol-value (reg symbol)
63   `(inst mov ,reg (make-ea-for-symbol-value ,symbol)))
64
65 (defmacro store-symbol-value (reg symbol)
66   `(inst mov (make-ea-for-symbol-value ,symbol) ,reg))
67
68 #!+sb-thread
69 (defmacro make-ea-for-symbol-tls-index (symbol)
70   `(make-ea :qword
71     :disp (+ nil-value
72            (static-symbol-offset ',symbol)
73            (ash symbol-tls-index-slot word-shift)
74            (- other-pointer-lowtag))))
75
76 #!+sb-thread
77 (defmacro load-tl-symbol-value (reg symbol)
78   `(progn
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))))
81 #!-sb-thread
82 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
83
84 #!+sb-thread
85 (defmacro store-tl-symbol-value (reg symbol temp)
86   `(progn
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)))
89 #!-sb-thread
90 (defmacro store-tl-symbol-value (reg symbol temp)
91   (declare (ignore temp))
92   `(store-symbol-value ,reg ,symbol))
93
94 (defmacro load-type (target source &optional (offset 0))
95   #!+sb-doc
96   "Loads the type bits of a pointer into target independent of
97    byte-ordering issues."
98   (once-only ((n-target target)
99               (n-source source)
100               (n-offset offset))
101     (ecase *backend-byte-order*
102       (:little-endian
103        `(inst mov ,n-target
104               (make-ea :byte :base ,n-source :disp ,n-offset)))
105       (:big-endian
106        `(inst mov ,n-target
107               (make-ea :byte :base ,n-source :disp (+ ,n-offset 4)))))))
108 \f
109 ;;;; allocation helpers
110
111 ;;; All allocation is done by calls to assembler routines that
112 ;;; eventually invoke the C alloc() function.
113
114 ;;; Emit code to allocate an object with a size in bytes given by
115 ;;; Size. The size may be an integer of a TN. If Inline is a VOP
116 ;;; node-var then it is used to make an appropriate speed vs size
117 ;;; decision.
118
119 (defun allocation-dynamic-extent (alloc-tn size)
120   (inst sub rsp-tn size)
121   ;; see comment in x86/macros.lisp implementation of this
122   (inst and rsp-tn #.(lognot lowtag-mask))
123   (aver (not (location= alloc-tn rsp-tn)))
124   (inst mov alloc-tn rsp-tn)
125   (values))
126
127 ;;; This macro should only be used inside a pseudo-atomic section,
128 ;;; which should also cover subsequent initialization of the
129 ;;; object.
130 (defun allocation-tramp (alloc-tn size &optional ignored)
131   (declare (ignore ignored))
132   (inst push size)
133   (inst lea r13-tn (make-ea :qword
134                             :disp (make-fixup "alloc_tramp" :foreign)))
135   (inst call r13-tn)
136   (inst pop alloc-tn)
137   (values))
138
139 (defun allocation (alloc-tn size &optional ignored dynamic-extent)
140   (declare (ignore ignored))
141   (when dynamic-extent
142     (allocation-dynamic-extent alloc-tn size)
143     (return-from allocation (values)))
144   (let ((NOT-INLINE (gen-label))
145         (DONE (gen-label))
146         ;; Yuck.
147         (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
148         ;; thread->alloc_region.free_pointer
149         (free-pointer
150          #!+sb-thread
151          (make-ea :qword
152                   :base thread-base-tn :scale 1
153                   :disp (* n-word-bytes thread-alloc-region-slot))
154          #!-sb-thread
155          (make-ea :qword
156                   :scale 1 :disp
157                   (make-fixup (extern-alien-name "boxed_region") :foreign)))
158         ;; thread->alloc_region.end_addr
159         (end-addr
160          #!+sb-thread
161          (make-ea :qword
162                   :base thread-base-tn :scale 1
163                   :disp (* n-word-bytes (1+ thread-alloc-region-slot)))
164          #!-sb-thread
165          (make-ea :qword
166                   :scale 1 :disp
167                   (make-fixup (extern-alien-name "boxed_region") :foreign 8))))
168     (cond (in-elsewhere
169            (allocation-tramp alloc-tn size))
170           (t
171            (unless (and (tn-p size) (location= alloc-tn size))
172              (inst mov alloc-tn size))
173            (inst add alloc-tn free-pointer)
174            (inst cmp end-addr alloc-tn)
175            (inst jmp :be NOT-INLINE)
176            (inst xchg free-pointer alloc-tn)
177            (emit-label DONE)
178            (assemble (*elsewhere*)
179              (emit-label NOT-INLINE)
180              (cond ((numberp size)
181                     (allocation-tramp alloc-tn size))
182                    (t
183                     (inst sub alloc-tn free-pointer)
184                     (allocation-tramp alloc-tn alloc-tn)))
185              (inst jmp DONE))
186            (values)))))
187
188 #+nil
189 (defun allocation (alloc-tn size &optional ignored)
190   (declare (ignore ignored))
191   (inst push size)
192   (inst lea r13-tn (make-ea :qword
193                             :disp (make-fixup "alloc_tramp" :foreign)))
194   (inst call r13-tn)
195   (inst pop alloc-tn)
196   (values))
197
198 ;;; Allocate an other-pointer object of fixed SIZE with a single word
199 ;;; header having the specified WIDETAG value. The result is placed in
200 ;;; RESULT-TN.
201 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline)
202                                  &body forms)
203   (unless forms
204     (bug "empty &body in WITH-FIXED-ALLOCATION"))
205   (once-only ((result-tn result-tn) (size size))
206     `(pseudo-atomic
207       (allocation ,result-tn (pad-data-block ,size) ,inline)
208       (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
209               ,result-tn)
210       (inst lea ,result-tn
211             (make-ea :qword :base ,result-tn :disp other-pointer-lowtag))
212       ,@forms)))
213 \f
214 ;;;; error code
215 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
216   (defun emit-error-break (vop kind code values)
217     (let ((vector (gensym)))
218       `((inst int 3)                            ; i386 breakpoint instruction
219         ;; The return PC points here; note the location for the debugger.
220         (let ((vop ,vop))
221           (when vop
222                 (note-this-location vop :internal-error)))
223         (inst byte ,kind)                       ; eg trap_Xyyy
224         (with-adjustable-vector (,vector)       ; interr arguments
225           (write-var-integer (error-number-or-lose ',code) ,vector)
226           ,@(mapcar (lambda (tn)
227                       `(let ((tn ,tn))
228                          ;; classic CMU CL comment:
229                          ;;   zzzzz jrd here. tn-offset is zero for constant
230                          ;;   tns.
231                          (write-var-integer (make-sc-offset (sc-number
232                                                              (tn-sc tn))
233                                                             (or (tn-offset tn)
234                                                                 0))
235                                             ,vector)))
236                     values)
237           (inst byte (length ,vector))
238           (dotimes (i (length ,vector))
239             (inst byte (aref ,vector i))))))))
240
241 (defmacro error-call (vop error-code &rest values)
242   #!+sb-doc
243   "Cause an error. ERROR-CODE is the error to cause."
244   (cons 'progn
245         (emit-error-break vop error-trap error-code values)))
246
247 (defmacro generate-error-code (vop error-code &rest values)
248   #!+sb-doc
249   "Generate-Error-Code Error-code Value*
250   Emit code for an error with the specified Error-Code and context Values."
251   `(assemble (*elsewhere*)
252      (let ((start-lab (gen-label)))
253        (emit-label start-lab)
254        (error-call ,vop ,error-code ,@values)
255        start-lab)))
256
257 \f
258 ;;;; PSEUDO-ATOMIC
259
260 ;;; This is used to wrap operations which leave untagged memory lying
261 ;;; around.  It's an operation which the AOP weenies would describe as
262 ;;; having "cross-cutting concerns", meaning it appears all over the
263 ;;; place and there's no logical single place to attach documentation.
264 ;;; grep (mostly in src/runtime) is your friend
265
266 ;;; FIXME: *PSEUDO-ATOMIC-FOO* could be made into *PSEUDO-ATOMIC-BITS*,
267 ;;; set with a single operation and cleared with SHR *PSEUDO-ATOMIC-BITS*,-2;
268 ;;; the ATOMIC bit is bit 0, the INTERRUPTED bit is bit 1, and you check
269 ;;; the C flag after the shift to see whether you were interrupted.
270
271 ;;; FIXME: THIS NAME IS BACKWARDS!
272 (defmacro maybe-pseudo-atomic (really-p &body body)
273   `(if ,really-p
274        (progn ,@body)
275        (pseudo-atomic ,@body)))
276
277 #!+sb-thread
278 (defmacro pseudo-atomic (&rest forms)
279   (with-unique-names (label)
280     `(let ((,label (gen-label)))
281       (inst mov (make-ea :byte
282                  :base thread-base-tn
283                  :disp (* 8 thread-pseudo-atomic-interrupted-slot)) 0)
284       (inst mov (make-ea :byte
285                  :base thread-base-tn
286                  :disp (* 8 thread-pseudo-atomic-atomic-slot))
287             (fixnumize 1))
288       ,@forms
289       (inst mov (make-ea :byte
290                  :base thread-base-tn
291                  :disp (* 8 thread-pseudo-atomic-atomic-slot)) 0)
292       (inst cmp (make-ea :byte
293                  :base thread-base-tn
294                  :disp (* 8 thread-pseudo-atomic-interrupted-slot)) 0)
295       (inst jmp :eq ,label)
296       ;; if PAI was set, interrupts were disabled at the same
297       ;; time using the process signal mask.
298       (inst break pending-interrupt-trap)
299       (emit-label ,label))))
300
301
302 #!-sb-thread
303 (defmacro pseudo-atomic (&rest forms)
304   (with-unique-names (label)
305     `(let ((,label (gen-label)))
306       ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
307       ;; something. (perhaps SVLB, for static variable low byte)
308       (inst mov (make-ea :byte :disp (+ nil-value
309                                         (static-symbol-offset
310                                          '*pseudo-atomic-interrupted*)
311                                         (ash symbol-value-slot word-shift)
312                                         ;; FIXME: Use mask, not minus, to
313                                         ;; take out type bits.
314                                         (- other-pointer-lowtag)))
315        0)
316       (inst mov (make-ea :byte :disp (+ nil-value
317                                         (static-symbol-offset
318                                          '*pseudo-atomic-atomic*)
319                                         (ash symbol-value-slot word-shift)
320                                         (- other-pointer-lowtag)))
321        (fixnumize 1))
322       ,@forms
323       (inst mov (make-ea :byte :disp (+ nil-value
324                                         (static-symbol-offset
325                                          '*pseudo-atomic-atomic*)
326                                         (ash symbol-value-slot word-shift)
327                                         (- other-pointer-lowtag)))
328        0)
329       ;; KLUDGE: Is there any requirement for interrupts to be
330       ;; handled in order? It seems as though an interrupt coming
331       ;; in at this point will be executed before any pending interrupts.
332       ;; Or do incoming interrupts check to see whether any interrupts
333       ;; are pending? I wish I could find the documentation for
334       ;; pseudo-atomics.. -- WHN 19991130
335       (inst cmp (make-ea :byte
336                  :disp (+ nil-value
337                           (static-symbol-offset
338                            '*pseudo-atomic-interrupted*)
339                           (ash symbol-value-slot word-shift)
340                           (- other-pointer-lowtag)))
341        0)
342       (inst jmp :eq ,label)
343       ;; if PAI was set, interrupts were disabled at the same time
344       ;; using the process signal mask.
345       (inst break pending-interrupt-trap)
346       (emit-label ,label))))
347
348
349 \f
350 ;;;; indexed references
351
352 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
353   `(progn
354      (define-vop (,name)
355        ,@(when translate
356            `((:translate ,translate)))
357        (:policy :fast-safe)
358        (:args (object :scs (descriptor-reg))
359               (index :scs (any-reg)))
360        (:arg-types ,type tagged-num)
361        (:results (value :scs ,scs))
362        (:result-types ,el-type)
363        (:generator 3                    ; pw was 5
364          (inst mov value (make-ea :qword :base object :index index
365                                   :disp (- (* ,offset n-word-bytes)
366                                            ,lowtag)))))
367      (define-vop (,(symbolicate name "-C"))
368        ,@(when translate
369            `((:translate ,translate)))
370        (:policy :fast-safe)
371        (:args (object :scs (descriptor-reg)))
372        (:info index)
373        (:arg-types ,type
374                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
375                                                 ,(eval offset))))
376        (:results (value :scs ,scs))
377        (:result-types ,el-type)
378        (:generator 2                    ; pw was 5
379          (inst mov value (make-ea :qword :base object
380                                   :disp (- (* (+ ,offset index) n-word-bytes)
381                                            ,lowtag)))))))
382
383 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
384   `(progn
385      (define-vop (,name)
386        ,@(when translate
387            `((:translate ,translate)))
388        (:policy :fast-safe)
389        (:args (object :scs (descriptor-reg))
390               (index :scs (any-reg))
391               (value :scs ,scs :target result))
392        (:arg-types ,type tagged-num ,el-type)
393        (:results (result :scs ,scs))
394        (:result-types ,el-type)
395        (:generator 4                    ; was 5
396          (inst mov (make-ea :qword :base object :index index
397                             :disp (- (* ,offset n-word-bytes) ,lowtag))
398                value)
399          (move result value)))
400      (define-vop (,(symbolicate name "-C"))
401        ,@(when translate
402            `((:translate ,translate)))
403        (:policy :fast-safe)
404        (:args (object :scs (descriptor-reg))
405               (value :scs ,scs :target result))
406        (:info index)
407        (:arg-types ,type
408                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
409                                                 ,(eval offset)))
410                    ,el-type)
411        (:results (result :scs ,scs))
412        (:result-types ,el-type)
413        (:generator 3                    ; was 5
414          (inst mov (make-ea :qword :base object
415                             :disp (- (* (+ ,offset index) n-word-bytes)
416                                      ,lowtag))
417                value)
418          (move result value)))))
419
420 ;;; helper for alien stuff.
421 (defmacro with-pinned-objects ((&rest objects) &body body)
422   "Arrange with the garbage collector that the pages occupied by
423 OBJECTS will not be moved in memory for the duration of BODY.
424 Useful for e.g. foreign calls where another thread may trigger
425 garbage collection"
426   `(multiple-value-prog1
427        (progn
428          ,@(loop for p in objects
429                  collect `(push-word-on-c-stack
430                            (int-sap (sb!kernel:get-lisp-obj-address ,p))))
431          ,@body)
432      ;; If the body returned normally, we should restore the stack pointer
433      ;; for the benefit of any following code in the same function.  If
434      ;; there's a non-local exit in the body, sp is garbage anyway and
435      ;; will get set appropriately from {a, the} frame pointer before it's
436      ;; next needed
437      (pop-words-from-c-stack ,(length objects))))