mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/internal/obj: remove use of package bio
Also add MustClose and MustWriter to cmd/internal/bio, and use them in cmd/asm. Change-Id: I07f5df3b66c17bc5b2e6ec9c4357d9b653e354e0 Reviewed-on: https://go-review.googlesource.com/21938 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
b623b71509
commit
045411e6f2
5 changed files with 57 additions and 12 deletions
|
|
@ -44,12 +44,15 @@ func main() {
|
||||||
defer ctxt.Bso.Flush()
|
defer ctxt.Bso.Flush()
|
||||||
|
|
||||||
// Create object file, write header.
|
// Create object file, write header.
|
||||||
output, err := bio.Create(*flags.OutputFile)
|
out, err := os.Create(*flags.OutputFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Fprintf(output, "go object %s %s %s\n", obj.Getgoos(), obj.Getgoarch(), obj.Getgoversion())
|
defer bio.MustClose(out)
|
||||||
fmt.Fprintf(output, "!\n")
|
buf := bufio.NewWriter(bio.MustWriter(out))
|
||||||
|
|
||||||
|
fmt.Fprintf(buf, "go object %s %s %s\n", obj.Getgoos(), obj.Getgoarch(), obj.Getgoversion())
|
||||||
|
fmt.Fprintf(buf, "!\n")
|
||||||
|
|
||||||
lexer := lex.NewLexer(flag.Arg(0), ctxt)
|
lexer := lex.NewLexer(flag.Arg(0), ctxt)
|
||||||
parser := asm.NewParser(ctxt, architecture, lexer)
|
parser := asm.NewParser(ctxt, architecture, lexer)
|
||||||
|
|
@ -63,12 +66,12 @@ func main() {
|
||||||
pList.Firstpc, ok = parser.Parse()
|
pList.Firstpc, ok = parser.Parse()
|
||||||
if ok {
|
if ok {
|
||||||
// reports errors to parser.Errorf
|
// reports errors to parser.Errorf
|
||||||
obj.Writeobjdirect(ctxt, output)
|
obj.Writeobjdirect(ctxt, buf)
|
||||||
}
|
}
|
||||||
if !ok || diag {
|
if !ok || diag {
|
||||||
log.Printf("assembly of %s failed", flag.Arg(0))
|
log.Printf("assembly of %s failed", flag.Arg(0))
|
||||||
os.Remove(*flags.OutputFile)
|
os.Remove(*flags.OutputFile)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
output.Flush()
|
buf.Flush()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ func dumpobj() {
|
||||||
externdcl = tmp
|
externdcl = tmp
|
||||||
|
|
||||||
dumpdata()
|
dumpdata()
|
||||||
obj.Writeobjdirect(Ctxt, bout)
|
obj.Writeobjdirect(Ctxt, bout.Writer)
|
||||||
|
|
||||||
if writearchive {
|
if writearchive {
|
||||||
bout.Flush()
|
bout.Flush()
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
// Package bio implements seekable buffered I/O.
|
// Package bio implements common I/O abstractions used within the Go toolchain.
|
||||||
package bio
|
package bio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
||||||
43
src/cmd/internal/bio/must.go
Normal file
43
src/cmd/internal/bio/must.go
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
// Copyright 2016 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package bio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MustClose closes Closer c and calls log.Fatal if it returns a non-nil error.
|
||||||
|
func MustClose(c io.Closer) {
|
||||||
|
if err := c.Close(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustWriter returns a Writer that wraps the provided Writer,
|
||||||
|
// except that it calls log.Fatal instead of returning a non-nil error.
|
||||||
|
func MustWriter(w io.Writer) io.Writer {
|
||||||
|
return mustWriter{w}
|
||||||
|
}
|
||||||
|
|
||||||
|
type mustWriter struct {
|
||||||
|
w io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w mustWriter) Write(b []byte) (int, error) {
|
||||||
|
n, err := w.w.Write(b)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w mustWriter) WriteString(s string) (int, error) {
|
||||||
|
n, err := io.WriteString(w.w, s)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
@ -109,7 +109,6 @@ package obj
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"cmd/internal/bio"
|
|
||||||
"cmd/internal/sys"
|
"cmd/internal/sys"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
@ -120,7 +119,7 @@ import (
|
||||||
// The Go and C compilers, and the assembler, call writeobj to write
|
// The Go and C compilers, and the assembler, call writeobj to write
|
||||||
// out a Go object file. The linker does not call this; the linker
|
// out a Go object file. The linker does not call this; the linker
|
||||||
// does not write out object files.
|
// does not write out object files.
|
||||||
func Writeobjdirect(ctxt *Link, b *bio.Writer) {
|
func Writeobjdirect(ctxt *Link, b *bufio.Writer) {
|
||||||
Flushplist(ctxt)
|
Flushplist(ctxt)
|
||||||
WriteObjFile(ctxt, b)
|
WriteObjFile(ctxt, b)
|
||||||
}
|
}
|
||||||
|
|
@ -187,16 +186,16 @@ func (w *objWriter) writeLengths() {
|
||||||
w.writeInt(int64(w.nFile))
|
w.writeInt(int64(w.nFile))
|
||||||
}
|
}
|
||||||
|
|
||||||
func newObjWriter(ctxt *Link, b *bio.Writer) *objWriter {
|
func newObjWriter(ctxt *Link, b *bufio.Writer) *objWriter {
|
||||||
return &objWriter{
|
return &objWriter{
|
||||||
ctxt: ctxt,
|
ctxt: ctxt,
|
||||||
wr: b.Writer,
|
wr: b,
|
||||||
vrefIdx: make(map[string]int),
|
vrefIdx: make(map[string]int),
|
||||||
refIdx: make(map[string]int),
|
refIdx: make(map[string]int),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteObjFile(ctxt *Link, b *bio.Writer) {
|
func WriteObjFile(ctxt *Link, b *bufio.Writer) {
|
||||||
w := newObjWriter(ctxt, b)
|
w := newObjWriter(ctxt, b)
|
||||||
|
|
||||||
// Magic header
|
// Magic header
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue