|
| 1 | +package compiler |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "go/ast" |
| 8 | + "go/parser" |
| 9 | + "go/token" |
| 10 | + "go/types" |
| 11 | + "io/ioutil" |
| 12 | + "path/filepath" |
| 13 | + "strings" |
| 14 | + "sync" |
| 15 | + "testing" |
| 16 | + |
| 17 | + "github.com/tinygo-org/tinygo/compileopts" |
| 18 | + "github.com/tinygo-org/tinygo/compiler/ircheck" |
| 19 | + "golang.org/x/tools/go/ssa" |
| 20 | + "golang.org/x/tools/go/ssa/ssautil" |
| 21 | + "tinygo.org/x/go-llvm" |
| 22 | +) |
| 23 | + |
| 24 | +var flagUpdate = flag.Bool("update", false, "update all tests") |
| 25 | + |
| 26 | +func TestCompiler(t *testing.T) { |
| 27 | + t.Parallel() |
| 28 | + for _, name := range []string{"basic"} { |
| 29 | + t.Run(name, func(t *testing.T) { |
| 30 | + runCompilerTest(t, name) |
| 31 | + }) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func runCompilerTest(t *testing.T, name string) { |
| 36 | + // Read the AST in memory. |
| 37 | + path := filepath.Join("testdata", name+".go") |
| 38 | + fset := token.NewFileSet() |
| 39 | + f, err := parser.ParseFile(fset, path, nil, parser.ParseComments) |
| 40 | + if err != nil { |
| 41 | + t.Fatal("could not parse Go source file:", err) |
| 42 | + } |
| 43 | + files := []*ast.File{f} |
| 44 | + |
| 45 | + // Create Go SSA from the AST. |
| 46 | + var typecheckErrors []error |
| 47 | + var typecheckErrorsLock sync.Mutex |
| 48 | + typesConfig := types.Config{ |
| 49 | + Error: func(err error) { |
| 50 | + typecheckErrorsLock.Lock() |
| 51 | + defer typecheckErrorsLock.Unlock() |
| 52 | + typecheckErrors = append(typecheckErrors, err) |
| 53 | + }, |
| 54 | + Importer: simpleImporter{}, |
| 55 | + Sizes: types.SizesFor("gccgo", "arm"), |
| 56 | + } |
| 57 | + pkg, _, err := ssautil.BuildPackage(&typesConfig, fset, types.NewPackage("main", ""), files, ssa.SanityCheckFunctions|ssa.BareInits|ssa.GlobalDebug) |
| 58 | + for _, err := range typecheckErrors { |
| 59 | + t.Error(err) |
| 60 | + } |
| 61 | + if err != nil && len(typecheckErrors) == 0 { |
| 62 | + // Only report errors when no type errors are found (an |
| 63 | + // unexpected condition). |
| 64 | + t.Error(err) |
| 65 | + } |
| 66 | + if t.Failed() { |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + // Configure the compiler. |
| 71 | + config := compileopts.Config{ |
| 72 | + Options: &compileopts.Options{}, |
| 73 | + Target: &compileopts.TargetSpec{ |
| 74 | + Triple: "armv7m-none-eabi", |
| 75 | + BuildTags: []string{"cortexm", "baremetal", "linux", "arm"}, |
| 76 | + Scheduler: "tasks", |
| 77 | + }, |
| 78 | + } |
| 79 | + machine, err := NewTargetMachine(&config) |
| 80 | + if err != nil { |
| 81 | + t.Fatal(err) |
| 82 | + } |
| 83 | + c := newCompilerContext("main", machine, &config) |
| 84 | + irbuilder := c.ctx.NewBuilder() |
| 85 | + defer irbuilder.Dispose() |
| 86 | + |
| 87 | + // Create LLVM IR from the Go SSA. |
| 88 | + c.createPackage(pkg, irbuilder) |
| 89 | + |
| 90 | + // Check the IR with the LLVM verifier. |
| 91 | + if err := llvm.VerifyModule(c.mod, llvm.PrintMessageAction); err != nil { |
| 92 | + t.Error("verification error after IR construction") |
| 93 | + } |
| 94 | + |
| 95 | + // Check the IR with our own verifier (which checks for different things). |
| 96 | + errs := ircheck.Module(c.mod) |
| 97 | + for _, err := range errs { |
| 98 | + t.Error(err) |
| 99 | + } |
| 100 | + |
| 101 | + // Check whether the IR matches the expected IR. |
| 102 | + ir := c.mod.String() |
| 103 | + ir = ir[strings.Index(ir, "\ntarget datalayout = ")+1:] |
| 104 | + outfile := filepath.Join("testdata", name+".ll") |
| 105 | + if *flagUpdate { |
| 106 | + err := ioutil.WriteFile(outfile, []byte(ir), 0666) |
| 107 | + if err != nil { |
| 108 | + t.Error("could not read output file:", err) |
| 109 | + } |
| 110 | + } else { |
| 111 | + ir2, err := ioutil.ReadFile(outfile) |
| 112 | + if err != nil { |
| 113 | + t.Fatal("could not read input file:", err) |
| 114 | + } |
| 115 | + ir2 = bytes.Replace(ir2, []byte("\r\n"), []byte("\n"), -1) |
| 116 | + if ir != string(ir2) { |
| 117 | + t.Error("output did not match") |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// simpleImporter implements the types.Importer interface, but only allows |
| 123 | +// importing the unsafe package. |
| 124 | +type simpleImporter struct { |
| 125 | +} |
| 126 | + |
| 127 | +// Import implements the Importer interface. For testing usage only: it only |
| 128 | +// supports importing the unsafe package. |
| 129 | +func (i simpleImporter) Import(path string) (*types.Package, error) { |
| 130 | + switch path { |
| 131 | + case "unsafe": |
| 132 | + return types.Unsafe, nil |
| 133 | + default: |
| 134 | + return nil, fmt.Errorf("importer not implemented for package %s", path) |
| 135 | + } |
| 136 | +} |
0 commit comments