github.com/euank/go@v0.0.0-20160829210321-495514729181/src/cmd/compile/internal/ssa/value.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ssa 6 7 import ( 8 "fmt" 9 "math" 10 ) 11 12 // A Value represents a value in the SSA representation of the program. 13 // The ID and Type fields must not be modified. The remainder may be modified 14 // if they preserve the value of the Value (e.g. changing a (mul 2 x) to an (add x x)). 15 type Value struct { 16 // A unique identifier for the value. For performance we allocate these IDs 17 // densely starting at 1. There is no guarantee that there won't be occasional holes, though. 18 ID ID 19 20 // The operation that computes this value. See op.go. 21 Op Op 22 23 // The type of this value. Normally this will be a Go type, but there 24 // are a few other pseudo-types, see type.go. 25 Type Type 26 27 // Auxiliary info for this value. The type of this information depends on the opcode and type. 28 // AuxInt is used for integer values, Aux is used for other values. 29 // Floats are stored in AuxInt using math.Float64bits(f). 30 AuxInt int64 31 Aux interface{} 32 33 // Arguments of this value 34 Args []*Value 35 36 // Containing basic block 37 Block *Block 38 39 // Source line number 40 Line int32 41 42 // Use count. Each appearance in Value.Args and Block.Control counts once. 43 Uses int32 44 45 // Storage for the first three args 46 argstorage [3]*Value 47 } 48 49 // Examples: 50 // Opcode aux args 51 // OpAdd nil 2 52 // OpConst string 0 string constant 53 // OpConst int64 0 int64 constant 54 // OpAddcq int64 1 amd64 op: v = arg[0] + constant 55 56 // short form print. Just v#. 57 func (v *Value) String() string { 58 if v == nil { 59 return "nil" // should never happen, but not panicking helps with debugging 60 } 61 return fmt.Sprintf("v%d", v.ID) 62 } 63 64 func (v *Value) AuxInt8() int8 { 65 if opcodeTable[v.Op].auxType != auxInt8 { 66 v.Fatalf("op %s doesn't have an int8 aux field", v.Op) 67 } 68 return int8(v.AuxInt) 69 } 70 71 func (v *Value) AuxInt16() int16 { 72 if opcodeTable[v.Op].auxType != auxInt16 { 73 v.Fatalf("op %s doesn't have an int16 aux field", v.Op) 74 } 75 return int16(v.AuxInt) 76 } 77 78 func (v *Value) AuxInt32() int32 { 79 if opcodeTable[v.Op].auxType != auxInt32 { 80 v.Fatalf("op %s doesn't have an int32 aux field", v.Op) 81 } 82 return int32(v.AuxInt) 83 } 84 85 func (v *Value) AuxFloat() float64 { 86 if opcodeTable[v.Op].auxType != auxFloat32 && opcodeTable[v.Op].auxType != auxFloat64 { 87 v.Fatalf("op %s doesn't have a float aux field", v.Op) 88 } 89 return math.Float64frombits(uint64(v.AuxInt)) 90 } 91 func (v *Value) AuxValAndOff() ValAndOff { 92 if opcodeTable[v.Op].auxType != auxSymValAndOff { 93 v.Fatalf("op %s doesn't have a ValAndOff aux field", v.Op) 94 } 95 return ValAndOff(v.AuxInt) 96 } 97 98 // long form print. v# = opcode <type> [aux] args [: reg] 99 func (v *Value) LongString() string { 100 s := fmt.Sprintf("v%d = %s", v.ID, v.Op.String()) 101 s += " <" + v.Type.String() + ">" 102 s += v.auxString() 103 for _, a := range v.Args { 104 s += fmt.Sprintf(" %v", a) 105 } 106 r := v.Block.Func.RegAlloc 107 if int(v.ID) < len(r) && r[v.ID] != nil { 108 s += " : " + r[v.ID].Name() 109 } 110 return s 111 } 112 113 func (v *Value) auxString() string { 114 switch opcodeTable[v.Op].auxType { 115 case auxBool: 116 if v.AuxInt == 0 { 117 return " [false]" 118 } else { 119 return " [true]" 120 } 121 case auxInt8: 122 return fmt.Sprintf(" [%d]", v.AuxInt8()) 123 case auxInt16: 124 return fmt.Sprintf(" [%d]", v.AuxInt16()) 125 case auxInt32: 126 return fmt.Sprintf(" [%d]", v.AuxInt32()) 127 case auxInt64, auxInt128: 128 return fmt.Sprintf(" [%d]", v.AuxInt) 129 case auxFloat32, auxFloat64: 130 return fmt.Sprintf(" [%g]", v.AuxFloat()) 131 case auxString: 132 return fmt.Sprintf(" {%q}", v.Aux) 133 case auxSym: 134 if v.Aux != nil { 135 return fmt.Sprintf(" {%s}", v.Aux) 136 } 137 case auxSymOff, auxSymInt32: 138 s := "" 139 if v.Aux != nil { 140 s = fmt.Sprintf(" {%s}", v.Aux) 141 } 142 if v.AuxInt != 0 { 143 s += fmt.Sprintf(" [%v]", v.AuxInt) 144 } 145 return s 146 case auxSymValAndOff: 147 s := "" 148 if v.Aux != nil { 149 s = fmt.Sprintf(" {%s}", v.Aux) 150 } 151 return s + fmt.Sprintf(" [%s]", v.AuxValAndOff()) 152 } 153 return "" 154 } 155 156 func (v *Value) AddArg(w *Value) { 157 if v.Args == nil { 158 v.resetArgs() // use argstorage 159 } 160 v.Args = append(v.Args, w) 161 w.Uses++ 162 } 163 func (v *Value) AddArgs(a ...*Value) { 164 if v.Args == nil { 165 v.resetArgs() // use argstorage 166 } 167 v.Args = append(v.Args, a...) 168 for _, x := range a { 169 x.Uses++ 170 } 171 } 172 func (v *Value) SetArg(i int, w *Value) { 173 v.Args[i].Uses-- 174 v.Args[i] = w 175 w.Uses++ 176 } 177 func (v *Value) RemoveArg(i int) { 178 v.Args[i].Uses-- 179 copy(v.Args[i:], v.Args[i+1:]) 180 v.Args[len(v.Args)-1] = nil // aid GC 181 v.Args = v.Args[:len(v.Args)-1] 182 } 183 func (v *Value) SetArgs1(a *Value) { 184 v.resetArgs() 185 v.AddArg(a) 186 } 187 func (v *Value) SetArgs2(a *Value, b *Value) { 188 v.resetArgs() 189 v.AddArg(a) 190 v.AddArg(b) 191 } 192 193 func (v *Value) resetArgs() { 194 for _, a := range v.Args { 195 a.Uses-- 196 } 197 v.argstorage[0] = nil 198 v.argstorage[1] = nil 199 v.argstorage[2] = nil 200 v.Args = v.argstorage[:0] 201 } 202 203 func (v *Value) reset(op Op) { 204 v.Op = op 205 v.resetArgs() 206 v.AuxInt = 0 207 v.Aux = nil 208 } 209 210 // copyInto makes a new value identical to v and adds it to the end of b. 211 func (v *Value) copyInto(b *Block) *Value { 212 c := b.NewValue0(v.Line, v.Op, v.Type) 213 c.Aux = v.Aux 214 c.AuxInt = v.AuxInt 215 c.AddArgs(v.Args...) 216 for _, a := range v.Args { 217 if a.Type.IsMemory() { 218 v.Fatalf("can't move a value with a memory arg %s", v.LongString()) 219 } 220 } 221 return c 222 } 223 224 func (v *Value) Logf(msg string, args ...interface{}) { v.Block.Logf(msg, args...) } 225 func (v *Value) Log() bool { return v.Block.Log() } 226 func (v *Value) Fatalf(msg string, args ...interface{}) { 227 v.Block.Func.Config.Fatalf(v.Line, msg, args...) 228 } 229 func (v *Value) Unimplementedf(msg string, args ...interface{}) { 230 v.Block.Func.Config.Unimplementedf(v.Line, msg, args...) 231 } 232 233 // isGenericIntConst returns whether v is a generic integer constant. 234 func (v *Value) isGenericIntConst() bool { 235 return v != nil && (v.Op == OpConst64 || v.Op == OpConst32 || v.Op == OpConst16 || v.Op == OpConst8) 236 } 237 238 // ExternSymbol is an aux value that encodes a variable's 239 // constant offset from the static base pointer. 240 type ExternSymbol struct { 241 Typ Type // Go type 242 Sym fmt.Stringer // A *gc.Sym referring to a global variable 243 // Note: the offset for an external symbol is not 244 // calculated until link time. 245 } 246 247 // ArgSymbol is an aux value that encodes an argument or result 248 // variable's constant offset from FP (FP = SP + framesize). 249 type ArgSymbol struct { 250 Typ Type // Go type 251 Node GCNode // A *gc.Node referring to the argument/result variable. 252 } 253 254 // AutoSymbol is an aux value that encodes a local variable's 255 // constant offset from SP. 256 type AutoSymbol struct { 257 Typ Type // Go type 258 Node GCNode // A *gc.Node referring to a local (auto) variable. 259 } 260 261 func (s *ExternSymbol) String() string { 262 return s.Sym.String() 263 } 264 265 func (s *ArgSymbol) String() string { 266 return s.Node.String() 267 } 268 269 func (s *AutoSymbol) String() string { 270 return s.Node.String() 271 }