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