forked from dominikh/go-tools
/
main.go
122 lines (105 loc) · 3.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 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.
// irdump: a tool for displaying the IR form of Go programs.
package main
import (
"flag"
"fmt"
"go/build"
"os"
"runtime/pprof"
"github.com/Abirdcfly/go-tools/go/ir"
"github.com/Abirdcfly/go-tools/go/ir/irutil"
"golang.org/x/tools/go/buildutil"
"golang.org/x/tools/go/packages"
)
// flags
var (
mode = ir.BuilderMode(ir.PrintPackages | ir.PrintFunctions)
testFlag = flag.Bool("test", false, "include implicit test packages and executables")
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
dot bool
html string
)
func init() {
flag.Var(&mode, "build", ir.BuilderModeDoc)
flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc)
flag.BoolVar(&dot, "dot", false, "Print Graphviz dot of CFG")
flag.StringVar(&html, "html", "", "Print HTML for 'function'")
}
const usage = `IR builder.
Usage: irdump [-build=[DBCSNFL]] [-test] [-arg=...] package...
Use -help flag to display options.
Examples:
% irdump -build=F hello.go # dump IR form of a single package
% irdump -build=F -test fmt # dump IR form of a package and its tests
`
func main() {
if err := doMain(); err != nil {
fmt.Fprintf(os.Stderr, "irdump: %s\n", err)
os.Exit(1)
}
}
func doMain() error {
flag.Parse()
if len(flag.Args()) == 0 {
fmt.Fprint(os.Stderr, usage)
os.Exit(1)
}
cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedTypesSizes | packages.NeedSyntax | packages.NeedTypesInfo,
Tests: *testFlag,
}
// Profiling support.
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// Load, parse and type-check the initial packages.
initial, err := packages.Load(cfg, flag.Args()...)
if err != nil {
return err
}
if len(initial) == 0 {
return fmt.Errorf("no packages")
}
if packages.PrintErrors(initial) > 0 {
return fmt.Errorf("packages contain errors")
}
// Create IR-form program representation.
_, pkgs := irutil.Packages(initial, mode, &irutil.Options{PrintFunc: html})
for i, p := range pkgs {
if p == nil {
return fmt.Errorf("cannot build IR for package %s", initial[i])
}
}
// Build and display only the initial packages
// (and synthetic wrappers).
for _, p := range pkgs {
p.Build()
}
if dot {
for _, p := range pkgs {
for _, m := range p.Members {
if fn, ok := m.(*ir.Function); ok {
fmt.Println("digraph{")
fmt.Printf("label = %q;\n", fn.Name())
for _, b := range fn.Blocks {
fmt.Printf("n%d [label=\"%d: %s\"]\n", b.Index, b.Index, b.Comment)
for _, succ := range b.Succs {
fmt.Printf("n%d -> n%d\n", b.Index, succ.Index)
}
}
fmt.Println("}")
}
}
}
}
return nil
}