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