cl: call llgo/ssa.CType/CFuncDecl

This commit is contained in:
xushiwei
2024-05-03 16:00:31 +08:00
parent afd3d40348
commit 4a5c8d3fbb
3 changed files with 43 additions and 26 deletions

View File

@@ -177,15 +177,18 @@ 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, isDef := p.varName(gbl.Pkg.Pkg, gbl)
name, vtype := p.varName(gbl.Pkg.Pkg, gbl)
if ignoreName(name) || checkCgo(gbl.Name()) {
return
}
if debugInstr {
log.Println("==> NewVar", name, typ)
}
if vtype == cVar {
typ = llssa.CType(typ)
}
g := pkg.NewVar(name, typ)
if isDef {
if vtype == goVar {
g.Init(p.prog.Null(g.Type))
}
}
@@ -200,6 +203,9 @@ func (p *context) compileFunc(pkg llssa.Package, pkgTypes *types.Package, f *ssa
if debugInstr {
log.Println("==> NewFunc", name, "type:", sig.Recv(), sig)
}
if ftype == cFunc {
sig = llssa.CFuncDecl(sig)
}
fn := pkg.NewFunc(name, sig)
p.inits = append(p.inits, func() {
p.fn = fn

View File

@@ -220,12 +220,18 @@ 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) (vName string, isDef bool) {
const (
ignoredVar = iota
goVar
cVar
)
func (p *context) varName(pkg *types.Package, v *ssa.Global) (vName string, vtype int) {
name := llssa.FullName(pkg, v.Name())
if v, ok := p.link[name]; ok {
return v, false
return v, cVar
}
return name, true
return name, goVar
}
// funcOf returns a function by name and set ftype = goFunc, cFunc, etc.

View File

@@ -102,11 +102,16 @@ func methodToFunc(sig *types.Signature) *types.Signature {
// -----------------------------------------------------------------------------
// CType convert a cdecl type into Go type.
// CType convert a C type into Go.
func CType(typ types.Type) types.Type {
panic("todo")
}
// CFuncDecl convert a C function decl into Go signature.
func CFuncDecl(sig *types.Signature) *types.Signature {
panic("todo")
}
// -----------------------------------------------------------------------------
type aType struct {
@@ -319,11 +324,12 @@ func (p Program) toLLVMTypes(t *types.Tuple, n int) (ret []llvm.Type) {
}
func (p Program) toLLVMFunc(sig *types.Signature, inC, isDecl bool) Type {
var hasVArg bool
var kind valueKind
var ft llvm.Type
tParams := sig.Params()
n := tParams.Len()
if isDecl || inC {
var tParams = sig.Params()
var n = tParams.Len()
var hasVArg bool
if inC {
hasVArg = HasVArg(tParams, n)
if hasVArg {
@@ -341,7 +347,6 @@ func (p Program) toLLVMFunc(sig *types.Signature, inC, isDecl bool) Type {
default:
ret = p.toLLVMTuple(out)
}
if inC || isDecl {
ft = llvm.FunctionType(ret, params, hasVArg)
if isDecl {
kind = vkFuncDecl