ssa: makeSlice fit int size and check

This commit is contained in:
visualfc
2024-06-25 21:13:41 +08:00
parent 4a28893171
commit c90703dc13
6 changed files with 170 additions and 74 deletions

View File

@@ -416,16 +416,29 @@ func (b Builder) MakeSlice(t Type, len, cap Expr) (ret Expr) {
log.Printf("MakeSlice %v, %v, %v\n", t.RawType(), len.impl, cap.impl)
}
prog := b.Prog
len = b.fitIntSize(len)
if cap.IsNil() {
cap = len
} else {
cap = b.fitIntSize(cap)
}
telem := prog.Index(t)
ptr := b.ArrayAlloc(telem, cap)
ret.impl = b.unsafeSlice(ptr, len.impl, cap.impl).impl
ret = b.InlineCall(b.Pkg.rtFunc("MakeSlice"), len, cap, prog.IntVal(prog.SizeOf(telem), prog.Int()))
ret.Type = t
return
}
// fit size to int
func (b Builder) fitIntSize(n Expr) Expr {
prog := b.Prog
typ := prog.Int()
if prog.SizeOf(n.Type) != prog.SizeOf(typ) {
n.Type = typ
n.impl = castInt(b, n.impl, typ)
}
return n
}
// -----------------------------------------------------------------------------
// The MakeMap instruction creates a new hash-table-based map object