Files
llgo/cl/compile_test.go

169 lines
3.8 KiB
Go
Raw Normal View History

2024-04-20 15:58:34 +08:00
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cl
import (
"go/ast"
"go/parser"
"go/token"
"go/types"
2024-04-20 23:15:10 +08:00
"log"
2024-04-20 17:31:49 +08:00
"os"
2024-04-20 23:15:10 +08:00
"path"
"strings"
2024-04-20 15:58:34 +08:00
"testing"
"github.com/goplus/gogen/packages"
2024-04-20 15:58:34 +08:00
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
llssa "github.com/goplus/llgo/ssa"
2024-04-20 15:58:34 +08:00
)
2024-04-20 23:15:10 +08:00
func TestFromTestdata(t *testing.T) {
2024-04-22 20:23:01 +08:00
testFromDir(t, "", "./_testdata")
2024-04-21 15:12:57 +08:00
}
func init() {
2024-04-21 16:04:05 +08:00
SetDebug(DbgFlagAll)
2024-04-21 15:12:57 +08:00
llssa.Initialize(llssa.InitAll)
llssa.SetDebug(llssa.DbgFlagAll)
2024-04-20 23:15:10 +08:00
}
func testFromDir(t *testing.T, sel, relDir string) {
dir, err := os.Getwd()
if err != nil {
t.Fatal("Getwd failed:", err)
}
dir = path.Join(dir, relDir)
fis, err := os.ReadDir(dir)
if err != nil {
t.Fatal("ReadDir failed:", err)
}
for _, fi := range fis {
name := fi.Name()
if !fi.IsDir() || strings.HasPrefix(name, "_") {
continue
}
t.Run(name, func(t *testing.T) {
testFrom(t, dir+"/"+name, sel)
})
}
}
func testFrom(t *testing.T, pkgDir, sel string) {
if sel != "" && !strings.Contains(pkgDir, sel) {
return
}
log.Println("Parsing", pkgDir)
in := pkgDir + "/in.go"
out := pkgDir + "/out.ll"
expected, err := os.ReadFile(out)
if err != nil {
t.Fatal("ReadFile failed:", err)
}
testCompileEx(t, nil, in, string(expected))
}
func testCompileEx(t *testing.T, src any, fname, expected string) {
t.Helper()
2024-04-20 15:58:34 +08:00
fset := token.NewFileSet()
2024-04-20 23:15:10 +08:00
f, err := parser.ParseFile(fset, fname, src, parser.ParseComments)
2024-04-20 15:58:34 +08:00
if err != nil {
t.Fatal("ParseFile failed:", err)
}
files := []*ast.File{f}
2024-04-20 23:15:10 +08:00
name := f.Name.Name
pkg := types.NewPackage(name, name)
imp := packages.NewImporter(fset)
2024-04-20 15:58:34 +08:00
foo, _, err := ssautil.BuildPackage(
&types.Config{Importer: imp}, fset, pkg, files, ssa.SanityCheckFunctions)
2024-04-20 15:58:34 +08:00
if err != nil {
t.Fatal("BuildPackage failed:", err)
}
2024-04-20 17:31:49 +08:00
foo.WriteTo(os.Stderr)
2024-04-20 15:58:34 +08:00
prog := llssa.NewProgram(nil)
2024-04-22 20:23:01 +08:00
ret, err := NewPackage(prog, foo, files)
2024-04-20 15:58:34 +08:00
if err != nil {
t.Fatal("cl.NewPackage failed:", err)
}
if v := ret.String(); v != expected {
t.Fatalf("\n==> got:\n%s\n==> expected:\n%s\n", v, expected)
}
}
2024-04-20 23:15:10 +08:00
func testCompile(t *testing.T, src, expected string) {
t.Helper()
testCompileEx(t, src, "foo.go", expected)
}
2024-04-20 15:58:34 +08:00
func TestVar(t *testing.T) {
testCompile(t, `package foo
var a int
`, `; ModuleID = 'foo'
source_filename = "foo"
2024-04-21 00:22:39 +08:00
@"init$guard" = global ptr null
@a = global ptr null
2024-04-20 22:05:45 +08:00
2024-04-22 15:09:08 +08:00
define void @foo.init() {
2024-04-20 22:05:45 +08:00
_llgo_0:
%0 = load i1, ptr @"init$guard", align 1
2024-04-20 22:16:28 +08:00
br i1 %0, label %_llgo_2, label %_llgo_1
2024-04-20 22:05:45 +08:00
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"init$guard", align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
2024-04-20 15:58:34 +08:00
`)
}
2024-04-20 22:30:38 +08:00
func TestBasicFunc(t *testing.T) {
testCompile(t, `package foo
func fn(a int, b float64) int {
return 1
}
`, `; ModuleID = 'foo'
source_filename = "foo"
2024-04-21 00:22:39 +08:00
@"init$guard" = global ptr null
2024-04-20 22:30:38 +08:00
2024-04-22 15:09:08 +08:00
define void @foo.init() {
2024-04-20 22:30:38 +08:00
_llgo_0:
%0 = load i1, ptr @"init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"init$guard", align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
2024-04-22 15:09:08 +08:00
define i64 @foo.fn(i64 %0, double %1) {
2024-04-20 22:30:38 +08:00
_llgo_0:
ret i64 1
}
`)
}