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