提交 7f536c42 编写于 作者: 3 3dgen

支持 ssa.Index

上级 6b2266b2
......@@ -360,6 +360,9 @@ func (g *functionGenerator) genValue(v ssa.Value) ([]wat.Inst, wir.ValueType) {
case *ssa.IndexAddr:
return g.genIndexAddr(v)
case *ssa.Index:
return g.genIndex(v)
case *ssa.Slice:
return g.genSlice(v)
......@@ -717,6 +720,13 @@ func (g *functionGenerator) genIndexAddr(inst *ssa.IndexAddr) ([]wat.Inst, wir.V
return g.module.EmitGenIndexAddr(x.value, id.value)
}
func (g *functionGenerator) genIndex(inst *ssa.Index) (insts []wat.Inst, ret_type wir.ValueType) {
x := g.getValue(inst.X)
id := g.getValue(inst.Index)
return g.module.EmitGenIndex(x.value, id.value)
}
func (g *functionGenerator) genSlice(inst *ssa.Slice) ([]wat.Inst, wir.ValueType) {
if inst.Parent().ForceRegister() {
logger.Fatal("ssa.Slice is not available in ForceRegister-mode")
......
......@@ -296,6 +296,23 @@ func (m *Module) EmitGenIndexAddr(x, id Value) (insts []wat.Inst, ret_type Value
return
}
func (m *Module) EmitGenIndex(x, id Value) (insts []wat.Inst, ret_type ValueType) {
if !id.Type().Equal(m.I32) {
panic("index should be i32")
}
switch x := x.(type) {
case *aArray:
ret_type = x.Type().(*Array).Base
insts = append(insts, x.emitIndexOf(id)...)
default:
logger.Fatalf("Todo: %T", x)
}
return
}
func (m *Module) EmitGenSlice(x, low, high Value) (insts []wat.Inst, ret_type ValueType) {
switch x := x.(type) {
case *aSlice:
......
......@@ -51,6 +51,56 @@ func (t *Array) EmitLoadFromAddr(addr Value, offset int) (insts []wat.Inst) {
return t.Struct.EmitLoadFromAddr(addr, offset)
}
func (t *Array) genFunc_IndexOf() string {
if t.Capacity == 0 {
return ""
}
fn_name := "$" + t.Name() + ".$IndexOf"
if currentModule.FindFunc(fn_name) != nil {
return fn_name
}
var f Function
f.InternalName = fn_name
x := newValue_Array("x", ValueKindLocal, t)
id := newValue_Basic("id", ValueKindLocal, t._u32)
f.Params = append(f.Params, x)
f.Params = append(f.Params, id)
f.Results = append(f.Results, t.Base)
ret := NewLocal("ret", t.Base)
f.Locals = append(f.Locals, ret)
var block_pre wat.Inst
{
table := make([]int, t.Capacity+1)
for i := 0; i < t.Capacity; i++ {
table[i] = i
}
table[t.Capacity] = t.Capacity - 1
block_sel := wat.NewInstBlock("block_sel")
block_sel.Insts = append(block_sel.Insts, id.EmitPush()...)
block_sel.Insts = append(block_sel.Insts, wat.NewInstBrTable(table))
block_pre = block_sel
}
for i := 0; i < t.Capacity; i++ {
block := wat.NewInstBlock("block" + strconv.Itoa(i))
block.Insts = append(block.Insts, block_pre)
block.Insts = append(block.Insts, x.Extract("m"+strconv.Itoa(i)).EmitPush()...)
block.Insts = append(block.Insts, ret.EmitPop()...)
block.Insts = append(block.Insts, wat.NewInstBr("block"+strconv.Itoa(t.Capacity-1)))
block_pre = block
}
f.Insts = append(f.Insts, block_pre)
f.Insts = append(f.Insts, ret.EmitPush()...)
currentModule.AddFunc(&f)
return fn_name
}
/**************************************
aArray:
**************************************/
......@@ -82,3 +132,16 @@ func (v *aArray) emitStoreToAddr(addr Value, offset int) (insts []wat.Inst) {
return v.aStruct.emitStoreToAddr(addr, offset)
}
func (v *aArray) emitIndexOf(id Value) (insts []wat.Inst) {
fn_name := v.typ.genFunc_IndexOf()
if len(fn_name) == 0 {
return
}
insts = append(insts, v.EmitPush()...)
insts = append(insts, id.EmitPush()...)
insts = append(insts, wat.NewInstCall(fn_name))
return
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册