github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gotools/go/ssa/example_test.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ssa_test 6 7 import ( 8 "fmt" 9 "os" 10 11 "llvm.org/llgo/third_party/gotools/go/loader" 12 "llvm.org/llgo/third_party/gotools/go/ssa" 13 ) 14 15 // This program demonstrates how to run the SSA builder on a "Hello, 16 // World!" program and shows the printed representation of packages, 17 // functions and instructions. 18 // 19 // Within the function listing, the name of each BasicBlock such as 20 // ".0.entry" is printed left-aligned, followed by the block's 21 // Instructions. 22 // 23 // For each instruction that defines an SSA virtual register 24 // (i.e. implements Value), the type of that value is shown in the 25 // right column. 26 // 27 // Build and run the ssadump.go program if you want a standalone tool 28 // with similar functionality. It is located at 29 // golang.org/x/tools/cmd/ssadump. 30 // 31 func Example() { 32 const hello = ` 33 package main 34 35 import "fmt" 36 37 const message = "Hello, World!" 38 39 func main() { 40 fmt.Println(message) 41 } 42 ` 43 var conf loader.Config 44 45 // Parse the input file. 46 file, err := conf.ParseFile("hello.go", hello) 47 if err != nil { 48 fmt.Print(err) // parse error 49 return 50 } 51 52 // Create single-file main package. 53 conf.CreateFromFiles("main", file) 54 55 // Load the main package and its dependencies. 56 iprog, err := conf.Load() 57 if err != nil { 58 fmt.Print(err) // type error in some package 59 return 60 } 61 62 // Create SSA-form program representation. 63 prog := ssa.Create(iprog, ssa.SanityCheckFunctions) 64 mainPkg := prog.Package(iprog.Created[0].Pkg) 65 66 // Print out the package. 67 mainPkg.WriteTo(os.Stdout) 68 69 // Build SSA code for bodies of functions in mainPkg. 70 mainPkg.Build() 71 72 // Print out the package-level functions. 73 mainPkg.Func("init").WriteTo(os.Stdout) 74 mainPkg.Func("main").WriteTo(os.Stdout) 75 76 // Output: 77 // 78 // package main: 79 // func init func() 80 // var init$guard bool 81 // func main func() 82 // const message message = "Hello, World!":untyped string 83 // 84 // # Name: main.init 85 // # Package: main 86 // # Synthetic: package initializer 87 // func init(): 88 // 0: entry P:0 S:2 89 // t0 = *init$guard bool 90 // if t0 goto 2 else 1 91 // 1: init.start P:1 S:1 92 // *init$guard = true:bool 93 // t1 = fmt.init() () 94 // jump 2 95 // 2: init.done P:2 S:0 96 // return 97 // 98 // # Name: main.main 99 // # Package: main 100 // # Location: hello.go:8:6 101 // func main(): 102 // 0: entry P:0 S:0 103 // t0 = new [1]interface{} (varargs) *[1]interface{} 104 // t1 = &t0[0:int] *interface{} 105 // t2 = make interface{} <- string ("Hello, World!":string) interface{} 106 // *t1 = t2 107 // t3 = slice t0[:] []interface{} 108 // t4 = fmt.Println(t3...) (n int, err error) 109 // return 110 }