mirror of
https://github.com/golang/go
synced 2024-11-18 15:04:44 -07:00
e8afbfad8c
The Importer type has been replaced with Config and Program. Clients populate a Config, directly or more usually via convenience functions. They then call its Load() method to do all of the typechecking and transitive-closure computation. ssa.NewProgram and ssa.CreatePackages have been fused into ssa.Create, which now cannot fail, since (*Config).Load() reports all type errors. Also: - The addition of an ssa.GlobalDebug builder mode flag eliminates a loop-over-packages repeated in many clients. - PackageInfo.Err flag unexported. Clients never see bad infos now. - cmd/ssadump: now only looks for func "main" in package "main". - importsOf deleted, was dead code. STILL TODO: - ParseFile seems like API creep (though it's convenient) and CreateFromFiles is dangerous (w.r.t. FileSet identity). Need to think more... - the need for clients to rely on elementwise correspondence of Config.CreatePkgs and Program.Created is a little sad. - The command-line interface has not changed. That will happen in a follow-up. r recommends using a repeated flag: -package p -package q ... R=gri CC=axwalk, frederik.zipp, golang-codereviews https://golang.org/cl/49530047
192 lines
4.9 KiB
Go
192 lines
4.9 KiB
Go
// Copyright 2013 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// ssadump: a tool for displaying and interpreting the SSA form of Go programs.
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"go/build"
|
|
"log"
|
|
"os"
|
|
"runtime"
|
|
"runtime/pprof"
|
|
|
|
"code.google.com/p/go.tools/go/types"
|
|
"code.google.com/p/go.tools/importer"
|
|
"code.google.com/p/go.tools/ssa"
|
|
"code.google.com/p/go.tools/ssa/interp"
|
|
)
|
|
|
|
var buildFlag = flag.String("build", "", `Options controlling the SSA builder.
|
|
The value is a sequence of zero or more of these letters:
|
|
C perform sanity [C]hecking of the SSA form.
|
|
D include [D]ebug info for every function.
|
|
P log [P]ackage inventory.
|
|
F log [F]unction SSA code.
|
|
S log [S]ource locations as SSA builder progresses.
|
|
G use binary object files from gc to provide imports (no code).
|
|
L build distinct packages seria[L]ly instead of in parallel.
|
|
N build [N]aive SSA form: don't replace local loads/stores with registers.
|
|
`)
|
|
|
|
var runFlag = flag.Bool("run", false, "Invokes the SSA interpreter on the program.")
|
|
|
|
var interpFlag = flag.String("interp", "", `Options controlling the SSA test interpreter.
|
|
The value is a sequence of zero or more more of these letters:
|
|
R disable [R]ecover() from panic; show interpreter crash instead.
|
|
T [T]race execution of the program. Best for single-threaded programs!
|
|
`)
|
|
|
|
const usage = `SSA builder and interpreter.
|
|
Usage: ssadump [<flag> ...] <args> ...
|
|
Use -help flag to display options.
|
|
|
|
Examples:
|
|
% ssadump -build=FPG hello.go # quickly dump SSA form of a single package
|
|
% ssadump -run -interp=T hello.go # interpret a program, with tracing
|
|
% ssadump -run unicode -- -test.v # interpret the unicode package's tests, verbosely
|
|
` + importer.FromArgsUsage +
|
|
`
|
|
When -run is specified, ssadump will find the first package that
|
|
defines a main function and run it in the interpreter.
|
|
If none is found, the tests of each package will be run instead.
|
|
`
|
|
|
|
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
|
|
|
func init() {
|
|
// If $GOMAXPROCS isn't set, use the full capacity of the machine.
|
|
// For small machines, use at least 4 threads.
|
|
if os.Getenv("GOMAXPROCS") == "" {
|
|
n := runtime.NumCPU()
|
|
if n < 4 {
|
|
n = 4
|
|
}
|
|
runtime.GOMAXPROCS(n)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
args := flag.Args()
|
|
|
|
conf := importer.Config{
|
|
Build: &build.Default,
|
|
SourceImports: true,
|
|
}
|
|
// TODO(adonovan): make go/types choose its default Sizes from
|
|
// build.Default or a specified *build.Context.
|
|
var wordSize int64 = 8
|
|
switch conf.Build.GOARCH {
|
|
case "386", "arm":
|
|
wordSize = 4
|
|
}
|
|
conf.TypeChecker.Sizes = &types.StdSizes{
|
|
MaxAlign: 8,
|
|
WordSize: wordSize,
|
|
}
|
|
|
|
var mode ssa.BuilderMode
|
|
for _, c := range *buildFlag {
|
|
switch c {
|
|
case 'D':
|
|
mode |= ssa.GlobalDebug
|
|
case 'P':
|
|
mode |= ssa.LogPackages | ssa.BuildSerially
|
|
case 'F':
|
|
mode |= ssa.LogFunctions | ssa.BuildSerially
|
|
case 'S':
|
|
mode |= ssa.LogSource | ssa.BuildSerially
|
|
case 'C':
|
|
mode |= ssa.SanityCheckFunctions
|
|
case 'N':
|
|
mode |= ssa.NaiveForm
|
|
case 'G':
|
|
conf.SourceImports = false
|
|
case 'L':
|
|
mode |= ssa.BuildSerially
|
|
default:
|
|
log.Fatalf("Unknown -build option: '%c'.", c)
|
|
}
|
|
}
|
|
|
|
var interpMode interp.Mode
|
|
for _, c := range *interpFlag {
|
|
switch c {
|
|
case 'T':
|
|
interpMode |= interp.EnableTracing
|
|
case 'R':
|
|
interpMode |= interp.DisableRecover
|
|
default:
|
|
log.Fatalf("Unknown -interp option: '%c'.", c)
|
|
}
|
|
}
|
|
|
|
if len(args) == 0 {
|
|
fmt.Fprint(os.Stderr, usage)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Profiling support.
|
|
if *cpuprofile != "" {
|
|
f, err := os.Create(*cpuprofile)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
pprof.StartCPUProfile(f)
|
|
defer pprof.StopCPUProfile()
|
|
}
|
|
|
|
// Use the initial packages from the command line.
|
|
args, err := conf.FromArgs(args)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// The interpreter needs the runtime package.
|
|
if *runFlag {
|
|
conf.Import("runtime")
|
|
}
|
|
|
|
// Load, parse and type-check the whole program.
|
|
iprog, err := conf.Load()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Create and build SSA-form program representation.
|
|
prog := ssa.Create(iprog, mode)
|
|
prog.BuildAll()
|
|
|
|
// Run the interpreter.
|
|
if *runFlag {
|
|
// If a package named "main" defines func main, run that.
|
|
// Otherwise run all packages' tests.
|
|
var main *ssa.Package
|
|
pkgs := prog.AllPackages()
|
|
for _, pkg := range pkgs {
|
|
if pkg.Object.Name() == "main" && pkg.Func("main") != nil {
|
|
main = pkg
|
|
break
|
|
}
|
|
}
|
|
if main == nil && len(pkgs) > 0 {
|
|
// TODO(adonovan): only run tests if -test flag specified.
|
|
main = prog.CreateTestMainPackage(pkgs...)
|
|
}
|
|
if main == nil {
|
|
log.Fatal("No main package and no tests")
|
|
}
|
|
|
|
if runtime.GOARCH != conf.Build.GOARCH {
|
|
log.Fatalf("Cross-interpretation is not yet supported (target has GOARCH %s, interpreter has %s).",
|
|
conf.Build.GOARCH, runtime.GOARCH)
|
|
}
|
|
|
|
interp.Interpret(main, interpMode, conf.TypeChecker.Sizes, main.Object.Path(), args)
|
|
}
|
|
}
|