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.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-04-25 07:29:45 +08:00
|
|
|
package cl_test
|
2024-04-20 15:58:34 +08:00
|
|
|
|
|
|
|
|
import (
|
2024-06-15 18:02:51 +08:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2024-06-15 18:08:08 +08:00
|
|
|
"runtime"
|
2024-04-20 15:58:34 +08:00
|
|
|
"testing"
|
|
|
|
|
|
2024-06-15 18:41:45 +08:00
|
|
|
"github.com/goplus/llgo/cl"
|
2024-04-25 07:29:45 +08:00
|
|
|
"github.com/goplus/llgo/cl/cltest"
|
2024-06-15 16:08:10 +08:00
|
|
|
"github.com/goplus/llgo/internal/build"
|
2024-04-28 14:00:29 +08:00
|
|
|
"github.com/goplus/llgo/ssa"
|
2024-04-20 15:58:34 +08:00
|
|
|
)
|
|
|
|
|
|
2024-04-20 23:15:10 +08:00
|
|
|
func testCompile(t *testing.T, src, expected string) {
|
|
|
|
|
t.Helper()
|
2024-04-25 07:29:45 +08:00
|
|
|
cltest.TestCompileEx(t, src, "foo.go", expected)
|
2024-04-20 23:15:10 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-27 11:28:03 +08:00
|
|
|
func TestFromTestgo(t *testing.T) {
|
|
|
|
|
cltest.FromDir(t, "", "./_testgo", false)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-11 10:07:46 +08:00
|
|
|
func TestFromTestpy(t *testing.T) {
|
2024-05-11 23:38:21 +08:00
|
|
|
cltest.FromDir(t, "", "./_testpy", false)
|
2024-05-11 10:07:46 +08:00
|
|
|
}
|
|
|
|
|
|
2024-06-13 02:53:54 +08:00
|
|
|
func TestFromTestlibgo(t *testing.T) {
|
|
|
|
|
cltest.FromDir(t, "", "./_testlibgo", true)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-07 15:35:37 +08:00
|
|
|
func TestFromTestlibc(t *testing.T) {
|
2024-05-12 11:11:19 +08:00
|
|
|
cltest.FromDir(t, "", "./_testlibc", true)
|
2024-05-07 15:35:37 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-30 23:18:18 +08:00
|
|
|
func TestFromTestrt(t *testing.T) {
|
2024-06-15 18:41:45 +08:00
|
|
|
cl.SetDebug(cl.DbgFlagAll)
|
2024-04-30 23:18:18 +08:00
|
|
|
cltest.FromDir(t, "", "./_testrt", true)
|
2024-06-15 18:41:45 +08:00
|
|
|
cl.SetDebug(0)
|
2024-04-26 02:40:36 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-25 14:25:14 +08:00
|
|
|
func TestFromTestdata(t *testing.T) {
|
2024-04-26 02:40:36 +08:00
|
|
|
cltest.FromDir(t, "", "./_testdata", false)
|
2024-04-25 14:25:14 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-15 21:43:38 +08:00
|
|
|
func TestSqlite(t *testing.T) {
|
2024-05-17 22:41:39 +08:00
|
|
|
cltest.Pkg(t, "github.com/goplus/llgo/c/sqlite", "../c/sqlite/llgo_autogen.ll")
|
2024-05-15 21:43:38 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-12 11:51:46 +08:00
|
|
|
func TestFromTestpymath(t *testing.T) {
|
|
|
|
|
cltest.Pkg(t, ssa.PkgPython+"/math", "../py/math/llgo_autogen.ll")
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-12 13:05:15 +08:00
|
|
|
func TestPython(t *testing.T) {
|
|
|
|
|
cltest.Pkg(t, ssa.PkgPython, "../py/llgo_autogen.ll")
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-15 16:08:10 +08:00
|
|
|
func TestGoLibMath(t *testing.T) {
|
2024-06-15 18:08:08 +08:00
|
|
|
if runtime.GOOS == "darwin" { // TODO(xsw): support linux/windows
|
|
|
|
|
root, _ := filepath.Abs("..")
|
|
|
|
|
os.Setenv("LLGOROOT", root)
|
|
|
|
|
conf := build.NewDefaultConf(build.ModeInstall)
|
|
|
|
|
build.Do([]string{"math"}, conf)
|
|
|
|
|
}
|
2024-06-15 16:08:10 +08:00
|
|
|
}
|
|
|
|
|
|
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-06-14 09:57:23 +08:00
|
|
|
@foo.a = global i64 0, align 8
|
|
|
|
|
@"foo.init$guard" = global i1 false, align 1
|
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:
|
2024-04-23 01:16:31 +08:00
|
|
|
%0 = load i1, ptr @"foo.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
|
2024-04-23 01:16:31 +08:00
|
|
|
store i1 true, ptr @"foo.init$guard", align 1
|
2024-04-20 22:05:45 +08:00
|
|
|
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-06-14 09:57:23 +08:00
|
|
|
@"foo.init$guard" = global i1 false, align 1
|
2024-04-20 22:30:38 +08:00
|
|
|
|
2024-04-26 03:25:11 +08:00
|
|
|
define i64 @foo.fn(i64 %0, double %1) {
|
|
|
|
|
_llgo_0:
|
|
|
|
|
ret i64 1
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 15:09:08 +08:00
|
|
|
define void @foo.init() {
|
2024-04-20 22:30:38 +08:00
|
|
|
_llgo_0:
|
2024-04-23 01:16:31 +08:00
|
|
|
%0 = load i1, ptr @"foo.init$guard", align 1
|
2024-04-20 22:30:38 +08:00
|
|
|
br i1 %0, label %_llgo_2, label %_llgo_1
|
|
|
|
|
|
|
|
|
|
_llgo_1: ; preds = %_llgo_0
|
2024-04-23 01:16:31 +08:00
|
|
|
store i1 true, ptr @"foo.init$guard", align 1
|
2024-04-20 22:30:38 +08:00
|
|
|
br label %_llgo_2
|
|
|
|
|
|
|
|
|
|
_llgo_2: ; preds = %_llgo_1, %_llgo_0
|
|
|
|
|
ret void
|
|
|
|
|
}
|
|
|
|
|
`)
|
|
|
|
|
}
|