0.8.9.10:
[sbcl.git] / src / compiler / x86 / 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 :dword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
51
52 (defmacro loadw (value ptr &optional (slot 0) (lowtag 0))
53   `(inst mov ,value (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
54
55 (defmacro storew (value ptr &optional (slot 0) (lowtag 0))
56   (once-only ((value value))
57     `(inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) ,value)))
58
59 (defmacro pushw (ptr &optional (slot 0) (lowtag 0))
60   `(inst push (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
61
62 (defmacro popw (ptr &optional (slot 0) (lowtag 0))
63   `(inst pop (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
64 \f
65 ;;;; macros to generate useful values
66
67 (defmacro load-symbol (reg symbol)
68   `(inst mov ,reg (+ nil-value (static-symbol-offset ,symbol))))
69
70 (defmacro load-symbol-value (reg symbol)
71   `(inst mov ,reg
72          (make-ea :dword
73                   :disp (+ nil-value
74                            (static-symbol-offset ',symbol)
75                            (ash symbol-value-slot word-shift)
76                            (- other-pointer-lowtag)))))
77
78 (defmacro store-symbol-value (reg symbol)
79   `(inst mov
80          (make-ea :dword
81                   :disp (+ nil-value
82                            (static-symbol-offset ',symbol)
83                            (ash symbol-value-slot word-shift)
84                            (- other-pointer-lowtag)))
85          ,reg))
86
87 #!+sb-thread
88 (defmacro load-tl-symbol-value (reg symbol)
89   `(progn
90     (inst mov ,reg
91      (make-ea :dword
92       :disp (+ nil-value
93                (static-symbol-offset ',symbol)
94                (ash symbol-tls-index-slot word-shift)
95                (- other-pointer-lowtag))))
96     (inst fs-segment-prefix)
97     (inst mov ,reg (make-ea :dword :scale 1 :index ,reg))))
98 #!-sb-thread
99 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
100
101 #!+sb-thread
102 (defmacro store-tl-symbol-value (reg symbol temp)
103   `(progn
104     (inst mov ,temp
105      (make-ea :dword
106       :disp (+ nil-value
107                (static-symbol-offset ',symbol)
108                (ash symbol-tls-index-slot word-shift)
109                (- other-pointer-lowtag))))
110     (inst fs-segment-prefix)
111     (inst mov (make-ea :dword :scale 1 :index ,temp) ,reg)))
112 #!-sb-thread
113 (defmacro store-tl-symbol-value (reg symbol temp)
114   (declare (ignore temp))
115   `(store-symbol-value ,reg ,symbol))
116   
117 (defmacro load-type (target source &optional (offset 0))
118   #!+sb-doc
119   "Loads the type bits of a pointer into target independent of
120    byte-ordering issues."
121   (once-only ((n-target target)
122               (n-source source)
123               (n-offset offset))
124     (ecase *backend-byte-order*
125       (:little-endian
126        `(inst mov ,n-target
127               (make-ea :byte :base ,n-source :disp ,n-offset)))
128       (:big-endian
129        `(inst mov ,n-target
130               (make-ea :byte :base ,n-source :disp (+ ,n-offset 3)))))))
131 \f
132 ;;;; allocation helpers
133
134 ;;; All allocation is done by calls to assembler routines that
135 ;;; eventually invoke the C alloc() function.  Once upon a time
136 ;;; (before threads) allocation within an alloc_region could also be
137 ;;; done inline, with the aid of two C symbols storing the current
138 ;;; allocation region boundaries; however, C cymbols are global.
139
140 ;;; C calls for allocation don't /seem/ to make an awful lot of
141 ;;; difference to speed.  Guessing from historical context, it looks
142 ;;; like inline allocation was introduced before pseudo-atomic, at
143 ;;; which time all calls to alloc() would have needed a syscall to
144 ;;; mask signals for the duration.  Now we have pseudoatomic there's
145 ;;; no need for that overhead.  Still, inline alloc would be a neat
146 ;;; addition someday
147
148 (defvar *maybe-use-inline-allocation* t) ; FIXME unused
149
150 ;;; Emit code to allocate an object with a size in bytes given by
151 ;;; SIZE. The size may be an integer of a TN. If Inline is a VOP
152 ;;; node-var then it is used to make an appropriate speed vs size
153 ;;; decision.
154
155 (defun allocation-dynamic-extent (alloc-tn size)
156   (inst sub esp-tn size)
157   ;; FIXME: SIZE _should_ be double-word aligned (suggested but
158   ;; unfortunately not enforced by PAD-DATA-BLOCK and
159   ;; WITH-FIXED-ALLOCATION), so that ESP is always divisible by 8 (for
160   ;; 32-bit lispobjs).  In that case, this AND instruction is
161   ;; unneccessary and could be removed.  If not, explain why.  -- CSR,
162   ;; 2004-03-30
163   (inst and esp-tn #.(ldb (byte 32 0) (lognot lowtag-mask)))
164   (aver (not (location= alloc-tn esp-tn)))
165   (inst mov alloc-tn esp-tn)
166   (values))
167
168 (defun allocation-notinline (alloc-tn size)
169   (flet ((load-size (dst-tn size)
170            (unless (and (tn-p size) (location= alloc-tn size))
171              (inst mov dst-tn size))))
172     (let ((alloc-tn-offset (tn-offset alloc-tn)))
173           ;; C call to allocate via dispatch routines. Each
174           ;; destination has a special entry point. The size may be a
175           ;; register or a constant.
176           (ecase alloc-tn-offset
177             (#.eax-offset
178              (case size
179                (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_eax")
180                                          :foreign)))
181                (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_eax")
182                                           :foreign)))
183                (t
184                 (load-size eax-tn size)
185                 (inst call (make-fixup (extern-alien-name "alloc_to_eax")
186                                        :foreign)))))
187             (#.ecx-offset
188              (case size
189                (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_ecx")
190                                          :foreign)))
191                (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_ecx")
192                                           :foreign)))
193                (t
194                 (load-size ecx-tn size)
195                 (inst call (make-fixup (extern-alien-name "alloc_to_ecx")
196                                        :foreign)))))
197             (#.edx-offset
198              (case size
199                (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_edx")
200                                          :foreign)))
201                (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_edx")
202                                           :foreign)))
203                (t
204                 (load-size edx-tn size)
205                 (inst call (make-fixup (extern-alien-name "alloc_to_edx")
206                                        :foreign)))))
207             (#.ebx-offset
208              (case size
209                (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_ebx")
210                                          :foreign)))
211                (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_ebx")
212                                           :foreign)))
213                (t
214                 (load-size ebx-tn size)
215                 (inst call (make-fixup (extern-alien-name "alloc_to_ebx")
216                                        :foreign)))))
217             (#.esi-offset
218              (case size
219                (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_esi")
220                                          :foreign)))
221                (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_esi")
222                                           :foreign)))
223                (t
224                 (load-size esi-tn size)
225                 (inst call (make-fixup (extern-alien-name "alloc_to_esi")
226                                        :foreign)))))
227             (#.edi-offset
228              (case size
229                (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_edi")
230                                          :foreign)))
231                (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_edi")
232                                           :foreign)))
233                (t
234                 (load-size edi-tn size)
235                 (inst call (make-fixup (extern-alien-name "alloc_to_edi")
236                                    :foreign)))))))))
237   
238 ;;; This macro should only be used inside a pseudo-atomic section,
239 ;;; which should also cover subsequent initialization of the object.
240 ;;; (FIXME: so why aren't we asserting this?)
241 (defun allocation (alloc-tn size &optional inline dynamic-extent)
242   ;; FIXME: since it appears that inline allocation is gone, we should
243   ;; remove the INLINE parameter and *MAYBE-USE-INLINE-ALLOCATION*
244   (declare (ignore inline))  
245   (cond
246     (dynamic-extent (allocation-dynamic-extent alloc-tn size))
247     (t (allocation-notinline alloc-tn size)))
248   (values))
249
250 ;;; Allocate an other-pointer object of fixed SIZE with a single word
251 ;;; header having the specified WIDETAG value. The result is placed in
252 ;;; RESULT-TN.
253 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline)
254                                  &rest forms)
255   `(pseudo-atomic
256     (allocation ,result-tn (pad-data-block ,size) ,inline)
257     (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
258             ,result-tn)
259     (inst lea ,result-tn
260      (make-ea :byte :base ,result-tn :disp other-pointer-lowtag))
261     ,@forms))
262 \f
263 ;;;; error code
264 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
265   (defun emit-error-break (vop kind code values)
266     (let ((vector (gensym)))
267       `((inst int 3)                            ; i386 breakpoint instruction
268         ;; The return PC points here; note the location for the debugger.
269         (let ((vop ,vop))
270           (when vop
271                 (note-this-location vop :internal-error)))
272         (inst byte ,kind)                       ; eg trap_Xyyy
273         (with-adjustable-vector (,vector)       ; interr arguments
274           (write-var-integer (error-number-or-lose ',code) ,vector)
275           ,@(mapcar (lambda (tn)
276                       `(let ((tn ,tn))
277                          ;; classic CMU CL comment:
278                          ;;   zzzzz jrd here. tn-offset is zero for constant
279                          ;;   tns.
280                          (write-var-integer (make-sc-offset (sc-number
281                                                              (tn-sc tn))
282                                                             (or (tn-offset tn)
283                                                                 0))
284                                             ,vector)))
285                     values)
286           (inst byte (length ,vector))
287           (dotimes (i (length ,vector))
288             (inst byte (aref ,vector i))))))))
289
290 (defmacro error-call (vop error-code &rest values)
291   #!+sb-doc
292   "Cause an error. ERROR-CODE is the error to cause."
293   (cons 'progn
294         (emit-error-break vop error-trap error-code values)))
295
296 (defmacro generate-error-code (vop error-code &rest values)
297   #!+sb-doc
298   "Generate-Error-Code Error-code Value*
299   Emit code for an error with the specified Error-Code and context Values."
300   `(assemble (*elsewhere*)
301      (let ((start-lab (gen-label)))
302        (emit-label start-lab)
303        (error-call ,vop ,error-code ,@values)
304        start-lab)))
305
306 \f
307 ;;;; PSEUDO-ATOMIC
308
309 ;;; This is used to wrap operations which leave untagged memory lying
310 ;;; around.  It's an operation which the AOP weenies would describe as
311 ;;; having "cross-cutting concerns", meaning it appears all over the
312 ;;; place and there's no logical single place to attach documentation.
313 ;;; grep (mostly in src/runtime) is your friend 
314
315 ;;; FIXME: *PSEUDO-ATOMIC-FOO* could be made into *PSEUDO-ATOMIC-BITS*,
316 ;;; set with a single operation and cleared with SHR *PSEUDO-ATOMIC-BITS*,-2;
317 ;;; the ATOMIC bit is bit 0, the INTERRUPTED bit is bit 1, and you check
318 ;;; the C flag after the shift to see whether you were interrupted.
319 ;;;
320 ;;; KLUDGE: since the stack on the x86 is treated conservatively, it
321 ;;; does not matter whether a signal occurs during construction of a
322 ;;; dynamic-extent object, as the half-finished construction of the
323 ;;; object will not cause any difficulty.  We can therefore elide 
324 (defvar *dynamic-extent* nil)
325
326 #!+sb-thread
327 (defmacro pseudo-atomic (&rest forms)
328   (with-unique-names (label)
329     `(if *dynamic-extent* ; I will burn in hell
330          (progn ,@forms)
331          (let ((,label (gen-label)))
332            (inst fs-segment-prefix)
333            (inst mov (make-ea :byte 
334                               :disp (* 4 thread-pseudo-atomic-interrupted-slot)) 0)
335            (inst fs-segment-prefix)
336            (inst mov (make-ea :byte :disp (* 4 thread-pseudo-atomic-atomic-slot)) 1)
337            ,@forms
338            (inst fs-segment-prefix)
339            (inst mov (make-ea :byte :disp (* 4 thread-pseudo-atomic-atomic-slot)) 0)
340            (inst fs-segment-prefix)
341            (inst cmp (make-ea :byte
342                               :disp (* 4 thread-pseudo-atomic-interrupted-slot)) 0)
343            (inst jmp :eq ,label)
344            ;; if PAI was set, interrupts were disabled at the same
345            ;; time using the process signal mask.
346            (inst break pending-interrupt-trap)
347            (emit-label ,label)))))
348
349 #!-sb-thread
350 (defmacro pseudo-atomic (&rest forms)
351   (with-unique-names (label)
352     `(if *dynamic-extent*
353          (progn ,@forms)
354          (let ((,label (gen-label)))
355            ;; FIXME: The MAKE-EA noise should become a MACROLET macro
356            ;; or something. (perhaps SVLB, for static variable low
357            ;; byte)
358            (inst mov (make-ea :byte :disp (+ nil-value
359                                              (static-symbol-offset
360                                               '*pseudo-atomic-interrupted*)
361                                              (ash symbol-value-slot word-shift)
362                                              ;; FIXME: Use mask, not minus, to
363                                              ;; take out type bits.
364                                              (- other-pointer-lowtag)))
365                  0)
366            (inst mov (make-ea :byte :disp (+ nil-value
367                                              (static-symbol-offset
368                                               '*pseudo-atomic-atomic*)
369                                              (ash symbol-value-slot word-shift)
370                                              (- other-pointer-lowtag)))
371                  (fixnumize 1))
372            ,@forms
373            (inst mov (make-ea :byte :disp (+ nil-value
374                                              (static-symbol-offset
375                                               '*pseudo-atomic-atomic*)
376                                              (ash symbol-value-slot word-shift)
377                                              (- other-pointer-lowtag)))
378                  0)
379            ;; KLUDGE: Is there any requirement for interrupts to be
380            ;; handled in order? It seems as though an interrupt coming
381            ;; in at this point will be executed before any pending
382            ;; interrupts.  Or do incoming interrupts check to see
383            ;; whether any interrupts are pending? I wish I could find
384            ;; the documentation for pseudo-atomics.. -- WHN 19991130
385            (inst cmp (make-ea :byte
386                               :disp (+ nil-value
387                                        (static-symbol-offset
388                                         '*pseudo-atomic-interrupted*)
389                                        (ash symbol-value-slot word-shift)
390                                        (- other-pointer-lowtag)))
391                  0)
392            (inst jmp :eq ,label)
393            ;; if PAI was set, interrupts were disabled at the same
394            ;; time using the process signal mask.
395            (inst break pending-interrupt-trap)
396            (emit-label ,label)))))
397 \f
398 ;;;; indexed references
399
400 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
401   `(progn
402      (define-vop (,name)
403        ,@(when translate
404            `((:translate ,translate)))
405        (:policy :fast-safe)
406        (:args (object :scs (descriptor-reg))
407               (index :scs (any-reg)))
408        (:arg-types ,type tagged-num)
409        (:results (value :scs ,scs))
410        (:result-types ,el-type)
411        (:generator 3                    ; pw was 5
412          (inst mov value (make-ea :dword :base object :index index
413                                   :disp (- (* ,offset n-word-bytes)
414                                            ,lowtag)))))
415      (define-vop (,(symbolicate name "-C"))
416        ,@(when translate
417            `((:translate ,translate)))
418        (:policy :fast-safe)
419        (:args (object :scs (descriptor-reg)))
420        (:info index)
421        (:arg-types ,type (:constant (signed-byte 30)))
422        (:results (value :scs ,scs))
423        (:result-types ,el-type)
424        (:generator 2                    ; pw was 5
425          (inst mov value (make-ea :dword :base object
426                                   :disp (- (* (+ ,offset index) n-word-bytes)
427                                            ,lowtag)))))))
428
429 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
430   `(progn
431      (define-vop (,name)
432        ,@(when translate
433            `((:translate ,translate)))
434        (:policy :fast-safe)
435        (:args (object :scs (descriptor-reg))
436               (index :scs (any-reg))
437               (value :scs ,scs :target result))
438        (:arg-types ,type tagged-num ,el-type)
439        (:results (result :scs ,scs))
440        (:result-types ,el-type)
441        (:generator 4                    ; was 5
442          (inst mov (make-ea :dword :base object :index index
443                             :disp (- (* ,offset n-word-bytes) ,lowtag))
444                value)
445          (move result value)))
446      (define-vop (,(symbolicate name "-C"))
447        ,@(when translate
448            `((:translate ,translate)))
449        (:policy :fast-safe)
450        (:args (object :scs (descriptor-reg))
451               (value :scs ,scs :target result))
452        (:info index)
453        (:arg-types ,type (:constant (signed-byte 30)) ,el-type)
454        (:results (result :scs ,scs))
455        (:result-types ,el-type)
456        (:generator 3                    ; was 5
457          (inst mov (make-ea :dword :base object
458                             :disp (- (* (+ ,offset index) n-word-bytes)
459                                      ,lowtag))
460                value)
461          (move result value)))))
462
463 ;;; helper for alien stuff.
464 (defmacro with-pinned-objects ((&rest objects) &body body)
465   "Arrange with the garbage collector that the pages occupied by
466 OBJECTS will not be moved in memory for the duration of BODY.
467 Useful for e.g. foreign calls where another thread may trigger
468 garbage collection"
469   `(multiple-value-prog1
470        (progn
471          ,@(loop for p in objects 
472                  collect `(push-word-on-c-stack
473                            (int-sap (sb!kernel:get-lisp-obj-address ,p))))
474          ,@body)
475      ;; If the body returned normally, we should restore the stack pointer
476      ;; for the benefit of any following code in the same function.  If
477      ;; there's a non-local exit in the body, sp is garbage anyway and
478      ;; will get set appropriately from {a, the} frame pointer before it's
479      ;; next needed
480      (pop-words-from-c-stack ,(length objects))))