1 // Inferno utils/6l/pass.c
2 // http://code.google.com/p/inferno-os/source/browse/utils/6l/pass.c
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.
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:
20 // The above copyright notice and this permission notice shall be included in
21 // all copies or substantial portions of the Software.
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
33 // Code and data passes.
35 func Brchain(ctxt *Link, p *Prog) *Prog {
36 for i := 0; i < 20; i++ {
37 if p == nil || p.As != AJMP || p.Pcond == nil {
46 func brloop(ctxt *Link, p *Prog) *Prog {
50 for q = p; q != nil; q = q.Pcond {
51 if q.As != AJMP || q.Pcond == nil {
63 func checkaddr(ctxt *Link, p *Prog, a *Addr) {
64 // Check expected encoding, especially TYPE_CONST vs TYPE_ADDR.
70 if a.Reg != 0 || a.Index != 0 || a.Scale != 0 || a.Name != 0 {
76 if a.Reg != 0 || a.Index != 0 || a.Scale != 0 || a.Name != 0 {
86 // TODO(rsc): After fixing SHRQ, check a->index != 0 too.
88 if a.Name != 0 || a.Sym != nil || a.Reg != 0 {
89 ctxt.Diag("argument is TYPE_CONST, should be TYPE_ADDR, in %v", p)
93 if a.Reg != 0 || a.Scale != 0 || a.Name != 0 || a.Sym != nil || a.Val != nil {
98 case TYPE_FCONST, TYPE_SCONST:
99 if a.Reg != 0 || a.Index != 0 || a.Scale != 0 || a.Name != 0 || a.Offset != 0 || a.Sym != nil {
104 // TODO(rsc): After fixing PINSRQ, check a->offset != 0 too.
105 // TODO(rsc): After fixing SHRQ, check a->index != 0 too.
107 if a.Scale != 0 || a.Name != 0 || a.Sym != nil {
116 if a.Reg == 0 && a.Index == 0 && a.Scale == 0 && a.Name == 0 && a.Sym == nil {
117 ctxt.Diag("argument is TYPE_ADDR, should be TYPE_CONST, in %v", p)
122 if a.Index != 0 || a.Scale != 0 || a.Name != 0 || a.Sym != nil || a.Val != nil {
128 if a.Index != 0 || a.Scale != 0 || a.Name != 0 || a.Sym != nil || a.Val != nil {
139 // Expect sym and name to be set, nothing else.
140 // Technically more is allowed, but this is only used for *name(SB).
142 if a.Reg != 0 || a.Index != 0 || a.Scale != 0 || a.Name == 0 || a.Offset != 0 || a.Sym == nil || a.Val != nil {
148 ctxt.Diag("invalid encoding for argument %v", p)
151 func linkpatch(ctxt *Link, sym *LSym) {
158 for p := sym.Text; p != nil; p = p.Link {
159 checkaddr(ctxt, p, &p.From)
161 checkaddr(ctxt, p, p.From3)
163 checkaddr(ctxt, p, &p.To)
165 if ctxt.Arch.Progedit != nil {
166 ctxt.Arch.Progedit(ctxt, p)
168 if p.To.Type != TYPE_BRANCH {
172 // TODO: Remove To.Val.(*Prog) in favor of p->pcond.
173 p.Pcond = p.To.Val.(*Prog)
180 c = int32(p.To.Offset)
181 for q = sym.Text; q != nil; {
182 if int64(c) == q.Pc {
185 if q.Forwd != nil && int64(c) >= q.Forwd.Pc {
197 ctxt.Diag("branch out of range (%#x)\n%v [%s]", uint32(c), p, name)
198 p.To.Type = TYPE_NONE
205 if ctxt.Flag_optimize {
206 for p := sym.Text; p != nil; p = p.Link {
208 p.Pcond = brloop(ctxt, p.Pcond)
210 if p.To.Type == TYPE_BRANCH {
211 p.To.Offset = p.Pcond.Pc