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