Merge pull request #303 from xushiwei/q

llgo/ssa: float Const fix; cl: replace runtime => llgo/internal/runtime
This commit is contained in:
xushiwei
2024-06-13 03:10:40 +08:00
committed by GitHub
10 changed files with 1502 additions and 11 deletions

View File

@@ -207,6 +207,7 @@ Here are the Go packages that can be imported correctly:
* [unicode](https://pkg.go.dev/unicode) * [unicode](https://pkg.go.dev/unicode)
* [unicode/utf8](https://pkg.go.dev/unicode/utf8) * [unicode/utf8](https://pkg.go.dev/unicode/utf8)
* [unicode/utf16](https://pkg.go.dev/unicode/utf16) * [unicode/utf16](https://pkg.go.dev/unicode/utf16)
* [math/bits](https://pkg.go.dev/math/bits)
## How to install ## How to install

View File

@@ -34,6 +34,7 @@ func main() {
llgen.Verbose = false llgen.Verbose = false
llgenDir(dir + "/cl/_testlibc") llgenDir(dir + "/cl/_testlibc")
llgenDir(dir + "/cl/_testlibgo")
llgenDir(dir + "/cl/_testrt") llgenDir(dir + "/cl/_testrt")
llgenDir(dir + "/cl/_testgo") llgenDir(dir + "/cl/_testgo")
llgenDir(dir+"/cl/_testpy", "") llgenDir(dir+"/cl/_testpy", "")

10
cl/_testlibgo/math/in.go Normal file
View File

@@ -0,0 +1,10 @@
package main
import (
"math/bits"
)
func main() {
println(bits.Len8(20))
println(bits.OnesCount(20))
}

47
cl/_testlibgo/math/out.ll Normal file
View File

@@ -0,0 +1,47 @@
; 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)
%3 = call i64 @"math/bits.OnesCount"(i64 20)
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %3)
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)
declare i64 @"math/bits.OnesCount"(i64)

View File

@@ -27,6 +27,12 @@ import (
"golang.org/x/tools/go/ssa" "golang.org/x/tools/go/ssa"
) )
func TestReplaceGoName(t *testing.T) {
if ret := replaceGoName("foo", 0); ret != "foo" {
t.Fatal("replaceGoName:", ret)
}
}
func TestIsAllocVargs(t *testing.T) { func TestIsAllocVargs(t *testing.T) {
if isAllocVargs(nil, ssaAlloc(&ssa.Return{})) { if isAllocVargs(nil, ssaAlloc(&ssa.Return{})) {
t.Fatal("isVargs?") t.Fatal("isVargs?")

View File

@@ -192,7 +192,7 @@ func (p *context) compileMethods(pkg llssa.Package, typ types.Type) {
// Global variable. // Global variable.
func (p *context) compileGlobal(pkg llssa.Package, gbl *ssa.Global) { func (p *context) compileGlobal(pkg llssa.Package, gbl *ssa.Global) {
typ := gbl.Type() 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()) { if vtype == pyVar || ignoreName(name) || checkCgo(gbl.Name()) {
return return
} }
@@ -200,7 +200,7 @@ func (p *context) compileGlobal(pkg llssa.Package, gbl *ssa.Global) {
log.Println("==> NewVar", name, typ) log.Println("==> NewVar", name, typ)
} }
g := pkg.NewVar(name, typ, llssa.Background(vtype)) g := pkg.NewVar(name, typ, llssa.Background(vtype))
if vtype == goVar { if define {
g.Init(p.prog.Nil(g.Type)) g.Init(p.prog.Nil(g.Type))
} }
} }

View File

@@ -36,6 +36,10 @@ func TestFromTestpy(t *testing.T) {
cltest.FromDir(t, "", "./_testpy", false) cltest.FromDir(t, "", "./_testpy", false)
} }
func TestFromTestlibgo(t *testing.T) {
cltest.FromDir(t, "", "./_testlibgo", true)
}
func TestFromTestlibc(t *testing.T) { func TestFromTestlibc(t *testing.T) {
cltest.FromDir(t, "", "./_testlibc", true) cltest.FromDir(t, "", "./_testlibc", true)
} }

View File

@@ -356,21 +356,24 @@ const (
pyVar = int(llssa.InPython) 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()) name := llssa.FullName(pkg, v.Name())
if v, ok := p.link[name]; ok { if v, ok := p.link[name]; ok {
if strings.HasPrefix(v, "py.") { if pos := strings.IndexByte(v, '.'); pos >= 0 {
return v[3:], pyVar if pos == 2 && v[0] == 'p' && v[1] == 'y' {
return v[3:], pyVar, false
}
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 { func (p *context) varOf(b llssa.Builder, v *ssa.Global) llssa.Expr {
pkgTypes := p.ensureLoaded(v.Pkg.Pkg) pkgTypes := p.ensureLoaded(v.Pkg.Pkg)
pkg := p.pkg pkg := p.pkg
name, vtype := p.varName(pkgTypes, v) name, vtype, _ := p.varName(pkgTypes, v)
if vtype == pyVar { if vtype == pyVar {
if kind, mod := pkgKindByScope(pkgTypes.Scope()); kind == PkgPyModule { if kind, mod := pkgKindByScope(pkgTypes.Scope()); kind == PkgPyModule {
return b.PyNewVar(pysymPrefix+mod, name).Expr return b.PyNewVar(pysymPrefix+mod, name).Expr
@@ -405,6 +408,14 @@ func pkgKindByPath(pkgPath string) int {
return PkgNormal return PkgNormal
} }
func replaceGoName(v string, pos int) string {
switch v[:pos] {
case "runtime":
return "github.com/goplus/llgo/internal/runtime" + v[pos:]
}
return v
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
const ( const (

1412
internal/runtime/panic.go Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -194,9 +194,8 @@ func (b Builder) Const(v constant.Value, typ Type) Expr {
return prog.IntVal(v, typ) return prog.IntVal(v, typ)
} }
case kind == types.Float32 || kind == types.Float64: case kind == types.Float32 || kind == types.Float64:
if v, exact := constant.Float64Val(v); exact { v, _ := constant.Float64Val(v)
return prog.FloatVal(v, typ) return prog.FloatVal(v, typ)
}
case kind == types.String: case kind == types.String:
return Expr{b.Str(constant.StringVal(v)).impl, typ} return Expr{b.Str(constant.StringVal(v)).impl, typ}
} }