github.com/bir3/gocompiler@v0.3.205/src/cmd/asm/internal/flags/flags.go (about)

     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  	"github.com/bir3/gocompiler/src/cmd/internal/obj"
    10  	"github.com/bir3/gocompiler/src/cmd/asm/flag_objabi"
    11  	"github.com/bir3/gocompiler/src/cmd/asm/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  	CompilingRuntime = flag.Bool("compiling-runtime", false, "source to be compiled is part of the Go runtime")
    30  )
    31  
    32  var DebugFlags struct {
    33  	MayMoreStack string `help:"call named function before all stack growth checks"`
    34  	PCTab        string `help:"print named pc-value table\nOne of: pctospadj, pctofile, pctoline, pctoinline, pctopcdata"`
    35  }
    36  
    37  var (
    38  	D        MultiFlag
    39  	I        MultiFlag
    40  	PrintOut int
    41  	DebugV   bool
    42  )
    43  
    44  func init() {
    45  	flag.Var(&D, "D", "predefined symbol with optional simple value -D=identifier=value; can be set multiple times")
    46  	flag.Var(&I, "I", "include directory; can be set multiple times")
    47  	flag.BoolVar(&DebugV, "v", false, "print debug output")
    48  	flag.Var(flag_objabi.NewDebugFlag(&DebugFlags, nil), "d", "enable debugging settings; try -d help")
    49  	flag_objabi.AddVersionFlag() // -V
    50  	flag_objabi.Flagcount("S", "print assembly and machine code", &PrintOut)
    51  }
    52  
    53  // MultiFlag allows setting a value multiple times to collect a list, as in -I=dir1 -I=dir2.
    54  type MultiFlag []string
    55  
    56  func (m *MultiFlag) String() string {
    57  	if len(*m) == 0 {
    58  		return ""
    59  	}
    60  	return fmt.Sprint(*m)
    61  }
    62  
    63  func (m *MultiFlag) Set(val string) error {
    64  	(*m) = append(*m, val)
    65  	return nil
    66  }
    67  
    68  func Usage() {
    69  	fmt.Fprintf(os.Stderr, "usage: asm [options] file.s ...\n")
    70  	fmt.Fprintf(os.Stderr, "Flags:\n")
    71  	flag.PrintDefaults()
    72  	os.Exit(2)
    73  }
    74  
    75  func Parse() {
    76  	flag_objabi.Flagparse(Usage)
    77  	if flag.NArg() == 0 {
    78  		flag.Usage()
    79  	}
    80  
    81  	// Flag refinement.
    82  	if *OutputFile == "" {
    83  		if flag.NArg() != 1 {
    84  			flag.Usage()
    85  		}
    86  		input := filepath.Base(flag.Arg(0))
    87  		input = strings.TrimSuffix(input, ".s")
    88  		*OutputFile = fmt.Sprintf("%s.o", input)
    89  	}
    90  }