github.com/robhaswell/grandperspective-scan@v0.1.0/test/go-go1.7.1/src/cmd/asm/main.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"bufio"
     9  	"flag"
    10  	"fmt"
    11  	"log"
    12  	"os"
    13  
    14  	"cmd/asm/internal/arch"
    15  	"cmd/asm/internal/asm"
    16  	"cmd/asm/internal/flags"
    17  	"cmd/asm/internal/lex"
    18  
    19  	"cmd/internal/bio"
    20  	"cmd/internal/obj"
    21  )
    22  
    23  func main() {
    24  	log.SetFlags(0)
    25  	log.SetPrefix("asm: ")
    26  
    27  	GOARCH := obj.Getgoarch()
    28  
    29  	architecture := arch.Set(GOARCH)
    30  	if architecture == nil {
    31  		log.Fatalf("unrecognized architecture %s", GOARCH)
    32  	}
    33  
    34  	flags.Parse()
    35  
    36  	ctxt := obj.Linknew(architecture.LinkArch)
    37  	if *flags.PrintOut {
    38  		ctxt.Debugasm = 1
    39  	}
    40  	ctxt.LineHist.TrimPathPrefix = *flags.TrimPath
    41  	ctxt.Flag_dynlink = *flags.Dynlink
    42  	ctxt.Flag_shared = *flags.Shared || *flags.Dynlink
    43  	ctxt.Bso = bufio.NewWriter(os.Stdout)
    44  	defer ctxt.Bso.Flush()
    45  
    46  	// Create object file, write header.
    47  	out, err := os.Create(*flags.OutputFile)
    48  	if err != nil {
    49  		log.Fatal(err)
    50  	}
    51  	defer bio.MustClose(out)
    52  	buf := bufio.NewWriter(bio.MustWriter(out))
    53  
    54  	fmt.Fprintf(buf, "go object %s %s %s\n", obj.Getgoos(), obj.Getgoarch(), obj.Getgoversion())
    55  	fmt.Fprintf(buf, "!\n")
    56  
    57  	lexer := lex.NewLexer(flag.Arg(0), ctxt)
    58  	parser := asm.NewParser(ctxt, architecture, lexer)
    59  	diag := false
    60  	ctxt.DiagFunc = func(format string, args ...interface{}) {
    61  		diag = true
    62  		log.Printf(format, args...)
    63  	}
    64  	pList := obj.Linknewplist(ctxt)
    65  	var ok bool
    66  	pList.Firstpc, ok = parser.Parse()
    67  	if ok {
    68  		// reports errors to parser.Errorf
    69  		obj.Writeobjdirect(ctxt, buf)
    70  	}
    71  	if !ok || diag {
    72  		log.Printf("assembly of %s failed", flag.Arg(0))
    73  		os.Remove(*flags.OutputFile)
    74  		os.Exit(1)
    75  	}
    76  	buf.Flush()
    77  }