forked from pgrunde/ssa-play
/
ssa.go
47 lines (38 loc) · 1019 Bytes
/
ssa.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
package main
import (
"fmt"
"go/build"
"os"
"path/filepath"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
)
func main() {
// Loader is a tool for opening Go files, it loads from a Config type
conf := loader.Config{
Build: &build.Default,
}
path, _ := filepath.Abs("looper")
file, err := conf.ParseFile(path + "/" + "looper.go", nil)
if err != nil {
fmt.Println(err)
return
}
// Creation of a single file main pacakge
conf.CreateFromFiles("looper", file)
conf.Import("runtime")
p, err := conf.Load()
if err != nil {
fmt.Println(err)
return
}
// Finally, create SSA representation from the package we've loaded
program := ssautil.CreateProgram(p, ssa.SanityCheckFunctions)
looperPkg := program.Package(p.Created[0].Pkg)
fmt.Println("RIGHT IN THE SINGLE STATIC ASSIGNMENT FORM:")
looperPkg.WriteTo(os.Stdout)
fmt.Println("LOOK AT THIS HERE LOOPER FUNC:")
looperFunc := looperPkg.Func("Looper")
looperFunc.WriteTo(os.Stdout)
}