ssa: support runtime.map

This commit is contained in:
visualfc
2024-06-28 22:01:58 +08:00
parent e05e61a59b
commit 2ccd1625e7
15 changed files with 1391 additions and 102 deletions

View File

@@ -145,6 +145,15 @@ func (b Builder) SliceCap(x Expr) Expr {
return Expr{ptr, b.Prog.Int()}
}
func (b Builder) MapLen(x Expr) Expr {
if debugInstr {
log.Printf("MapLen %v\n", x.impl)
}
prog := b.Prog
x.Type = prog.Pointer(prog.Int())
return b.Load(x)
}
// -----------------------------------------------------------------------------
// The IndexAddr instruction yields the address of the element at
@@ -450,9 +459,12 @@ func (b Builder) MakeMap(t Type, nReserve Expr) (ret Expr) {
if debugInstr {
log.Printf("MakeMap %v, %v\n", t.RawType(), nReserve.impl)
}
if nReserve.IsNil() {
nReserve = b.Prog.Val(0)
}
typ := b.abiType(t.raw.Type)
ret = b.InlineCall(b.Pkg.rtFunc("MakeMap"), typ, nReserve)
ret.Type = t
ret.impl = b.InlineCall(b.Pkg.rtFunc("MakeSmallMap")).impl
// TODO(xsw): nReserve
return
}
@@ -471,8 +483,21 @@ func (b Builder) Lookup(x, key Expr, commaOk bool) (ret Expr) {
if debugInstr {
log.Printf("Lookup %v, %v, %v\n", x.impl, key.impl, commaOk)
}
// TODO(xsw)
// panic("todo")
prog := b.Prog
typ := b.abiType(x.raw.Type)
vtyp := prog.Elem(x.Type)
ptr := b.mapKeyPtr(key)
if commaOk {
vals := b.Call(b.Pkg.rtFunc("MapAccess2"), typ, x, ptr)
val := b.Load(Expr{b.impl.CreateExtractValue(vals.impl, 0, ""), prog.Pointer(vtyp)})
ok := b.impl.CreateExtractValue(vals.impl, 1, "")
t := prog.Struct(vtyp, prog.Bool())
return b.aggregateValue(t, val.impl, ok)
} else {
val := b.Call(b.Pkg.rtFunc("MapAccess1"), typ, x, ptr)
val.Type = prog.Pointer(vtyp)
ret = b.Load(val)
}
return
}
@@ -489,8 +514,20 @@ func (b Builder) MapUpdate(m, k, v Expr) {
if debugInstr {
log.Printf("MapUpdate %v[%v] = %v\n", m.impl, k.impl, v.impl)
}
// TODO(xsw)
// panic("todo")
typ := b.abiType(m.raw.Type)
ptr := b.mapKeyPtr(k)
ret := b.Call(b.Pkg.rtFunc("MapAssign"), typ, m, ptr)
ret.Type = b.Prog.Pointer(v.Type)
b.Store(ret, v)
}
// key => unsafe.Pointer
func (b Builder) mapKeyPtr(x Expr) Expr {
typ := x.Type
vtyp := b.Prog.VoidPtr()
vptr := b.AllocU(typ)
b.Store(vptr, x)
return Expr{vptr.impl, vtyp}
}
// -----------------------------------------------------------------------------