github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/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 "encoding/binary" 9 "log" 10 ) 11 12 // funcpctab writes to dst a pc-value table mapping the code in func to the values 13 // returned by valfunc parameterized by arg. The invocation of valfunc to update the 14 // current value is, for each p, 15 // 16 // val = valfunc(func, val, p, 0, arg); 17 // record val as value at p->pc; 18 // val = valfunc(func, val, p, 1, arg); 19 // 20 // where func is the function, val is the current value, p is the instruction being 21 // considered, and arg can be used to further parameterize valfunc. 22 func funcpctab(ctxt *Link, dst *Pcdata, func_ *LSym, desc string, valfunc func(*Link, *LSym, int32, *Prog, int32, interface{}) int32, arg interface{}) { 23 dbg := desc == ctxt.Debugpcln 24 25 dst.P = dst.P[:0] 26 27 if dbg { 28 ctxt.Logf("funcpctab %s [valfunc=%s]\n", func_.Name, desc) 29 } 30 31 val := int32(-1) 32 oldval := val 33 if func_.Func.Text == nil { 34 return 35 } 36 37 pc := func_.Func.Text.Pc 38 39 if dbg { 40 ctxt.Logf("%6x %6d %v\n", uint64(pc), val, func_.Func.Text) 41 } 42 43 buf := make([]byte, binary.MaxVarintLen32) 44 started := false 45 for p := func_.Func.Text; p != nil; p = p.Link { 46 // Update val. If it's not changing, keep going. 47 val = valfunc(ctxt, func_, val, p, 0, arg) 48 49 if val == oldval && started { 50 val = valfunc(ctxt, func_, val, p, 1, arg) 51 if dbg { 52 ctxt.Logf("%6x %6s %v\n", uint64(p.Pc), "", p) 53 } 54 continue 55 } 56 57 // If the pc of the next instruction is the same as the 58 // pc of this instruction, this instruction is not a real 59 // instruction. Keep going, so that we only emit a delta 60 // for a true instruction boundary in the program. 61 if p.Link != nil && p.Link.Pc == p.Pc { 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 // The table is a sequence of (value, pc) pairs, where each 70 // pair states that the given value is in effect from the current position 71 // up to the given pc, which becomes the new current position. 72 // To generate the table as we scan over the program instructions, 73 // we emit a "(value" when pc == func->value, and then 74 // each time we observe a change in value we emit ", pc) (value". 75 // When the scan is over, we emit the closing ", pc)". 76 // 77 // The table is delta-encoded. The value deltas are signed and 78 // transmitted in zig-zag form, where a complement bit is placed in bit 0, 79 // and the pc deltas are unsigned. Both kinds of deltas are sent 80 // as variable-length little-endian base-128 integers, 81 // where the 0x80 bit indicates that the integer continues. 82 83 if dbg { 84 ctxt.Logf("%6x %6d %v\n", uint64(p.Pc), val, p) 85 } 86 87 if started { 88 pcdelta := (p.Pc - pc) / int64(ctxt.Arch.MinLC) 89 n := binary.PutUvarint(buf, uint64(pcdelta)) 90 dst.P = append(dst.P, buf[:n]...) 91 pc = p.Pc 92 } 93 94 delta := val - oldval 95 n := binary.PutVarint(buf, int64(delta)) 96 dst.P = append(dst.P, buf[:n]...) 97 oldval = val 98 started = true 99 val = valfunc(ctxt, func_, val, p, 1, arg) 100 } 101 102 if started { 103 if dbg { 104 ctxt.Logf("%6x done\n", uint64(func_.Func.Text.Pc+func_.Size)) 105 } 106 v := (func_.Size - pc) / int64(ctxt.Arch.MinLC) 107 if v < 0 { 108 ctxt.Diag("negative pc offset: %v", v) 109 } 110 n := binary.PutUvarint(buf, uint64(v)) 111 dst.P = append(dst.P, buf[:n]...) 112 // add terminating varint-encoded 0, which is just 0 113 dst.P = append(dst.P, 0) 114 } 115 116 if dbg { 117 ctxt.Logf("wrote %d bytes to %p\n", len(dst.P), dst) 118 for _, p := range dst.P { 119 ctxt.Logf(" %02x", p) 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 func (s *pcinlineState) setParentPC(ctxt *Link, globalIndex int, pc int32) { 189 localIndex, ok := s.globalToLocal[globalIndex] 190 if !ok { 191 // We know where to unwind to when we need to unwind a body identified 192 // by globalIndex. But there may be no instructions generated by that 193 // body (it's empty, or its instructions were CSEd with other things, etc.). 194 // In that case, we don't need an unwind entry. 195 // TODO: is this really right? Seems to happen a whole lot... 196 return 197 } 198 s.localTree.setParentPC(localIndex, pc) 199 } 200 201 // pctoinline computes the index into the local inlining tree to use at p. 202 // If p is not the result of inlining, pctoinline returns -1. Because p.Pos 203 // applies to p, phase == 0 (before p) takes care of the update. 204 func (s *pcinlineState) pctoinline(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 { 205 if phase == 1 { 206 return oldval 207 } 208 209 posBase := ctxt.PosTable.Pos(p.Pos).Base() 210 if posBase == nil { 211 return -1 212 } 213 214 globalIndex := posBase.InliningIndex() 215 if globalIndex < 0 { 216 return -1 217 } 218 219 if s.globalToLocal == nil { 220 s.globalToLocal = make(map[int]int) 221 } 222 223 return int32(s.addBranch(ctxt, globalIndex)) 224 } 225 226 // pctospadj computes the sp adjustment in effect. 227 // It is oldval plus any adjustment made by p itself. 228 // The adjustment by p takes effect only after p, so we 229 // apply the change during phase == 1. 230 func pctospadj(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 { 231 if oldval == -1 { // starting 232 oldval = 0 233 } 234 if phase == 0 { 235 return oldval 236 } 237 if oldval+p.Spadj < -10000 || oldval+p.Spadj > 1100000000 { 238 ctxt.Diag("overflow in spadj: %d + %d = %d", oldval, p.Spadj, oldval+p.Spadj) 239 ctxt.DiagFlush() 240 log.Fatalf("bad code") 241 } 242 243 return oldval + p.Spadj 244 } 245 246 // pctopcdata computes the pcdata value in effect at p. 247 // A PCDATA instruction sets the value in effect at future 248 // non-PCDATA instructions. 249 // Since PCDATA instructions have no width in the final code, 250 // it does not matter which phase we use for the update. 251 func pctopcdata(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 { 252 if phase == 0 || p.As != APCDATA || p.From.Offset != int64(arg.(uint32)) { 253 return oldval 254 } 255 if int64(int32(p.To.Offset)) != p.To.Offset { 256 ctxt.Diag("overflow in PCDATA instruction: %v", p) 257 ctxt.DiagFlush() 258 log.Fatalf("bad code") 259 } 260 261 return int32(p.To.Offset) 262 } 263 264 func linkpcln(ctxt *Link, cursym *LSym) { 265 pcln := &cursym.Func.Pcln 266 267 npcdata := 0 268 nfuncdata := 0 269 for p := cursym.Func.Text; p != nil; p = p.Link { 270 // Find the highest ID of any used PCDATA table. This ignores PCDATA table 271 // that consist entirely of "-1", since that's the assumed default value. 272 // From.Offset is table ID 273 // To.Offset is data 274 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 275 npcdata = int(p.From.Offset + 1) 276 } 277 // Find the highest ID of any FUNCDATA table. 278 // From.Offset is table ID 279 if p.As == AFUNCDATA && p.From.Offset >= int64(nfuncdata) { 280 nfuncdata = int(p.From.Offset + 1) 281 } 282 } 283 284 pcln.Pcdata = make([]Pcdata, npcdata) 285 pcln.Pcdata = pcln.Pcdata[:npcdata] 286 pcln.Funcdata = make([]*LSym, nfuncdata) 287 pcln.Funcdataoff = make([]int64, nfuncdata) 288 pcln.Funcdataoff = pcln.Funcdataoff[:nfuncdata] 289 290 funcpctab(ctxt, &pcln.Pcsp, cursym, "pctospadj", pctospadj, nil) 291 funcpctab(ctxt, &pcln.Pcfile, cursym, "pctofile", pctofileline, pcln) 292 funcpctab(ctxt, &pcln.Pcline, cursym, "pctoline", pctofileline, nil) 293 294 pcinlineState := new(pcinlineState) 295 funcpctab(ctxt, &pcln.Pcinline, cursym, "pctoinline", pcinlineState.pctoinline, nil) 296 for _, inlMark := range cursym.Func.InlMarks { 297 pcinlineState.setParentPC(ctxt, int(inlMark.id), int32(inlMark.p.Pc)) 298 } 299 pcln.InlTree = pcinlineState.localTree 300 if ctxt.Debugpcln == "pctoinline" && len(pcln.InlTree.nodes) > 0 { 301 ctxt.Logf("-- inlining tree for %s:\n", cursym) 302 dumpInlTree(ctxt, pcln.InlTree) 303 ctxt.Logf("--\n") 304 } 305 306 // tabulate which pc and func data we have. 307 havepc := make([]uint32, (npcdata+31)/32) 308 havefunc := make([]uint32, (nfuncdata+31)/32) 309 for p := cursym.Func.Text; p != nil; p = p.Link { 310 if p.As == AFUNCDATA { 311 if (havefunc[p.From.Offset/32]>>uint64(p.From.Offset%32))&1 != 0 { 312 ctxt.Diag("multiple definitions for FUNCDATA $%d", p.From.Offset) 313 } 314 havefunc[p.From.Offset/32] |= 1 << uint64(p.From.Offset%32) 315 } 316 317 if p.As == APCDATA && p.To.Offset != -1 { 318 havepc[p.From.Offset/32] |= 1 << uint64(p.From.Offset%32) 319 } 320 } 321 322 // pcdata. 323 for i := 0; i < npcdata; i++ { 324 if (havepc[i/32]>>uint(i%32))&1 == 0 { 325 continue 326 } 327 funcpctab(ctxt, &pcln.Pcdata[i], cursym, "pctopcdata", pctopcdata, interface{}(uint32(i))) 328 } 329 330 // funcdata 331 if nfuncdata > 0 { 332 for p := cursym.Func.Text; p != nil; p = p.Link { 333 if p.As != AFUNCDATA { 334 continue 335 } 336 i := int(p.From.Offset) 337 pcln.Funcdataoff[i] = p.To.Offset 338 if p.To.Type != TYPE_CONST { 339 // TODO: Dedup. 340 //funcdata_bytes += p->to.sym->size; 341 pcln.Funcdata[i] = p.To.Sym 342 } 343 } 344 } 345 } 346 347 // PCIter iterates over encoded pcdata tables. 348 type PCIter struct { 349 p []byte 350 PC uint32 351 NextPC uint32 352 PCScale uint32 353 Value int32 354 start bool 355 Done bool 356 } 357 358 // newPCIter creates a PCIter with a scale factor for the PC step size. 359 func NewPCIter(pcScale uint32) *PCIter { 360 it := new(PCIter) 361 it.PCScale = pcScale 362 return it 363 } 364 365 // Next advances it to the Next pc. 366 func (it *PCIter) Next() { 367 it.PC = it.NextPC 368 if it.Done { 369 return 370 } 371 if len(it.p) == 0 { 372 it.Done = true 373 return 374 } 375 376 // Value delta 377 val, n := binary.Varint(it.p) 378 if n <= 0 { 379 log.Fatalf("bad Value varint in pciterNext: read %v", n) 380 } 381 it.p = it.p[n:] 382 383 if val == 0 && !it.start { 384 it.Done = true 385 return 386 } 387 388 it.start = false 389 it.Value += int32(val) 390 391 // pc delta 392 pc, n := binary.Uvarint(it.p) 393 if n <= 0 { 394 log.Fatalf("bad pc varint in pciterNext: read %v", n) 395 } 396 it.p = it.p[n:] 397 398 it.NextPC = it.PC + uint32(pc)*it.PCScale 399 } 400 401 // init prepares it to iterate over p, 402 // and advances it to the first pc. 403 func (it *PCIter) Init(p []byte) { 404 it.p = p 405 it.PC = 0 406 it.NextPC = 0 407 it.Value = -1 408 it.start = true 409 it.Done = false 410 it.Next() 411 }