2013-08-27 16:49:13 -06:00
|
|
|
// 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.
|
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
// ssadump: a tool for displaying and interpreting the SSA form of Go programs.
|
2013-08-29 19:34:36 -06:00
|
|
|
package main
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2013-09-04 11:15:49 -06:00
|
|
|
"go/build"
|
2013-05-17 14:25:48 -06:00
|
|
|
"os"
|
2013-08-29 19:34:36 -06:00
|
|
|
"runtime"
|
2013-05-17 14:25:48 -06:00
|
|
|
"runtime/pprof"
|
|
|
|
|
2014-01-16 07:33:58 -07:00
|
|
|
"code.google.com/p/go.tools/go/loader"
|
|
|
|
"code.google.com/p/go.tools/go/ssa"
|
|
|
|
"code.google.com/p/go.tools/go/ssa/interp"
|
2014-01-08 12:46:17 -07:00
|
|
|
"code.google.com/p/go.tools/go/types"
|
2013-05-17 14:25:48 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
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.
|
2013-08-19 10:50:40 -06:00
|
|
|
D include [D]ebug info for every function.
|
2014-08-01 12:44:37 -06:00
|
|
|
P print [P]ackage inventory.
|
|
|
|
F print [F]unction SSA code.
|
2013-05-17 14:25:48 -06:00
|
|
|
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.
|
2014-06-16 19:34:51 -06:00
|
|
|
I build bare [I]nit functions: no init guards or calls to dependent inits.
|
2013-05-17 14:25:48 -06:00
|
|
|
`)
|
|
|
|
|
2014-02-11 14:52:16 -07:00
|
|
|
var testFlag = flag.Bool("test", false, "Loads test code (*_test.go) for imported packages.")
|
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
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.
|
2013-10-23 16:07:53 -06:00
|
|
|
Usage: ssadump [<flag> ...] <args> ...
|
2013-05-17 14:25:48 -06:00
|
|
|
Use -help flag to display options.
|
|
|
|
|
|
|
|
Examples:
|
2014-02-11 14:52:16 -07:00
|
|
|
% 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 -test unicode -- -test.v # interpret the unicode package's tests, verbosely
|
2014-01-16 07:33:58 -07:00
|
|
|
` + loader.FromArgsUsage +
|
2013-10-23 16:07:53 -06:00
|
|
|
`
|
2014-02-11 14:52:16 -07:00
|
|
|
When -run is specified, ssadump will run the program.
|
|
|
|
The entry point depends on the -test flag:
|
|
|
|
if clear, it runs the first package named main.
|
|
|
|
if set, it runs the tests of each package.
|
2013-05-17 14:25:48 -06:00
|
|
|
`
|
|
|
|
|
|
|
|
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
|
|
|
|
2013-08-29 19:34:36 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
func main() {
|
2014-02-11 14:52:16 -07:00
|
|
|
if err := doMain(); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "ssadump: %s.\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doMain() error {
|
2013-05-17 14:25:48 -06:00
|
|
|
flag.Parse()
|
|
|
|
args := flag.Args()
|
|
|
|
|
2014-01-16 07:33:58 -07:00
|
|
|
conf := loader.Config{
|
2014-01-09 12:11:54 -07:00
|
|
|
Build: &build.Default,
|
|
|
|
SourceImports: true,
|
|
|
|
}
|
2014-01-08 12:46:17 -07:00
|
|
|
// TODO(adonovan): make go/types choose its default Sizes from
|
|
|
|
// build.Default or a specified *build.Context.
|
|
|
|
var wordSize int64 = 8
|
2014-01-15 19:37:55 -07:00
|
|
|
switch conf.Build.GOARCH {
|
2014-01-08 12:46:17 -07:00
|
|
|
case "386", "arm":
|
|
|
|
wordSize = 4
|
|
|
|
}
|
2014-01-15 19:37:55 -07:00
|
|
|
conf.TypeChecker.Sizes = &types.StdSizes{
|
2014-01-08 12:46:17 -07:00
|
|
|
MaxAlign: 8,
|
|
|
|
WordSize: wordSize,
|
|
|
|
}
|
2013-05-31 14:14:13 -06:00
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
var mode ssa.BuilderMode
|
|
|
|
for _, c := range *buildFlag {
|
|
|
|
switch c {
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
case 'D':
|
2014-01-15 19:37:55 -07:00
|
|
|
mode |= ssa.GlobalDebug
|
2013-05-17 14:25:48 -06:00
|
|
|
case 'P':
|
2014-08-01 12:44:37 -06:00
|
|
|
mode |= ssa.PrintPackages
|
2013-05-17 14:25:48 -06:00
|
|
|
case 'F':
|
2014-08-01 12:44:37 -06:00
|
|
|
mode |= ssa.PrintFunctions
|
2013-05-17 14:25:48 -06:00
|
|
|
case 'S':
|
|
|
|
mode |= ssa.LogSource | ssa.BuildSerially
|
|
|
|
case 'C':
|
|
|
|
mode |= ssa.SanityCheckFunctions
|
|
|
|
case 'N':
|
|
|
|
mode |= ssa.NaiveForm
|
|
|
|
case 'G':
|
2014-01-15 19:37:55 -07:00
|
|
|
conf.SourceImports = false
|
2013-05-17 14:25:48 -06:00
|
|
|
case 'L':
|
|
|
|
mode |= ssa.BuildSerially
|
2014-06-16 19:34:51 -06:00
|
|
|
case 'I':
|
|
|
|
mode |= ssa.BareInits
|
2013-05-17 14:25:48 -06:00
|
|
|
default:
|
2014-02-11 14:52:16 -07:00
|
|
|
return fmt.Errorf("unknown -build option: '%c'", c)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var interpMode interp.Mode
|
|
|
|
for _, c := range *interpFlag {
|
|
|
|
switch c {
|
|
|
|
case 'T':
|
|
|
|
interpMode |= interp.EnableTracing
|
|
|
|
case 'R':
|
|
|
|
interpMode |= interp.DisableRecover
|
|
|
|
default:
|
2014-04-09 16:00:57 -06:00
|
|
|
return fmt.Errorf("unknown -interp option: '%c'", c)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
fmt.Fprint(os.Stderr, usage)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Profiling support.
|
|
|
|
if *cpuprofile != "" {
|
|
|
|
f, err := os.Create(*cpuprofile)
|
|
|
|
if err != nil {
|
2014-02-11 14:52:16 -07:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
|
2014-01-15 19:37:55 -07:00
|
|
|
// Use the initial packages from the command line.
|
2014-02-11 14:52:16 -07:00
|
|
|
args, err := conf.FromArgs(args, *testFlag)
|
2013-05-17 14:25:48 -06:00
|
|
|
if err != nil {
|
2014-02-11 14:52:16 -07:00
|
|
|
return err
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2013-05-31 14:14:13 -06:00
|
|
|
|
go.tools/ssa: implement correct control flow for recovered panic.
A function such as this:
func one() (x int) {
defer func() { recover() }()
x = 1
panic("return")
}
that combines named return parameters (NRPs) with deferred calls
that call recover, may return non-zero values despite the
fact it doesn't even contain a return statement. (!)
This requires a change to the SSA API: all functions'
control-flow graphs now have a second entry point, called
Recover, which is the block at which control flow resumes
after a recovered panic. The Recover block simply loads the
NRPs and returns them.
As an optimization, most functions don't need a Recover block,
so it is omitted. In fact it is only needed for functions that
have NRPs and defer a call to another function that _may_ call
recover.
Dataflow analysis of SSA now requires extra work, since every
may-panic instruction has an implicit control-flow edge to
the Recover block. The only dataflow analysis so far implemented
is SSA renaming, for which we make the following simplifying
assumption: the Recover block only loads the NRPs and returns.
This means we don't really need to analyze it, we can just
skip the "lifting" of such NRPs. We also special-case the Recover
block in the dominance computation.
Rejected alternative approaches:
- Specifying a Recover block for every defer instruction (like a
traditional exception handler).
This seemed like excessive generality, since Go programs
only need the same degenerate form of Recover block.
- Adding an instruction to set the Recover block immediately
after the named return values are set up, so that dominance
can be computed without special-casing.
This didn't seem worth the effort.
Interpreter:
- This CL completely reimplements the panic/recover/
defer logic in the interpreter. It's clearer and simpler
and closer to the model in the spec.
- Some runtime panic messages have been changed to be closer
to gc's, since tests depend on it.
- The interpreter now requires that the runtime.runtimeError
type be part of the SSA program. This requires that clients
import this package prior to invoking the interpreter.
This in turn requires (Importer).ImportPackage(path string),
which this CL adds.
- All $GOROOT/test/recover{,1,2,3}.go tests are now passing.
NB, the bug described in coverage.go (defer/recover in a concatenated
init function) remains. Will be fixed in a follow-up.
Fixes golang/go#6381
R=gri
CC=crawshaw, golang-dev
https://golang.org/cl/13844043
2013-10-14 13:38:56 -06:00
|
|
|
// The interpreter needs the runtime package.
|
|
|
|
if *runFlag {
|
2014-01-15 19:37:55 -07:00
|
|
|
conf.Import("runtime")
|
go.tools/ssa: implement correct control flow for recovered panic.
A function such as this:
func one() (x int) {
defer func() { recover() }()
x = 1
panic("return")
}
that combines named return parameters (NRPs) with deferred calls
that call recover, may return non-zero values despite the
fact it doesn't even contain a return statement. (!)
This requires a change to the SSA API: all functions'
control-flow graphs now have a second entry point, called
Recover, which is the block at which control flow resumes
after a recovered panic. The Recover block simply loads the
NRPs and returns them.
As an optimization, most functions don't need a Recover block,
so it is omitted. In fact it is only needed for functions that
have NRPs and defer a call to another function that _may_ call
recover.
Dataflow analysis of SSA now requires extra work, since every
may-panic instruction has an implicit control-flow edge to
the Recover block. The only dataflow analysis so far implemented
is SSA renaming, for which we make the following simplifying
assumption: the Recover block only loads the NRPs and returns.
This means we don't really need to analyze it, we can just
skip the "lifting" of such NRPs. We also special-case the Recover
block in the dominance computation.
Rejected alternative approaches:
- Specifying a Recover block for every defer instruction (like a
traditional exception handler).
This seemed like excessive generality, since Go programs
only need the same degenerate form of Recover block.
- Adding an instruction to set the Recover block immediately
after the named return values are set up, so that dominance
can be computed without special-casing.
This didn't seem worth the effort.
Interpreter:
- This CL completely reimplements the panic/recover/
defer logic in the interpreter. It's clearer and simpler
and closer to the model in the spec.
- Some runtime panic messages have been changed to be closer
to gc's, since tests depend on it.
- The interpreter now requires that the runtime.runtimeError
type be part of the SSA program. This requires that clients
import this package prior to invoking the interpreter.
This in turn requires (Importer).ImportPackage(path string),
which this CL adds.
- All $GOROOT/test/recover{,1,2,3}.go tests are now passing.
NB, the bug described in coverage.go (defer/recover in a concatenated
init function) remains. Will be fixed in a follow-up.
Fixes golang/go#6381
R=gri
CC=crawshaw, golang-dev
https://golang.org/cl/13844043
2013-10-14 13:38:56 -06:00
|
|
|
}
|
|
|
|
|
2014-01-15 19:37:55 -07:00
|
|
|
// Load, parse and type-check the whole program.
|
|
|
|
iprog, err := conf.Load()
|
|
|
|
if err != nil {
|
2014-02-11 14:52:16 -07:00
|
|
|
return err
|
2013-09-06 16:13:57 -06:00
|
|
|
}
|
2013-10-23 16:07:53 -06:00
|
|
|
|
2014-01-15 19:37:55 -07:00
|
|
|
// Create and build SSA-form program representation.
|
|
|
|
prog := ssa.Create(iprog, mode)
|
2013-06-03 14:46:57 -06:00
|
|
|
prog.BuildAll()
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-10-23 16:07:53 -06:00
|
|
|
// Run the interpreter.
|
2013-05-17 14:25:48 -06:00
|
|
|
if *runFlag {
|
2013-09-06 16:13:57 -06:00
|
|
|
var main *ssa.Package
|
2014-01-15 19:37:55 -07:00
|
|
|
pkgs := prog.AllPackages()
|
2014-02-11 14:52:16 -07:00
|
|
|
if *testFlag {
|
|
|
|
// If -test, run all packages' tests.
|
|
|
|
if len(pkgs) > 0 {
|
|
|
|
main = prog.CreateTestMainPackage(pkgs...)
|
|
|
|
}
|
|
|
|
if main == nil {
|
|
|
|
return fmt.Errorf("no tests")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise, run main.main.
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
if pkg.Object.Name() == "main" {
|
|
|
|
main = pkg
|
|
|
|
if main.Func("main") == nil {
|
|
|
|
return fmt.Errorf("no func main() in main package")
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if main == nil {
|
|
|
|
return fmt.Errorf("no main package")
|
2013-09-06 16:13:57 -06:00
|
|
|
}
|
|
|
|
}
|
2014-01-08 12:46:17 -07:00
|
|
|
|
2014-02-11 14:52:16 -07:00
|
|
|
if runtime.GOARCH != build.Default.GOARCH {
|
|
|
|
return fmt.Errorf("cross-interpretation is not yet supported (target has GOARCH %s, interpreter has %s)",
|
|
|
|
build.Default.GOARCH, runtime.GOARCH)
|
2014-01-08 12:46:17 -07:00
|
|
|
}
|
|
|
|
|
2014-01-15 19:37:55 -07:00
|
|
|
interp.Interpret(main, interpMode, conf.TypeChecker.Sizes, main.Object.Path(), args)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2014-02-11 14:52:16 -07:00
|
|
|
return nil
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|