cl: initLinknameByDoc

This commit is contained in:
xushiwei
2024-05-01 21:18:28 +08:00
parent 8d3cb246c2
commit 52a64a7770
6 changed files with 79 additions and 20 deletions

9
cl/_testrt/concat/in.go Normal file
View File

@@ -0,0 +1,9 @@
package main
import (
"github.com/goplus/llgo/internal/runtime/c"
)
func main() {
c.Fprintf(c.Stderr, c.Str("Hello %d\n"), 100)
}

32
cl/_testrt/concat/out.ll Normal file
View File

@@ -0,0 +1,32 @@
; ModuleID = 'main'
source_filename = "main"
@"main.init$guard" = global ptr null
@__stderrp = external global ptr
@0 = private unnamed_addr constant [10 x i8] c"Hello %d\0A\00", align 1
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
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define void @main() {
_llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%0 = load ptr, ptr @__stderrp, align 8
%1 = call i32 (ptr, ptr, ...) @fprintf(ptr %0, ptr @0, i64 100)
ret void
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare i32 @fprintf(ptr, ptr, ...)

View File

@@ -1,9 +1,16 @@
package main
import (
"github.com/goplus/llgo/internal/runtime/c"
)
import "unsafe"
//go:linkname cstr llgo.cstr
func cstr(string) *int8
//go:linkname stderr __stderrp
var stderr unsafe.Pointer
//go:linkname fprintf C.fprintf
func fprintf(fp unsafe.Pointer, format *int8, __llgo_va_list ...any)
func main() {
c.Fprintf(c.Stderr, c.Str("Hello %d\n"), 100)
fprintf(stderr, cstr("Hello %d\n"), 100)
}

View File

@@ -5,6 +5,8 @@ source_filename = "main"
@__stderrp = external global ptr
@0 = private unnamed_addr constant [10 x i8] c"Hello %d\0A\00", align 1
declare void @fprintf(ptr, ptr, ...)
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
@@ -23,10 +25,8 @@ _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%0 = load ptr, ptr @__stderrp, align 8
%1 = call i32 (ptr, ptr, ...) @fprintf(ptr %0, ptr @0, i64 100)
call void (ptr, ptr, ...) @fprintf(ptr %0, ptr @0, i64 100)
ret void
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare i32 @fprintf(ptr, ptr, ...)

View File

@@ -177,7 +177,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 := p.varName(gbl.Pkg.Pkg, gbl)
name, isDef := p.varName(gbl.Pkg.Pkg, gbl)
if ignoreName(name) || checkCgo(gbl.Name()) {
return
}
@@ -185,7 +185,9 @@ func (p *context) compileGlobal(pkg llssa.Package, gbl *ssa.Global) {
log.Println("==> NewVar", name, typ)
}
g := pkg.NewVar(name, typ)
if isDef {
g.Init(p.prog.Null(g.Type))
}
}
func (p *context) compileFunc(pkg llssa.Package, pkgTypes *types.Package, f *ssa.Function) {

View File

@@ -107,16 +107,25 @@ func (p *context) importPkg(pkg *types.Package, i *pkgInfo) {
func (p *context) initFiles(pkgPath string, files []*ast.File) {
for _, file := range files {
for _, decl := range file.Decls {
if decl, ok := decl.(*ast.FuncDecl); ok {
switch decl := decl.(type) {
case *ast.FuncDecl:
if decl.Recv == nil {
if doc := decl.Doc; doc != nil {
p.initLinknameByDoc(decl.Doc, pkgPath, false)
}
case *ast.GenDecl:
if decl.Tok == token.VAR && len(decl.Specs) == 1 {
p.initLinknameByDoc(decl.Doc, pkgPath, true)
}
}
}
}
}
func (p *context) initLinknameByDoc(doc *ast.CommentGroup, pkgPath string, isVar bool) {
if doc != nil {
if n := len(doc.List); n > 0 {
line := doc.List[n-1].Text
p.initLinkname(pkgPath, line, false)
}
}
}
}
p.initLinkname(pkgPath, line, isVar)
}
}
}
@@ -210,12 +219,12 @@ func (p *context) funcName(pkg *types.Package, fn *ssa.Function, ignore bool) (s
return name, goFunc
}
func (p *context) varName(pkg *types.Package, v *ssa.Global) string {
func (p *context) varName(pkg *types.Package, v *ssa.Global) (vName string, isDef bool) {
name := llssa.FullName(pkg, v.Name())
if v, ok := p.link[name]; ok {
return v
return v, false
}
return name
return name, true
}
// funcOf returns a function by name and set ftype = goFunc, cFunc, etc.
@@ -246,7 +255,7 @@ func (p *context) funcOf(fn *ssa.Function) (ret llssa.Function, ftype int) {
func (p *context) varOf(v *ssa.Global) (ret llssa.Global) {
pkgTypes := p.ensureLoaded(v.Pkg.Pkg)
pkg := p.pkg
name := p.varName(pkgTypes, v)
name, _ := p.varName(pkgTypes, v)
if ret = pkg.VarOf(name); ret == nil {
ret = pkg.NewVar(name, v.Type())
}