Port WITH-TLS-EA and other remaining FS prefix uses to Windows.
[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 align-stack-pointer (tn)
50   #!-darwin (declare (ignore tn))
51   #!+darwin
52   ;; 16 byte alignment.
53   `(inst and ,tn #xfffffff0))
54
55 (defmacro make-ea-for-object-slot (ptr slot lowtag &optional (size :dword))
56   `(make-ea ,size :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
57
58 (defmacro loadw (value ptr &optional (slot 0) (lowtag 0))
59   `(inst mov ,value (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
60
61 (defmacro storew (value ptr &optional (slot 0) (lowtag 0))
62   (once-only ((value value))
63     `(inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) ,value)))
64
65 ;;; A handy macro for storing widetags.
66 (defmacro storeb (value ptr &optional (slot 0) (lowtag 0))
67   (once-only ((value value))
68     `(inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag :byte) ,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
76 (defmacro make-ea-for-vector-data (object &key (size :dword) (offset 0)
77                                    index (scale (ash (width-bits size) -3)))
78   `(make-ea ,size :base ,object :index ,index :scale ,scale
79             :disp (- (+ (* vector-data-offset n-word-bytes)
80                         (* ,offset ,scale))
81                      other-pointer-lowtag)))
82 \f
83 ;;;; macros to generate useful values
84
85 (defmacro load-symbol (reg symbol)
86   `(inst mov ,reg (+ nil-value (static-symbol-offset ,symbol))))
87
88 (defmacro make-ea-for-symbol-value (symbol &optional (width :dword))
89   (declare (type symbol symbol))
90   `(make-ea ,width
91     :disp (+ nil-value
92            (static-symbol-offset ',symbol)
93            (ash symbol-value-slot word-shift)
94            (- other-pointer-lowtag))))
95
96 (defmacro load-symbol-value (reg symbol)
97   `(inst mov ,reg (make-ea-for-symbol-value ,symbol)))
98
99 (defmacro store-symbol-value (reg symbol)
100   `(inst mov (make-ea-for-symbol-value ,symbol) ,reg))
101
102 #!+sb-thread
103 (defmacro make-ea-for-symbol-tls-index (symbol)
104   (declare (type symbol symbol))
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
111 #!+sb-thread
112 (defmacro load-tl-symbol-value (reg symbol)
113   `(with-tls-ea (EA :base ,reg
114                     :disp-type :index
115                     :disp (make-ea-for-symbol-tls-index ,symbol))
116      (inst mov ,reg (make-ea :dword :base ,reg) :maybe-fs)))
117 #!-sb-thread
118 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
119
120 #!+sb-thread
121 (defmacro store-tl-symbol-value (reg symbol temp)
122   `(with-tls-ea (EA :base ,temp
123                     :disp-type :index
124                     :disp (make-ea-for-symbol-tls-index ,symbol))
125      (inst mov EA ,reg :maybe-fs)))
126 #!-sb-thread
127 (defmacro store-tl-symbol-value (reg symbol temp)
128   (declare (ignore temp))
129   `(store-symbol-value ,reg ,symbol))
130
131 (defmacro load-binding-stack-pointer (reg)
132   #!+sb-thread
133   `(with-tls-ea (EA :base ,reg
134                     :disp-type :constant
135                     :disp (* 4 thread-binding-stack-pointer-slot))
136      (inst mov ,reg EA :maybe-fs))
137   #!-sb-thread
138   `(load-symbol-value ,reg *binding-stack-pointer*))
139
140 (defmacro store-binding-stack-pointer (reg)
141   #!+sb-thread
142   `(progn
143      #!+win32
144      (progn
145        (inst push eax-tn)
146        (inst push ,reg)
147        (with-tls-ea (EA :base eax-tn
148                         :disp-type :constant
149                         :disp (* 4 thread-binding-stack-pointer-slot))
150          (inst pop EA))
151        (inst pop eax-tn))
152      #!-win32
153      (with-tls-ea (EA :disp-type :constant
154                       :disp (* 4 thread-binding-stack-pointer-slot))
155        (inst mov EA ,reg :maybe-fs)))
156   #!-sb-thread
157   `(store-symbol-value ,reg *binding-stack-pointer*))
158
159 (defmacro load-type (target source &optional (offset 0))
160   #!+sb-doc
161   "Loads the type bits of a pointer into target independent of
162    byte-ordering issues."
163   (once-only ((n-target target)
164               (n-source source)
165               (n-offset offset))
166     (ecase *backend-byte-order*
167       (:little-endian
168        `(inst mov ,n-target
169               (make-ea :byte :base ,n-source :disp ,n-offset)))
170       (:big-endian
171        `(inst mov ,n-target
172               (make-ea :byte :base ,n-source
173                              :disp (+ ,n-offset (1- n-word-bytes))))))))
174 \f
175 ;;;; allocation helpers
176
177 ;;; Allocation within alloc_region (which is thread local) can be done
178 ;;; inline.  If the alloc_region is overflown allocation is done by
179 ;;; calling the C alloc() function.
180
181 ;;; C calls for allocation don't /seem/ to make an awful lot of
182 ;;; difference to speed. On pure consing it's about a 25%
183 ;;; gain. Guessing from historical context, it looks like inline
184 ;;; allocation was introduced before pseudo-atomic, at which time all
185 ;;; calls to alloc() would have needed a syscall to mask signals for
186 ;;; the duration.  Now we have pseudoatomic there's no need for that
187 ;;; overhead.
188
189 (defun allocation-dynamic-extent (alloc-tn size lowtag)
190   (inst sub esp-tn size)
191   ;; FIXME: SIZE _should_ be double-word aligned (suggested but
192   ;; unfortunately not enforced by PAD-DATA-BLOCK and
193   ;; WITH-FIXED-ALLOCATION), so that ESP is always divisible by 8 (for
194   ;; 32-bit lispobjs).  In that case, this AND instruction is
195   ;; unneccessary and could be removed.  If not, explain why.  -- CSR,
196   ;; 2004-03-30
197   (inst and esp-tn (lognot lowtag-mask))
198   (aver (not (location= alloc-tn esp-tn)))
199   (inst lea alloc-tn (make-ea :byte :base esp-tn :disp lowtag))
200   (values))
201
202 (defun allocation-notinline (alloc-tn size)
203   (let* ((alloc-tn-offset (tn-offset alloc-tn))
204          ;; C call to allocate via dispatch routines. Each
205          ;; destination has a special entry point. The size may be a
206          ;; register or a constant.
207          (tn-text (ecase alloc-tn-offset
208                     (#.eax-offset "eax")
209                     (#.ecx-offset "ecx")
210                     (#.edx-offset "edx")
211                     (#.ebx-offset "ebx")
212                     (#.esi-offset "esi")
213                     (#.edi-offset "edi")))
214          (size-text (case size (8 "8_") (16 "16_") (t ""))))
215     (unless (or (eql size 8) (eql size 16))
216       (unless (and (tn-p size) (location= alloc-tn size))
217         (inst mov alloc-tn size)))
218     (inst call (make-fixup (concatenate 'string
219                                          "alloc_" size-text
220                                          "to_" tn-text)
221                            :foreign))))
222
223 (defun allocation-inline (alloc-tn size)
224   (let* ((ok (gen-label)) ;reindent after merging
225         (done (gen-label))
226         #!+(and sb-thread win32)
227         (scratch-tns (loop for my-tn in `(,eax-tn ,ebx-tn ,edx-tn ,ecx-tn)
228                            when (and (not (location= alloc-tn my-tn))
229                                      (or (not (tn-p size))
230                                          (not (location= size my-tn))))
231                              collect my-tn))
232         (tls-prefix #!+sb-thread :fs #!-sb-thread nil)
233         (free-pointer
234          (make-ea :dword :disp
235                   #!+sb-thread (* n-word-bytes thread-alloc-region-slot)
236                   #!-sb-thread (make-fixup "boxed_region" :foreign)
237                   :scale 1)) ; thread->alloc_region.free_pointer
238         (end-addr
239          (make-ea :dword :disp
240                   #!+sb-thread (* n-word-bytes (1+ thread-alloc-region-slot))
241                   #!-sb-thread (make-fixup "boxed_region" :foreign 4)
242                   :scale 1))   ; thread->alloc_region.end_addr
243         #!+(and sb-thread win32) (scratch-tn (pop scratch-tns))
244         #!+(and sb-thread win32) (swap-tn (pop scratch-tns)))
245     (unless (and (tn-p size) (location= alloc-tn size))
246       (inst mov alloc-tn size))
247     #!+(and sb-thread win32)
248     (progn
249       (inst push scratch-tn)
250       (inst push swap-tn)
251       (inst mov scratch-tn
252             (make-ea :dword :disp
253                      +win32-tib-arbitrary-field-offset+) tls-prefix)
254       (setf (ea-base free-pointer) scratch-tn
255             (ea-base end-addr) scratch-tn
256             tls-prefix nil))
257     (inst add alloc-tn free-pointer tls-prefix)
258     (inst cmp alloc-tn end-addr tls-prefix)
259     (inst jmp :be ok)
260     (let ((dst (ecase (tn-offset alloc-tn)
261                  (#.eax-offset "alloc_overflow_eax")
262                  (#.ecx-offset "alloc_overflow_ecx")
263                  (#.edx-offset "alloc_overflow_edx")
264                  (#.ebx-offset "alloc_overflow_ebx")
265                  (#.esi-offset "alloc_overflow_esi")
266                  (#.edi-offset "alloc_overflow_edi"))))
267       (inst call (make-fixup dst :foreign)))
268     (inst jmp-short done)
269     (emit-label ok)
270     ;; Swap ALLOC-TN and FREE-POINTER
271     (cond ((and (tn-p size) (location= alloc-tn size))
272            ;; XCHG is extremely slow, use the xor swap trick
273            #!-(and sb-thread win32)
274            (progn
275              (inst xor alloc-tn free-pointer tls-prefix)
276              (inst xor free-pointer alloc-tn tls-prefix)
277              (inst xor alloc-tn free-pointer tls-prefix))
278            #!+(and sb-thread win32)
279            (progn
280              (inst mov swap-tn free-pointer tls-prefix)
281              (inst mov free-pointer alloc-tn tls-prefix)
282              (inst mov alloc-tn swap-tn)))
283           (t
284            ;; It's easier if SIZE is still available.
285            (inst mov free-pointer alloc-tn tls-prefix)
286            (inst sub alloc-tn size)))
287     (emit-label done)
288     #!+(and sb-thread win32)
289     (progn
290       (inst pop swap-tn)
291       (inst pop scratch-tn))
292     (values)))
293
294
295 ;;; Emit code to allocate an object with a size in bytes given by
296 ;;; SIZE.  The size may be an integer or a TN. If Inline is a VOP
297 ;;; node-var then it is used to make an appropriate speed vs size
298 ;;; decision.
299
300 ;;; Allocation should only be used inside a pseudo-atomic section, which
301 ;;; should also cover subsequent initialization of the object.
302
303 ;;; (FIXME: so why aren't we asserting this?)
304
305 (defun allocation (alloc-tn size &optional inline dynamic-extent lowtag)
306   (declare (ignorable inline))
307   (cond
308     (dynamic-extent
309      (allocation-dynamic-extent alloc-tn size lowtag))
310     ((or (null inline) (policy inline (>= speed space)))
311      (allocation-inline alloc-tn size))
312     (t
313      (allocation-notinline alloc-tn size)))
314   (when (and lowtag (not dynamic-extent))
315     (inst lea alloc-tn (make-ea :byte :base alloc-tn :disp lowtag)))
316   (values))
317
318 ;;; Allocate an other-pointer object of fixed SIZE with a single word
319 ;;; header having the specified WIDETAG value. The result is placed in
320 ;;; RESULT-TN.
321 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline stack-allocate-p)
322                                  &body forms)
323   (unless forms
324     (bug "empty &body in WITH-FIXED-ALLOCATION"))
325   (once-only ((result-tn result-tn) (size size) (stack-allocate-p stack-allocate-p))
326     `(maybe-pseudo-atomic ,stack-allocate-p
327        (allocation ,result-tn (pad-data-block ,size) ,inline ,stack-allocate-p
328                    other-pointer-lowtag)
329        (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
330                ,result-tn 0 other-pointer-lowtag)
331        ,@forms)))
332 \f
333 ;;;; error code
334 (defun emit-error-break (vop kind code values)
335   (assemble ()
336     #!-ud2-breakpoints
337     (inst int 3)                        ; i386 breakpoint instruction
338     ;; CLH 20060314
339     ;; On Darwin, we need to use #x0b0f instead of int3 in order
340     ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
341     ;; doesn't seem to be reliably firing SIGTRAP
342     ;; handlers. Hopefully this will be fixed by Apple at a
343     ;; later date.
344     #!+ud2-breakpoints
345     (inst word #x0b0f)
346     ;; The return PC points here; note the location for the debugger.
347     (when vop
348       (note-this-location vop :internal-error))
349     (inst byte kind)                    ; e.g. trap_xyyy
350     (with-adjustable-vector (vector)    ; interr arguments
351       (write-var-integer code vector)
352       (dolist (tn values)
353         ;; classic CMU CL comment:
354         ;;   zzzzz jrd here. tn-offset is zero for constant
355         ;;   tns.
356         (write-var-integer (make-sc-offset (sc-number (tn-sc tn))
357                                            (or (tn-offset tn) 0))
358                            vector))
359       (inst byte (length vector))
360       (dotimes (i (length vector))
361         (inst byte (aref vector i))))))
362
363 (defun error-call (vop error-code &rest values)
364   #!+sb-doc
365   "Cause an error. ERROR-CODE is the error to cause."
366   (emit-error-break vop error-trap (error-number-or-lose error-code) values))
367
368 (defun generate-error-code (vop error-code &rest values)
369   #!+sb-doc
370   "Generate-Error-Code Error-code Value*
371   Emit code for an error with the specified Error-Code and context Values."
372   (assemble (*elsewhere*)
373     (let ((start-lab (gen-label)))
374       (emit-label start-lab)
375       (emit-error-break vop error-trap (error-number-or-lose error-code) values)
376       start-lab)))
377
378 \f
379 ;;;; PSEUDO-ATOMIC
380
381 ;;; This is used to wrap operations which leave untagged memory lying
382 ;;; around.  It's an operation which the AOP weenies would describe as
383 ;;; having "cross-cutting concerns", meaning it appears all over the
384 ;;; place and there's no logical single place to attach documentation.
385 ;;; grep (mostly in src/runtime) is your friend
386
387 ;;; KLUDGE: since the stack on the x86 is treated conservatively, it
388 ;;; does not matter whether a signal occurs during construction of a
389 ;;; dynamic-extent object, as the half-finished construction of the
390 ;;; object will not cause any difficulty.  We can therefore elide
391 (defmacro maybe-pseudo-atomic (not-really-p &body forms)
392   `(if ,not-really-p
393        (progn ,@forms)
394        (pseudo-atomic ,@forms)))
395
396 ;;; Unsafely clear pa flags so that the image can properly lose in a
397 ;;; pa section.
398 #!+sb-thread
399 (defmacro %clear-pseudo-atomic ()
400   #!+win32
401   `(progn)
402   #!-win32
403   '(inst mov (make-ea :dword :disp (* 4 thread-pseudo-atomic-bits-slot)) 0 :fs))
404
405 #!+sb-safepoint
406 (defun emit-safepoint ()
407   (inst test al-tn (make-ea :byte
408                             :disp (make-fixup "gc_safepoint_page" :foreign))))
409
410 #!+sb-thread
411 (defmacro pseudo-atomic (&rest forms)
412   #!+win32
413   `(progn ,@forms (emit-safepoint))
414   #!-win32
415   (with-unique-names (label)
416     `(let ((,label (gen-label)))
417        (inst mov (make-ea :dword :disp (* 4 thread-pseudo-atomic-bits-slot))
418              ebp-tn :fs)
419        ,@forms
420        (inst xor (make-ea :dword :disp (* 4 thread-pseudo-atomic-bits-slot))
421              ebp-tn :fs)
422        (inst jmp :z ,label)
423        ;; if PAI was set, interrupts were disabled at the same time
424        ;; using the process signal mask.
425        (inst break pending-interrupt-trap)
426        (emit-label ,label)
427        #!+sb-safepoint
428        ;; In this case, when allocation thinks a GC should be done, it
429        ;; does not mark PA as interrupted, but schedules a safepoint
430        ;; trap instead.  Let's take the opportunity to trigger that
431        ;; safepoint right now.
432        (emit-safepoint))))
433
434 #!-sb-thread
435 (defmacro pseudo-atomic (&rest forms)
436   (with-unique-names (label)
437     `(let ((,label (gen-label)))
438        (inst mov (make-ea-for-symbol-value *pseudo-atomic-bits* :dword)
439              ebp-tn)
440        ,@forms
441        (inst xor (make-ea-for-symbol-value *pseudo-atomic-bits* :dword)
442              ebp-tn)
443        (inst jmp :z ,label)
444        ;; if PAI was set, interrupts were disabled at the same time
445        ;; using the process signal mask.
446        (inst break pending-interrupt-trap)
447        (emit-label ,label))))
448 \f
449 ;;;; indexed references
450
451 (defmacro define-full-compare-and-swap
452     (name type offset lowtag scs el-type &optional translate)
453   `(progn
454      (define-vop (,name)
455          ,@(when translate `((:translate ,translate)))
456        (:policy :fast-safe)
457        (:args (object :scs (descriptor-reg) :to :eval)
458               (index :scs (any-reg immediate unsigned-reg) :to :result)
459               (old-value :scs ,scs :target eax)
460               (new-value :scs ,scs))
461        (:arg-types ,type tagged-num ,el-type ,el-type)
462        (:temporary (:sc descriptor-reg :offset eax-offset
463                         :from (:argument 2) :to :result :target value)  eax)
464        (:results (value :scs ,scs))
465        (:result-types ,el-type)
466        (:generator 5
467          (move eax old-value)
468          (let ((ea (sc-case index
469                      (immediate
470                       (make-ea :dword :base object
471                                :disp (- (* (+ ,offset (tn-value index))
472                                            n-word-bytes)
473                                         ,lowtag)))
474                      (unsigned-reg
475                       (make-ea :dword :base object :index index :scale 4
476                                :disp (- (* ,offset n-word-bytes)
477                                         ,lowtag)))
478                      (t
479                       (make-ea :dword :base object :index index
480                                :disp (- (* ,offset n-word-bytes)
481                                         ,lowtag))))))
482            (inst cmpxchg ea new-value :lock))
483          (move value eax)))))
484
485 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
486   `(progn
487      (define-vop (,name)
488        ,@(when translate
489            `((:translate ,translate)))
490        (:policy :fast-safe)
491        (:args (object :scs (descriptor-reg))
492               (index :scs (any-reg immediate unsigned-reg)))
493        (:arg-types ,type tagged-num)
494        (:results (value :scs ,scs))
495        (:result-types ,el-type)
496        (:generator 3                    ; pw was 5
497          (sc-case index
498            (immediate
499             (inst mov value (make-ea :dword :base object
500                                      :disp (- (* (+ ,offset (tn-value index))
501                                                  n-word-bytes)
502                                               ,lowtag))))
503            (unsigned-reg
504             (inst mov value (make-ea :dword :base object :index index :scale 4
505                                      :disp (- (* ,offset n-word-bytes)
506                                               ,lowtag))))
507            (t
508             (inst mov value (make-ea :dword :base object :index index
509                                      :disp (- (* ,offset n-word-bytes)
510                                               ,lowtag)))))))))
511
512 (defmacro define-full-reffer+offset (name type offset lowtag scs el-type &optional translate)
513   `(progn
514      (define-vop (,name)
515        ,@(when translate
516            `((:translate ,translate)))
517        (:policy :fast-safe)
518        (:args (object :scs (descriptor-reg))
519               (index :scs (any-reg immediate unsigned-reg)))
520        (:arg-types ,type tagged-num
521                    (:constant (constant-displacement ,lowtag sb!vm:n-word-bytes ,offset)))
522        (:info offset)
523        (:results (value :scs ,scs))
524        (:result-types ,el-type)
525        (:generator 3                    ; pw was 5
526          (sc-case index
527            (immediate
528             (inst mov value (make-ea :dword :base object
529                                      :disp (- (* (+ ,offset
530                                                     (tn-value index)
531                                                     offset)
532                                                  n-word-bytes)
533                                               ,lowtag))))
534            (unsigned-reg
535             (inst mov value (make-ea :dword :base object :index index :scale 4
536                                      :disp (- (* (+ ,offset offset)
537                                                  n-word-bytes)
538                                               ,lowtag))))
539            (t
540             (inst mov value (make-ea :dword :base object :index index
541                                      :disp (- (* (+ ,offset offset)
542                                                  n-word-bytes)
543                                               ,lowtag)))))))))
544
545 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
546   `(progn
547      (define-vop (,name)
548        ,@(when translate
549            `((:translate ,translate)))
550        (:policy :fast-safe)
551        (:args (object :scs (descriptor-reg))
552               (index :scs (any-reg immediate))
553               (value :scs ,scs :target result))
554        (:arg-types ,type tagged-num ,el-type)
555        (:results (result :scs ,scs))
556        (:result-types ,el-type)
557        (:generator 4                    ; was 5
558          (sc-case index
559            (immediate
560             (inst mov (make-ea :dword :base object
561                                :disp (- (* (+ ,offset (tn-value index))
562                                            n-word-bytes)
563                                         ,lowtag))
564                   value))
565            (t
566             (inst mov (make-ea :dword :base object :index index
567                                :disp (- (* ,offset n-word-bytes) ,lowtag))
568                   value)))
569         (move result value)))))
570
571 (defmacro define-full-setter+offset (name type offset lowtag scs el-type &optional translate)
572   `(progn
573      (define-vop (,name)
574        ,@(when translate
575            `((:translate ,translate)))
576        (:policy :fast-safe)
577        (:args (object :scs (descriptor-reg))
578               (index :scs (any-reg immediate))
579               (value :scs ,scs :target result))
580        (:info offset)
581        (:arg-types ,type tagged-num
582                    (:constant (constant-displacement ,lowtag sb!vm:n-word-bytes ,offset)) ,el-type)
583        (:results (result :scs ,scs))
584        (:result-types ,el-type)
585        (:generator 4                    ; was 5
586          (sc-case index
587            (immediate
588             (inst mov (make-ea :dword :base object
589                                :disp (- (* (+ ,offset (tn-value index) offset)
590                                            n-word-bytes)
591                                         ,lowtag))
592                   value))
593            (t
594             (inst mov (make-ea :dword :base object :index index
595                                :disp (- (* (+ ,offset offset)
596                                            n-word-bytes) ,lowtag))
597                   value)))
598         (move result value)))))
599
600 ;;; helper for alien stuff.
601
602 (def!macro with-pinned-objects ((&rest objects) &body body)
603   "Arrange with the garbage collector that the pages occupied by
604 OBJECTS will not be moved in memory for the duration of BODY.
605 Useful for e.g. foreign calls where another thread may trigger
606 collection."
607   (if objects
608       (let ((pins (make-gensym-list (length objects)))
609             (wpo (gensym "WITH-PINNED-OBJECTS-THUNK")))
610         ;; BODY is stuffed in a function to preserve the lexical
611         ;; environment.
612         `(flet ((,wpo () (progn ,@body)))
613            (declare (muffle-conditions compiler-note))
614            ;; PINS are dx-allocated in case the compiler for some
615            ;; unfathomable reason decides to allocate value-cells
616            ;; for them -- since we have DX value-cells on x86oid
617            ;; platforms this still forces them on the stack.
618            (dx-let ,(mapcar #'list pins objects)
619              (multiple-value-prog1 (,wpo)
620                ;; TOUCH-OBJECT has a VOP with an empty body: compiler
621                ;; thinks we're using the argument and doesn't flush
622                ;; the variable, but we don't have to pay any extra
623                ;; beyond that -- and MULTIPLE-VALUE-PROG1 keeps them
624                ;; live till the body has finished. *whew*
625                ,@(mapcar (lambda (pin)
626                            `(touch-object ,pin))
627                          pins)))))
628       `(progn ,@body)))
629
630 ;;; Helper to hide the fact that thread access on Windows needs one more
631 ;;; instruction, needs the FS prefix in that instruction _instead_ of
632 ;;; the actual load/store, and partially hide the resulting need for a
633 ;;; temporary TN when the non-windows might have have dereferenced an EA
634 ;;; without a TN as a base.
635
636 (defmacro with-tls-ea ((ea-var &key base
637                                     base-already-live-p
638                                     (disp-type :constant)
639                                     (disp 0))
640                        &body body)
641   "Execute BODY with various magic.  BODY is expected to emit instructions.
642
643    In the body, EA-VAR will be an alias for an EA which BODY can use to
644    perform a thread-local load or store.
645
646    Within the body, :MAYBE-FS will be replaced with :FS or NIL,
647    depending on the target, and needs to be included in any instruction
648    performing an access through the EA.
649
650    DISP-TYPE must be :INDEX, or :CONSTANT, and DISP must be an EA/TN,
651    or an expression returning an integer, respectively.
652
653    BASE must be a temporary TN, except in the following situation: BASE
654    will be unused when DISP-TYPE is constant, BASE-ALREADY-LIVE-P is
655    true, _and_ we're on POSIX.  This is an intentional optimization, and
656    the caller needs to take care to ignore the TN in this case, or can
657    omit this parameter.
658
659    BASE-ALREADY-LIVE-P means that at run-time, the BASE register already
660    holds an offset that we should add to instead of overwriting it.
661    The value of the BASE register is undefined following the macro invocation."
662   (check-type base-already-live-p boolean)
663   (check-type disp-type (member :index :constant))
664   #!-(and win32 sb-thread)
665   (let ((body (subst :fs :maybe-fs body)))
666     (ecase disp-type
667       (:constant
668        `(progn
669           ,@(subst (if base-already-live-p
670                        ;; use BASE and DISP
671                        `(make-ea :dword :base ,base :disp ,disp)
672                        ;; BASE not live and not needed, just use DISP
673                        `(make-ea :dword :disp ,disp))
674                    ea-var
675                    body)))
676       (:index
677        ;; need to use BASE in any case; and DISP is an EA
678        `(progn
679           (inst ,(if base-already-live-p 'add 'mov) ,base ,disp)
680           ,@(subst `(make-ea :dword :base ,base)
681                    ea-var
682                    body)))))
683   #!+(and win32 sb-thread)
684   ;; goes through a temporary register to add the thread address into it
685   (multiple-value-bind (constant-disp ea-disp)
686       (ecase disp-type
687         (:constant (values disp nil))
688         (:index    (values 0 disp)))
689     `(progn
690        ,@(when ea-disp
691            `((inst ,(if base-already-live-p 'add 'mov) ,base ,ea-disp)))
692        (inst ,(if (or base-already-live-p ea-disp) 'add 'mov)
693              ,base
694              (make-ea :dword :disp +win32-tib-arbitrary-field-offset+)
695              :fs)
696        ,@(subst `(make-ea :dword :base ,base :disp ,constant-disp)
697                 ea-var
698                 (subst nil :maybe-fs body)))))