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