diff --git a/chore/gentests/gentests.go b/chore/gentests/gentests.go index 66f31ce2..576a2aee 100644 --- a/chore/gentests/gentests.go +++ b/chore/gentests/gentests.go @@ -34,6 +34,7 @@ func main() { llgen.Verbose = false llgenDir(dir + "/cl/_testlibc") + llgenDir(dir + "/cl/_testlibgo") llgenDir(dir + "/cl/_testrt") llgenDir(dir + "/cl/_testgo") llgenDir(dir+"/cl/_testpy", "") diff --git a/cl/_testlibgo/math/in.go b/cl/_testlibgo/math/in.go new file mode 100644 index 00000000..2b07ec66 --- /dev/null +++ b/cl/_testlibgo/math/in.go @@ -0,0 +1,9 @@ +package main + +import ( + "math/bits" +) + +func main() { + println(bits.Len8(20)) +} diff --git a/cl/_testlibgo/math/out.ll b/cl/_testlibgo/math/out.ll new file mode 100644 index 00000000..ab803b48 --- /dev/null +++ b/cl/_testlibgo/math/out.ll @@ -0,0 +1,42 @@ +; ModuleID = 'main' +source_filename = "main" + +@"main.init$guard" = global ptr null +@__llgo_argc = global ptr null +@__llgo_argv = global ptr null + +define void @main.init() { +_llgo_0: + %0 = load i1, ptr @"main.init$guard", align 1 + br i1 %0, label %_llgo_2, label %_llgo_1 + +_llgo_1: ; preds = %_llgo_0 + store i1 true, ptr @"main.init$guard", align 1 + call void @"math/bits.init"() + br label %_llgo_2 + +_llgo_2: ; preds = %_llgo_1, %_llgo_0 + ret void +} + +define i32 @main(i32 %0, ptr %1) { +_llgo_0: + store i32 %0, ptr @__llgo_argc, align 4 + store ptr %1, ptr @__llgo_argv, align 8 + call void @"github.com/goplus/llgo/internal/runtime.init"() + call void @main.init() + %2 = call i64 @"math/bits.Len8"(i8 20) + call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %2) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10) + ret i32 0 +} + +declare void @"math/bits.init"() + +declare void @"github.com/goplus/llgo/internal/runtime.init"() + +declare i64 @"math/bits.Len8"(i8) + +declare void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64) + +declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8) diff --git a/cl/compile.go b/cl/compile.go index 970589ce..e71609dc 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -192,7 +192,7 @@ func (p *context) compileMethods(pkg llssa.Package, typ types.Type) { // Global variable. func (p *context) compileGlobal(pkg llssa.Package, gbl *ssa.Global) { typ := gbl.Type() - name, vtype := p.varName(gbl.Pkg.Pkg, gbl) + name, vtype, define := p.varName(gbl.Pkg.Pkg, gbl) if vtype == pyVar || ignoreName(name) || checkCgo(gbl.Name()) { return } @@ -200,7 +200,7 @@ func (p *context) compileGlobal(pkg llssa.Package, gbl *ssa.Global) { log.Println("==> NewVar", name, typ) } g := pkg.NewVar(name, typ, llssa.Background(vtype)) - if vtype == goVar { + if define { g.Init(p.prog.Nil(g.Type)) } } diff --git a/cl/compile_test.go b/cl/compile_test.go index 130d6d6b..d1126f86 100644 --- a/cl/compile_test.go +++ b/cl/compile_test.go @@ -36,6 +36,10 @@ func TestFromTestpy(t *testing.T) { cltest.FromDir(t, "", "./_testpy", false) } +func TestFromTestlibgo(t *testing.T) { + cltest.FromDir(t, "", "./_testlibgo", true) +} + func TestFromTestlibc(t *testing.T) { cltest.FromDir(t, "", "./_testlibc", true) } diff --git a/cl/import.go b/cl/import.go index 9b3c7831..a79144d6 100644 --- a/cl/import.go +++ b/cl/import.go @@ -356,24 +356,24 @@ const ( pyVar = int(llssa.InPython) ) -func (p *context) varName(pkg *types.Package, v *ssa.Global) (vName string, vtype int) { +func (p *context) varName(pkg *types.Package, v *ssa.Global) (vName string, vtype int, define bool) { name := llssa.FullName(pkg, v.Name()) if v, ok := p.link[name]; ok { if pos := strings.IndexByte(v, '.'); pos >= 0 { if pos == 2 && v[0] == 'p' && v[1] == 'y' { - return v[3:], pyVar + return v[3:], pyVar, false } - return replaceGoName(v, pos), goVar + return replaceGoName(v, pos), goVar, false } - return v, cVar + return v, cVar, false } - return name, goVar + return name, goVar, true } func (p *context) varOf(b llssa.Builder, v *ssa.Global) llssa.Expr { pkgTypes := p.ensureLoaded(v.Pkg.Pkg) pkg := p.pkg - name, vtype := p.varName(pkgTypes, v) + name, vtype, _ := p.varName(pkgTypes, v) if vtype == pyVar { if kind, mod := pkgKindByScope(pkgTypes.Scope()); kind == PkgPyModule { return b.PyNewVar(pysymPrefix+mod, name).Expr diff --git a/internal/runtime/panic.go b/internal/runtime/panic.go index d252be26..1d90e032 100644 --- a/internal/runtime/panic.go +++ b/internal/runtime/panic.go @@ -218,6 +218,10 @@ func panicSlice3CU(x uint, y int) func panicSliceConvert(x int, y int) */ +var divideError error +var overflowError error + +/* TODO(xsw): var shiftError = error(errorString("negative shift amount")) func Panicshift() {