1.0.17.24: refactor handling of constants in the compiler
[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 (defun emit-error-break (vop kind code values)
234   (assemble ()
235     #!-darwin
236     (inst int 3)                  ; i386 breakpoint instruction
237     ;; On Darwin, we need to use #x0b0f instead of int3 in order
238     ;; to generate a SIGILL instead of a SIGTRAP as darwin/x86
239     ;; doesn't seem to be reliably firing SIGTRAP
240     ;; handlers. Hopefully this will be fixed by Apple at a
241     ;; later date. Do the same on x86-64 as we do on x86 until this gets
242     ;; sorted out.
243     #!+darwin
244     (inst word #x0b0f)
245     ;; The return PC points here; note the location for the debugger.
246     (when vop
247       (note-this-location vop :internal-error))
248     (inst byte kind)                       ; eg trap_Xyyy
249     (with-adjustable-vector (vector)       ; interr arguments
250       (write-var-integer code vector)
251       (dolist (tn values)
252         ;; classic CMU CL comment:
253         ;;   zzzzz jrd here. tn-offset is zero for constant
254         ;;   tns.
255         (write-var-integer (make-sc-offset (sc-number (tn-sc tn))
256                                            (or (tn-offset tn) 0))
257                            vector))
258       (inst byte (length vector))
259       (dotimes (i (length vector))
260         (inst byte (aref vector i))))))
261
262 (defun error-call (vop error-code &rest values)
263   #!+sb-doc
264   "Cause an error. ERROR-CODE is the error to cause."
265   (emit-error-break vop error-trap (error-number-or-lose error-code) values))
266
267 (defun generate-error-code (vop error-code &rest values)
268   #!+sb-doc
269   "Generate-Error-Code Error-code Value*
270   Emit code for an error with the specified Error-Code and context Values."
271   (assemble (*elsewhere*)
272     (let ((start-lab (gen-label)))
273       (emit-label start-lab)
274       (emit-error-break vop error-trap (error-number-or-lose error-code) values)
275        start-lab)))
276
277 \f
278 ;;;; PSEUDO-ATOMIC
279
280 ;;; This is used to wrap operations which leave untagged memory lying
281 ;;; around.  It's an operation which the AOP weenies would describe as
282 ;;; having "cross-cutting concerns", meaning it appears all over the
283 ;;; place and there's no logical single place to attach documentation.
284 ;;; grep (mostly in src/runtime) is your friend
285
286 (defmacro maybe-pseudo-atomic (not-really-p &body body)
287   `(if ,not-really-p
288        (progn ,@body)
289        (pseudo-atomic ,@body)))
290
291 #!+sb-thread
292 (defmacro pseudo-atomic (&rest forms)
293   (with-unique-names (label)
294     `(let ((,label (gen-label)))
295       (inst or (make-ea :byte
296                  :base thread-base-tn
297                  :disp (* 8 thread-pseudo-atomic-bits-slot))
298             (fixnumize 1))
299       ,@forms
300       (inst xor (make-ea :byte
301                  :base thread-base-tn
302                  :disp (* 8 thread-pseudo-atomic-bits-slot))
303             (fixnumize 1))
304       (inst jmp :z ,label)
305       ;; if PAI was set, interrupts were disabled at the same
306       ;; time using the process signal mask.
307       (inst break pending-interrupt-trap)
308       (emit-label ,label))))
309
310
311 #!-sb-thread
312 (defmacro pseudo-atomic (&rest forms)
313   (with-unique-names (label)
314     `(let ((,label (gen-label)))
315       ;; FIXME: The MAKE-EA noise should become a MACROLET macro or
316       ;; something. (perhaps SVLB, for static variable low byte)
317       (inst or (make-ea :byte :disp (+ nil-value
318                                        (static-symbol-offset
319                                         '*pseudo-atomic-bits*)
320                                        (ash symbol-value-slot word-shift)
321                                        (- other-pointer-lowtag)))
322             (fixnumize 1))
323       ,@forms
324       (inst xor (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       (inst jmp :z ,label)
331       ;; if PAI was set, interrupts were disabled at the same time
332       ;; using the process signal mask.
333       (inst break pending-interrupt-trap)
334       (emit-label ,label))))
335
336
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          #!+sb-thread
358          (inst lock)
359          (inst cmpxchg (make-ea :qword :base object :index index
360                                 :disp (- (* ,offset n-word-bytes) ,lowtag))
361                new-value)
362          (move value rax)))))
363
364 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
365   `(progn
366      (define-vop (,name)
367        ,@(when translate
368            `((:translate ,translate)))
369        (:policy :fast-safe)
370        (:args (object :scs (descriptor-reg))
371               (index :scs (any-reg)))
372        (:arg-types ,type tagged-num)
373        (:results (value :scs ,scs))
374        (:result-types ,el-type)
375        (:generator 3                    ; pw was 5
376          (inst mov value (make-ea :qword :base object :index index
377                                   :disp (- (* ,offset n-word-bytes)
378                                            ,lowtag)))))
379      (define-vop (,(symbolicate name "-C"))
380        ,@(when translate
381            `((:translate ,translate)))
382        (:policy :fast-safe)
383        (:args (object :scs (descriptor-reg)))
384        (:info index)
385        (:arg-types ,type
386                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
387                                                 ,(eval offset))))
388        (:results (value :scs ,scs))
389        (:result-types ,el-type)
390        (:generator 2                    ; pw was 5
391          (inst mov value (make-ea :qword :base object
392                                   :disp (- (* (+ ,offset index) n-word-bytes)
393                                            ,lowtag)))))))
394
395 (defmacro define-full-reffer+offset (name type offset lowtag scs el-type &optional translate)
396   `(progn
397      (define-vop (,name)
398        ,@(when translate
399            `((:translate ,translate)))
400        (:policy :fast-safe)
401        (:args (object :scs (descriptor-reg))
402               (index :scs (any-reg)))
403        (:info offset)
404        (:arg-types ,type tagged-num
405                    (:constant (constant-displacement other-pointer-lowtag
406                                                      n-word-bytes vector-data-offset)))
407        (:results (value :scs ,scs))
408        (:result-types ,el-type)
409        (:generator 3                    ; pw was 5
410          (inst mov value (make-ea :qword :base object :index index
411                                   :disp (- (* (+ ,offset offset) n-word-bytes)
412                                            ,lowtag)))))
413      (define-vop (,(symbolicate name "-C"))
414        ,@(when translate
415            `((:translate ,translate)))
416        (:policy :fast-safe)
417        (:args (object :scs (descriptor-reg)))
418        (:info index offset)
419        (:arg-types ,type
420                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
421                                                 ,(eval offset)))
422                    (:constant (constant-displacement other-pointer-lowtag
423                                                      n-word-bytes vector-data-offset)))
424        (:results (value :scs ,scs))
425        (:result-types ,el-type)
426        (:generator 2                    ; pw was 5
427          (inst mov value (make-ea :qword :base object
428                                   :disp (- (* (+ ,offset index offset) n-word-bytes)
429                                            ,lowtag)))))))
430
431 (defmacro define-full-setter (name type offset lowtag scs el-type &optional translate)
432   `(progn
433      (define-vop (,name)
434        ,@(when translate
435            `((:translate ,translate)))
436        (:policy :fast-safe)
437        (:args (object :scs (descriptor-reg))
438               (index :scs (any-reg))
439               (value :scs ,scs :target result))
440        (:arg-types ,type tagged-num ,el-type)
441        (:results (result :scs ,scs))
442        (:result-types ,el-type)
443        (:generator 4                    ; was 5
444          (inst mov (make-ea :qword :base object :index index
445                             :disp (- (* ,offset n-word-bytes) ,lowtag))
446                value)
447          (move result value)))
448      (define-vop (,(symbolicate name "-C"))
449        ,@(when translate
450            `((:translate ,translate)))
451        (:policy :fast-safe)
452        (:args (object :scs (descriptor-reg))
453               (value :scs ,scs :target result))
454        (:info index)
455        (:arg-types ,type
456                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
457                                                 ,(eval offset)))
458                    ,el-type)
459        (:results (result :scs ,scs))
460        (:result-types ,el-type)
461        (:generator 3                    ; was 5
462          (inst mov (make-ea :qword :base object
463                             :disp (- (* (+ ,offset index) n-word-bytes)
464                                      ,lowtag))
465                value)
466          (move result value)))))
467
468 (defmacro define-full-setter+offset (name type offset lowtag scs el-type &optional translate)
469   `(progn
470      (define-vop (,name)
471        ,@(when translate
472            `((:translate ,translate)))
473        (:policy :fast-safe)
474        (:args (object :scs (descriptor-reg))
475               (index :scs (any-reg))
476               (value :scs ,scs :target result))
477        (:info offset)
478        (:arg-types ,type tagged-num
479                    (:constant (constant-displacement other-pointer-lowtag
480                                                      n-word-bytes
481                                                      vector-data-offset))
482                    ,el-type)
483        (:results (result :scs ,scs))
484        (:result-types ,el-type)
485        (:generator 4                    ; was 5
486          (inst mov (make-ea :qword :base object :index index
487                             :disp (- (* (+ ,offset offset) n-word-bytes) ,lowtag))
488                value)
489          (move result value)))
490      (define-vop (,(symbolicate name "-C"))
491        ,@(when translate
492            `((:translate ,translate)))
493        (:policy :fast-safe)
494        (:args (object :scs (descriptor-reg))
495               (value :scs ,scs :target result))
496        (:info index offset)
497        (:arg-types ,type
498                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
499                                                 ,(eval offset)))
500                    (:constant (constant-displacement other-pointer-lowtag
501                                                      n-word-bytes
502                                                      vector-data-offset))
503                    ,el-type)
504        (:results (result :scs ,scs))
505        (:result-types ,el-type)
506        (:generator 3                    ; was 5
507          (inst mov (make-ea :qword :base object
508                             :disp (- (* (+ ,offset index offset) n-word-bytes)
509                                      ,lowtag))
510                value)
511          (move result value)))))
512
513 ;;; helper for alien stuff.
514
515 (def!macro with-pinned-objects ((&rest objects) &body body)
516   "Arrange with the garbage collector that the pages occupied by
517 OBJECTS will not be moved in memory for the duration of BODY.
518 Useful for e.g. foreign calls where another thread may trigger
519 collection."
520   (if objects
521       (let ((pins (make-gensym-list (length objects)))
522             (wpo (block-gensym "WPO")))
523         ;; BODY is stuffed in a function to preserve the lexical
524         ;; environment.
525         `(flet ((,wpo () (progn ,@body)))
526            ;; PINS are dx-allocated in case the compiler for some
527            ;; unfathomable reason decides to allocate value-cells
528            ;; for them -- since we have DX value-cells on x86oid
529            ;; platforms this still forces them on the stack.
530            (dx-let ,(mapcar #'list pins objects)
531              (multiple-value-prog1 (,wpo)
532                ;; TOUCH-OBJECT has a VOP with an empty body: compiler
533                ;; thinks we're using the argument and doesn't flush
534                ;; the variable, but we don't have to pay any extra
535                ;; beyond that -- and MULTIPLE-VALUE-PROG1 keeps them
536                ;; live till the body has finished. *whew*
537                ,@(mapcar (lambda (pin)
538                            `(touch-object ,pin))
539                          pins)))))
540       `(progn ,@body)))