7142aa3aedf3cfd27e5d9f4cca2fa0f6dac3eebe
[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 ;;; This macro should only be used inside a pseudo-atomic section,
156 ;;; which should also cover subsequent initialization of the
157 ;;; object.
158 (defun allocation (alloc-tn size &optional inline)
159   ;; FIXME: since it appears that inline allocation is gone, we should
160   ;; remove the INLINE parameter and *MAYBE-USE-INLINE-ALLOCATION*
161   (declare (ignore inline))  
162   (flet ((load-size (dst-tn size)
163            (unless (and (tn-p size) (location= alloc-tn size))
164              (inst mov dst-tn size))))
165     (let ((alloc-tn-offset (tn-offset alloc-tn)))
166           ;; C call to allocate via dispatch routines. Each
167           ;; destination has a special entry point. The size may be a
168           ;; register or a constant.
169           (ecase alloc-tn-offset
170             (#.eax-offset
171              (case size
172                (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_eax")
173                                          :foreign)))
174                (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_eax")
175                                           :foreign)))
176                (t
177                 (load-size eax-tn size)
178                 (inst call (make-fixup (extern-alien-name "alloc_to_eax")
179                                        :foreign)))))
180             (#.ecx-offset
181              (case size
182                (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_ecx")
183                                          :foreign)))
184                (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_ecx")
185                                           :foreign)))
186                (t
187                 (load-size ecx-tn size)
188                 (inst call (make-fixup (extern-alien-name "alloc_to_ecx")
189                                        :foreign)))))
190             (#.edx-offset
191              (case size
192                (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_edx")
193                                          :foreign)))
194                (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_edx")
195                                           :foreign)))
196                (t
197                 (load-size edx-tn size)
198                 (inst call (make-fixup (extern-alien-name "alloc_to_edx")
199                                        :foreign)))))
200             (#.ebx-offset
201              (case size
202                (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_ebx")
203                                          :foreign)))
204                (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_ebx")
205                                           :foreign)))
206                (t
207                 (load-size ebx-tn size)
208                 (inst call (make-fixup (extern-alien-name "alloc_to_ebx")
209                                        :foreign)))))
210             (#.esi-offset
211              (case size
212                (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_esi")
213                                          :foreign)))
214                (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_esi")
215                                           :foreign)))
216                (t
217                 (load-size esi-tn size)
218                 (inst call (make-fixup (extern-alien-name "alloc_to_esi")
219                                        :foreign)))))
220             (#.edi-offset
221              (case size
222                (8 (inst call (make-fixup (extern-alien-name "alloc_8_to_edi")
223                                          :foreign)))
224                (16 (inst call (make-fixup (extern-alien-name "alloc_16_to_edi")
225                                           :foreign)))
226                (t
227                 (load-size edi-tn size)
228                 (inst call (make-fixup (extern-alien-name "alloc_to_edi")
229                                    :foreign))))))))
230   (values))
231
232 ;;; Allocate an other-pointer object of fixed SIZE with a single word
233 ;;; header having the specified WIDETAG value. The result is placed in
234 ;;; RESULT-TN.
235 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline)
236                                  &rest forms)
237   `(pseudo-atomic
238     (allocation ,result-tn (pad-data-block ,size) ,inline)
239     (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
240             ,result-tn)
241     (inst lea ,result-tn
242      (make-ea :byte :base ,result-tn :disp other-pointer-lowtag))
243     ,@forms))
244 \f
245 ;;;; error code
246 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
247   (defun emit-error-break (vop kind code values)
248     (let ((vector (gensym)))
249       `((inst int 3)                            ; i386 breakpoint instruction
250         ;; The return PC points here; note the location for the debugger.
251         (let ((vop ,vop))
252           (when vop
253                 (note-this-location vop :internal-error)))
254         (inst byte ,kind)                       ; eg trap_Xyyy
255         (with-adjustable-vector (,vector)       ; interr arguments
256           (write-var-integer (error-number-or-lose ',code) ,vector)
257           ,@(mapcar (lambda (tn)
258                       `(let ((tn ,tn))
259                          ;; classic CMU CL comment:
260                          ;;   zzzzz jrd here. tn-offset is zero for constant
261                          ;;   tns.
262                          (write-var-integer (make-sc-offset (sc-number
263                                                              (tn-sc tn))
264                                                             (or (tn-offset tn)
265                                                                 0))
266                                             ,vector)))
267                     values)
268           (inst byte (length ,vector))
269           (dotimes (i (length ,vector))
270             (inst byte (aref ,vector i))))))))
271
272 (defmacro error-call (vop error-code &rest values)
273   #!+sb-doc
274   "Cause an error. ERROR-CODE is the error to cause."
275   (cons 'progn
276         (emit-error-break vop error-trap error-code values)))
277
278 (defmacro generate-error-code (vop error-code &rest values)
279   #!+sb-doc
280   "Generate-Error-Code Error-code Value*
281   Emit code for an error with the specified Error-Code and context Values."
282   `(assemble (*elsewhere*)
283      (let ((start-lab (gen-label)))
284        (emit-label start-lab)
285        (error-call ,vop ,error-code ,@values)
286        start-lab)))
287
288 \f
289 ;;;; PSEUDO-ATOMIC
290
291 ;;; This is used to wrap operations which leave untagged memory lying
292 ;;; around.  It's an operation which the AOP weenies would describe as
293 ;;; having "cross-cutting concerns", meaning it appears all over the
294 ;;; place and there's no logical single place to attach documentation.
295 ;;; grep (mostly in src/runtime) is your friend 
296
297 ;;; FIXME: *PSEUDO-ATOMIC-FOO* could be made into *PSEUDO-ATOMIC-BITS*,
298 ;;; set with a single operation and cleared with SHR *PSEUDO-ATOMIC-BITS*,-2;
299 ;;; the ATOMIC bit is bit 0, the INTERRUPTED bit is bit 1, and you check
300 ;;; the C flag after the shift to see whether you were interrupted.
301
302 #!+sb-thread
303 (defmacro pseudo-atomic (&rest forms)
304   (with-unique-names (label)
305     `(let ((,label (gen-label)))
306       (inst fs-segment-prefix)
307       (inst mov (make-ea :byte 
308                  :disp (* 4 thread-pseudo-atomic-interrupted-slot)) 0)
309       (inst fs-segment-prefix)
310       (inst mov (make-ea :byte :disp (* 4 thread-pseudo-atomic-atomic-slot)) 1)
311       ,@forms
312       (inst fs-segment-prefix)
313       (inst mov (make-ea :byte :disp (* 4 thread-pseudo-atomic-atomic-slot)) 0)
314       (inst fs-segment-prefix)
315       (inst cmp (make-ea :byte
316                  :disp (* 4 thread-pseudo-atomic-interrupted-slot)) 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 #!-sb-thread
324 (defmacro pseudo-atomic (&rest forms)
325   (with-unique-names (label)
326     `(let ((,label (gen-label)))
327       ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
328       ;; something. (perhaps SVLB, for static variable low byte)
329       (inst mov (make-ea :byte :disp (+ nil-value
330                                         (static-symbol-offset
331                                          '*pseudo-atomic-interrupted*)
332                                         (ash symbol-value-slot word-shift)
333                                         ;; FIXME: Use mask, not minus, to
334                                         ;; take out type bits.
335                                         (- other-pointer-lowtag)))
336        0)
337       (inst mov (make-ea :byte :disp (+ nil-value
338                                         (static-symbol-offset
339                                          '*pseudo-atomic-atomic*)
340                                         (ash symbol-value-slot word-shift)
341                                         (- other-pointer-lowtag)))
342        (fixnumize 1))
343       ,@forms
344       (inst mov (make-ea :byte :disp (+ nil-value
345                                         (static-symbol-offset
346                                          '*pseudo-atomic-atomic*)
347                                         (ash symbol-value-slot word-shift)
348                                         (- other-pointer-lowtag)))
349        0)
350       ;; KLUDGE: Is there any requirement for interrupts to be
351       ;; handled in order? It seems as though an interrupt coming
352       ;; in at this point will be executed before any pending interrupts.
353       ;; Or do incoming interrupts check to see whether any interrupts
354       ;; are pending? I wish I could find the documentation for
355       ;; pseudo-atomics.. -- WHN 19991130
356       (inst cmp (make-ea :byte
357                  :disp (+ nil-value
358                           (static-symbol-offset
359                            '*pseudo-atomic-interrupted*)
360                           (ash symbol-value-slot word-shift)
361                           (- other-pointer-lowtag)))
362        0)
363       (inst jmp :eq ,label)
364       ;; if PAI was set, interrupts were disabled at the same time
365       ;; using the process signal mask.  
366       (inst break pending-interrupt-trap)
367       (emit-label ,label))))
368
369
370 \f
371 ;;;; indexed references
372
373 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
374   `(progn
375      (define-vop (,name)
376        ,@(when translate
377            `((:translate ,translate)))
378        (:policy :fast-safe)
379        (:args (object :scs (descriptor-reg))
380               (index :scs (any-reg)))
381        (:arg-types ,type tagged-num)
382        (:results (value :scs ,scs))
383        (:result-types ,el-type)
384        (:generator 3                    ; pw was 5
385          (inst mov value (make-ea :dword :base object :index index
386                                   :disp (- (* ,offset n-word-bytes)
387                                            ,lowtag)))))
388      (define-vop (,(symbolicate name "-C"))
389        ,@(when translate
390            `((:translate ,translate)))
391        (:policy :fast-safe)
392        (:args (object :scs (descriptor-reg)))
393        (:info index)
394        (:arg-types ,type (:constant (signed-byte 30)))
395        (:results (value :scs ,scs))
396        (:result-types ,el-type)
397        (:generator 2                    ; pw was 5
398          (inst mov value (make-ea :dword :base object
399                                   :disp (- (* (+ ,offset index) n-word-bytes)
400                                            ,lowtag)))))))
401
402 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
403   `(progn
404      (define-vop (,name)
405        ,@(when translate
406            `((:translate ,translate)))
407        (:policy :fast-safe)
408        (:args (object :scs (descriptor-reg))
409               (index :scs (any-reg))
410               (value :scs ,scs :target result))
411        (:arg-types ,type tagged-num ,el-type)
412        (:results (result :scs ,scs))
413        (:result-types ,el-type)
414        (:generator 4                    ; was 5
415          (inst mov (make-ea :dword :base object :index index
416                             :disp (- (* ,offset n-word-bytes) ,lowtag))
417                value)
418          (move result value)))
419      (define-vop (,(symbolicate name "-C"))
420        ,@(when translate
421            `((:translate ,translate)))
422        (:policy :fast-safe)
423        (:args (object :scs (descriptor-reg))
424               (value :scs ,scs :target result))
425        (:info index)
426        (:arg-types ,type (:constant (signed-byte 30)) ,el-type)
427        (:results (result :scs ,scs))
428        (:result-types ,el-type)
429        (:generator 3                    ; was 5
430          (inst mov (make-ea :dword :base object
431                             :disp (- (* (+ ,offset index) n-word-bytes)
432                                      ,lowtag))
433                value)
434          (move result value)))))
435
436 ;;; helper for alien stuff.
437 (defmacro with-pinned-objects ((&rest objects) &body body)
438   "Arrange with the garbage collector that the pages occupied by
439 OBJECTS will not be moved in memory for the duration of BODY.
440 Useful for e.g. foreign calls where another thread may trigger
441 garbage collection"
442   `(multiple-value-prog1
443        (progn
444          ,@(loop for p in objects 
445                  collect `(push-word-on-c-stack
446                            (int-sap (sb!kernel:get-lisp-obj-address ,p))))
447          ,@body)
448      ;; If the body returned normally, we should restore the stack pointer
449      ;; for the benefit of any following code in the same function.  If
450      ;; there's a non-local exit in the body, sp is garbage anyway and
451      ;; will get set appropriately from {a, the} frame pointer before it's
452      ;; next needed
453      (pop-words-from-c-stack ,(length objects))))