github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/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  	"flag"
     9  	"fmt"
    10  	"log"
    11  	"os"
    12  
    13  	"cmd/asm/internal/arch"
    14  	"cmd/asm/internal/asm"
    15  	"cmd/asm/internal/flags"
    16  	"cmd/asm/internal/lex"
    17  
    18  	"cmd/internal/obj"
    19  )
    20  
    21  func main() {
    22  	log.SetFlags(0)
    23  	log.SetPrefix("asm: ")
    24  
    25  	GOARCH := obj.Getgoarch()
    26  
    27  	architecture := arch.Set(GOARCH)
    28  	if architecture == nil {
    29  		log.Fatalf("asm: unrecognized architecture %s", GOARCH)
    30  	}
    31  
    32  	flags.Parse()
    33  
    34  	// Create object file, write header.
    35  	fd, err := os.Create(*flags.OutputFile)
    36  	if err != nil {
    37  		log.Fatal(err)
    38  	}
    39  	ctxt := obj.Linknew(architecture.LinkArch)
    40  	if *flags.PrintOut {
    41  		ctxt.Debugasm = 1
    42  	}
    43  	ctxt.LineHist.TrimPathPrefix = *flags.TrimPath
    44  	ctxt.Flag_dynlink = *flags.Dynlink
    45  	if *flags.Shared || *flags.Dynlink {
    46  		ctxt.Flag_shared = 1
    47  	}
    48  	ctxt.Bso = obj.Binitw(os.Stdout)
    49  	defer ctxt.Bso.Flush()
    50  	ctxt.Diag = log.Fatalf
    51  	output := obj.Binitw(fd)
    52  	fmt.Fprintf(output, "go object %s %s %s\n", obj.Getgoos(), obj.Getgoarch(), obj.Getgoversion())
    53  	fmt.Fprintf(output, "!\n")
    54  
    55  	lexer := lex.NewLexer(flag.Arg(0), ctxt)
    56  	parser := asm.NewParser(ctxt, architecture, lexer)
    57  	pList := obj.Linknewplist(ctxt)
    58  	var ok bool
    59  	pList.Firstpc, ok = parser.Parse()
    60  	if !ok {
    61  		log.Printf("asm: assembly of %s failed", flag.Arg(0))
    62  		os.Remove(*flags.OutputFile)
    63  		os.Exit(1)
    64  	}
    65  	obj.Writeobjdirect(ctxt, output)
    66  	output.Flush()
    67  }