ssa: index take addr

This commit is contained in:
visualfc
2024-07-02 21:14:36 +08:00
parent 46423ed166
commit 28d8c56534
9 changed files with 466 additions and 91 deletions

View File

@@ -405,6 +405,8 @@ func (b Builder) abiType(t types.Type) Expr {
switch t := t.(type) {
case *types.Pointer:
b.loadType(t.Elem())
case *types.Array:
b.loadType(t.Elem())
}
g := b.loadType(t)
return b.Load(g.Expr)

View File

@@ -286,7 +286,7 @@ func (b Builder) checkIndex(idx Expr, max Expr) Expr {
// Example printed form:
//
// t2 = t0[t1]
func (b Builder) Index(x, idx Expr, addr func(Expr) (Expr, bool)) Expr {
func (b Builder) Index(x, idx Expr, takeAddr func() Expr) Expr {
if debugInstr {
log.Printf("Index %v, %v\n", x.impl, idx.impl)
}
@@ -294,7 +294,6 @@ func (b Builder) Index(x, idx Expr, addr func(Expr) (Expr, bool)) Expr {
var telem Type
var ptr Expr
var max Expr
var zero bool
switch t := x.raw.Type.Underlying().(type) {
case *types.Basic:
if t.Kind() != types.String {
@@ -305,21 +304,12 @@ func (b Builder) Index(x, idx Expr, addr func(Expr) (Expr, bool)) Expr {
max = b.StringLen(x)
case *types.Array:
telem = prog.Index(x.Type)
if addr != nil {
ptr, zero = addr(x)
} else {
/*
size := SizeOf(prog, telem, t.Len())
ptr = b.Alloca(size)
b.Store(ptr, x)
*/
panic("unreachable")
}
ptr = takeAddr()
max = prog.IntVal(uint64(t.Len()), prog.Int())
}
idx = b.checkIndex(idx, max)
if zero {
return Expr{llvm.ConstNull(telem.ll), telem}
if ptr.IsNil() {
return Expr{llvm.ConstExtractElement(x.impl, idx.impl), telem}
}
pt := prog.Pointer(telem)
indices := []llvm.Value{idx.impl}

View File

@@ -138,6 +138,8 @@ func (p Program) Zero(t Type) Expr {
ret = llvm.ConstStruct(flds, false)
case *types.Slice:
ret = p.Zero(p.rtType("Slice")).impl
case *types.Array:
ret = llvm.ConstNull(t.ll)
case *types.Interface:
var name string
if u.Empty() {

View File

@@ -179,6 +179,10 @@ func (b Builder) buildVal(typ Type, val llvm.Value, lvl int) Expr {
telem := b.Prog.rawType(t.Field(0).Type())
elem := b.buildVal(telem, val, lvl-1)
return Expr{aggregateValue(b.impl, typ.ll, elem.impl), typ}
case *types.Array:
telem := b.Prog.rawType(t.Elem())
elem := b.buildVal(telem, val, lvl-1)
return Expr{llvm.ConstArray(typ.ll, []llvm.Value{elem.impl}), typ}
}
panic("todo")
}

View File

@@ -358,7 +358,7 @@ func (p Program) toType(raw types.Type) Type {
case *types.Slice:
return &aType{p.rtSlice(), typ, vkSlice}
case *types.Map:
return &aType{p.rtMap(), typ, vkMap}
return &aType{llvm.PointerType(p.rtMap(), 0), typ, vkMap}
case *types.Struct:
ll, kind := p.toLLVMStruct(t)
return &aType{ll, typ, kind}