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