github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/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 // pctoinline computes the index into the local inlining tree to use at p. 197 // If p is not the result of inlining, pctoinline returns -1. Because p.Pos 198 // applies to p, phase == 0 (before p) takes care of the update. 199 func (s *pcinlineState) pctoinline(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 { 200 if phase == 1 { 201 return oldval 202 } 203 204 posBase := ctxt.PosTable.Pos(p.Pos).Base() 205 if posBase == nil { 206 return -1 207 } 208 209 globalIndex := posBase.InliningIndex() 210 if globalIndex < 0 { 211 return -1 212 } 213 214 if s.globalToLocal == nil { 215 s.globalToLocal = make(map[int]int) 216 } 217 218 return int32(s.addBranch(ctxt, globalIndex)) 219 } 220 221 // pctospadj computes the sp adjustment in effect. 222 // It is oldval plus any adjustment made by p itself. 223 // The adjustment by p takes effect only after p, so we 224 // apply the change during phase == 1. 225 func pctospadj(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 { 226 if oldval == -1 { // starting 227 oldval = 0 228 } 229 if phase == 0 { 230 return oldval 231 } 232 if oldval+p.Spadj < -10000 || oldval+p.Spadj > 1100000000 { 233 ctxt.Diag("overflow in spadj: %d + %d = %d", oldval, p.Spadj, oldval+p.Spadj) 234 ctxt.DiagFlush() 235 log.Fatalf("bad code") 236 } 237 238 return oldval + p.Spadj 239 } 240 241 // pctostmt returns either, 242 // if phase==0, then whether the current instruction is a step-target (Dwarf is_stmt) 243 // bit-or'd with whether the current statement is a prologue end or epilogue begin 244 // else (phase == 1), zero. 245 // 246 func pctostmt(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 { 247 if phase == 1 { 248 return 0 // Ignored; also different from initial value of -1, if that ever matters. 249 } 250 s := p.Pos.IsStmt() 251 l := p.Pos.Xlogue() 252 253 var is_stmt int32 254 255 // PrologueEnd, at least, is passed to the next instruction 256 switch l { 257 case src.PosPrologueEnd: 258 is_stmt = PrologueEnd 259 case src.PosEpilogueBegin: 260 is_stmt = EpilogueBegin 261 } 262 263 if s != src.PosNotStmt { 264 is_stmt |= 1 // either PosDefaultStmt from asm, or PosIsStmt from go 265 } 266 return is_stmt 267 } 268 269 // pctopcdata computes the pcdata value in effect at p. 270 // A PCDATA instruction sets the value in effect at future 271 // non-PCDATA instructions. 272 // Since PCDATA instructions have no width in the final code, 273 // it does not matter which phase we use for the update. 274 func pctopcdata(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 { 275 if phase == 0 || p.As != APCDATA || p.From.Offset != int64(arg.(uint32)) { 276 return oldval 277 } 278 if int64(int32(p.To.Offset)) != p.To.Offset { 279 ctxt.Diag("overflow in PCDATA instruction: %v", p) 280 ctxt.DiagFlush() 281 log.Fatalf("bad code") 282 } 283 284 return int32(p.To.Offset) 285 } 286 287 // stmtData writes out pc-linked is_stmt data for eventual use in the DWARF line numbering table. 288 func stmtData(ctxt *Link, cursym *LSym) { 289 var pctostmtData Pcdata 290 funcpctab(ctxt, &pctostmtData, cursym, "pctostmt", pctostmt, nil) 291 cursym.Func.dwarfIsStmtSym.P = pctostmtData.P 292 } 293 294 func linkpcln(ctxt *Link, cursym *LSym) { 295 pcln := &cursym.Func.Pcln 296 297 npcdata := 0 298 nfuncdata := 0 299 for p := cursym.Func.Text; p != nil; p = p.Link { 300 // Find the highest ID of any used PCDATA table. This ignores PCDATA table 301 // that consist entirely of "-1", since that's the assumed default value. 302 // From.Offset is table ID 303 // To.Offset is data 304 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 305 npcdata = int(p.From.Offset + 1) 306 } 307 // Find the highest ID of any FUNCDATA table. 308 // From.Offset is table ID 309 if p.As == AFUNCDATA && p.From.Offset >= int64(nfuncdata) { 310 nfuncdata = int(p.From.Offset + 1) 311 } 312 } 313 314 pcln.Pcdata = make([]Pcdata, npcdata) 315 pcln.Pcdata = pcln.Pcdata[:npcdata] 316 pcln.Funcdata = make([]*LSym, nfuncdata) 317 pcln.Funcdataoff = make([]int64, nfuncdata) 318 pcln.Funcdataoff = pcln.Funcdataoff[:nfuncdata] 319 320 funcpctab(ctxt, &pcln.Pcsp, cursym, "pctospadj", pctospadj, nil) 321 funcpctab(ctxt, &pcln.Pcfile, cursym, "pctofile", pctofileline, pcln) 322 funcpctab(ctxt, &pcln.Pcline, cursym, "pctoline", pctofileline, nil) 323 324 pcinlineState := new(pcinlineState) 325 funcpctab(ctxt, &pcln.Pcinline, cursym, "pctoinline", pcinlineState.pctoinline, nil) 326 pcln.InlTree = pcinlineState.localTree 327 if ctxt.Debugpcln == "pctoinline" && len(pcln.InlTree.nodes) > 0 { 328 ctxt.Logf("-- inlining tree for %s:\n", cursym) 329 dumpInlTree(ctxt, pcln.InlTree) 330 ctxt.Logf("--\n") 331 } 332 333 // tabulate which pc and func data we have. 334 havepc := make([]uint32, (npcdata+31)/32) 335 havefunc := make([]uint32, (nfuncdata+31)/32) 336 for p := cursym.Func.Text; p != nil; p = p.Link { 337 if p.As == AFUNCDATA { 338 if (havefunc[p.From.Offset/32]>>uint64(p.From.Offset%32))&1 != 0 { 339 ctxt.Diag("multiple definitions for FUNCDATA $%d", p.From.Offset) 340 } 341 havefunc[p.From.Offset/32] |= 1 << uint64(p.From.Offset%32) 342 } 343 344 if p.As == APCDATA && p.To.Offset != -1 { 345 havepc[p.From.Offset/32] |= 1 << uint64(p.From.Offset%32) 346 } 347 } 348 349 // pcdata. 350 for i := 0; i < npcdata; i++ { 351 if (havepc[i/32]>>uint(i%32))&1 == 0 { 352 continue 353 } 354 funcpctab(ctxt, &pcln.Pcdata[i], cursym, "pctopcdata", pctopcdata, interface{}(uint32(i))) 355 } 356 357 // funcdata 358 if nfuncdata > 0 { 359 for p := cursym.Func.Text; p != nil; p = p.Link { 360 if p.As != AFUNCDATA { 361 continue 362 } 363 i := int(p.From.Offset) 364 pcln.Funcdataoff[i] = p.To.Offset 365 if p.To.Type != TYPE_CONST { 366 // TODO: Dedup. 367 //funcdata_bytes += p->to.sym->size; 368 pcln.Funcdata[i] = p.To.Sym 369 } 370 } 371 } 372 }