Skip to content

Commit

Permalink
后端增加内置函数 raw() 支持
Browse files Browse the repository at this point in the history
  • Loading branch information
3dgen committed Nov 24, 2023
1 parent 4bc4e68 commit c297fb7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/backends/compiler_wat/compile_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,13 @@ func (g *functionGenerator) genBuiltin(call *ssa.CallCommon) (insts []wat.Inst,
insts = g.module.EmitGenCopy(g.getValue(call.Args[0]).value, g.getValue(call.Args[1]).value)
ret_type = g.module.I32

case "raw":
if len(call.Args) != 1 {
panic("len(cap.Args) != 1")
}
insts = g.module.EmitGenRaw(g.getValue(call.Args[0]).value)
ret_type = g.module.BYTES

case "ssa:wrapnilchk":
insts = g.getValue(call.Args[0]).value.EmitPushNoRetain()
ret_type = g.getValue(call.Args[0]).value.Type()
Expand Down
5 changes: 5 additions & 0 deletions internal/backends/compiler_wat/wir/instruction_emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,11 @@ func (m *Module) EmitGenCopy(x, y Value) (insts []wat.Inst) {
return
}

func (m *Module) EmitGenRaw(x Value) (insts []wat.Inst) {
s := x.(*aSlice)
return s.emitConvertToBytes()
}

func (m *Module) EmitGenMakeInterface(x Value, itype ValueType) (insts []wat.Inst) {
x_type := x.Type()
m.markConcreteTypeUsed(x_type)
Expand Down
18 changes: 18 additions & 0 deletions internal/backends/compiler_wat/wir/value_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,3 +569,21 @@ func (v *aSlice) emitEq(r Value) ([]wat.Inst, bool) {

return v.ExtractByName("d").emitEq(r_s.ExtractByName("d"))
}

func (v *aSlice) emitConvertToBytes() (insts []wat.Inst) {
// block:
insts = append(insts, v.ExtractByName("b").EmitPush()...)

// data:
insts = append(insts, v.ExtractByName("d").EmitPush()...)

// len:
insts = append(insts, v.ExtractByName("l").EmitPush()...)
insts = append(insts, wat.NewInstConst(wat.U32{}, strconv.Itoa(v.typ.Base.Size())))
insts = append(insts, wat.NewInstMul(wat.U32{}))

// cap:
insts = append(insts, wat.NewInstCall("runtime.DupI32"))

return
}

0 comments on commit c297fb7

Please sign in to comment.