pcln.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. package obj
  5. import (
  6. "github.com/twitchyliquid64/golang-asm/goobj"
  7. "encoding/binary"
  8. "log"
  9. )
  10. // funcpctab writes to dst a pc-value table mapping the code in func to the values
  11. // returned by valfunc parameterized by arg. The invocation of valfunc to update the
  12. // current value is, for each p,
  13. //
  14. // val = valfunc(func, val, p, 0, arg);
  15. // record val as value at p->pc;
  16. // val = valfunc(func, val, p, 1, arg);
  17. //
  18. // where func is the function, val is the current value, p is the instruction being
  19. // considered, and arg can be used to further parameterize valfunc.
  20. func funcpctab(ctxt *Link, dst *Pcdata, func_ *LSym, desc string, valfunc func(*Link, *LSym, int32, *Prog, int32, interface{}) int32, arg interface{}) {
  21. dbg := desc == ctxt.Debugpcln
  22. dst.P = dst.P[:0]
  23. if dbg {
  24. ctxt.Logf("funcpctab %s [valfunc=%s]\n", func_.Name, desc)
  25. }
  26. val := int32(-1)
  27. oldval := val
  28. if func_.Func.Text == nil {
  29. return
  30. }
  31. pc := func_.Func.Text.Pc
  32. if dbg {
  33. ctxt.Logf("%6x %6d %v\n", uint64(pc), val, func_.Func.Text)
  34. }
  35. buf := make([]byte, binary.MaxVarintLen32)
  36. started := false
  37. for p := func_.Func.Text; p != nil; p = p.Link {
  38. // Update val. If it's not changing, keep going.
  39. val = valfunc(ctxt, func_, val, p, 0, arg)
  40. if val == oldval && started {
  41. val = valfunc(ctxt, func_, val, p, 1, arg)
  42. if dbg {
  43. ctxt.Logf("%6x %6s %v\n", uint64(p.Pc), "", p)
  44. }
  45. continue
  46. }
  47. // If the pc of the next instruction is the same as the
  48. // pc of this instruction, this instruction is not a real
  49. // instruction. Keep going, so that we only emit a delta
  50. // for a true instruction boundary in the program.
  51. if p.Link != nil && p.Link.Pc == p.Pc {
  52. val = valfunc(ctxt, func_, val, p, 1, arg)
  53. if dbg {
  54. ctxt.Logf("%6x %6s %v\n", uint64(p.Pc), "", p)
  55. }
  56. continue
  57. }
  58. // The table is a sequence of (value, pc) pairs, where each
  59. // pair states that the given value is in effect from the current position
  60. // up to the given pc, which becomes the new current position.
  61. // To generate the table as we scan over the program instructions,
  62. // we emit a "(value" when pc == func->value, and then
  63. // each time we observe a change in value we emit ", pc) (value".
  64. // When the scan is over, we emit the closing ", pc)".
  65. //
  66. // The table is delta-encoded. The value deltas are signed and
  67. // transmitted in zig-zag form, where a complement bit is placed in bit 0,
  68. // and the pc deltas are unsigned. Both kinds of deltas are sent
  69. // as variable-length little-endian base-128 integers,
  70. // where the 0x80 bit indicates that the integer continues.
  71. if dbg {
  72. ctxt.Logf("%6x %6d %v\n", uint64(p.Pc), val, p)
  73. }
  74. if started {
  75. pcdelta := (p.Pc - pc) / int64(ctxt.Arch.MinLC)
  76. n := binary.PutUvarint(buf, uint64(pcdelta))
  77. dst.P = append(dst.P, buf[:n]...)
  78. pc = p.Pc
  79. }
  80. delta := val - oldval
  81. n := binary.PutVarint(buf, int64(delta))
  82. dst.P = append(dst.P, buf[:n]...)
  83. oldval = val
  84. started = true
  85. val = valfunc(ctxt, func_, val, p, 1, arg)
  86. }
  87. if started {
  88. if dbg {
  89. ctxt.Logf("%6x done\n", uint64(func_.Func.Text.Pc+func_.Size))
  90. }
  91. v := (func_.Size - pc) / int64(ctxt.Arch.MinLC)
  92. if v < 0 {
  93. ctxt.Diag("negative pc offset: %v", v)
  94. }
  95. n := binary.PutUvarint(buf, uint64(v))
  96. dst.P = append(dst.P, buf[:n]...)
  97. // add terminating varint-encoded 0, which is just 0
  98. dst.P = append(dst.P, 0)
  99. }
  100. if dbg {
  101. ctxt.Logf("wrote %d bytes to %p\n", len(dst.P), dst)
  102. for _, p := range dst.P {
  103. ctxt.Logf(" %02x", p)
  104. }
  105. ctxt.Logf("\n")
  106. }
  107. }
  108. // pctofileline computes either the file number (arg == 0)
  109. // or the line number (arg == 1) to use at p.
  110. // Because p.Pos applies to p, phase == 0 (before p)
  111. // takes care of the update.
  112. func pctofileline(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 {
  113. if p.As == ATEXT || p.As == ANOP || p.Pos.Line() == 0 || phase == 1 {
  114. return oldval
  115. }
  116. f, l := getFileIndexAndLine(ctxt, p.Pos)
  117. if arg == nil {
  118. return l
  119. }
  120. pcln := arg.(*Pcln)
  121. pcln.UsedFiles[goobj.CUFileIndex(f)] = struct{}{}
  122. return int32(f)
  123. }
  124. // pcinlineState holds the state used to create a function's inlining
  125. // tree and the PC-value table that maps PCs to nodes in that tree.
  126. type pcinlineState struct {
  127. globalToLocal map[int]int
  128. localTree InlTree
  129. }
  130. // addBranch adds a branch from the global inlining tree in ctxt to
  131. // the function's local inlining tree, returning the index in the local tree.
  132. func (s *pcinlineState) addBranch(ctxt *Link, globalIndex int) int {
  133. if globalIndex < 0 {
  134. return -1
  135. }
  136. localIndex, ok := s.globalToLocal[globalIndex]
  137. if ok {
  138. return localIndex
  139. }
  140. // Since tracebacks don't include column information, we could
  141. // use one node for multiple calls of the same function on the
  142. // same line (e.g., f(x) + f(y)). For now, we use one node for
  143. // each inlined call.
  144. call := ctxt.InlTree.nodes[globalIndex]
  145. call.Parent = s.addBranch(ctxt, call.Parent)
  146. localIndex = len(s.localTree.nodes)
  147. s.localTree.nodes = append(s.localTree.nodes, call)
  148. s.globalToLocal[globalIndex] = localIndex
  149. return localIndex
  150. }
  151. func (s *pcinlineState) setParentPC(ctxt *Link, globalIndex int, pc int32) {
  152. localIndex, ok := s.globalToLocal[globalIndex]
  153. if !ok {
  154. // We know where to unwind to when we need to unwind a body identified
  155. // by globalIndex. But there may be no instructions generated by that
  156. // body (it's empty, or its instructions were CSEd with other things, etc.).
  157. // In that case, we don't need an unwind entry.
  158. // TODO: is this really right? Seems to happen a whole lot...
  159. return
  160. }
  161. s.localTree.setParentPC(localIndex, pc)
  162. }
  163. // pctoinline computes the index into the local inlining tree to use at p.
  164. // If p is not the result of inlining, pctoinline returns -1. Because p.Pos
  165. // applies to p, phase == 0 (before p) takes care of the update.
  166. func (s *pcinlineState) pctoinline(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 {
  167. if phase == 1 {
  168. return oldval
  169. }
  170. posBase := ctxt.PosTable.Pos(p.Pos).Base()
  171. if posBase == nil {
  172. return -1
  173. }
  174. globalIndex := posBase.InliningIndex()
  175. if globalIndex < 0 {
  176. return -1
  177. }
  178. if s.globalToLocal == nil {
  179. s.globalToLocal = make(map[int]int)
  180. }
  181. return int32(s.addBranch(ctxt, globalIndex))
  182. }
  183. // pctospadj computes the sp adjustment in effect.
  184. // It is oldval plus any adjustment made by p itself.
  185. // The adjustment by p takes effect only after p, so we
  186. // apply the change during phase == 1.
  187. func pctospadj(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 {
  188. if oldval == -1 { // starting
  189. oldval = 0
  190. }
  191. if phase == 0 {
  192. return oldval
  193. }
  194. if oldval+p.Spadj < -10000 || oldval+p.Spadj > 1100000000 {
  195. ctxt.Diag("overflow in spadj: %d + %d = %d", oldval, p.Spadj, oldval+p.Spadj)
  196. ctxt.DiagFlush()
  197. log.Fatalf("bad code")
  198. }
  199. return oldval + p.Spadj
  200. }
  201. // pctopcdata computes the pcdata value in effect at p.
  202. // A PCDATA instruction sets the value in effect at future
  203. // non-PCDATA instructions.
  204. // Since PCDATA instructions have no width in the final code,
  205. // it does not matter which phase we use for the update.
  206. func pctopcdata(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 {
  207. if phase == 0 || p.As != APCDATA || p.From.Offset != int64(arg.(uint32)) {
  208. return oldval
  209. }
  210. if int64(int32(p.To.Offset)) != p.To.Offset {
  211. ctxt.Diag("overflow in PCDATA instruction: %v", p)
  212. ctxt.DiagFlush()
  213. log.Fatalf("bad code")
  214. }
  215. return int32(p.To.Offset)
  216. }
  217. func linkpcln(ctxt *Link, cursym *LSym) {
  218. pcln := &cursym.Func.Pcln
  219. pcln.UsedFiles = make(map[goobj.CUFileIndex]struct{})
  220. npcdata := 0
  221. nfuncdata := 0
  222. for p := cursym.Func.Text; p != nil; p = p.Link {
  223. // Find the highest ID of any used PCDATA table. This ignores PCDATA table
  224. // that consist entirely of "-1", since that's the assumed default value.
  225. // From.Offset is table ID
  226. // To.Offset is data
  227. 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
  228. npcdata = int(p.From.Offset + 1)
  229. }
  230. // Find the highest ID of any FUNCDATA table.
  231. // From.Offset is table ID
  232. if p.As == AFUNCDATA && p.From.Offset >= int64(nfuncdata) {
  233. nfuncdata = int(p.From.Offset + 1)
  234. }
  235. }
  236. pcln.Pcdata = make([]Pcdata, npcdata)
  237. pcln.Pcdata = pcln.Pcdata[:npcdata]
  238. pcln.Funcdata = make([]*LSym, nfuncdata)
  239. pcln.Funcdataoff = make([]int64, nfuncdata)
  240. pcln.Funcdataoff = pcln.Funcdataoff[:nfuncdata]
  241. funcpctab(ctxt, &pcln.Pcsp, cursym, "pctospadj", pctospadj, nil)
  242. funcpctab(ctxt, &pcln.Pcfile, cursym, "pctofile", pctofileline, pcln)
  243. funcpctab(ctxt, &pcln.Pcline, cursym, "pctoline", pctofileline, nil)
  244. // Check that all the Progs used as inline markers are still reachable.
  245. // See issue #40473.
  246. inlMarkProgs := make(map[*Prog]struct{}, len(cursym.Func.InlMarks))
  247. for _, inlMark := range cursym.Func.InlMarks {
  248. inlMarkProgs[inlMark.p] = struct{}{}
  249. }
  250. for p := cursym.Func.Text; p != nil; p = p.Link {
  251. if _, ok := inlMarkProgs[p]; ok {
  252. delete(inlMarkProgs, p)
  253. }
  254. }
  255. if len(inlMarkProgs) > 0 {
  256. ctxt.Diag("one or more instructions used as inline markers are no longer reachable")
  257. }
  258. pcinlineState := new(pcinlineState)
  259. funcpctab(ctxt, &pcln.Pcinline, cursym, "pctoinline", pcinlineState.pctoinline, nil)
  260. for _, inlMark := range cursym.Func.InlMarks {
  261. pcinlineState.setParentPC(ctxt, int(inlMark.id), int32(inlMark.p.Pc))
  262. }
  263. pcln.InlTree = pcinlineState.localTree
  264. if ctxt.Debugpcln == "pctoinline" && len(pcln.InlTree.nodes) > 0 {
  265. ctxt.Logf("-- inlining tree for %s:\n", cursym)
  266. dumpInlTree(ctxt, pcln.InlTree)
  267. ctxt.Logf("--\n")
  268. }
  269. // tabulate which pc and func data we have.
  270. havepc := make([]uint32, (npcdata+31)/32)
  271. havefunc := make([]uint32, (nfuncdata+31)/32)
  272. for p := cursym.Func.Text; p != nil; p = p.Link {
  273. if p.As == AFUNCDATA {
  274. if (havefunc[p.From.Offset/32]>>uint64(p.From.Offset%32))&1 != 0 {
  275. ctxt.Diag("multiple definitions for FUNCDATA $%d", p.From.Offset)
  276. }
  277. havefunc[p.From.Offset/32] |= 1 << uint64(p.From.Offset%32)
  278. }
  279. if p.As == APCDATA && p.To.Offset != -1 {
  280. havepc[p.From.Offset/32] |= 1 << uint64(p.From.Offset%32)
  281. }
  282. }
  283. // pcdata.
  284. for i := 0; i < npcdata; i++ {
  285. if (havepc[i/32]>>uint(i%32))&1 == 0 {
  286. continue
  287. }
  288. funcpctab(ctxt, &pcln.Pcdata[i], cursym, "pctopcdata", pctopcdata, interface{}(uint32(i)))
  289. }
  290. // funcdata
  291. if nfuncdata > 0 {
  292. for p := cursym.Func.Text; p != nil; p = p.Link {
  293. if p.As != AFUNCDATA {
  294. continue
  295. }
  296. i := int(p.From.Offset)
  297. pcln.Funcdataoff[i] = p.To.Offset
  298. if p.To.Type != TYPE_CONST {
  299. // TODO: Dedup.
  300. //funcdata_bytes += p->to.sym->size;
  301. pcln.Funcdata[i] = p.To.Sym
  302. }
  303. }
  304. }
  305. }
  306. // PCIter iterates over encoded pcdata tables.
  307. type PCIter struct {
  308. p []byte
  309. PC uint32
  310. NextPC uint32
  311. PCScale uint32
  312. Value int32
  313. start bool
  314. Done bool
  315. }
  316. // newPCIter creates a PCIter with a scale factor for the PC step size.
  317. func NewPCIter(pcScale uint32) *PCIter {
  318. it := new(PCIter)
  319. it.PCScale = pcScale
  320. return it
  321. }
  322. // Next advances it to the Next pc.
  323. func (it *PCIter) Next() {
  324. it.PC = it.NextPC
  325. if it.Done {
  326. return
  327. }
  328. if len(it.p) == 0 {
  329. it.Done = true
  330. return
  331. }
  332. // Value delta
  333. val, n := binary.Varint(it.p)
  334. if n <= 0 {
  335. log.Fatalf("bad Value varint in pciterNext: read %v", n)
  336. }
  337. it.p = it.p[n:]
  338. if val == 0 && !it.start {
  339. it.Done = true
  340. return
  341. }
  342. it.start = false
  343. it.Value += int32(val)
  344. // pc delta
  345. pc, n := binary.Uvarint(it.p)
  346. if n <= 0 {
  347. log.Fatalf("bad pc varint in pciterNext: read %v", n)
  348. }
  349. it.p = it.p[n:]
  350. it.NextPC = it.PC + uint32(pc)*it.PCScale
  351. }
  352. // init prepares it to iterate over p,
  353. // and advances it to the first pc.
  354. func (it *PCIter) Init(p []byte) {
  355. it.p = p
  356. it.PC = 0
  357. it.NextPC = 0
  358. it.Value = -1
  359. it.start = true
  360. it.Done = false
  361. it.Next()
  362. }