github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/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 ctxt.DiagFlush() 227 log.Fatalf("bad code") 228 } 229 230 return oldval + p.Spadj 231 } 232 233 // pctopcdata computes the pcdata value in effect at p. 234 // A PCDATA instruction sets the value in effect at future 235 // non-PCDATA instructions. 236 // Since PCDATA instructions have no width in the final code, 237 // it does not matter which phase we use for the update. 238 func pctopcdata(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 { 239 if phase == 0 || p.As != APCDATA || p.From.Offset != int64(arg.(uint32)) { 240 return oldval 241 } 242 if int64(int32(p.To.Offset)) != p.To.Offset { 243 ctxt.Diag("overflow in PCDATA instruction: %v", p) 244 ctxt.DiagFlush() 245 log.Fatalf("bad code") 246 } 247 248 return int32(p.To.Offset) 249 } 250 251 func linkpcln(ctxt *Link, cursym *LSym) { 252 pcln := &cursym.Func.Pcln 253 254 npcdata := 0 255 nfuncdata := 0 256 for p := cursym.Func.Text; p != nil; p = p.Link { 257 // Find the highest ID of any used PCDATA table. This ignores PCDATA table 258 // that consist entirely of "-1", since that's the assumed default value. 259 // From.Offset is table ID 260 // To.Offset is data 261 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 262 npcdata = int(p.From.Offset + 1) 263 } 264 // Find the highest ID of any FUNCDATA table. 265 // From.Offset is table ID 266 if p.As == AFUNCDATA && p.From.Offset >= int64(nfuncdata) { 267 nfuncdata = int(p.From.Offset + 1) 268 } 269 } 270 271 pcln.Pcdata = make([]Pcdata, npcdata) 272 pcln.Pcdata = pcln.Pcdata[:npcdata] 273 pcln.Funcdata = make([]*LSym, nfuncdata) 274 pcln.Funcdataoff = make([]int64, nfuncdata) 275 pcln.Funcdataoff = pcln.Funcdataoff[:nfuncdata] 276 277 funcpctab(ctxt, &pcln.Pcsp, cursym, "pctospadj", pctospadj, nil) 278 funcpctab(ctxt, &pcln.Pcfile, cursym, "pctofile", pctofileline, pcln) 279 funcpctab(ctxt, &pcln.Pcline, cursym, "pctoline", pctofileline, nil) 280 281 pcinlineState := new(pcinlineState) 282 funcpctab(ctxt, &pcln.Pcinline, cursym, "pctoinline", pcinlineState.pctoinline, nil) 283 pcln.InlTree = pcinlineState.localTree 284 if ctxt.Debugpcln == "pctoinline" && len(pcln.InlTree.nodes) > 0 { 285 ctxt.Logf("-- inlining tree for %s:\n", cursym) 286 dumpInlTree(ctxt, pcln.InlTree) 287 ctxt.Logf("--\n") 288 } 289 290 // tabulate which pc and func data we have. 291 havepc := make([]uint32, (npcdata+31)/32) 292 havefunc := make([]uint32, (nfuncdata+31)/32) 293 for p := cursym.Func.Text; p != nil; p = p.Link { 294 if p.As == AFUNCDATA { 295 if (havefunc[p.From.Offset/32]>>uint64(p.From.Offset%32))&1 != 0 { 296 ctxt.Diag("multiple definitions for FUNCDATA $%d", p.From.Offset) 297 } 298 havefunc[p.From.Offset/32] |= 1 << uint64(p.From.Offset%32) 299 } 300 301 if p.As == APCDATA && p.To.Offset != -1 { 302 havepc[p.From.Offset/32] |= 1 << uint64(p.From.Offset%32) 303 } 304 } 305 306 // pcdata. 307 for i := 0; i < npcdata; i++ { 308 if (havepc[i/32]>>uint(i%32))&1 == 0 { 309 continue 310 } 311 funcpctab(ctxt, &pcln.Pcdata[i], cursym, "pctopcdata", pctopcdata, interface{}(uint32(i))) 312 } 313 314 // funcdata 315 if nfuncdata > 0 { 316 var i int 317 for p := cursym.Func.Text; p != nil; p = p.Link { 318 if p.As == AFUNCDATA { 319 i = int(p.From.Offset) 320 pcln.Funcdataoff[i] = p.To.Offset 321 if p.To.Type != TYPE_CONST { 322 // TODO: Dedup. 323 //funcdata_bytes += p->to.sym->size; 324 pcln.Funcdata[i] = p.To.Sym 325 } 326 } 327 } 328 } 329 }