0df9cf46ff237fabf9909b4938db2e0e8b876c88
[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             (inst mov temp-reg-tn ,value)
45             (inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) temp-reg-tn))
46            (t
47             (inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) ,value)))))
48
49 (defmacro pushw (ptr &optional (slot 0) (lowtag 0))
50   `(inst push (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
51
52 (defmacro popw (ptr &optional (slot 0) (lowtag 0))
53   `(inst pop (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
54 \f
55 ;;;; macros to generate useful values
56
57 (defmacro load-symbol (reg symbol)
58   `(inst mov ,reg (+ nil-value (static-symbol-offset ,symbol))))
59
60 (defmacro make-ea-for-symbol-value (symbol)
61   `(make-ea :qword
62     :disp (+ nil-value
63            (static-symbol-offset ',symbol)
64            (ash symbol-value-slot word-shift)
65            (- other-pointer-lowtag))))
66
67 (defmacro load-symbol-value (reg symbol)
68   `(inst mov ,reg (make-ea-for-symbol-value ,symbol)))
69
70 (defmacro store-symbol-value (reg symbol)
71   `(inst mov (make-ea-for-symbol-value ,symbol) ,reg))
72
73 #!+sb-thread
74 (defmacro make-ea-for-symbol-tls-index (symbol)
75   `(make-ea :qword
76     :disp (+ nil-value
77            (static-symbol-offset ',symbol)
78            (ash symbol-tls-index-slot word-shift)
79            (- other-pointer-lowtag))))
80
81 #!+sb-thread
82 (defmacro load-tl-symbol-value (reg symbol)
83   `(progn
84     (inst mov ,reg (make-ea-for-symbol-tls-index ,symbol))
85     (inst mov ,reg (make-ea :qword :base thread-base-tn :scale 1 :index ,reg))))
86 #!-sb-thread
87 (defmacro load-tl-symbol-value (reg symbol) `(load-symbol-value ,reg ,symbol))
88
89 #!+sb-thread
90 (defmacro store-tl-symbol-value (reg symbol temp)
91   `(progn
92     (inst mov ,temp (make-ea-for-symbol-tls-index ,symbol))
93     (inst mov (make-ea :qword :base thread-base-tn :scale 1 :index ,temp) ,reg)))
94 #!-sb-thread
95 (defmacro store-tl-symbol-value (reg symbol temp)
96   (declare (ignore temp))
97   `(store-symbol-value ,reg ,symbol))
98
99 (defmacro load-binding-stack-pointer (reg)
100   #!+sb-thread
101   `(inst mov ,reg (make-ea :qword :base thread-base-tn
102                    :disp (* n-word-bytes thread-binding-stack-pointer-slot)))
103   #!-sb-thread
104   `(load-symbol-value ,reg *binding-stack-pointer*))
105
106 (defmacro store-binding-stack-pointer (reg)
107   #!+sb-thread
108   `(inst mov (make-ea :qword :base thread-base-tn
109               :disp (* n-word-bytes thread-binding-stack-pointer-slot))
110     ,reg)
111   #!-sb-thread
112   `(store-symbol-value ,reg *binding-stack-pointer*))
113
114 (defmacro load-type (target source &optional (offset 0))
115   #!+sb-doc
116   "Loads the type bits of a pointer into target independent of
117    byte-ordering issues."
118   (once-only ((n-target target)
119               (n-source source)
120               (n-offset offset))
121     (ecase *backend-byte-order*
122       (:little-endian
123        `(inst movzx ,n-target
124               (make-ea :byte :base ,n-source :disp ,n-offset)))
125       (:big-endian
126        `(inst movzx ,n-target
127               (make-ea :byte :base ,n-source
128                              :disp (+ ,n-offset (1- n-word-bytes))))))))
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 lowtag)
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 lea alloc-tn (make-ea :byte :base rsp-tn :disp lowtag))
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 lowtag)
152   (inst push size)
153   (inst lea temp-reg-tn (make-ea :qword
154                             :disp (make-fixup "alloc_tramp" :foreign)))
155   (inst call temp-reg-tn)
156   (inst pop alloc-tn)
157   (when lowtag
158     (inst lea alloc-tn (make-ea :byte :base alloc-tn :disp lowtag)))
159   (values))
160
161 (defun allocation (alloc-tn size &optional ignored dynamic-extent lowtag)
162   (declare (ignore ignored))
163   (when dynamic-extent
164     (allocation-dynamic-extent alloc-tn size lowtag)
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 lowtag))
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            (if lowtag
205                (inst lea alloc-tn (make-ea :byte :base temp-reg-tn :disp lowtag))
206                (inst mov alloc-tn temp-reg-tn))
207            (emit-label DONE)
208            (assemble (*elsewhere*)
209              (emit-label NOT-INLINE)
210              (cond ((numberp size)
211                     (allocation-tramp alloc-tn size lowtag))
212                    (t
213                     (inst sub alloc-tn free-pointer)
214                     (allocation-tramp alloc-tn alloc-tn lowtag)))
215              (inst jmp DONE))))
216     (values)))
217
218 ;;; Allocate an other-pointer object of fixed SIZE with a single word
219 ;;; header having the specified WIDETAG value. The result is placed in
220 ;;; RESULT-TN.
221 (defmacro with-fixed-allocation ((result-tn widetag size &optional inline stack-allocate-p)
222                                  &body forms)
223   (unless forms
224     (bug "empty &body in WITH-FIXED-ALLOCATION"))
225   (once-only ((result-tn result-tn) (size size) (stack-allocate-p stack-allocate-p))
226     `(maybe-pseudo-atomic ,stack-allocate-p
227       (allocation ,result-tn (pad-data-block ,size) ,inline ,stack-allocate-p
228                   other-pointer-lowtag)
229       (storew (logior (ash (1- ,size) n-widetag-bits) ,widetag)
230               ,result-tn 0 other-pointer-lowtag)
231       ,@forms)))
232 \f
233 ;;;; error code
234 (defun emit-error-break (vop kind code values)
235   (assemble ()
236     #!-ud2-breakpoints
237     (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     #!+ud2-breakpoints
245     (inst word #x0b0f)
246     ;; The return PC points here; note the location for the debugger.
247     (when vop
248       (note-this-location vop :internal-error))
249     (inst byte kind)                       ; eg trap_Xyyy
250     (with-adjustable-vector (vector)       ; interr arguments
251       (write-var-integer code vector)
252       (dolist (tn values)
253         ;; classic CMU CL comment:
254         ;;   zzzzz jrd here. tn-offset is zero for constant
255         ;;   tns.
256         (write-var-integer (make-sc-offset (sc-number (tn-sc tn))
257                                            (or (tn-offset tn) 0))
258                            vector))
259       (inst byte (length vector))
260       (dotimes (i (length vector))
261         (inst byte (aref vector i))))))
262
263 (defun error-call (vop error-code &rest values)
264   #!+sb-doc
265   "Cause an error. ERROR-CODE is the error to cause."
266   (emit-error-break vop error-trap (error-number-or-lose error-code) values))
267
268 (defun generate-error-code (vop error-code &rest values)
269   #!+sb-doc
270   "Generate-Error-Code Error-code Value*
271   Emit code for an error with the specified Error-Code and context Values."
272   (assemble (*elsewhere*)
273     (let ((start-lab (gen-label)))
274       (emit-label start-lab)
275       (emit-error-break vop error-trap (error-number-or-lose error-code) values)
276        start-lab)))
277
278 \f
279 ;;;; PSEUDO-ATOMIC
280
281 ;;; This is used to wrap operations which leave untagged memory lying
282 ;;; around.  It's an operation which the AOP weenies would describe as
283 ;;; having "cross-cutting concerns", meaning it appears all over the
284 ;;; place and there's no logical single place to attach documentation.
285 ;;; grep (mostly in src/runtime) is your friend
286
287 (defmacro maybe-pseudo-atomic (not-really-p &body body)
288   `(if ,not-really-p
289        (progn ,@body)
290        (pseudo-atomic ,@body)))
291
292 ;;; Unsafely clear pa flags so that the image can properly lose in a
293 ;;; pa section.
294 #!+sb-thread
295 (defmacro %clear-pseudo-atomic ()
296   '(inst mov (make-ea :qword :base thread-base-tn
297               :disp (* n-word-bytes thread-pseudo-atomic-bits-slot))
298     0))
299
300 #!+sb-safepoint
301 (defun emit-safepoint ()
302   (inst test al-tn (make-ea :byte
303                             :disp (make-fixup "gc_safepoint_page" :foreign))))
304
305 #!+sb-thread
306 (defmacro pseudo-atomic (&rest forms)
307   (with-unique-names (label)
308     `(let ((,label (gen-label)))
309        (inst mov (make-ea :qword
310                           :base thread-base-tn
311                           :disp (* n-word-bytes thread-pseudo-atomic-bits-slot))
312              rbp-tn)
313        ,@forms
314        (inst xor (make-ea :qword
315                           :base thread-base-tn
316                           :disp (* n-word-bytes thread-pseudo-atomic-bits-slot))
317              rbp-tn)
318        (inst jmp :z ,label)
319        ;; if PAI was set, interrupts were disabled at the same time
320        ;; using the process signal mask.
321        (inst break pending-interrupt-trap)
322        (emit-label ,label)
323        #!+sb-safepoint
324        ;; In this case, when allocation thinks a GC should be done, it
325        ;; does not mark PA as interrupted, but schedules a safepoint
326        ;; trap instead.  Let's take the opportunity to trigger that
327        ;; safepoint right now.
328        (emit-safepoint))))
329
330
331 #!-sb-thread
332 (defmacro pseudo-atomic (&rest forms)
333   (with-unique-names (label)
334     `(let ((,label (gen-label)))
335        ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
336        ;; something. (perhaps SVLB, for static variable low byte)
337        (inst mov (make-ea :qword :disp (+ nil-value
338                                           (static-symbol-offset
339                                            '*pseudo-atomic-bits*)
340                                           (ash symbol-value-slot word-shift)
341                                           (- other-pointer-lowtag)))
342              rbp-tn)
343        ,@forms
344        (inst xor (make-ea :qword :disp (+ nil-value
345                                           (static-symbol-offset
346                                            '*pseudo-atomic-bits*)
347                                           (ash symbol-value-slot word-shift)
348                                           (- other-pointer-lowtag)))
349              rbp-tn)
350        (inst jmp :z ,label)
351        ;; if PAI was set, interrupts were disabled at the same time
352        ;; using the process signal mask.
353        (inst break pending-interrupt-trap)
354        (emit-label ,label))))
355 \f
356 ;;;; indexed references
357
358 (defmacro define-full-compare-and-swap
359     (name type offset lowtag scs el-type &optional translate)
360   `(progn
361      (define-vop (,name)
362          ,@(when translate `((:translate ,translate)))
363        (:policy :fast-safe)
364        (:args (object :scs (descriptor-reg) :to :eval)
365               (index :scs (any-reg) :to :result)
366               (old-value :scs ,scs :target rax)
367               (new-value :scs ,scs))
368        (:arg-types ,type tagged-num ,el-type ,el-type)
369        (:temporary (:sc descriptor-reg :offset rax-offset
370                         :from (:argument 2) :to :result :target value)  rax)
371        (:results (value :scs ,scs))
372        (:result-types ,el-type)
373        (:generator 5
374          (move rax old-value)
375          (inst cmpxchg (make-ea :qword :base object :index index
376                                 :scale (ash 1 (- word-shift n-fixnum-tag-bits))
377                                 :disp (- (* ,offset n-word-bytes) ,lowtag))
378                new-value :lock)
379          (move value rax)))))
380
381 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
382   `(progn
383      (define-vop (,name)
384        ,@(when translate
385            `((:translate ,translate)))
386        (:policy :fast-safe)
387        (:args (object :scs (descriptor-reg))
388               (index :scs (any-reg)))
389        (:arg-types ,type tagged-num)
390        (:results (value :scs ,scs))
391        (:result-types ,el-type)
392        (:generator 3                    ; pw was 5
393          (inst mov value (make-ea :qword :base object :index index
394                                   :scale (ash 1 (- word-shift n-fixnum-tag-bits))
395                                   :disp (- (* ,offset n-word-bytes)
396                                            ,lowtag)))))
397      (define-vop (,(symbolicate name "-C"))
398        ,@(when translate
399            `((:translate ,translate)))
400        (:policy :fast-safe)
401        (:args (object :scs (descriptor-reg)))
402        (:info index)
403        (:arg-types ,type
404                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
405                                                 ,(eval offset))))
406        (:results (value :scs ,scs))
407        (:result-types ,el-type)
408        (:generator 2                    ; pw was 5
409          (inst mov value (make-ea :qword :base object
410                                   :disp (- (* (+ ,offset index) n-word-bytes)
411                                            ,lowtag)))))))
412
413 (defmacro define-full-reffer+offset (name type offset lowtag scs el-type &optional translate)
414   `(progn
415      (define-vop (,name)
416        ,@(when translate
417            `((:translate ,translate)))
418        (:policy :fast-safe)
419        (:args (object :scs (descriptor-reg))
420               (index :scs (any-reg)))
421        (:info offset)
422        (:arg-types ,type tagged-num
423                    (:constant (constant-displacement other-pointer-lowtag
424                                                      n-word-bytes vector-data-offset)))
425        (:results (value :scs ,scs))
426        (:result-types ,el-type)
427        (:generator 3                    ; pw was 5
428          (inst mov value (make-ea :qword :base object :index index
429                                   :scale (ash 1 (- word-shift n-fixnum-tag-bits))
430                                   :disp (- (* (+ ,offset offset) n-word-bytes)
431                                            ,lowtag)))))
432      (define-vop (,(symbolicate name "-C"))
433        ,@(when translate
434            `((:translate ,translate)))
435        (:policy :fast-safe)
436        (:args (object :scs (descriptor-reg)))
437        (:info index offset)
438        (:arg-types ,type
439                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
440                                                 ,(eval offset)))
441                    (:constant (constant-displacement other-pointer-lowtag
442                                                      n-word-bytes vector-data-offset)))
443        (:results (value :scs ,scs))
444        (:result-types ,el-type)
445        (:generator 2                    ; pw was 5
446          (inst mov value (make-ea :qword :base object
447                                   :disp (- (* (+ ,offset index offset) n-word-bytes)
448                                            ,lowtag)))))))
449
450 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
451   `(progn
452      (define-vop (,name)
453        ,@(when translate
454            `((:translate ,translate)))
455        (:policy :fast-safe)
456        (:args (object :scs (descriptor-reg))
457               (index :scs (any-reg))
458               (value :scs ,scs :target result))
459        (:arg-types ,type tagged-num ,el-type)
460        (:results (result :scs ,scs))
461        (:result-types ,el-type)
462        (:generator 4                    ; was 5
463          (inst mov (make-ea :qword :base object :index index
464                             :scale (ash 1 (- word-shift n-fixnum-tag-bits))
465                             :disp (- (* ,offset n-word-bytes) ,lowtag))
466                value)
467          (move result value)))
468      (define-vop (,(symbolicate name "-C"))
469        ,@(when translate
470            `((:translate ,translate)))
471        (:policy :fast-safe)
472        (:args (object :scs (descriptor-reg))
473               (value :scs ,scs :target result))
474        (:info index)
475        (:arg-types ,type
476                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
477                                                 ,(eval offset)))
478                    ,el-type)
479        (:results (result :scs ,scs))
480        (:result-types ,el-type)
481        (:generator 3                    ; was 5
482          (inst mov (make-ea :qword :base object
483                             :disp (- (* (+ ,offset index) n-word-bytes)
484                                      ,lowtag))
485                value)
486          (move result value)))))
487
488 (defmacro define-full-setter+offset (name type offset lowtag scs el-type &optional translate)
489   `(progn
490      (define-vop (,name)
491        ,@(when translate
492            `((:translate ,translate)))
493        (:policy :fast-safe)
494        (:args (object :scs (descriptor-reg))
495               (index :scs (any-reg))
496               (value :scs ,scs :target result))
497        (:info offset)
498        (:arg-types ,type tagged-num
499                    (:constant (constant-displacement other-pointer-lowtag
500                                                      n-word-bytes
501                                                      vector-data-offset))
502                    ,el-type)
503        (:results (result :scs ,scs))
504        (:result-types ,el-type)
505        (:generator 4                    ; was 5
506          (inst mov (make-ea :qword :base object :index index
507                             :scale (ash 1 (- word-shift n-fixnum-tag-bits))
508                             :disp (- (* (+ ,offset offset) n-word-bytes) ,lowtag))
509                value)
510          (move result value)))
511      (define-vop (,(symbolicate name "-C"))
512        ,@(when translate
513            `((:translate ,translate)))
514        (:policy :fast-safe)
515        (:args (object :scs (descriptor-reg))
516               (value :scs ,scs :target result))
517        (:info index offset)
518        (:arg-types ,type
519                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
520                                                 ,(eval offset)))
521                    (:constant (constant-displacement other-pointer-lowtag
522                                                      n-word-bytes
523                                                      vector-data-offset))
524                    ,el-type)
525        (:results (result :scs ,scs))
526        (:result-types ,el-type)
527        (:generator 3                    ; was 5
528          (inst mov (make-ea :qword :base object
529                             :disp (- (* (+ ,offset index offset) n-word-bytes)
530                                      ,lowtag))
531                value)
532          (move result value)))))
533
534 ;;; helper for alien stuff.
535
536 (def!macro with-pinned-objects ((&rest objects) &body body)
537   "Arrange with the garbage collector that the pages occupied by
538 OBJECTS will not be moved in memory for the duration of BODY.
539 Useful for e.g. foreign calls where another thread may trigger
540 collection."
541   (if objects
542       (let ((pins (make-gensym-list (length objects)))
543             (wpo (gensym "WITH-PINNED-OBJECTS-THUNK")))
544         ;; BODY is stuffed in a function to preserve the lexical
545         ;; environment.
546         `(flet ((,wpo () (progn ,@body)))
547            (declare (muffle-conditions compiler-note))
548            ;; PINS are dx-allocated in case the compiler for some
549            ;; unfathomable reason decides to allocate value-cells
550            ;; for them -- since we have DX value-cells on x86oid
551            ;; platforms this still forces them on the stack.
552            (dx-let ,(mapcar #'list pins objects)
553              (multiple-value-prog1 (,wpo)
554                ;; TOUCH-OBJECT has a VOP with an empty body: compiler
555                ;; thinks we're using the argument and doesn't flush
556                ;; the variable, but we don't have to pay any extra
557                ;; beyond that -- and MULTIPLE-VALUE-PROG1 keeps them
558                ;; live till the body has finished. *whew*
559                ,@(mapcar (lambda (pin)
560                            `(touch-object ,pin))
561                          pins)))))
562       `(progn ,@body)))