Source file src/cmd/asm/internal/flags/flags.go

     1  // Copyright 2015 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 flags implements top-level flags and the usage message for the assembler.
     6  package flags
     7  
     8  import (
     9  	"cmd/internal/obj"
    10  	"cmd/internal/objabi"
    11  	"flag"
    12  	"fmt"
    13  	"os"
    14  	"path/filepath"
    15  	"strings"
    16  )
    17  
    18  var (
    19  	Debug      = flag.Bool("debug", false, "dump instructions as they are parsed")
    20  	OutputFile = flag.String("o", "", "output file; default foo.o for /a/b/c/foo.s as first argument")
    21  	TrimPath   = flag.String("trimpath", "", "remove prefix from recorded source file paths")
    22  	Shared     = flag.Bool("shared", false, "generate code that can be linked into a shared library")
    23  	Dynlink    = flag.Bool("dynlink", false, "support references to Go symbols defined in other shared libraries")
    24  	Linkshared = flag.Bool("linkshared", false, "generate code that will be linked against Go shared libraries")
    25  	AllErrors  = flag.Bool("e", false, "no limit on number of errors reported")
    26  	SymABIs    = flag.Bool("gensymabis", false, "write symbol ABI information to output file, don't assemble")
    27  	Importpath = flag.String("p", obj.UnlinkablePkg, "set expected package import to path")
    28  	Spectre    = flag.String("spectre", "", "enable spectre mitigations in `list` (all, ret)")
    29  	Std        = flag.Bool("std", false, "building standard library")
    30  )
    31  
    32  var DebugFlags struct {
    33  	CompressInstructions int    `help:"use compressed instructions when possible (if supported by architecture)"`
    34  	MayMoreStack         string `help:"call named function before all stack growth checks"`
    35  	PCTab                string `help:"print named pc-value table\nOne of: pctospadj, pctofile, pctoline, pctoinline, pctopcdata"`
    36  }
    37  
    38  var (
    39  	D        MultiFlag
    40  	I        MultiFlag
    41  	PrintOut int
    42  	DebugV   bool
    43  )
    44  
    45  func init() {
    46  	flag.Var(&D, "D", "predefined symbol with optional simple value -D=identifier=value; can be set multiple times")
    47  	flag.Var(&I, "I", "include directory; can be set multiple times")
    48  	flag.BoolVar(&DebugV, "v", false, "print debug output")
    49  	flag.Var(objabi.NewDebugFlag(&DebugFlags, nil), "d", "enable debugging settings; try -d help")
    50  	objabi.AddVersionFlag() // -V
    51  	objabi.Flagcount("S", "print assembly and machine code", &PrintOut)
    52  
    53  	DebugFlags.CompressInstructions = 1
    54  }
    55  
    56  // MultiFlag allows setting a value multiple times to collect a list, as in -I=dir1 -I=dir2.
    57  type MultiFlag []string
    58  
    59  func (m *MultiFlag) String() string {
    60  	if len(*m) == 0 {
    61  		return ""
    62  	}
    63  	return fmt.Sprint(*m)
    64  }
    65  
    66  func (m *MultiFlag) Set(val string) error {
    67  	(*m) = append(*m, val)
    68  	return nil
    69  }
    70  
    71  func Usage() {
    72  	fmt.Fprintf(os.Stderr, "usage: asm [options] file.s ...\n")
    73  	fmt.Fprintf(os.Stderr, "Flags:\n")
    74  	flag.PrintDefaults()
    75  	os.Exit(2)
    76  }
    77  
    78  func Parse() {
    79  	objabi.Flagparse(Usage)
    80  	if flag.NArg() == 0 {
    81  		flag.Usage()
    82  	}
    83  
    84  	// Flag refinement.
    85  	if *OutputFile == "" {
    86  		if flag.NArg() != 1 {
    87  			flag.Usage()
    88  		}
    89  		input := filepath.Base(flag.Arg(0))
    90  		input = strings.TrimSuffix(input, ".s")
    91  		*OutputFile = fmt.Sprintf("%s.o", input)
    92  	}
    93  }
    94  

View as plain text