github.com/sanprasirt/go@v0.0.0-20170607001320-a027466e4b6d/src/cmd/internal/obj/pcln.go (about)

     1  // Copyright 2013 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 obj
     6  
     7  import "log"
     8  
     9  func addvarint(d *Pcdata, v uint32) {
    10  	for ; v >= 0x80; v >>= 7 {
    11  		d.P = append(d.P, uint8(v|0x80))
    12  	}
    13  	d.P = append(d.P, uint8(v))
    14  }
    15  
    16  // funcpctab writes to dst a pc-value table mapping the code in func to the values
    17  // returned by valfunc parameterized by arg. The invocation of valfunc to update the
    18  // current value is, for each p,
    19  //
    20  //	val = valfunc(func, val, p, 0, arg);
    21  //	record val as value at p->pc;
    22  //	val = valfunc(func, val, p, 1, arg);
    23  //
    24  // where func is the function, val is the current value, p is the instruction being
    25  // considered, and arg can be used to further parameterize valfunc.
    26  func funcpctab(ctxt *Link, dst *Pcdata, func_ *LSym, desc string, valfunc func(*Link, *LSym, int32, *Prog, int32, interface{}) int32, arg interface{}) {
    27  	dbg := desc == ctxt.Debugpcln
    28  
    29  	dst.P = dst.P[:0]
    30  
    31  	if dbg {
    32  		ctxt.Logf("funcpctab %s [valfunc=%s]\n", func_.Name, desc)
    33  	}
    34  
    35  	val := int32(-1)
    36  	oldval := val
    37  	if func_.Func.Text == nil {
    38  		return
    39  	}
    40  
    41  	pc := func_.Func.Text.Pc
    42  
    43  	if dbg {
    44  		ctxt.Logf("%6x %6d %v\n", uint64(pc), val, func_.Func.Text)
    45  	}
    46  
    47  	started := false
    48  	var delta uint32
    49  	for p := func_.Func.Text; p != nil; p = p.Link {
    50  		// Update val. If it's not changing, keep going.
    51  		val = valfunc(ctxt, func_, val, p, 0, arg)
    52  
    53  		if val == oldval && started {
    54  			val = valfunc(ctxt, func_, val, p, 1, arg)
    55  			if dbg {
    56  				ctxt.Logf("%6x %6s %v\n", uint64(p.Pc), "", p)
    57  			}
    58  			continue
    59  		}
    60  
    61  		// If the pc of the next instruction is the same as the
    62  		// pc of this instruction, this instruction is not a real
    63  		// instruction. Keep going, so that we only emit a delta
    64  		// for a true instruction boundary in the program.
    65  		if p.Link != nil && p.Link.Pc == p.Pc {
    66  			val = valfunc(ctxt, func_, val, p, 1, arg)
    67  			if dbg {
    68  				ctxt.Logf("%6x %6s %v\n", uint64(p.Pc), "", p)
    69  			}
    70  			continue
    71  		}
    72  
    73  		// The table is a sequence of (value, pc) pairs, where each
    74  		// pair states that the given value is in effect from the current position
    75  		// up to the given pc, which becomes the new current position.
    76  		// To generate the table as we scan over the program instructions,
    77  		// we emit a "(value" when pc == func->value, and then
    78  		// each time we observe a change in value we emit ", pc) (value".
    79  		// When the scan is over, we emit the closing ", pc)".
    80  		//
    81  		// The table is delta-encoded. The value deltas are signed and
    82  		// transmitted in zig-zag form, where a complement bit is placed in bit 0,
    83  		// and the pc deltas are unsigned. Both kinds of deltas are sent
    84  		// as variable-length little-endian base-128 integers,
    85  		// where the 0x80 bit indicates that the integer continues.
    86  
    87  		if dbg {
    88  			ctxt.Logf("%6x %6d %v\n", uint64(p.Pc), val, p)
    89  		}
    90  
    91  		if started {
    92  			addvarint(dst, uint32((p.Pc-pc)/int64(ctxt.Arch.MinLC)))
    93  			pc = p.Pc
    94  		}
    95  
    96  		delta = uint32(val) - uint32(oldval)
    97  		if delta>>31 != 0 {
    98  			delta = 1 | ^(delta << 1)
    99  		} else {
   100  			delta <<= 1
   101  		}
   102  		addvarint(dst, delta)
   103  		oldval = val
   104  		started = true
   105  		val = valfunc(ctxt, func_, val, p, 1, arg)
   106  	}
   107  
   108  	if started {
   109  		if dbg {
   110  			ctxt.Logf("%6x done\n", uint64(func_.Func.Text.Pc+func_.Size))
   111  		}
   112  		addvarint(dst, uint32((func_.Size-pc)/int64(ctxt.Arch.MinLC)))
   113  		addvarint(dst, 0) // terminator
   114  	}
   115  
   116  	if dbg {
   117  		ctxt.Logf("wrote %d bytes to %p\n", len(dst.P), dst)
   118  		for i := 0; i < len(dst.P); i++ {
   119  			ctxt.Logf(" %02x", dst.P[i])
   120  		}
   121  		ctxt.Logf("\n")
   122  	}
   123  }
   124  
   125  // pctofileline computes either the file number (arg == 0)
   126  // or the line number (arg == 1) to use at p.
   127  // Because p.Pos applies to p, phase == 0 (before p)
   128  // takes care of the update.
   129  func pctofileline(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 {
   130  	if p.As == ATEXT || p.As == ANOP || p.Pos.Line() == 0 || phase == 1 {
   131  		return oldval
   132  	}
   133  	f, l := linkgetlineFromPos(ctxt, p.Pos)
   134  	if arg == nil {
   135  		return l
   136  	}
   137  	pcln := arg.(*Pcln)
   138  
   139  	if f == pcln.Lastfile {
   140  		return int32(pcln.Lastindex)
   141  	}
   142  
   143  	for i, file := range pcln.File {
   144  		if file == f {
   145  			pcln.Lastfile = f
   146  			pcln.Lastindex = i
   147  			return int32(i)
   148  		}
   149  	}
   150  	i := len(pcln.File)
   151  	pcln.File = append(pcln.File, f)
   152  	pcln.Lastfile = f
   153  	pcln.Lastindex = i
   154  	return int32(i)
   155  }
   156  
   157  // pcinlineState holds the state used to create a function's inlining
   158  // tree and the PC-value table that maps PCs to nodes in that tree.
   159  type pcinlineState struct {
   160  	globalToLocal map[int]int
   161  	localTree     InlTree
   162  }
   163  
   164  // addBranch adds a branch from the global inlining tree in ctxt to
   165  // the function's local inlining tree, returning the index in the local tree.
   166  func (s *pcinlineState) addBranch(ctxt *Link, globalIndex int) int {
   167  	if globalIndex < 0 {
   168  		return -1
   169  	}
   170  
   171  	localIndex, ok := s.globalToLocal[globalIndex]
   172  	if ok {
   173  		return localIndex
   174  	}
   175  
   176  	// Since tracebacks don't include column information, we could
   177  	// use one node for multiple calls of the same function on the
   178  	// same line (e.g., f(x) + f(y)). For now, we use one node for
   179  	// each inlined call.
   180  	call := ctxt.InlTree.nodes[globalIndex]
   181  	call.Parent = s.addBranch(ctxt, call.Parent)
   182  	localIndex = len(s.localTree.nodes)
   183  	s.localTree.nodes = append(s.localTree.nodes, call)
   184  	s.globalToLocal[globalIndex] = localIndex
   185  	return localIndex
   186  }
   187  
   188  // pctoinline computes the index into the local inlining tree to use at p.
   189  // If p is not the result of inlining, pctoinline returns -1. Because p.Pos
   190  // applies to p, phase == 0 (before p) takes care of the update.
   191  func (s *pcinlineState) pctoinline(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 {
   192  	if phase == 1 {
   193  		return oldval
   194  	}
   195  
   196  	posBase := ctxt.PosTable.Pos(p.Pos).Base()
   197  	if posBase == nil {
   198  		return -1
   199  	}
   200  
   201  	globalIndex := posBase.InliningIndex()
   202  	if globalIndex < 0 {
   203  		return -1
   204  	}
   205  
   206  	if s.globalToLocal == nil {
   207  		s.globalToLocal = make(map[int]int)
   208  	}
   209  
   210  	return int32(s.addBranch(ctxt, globalIndex))
   211  }
   212  
   213  // pctospadj computes the sp adjustment in effect.
   214  // It is oldval plus any adjustment made by p itself.
   215  // The adjustment by p takes effect only after p, so we
   216  // apply the change during phase == 1.
   217  func pctospadj(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 {
   218  	if oldval == -1 { // starting
   219  		oldval = 0
   220  	}
   221  	if phase == 0 {
   222  		return oldval
   223  	}
   224  	if oldval+p.Spadj < -10000 || oldval+p.Spadj > 1100000000 {
   225  		ctxt.Diag("overflow in spadj: %d + %d = %d", oldval, p.Spadj, oldval+p.Spadj)
   226  		log.Fatalf("bad code")
   227  	}
   228  
   229  	return oldval + p.Spadj
   230  }
   231  
   232  // pctopcdata computes the pcdata value in effect at p.
   233  // A PCDATA instruction sets the value in effect at future
   234  // non-PCDATA instructions.
   235  // Since PCDATA instructions have no width in the final code,
   236  // it does not matter which phase we use for the update.
   237  func pctopcdata(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 {
   238  	if phase == 0 || p.As != APCDATA || p.From.Offset != int64(arg.(uint32)) {
   239  		return oldval
   240  	}
   241  	if int64(int32(p.To.Offset)) != p.To.Offset {
   242  		ctxt.Diag("overflow in PCDATA instruction: %v", p)
   243  		log.Fatalf("bad code")
   244  	}
   245  
   246  	return int32(p.To.Offset)
   247  }
   248  
   249  func linkpcln(ctxt *Link, cursym *LSym) {
   250  	pcln := &cursym.Func.Pcln
   251  
   252  	npcdata := 0
   253  	nfuncdata := 0
   254  	for p := cursym.Func.Text; p != nil; p = p.Link {
   255  		// Find the highest ID of any used PCDATA table. This ignores PCDATA table
   256  		// that consist entirely of "-1", since that's the assumed default value.
   257  		//   From.Offset is table ID
   258  		//   To.Offset is data
   259  		if p.As == APCDATA && p.From.Offset >= int64(npcdata) && p.To.Offset != -1 { // ignore -1 as we start at -1, if we only see -1, nothing changed
   260  			npcdata = int(p.From.Offset + 1)
   261  		}
   262  		// Find the highest ID of any FUNCDATA table.
   263  		//   From.Offset is table ID
   264  		if p.As == AFUNCDATA && p.From.Offset >= int64(nfuncdata) {
   265  			nfuncdata = int(p.From.Offset + 1)
   266  		}
   267  	}
   268  
   269  	pcln.Pcdata = make([]Pcdata, npcdata)
   270  	pcln.Pcdata = pcln.Pcdata[:npcdata]
   271  	pcln.Funcdata = make([]*LSym, nfuncdata)
   272  	pcln.Funcdataoff = make([]int64, nfuncdata)
   273  	pcln.Funcdataoff = pcln.Funcdataoff[:nfuncdata]
   274  
   275  	funcpctab(ctxt, &pcln.Pcsp, cursym, "pctospadj", pctospadj, nil)
   276  	funcpctab(ctxt, &pcln.Pcfile, cursym, "pctofile", pctofileline, pcln)
   277  	funcpctab(ctxt, &pcln.Pcline, cursym, "pctoline", pctofileline, nil)
   278  
   279  	pcinlineState := new(pcinlineState)
   280  	funcpctab(ctxt, &pcln.Pcinline, cursym, "pctoinline", pcinlineState.pctoinline, nil)
   281  	pcln.InlTree = pcinlineState.localTree
   282  	if ctxt.Debugpcln == "pctoinline" && len(pcln.InlTree.nodes) > 0 {
   283  		ctxt.Logf("-- inlining tree for %s:\n", cursym)
   284  		dumpInlTree(ctxt, pcln.InlTree)
   285  		ctxt.Logf("--\n")
   286  	}
   287  
   288  	// tabulate which pc and func data we have.
   289  	havepc := make([]uint32, (npcdata+31)/32)
   290  	havefunc := make([]uint32, (nfuncdata+31)/32)
   291  	for p := cursym.Func.Text; p != nil; p = p.Link {
   292  		if p.As == AFUNCDATA {
   293  			if (havefunc[p.From.Offset/32]>>uint64(p.From.Offset%32))&1 != 0 {
   294  				ctxt.Diag("multiple definitions for FUNCDATA $%d", p.From.Offset)
   295  			}
   296  			havefunc[p.From.Offset/32] |= 1 << uint64(p.From.Offset%32)
   297  		}
   298  
   299  		if p.As == APCDATA && p.To.Offset != -1 {
   300  			havepc[p.From.Offset/32] |= 1 << uint64(p.From.Offset%32)
   301  		}
   302  	}
   303  
   304  	// pcdata.
   305  	for i := 0; i < npcdata; i++ {
   306  		if (havepc[i/32]>>uint(i%32))&1 == 0 {
   307  			continue
   308  		}
   309  		funcpctab(ctxt, &pcln.Pcdata[i], cursym, "pctopcdata", pctopcdata, interface{}(uint32(i)))
   310  	}
   311  
   312  	// funcdata
   313  	if nfuncdata > 0 {
   314  		var i int
   315  		for p := cursym.Func.Text; p != nil; p = p.Link {
   316  			if p.As == AFUNCDATA {
   317  				i = int(p.From.Offset)
   318  				pcln.Funcdataoff[i] = p.To.Offset
   319  				if p.To.Type != TYPE_CONST {
   320  					// TODO: Dedup.
   321  					//funcdata_bytes += p->to.sym->size;
   322  					pcln.Funcdata[i] = p.To.Sym
   323  				}
   324  			}
   325  		}
   326  	}
   327  }