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. // 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, isDef := p.varName(gbl.Pkg.Pkg, gbl) name, vtype := p.varName(gbl.Pkg.Pkg, gbl)
if ignoreName(name) || checkCgo(gbl.Name()) { if ignoreName(name) || checkCgo(gbl.Name()) {
return return
} }
if debugInstr { if debugInstr {
log.Println("==> NewVar", name, typ) log.Println("==> NewVar", name, typ)
} }
if vtype == cVar {
typ = llssa.CType(typ)
}
g := pkg.NewVar(name, typ) g := pkg.NewVar(name, typ)
if isDef { if vtype == goVar {
g.Init(p.prog.Null(g.Type)) 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 { if debugInstr {
log.Println("==> NewFunc", name, "type:", sig.Recv(), sig) log.Println("==> NewFunc", name, "type:", sig.Recv(), sig)
} }
if ftype == cFunc {
sig = llssa.CFuncDecl(sig)
}
fn := pkg.NewFunc(name, sig) fn := pkg.NewFunc(name, sig)
p.inits = append(p.inits, func() { p.inits = append(p.inits, func() {
p.fn = fn 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 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()) name := llssa.FullName(pkg, v.Name())
if v, ok := p.link[name]; ok { 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. // 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 { func CType(typ types.Type) types.Type {
panic("todo") panic("todo")
} }
// CFuncDecl convert a C function decl into Go signature.
func CFuncDecl(sig *types.Signature) *types.Signature {
panic("todo")
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
type aType struct { 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 { func (p Program) toLLVMFunc(sig *types.Signature, inC, isDecl bool) Type {
var hasVArg bool
var kind valueKind var kind valueKind
var ft llvm.Type var ft llvm.Type
tParams := sig.Params() if isDecl || inC {
n := tParams.Len() var tParams = sig.Params()
var n = tParams.Len()
var hasVArg bool
if inC { if inC {
hasVArg = HasVArg(tParams, n) hasVArg = HasVArg(tParams, n)
if hasVArg { if hasVArg {
@@ -341,7 +347,6 @@ func (p Program) toLLVMFunc(sig *types.Signature, inC, isDecl bool) Type {
default: default:
ret = p.toLLVMTuple(out) ret = p.toLLVMTuple(out)
} }
if inC || isDecl {
ft = llvm.FunctionType(ret, params, hasVArg) ft = llvm.FunctionType(ret, params, hasVArg)
if isDecl { if isDecl {
kind = vkFuncDecl kind = vkFuncDecl