2013-05-17 14:25:48 -06:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
// ssadump: a tool for displaying and interpreting the SSA form of Go programs.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"runtime/pprof"
|
|
|
|
|
2013-05-31 14:14:13 -06:00
|
|
|
"code.google.com/p/go.tools/importer"
|
2013-05-17 14:25:48 -06:00
|
|
|
"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.
|
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
|
|
|
D include debug info for every function.
|
2013-05-17 14:25:48 -06:00
|
|
|
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> ...] [<file.go> ...] [<arg> ...]
|
|
|
|
ssadump [<flag> ...] <import/path> [<arg> ...]
|
|
|
|
Use -help flag to display options.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
% ssadump -run -interp=T hello.go # interpret a program, with tracing
|
|
|
|
% ssadump -build=FPG hello.go # quickly dump SSA form of a single package
|
|
|
|
`
|
|
|
|
|
|
|
|
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
args := flag.Args()
|
|
|
|
|
2013-07-19 15:35:29 -06:00
|
|
|
impctx := importer.Config{Loader: importer.MakeGoBuildLoader(nil)}
|
2013-05-31 14:14:13 -06:00
|
|
|
|
2013-07-31 11:13:05 -06:00
|
|
|
var debugMode bool
|
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':
|
2013-07-31 11:13:05 -06:00
|
|
|
debugMode = true
|
2013-05-17 14:25:48 -06:00
|
|
|
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':
|
2013-05-31 14:14:13 -06:00
|
|
|
impctx.Loader = nil
|
2013-05-17 14:25:48 -06:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2013-05-31 14:14:13 -06:00
|
|
|
// Load, parse and type-check the program.
|
|
|
|
imp := importer.New(&impctx)
|
|
|
|
info, args, err := importer.CreatePackageFromArgs(imp, args)
|
2013-05-17 14:25:48 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
2013-05-31 14:14:13 -06:00
|
|
|
|
2013-06-03 14:46:57 -06:00
|
|
|
// Create and build SSA-form program representation.
|
|
|
|
prog := ssa.NewProgram(imp.Fset, mode)
|
2013-07-18 14:59:06 -06:00
|
|
|
for _, info := range imp.Packages {
|
2013-07-31 11:13:05 -06:00
|
|
|
prog.CreatePackage(info).SetDebugMode(debugMode)
|
2013-07-18 14:59:06 -06:00
|
|
|
}
|
2013-06-03 14:46:57 -06:00
|
|
|
prog.BuildAll()
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-05-31 14:14:13 -06:00
|
|
|
// Run the interpreter.
|
2013-05-17 14:25:48 -06:00
|
|
|
if *runFlag {
|
2013-06-03 14:46:57 -06:00
|
|
|
interp.Interpret(prog.Package(info.Pkg), interpMode, info.Pkg.Path(), args)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
}
|