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 = 1
    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  	if !*flags.SymABIs {
    57  		fmt.Fprintf(buf, "go object %s %s %s\n", objabi.GOOS, objabi.GOARCH, objabi.Version)
    58  		fmt.Fprintf(buf, "!\n")
    59  	}
    60  
    61  	var ok, diag bool
    62  	var failedFile string
    63  	for _, f := range flag.Args() {
    64  		lexer := lex.NewLexer(f)
    65  		parser := asm.NewParser(ctxt, architecture, lexer)
    66  		ctxt.DiagFunc = func(format string, args ...interface{}) {
    67  			diag = true
    68  			log.Printf(format, args...)
    69  		}
    70  		if *flags.SymABIs {
    71  			ok = parser.ParseSymABIs(buf)
    72  		} else {
    73  			pList := new(obj.Plist)
    74  			pList.Firstpc, ok = parser.Parse()
    75  			// reports errors to parser.Errorf
    76  			if ok {
    77  				obj.Flushplist(ctxt, pList, nil, "")
    78  			}
    79  		}
    80  		if !ok {
    81  			failedFile = f
    82  			break
    83  		}
    84  	}
    85  	if ok && !*flags.SymABIs {
    86  		obj.WriteObjFile(ctxt, buf, "")
    87  	}
    88  	if !ok || diag {
    89  		if failedFile != "" {
    90  			log.Printf("assembly of %s failed", failedFile)
    91  		} else {
    92  			log.Print("assembly failed")
    93  		}
    94  		out.Close()
    95  		os.Remove(*flags.OutputFile)
    96  		os.Exit(1)
    97  	}
    98  	buf.Flush()
    99  }
   100  

View as plain text