ssa: fix ssa.Index and indexType
This commit is contained in:
@@ -163,13 +163,14 @@ func (b Builder) checkIndex(idx Expr) Expr {
|
||||
// Example printed form:
|
||||
//
|
||||
// t2 = t0[t1]
|
||||
func (b Builder) Index(x, idx Expr, addr func(Expr) Expr) Expr {
|
||||
func (b Builder) Index(x, idx Expr, addr func(Expr) (Expr, bool)) Expr {
|
||||
if debugInstr {
|
||||
log.Printf("Index %v, %v\n", x.impl, idx.impl)
|
||||
}
|
||||
prog := b.Prog
|
||||
var telem Type
|
||||
var ptr Expr
|
||||
var zero bool
|
||||
switch t := x.raw.Type.Underlying().(type) {
|
||||
case *types.Basic:
|
||||
if t.Kind() != types.String {
|
||||
@@ -180,7 +181,7 @@ func (b Builder) Index(x, idx Expr, addr func(Expr) Expr) Expr {
|
||||
case *types.Array:
|
||||
telem = prog.Index(x.Type)
|
||||
if addr != nil {
|
||||
ptr = addr(x)
|
||||
ptr, zero = addr(x)
|
||||
} else {
|
||||
/*
|
||||
size := SizeOf(prog, telem, t.Len())
|
||||
@@ -190,7 +191,11 @@ func (b Builder) Index(x, idx Expr, addr func(Expr) Expr) Expr {
|
||||
panic("unreachable")
|
||||
}
|
||||
}
|
||||
// TODO check range
|
||||
idx = b.checkIndex(idx)
|
||||
if zero {
|
||||
return Expr{llvm.ConstNull(telem.ll), telem}
|
||||
}
|
||||
pt := prog.Pointer(telem)
|
||||
indices := []llvm.Value{idx.impl}
|
||||
buf := Expr{llvm.CreateInBoundsGEP(b.impl, telem.ll, ptr.impl, indices), pt}
|
||||
|
||||
11
ssa/type.go
11
ssa/type.go
@@ -61,11 +61,16 @@ const (
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
func indexType(t types.Type) types.Type {
|
||||
switch t := t.(type) {
|
||||
typ := t
|
||||
retry:
|
||||
switch t := typ.(type) {
|
||||
case *types.Named:
|
||||
typ = t.Underlying()
|
||||
goto retry
|
||||
case *types.Slice:
|
||||
return t.Elem()
|
||||
case *types.Pointer:
|
||||
switch t := t.Elem().(type) {
|
||||
switch t := t.Elem().Underlying().(type) {
|
||||
case *types.Array:
|
||||
return t.Elem()
|
||||
}
|
||||
@@ -193,7 +198,7 @@ func (p Program) Pointer(typ Type) Type {
|
||||
}
|
||||
|
||||
func (p Program) Elem(typ Type) Type {
|
||||
elem := typ.raw.Type.(interface {
|
||||
elem := typ.raw.Type.Underlying().(interface {
|
||||
Elem() types.Type
|
||||
}).Elem()
|
||||
return p.rawType(elem)
|
||||
|
||||
Reference in New Issue
Block a user