github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/src/cmd/compile/internal/ssa/func.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 "cmd/compile/internal/types" 9 "cmd/internal/src" 10 "crypto/sha1" 11 "fmt" 12 "io" 13 "math" 14 "os" 15 "strings" 16 ) 17 18 type writeSyncer interface { 19 io.Writer 20 Sync() error 21 } 22 23 // A Func represents a Go func declaration (or function literal) and its body. 24 // This package compiles each Func independently. 25 // Funcs are single-use; a new Func must be created for every compiled function. 26 type Func struct { 27 Config *Config // architecture information 28 Cache *Cache // re-usable cache 29 fe Frontend // frontend state associated with this Func, callbacks into compiler frontend 30 pass *pass // current pass information (name, options, etc.) 31 Name string // e.g. NewFunc or (*Func).NumBlocks (no package prefix) 32 Type *types.Type // type signature of the function. 33 Blocks []*Block // unordered set of all basic blocks (note: not indexable by ID) 34 Entry *Block // the entry basic block 35 bid idAlloc // block ID allocator 36 vid idAlloc // value ID allocator 37 38 // Given an environment variable used for debug hash match, 39 // what file (if any) receives the yes/no logging? 40 logfiles map[string]writeSyncer 41 HTMLWriter *HTMLWriter // html writer, for debugging 42 DebugTest bool // default true unless $GOSSAHASH != ""; as a debugging aid, make new code conditional on this and use GOSSAHASH to binary search for failing cases 43 PrintOrHtmlSSA bool // true if GOSSAFUNC matches, true even if fe.Log() (spew phase results to stdout) is false. 44 45 scheduled bool // Values in Blocks are in final order 46 NoSplit bool // true if function is marked as nosplit. Used by schedule check pass. 47 48 // when register allocation is done, maps value ids to locations 49 RegAlloc []Location 50 51 // map from LocalSlot to set of Values that we want to store in that slot. 52 NamedValues map[LocalSlot][]*Value 53 // Names is a copy of NamedValues.Keys. We keep a separate list 54 // of keys to make iteration order deterministic. 55 Names []LocalSlot 56 57 // WBLoads is a list of Blocks that branch on the write 58 // barrier flag. Safe-points are disabled from the OpLoad that 59 // reads the write-barrier flag until the control flow rejoins 60 // below the two successors of this block. 61 WBLoads []*Block 62 63 freeValues *Value // free Values linked by argstorage[0]. All other fields except ID are 0/nil. 64 freeBlocks *Block // free Blocks linked by succstorage[0].b. All other fields except ID are 0/nil. 65 66 cachedPostorder []*Block // cached postorder traversal 67 cachedIdom []*Block // cached immediate dominators 68 cachedSdom SparseTree // cached dominator tree 69 cachedLoopnest *loopnest // cached loop nest information 70 cachedLineStarts *biasedSparseMap // cached map/set of line numbers to integers 71 72 auxmap auxmap // map from aux values to opaque ids used by CSE 73 constants map[int64][]*Value // constants cache, keyed by constant value; users must check value's Op and Type 74 } 75 76 // NewFunc returns a new, empty function object. 77 // Caller must set f.Config and f.Cache before using f. 78 func NewFunc(fe Frontend) *Func { 79 return &Func{fe: fe, NamedValues: make(map[LocalSlot][]*Value)} 80 } 81 82 // NumBlocks returns an integer larger than the id of any Block in the Func. 83 func (f *Func) NumBlocks() int { 84 return f.bid.num() 85 } 86 87 // NumValues returns an integer larger than the id of any Value in the Func. 88 func (f *Func) NumValues() int { 89 return f.vid.num() 90 } 91 92 // newSparseSet returns a sparse set that can store at least up to n integers. 93 func (f *Func) newSparseSet(n int) *sparseSet { 94 for i, scr := range f.Cache.scrSparseSet { 95 if scr != nil && scr.cap() >= n { 96 f.Cache.scrSparseSet[i] = nil 97 scr.clear() 98 return scr 99 } 100 } 101 return newSparseSet(n) 102 } 103 104 // retSparseSet returns a sparse set to the config's cache of sparse 105 // sets to be reused by f.newSparseSet. 106 func (f *Func) retSparseSet(ss *sparseSet) { 107 for i, scr := range f.Cache.scrSparseSet { 108 if scr == nil { 109 f.Cache.scrSparseSet[i] = ss 110 return 111 } 112 } 113 f.Cache.scrSparseSet = append(f.Cache.scrSparseSet, ss) 114 } 115 116 // newSparseMap returns a sparse map that can store at least up to n integers. 117 func (f *Func) newSparseMap(n int) *sparseMap { 118 for i, scr := range f.Cache.scrSparseMap { 119 if scr != nil && scr.cap() >= n { 120 f.Cache.scrSparseMap[i] = nil 121 scr.clear() 122 return scr 123 } 124 } 125 return newSparseMap(n) 126 } 127 128 // retSparseMap returns a sparse map to the config's cache of sparse 129 // sets to be reused by f.newSparseMap. 130 func (f *Func) retSparseMap(ss *sparseMap) { 131 for i, scr := range f.Cache.scrSparseMap { 132 if scr == nil { 133 f.Cache.scrSparseMap[i] = ss 134 return 135 } 136 } 137 f.Cache.scrSparseMap = append(f.Cache.scrSparseMap, ss) 138 } 139 140 // newPoset returns a new poset from the internal cache 141 func (f *Func) newPoset() *poset { 142 if len(f.Cache.scrPoset) > 0 { 143 po := f.Cache.scrPoset[len(f.Cache.scrPoset)-1] 144 f.Cache.scrPoset = f.Cache.scrPoset[:len(f.Cache.scrPoset)-1] 145 return po 146 } 147 return newPoset() 148 } 149 150 // retPoset returns a poset to the internal cache 151 func (f *Func) retPoset(po *poset) { 152 f.Cache.scrPoset = append(f.Cache.scrPoset, po) 153 } 154 155 // newValue allocates a new Value with the given fields and places it at the end of b.Values. 156 func (f *Func) newValue(op Op, t *types.Type, b *Block, pos src.XPos) *Value { 157 var v *Value 158 if f.freeValues != nil { 159 v = f.freeValues 160 f.freeValues = v.argstorage[0] 161 v.argstorage[0] = nil 162 } else { 163 ID := f.vid.get() 164 if int(ID) < len(f.Cache.values) { 165 v = &f.Cache.values[ID] 166 v.ID = ID 167 } else { 168 v = &Value{ID: ID} 169 } 170 } 171 v.Op = op 172 v.Type = t 173 v.Block = b 174 if notStmtBoundary(op) { 175 pos = pos.WithNotStmt() 176 } 177 v.Pos = pos 178 b.Values = append(b.Values, v) 179 return v 180 } 181 182 // newValueNoBlock allocates a new Value with the given fields. 183 // The returned value is not placed in any block. Once the caller 184 // decides on a block b, it must set b.Block and append 185 // the returned value to b.Values. 186 func (f *Func) newValueNoBlock(op Op, t *types.Type, pos src.XPos) *Value { 187 var v *Value 188 if f.freeValues != nil { 189 v = f.freeValues 190 f.freeValues = v.argstorage[0] 191 v.argstorage[0] = nil 192 } else { 193 ID := f.vid.get() 194 if int(ID) < len(f.Cache.values) { 195 v = &f.Cache.values[ID] 196 v.ID = ID 197 } else { 198 v = &Value{ID: ID} 199 } 200 } 201 v.Op = op 202 v.Type = t 203 v.Block = nil // caller must fix this. 204 if notStmtBoundary(op) { 205 pos = pos.WithNotStmt() 206 } 207 v.Pos = pos 208 return v 209 } 210 211 // logPassStat writes a string key and int value as a warning in a 212 // tab-separated format easily handled by spreadsheets or awk. 213 // file names, lines, and function names are included to provide enough (?) 214 // context to allow item-by-item comparisons across runs. 215 // For example: 216 // awk 'BEGIN {FS="\t"} $3~/TIME/{sum+=$4} END{print "t(ns)=",sum}' t.log 217 func (f *Func) LogStat(key string, args ...interface{}) { 218 value := "" 219 for _, a := range args { 220 value += fmt.Sprintf("\t%v", a) 221 } 222 n := "missing_pass" 223 if f.pass != nil { 224 n = strings.Replace(f.pass.name, " ", "_", -1) 225 } 226 f.Warnl(f.Entry.Pos, "\t%s\t%s%s\t%s", n, key, value, f.Name) 227 } 228 229 // freeValue frees a value. It must no longer be referenced or have any args. 230 func (f *Func) freeValue(v *Value) { 231 if v.Block == nil { 232 f.Fatalf("trying to free an already freed value") 233 } 234 if v.Uses != 0 { 235 f.Fatalf("value %s still has %d uses", v, v.Uses) 236 } 237 if len(v.Args) != 0 { 238 f.Fatalf("value %s still has %d args", v, len(v.Args)) 239 } 240 // Clear everything but ID (which we reuse). 241 id := v.ID 242 243 // Values with zero arguments and OpOffPtr values might be cached, so remove them there. 244 nArgs := opcodeTable[v.Op].argLen 245 if nArgs == 0 || v.Op == OpOffPtr { 246 vv := f.constants[v.AuxInt] 247 for i, cv := range vv { 248 if v == cv { 249 vv[i] = vv[len(vv)-1] 250 vv[len(vv)-1] = nil 251 f.constants[v.AuxInt] = vv[0 : len(vv)-1] 252 break 253 } 254 } 255 } 256 *v = Value{} 257 v.ID = id 258 v.argstorage[0] = f.freeValues 259 f.freeValues = v 260 } 261 262 // newBlock allocates a new Block of the given kind and places it at the end of f.Blocks. 263 func (f *Func) NewBlock(kind BlockKind) *Block { 264 var b *Block 265 if f.freeBlocks != nil { 266 b = f.freeBlocks 267 f.freeBlocks = b.succstorage[0].b 268 b.succstorage[0].b = nil 269 } else { 270 ID := f.bid.get() 271 if int(ID) < len(f.Cache.blocks) { 272 b = &f.Cache.blocks[ID] 273 b.ID = ID 274 } else { 275 b = &Block{ID: ID} 276 } 277 } 278 b.Kind = kind 279 b.Func = f 280 b.Preds = b.predstorage[:0] 281 b.Succs = b.succstorage[:0] 282 b.Values = b.valstorage[:0] 283 f.Blocks = append(f.Blocks, b) 284 f.invalidateCFG() 285 return b 286 } 287 288 func (f *Func) freeBlock(b *Block) { 289 if b.Func == nil { 290 f.Fatalf("trying to free an already freed block") 291 } 292 // Clear everything but ID (which we reuse). 293 id := b.ID 294 *b = Block{} 295 b.ID = id 296 b.succstorage[0].b = f.freeBlocks 297 f.freeBlocks = b 298 } 299 300 // NewValue0 returns a new value in the block with no arguments and zero aux values. 301 func (b *Block) NewValue0(pos src.XPos, op Op, t *types.Type) *Value { 302 v := b.Func.newValue(op, t, b, pos) 303 v.AuxInt = 0 304 v.Args = v.argstorage[:0] 305 return v 306 } 307 308 // NewValue returns a new value in the block with no arguments and an auxint value. 309 func (b *Block) NewValue0I(pos src.XPos, op Op, t *types.Type, auxint int64) *Value { 310 v := b.Func.newValue(op, t, b, pos) 311 v.AuxInt = auxint 312 v.Args = v.argstorage[:0] 313 return v 314 } 315 316 // NewValue returns a new value in the block with no arguments and an aux value. 317 func (b *Block) NewValue0A(pos src.XPos, op Op, t *types.Type, aux interface{}) *Value { 318 if _, ok := aux.(int64); ok { 319 // Disallow int64 aux values. They should be in the auxint field instead. 320 // Maybe we want to allow this at some point, but for now we disallow it 321 // to prevent errors like using NewValue1A instead of NewValue1I. 322 b.Fatalf("aux field has int64 type op=%s type=%s aux=%v", op, t, aux) 323 } 324 v := b.Func.newValue(op, t, b, pos) 325 v.AuxInt = 0 326 v.Aux = aux 327 v.Args = v.argstorage[:0] 328 return v 329 } 330 331 // NewValue returns a new value in the block with no arguments and both an auxint and aux values. 332 func (b *Block) NewValue0IA(pos src.XPos, op Op, t *types.Type, auxint int64, aux interface{}) *Value { 333 v := b.Func.newValue(op, t, b, pos) 334 v.AuxInt = auxint 335 v.Aux = aux 336 v.Args = v.argstorage[:0] 337 return v 338 } 339 340 // NewValue1 returns a new value in the block with one argument and zero aux values. 341 func (b *Block) NewValue1(pos src.XPos, op Op, t *types.Type, arg *Value) *Value { 342 v := b.Func.newValue(op, t, b, pos) 343 v.AuxInt = 0 344 v.Args = v.argstorage[:1] 345 v.argstorage[0] = arg 346 arg.Uses++ 347 return v 348 } 349 350 // NewValue1I returns a new value in the block with one argument and an auxint value. 351 func (b *Block) NewValue1I(pos src.XPos, op Op, t *types.Type, auxint int64, arg *Value) *Value { 352 v := b.Func.newValue(op, t, b, pos) 353 v.AuxInt = auxint 354 v.Args = v.argstorage[:1] 355 v.argstorage[0] = arg 356 arg.Uses++ 357 return v 358 } 359 360 // NewValue1A returns a new value in the block with one argument and an aux value. 361 func (b *Block) NewValue1A(pos src.XPos, op Op, t *types.Type, aux interface{}, arg *Value) *Value { 362 v := b.Func.newValue(op, t, b, pos) 363 v.AuxInt = 0 364 v.Aux = aux 365 v.Args = v.argstorage[:1] 366 v.argstorage[0] = arg 367 arg.Uses++ 368 return v 369 } 370 371 // NewValue1IA returns a new value in the block with one argument and both an auxint and aux values. 372 func (b *Block) NewValue1IA(pos src.XPos, op Op, t *types.Type, auxint int64, aux interface{}, arg *Value) *Value { 373 v := b.Func.newValue(op, t, b, pos) 374 v.AuxInt = auxint 375 v.Aux = aux 376 v.Args = v.argstorage[:1] 377 v.argstorage[0] = arg 378 arg.Uses++ 379 return v 380 } 381 382 // NewValue2 returns a new value in the block with two arguments and zero aux values. 383 func (b *Block) NewValue2(pos src.XPos, op Op, t *types.Type, arg0, arg1 *Value) *Value { 384 v := b.Func.newValue(op, t, b, pos) 385 v.AuxInt = 0 386 v.Args = v.argstorage[:2] 387 v.argstorage[0] = arg0 388 v.argstorage[1] = arg1 389 arg0.Uses++ 390 arg1.Uses++ 391 return v 392 } 393 394 // NewValue2A returns a new value in the block with two arguments and one aux values. 395 func (b *Block) NewValue2A(pos src.XPos, op Op, t *types.Type, aux interface{}, arg0, arg1 *Value) *Value { 396 v := b.Func.newValue(op, t, b, pos) 397 v.AuxInt = 0 398 v.Aux = aux 399 v.Args = v.argstorage[:2] 400 v.argstorage[0] = arg0 401 v.argstorage[1] = arg1 402 arg0.Uses++ 403 arg1.Uses++ 404 return v 405 } 406 407 // NewValue2I returns a new value in the block with two arguments and an auxint value. 408 func (b *Block) NewValue2I(pos src.XPos, op Op, t *types.Type, auxint int64, arg0, arg1 *Value) *Value { 409 v := b.Func.newValue(op, t, b, pos) 410 v.AuxInt = auxint 411 v.Args = v.argstorage[:2] 412 v.argstorage[0] = arg0 413 v.argstorage[1] = arg1 414 arg0.Uses++ 415 arg1.Uses++ 416 return v 417 } 418 419 // NewValue2IA returns a new value in the block with two arguments and both an auxint and aux values. 420 func (b *Block) NewValue2IA(pos src.XPos, op Op, t *types.Type, auxint int64, aux interface{}, arg0, arg1 *Value) *Value { 421 v := b.Func.newValue(op, t, b, pos) 422 v.AuxInt = auxint 423 v.Aux = aux 424 v.Args = v.argstorage[:2] 425 v.argstorage[0] = arg0 426 v.argstorage[1] = arg1 427 arg0.Uses++ 428 arg1.Uses++ 429 return v 430 } 431 432 // NewValue3 returns a new value in the block with three arguments and zero aux values. 433 func (b *Block) NewValue3(pos src.XPos, op Op, t *types.Type, arg0, arg1, arg2 *Value) *Value { 434 v := b.Func.newValue(op, t, b, pos) 435 v.AuxInt = 0 436 v.Args = v.argstorage[:3] 437 v.argstorage[0] = arg0 438 v.argstorage[1] = arg1 439 v.argstorage[2] = arg2 440 arg0.Uses++ 441 arg1.Uses++ 442 arg2.Uses++ 443 return v 444 } 445 446 // NewValue3I returns a new value in the block with three arguments and an auxint value. 447 func (b *Block) NewValue3I(pos src.XPos, op Op, t *types.Type, auxint int64, arg0, arg1, arg2 *Value) *Value { 448 v := b.Func.newValue(op, t, b, pos) 449 v.AuxInt = auxint 450 v.Args = v.argstorage[:3] 451 v.argstorage[0] = arg0 452 v.argstorage[1] = arg1 453 v.argstorage[2] = arg2 454 arg0.Uses++ 455 arg1.Uses++ 456 arg2.Uses++ 457 return v 458 } 459 460 // NewValue3A returns a new value in the block with three argument and an aux value. 461 func (b *Block) NewValue3A(pos src.XPos, op Op, t *types.Type, aux interface{}, arg0, arg1, arg2 *Value) *Value { 462 v := b.Func.newValue(op, t, b, pos) 463 v.AuxInt = 0 464 v.Aux = aux 465 v.Args = v.argstorage[:3] 466 v.argstorage[0] = arg0 467 v.argstorage[1] = arg1 468 v.argstorage[2] = arg2 469 arg0.Uses++ 470 arg1.Uses++ 471 arg2.Uses++ 472 return v 473 } 474 475 // NewValue4 returns a new value in the block with four arguments and zero aux values. 476 func (b *Block) NewValue4(pos src.XPos, op Op, t *types.Type, arg0, arg1, arg2, arg3 *Value) *Value { 477 v := b.Func.newValue(op, t, b, pos) 478 v.AuxInt = 0 479 v.Args = []*Value{arg0, arg1, arg2, arg3} 480 arg0.Uses++ 481 arg1.Uses++ 482 arg2.Uses++ 483 arg3.Uses++ 484 return v 485 } 486 487 // constVal returns a constant value for c. 488 func (f *Func) constVal(op Op, t *types.Type, c int64, setAuxInt bool) *Value { 489 if f.constants == nil { 490 f.constants = make(map[int64][]*Value) 491 } 492 vv := f.constants[c] 493 for _, v := range vv { 494 if v.Op == op && v.Type.Compare(t) == types.CMPeq { 495 if setAuxInt && v.AuxInt != c { 496 panic(fmt.Sprintf("cached const %s should have AuxInt of %d", v.LongString(), c)) 497 } 498 return v 499 } 500 } 501 var v *Value 502 if setAuxInt { 503 v = f.Entry.NewValue0I(src.NoXPos, op, t, c) 504 } else { 505 v = f.Entry.NewValue0(src.NoXPos, op, t) 506 } 507 f.constants[c] = append(vv, v) 508 return v 509 } 510 511 // These magic auxint values let us easily cache non-numeric constants 512 // using the same constants map while making collisions unlikely. 513 // These values are unlikely to occur in regular code and 514 // are easy to grep for in case of bugs. 515 const ( 516 constSliceMagic = 1122334455 517 constInterfaceMagic = 2233445566 518 constNilMagic = 3344556677 519 constEmptyStringMagic = 4455667788 520 ) 521 522 // ConstInt returns an int constant representing its argument. 523 func (f *Func) ConstBool(t *types.Type, c bool) *Value { 524 i := int64(0) 525 if c { 526 i = 1 527 } 528 return f.constVal(OpConstBool, t, i, true) 529 } 530 func (f *Func) ConstInt8(t *types.Type, c int8) *Value { 531 return f.constVal(OpConst8, t, int64(c), true) 532 } 533 func (f *Func) ConstInt16(t *types.Type, c int16) *Value { 534 return f.constVal(OpConst16, t, int64(c), true) 535 } 536 func (f *Func) ConstInt32(t *types.Type, c int32) *Value { 537 return f.constVal(OpConst32, t, int64(c), true) 538 } 539 func (f *Func) ConstInt64(t *types.Type, c int64) *Value { 540 return f.constVal(OpConst64, t, c, true) 541 } 542 func (f *Func) ConstFloat32(t *types.Type, c float64) *Value { 543 return f.constVal(OpConst32F, t, int64(math.Float64bits(float64(float32(c)))), true) 544 } 545 func (f *Func) ConstFloat64(t *types.Type, c float64) *Value { 546 return f.constVal(OpConst64F, t, int64(math.Float64bits(c)), true) 547 } 548 549 func (f *Func) ConstSlice(t *types.Type) *Value { 550 return f.constVal(OpConstSlice, t, constSliceMagic, false) 551 } 552 func (f *Func) ConstInterface(t *types.Type) *Value { 553 return f.constVal(OpConstInterface, t, constInterfaceMagic, false) 554 } 555 func (f *Func) ConstNil(t *types.Type) *Value { 556 return f.constVal(OpConstNil, t, constNilMagic, false) 557 } 558 func (f *Func) ConstEmptyString(t *types.Type) *Value { 559 v := f.constVal(OpConstString, t, constEmptyStringMagic, false) 560 v.Aux = "" 561 return v 562 } 563 func (f *Func) ConstOffPtrSP(t *types.Type, c int64, sp *Value) *Value { 564 v := f.constVal(OpOffPtr, t, c, true) 565 if len(v.Args) == 0 { 566 v.AddArg(sp) 567 } 568 return v 569 570 } 571 572 func (f *Func) Frontend() Frontend { return f.fe } 573 func (f *Func) Warnl(pos src.XPos, msg string, args ...interface{}) { f.fe.Warnl(pos, msg, args...) } 574 func (f *Func) Logf(msg string, args ...interface{}) { f.fe.Logf(msg, args...) } 575 func (f *Func) Log() bool { return f.fe.Log() } 576 func (f *Func) Fatalf(msg string, args ...interface{}) { f.fe.Fatalf(f.Entry.Pos, msg, args...) } 577 578 // postorder returns the reachable blocks in f in a postorder traversal. 579 func (f *Func) postorder() []*Block { 580 if f.cachedPostorder == nil { 581 f.cachedPostorder = postorder(f) 582 } 583 return f.cachedPostorder 584 } 585 586 func (f *Func) Postorder() []*Block { 587 return f.postorder() 588 } 589 590 // Idom returns a map from block ID to the immediate dominator of that block. 591 // f.Entry.ID maps to nil. Unreachable blocks map to nil as well. 592 func (f *Func) Idom() []*Block { 593 if f.cachedIdom == nil { 594 f.cachedIdom = dominators(f) 595 } 596 return f.cachedIdom 597 } 598 599 // sdom returns a sparse tree representing the dominator relationships 600 // among the blocks of f. 601 func (f *Func) sdom() SparseTree { 602 if f.cachedSdom == nil { 603 f.cachedSdom = newSparseTree(f, f.Idom()) 604 } 605 return f.cachedSdom 606 } 607 608 // loopnest returns the loop nest information for f. 609 func (f *Func) loopnest() *loopnest { 610 if f.cachedLoopnest == nil { 611 f.cachedLoopnest = loopnestfor(f) 612 } 613 return f.cachedLoopnest 614 } 615 616 // invalidateCFG tells f that its CFG has changed. 617 func (f *Func) invalidateCFG() { 618 f.cachedPostorder = nil 619 f.cachedIdom = nil 620 f.cachedSdom = nil 621 f.cachedLoopnest = nil 622 } 623 624 // DebugHashMatch returns true if environment variable evname 625 // 1) is empty (this is a special more-quickly implemented case of 3) 626 // 2) is "y" or "Y" 627 // 3) is a suffix of the sha1 hash of name 628 // 4) is a suffix of the environment variable 629 // fmt.Sprintf("%s%d", evname, n) 630 // provided that all such variables are nonempty for 0 <= i <= n 631 // Otherwise it returns false. 632 // When true is returned the message 633 // "%s triggered %s\n", evname, name 634 // is printed on the file named in environment variable 635 // GSHS_LOGFILE 636 // or standard out if that is empty or there is an error 637 // opening the file. 638 func (f *Func) DebugHashMatch(evname, name string) bool { 639 evhash := os.Getenv(evname) 640 switch evhash { 641 case "": 642 return true // default behavior with no EV is "on" 643 case "y", "Y": 644 f.logDebugHashMatch(evname, name) 645 return true 646 case "n", "N": 647 return false 648 } 649 // Check the hash of the name against a partial input hash. 650 // We use this feature to do a binary search to 651 // find a function that is incorrectly compiled. 652 hstr := "" 653 for _, b := range sha1.Sum([]byte(name)) { 654 hstr += fmt.Sprintf("%08b", b) 655 } 656 657 if strings.HasSuffix(hstr, evhash) { 658 f.logDebugHashMatch(evname, name) 659 return true 660 } 661 662 // Iteratively try additional hashes to allow tests for multi-point 663 // failure. 664 for i := 0; true; i++ { 665 ev := fmt.Sprintf("%s%d", evname, i) 666 evv := os.Getenv(ev) 667 if evv == "" { 668 break 669 } 670 if strings.HasSuffix(hstr, evv) { 671 f.logDebugHashMatch(ev, name) 672 return true 673 } 674 } 675 return false 676 } 677 678 func (f *Func) logDebugHashMatch(evname, name string) { 679 if f.logfiles == nil { 680 f.logfiles = make(map[string]writeSyncer) 681 } 682 file := f.logfiles[evname] 683 if file == nil { 684 file = os.Stdout 685 if tmpfile := os.Getenv("GSHS_LOGFILE"); tmpfile != "" { 686 var err error 687 file, err = os.Create(tmpfile) 688 if err != nil { 689 f.Fatalf("could not open hash-testing logfile %s", tmpfile) 690 } 691 } 692 f.logfiles[evname] = file 693 } 694 fmt.Fprintf(file, "%s triggered %s\n", evname, name) 695 file.Sync() 696 } 697 698 func DebugNameMatch(evname, name string) bool { 699 return os.Getenv(evname) == name 700 }