pass.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Inferno utils/6l/pass.c
  2. // https://bitbucket.org/inferno-os/inferno-os/src/master/utils/6l/pass.c
  3. //
  4. // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
  5. // Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
  6. // Portions Copyright © 1997-1999 Vita Nuova Limited
  7. // Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
  8. // Portions Copyright © 2004,2006 Bruce Ellis
  9. // Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
  10. // Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
  11. // Portions Copyright © 2009 The Go Authors. All rights reserved.
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining a copy
  14. // of this software and associated documentation files (the "Software"), to deal
  15. // in the Software without restriction, including without limitation the rights
  16. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. // copies of the Software, and to permit persons to whom the Software is
  18. // furnished to do so, subject to the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be included in
  21. // all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. // THE SOFTWARE.
  30. package obj
  31. // Code and data passes.
  32. // brloop returns the ultimate destination of the series of unconditional jumps beginning at p.
  33. // In the case of an infinite loop, brloop returns nil.
  34. func brloop(p *Prog) *Prog {
  35. c := 0
  36. for q := p; q != nil; q = q.To.Target() {
  37. if q.As != AJMP || q.To.Target() == nil {
  38. return q
  39. }
  40. c++
  41. if c >= 5000 {
  42. // infinite loop
  43. return nil
  44. }
  45. }
  46. panic("unreachable")
  47. }
  48. // checkaddr checks that a has an expected encoding, especially TYPE_CONST vs TYPE_ADDR.
  49. func checkaddr(ctxt *Link, p *Prog, a *Addr) {
  50. switch a.Type {
  51. case TYPE_NONE, TYPE_REGREG2, TYPE_REGLIST:
  52. return
  53. case TYPE_BRANCH, TYPE_TEXTSIZE:
  54. if a.Reg != 0 || a.Index != 0 || a.Scale != 0 || a.Name != 0 {
  55. break
  56. }
  57. return
  58. case TYPE_MEM:
  59. return
  60. case TYPE_CONST:
  61. // TODO(rsc): After fixing SHRQ, check a.Index != 0 too.
  62. if a.Name != 0 || a.Sym != nil || a.Reg != 0 {
  63. ctxt.Diag("argument is TYPE_CONST, should be TYPE_ADDR, in %v", p)
  64. return
  65. }
  66. if a.Reg != 0 || a.Scale != 0 || a.Name != 0 || a.Sym != nil || a.Val != nil {
  67. break
  68. }
  69. return
  70. case TYPE_FCONST, TYPE_SCONST:
  71. if a.Reg != 0 || a.Index != 0 || a.Scale != 0 || a.Name != 0 || a.Offset != 0 || a.Sym != nil {
  72. break
  73. }
  74. return
  75. case TYPE_REG:
  76. // TODO(rsc): After fixing PINSRQ, check a.Offset != 0 too.
  77. // TODO(rsc): After fixing SHRQ, check a.Index != 0 too.
  78. if a.Scale != 0 || a.Name != 0 || a.Sym != nil {
  79. break
  80. }
  81. return
  82. case TYPE_ADDR:
  83. if a.Val != nil {
  84. break
  85. }
  86. if a.Reg == 0 && a.Index == 0 && a.Scale == 0 && a.Name == 0 && a.Sym == nil {
  87. ctxt.Diag("argument is TYPE_ADDR, should be TYPE_CONST, in %v", p)
  88. }
  89. return
  90. case TYPE_SHIFT, TYPE_REGREG:
  91. if a.Index != 0 || a.Scale != 0 || a.Name != 0 || a.Sym != nil || a.Val != nil {
  92. break
  93. }
  94. return
  95. case TYPE_INDIR:
  96. // Expect sym and name to be set, nothing else.
  97. // Technically more is allowed, but this is only used for *name(SB).
  98. if a.Reg != 0 || a.Index != 0 || a.Scale != 0 || a.Name == 0 || a.Offset != 0 || a.Sym == nil || a.Val != nil {
  99. break
  100. }
  101. return
  102. }
  103. ctxt.Diag("invalid encoding for argument %v", p)
  104. }
  105. func linkpatch(ctxt *Link, sym *LSym, newprog ProgAlloc) {
  106. for p := sym.Func.Text; p != nil; p = p.Link {
  107. checkaddr(ctxt, p, &p.From)
  108. if p.GetFrom3() != nil {
  109. checkaddr(ctxt, p, p.GetFrom3())
  110. }
  111. checkaddr(ctxt, p, &p.To)
  112. if ctxt.Arch.Progedit != nil {
  113. ctxt.Arch.Progedit(ctxt, p, newprog)
  114. }
  115. if p.To.Type != TYPE_BRANCH {
  116. continue
  117. }
  118. if p.To.Val != nil {
  119. continue
  120. }
  121. if p.To.Sym != nil {
  122. continue
  123. }
  124. q := sym.Func.Text
  125. for q != nil && p.To.Offset != q.Pc {
  126. if q.Forwd != nil && p.To.Offset >= q.Forwd.Pc {
  127. q = q.Forwd
  128. } else {
  129. q = q.Link
  130. }
  131. }
  132. if q == nil {
  133. name := "<nil>"
  134. if p.To.Sym != nil {
  135. name = p.To.Sym.Name
  136. }
  137. ctxt.Diag("branch out of range (%#x)\n%v [%s]", uint32(p.To.Offset), p, name)
  138. p.To.Type = TYPE_NONE
  139. }
  140. p.To.SetTarget(q)
  141. }
  142. if !ctxt.Flag_optimize {
  143. return
  144. }
  145. // Collapse series of jumps to jumps.
  146. for p := sym.Func.Text; p != nil; p = p.Link {
  147. if p.To.Target() == nil {
  148. continue
  149. }
  150. p.To.SetTarget(brloop(p.To.Target()))
  151. if p.To.Target() != nil && p.To.Type == TYPE_BRANCH {
  152. p.To.Offset = p.To.Target().Pc
  153. }
  154. }
  155. }