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