]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: clarify unsigned interpretation of AuxInt
authorAustin Clements <austin@google.com>
Sun, 25 Mar 2018 16:20:57 +0000 (12:20 -0400)
committerAustin Clements <austin@google.com>
Mon, 26 Mar 2018 19:08:24 +0000 (19:08 +0000)
The way Value.AuxInt represents unsigned numbers is currently
documented in genericOps.go, which is not the most obvious place for
it. Move that documentation to Value.AuxInt. Furthermore, to make it
harder to use incorrectly, introduce a Value.AuxUnsigned accessor that
returns the zero-extended value of Value.AuxInt.

Change-Id: I85030c3c68761404058a430e0b1c7464591b2f42
Reviewed-on: https://go-review.googlesource.com/102597
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
src/cmd/compile/internal/ssa/gen/genericOps.go
src/cmd/compile/internal/ssa/prove.go
src/cmd/compile/internal/ssa/value.go

index 49160ab7c442b7889760915771045cd0fae5befc..c077b0bfcf4ae0413b66d2490e94a149403f6a11 100644 (file)
@@ -17,8 +17,6 @@ package main
 // all args take signed inputs, or don't care whether their inputs
 // are signed or unsigned.
 
-// Unused portions of AuxInt are filled by sign-extending the used portion.
-// Users of AuxInt which interpret AuxInt as unsigned (e.g. shifts) must be careful.
 var genericOps = []opData{
        // 2-input arithmetic
        // Types must be consistent with Go typing. Add, for example, must take two values
index cf0118ac3c389951c587a6535aee7d2fb8750f38..d05f6088a5d47fccf360dd43b3b1a7a862213b5d 100644 (file)
@@ -234,7 +234,6 @@ func (ft *factsTable) update(parent *Block, v, w *Value, d domain, r relation) {
                r = reverseBits[r]
        }
        if v != nil && w.isGenericIntConst() {
-               c := w.AuxInt
                // Note: all the +1/-1 below could overflow/underflow. Either will
                // still generate correct results, it will just lead to imprecision.
                // In fact if there is overflow/underflow, the corresponding
@@ -247,6 +246,7 @@ func (ft *factsTable) update(parent *Block, v, w *Value, d domain, r relation) {
                lim := noLimit
                switch d {
                case signed:
+                       c := w.AuxInt
                        switch r {
                        case lt:
                                lim.max = c - 1
@@ -279,17 +279,7 @@ func (ft *factsTable) update(parent *Block, v, w *Value, d domain, r relation) {
                                lim.umax = uint64(lim.max)
                        }
                case unsigned:
-                       var uc uint64
-                       switch w.Op {
-                       case OpConst64:
-                               uc = uint64(c)
-                       case OpConst32:
-                               uc = uint64(uint32(c))
-                       case OpConst16:
-                               uc = uint64(uint16(c))
-                       case OpConst8:
-                               uc = uint64(uint8(c))
-                       }
+                       uc := w.AuxUnsigned()
                        switch r {
                        case lt:
                                lim.umax = uc - 1
index ecf7b80115ee265f484b21c9063d054f93d78772..9a79a99f54ba5bd41d2748bf7d827b65d70f4340 100644 (file)
@@ -31,6 +31,10 @@ type Value struct {
        // Auxiliary info for this value. The type of this information depends on the opcode and type.
        // AuxInt is used for integer values, Aux is used for other values.
        // Floats are stored in AuxInt using math.Float64bits(f).
+       // Unused portions of AuxInt are filled by sign-extending the used portion,
+       // even if the represented value is unsigned.
+       // Users of AuxInt which interpret AuxInt as unsigned (e.g. shifts) must be careful.
+       // Use Value.AuxUnsigned to get the zero-extended value of AuxInt.
        AuxInt int64
        Aux    interface{}
 
@@ -86,6 +90,25 @@ func (v *Value) AuxInt32() int32 {
        return int32(v.AuxInt)
 }
 
+// AuxUnsigned returns v.AuxInt as an unsigned value for OpConst*.
+// v.AuxInt is always sign-extended to 64 bits, even if the
+// represented value is unsigned. This undoes that sign extension.
+func (v *Value) AuxUnsigned() uint64 {
+       c := v.AuxInt
+       switch v.Op {
+       case OpConst64:
+               return uint64(c)
+       case OpConst32:
+               return uint64(uint32(c))
+       case OpConst16:
+               return uint64(uint16(c))
+       case OpConst8:
+               return uint64(uint8(c))
+       }
+       v.Fatalf("op %s isn't OpConst*", v.Op)
+       return 0
+}
+
 func (v *Value) AuxFloat() float64 {
        if opcodeTable[v.Op].auxType != auxFloat32 && opcodeTable[v.Op].auxType != auxFloat64 {
                v.Fatalf("op %s doesn't have a float aux field", v.Op)