0.9.2.48:
[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 load-symbol-value (reg symbol)
56   `(inst mov ,reg
57          (make-ea :qword
58                   :disp (+ nil-value
59                            (static-symbol-offset ',symbol)
60                            (ash symbol-value-slot word-shift)
61                            (- other-pointer-lowtag)))))
62
63 (defmacro store-symbol-value (reg symbol)
64   `(inst mov
65          (make-ea :qword
66                   :disp (+ nil-value
67                            (static-symbol-offset ',symbol)
68                            (ash symbol-value-slot word-shift)
69                            (- other-pointer-lowtag)))
70          ,reg))
71
72 #!+sb-thread
73 (defmacro load-tl-symbol-value (reg symbol)
74   `(progn
75     (inst mov ,reg
76      (make-ea :qword
77       :disp (+ nil-value
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))))
82 #!-sb-thread
83 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
84
85 #!+sb-thread
86 (defmacro store-tl-symbol-value (reg symbol temp)
87   `(progn
88     (inst mov ,temp
89      (make-ea :qword
90       :disp (+ nil-value
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)))
95 #!-sb-thread
96 (defmacro store-tl-symbol-value (reg symbol temp)
97   (declare (ignore temp))
98   `(store-symbol-value ,reg ,symbol))
99
100 (defmacro load-type (target source &optional (offset 0))
101   #!+sb-doc
102   "Loads the type bits of a pointer into target independent of
103    byte-ordering issues."
104   (once-only ((n-target target)
105               (n-source source)
106               (n-offset offset))
107     (ecase *backend-byte-order*
108       (:little-endian
109        `(inst mov ,n-target
110               (make-ea :byte :base ,n-source :disp ,n-offset)))
111       (:big-endian
112        `(inst mov ,n-target
113               (make-ea :byte :base ,n-source :disp (+ ,n-offset 4)))))))
114 \f
115 ;;;; allocation helpers
116
117 ;;; All allocation is done by calls to assembler routines that
118 ;;; eventually invoke the C alloc() function.
119
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
123 ;;; decision.
124
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)
131   (values))
132
133 ;;; This macro should only be used inside a pseudo-atomic section,
134 ;;; which should also cover subsequent initialization of the
135 ;;; object.
136 (defun allocation-tramp (alloc-tn size &optional ignored)
137   (declare (ignore ignored))
138   (inst push size)
139   (inst lea r13-tn (make-ea :qword
140                             :disp (make-fixup "alloc_tramp" :foreign)))
141   (inst call r13-tn)
142   (inst pop alloc-tn)
143   (values))
144
145 (defun allocation (alloc-tn size &optional ignored dynamic-extent)
146   (declare (ignore ignored))
147   (when dynamic-extent
148     (allocation-dynamic-extent alloc-tn size)
149     (return-from allocation (values)))
150   (let ((NOT-INLINE (gen-label))
151         (DONE (gen-label))
152         ;; Yuck.
153         (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
154         ;; thread->alloc_region.free_pointer
155         (free-pointer
156          #!+sb-thread
157          (make-ea :qword
158                   :base thread-base-tn :scale 1
159                   :disp (* n-word-bytes thread-alloc-region-slot))
160          #!-sb-thread
161          (make-ea :qword
162                   :scale 1 :disp
163                   (make-fixup (extern-alien-name "boxed_region") :foreign)))
164         ;; thread->alloc_region.end_addr
165         (end-addr
166          #!+sb-thread
167          (make-ea :qword
168                   :base thread-base-tn :scale 1
169                   :disp (* n-word-bytes (1+ thread-alloc-region-slot)))
170          #!-sb-thread
171          (make-ea :qword
172                   :scale 1 :disp
173                   (make-fixup (extern-alien-name "boxed_region") :foreign 8))))
174     (cond (in-elsewhere
175            (allocation-tramp alloc-tn size))
176           (t
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)
183            (emit-label DONE)
184            (assemble (*elsewhere*)
185              (emit-label NOT-INLINE)
186              (cond ((numberp size)
187                     (allocation-tramp alloc-tn size))
188                    (t
189                     (inst sub alloc-tn free-pointer)
190                     (allocation-tramp alloc-tn alloc-tn)))
191              (inst jmp DONE))
192            (values)))))
193
194 #+nil
195 (defun allocation (alloc-tn size &optional ignored)
196   (declare (ignore ignored))
197   (inst push size)
198   (inst lea r13-tn (make-ea :qword
199                             :disp (make-fixup "alloc_tramp" :foreign)))
200   (inst call r13-tn)
201   (inst pop alloc-tn)
202   (values))
203
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
206 ;;; RESULT-TN.
207 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline)
208                                  &body forms)
209   (unless forms
210     (bug "empty &body in WITH-FIXED-ALLOCATION"))
211   (once-only ((result-tn result-tn) (size size))
212     `(pseudo-atomic
213       (allocation ,result-tn (pad-data-block ,size) ,inline)
214       (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
215               ,result-tn)
216       (inst lea ,result-tn
217             (make-ea :qword :base ,result-tn :disp other-pointer-lowtag))
218       ,@forms)))
219 \f
220 ;;;; error code
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.
226         (let ((vop ,vop))
227           (when vop
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)
233                       `(let ((tn ,tn))
234                          ;; classic CMU CL comment:
235                          ;;   zzzzz jrd here. tn-offset is zero for constant
236                          ;;   tns.
237                          (write-var-integer (make-sc-offset (sc-number
238                                                              (tn-sc tn))
239                                                             (or (tn-offset tn)
240                                                                 0))
241                                             ,vector)))
242                     values)
243           (inst byte (length ,vector))
244           (dotimes (i (length ,vector))
245             (inst byte (aref ,vector i))))))))
246
247 (defmacro error-call (vop error-code &rest values)
248   #!+sb-doc
249   "Cause an error. ERROR-CODE is the error to cause."
250   (cons 'progn
251         (emit-error-break vop error-trap error-code values)))
252
253 (defmacro generate-error-code (vop error-code &rest values)
254   #!+sb-doc
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)
261        start-lab)))
262
263 \f
264 ;;;; PSEUDO-ATOMIC
265
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
271
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.
276
277 ;;; FIXME: THIS NAME IS BACKWARDS!
278 (defmacro maybe-pseudo-atomic (really-p &body body)
279   `(if ,really-p
280        (progn ,@body)
281        (pseudo-atomic ,@body)))
282
283 #!+sb-thread
284 (defmacro pseudo-atomic (&rest forms)
285   (with-unique-names (label)
286     `(let ((,label (gen-label)))
287       (inst mov (make-ea :byte
288                  :base thread-base-tn
289                  :disp (* 8 thread-pseudo-atomic-interrupted-slot)) 0)
290       (inst mov (make-ea :byte
291                  :base thread-base-tn
292                  :disp (* 8 thread-pseudo-atomic-atomic-slot))
293             (fixnumize 1))
294       ,@forms
295       (inst mov (make-ea :byte
296                  :base thread-base-tn
297                  :disp (* 8 thread-pseudo-atomic-atomic-slot)) 0)
298       (inst cmp (make-ea :byte
299                  :base thread-base-tn
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))))
306
307
308 #!-sb-thread
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)))
321        0)
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)))
327        (fixnumize 1))
328       ,@forms
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)))
334        0)
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
342                  :disp (+ nil-value
343                           (static-symbol-offset
344                            '*pseudo-atomic-interrupted*)
345                           (ash symbol-value-slot word-shift)
346                           (- other-pointer-lowtag)))
347        0)
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))))
353
354
355 \f
356 ;;;; indexed references
357
358 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
359   `(progn
360      (define-vop (,name)
361        ,@(when translate
362            `((:translate ,translate)))
363        (:policy :fast-safe)
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)
372                                            ,lowtag)))))
373      (define-vop (,(symbolicate name "-C"))
374        ,@(when translate
375            `((:translate ,translate)))
376        (:policy :fast-safe)
377        (:args (object :scs (descriptor-reg)))
378        (:info index)
379        (:arg-types ,type
380                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
381                                                 ,(eval offset))))
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)
387                                            ,lowtag)))))))
388
389 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
390   `(progn
391      (define-vop (,name)
392        ,@(when translate
393            `((:translate ,translate)))
394        (:policy :fast-safe)
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))
404                value)
405          (move result value)))
406      (define-vop (,(symbolicate name "-C"))
407        ,@(when translate
408            `((:translate ,translate)))
409        (:policy :fast-safe)
410        (:args (object :scs (descriptor-reg))
411               (value :scs ,scs :target result))
412        (:info index)
413        (:arg-types ,type
414                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
415                                                 ,(eval offset)))
416                    ,el-type)
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)
422                                      ,lowtag))
423                value)
424          (move result value)))))
425
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
431 garbage collection"
432   `(multiple-value-prog1
433        (progn
434          ,@(loop for p in objects
435                  collect `(push-word-on-c-stack
436                            (int-sap (sb!kernel:get-lisp-obj-address ,p))))
437          ,@body)
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
442      ;; next needed
443      (pop-words-from-c-stack ,(length objects))))