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