0.9.6.44:
[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-binding-stack-pointer (reg)
95   #!+sb-thread
96   `(inst mov ,reg (make-ea :qword :base thread-base-tn
97                    :disp (* 8 thread-binding-stack-pointer-slot)))
98   #!-sb-thread
99   `(load-symbol-value ,reg *binding-stack-pointer*))
100
101 (defmacro store-binding-stack-pointer (reg)
102   #!+sb-thread
103   `(inst mov (make-ea :qword :base thread-base-tn
104               :disp (* 8 thread-binding-stack-pointer-slot))
105     ,reg)
106   #!-sb-thread
107   `(store-symbol-value ,reg *binding-stack-pointer*))
108
109 (defmacro load-type (target source &optional (offset 0))
110   #!+sb-doc
111   "Loads the type bits of a pointer into target independent of
112    byte-ordering issues."
113   (once-only ((n-target target)
114               (n-source source)
115               (n-offset offset))
116     (ecase *backend-byte-order*
117       (:little-endian
118        `(inst mov ,n-target
119               (make-ea :byte :base ,n-source :disp ,n-offset)))
120       (:big-endian
121        `(inst mov ,n-target
122               (make-ea :byte :base ,n-source :disp (+ ,n-offset 4)))))))
123 \f
124 ;;;; allocation helpers
125
126 ;;; All allocation is done by calls to assembler routines that
127 ;;; eventually invoke the C alloc() function.
128
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
132 ;;; decision.
133
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)
140   (values))
141
142 ;;; This macro should only be used inside a pseudo-atomic section,
143 ;;; which should also cover subsequent initialization of the
144 ;;; object.
145 (defun allocation-tramp (alloc-tn size &optional ignored)
146   (declare (ignore ignored))
147   (inst push size)
148   (inst lea temp-reg-tn (make-ea :qword
149                             :disp (make-fixup "alloc_tramp" :foreign)))
150   (inst call temp-reg-tn)
151   (inst pop alloc-tn)
152   (values))
153
154 (defun allocation (alloc-tn size &optional ignored dynamic-extent)
155   (declare (ignore ignored))
156   (when dynamic-extent
157     (allocation-dynamic-extent alloc-tn size)
158     (return-from allocation (values)))
159   (let ((NOT-INLINE (gen-label))
160         (DONE (gen-label))
161         ;; Yuck.
162         (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
163         ;; thread->alloc_region.free_pointer
164         (free-pointer
165          #!+sb-thread
166          (make-ea :qword
167                   :base thread-base-tn :scale 1
168                   :disp (* n-word-bytes thread-alloc-region-slot))
169          #!-sb-thread
170          (make-ea :qword
171                   :scale 1 :disp
172                   (make-fixup (extern-alien-name "boxed_region") :foreign)))
173         ;; thread->alloc_region.end_addr
174         (end-addr
175          #!+sb-thread
176          (make-ea :qword
177                   :base thread-base-tn :scale 1
178                   :disp (* n-word-bytes (1+ thread-alloc-region-slot)))
179          #!-sb-thread
180          (make-ea :qword
181                   :scale 1 :disp
182                   (make-fixup (extern-alien-name "boxed_region") :foreign 8))))
183     (cond (in-elsewhere
184            (allocation-tramp alloc-tn size))
185           (t
186            (inst mov temp-reg-tn free-pointer)
187            (if (tn-p size)
188                (if (location= alloc-tn size)
189                    (inst add alloc-tn temp-reg-tn)
190                    (inst lea alloc-tn
191                          (make-ea :qword :base temp-reg-tn :index size)))
192                (inst lea alloc-tn
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)
198            (emit-label DONE)
199            (assemble (*elsewhere*)
200              (emit-label NOT-INLINE)
201              (cond ((numberp size)
202                     (allocation-tramp alloc-tn size))
203                    (t
204                     (inst sub alloc-tn free-pointer)
205                     (allocation-tramp alloc-tn alloc-tn)))
206              (inst jmp DONE))
207            (values)))))
208
209 #+nil
210 (defun allocation (alloc-tn size &optional ignored)
211   (declare (ignore ignored))
212   (inst push size)
213   (inst lea temp-reg-tn (make-ea :qword
214                             :disp (make-fixup "alloc_tramp" :foreign)))
215   (inst call temp-reg-tn)
216   (inst pop alloc-tn)
217   (values))
218
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
221 ;;; RESULT-TN.
222 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline)
223                                  &body forms)
224   (unless forms
225     (bug "empty &body in WITH-FIXED-ALLOCATION"))
226   (once-only ((result-tn result-tn) (size size))
227     `(pseudo-atomic
228       (allocation ,result-tn (pad-data-block ,size) ,inline)
229       (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
230               ,result-tn)
231       (inst lea ,result-tn
232             (make-ea :qword :base ,result-tn :disp other-pointer-lowtag))
233       ,@forms)))
234 \f
235 ;;;; error code
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.
241         (let ((vop ,vop))
242           (when vop
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)
248                       `(let ((tn ,tn))
249                          ;; classic CMU CL comment:
250                          ;;   zzzzz jrd here. tn-offset is zero for constant
251                          ;;   tns.
252                          (write-var-integer (make-sc-offset (sc-number
253                                                              (tn-sc tn))
254                                                             (or (tn-offset tn)
255                                                                 0))
256                                             ,vector)))
257                     values)
258           (inst byte (length ,vector))
259           (dotimes (i (length ,vector))
260             (inst byte (aref ,vector i))))))))
261
262 (defmacro error-call (vop error-code &rest values)
263   #!+sb-doc
264   "Cause an error. ERROR-CODE is the error to cause."
265   (cons 'progn
266         (emit-error-break vop error-trap error-code values)))
267
268 (defmacro generate-error-code (vop error-code &rest values)
269   #!+sb-doc
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)
276        start-lab)))
277
278 \f
279 ;;;; PSEUDO-ATOMIC
280
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
286
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.
291
292 ;;; FIXME: THIS NAME IS BACKWARDS!
293 (defmacro maybe-pseudo-atomic (really-p &body body)
294   `(if ,really-p
295        (progn ,@body)
296        (pseudo-atomic ,@body)))
297
298 #!+sb-thread
299 (defmacro pseudo-atomic (&rest forms)
300   (with-unique-names (label)
301     `(let ((,label (gen-label)))
302       (inst mov (make-ea :byte
303                  :base thread-base-tn
304                  :disp (* 8 thread-pseudo-atomic-atomic-slot))
305             (fixnumize 1))
306       ,@forms
307       (inst mov (make-ea :byte
308                  :base thread-base-tn
309                  :disp (* 8 thread-pseudo-atomic-atomic-slot)) 0)
310       (inst cmp (make-ea :byte
311                  :base thread-base-tn
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))))
318
319
320 #!-sb-thread
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)))
331        (fixnumize 1))
332       ,@forms
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)))
338        0)
339       (inst cmp (make-ea :byte
340                  :disp (+ nil-value
341                           (static-symbol-offset
342                            '*pseudo-atomic-interrupted*)
343                           (ash symbol-value-slot word-shift)
344                           (- other-pointer-lowtag)))
345        0)
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))))
351
352
353 \f
354 ;;;; indexed references
355
356 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
357   `(progn
358      (define-vop (,name)
359        ,@(when translate
360            `((:translate ,translate)))
361        (:policy :fast-safe)
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)
370                                            ,lowtag)))))
371      (define-vop (,(symbolicate name "-C"))
372        ,@(when translate
373            `((:translate ,translate)))
374        (:policy :fast-safe)
375        (:args (object :scs (descriptor-reg)))
376        (:info index)
377        (:arg-types ,type
378                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
379                                                 ,(eval offset))))
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)
385                                            ,lowtag)))))))
386
387 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
388   `(progn
389      (define-vop (,name)
390        ,@(when translate
391            `((:translate ,translate)))
392        (:policy :fast-safe)
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))
402                value)
403          (move result value)))
404      (define-vop (,(symbolicate name "-C"))
405        ,@(when translate
406            `((:translate ,translate)))
407        (:policy :fast-safe)
408        (:args (object :scs (descriptor-reg))
409               (value :scs ,scs :target result))
410        (:info index)
411        (:arg-types ,type
412                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
413                                                 ,(eval offset)))
414                    ,el-type)
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)
420                                      ,lowtag))
421                value)
422          (move result value)))))
423
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
429 garbage collection"
430   `(multiple-value-prog1
431        (progn
432          ,@(loop for p in objects
433                  collect `(push-word-on-c-stack
434                            (int-sap (sb!kernel:get-lisp-obj-address ,p))))
435          ,@body)
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
440      ;; next needed
441      (pop-words-from-c-stack ,(length objects))))