...
Run Format

Source file src/cmd/asm/main.go

Documentation: cmd/asm

     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  	"cmd/internal/objabi"
    22  )
    23  
    24  func main() {
    25  	log.SetFlags(0)
    26  	log.SetPrefix("asm: ")
    27  
    28  	GOARCH := objabi.GOARCH
    29  
    30  	architecture := arch.Set(GOARCH)
    31  	if architecture == nil {
    32  		log.Fatalf("unrecognized architecture %s", GOARCH)
    33  	}
    34  
    35  	flags.Parse()
    36  
    37  	ctxt := obj.Linknew(architecture.LinkArch)
    38  	if *flags.PrintOut {
    39  		ctxt.Debugasm = true
    40  	}
    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  	architecture.Init(ctxt)
    47  
    48  	// Create object file, write header.
    49  	out, err := os.Create(*flags.OutputFile)
    50  	if err != nil {
    51  		log.Fatal(err)
    52  	}
    53  	defer bio.MustClose(out)
    54  	buf := bufio.NewWriter(bio.MustWriter(out))
    55  
    56  	fmt.Fprintf(buf, "go object %s %s %s\n", objabi.GOOS, objabi.GOARCH, objabi.Version)
    57  	fmt.Fprintf(buf, "!\n")
    58  
    59  	var ok, diag bool
    60  	var failedFile string
    61  	for _, f := range flag.Args() {
    62  		lexer := lex.NewLexer(f)
    63  		parser := asm.NewParser(ctxt, architecture, lexer)
    64  		ctxt.DiagFunc = func(format string, args ...interface{}) {
    65  			diag = true
    66  			log.Printf(format, args...)
    67  		}
    68  		pList := new(obj.Plist)
    69  		pList.Firstpc, ok = parser.Parse()
    70  		if !ok {
    71  			failedFile = f
    72  			break
    73  		}
    74  		// reports errors to parser.Errorf
    75  		obj.Flushplist(ctxt, pList, nil, "")
    76  	}
    77  	if ok {
    78  		obj.WriteObjFile(ctxt, buf)
    79  	}
    80  	if !ok || diag {
    81  		if failedFile != "" {
    82  			log.Printf("assembly of %s failed", failedFile)
    83  		} else {
    84  			log.Print("assembly failed")
    85  		}
    86  		out.Close()
    87  		os.Remove(*flags.OutputFile)
    88  		os.Exit(1)
    89  	}
    90  	buf.Flush()
    91  }
    92  

View as plain text