github.com/stingnevermore/go@v0.0.0-20180120041312-3810f5bfed72/src/cmd/compile/internal/ssa/cache.go (about)

     1  // Copyright 2017 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 "sort"
     8  
     9  // A Cache holds reusable compiler state.
    10  // It is intended to be re-used for multiple Func compilations.
    11  type Cache struct {
    12  	// Storage for low-numbered values and blocks.
    13  	values [2000]Value
    14  	blocks [200]Block
    15  	locs   [2000]Location
    16  
    17  	// Storage for DWARF variable locations. Lazily allocated
    18  	// since location lists are off by default.
    19  	varLocs   []VarLoc
    20  	curVarLoc int
    21  
    22  	// Reusable stackAllocState.
    23  	// See stackalloc.go's {new,put}StackAllocState.
    24  	stackAllocState *stackAllocState
    25  
    26  	domblockstore []ID         // scratch space for computing dominators
    27  	scrSparse     []*sparseSet // scratch sparse sets to be re-used.
    28  }
    29  
    30  func (c *Cache) Reset() {
    31  	nv := sort.Search(len(c.values), func(i int) bool { return c.values[i].ID == 0 })
    32  	xv := c.values[:nv]
    33  	for i := range xv {
    34  		xv[i] = Value{}
    35  	}
    36  	nb := sort.Search(len(c.blocks), func(i int) bool { return c.blocks[i].ID == 0 })
    37  	xb := c.blocks[:nb]
    38  	for i := range xb {
    39  		xb[i] = Block{}
    40  	}
    41  	nl := sort.Search(len(c.locs), func(i int) bool { return c.locs[i] == nil })
    42  	xl := c.locs[:nl]
    43  	for i := range xl {
    44  		xl[i] = nil
    45  	}
    46  	xvl := c.varLocs[:c.curVarLoc]
    47  	for i := range xvl {
    48  		xvl[i] = VarLoc{}
    49  	}
    50  	c.curVarLoc = 0
    51  }
    52  
    53  func (c *Cache) NewVarLoc() *VarLoc {
    54  	if c.varLocs == nil {
    55  		c.varLocs = make([]VarLoc, 4000)
    56  	}
    57  	if c.curVarLoc == len(c.varLocs) {
    58  		return &VarLoc{}
    59  	}
    60  	vl := &c.varLocs[c.curVarLoc]
    61  	c.curVarLoc++
    62  	return vl
    63  }