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