Skip to content

Commit a5b340e

Browse files
committed
ssa2pal: handle zero length array at variable index
1 parent 318129c commit a5b340e

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

ssa2pal/t.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -282,33 +282,34 @@ func (p *T) genValueLoc(v ssa.Value) memory.Loc {
282282
i := int(i64) // should be ok also b/c it is type checked.
283283
res = x.At(i)
284284
default:
285-
// we have a variable or expression index.
286-
// we
287-
// 1. take address of the array, call it pa, in a new loc
288-
// 2. create qa, same type as pa
289-
// 3. add AddTransferIndex(qa, pa, p.indexing.Var())
290-
// 4. create res, type of element of array
291-
// 5. create res = load(qa)
292-
// TBD:
293-
294-
ty, ok := v.X.Type().Underlying().(*types.Array)
295-
if !ok {
296-
panic(fmt.Sprintf("v type %s %#v\n", v.Type(), v.Type()))
297-
}
298-
299-
eltTy := ty.Elem()
300-
ptrTy := types.NewPointer(ty.Elem())
301-
pelt := p.buildr.GoType(ptrTy).Gen()
302-
if x.Len() != 0 {
285+
if x.Len() == 0 {
286+
// this should be type checked, but it is
287+
// not.
288+
res = p.buildr.Memory().Zero()
289+
} else {
290+
// we have a variable or expression index.
291+
// we
292+
// 1. take address of the array, call it pa, in a new loc
293+
// 2. create qa, same type as pa
294+
// 3. add AddTransferIndex(qa, pa, p.indexing.Var())
295+
// 4. create res, type of element of array
296+
// 5. create res = load(qa)
297+
ty, ok := v.X.Type().Underlying().(*types.Array)
298+
if !ok {
299+
panic(fmt.Sprintf("v type %s %#v\n", v.Type(), v.Type()))
300+
}
301+
eltTy := ty.Elem()
302+
ptrTy := types.NewPointer(ty.Elem())
303+
pelt := p.buildr.GoType(ptrTy).Gen()
303304
p.buildr.AddAddressOf(pelt, x.At(0))
305+
qelt := p.buildr.Gen()
306+
// it may crash if oob, add address of nil
307+
// TBD: see if with indexing we can constrain this.
308+
p.buildr.AddAddressOf(qelt, p.buildr.Memory().Zero())
309+
res = p.buildr.GoType(eltTy).Gen()
310+
p.buildr.AddTransferIndex(qelt, pelt, p.indexing.Var())
311+
p.buildr.AddLoad(res, qelt)
304312
}
305-
qelt := p.buildr.Gen()
306-
// it may crash if oob, add address of nil
307-
// TBD: see if with indexing we can constrain this.
308-
p.buildr.AddAddressOf(qelt, p.buildr.Memory().Zero())
309-
res = p.buildr.GoType(eltTy).Gen()
310-
p.buildr.AddTransferIndex(qelt, pelt, p.indexing.Var())
311-
p.buildr.AddLoad(res, qelt)
312313
}
313314
case *ssa.Extract:
314315
tloc := p.vmap[v.Tuple]
@@ -512,8 +513,7 @@ func (p *T) genI9nConstraints(fnName string, i9n ssa.Instruction) error {
512513
switch i9n.Op {
513514
case token.MUL: // *p
514515
p.buildr.AddLoad(p.vmap[i9n], p.vmap[i9n.X])
515-
case token.ARROW: // <- TBD:
516-
fmt.Printf("<-: %s %#v\n", i9n, i9n)
516+
case token.ARROW:
517517
c := p.buildr.Object(p.vmap[i9n.X]).(*objects.Chan)
518518
dst := p.vmap[i9n]
519519
if dst == memory.NoLoc {

0 commit comments

Comments
 (0)