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