llgo/ssa: introduce rawType

This commit is contained in:
xushiwei
2024-05-05 12:11:51 +08:00
parent 52018cd424
commit 5d1d51dd58
13 changed files with 485 additions and 381 deletions

View File

@@ -21,10 +21,11 @@ _llgo_1: ; preds = %_llgo_2, %_llgo_0
br i1 %8, label %_llgo_2, label %_llgo_3
_llgo_2: ; preds = %_llgo_1
%9 = extractvalue { ptr, ptr } %1, 0
%10 = call ptr @"github.com/goplus/llgo/internal/runtime.SliceData"(%"github.com/goplus/llgo/internal/runtime.Slice" %4)
%11 = getelementptr inbounds i32, ptr %10, i64 %7
store i32 0, ptr %11, align 4
%9 = extractvalue { i32 ()*, ptr } %1, 0
%10 = call i32 ()* %9()
%11 = call ptr @"github.com/goplus/llgo/internal/runtime.SliceData"(%"github.com/goplus/llgo/internal/runtime.Slice" %4)
%12 = getelementptr inbounds i32, ptr %11, i64 %7
store ptr %10, ptr %12, align 8
br label %_llgo_1
_llgo_3: ; preds = %_llgo_1

View File

@@ -184,10 +184,7 @@ func (p *context) compileGlobal(pkg llssa.Package, gbl *ssa.Global) {
if debugInstr {
log.Println("==> NewVar", name, typ)
}
if vtype == cVar {
typ = llssa.CType(typ)
}
g := pkg.NewVar(name, typ)
g := pkg.NewVar(name, typ, llssa.Background(vtype))
if vtype == goVar {
g.Init(p.prog.Null(g.Type))
}
@@ -196,13 +193,13 @@ func (p *context) compileGlobal(pkg llssa.Package, gbl *ssa.Global) {
func (p *context) compileFunc(pkg llssa.Package, pkgTypes *types.Package, f *ssa.Function, closure bool) llssa.Function {
var sig = f.Signature
var name string
var ftype int
if closure {
name = funcName(pkgTypes, f)
name, ftype = funcName(pkgTypes, f), goFunc
if debugInstr {
log.Println("==> NewClosure", name, "type:", sig)
}
} else {
var ftype int
name, ftype = p.funcName(pkgTypes, f, true)
switch ftype {
case ignoredFunc, llgoInstr: // llgo extended instructions
@@ -211,11 +208,8 @@ 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)
fn := pkg.NewFunc(name, sig, llssa.Background(ftype))
p.inits = append(p.inits, func() {
p.fn = fn
defer func() {
@@ -265,7 +259,8 @@ const (
)
func callRuntimeInit(b llssa.Builder, pkg llssa.Package) {
fn := pkg.NewFunc(RuntimeInit, types.NewSignatureType(nil, nil, nil, nil, nil, false))
sig := types.NewSignatureType(nil, nil, nil, nil, nil, false)
fn := pkg.NewFunc(RuntimeInit, sig, llssa.InC) // don't need to convert runtime.init
b.Call(fn.Expr)
}
@@ -372,7 +367,7 @@ func (p *context) compilePhis(b llssa.Builder, instrs []ssa.Instruction) []ssa.I
}
func (p *context) compilePhi(b llssa.Builder, v *ssa.Phi) (ret llssa.Expr) {
phi := b.Phi(p.prog.Type(v.Type()))
phi := b.Phi(p.prog.Type(v.Type(), llssa.InGo))
ret = phi.Expr
p.phis = append(p.phis, func() {
preds := v.Block().Preds
@@ -451,11 +446,11 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue
case *ssa.ChangeType:
t := v.Type()
x := p.compileValue(b, v.X)
ret = b.ChangeType(p.prog.Type(t), x)
ret = b.ChangeType(p.prog.Type(t, llssa.InGo), x)
case *ssa.Convert:
t := v.Type()
x := p.compileValue(b, v.X)
ret = b.Convert(p.prog.Type(t), x)
ret = b.Convert(p.prog.Type(t, llssa.InGo), x)
case *ssa.FieldAddr:
x := p.compileValue(b, v.X)
ret = b.FieldAddr(x, v.Field)
@@ -464,7 +459,8 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue
if p.checkVArgs(v, t) { // varargs: this is a varargs allocation
return
}
ret = b.Alloc(t, v.Heap)
elem := p.prog.Type(t.Elem(), llssa.InGo)
ret = b.Alloc(elem, v.Heap)
case *ssa.IndexAddr:
vx := v.X
if _, ok := p.isVArgs(vx); ok { // varargs: this is a varargs index
@@ -505,24 +501,24 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue
const (
delayExpr = true // varargs: don't need to convert an expr to any
)
t := v.Type()
t := p.prog.Type(v.Type(), llssa.InGo)
x := p.compileValue(b, v.X)
ret = b.MakeInterface(t, x, delayExpr)
case *ssa.MakeSlice:
var nCap llssa.Expr
t := v.Type()
t := p.prog.Type(v.Type(), llssa.InGo)
nLen := p.compileValue(b, v.Len)
if v.Cap != nil {
nCap = p.compileValue(b, v.Cap)
}
ret = b.MakeSlice(p.prog.Type(t), nLen, nCap)
ret = b.MakeSlice(t, nLen, nCap)
case *ssa.MakeMap:
var nReserve llssa.Expr
t := v.Type()
t := p.prog.Type(v.Type(), llssa.InGo)
if v.Reserve != nil {
nReserve = p.compileValue(b, v.Reserve)
}
ret = b.MakeMap(p.prog.Type(t), nReserve)
ret = b.MakeMap(t, nReserve)
/*
case *ssa.MakeClosure:
fn := p.compileValue(b, v.Fn)
@@ -531,7 +527,8 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue
*/
case *ssa.TypeAssert:
x := p.compileValue(b, v.X)
ret = b.TypeAssert(x, p.prog.Type(v.AssertedType), v.CommaOk)
t := p.prog.Type(v.AssertedType, llssa.InGo)
ret = b.TypeAssert(x, t, v.CommaOk)
default:
panic(fmt.Sprintf("compileInstrAndValue: unknown instr - %T\n", iv))
}
@@ -622,7 +619,7 @@ func (p *context) compileValue(b llssa.Builder, v ssa.Value) llssa.Expr {
return g.Expr
case *ssa.Const:
t := types.Default(v.Type())
return b.Const(v.Value, p.prog.Type(t))
return b.Const(v.Value, p.prog.Type(t, llssa.InGo))
}
panic(fmt.Sprintf("compileValue: unknown value - %T\n", v))
}

View File

@@ -191,9 +191,9 @@ func checkCgo(fnName string) bool {
const (
ignoredFunc = iota
goFunc
cFunc
llgoInstr = -1
goFunc = int(llssa.InGo)
cFunc = int(llssa.InC)
llgoInstr = -1
llgoInstrBase = 0x80
llgoUnreachable = llgoInstrBase + 0
@@ -222,8 +222,8 @@ func (p *context) funcName(pkg *types.Package, fn *ssa.Function, ignore bool) (s
const (
ignoredVar = iota
goVar
cVar
goVar = int(llssa.InGo)
cVar = int(llssa.InC)
)
func (p *context) varName(pkg *types.Package, v *ssa.Global) (vName string, vtype int) {
@@ -256,7 +256,8 @@ func (p *context) funcOf(fn *ssa.Function) (ret llssa.Function, ftype int) {
panic("unknown llgo instruction: " + name)
}
} else if ret = pkg.FuncOf(name); ret == nil {
ret = pkg.NewFunc(name, fn.Signature)
sig := fn.Signature
ret = pkg.NewFunc(name, sig, llssa.Background(ftype))
}
return
}
@@ -264,9 +265,9 @@ 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, vtype := p.varName(pkgTypes, v)
if ret = pkg.VarOf(name); ret == nil {
ret = pkg.NewVar(name, v.Type())
ret = pkg.NewVar(name, v.Type(), llssa.Background(vtype))
}
return
}