1.0.5.6: compare-and-swap / instance-set-conditional refactoring
[sbcl.git] / src / compiler / x86 / macros.lisp
index dba9b91..10540ca 100644 (file)
@@ -46,8 +46,8 @@
     `(unless (location= ,n-dst ,n-src)
        (inst mov ,n-dst ,n-src))))
 
-(defmacro make-ea-for-object-slot (ptr slot lowtag)
-  `(make-ea :dword :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
+(defmacro make-ea-for-object-slot (ptr slot lowtag &optional (size :dword))
+  `(make-ea ,size :base ,ptr :disp (- (* ,slot n-word-bytes) ,lowtag)))
 
 (defmacro loadw (value ptr &optional (slot 0) (lowtag 0))
   `(inst mov ,value (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
   (once-only ((value value))
     `(inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag) ,value)))
 
+;;; A handy macro for storing widetags.
+(defmacro storeb (value ptr &optional (slot 0) (lowtag 0))
+  (once-only ((value value))
+    `(inst mov (make-ea-for-object-slot ,ptr ,slot ,lowtag :byte) ,value)))
+
 (defmacro pushw (ptr &optional (slot 0) (lowtag 0))
   `(inst push (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
 
 (defmacro popw (ptr &optional (slot 0) (lowtag 0))
   `(inst pop (make-ea-for-object-slot ,ptr ,slot ,lowtag)))
+
+(defmacro make-ea-for-vector-data (object &key (size :dword) (offset 0)
+                                   index (scale (ash (width-bits size) -3)))
+  `(make-ea ,size :base ,object :index ,index :scale ,scale
+            :disp (- (+ (* vector-data-offset n-word-bytes)
+                        (* ,offset ,scale))
+                     other-pointer-lowtag)))
 \f
 ;;;; macros to generate useful values
 
 (defmacro load-symbol (reg symbol)
   `(inst mov ,reg (+ nil-value (static-symbol-offset ,symbol))))
 
-(defmacro make-ea-for-symbol-value (symbol)
-  `(make-ea :dword
+(defmacro make-ea-for-symbol-value (symbol &optional (width :dword))
+  (declare (type symbol symbol))
+  `(make-ea ,width
     :disp (+ nil-value
            (static-symbol-offset ',symbol)
            (ash symbol-value-slot word-shift)
@@ -82,6 +95,7 @@
 
 #!+sb-thread
 (defmacro make-ea-for-symbol-tls-index (symbol)
+  (declare (type symbol symbol))
   `(make-ea :dword
     :disp (+ nil-value
            (static-symbol-offset ',symbol)
 (defmacro pseudo-atomic (&rest forms)
   (with-unique-names (label)
     `(let ((,label (gen-label)))
-       ;; FIXME: The MAKE-EA noise should become a MACROLET macro
-       ;; or something. (perhaps SVLB, for static variable low
-       ;; byte)
-       (inst or (make-ea :byte :disp (+ nil-value
-                                        (static-symbol-offset
-                                         '*pseudo-atomic-bits*)
-                                        (ash symbol-value-slot word-shift)
-                                        (- other-pointer-lowtag)))
+       (inst or (make-ea-for-symbol-value *pseudo-atomic-bits* :byte)
              (fixnumize 1))
        ,@forms
-       (inst xor (make-ea :byte :disp (+ nil-value
-                                         (static-symbol-offset
-                                          '*pseudo-atomic-bits*)
-                                         (ash symbol-value-slot word-shift)
-                                         (- other-pointer-lowtag)))
+       (inst xor (make-ea-for-symbol-value *pseudo-atomic-bits* :byte)
              (fixnumize 1))
        (inst jmp :z ,label)
        ;; if PAI was set, interrupts were disabled at the same
 \f
 ;;;; indexed references
 
+(defmacro define-full-compare-and-swap
+    (name type offset lowtag scs el-type &optional translate)
+  `(progn
+     (define-vop (,name)
+         ,@(when translate `((:translate ,translate)))
+       (:policy :fast-safe)
+       (:args (object :scs (descriptor-reg) :to :eval)
+              (index :scs (any-reg immediate unsigned-reg) :to :result)
+              (old-value :scs ,scs :target eax)
+              (new-value :scs ,scs))
+       (:arg-types ,type tagged-num ,el-type ,el-type)
+       (:temporary (:sc descriptor-reg :offset eax-offset
+                        :from (:argument 2) :to :result :target value)  eax)
+       (:results (value :scs ,scs))
+       (:result-types ,el-type)
+       (:generator 5
+         (move eax old-value)
+         #!+sb-thread
+         (inst lock)
+         (let ((ea (sc-case index
+                     (immediate
+                      (make-ea :dword :base object
+                               :disp (- (* (+ ,offset (tn-value index))
+                                           n-word-bytes)
+                                        ,lowtag)))
+                     (unsigned-reg
+                      (make-ea :dword :base object :index index :scale 4
+                               :disp (- (* ,offset n-word-bytes)
+                                        ,lowtag)))
+                     (t
+                      (make-ea :dword :base object :index index
+                               :disp (- (* ,offset n-word-bytes)
+                                        ,lowtag))))))
+           (inst cmpxchg ea new-value))
+         (move value eax)))))
+
 (defmacro define-full-reffer (name type offset lowtag scs el-type &optional translate)
   `(progn
      (define-vop (,name)
        (:results (value :scs ,scs))
        (:result-types ,el-type)
        (:generator 3                    ; pw was 5
-         (unless (zerop offset)
-           (format t "Attempting D-F-R-O, offset ~D~%" offset))
          (sc-case index
            (immediate
             (inst mov value (make-ea :dword :base object
         (move result value)))))
 
 ;;; helper for alien stuff.
-(defmacro with-pinned-objects ((&rest objects) &body body)
+(def!macro with-pinned-objects ((&rest objects) &body body)
   "Arrange with the garbage collector that the pages occupied by
 OBJECTS will not be moved in memory for the duration of BODY.
 Useful for e.g. foreign calls where another thread may trigger