9393c2b593974360dc3a1ef41f6539e29c395f3a
[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
129                              :disp (+ ,n-offset (1- n-word-bytes))))))))
130 \f
131 ;;;; allocation helpers
132
133 ;;; All allocation is done by calls to assembler routines that
134 ;;; eventually invoke the C alloc() function.
135
136 ;;; Emit code to allocate an object with a size in bytes given by
137 ;;; Size. The size may be an integer of a TN. If Inline is a VOP
138 ;;; node-var then it is used to make an appropriate speed vs size
139 ;;; decision.
140
141 (defun allocation-dynamic-extent (alloc-tn size)
142   (inst sub rsp-tn size)
143   ;; see comment in x86/macros.lisp implementation of this
144   (inst and rsp-tn #.(lognot lowtag-mask))
145   (aver (not (location= alloc-tn rsp-tn)))
146   (inst mov alloc-tn rsp-tn)
147   (values))
148
149 ;;; This macro should only be used inside a pseudo-atomic section,
150 ;;; which should also cover subsequent initialization of the
151 ;;; object.
152 (defun allocation-tramp (alloc-tn size &optional ignored)
153   (declare (ignore ignored))
154   (inst push size)
155   (inst lea temp-reg-tn (make-ea :qword
156                             :disp (make-fixup "alloc_tramp" :foreign)))
157   (inst call temp-reg-tn)
158   (inst pop alloc-tn)
159   (values))
160
161 (defun allocation (alloc-tn size &optional ignored dynamic-extent)
162   (declare (ignore ignored))
163   (when dynamic-extent
164     (allocation-dynamic-extent alloc-tn size)
165     (return-from allocation (values)))
166   (let ((NOT-INLINE (gen-label))
167         (DONE (gen-label))
168         ;; Yuck.
169         (in-elsewhere (eq *elsewhere* sb!assem::**current-segment**))
170         ;; thread->alloc_region.free_pointer
171         (free-pointer
172          #!+sb-thread
173          (make-ea :qword
174                   :base thread-base-tn :scale 1
175                   :disp (* n-word-bytes thread-alloc-region-slot))
176          #!-sb-thread
177          (make-ea :qword
178                   :scale 1 :disp
179                   (make-fixup "boxed_region" :foreign)))
180         ;; thread->alloc_region.end_addr
181         (end-addr
182          #!+sb-thread
183          (make-ea :qword
184                   :base thread-base-tn :scale 1
185                   :disp (* n-word-bytes (1+ thread-alloc-region-slot)))
186          #!-sb-thread
187          (make-ea :qword
188                   :scale 1 :disp
189                   (make-fixup "boxed_region" :foreign 8))))
190     (cond (in-elsewhere
191            (allocation-tramp alloc-tn size))
192           (t
193            (inst mov temp-reg-tn free-pointer)
194            (if (tn-p size)
195                (if (location= alloc-tn size)
196                    (inst add alloc-tn temp-reg-tn)
197                    (inst lea alloc-tn
198                          (make-ea :qword :base temp-reg-tn :index size)))
199                (inst lea alloc-tn
200                      (make-ea :qword :base temp-reg-tn :disp size)))
201            (inst cmp end-addr alloc-tn)
202            (inst jmp :be NOT-INLINE)
203            (inst mov free-pointer alloc-tn)
204            (inst mov alloc-tn temp-reg-tn)
205            (emit-label DONE)
206            (assemble (*elsewhere*)
207              (emit-label NOT-INLINE)
208              (cond ((numberp size)
209                     (allocation-tramp alloc-tn size))
210                    (t
211                     (inst sub alloc-tn free-pointer)
212                     (allocation-tramp alloc-tn alloc-tn)))
213              (inst jmp DONE))
214            (values)))))
215
216 ;;; Allocate an other-pointer object of fixed SIZE with a single word
217 ;;; header having the specified WIDETAG value. The result is placed in
218 ;;; RESULT-TN.
219 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline stack-allocate-p)
220                                  &body forms)
221   (unless forms
222     (bug "empty &body in WITH-FIXED-ALLOCATION"))
223   (once-only ((result-tn result-tn) (size size) (stack-allocate-p stack-allocate-p))
224     `(maybe-pseudo-atomic ,stack-allocate-p
225       (allocation ,result-tn (pad-data-block ,size) ,inline ,stack-allocate-p)
226       (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
227               ,result-tn)
228       (inst lea ,result-tn
229             (make-ea :qword :base ,result-tn :disp other-pointer-lowtag))
230       ,@forms)))
231 \f
232 ;;;; error code
233 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
234   (defun emit-error-break (vop kind code values)
235     (let ((vector (gensym)))
236       `((progn
237           #!-darwin (inst int 3)                  ; i386 breakpoint instruction
238           ;; On Darwin, we need to use #x0b0f instead of int3 in order
239           ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
240           ;; doesn't seem to be reliably firing SIGTRAP
241           ;; handlers. Hopefully this will be fixed by Apple at a
242           ;; later date. Do the same on x86-64 as we do on x86 until this gets
243           ;; sorted out.
244           #!+darwin (inst word #x0b0f))
245
246         ;; The return PC points here; note the location for the debugger.
247         (let ((vop ,vop))
248           (when vop
249                 (note-this-location vop :internal-error)))
250         (inst byte ,kind)                       ; eg trap_Xyyy
251         (with-adjustable-vector (,vector)       ; interr arguments
252           (write-var-integer (error-number-or-lose ',code) ,vector)
253           ,@(mapcar (lambda (tn)
254                       `(let ((tn ,tn))
255                          ;; classic CMU CL comment:
256                          ;;   zzzzz jrd here. tn-offset is zero for constant
257                          ;;   tns.
258                          (write-var-integer (make-sc-offset (sc-number
259                                                              (tn-sc tn))
260                                                             (or (tn-offset tn)
261                                                                 0))
262                                             ,vector)))
263                     values)
264           (inst byte (length ,vector))
265           (dotimes (i (length ,vector))
266             (inst byte (aref ,vector i))))))))
267
268 (defmacro error-call (vop error-code &rest values)
269   #!+sb-doc
270   "Cause an error. ERROR-CODE is the error to cause."
271   (cons 'progn
272         (emit-error-break vop error-trap error-code values)))
273
274 (defmacro generate-error-code (vop error-code &rest values)
275   #!+sb-doc
276   "Generate-Error-Code Error-code Value*
277   Emit code for an error with the specified Error-Code and context Values."
278   `(assemble (*elsewhere*)
279      (let ((start-lab (gen-label)))
280        (emit-label start-lab)
281        (error-call ,vop ,error-code ,@values)
282        start-lab)))
283
284 \f
285 ;;;; PSEUDO-ATOMIC
286
287 ;;; This is used to wrap operations which leave untagged memory lying
288 ;;; around.  It's an operation which the AOP weenies would describe as
289 ;;; having "cross-cutting concerns", meaning it appears all over the
290 ;;; place and there's no logical single place to attach documentation.
291 ;;; grep (mostly in src/runtime) is your friend
292
293 (defmacro maybe-pseudo-atomic (not-really-p &body body)
294   `(if ,not-really-p
295        (progn ,@body)
296        (pseudo-atomic ,@body)))
297
298 #!+sb-thread
299 (defmacro pseudo-atomic (&rest forms)
300   (with-unique-names (label)
301     `(let ((,label (gen-label)))
302       (inst or (make-ea :byte
303                  :base thread-base-tn
304                  :disp (* 8 thread-pseudo-atomic-bits-slot))
305             (fixnumize 1))
306       ,@forms
307       (inst xor (make-ea :byte
308                  :base thread-base-tn
309                  :disp (* 8 thread-pseudo-atomic-bits-slot))
310             (fixnumize 1))
311       (inst jmp :z ,label)
312       ;; if PAI was set, interrupts were disabled at the same
313       ;; time using the process signal mask.
314       (inst break pending-interrupt-trap)
315       (emit-label ,label))))
316
317
318 #!-sb-thread
319 (defmacro pseudo-atomic (&rest forms)
320   (with-unique-names (label)
321     `(let ((,label (gen-label)))
322       ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
323       ;; something. (perhaps SVLB, for static variable low byte)
324       (inst or (make-ea :byte :disp (+ nil-value
325                                        (static-symbol-offset
326                                         '*pseudo-atomic-bits*)
327                                        (ash symbol-value-slot word-shift)
328                                        (- other-pointer-lowtag)))
329             (fixnumize 1))
330       ,@forms
331       (inst xor (make-ea :byte :disp (+ nil-value
332                                         (static-symbol-offset
333                                          '*pseudo-atomic-bits*)
334                                         (ash symbol-value-slot word-shift)
335                                         (- other-pointer-lowtag)))
336             (fixnumize 1))
337       (inst jmp :z ,label)
338       ;; if PAI was set, interrupts were disabled at the same time
339       ;; using the process signal mask.
340       (inst break pending-interrupt-trap)
341       (emit-label ,label))))
342
343
344 \f
345 ;;;; indexed references
346
347 (defmacro define-full-compare-and-swap
348     (name type offset lowtag scs el-type &optional translate)
349   `(progn
350      (define-vop (,name)
351          ,@(when translate `((:translate ,translate)))
352        (:policy :fast-safe)
353        (:args (object :scs (descriptor-reg) :to :eval)
354               (index :scs (any-reg) :to :result)
355               (old-value :scs ,scs :target rax)
356               (new-value :scs ,scs))
357        (:arg-types ,type tagged-num ,el-type ,el-type)
358        (:temporary (:sc descriptor-reg :offset rax-offset
359                         :from (:argument 2) :to :result :target value)  rax)
360        (:results (value :scs ,scs))
361        (:result-types ,el-type)
362        (:generator 5
363          (move rax old-value)
364          #!+sb-thread
365          (inst lock)
366          (inst cmpxchg (make-ea :qword :base object :index index
367                                 :disp (- (* ,offset n-word-bytes) ,lowtag))
368                new-value)
369          (move value rax)))))
370
371 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
372   `(progn
373      (define-vop (,name)
374        ,@(when translate
375            `((:translate ,translate)))
376        (:policy :fast-safe)
377        (:args (object :scs (descriptor-reg))
378               (index :scs (any-reg)))
379        (:arg-types ,type tagged-num)
380        (:results (value :scs ,scs))
381        (:result-types ,el-type)
382        (:generator 3                    ; pw was 5
383          (inst mov value (make-ea :qword :base object :index index
384                                   :disp (- (* ,offset n-word-bytes)
385                                            ,lowtag)))))
386      (define-vop (,(symbolicate name "-C"))
387        ,@(when translate
388            `((:translate ,translate)))
389        (:policy :fast-safe)
390        (:args (object :scs (descriptor-reg)))
391        (:info index)
392        (:arg-types ,type
393                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
394                                                 ,(eval offset))))
395        (:results (value :scs ,scs))
396        (:result-types ,el-type)
397        (:generator 2                    ; pw was 5
398          (inst mov value (make-ea :qword :base object
399                                   :disp (- (* (+ ,offset index) n-word-bytes)
400                                            ,lowtag)))))))
401
402 (defmacro define-full-reffer+offset (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        (:info offset)
411        (:arg-types ,type tagged-num
412                    (:constant (constant-displacement other-pointer-lowtag
413                                                      n-word-bytes vector-data-offset)))
414        (:results (value :scs ,scs))
415        (:result-types ,el-type)
416        (:generator 3                    ; pw was 5
417          (inst mov value (make-ea :qword :base object :index index
418                                   :disp (- (* (+ ,offset offset) n-word-bytes)
419                                            ,lowtag)))))
420      (define-vop (,(symbolicate name "-C"))
421        ,@(when translate
422            `((:translate ,translate)))
423        (:policy :fast-safe)
424        (:args (object :scs (descriptor-reg)))
425        (:info index offset)
426        (:arg-types ,type
427                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
428                                                 ,(eval offset)))
429                    (:constant (constant-displacement other-pointer-lowtag
430                                                      n-word-bytes vector-data-offset)))
431        (:results (value :scs ,scs))
432        (:result-types ,el-type)
433        (:generator 2                    ; pw was 5
434          (inst mov value (make-ea :qword :base object
435                                   :disp (- (* (+ ,offset index offset) n-word-bytes)
436                                            ,lowtag)))))))
437
438 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
439   `(progn
440      (define-vop (,name)
441        ,@(when translate
442            `((:translate ,translate)))
443        (:policy :fast-safe)
444        (:args (object :scs (descriptor-reg))
445               (index :scs (any-reg))
446               (value :scs ,scs :target result))
447        (:arg-types ,type tagged-num ,el-type)
448        (:results (result :scs ,scs))
449        (:result-types ,el-type)
450        (:generator 4                    ; was 5
451          (inst mov (make-ea :qword :base object :index index
452                             :disp (- (* ,offset n-word-bytes) ,lowtag))
453                value)
454          (move result value)))
455      (define-vop (,(symbolicate name "-C"))
456        ,@(when translate
457            `((:translate ,translate)))
458        (:policy :fast-safe)
459        (:args (object :scs (descriptor-reg))
460               (value :scs ,scs :target result))
461        (:info index)
462        (:arg-types ,type
463                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
464                                                 ,(eval offset)))
465                    ,el-type)
466        (:results (result :scs ,scs))
467        (:result-types ,el-type)
468        (:generator 3                    ; was 5
469          (inst mov (make-ea :qword :base object
470                             :disp (- (* (+ ,offset index) n-word-bytes)
471                                      ,lowtag))
472                value)
473          (move result value)))))
474
475 (defmacro define-full-setter+offset (name type offset lowtag scs el-type &optional translate)
476   `(progn
477      (define-vop (,name)
478        ,@(when translate
479            `((:translate ,translate)))
480        (:policy :fast-safe)
481        (:args (object :scs (descriptor-reg))
482               (index :scs (any-reg))
483               (value :scs ,scs :target result))
484        (:info offset)
485        (:arg-types ,type tagged-num
486                    (:constant (constant-displacement other-pointer-lowtag
487                                                      n-word-bytes
488                                                      vector-data-offset))
489                    ,el-type)
490        (:results (result :scs ,scs))
491        (:result-types ,el-type)
492        (:generator 4                    ; was 5
493          (inst mov (make-ea :qword :base object :index index
494                             :disp (- (* (+ ,offset offset) n-word-bytes) ,lowtag))
495                value)
496          (move result value)))
497      (define-vop (,(symbolicate name "-C"))
498        ,@(when translate
499            `((:translate ,translate)))
500        (:policy :fast-safe)
501        (:args (object :scs (descriptor-reg))
502               (value :scs ,scs :target result))
503        (:info index offset)
504        (:arg-types ,type
505                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
506                                                 ,(eval offset)))
507                    (:constant (constant-displacement other-pointer-lowtag
508                                                      n-word-bytes
509                                                      vector-data-offset))
510                    ,el-type)
511        (:results (result :scs ,scs))
512        (:result-types ,el-type)
513        (:generator 3                    ; was 5
514          (inst mov (make-ea :qword :base object
515                             :disp (- (* (+ ,offset index offset) n-word-bytes)
516                                      ,lowtag))
517                value)
518          (move result value)))))
519
520 ;;; helper for alien stuff.
521
522 (def!macro with-pinned-objects ((&rest objects) &body body)
523   "Arrange with the garbage collector that the pages occupied by
524 OBJECTS will not be moved in memory for the duration of BODY.
525 Useful for e.g. foreign calls where another thread may trigger
526 collection."
527   (if objects
528       (let ((pins (make-gensym-list (length objects)))
529             (wpo (block-gensym "WPO")))
530         ;; BODY is stuffed in a function to preserve the lexical
531         ;; environment.
532         `(flet ((,wpo () (progn ,@body)))
533            ;; PINS are dx-allocated in case the compiler for some
534            ;; unfathomable reason decides to allocate value-cells
535            ;; for them -- since we have DX value-cells on x86oid
536            ;; platforms this still forces them on the stack.
537            (dx-let ,(mapcar #'list pins objects)
538              (multiple-value-prog1 (,wpo)
539                ;; TOUCH-OBJECT has a VOP with an empty body: compiler
540                ;; thinks we're using the argument and doesn't flush
541                ;; the variable, but we don't have to pay any extra
542                ;; beyond that -- and MULTIPLE-VALUE-PROG1 keeps them
543                ;; live till the body has finished. *whew*
544                ,@(mapcar (lambda (pin)
545                            `(touch-object ,pin))
546                          pins)))))
547       `(progn ,@body)))