Skip to content

Commit 2d8181e

Browse files
committed
cmd/compile: clarify unsigned interpretation of AuxInt
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Cherry Zhang <[email protected]>
1 parent a29e25b commit 2d8181e

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

src/cmd/compile/internal/ssa/gen/genericOps.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ package main
1717
// all args take signed inputs, or don't care whether their inputs
1818
// are signed or unsigned.
1919

20-
// Unused portions of AuxInt are filled by sign-extending the used portion.
21-
// Users of AuxInt which interpret AuxInt as unsigned (e.g. shifts) must be careful.
2220
var genericOps = []opData{
2321
// 2-input arithmetic
2422
// Types must be consistent with Go typing. Add, for example, must take two values

src/cmd/compile/internal/ssa/prove.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ func (ft *factsTable) update(parent *Block, v, w *Value, d domain, r relation) {
234234
r = reverseBits[r]
235235
}
236236
if v != nil && w.isGenericIntConst() {
237-
c := w.AuxInt
238237
// Note: all the +1/-1 below could overflow/underflow. Either will
239238
// still generate correct results, it will just lead to imprecision.
240239
// 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) {
247246
lim := noLimit
248247
switch d {
249248
case signed:
249+
c := w.AuxInt
250250
switch r {
251251
case lt:
252252
lim.max = c - 1
@@ -279,17 +279,7 @@ func (ft *factsTable) update(parent *Block, v, w *Value, d domain, r relation) {
279279
lim.umax = uint64(lim.max)
280280
}
281281
case unsigned:
282-
var uc uint64
283-
switch w.Op {
284-
case OpConst64:
285-
uc = uint64(c)
286-
case OpConst32:
287-
uc = uint64(uint32(c))
288-
case OpConst16:
289-
uc = uint64(uint16(c))
290-
case OpConst8:
291-
uc = uint64(uint8(c))
292-
}
282+
uc := w.AuxUnsigned()
293283
switch r {
294284
case lt:
295285
lim.umax = uc - 1

src/cmd/compile/internal/ssa/value.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ type Value struct {
3131
// Auxiliary info for this value. The type of this information depends on the opcode and type.
3232
// AuxInt is used for integer values, Aux is used for other values.
3333
// Floats are stored in AuxInt using math.Float64bits(f).
34+
// Unused portions of AuxInt are filled by sign-extending the used portion,
35+
// even if the represented value is unsigned.
36+
// Users of AuxInt which interpret AuxInt as unsigned (e.g. shifts) must be careful.
37+
// Use Value.AuxUnsigned to get the zero-extended value of AuxInt.
3438
AuxInt int64
3539
Aux interface{}
3640

@@ -86,6 +90,25 @@ func (v *Value) AuxInt32() int32 {
8690
return int32(v.AuxInt)
8791
}
8892

93+
// AuxUnsigned returns v.AuxInt as an unsigned value for OpConst*.
94+
// v.AuxInt is always sign-extended to 64 bits, even if the
95+
// represented value is unsigned. This undoes that sign extension.
96+
func (v *Value) AuxUnsigned() uint64 {
97+
c := v.AuxInt
98+
switch v.Op {
99+
case OpConst64:
100+
return uint64(c)
101+
case OpConst32:
102+
return uint64(uint32(c))
103+
case OpConst16:
104+
return uint64(uint16(c))
105+
case OpConst8:
106+
return uint64(uint8(c))
107+
}
108+
v.Fatalf("op %s isn't OpConst*", v.Op)
109+
return 0
110+
}
111+
89112
func (v *Value) AuxFloat() float64 {
90113
if opcodeTable[v.Op].auxType != auxFloat32 && opcodeTable[v.Op].auxType != auxFloat64 {
91114
v.Fatalf("op %s doesn't have a float aux field", v.Op)

0 commit comments

Comments
 (0)