aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/internal/obj/util.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/internal/obj/util.go')
-rw-r--r--src/cmd/internal/obj/util.go167
1 files changed, 93 insertions, 74 deletions
diff --git a/src/cmd/internal/obj/util.go b/src/cmd/internal/obj/util.go
index 46d662c6c8..d020026445 100644
--- a/src/cmd/internal/obj/util.go
+++ b/src/cmd/internal/obj/util.go
@@ -8,6 +8,7 @@ import (
"bytes"
"cmd/internal/objabi"
"fmt"
+ "io"
"strings"
)
@@ -17,8 +18,8 @@ const REG_NONE = 0
func (p *Prog) Line() string {
return p.Ctxt.OutermostPos(p.Pos).Format(false, true)
}
-func (p *Prog) InnermostLine() string {
- return p.Ctxt.InnermostPos(p.Pos).Format(false, true)
+func (p *Prog) InnermostLine(w io.Writer) {
+ p.Ctxt.InnermostPos(p.Pos).WriteTo(w, false, true)
}
// InnermostLineNumber returns a string containing the line number for the
@@ -121,45 +122,61 @@ func (p *Prog) String() string {
return fmt.Sprintf("%.5d (%v)\t%s", p.Pc, p.Line(), p.InstructionString())
}
-func (p *Prog) InnermostString() string {
+func (p *Prog) InnermostString(w io.Writer) {
if p == nil {
- return "<nil Prog>"
+ io.WriteString(w, "<nil Prog>")
+ return
}
if p.Ctxt == nil {
- return "<Prog without ctxt>"
+ io.WriteString(w, "<Prog without ctxt>")
+ return
}
- return fmt.Sprintf("%.5d (%v)\t%s", p.Pc, p.InnermostLine(), p.InstructionString())
+ fmt.Fprintf(w, "%.5d (", p.Pc)
+ p.InnermostLine(w)
+ io.WriteString(w, ")\t")
+ p.WriteInstructionString(w)
}
// InstructionString returns a string representation of the instruction without preceding
// program counter or file and line number.
func (p *Prog) InstructionString() string {
+ buf := new(bytes.Buffer)
+ p.WriteInstructionString(buf)
+ return buf.String()
+}
+
+// WriteInstructionString writes a string representation of the instruction without preceding
+// program counter or file and line number.
+func (p *Prog) WriteInstructionString(w io.Writer) {
if p == nil {
- return "<nil Prog>"
+ io.WriteString(w, "<nil Prog>")
+ return
}
if p.Ctxt == nil {
- return "<Prog without ctxt>"
+ io.WriteString(w, "<Prog without ctxt>")
+ return
}
sc := CConv(p.Scond)
- var buf bytes.Buffer
-
- fmt.Fprintf(&buf, "%v%s", p.As, sc)
+ io.WriteString(w, p.As.String())
+ io.WriteString(w, sc)
sep := "\t"
if p.From.Type != TYPE_NONE {
- fmt.Fprintf(&buf, "%s%v", sep, Dconv(p, &p.From))
+ io.WriteString(w, sep)
+ WriteDconv(w, p, &p.From)
sep = ", "
}
if p.Reg != REG_NONE {
// Should not happen but might as well show it if it does.
- fmt.Fprintf(&buf, "%s%v", sep, Rconv(int(p.Reg)))
+ fmt.Fprintf(w, "%s%v", sep, Rconv(int(p.Reg)))
sep = ", "
}
for i := range p.RestArgs {
- fmt.Fprintf(&buf, "%s%v", sep, Dconv(p, &p.RestArgs[i]))
+ io.WriteString(w, sep)
+ WriteDconv(w, p, &p.RestArgs[i])
sep = ", "
}
@@ -170,17 +187,17 @@ func (p *Prog) InstructionString() string {
// TEXT foo(SB), $0
s := p.From.Sym.Attribute.TextAttrString()
if s != "" {
- fmt.Fprintf(&buf, "%s%s", sep, s)
+ fmt.Fprintf(w, "%s%s", sep, s)
sep = ", "
}
}
if p.To.Type != TYPE_NONE {
- fmt.Fprintf(&buf, "%s%v", sep, Dconv(p, &p.To))
+ io.WriteString(w, sep)
+ WriteDconv(w, p, &p.To)
}
if p.RegTo2 != REG_NONE {
- fmt.Fprintf(&buf, "%s%v", sep, Rconv(int(p.RegTo2)))
+ fmt.Fprintf(w, "%s%v", sep, Rconv(int(p.RegTo2)))
}
- return buf.String()
}
func (ctxt *Link) NewProg() *Prog {
@@ -194,16 +211,20 @@ func (ctxt *Link) CanReuseProgs() bool {
}
func Dconv(p *Prog, a *Addr) string {
- var str string
+ buf := new(bytes.Buffer)
+ WriteDconv(buf, p, a)
+ return buf.String()
+}
+func WriteDconv(w io.Writer, p *Prog, a *Addr) {
switch a.Type {
default:
- str = fmt.Sprintf("type=%d", a.Type)
+ fmt.Fprintf(w, "type=%d", a.Type)
case TYPE_NONE:
- str = ""
if a.Name != NAME_NONE || a.Reg != 0 || a.Sym != nil {
- str = fmt.Sprintf("%v(%v)(NONE)", Mconv(a), Rconv(int(a.Reg)))
+ a.WriteNameTo(w)
+ fmt.Fprintf(w, "(%v)(NONE)", Rconv(int(a.Reg)))
}
case TYPE_REG:
@@ -212,71 +233,75 @@ func Dconv(p *Prog, a *Addr) string {
// where the $1 is included in the p->to Addr.
// Move into a new field.
if a.Offset != 0 && (a.Reg < RBaseARM64 || a.Reg >= RBaseMIPS) {
- str = fmt.Sprintf("$%d,%v", a.Offset, Rconv(int(a.Reg)))
- break
+ fmt.Fprintf(w, "$%d,%v", a.Offset, Rconv(int(a.Reg)))
+ return
}
- str = Rconv(int(a.Reg))
if a.Name != NAME_NONE || a.Sym != nil {
- str = fmt.Sprintf("%v(%v)(REG)", Mconv(a), Rconv(int(a.Reg)))
+ a.WriteNameTo(w)
+ fmt.Fprintf(w, "(%v)(REG)", Rconv(int(a.Reg)))
+ } else {
+ io.WriteString(w, Rconv(int(a.Reg)))
}
if (RBaseARM64+1<<10+1<<9) /* arm64.REG_ELEM */ <= a.Reg &&
a.Reg < (RBaseARM64+1<<11) /* arm64.REG_ELEM_END */ {
- str += fmt.Sprintf("[%d]", a.Index)
+ fmt.Fprintf(w, "[%d]", a.Index)
}
case TYPE_BRANCH:
if a.Sym != nil {
- str = fmt.Sprintf("%s(SB)", a.Sym.Name)
+ fmt.Fprintf(w, "%s(SB)", a.Sym.Name)
} else if p != nil && p.Pcond != nil {
- str = fmt.Sprint(p.Pcond.Pc)
+ fmt.Fprint(w, p.Pcond.Pc)
} else if a.Val != nil {
- str = fmt.Sprint(a.Val.(*Prog).Pc)
+ fmt.Fprint(w, a.Val.(*Prog).Pc)
} else {
- str = fmt.Sprintf("%d(PC)", a.Offset)
+ fmt.Fprintf(w, "%d(PC)", a.Offset)
}
case TYPE_INDIR:
- str = fmt.Sprintf("*%s", Mconv(a))
+ io.WriteString(w, "*")
+ a.WriteNameTo(w)
case TYPE_MEM:
- str = Mconv(a)
+ a.WriteNameTo(w)
if a.Index != REG_NONE {
if a.Scale == 0 {
// arm64 shifted or extended register offset, scale = 0.
- str += fmt.Sprintf("(%v)", Rconv(int(a.Index)))
+ fmt.Fprintf(w, "(%v)", Rconv(int(a.Index)))
} else {
- str += fmt.Sprintf("(%v*%d)", Rconv(int(a.Index)), int(a.Scale))
+ fmt.Fprintf(w, "(%v*%d)", Rconv(int(a.Index)), int(a.Scale))
}
}
case TYPE_CONST:
+ io.WriteString(w, "$")
+ a.WriteNameTo(w)
if a.Reg != 0 {
- str = fmt.Sprintf("$%v(%v)", Mconv(a), Rconv(int(a.Reg)))
- } else {
- str = fmt.Sprintf("$%v", Mconv(a))
+ fmt.Fprintf(w, "(%v)", Rconv(int(a.Reg)))
}
case TYPE_TEXTSIZE:
if a.Val.(int32) == objabi.ArgsSizeUnknown {
- str = fmt.Sprintf("$%d", a.Offset)
+ fmt.Fprintf(w, "$%d", a.Offset)
} else {
- str = fmt.Sprintf("$%d-%d", a.Offset, a.Val.(int32))
+ fmt.Fprintf(w, "$%d-%d", a.Offset, a.Val.(int32))
}
case TYPE_FCONST:
- str = fmt.Sprintf("%.17g", a.Val.(float64))
+ str := fmt.Sprintf("%.17g", a.Val.(float64))
// Make sure 1 prints as 1.0
if !strings.ContainsAny(str, ".e") {
str += ".0"
}
- str = fmt.Sprintf("$(%s)", str)
+ fmt.Fprintf(w, "$(%s)", str)
case TYPE_SCONST:
- str = fmt.Sprintf("$%q", a.Val.(string))
+ fmt.Fprintf(w, "$%q", a.Val.(string))
case TYPE_ADDR:
- str = fmt.Sprintf("$%s", Mconv(a))
+ io.WriteString(w, "$")
+ a.WriteNameTo(w)
case TYPE_SHIFT:
v := int(a.Offset)
@@ -285,49 +310,45 @@ func Dconv(p *Prog, a *Addr) string {
case "arm":
op := ops[((v>>5)&3)<<1:]
if v&(1<<4) != 0 {
- str = fmt.Sprintf("R%d%c%cR%d", v&15, op[0], op[1], (v>>8)&15)
+ fmt.Fprintf(w, "R%d%c%cR%d", v&15, op[0], op[1], (v>>8)&15)
} else {
- str = fmt.Sprintf("R%d%c%c%d", v&15, op[0], op[1], (v>>7)&31)
+ fmt.Fprintf(w, "R%d%c%c%d", v&15, op[0], op[1], (v>>7)&31)
}
if a.Reg != 0 {
- str += fmt.Sprintf("(%v)", Rconv(int(a.Reg)))
+ fmt.Fprintf(w, "(%v)", Rconv(int(a.Reg)))
}
case "arm64":
op := ops[((v>>22)&3)<<1:]
r := (v >> 16) & 31
- str = fmt.Sprintf("%s%c%c%d", Rconv(r+RBaseARM64), op[0], op[1], (v>>10)&63)
+ fmt.Fprintf(w, "%s%c%c%d", Rconv(r+RBaseARM64), op[0], op[1], (v>>10)&63)
default:
panic("TYPE_SHIFT is not supported on " + objabi.GOARCH)
}
case TYPE_REGREG:
- str = fmt.Sprintf("(%v, %v)", Rconv(int(a.Reg)), Rconv(int(a.Offset)))
+ fmt.Fprintf(w, "(%v, %v)", Rconv(int(a.Reg)), Rconv(int(a.Offset)))
case TYPE_REGREG2:
- str = fmt.Sprintf("%v, %v", Rconv(int(a.Offset)), Rconv(int(a.Reg)))
+ fmt.Fprintf(w, "%v, %v", Rconv(int(a.Offset)), Rconv(int(a.Reg)))
case TYPE_REGLIST:
- str = RLconv(a.Offset)
+ io.WriteString(w, RLconv(a.Offset))
}
-
- return str
}
-func Mconv(a *Addr) string {
- var str string
-
+func (a *Addr) WriteNameTo(w io.Writer) {
switch a.Name {
default:
- str = fmt.Sprintf("name=%d", a.Name)
+ fmt.Fprintf(w, "name=%d", a.Name)
case NAME_NONE:
switch {
case a.Reg == REG_NONE:
- str = fmt.Sprint(a.Offset)
+ fmt.Fprint(w, a.Offset)
case a.Offset == 0:
- str = fmt.Sprintf("(%v)", Rconv(int(a.Reg)))
+ fmt.Fprintf(w, "(%v)", Rconv(int(a.Reg)))
case a.Offset != 0:
- str = fmt.Sprintf("%d(%v)", a.Offset, Rconv(int(a.Reg)))
+ fmt.Fprintf(w, "%d(%v)", a.Offset, Rconv(int(a.Reg)))
}
// Note: a.Reg == REG_NONE encodes the default base register for the NAME_ type.
@@ -337,9 +358,9 @@ func Mconv(a *Addr) string {
reg = Rconv(int(a.Reg))
}
if a.Sym != nil {
- str = fmt.Sprintf("%s%s(%s)", a.Sym.Name, offConv(a.Offset), reg)
+ fmt.Fprintf(w, "%s%s(%s)", a.Sym.Name, offConv(a.Offset), reg)
} else {
- str = fmt.Sprintf("%s(%s)", offConv(a.Offset), reg)
+ fmt.Fprintf(w, "%s(%s)", offConv(a.Offset), reg)
}
case NAME_GOTREF:
@@ -348,9 +369,9 @@ func Mconv(a *Addr) string {
reg = Rconv(int(a.Reg))
}
if a.Sym != nil {
- str = fmt.Sprintf("%s%s@GOT(%s)", a.Sym.Name, offConv(a.Offset), reg)
+ fmt.Fprintf(w, "%s%s@GOT(%s)", a.Sym.Name, offConv(a.Offset), reg)
} else {
- str = fmt.Sprintf("%s@GOT(%s)", offConv(a.Offset), reg)
+ fmt.Fprintf(w, "%s@GOT(%s)", offConv(a.Offset), reg)
}
case NAME_STATIC:
@@ -359,9 +380,9 @@ func Mconv(a *Addr) string {
reg = Rconv(int(a.Reg))
}
if a.Sym != nil {
- str = fmt.Sprintf("%s<>%s(%s)", a.Sym.Name, offConv(a.Offset), reg)
+ fmt.Fprintf(w, "%s<>%s(%s)", a.Sym.Name, offConv(a.Offset), reg)
} else {
- str = fmt.Sprintf("<>%s(%s)", offConv(a.Offset), reg)
+ fmt.Fprintf(w, "<>%s(%s)", offConv(a.Offset), reg)
}
case NAME_AUTO:
@@ -370,9 +391,9 @@ func Mconv(a *Addr) string {
reg = Rconv(int(a.Reg))
}
if a.Sym != nil {
- str = fmt.Sprintf("%s%s(%s)", a.Sym.Name, offConv(a.Offset), reg)
+ fmt.Fprintf(w, "%s%s(%s)", a.Sym.Name, offConv(a.Offset), reg)
} else {
- str = fmt.Sprintf("%s(%s)", offConv(a.Offset), reg)
+ fmt.Fprintf(w, "%s(%s)", offConv(a.Offset), reg)
}
case NAME_PARAM:
@@ -381,9 +402,9 @@ func Mconv(a *Addr) string {
reg = Rconv(int(a.Reg))
}
if a.Sym != nil {
- str = fmt.Sprintf("%s%s(%s)", a.Sym.Name, offConv(a.Offset), reg)
+ fmt.Fprintf(w, "%s%s(%s)", a.Sym.Name, offConv(a.Offset), reg)
} else {
- str = fmt.Sprintf("%s(%s)", offConv(a.Offset), reg)
+ fmt.Fprintf(w, "%s(%s)", offConv(a.Offset), reg)
}
case NAME_TOCREF:
reg := "SB"
@@ -391,13 +412,11 @@ func Mconv(a *Addr) string {
reg = Rconv(int(a.Reg))
}
if a.Sym != nil {
- str = fmt.Sprintf("%s%s(%s)", a.Sym.Name, offConv(a.Offset), reg)
+ fmt.Fprintf(w, "%s%s(%s)", a.Sym.Name, offConv(a.Offset), reg)
} else {
- str = fmt.Sprintf("%s(%s)", offConv(a.Offset), reg)
+ fmt.Fprintf(w, "%s(%s)", offConv(a.Offset), reg)
}
-
}
- return str
}
func offConv(off int64) string {