"bytes"
"cmd/internal/objabi"
"fmt"
+ "io"
"strings"
)
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
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 = ", "
}
// 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 {
}
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:
// 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)
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.
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:
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:
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:
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:
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"
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 {
package src
import (
+ "bytes"
"fmt"
- "strconv"
+ "io"
)
// A Pos encodes a source position consisting of a (line, column) number pair
// shown as well, as in "filename:line[origfile:origline:origcolumn] if
// showOrig is set.
func (p Pos) Format(showCol, showOrig bool) string {
+ buf := new(bytes.Buffer)
+ p.WriteTo(buf, showCol, showOrig)
+ return buf.String()
+}
+
+// WriteTo a position to w, formatted as Format does.
+func (p Pos) WriteTo(w io.Writer, showCol, showOrig bool) {
if !p.IsKnown() {
- return "<unknown line number>"
+ io.WriteString(w, "<unknown line number>")
+ return
}
if b := p.base; b == b.Pos().base {
// base is file base (incl. nil)
- return format(p.Filename(), p.Line(), p.Col(), showCol)
+ format(w, p.Filename(), p.Line(), p.Col(), showCol)
+ return
}
// base is relative
// that's provided via a line directive).
// TODO(gri) This may not be true if we have an inlining base.
// We may want to differentiate at some point.
- s := format(p.RelFilename(), p.RelLine(), p.RelCol(), showCol)
+ format(w, p.RelFilename(), p.RelLine(), p.RelCol(), showCol)
if showOrig {
- s += "[" + format(p.Filename(), p.Line(), p.Col(), showCol) + "]"
+ io.WriteString(w, "[")
+ format(w, p.Filename(), p.Line(), p.Col(), showCol)
+ io.WriteString(w, "]")
}
- return s
}
// format formats a (filename, line, col) tuple as "filename:line" (showCol
// is false or col == 0) or "filename:line:column" (showCol is true and col != 0).
-func format(filename string, line, col uint, showCol bool) string {
- s := filename + ":" + strconv.FormatUint(uint64(line), 10)
+func format(w io.Writer, filename string, line, col uint, showCol bool) {
+ io.WriteString(w, filename)
+ io.WriteString(w, ":")
+ fmt.Fprint(w, line)
// col == 0 and col == colMax are interpreted as unknown column values
if showCol && 0 < col && col < colMax {
- s += ":" + strconv.FormatUint(uint64(col), 10)
+ io.WriteString(w, ":")
+ fmt.Fprint(w, col)
}
- return s
+}
+
+// formatstr wraps format to return a string.
+func formatstr(filename string, line, col uint, showCol bool) string {
+ buf := new(bytes.Buffer)
+ format(buf, filename, line, col, showCol)
+ return buf.String()
}
// ----------------------------------------------------------------------------