Skip to content

Commit

Permalink
wasm 支持 memory.fill 和 memory.copy 指令
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed Jan 18, 2025
1 parent 570175e commit dce7e9e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/wat/ast/ins.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ type Ins_I64Store32 struct {
type Ins_MemorySize struct{ OpToken }
type Ins_MemoryGrow struct{ OpToken }

type Ins_MemoryInit struct {
OpToken
DataIdx int32
}
type Ins_MemoryCopy struct{ OpToken }
type Ins_MemoryFill struct{ OpToken }

type Ins_I32Const struct {
OpToken
X int32
Expand Down
22 changes: 22 additions & 0 deletions internal/wat/parser/module_func_instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ func (p *parser) parseInstruction() ast.Instruction {
return p.parseIns_MemorySize()
case token.INS_MEMORY_GROW:
return p.parseIns_MemoryGrow()
case token.INS_MEMORY_INIT:
return p.parseIns_MemoryInit()
case token.INS_MEMORY_COPY:
return p.parseIns_MemoryCopy()
case token.INS_MEMORY_FILL:
return p.parseIns_MemoryFill()
case token.INS_I32_CONST:
return p.parseIns_I32Const()
case token.INS_I64_CONST:
Expand Down Expand Up @@ -1031,6 +1037,22 @@ func (p *parser) parseIns_MemoryGrow() (i ast.Ins_MemoryGrow) {
p.acceptToken(token.INS_MEMORY_GROW)
return
}
func (p *parser) parseIns_MemoryInit() (i ast.Ins_MemoryInit) {
i.OpToken = ast.OpToken(p.tok)
p.acceptToken(token.INS_MEMORY_INIT)
i.DataIdx = p.parseInt32Lit() // TODO: 支持标识符
return
}
func (p *parser) parseIns_MemoryCopy() (i ast.Ins_MemoryCopy) {
i.OpToken = ast.OpToken(p.tok)
p.acceptToken(token.INS_MEMORY_COPY)
return
}
func (p *parser) parseIns_MemoryFill() (i ast.Ins_MemoryFill) {
i.OpToken = ast.OpToken(p.tok)
p.acceptToken(token.INS_MEMORY_FILL)
return
}
func (p *parser) parseIns_I32Const() (i ast.Ins_I32Const) {
i.OpToken = ast.OpToken(p.tok)
p.acceptToken(token.INS_I32_CONST)
Expand Down
6 changes: 6 additions & 0 deletions internal/wat/printer/printer_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,12 @@ func (p *watPrinter) printFuncs_body_ins(fn *ast.Func, ins ast.Instruction, blkL
fmt.Fprintln(p.w, tok)
case token.INS_MEMORY_GROW:
fmt.Fprintln(p.w, tok)
case token.INS_MEMORY_INIT:
fmt.Fprintln(p.w, tok, ins.(ast.Ins_MemoryInit).DataIdx)
case token.INS_MEMORY_COPY:
fmt.Fprintln(p.w, tok)
case token.INS_MEMORY_FILL:
fmt.Fprintln(p.w, tok)
case token.INS_I32_CONST:
fmt.Fprintln(p.w, tok, ins.(ast.Ins_I32Const).X)
case token.INS_I64_CONST:
Expand Down
6 changes: 6 additions & 0 deletions internal/wat/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ const (
INS_I64_STORE32 // 0x3e, i64.store32
INS_MEMORY_SIZE // 0x3f, memory.size
INS_MEMORY_GROW // 0x40, memory.grow
INS_MEMORY_INIT // 0xfc 0x08, memory.init
INS_MEMORY_COPY // 0xfc 0x0a, memory.copy
INS_MEMORY_FILL // 0xfc 0x0b, memory.fill
INS_I32_CONST // 0x41, i32.const
INS_I64_CONST // 0x42, i64.const
INS_F32_CONST // 0x43, f32.const
Expand Down Expand Up @@ -340,6 +343,9 @@ var tokens = [...]string{
INS_I64_STORE32: "i64.store32",
INS_MEMORY_SIZE: "memory.size",
INS_MEMORY_GROW: "memory.grow",
INS_MEMORY_INIT: "memory.init",
INS_MEMORY_COPY: "memory.copy",
INS_MEMORY_FILL: "memory.fill",
INS_I32_CONST: "i32.const",
INS_I64_CONST: "i64.const",
INS_F32_CONST: "f32.const",
Expand Down
21 changes: 21 additions & 0 deletions internal/wat/watutil/wat2c/wat2c_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,27 @@ func (p *wat2cWorker) buildFunc_ins(w io.Writer, fn *ast.Func, stk *valueTypeSta
fmt.Fprintf(w, "%swasm_memoy_size += $R%d.i32; $R%d.i32 = wasm_memoy_size;\n",
indent, sp0, ret0,
)
case token.INS_MEMORY_INIT:
dst := stk.Pop(token.I32)
off := stk.Pop(token.I32)
len := stk.Pop(token.I32)
fmt.Fprintf(w, "%sTODO; // memcpy((void*)$R%d.i32; (void*)$R%d.i32, $R%d.i32);\n",
indent, dst, off, len,
)
case token.INS_MEMORY_COPY:
dst := stk.Pop(token.I32)
src := stk.Pop(token.I32)
len := stk.Pop(token.I32)
fmt.Fprintf(w, "%smemcpy((void*)$R%d.i32; (void*)$R%d.i32, $R%d.i32);\n",
indent, dst, src, len,
)
case token.INS_MEMORY_FILL:
dst := stk.Pop(token.I32)
val := stk.Pop(token.I32)
len := stk.Pop(token.I32)
fmt.Fprintf(w, "%smemset((void*)$R%d.i32; $R%d.i32, $R%d.i32);\n",
indent, dst, val, len,
)
case token.INS_I32_CONST:
i := i.(ast.Ins_I32Const)
sp0 := stk.Push(token.I32)
Expand Down
9 changes: 9 additions & 0 deletions internal/wat/watutil/wat2wasm_instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ func (p *wat2wasmWorker) buildInstruction(dst *wasm.Code, fn *ast.Func, i ast.In
dst.Body = append(dst.Body, wasm.OpcodeMemorySize, 0x00)
case token.INS_MEMORY_GROW:
dst.Body = append(dst.Body, wasm.OpcodeMemoryGrow, 0x00)
case token.INS_MEMORY_INIT:
ins := i.(ast.Ins_MemoryInit)
dst.Body = append(dst.Body, 0xfc, wasm.OpcodeMiscMemoryInit)
dst.Body = append(dst.Body, p.encodeUint32(uint32(ins.DataIdx))...)
dst.Body = append(dst.Body, 0x00)
case token.INS_MEMORY_COPY:
dst.Body = append(dst.Body, 0xfc, wasm.OpcodeMiscMemoryCopy, 0x00, 0x00)
case token.INS_MEMORY_FILL:
dst.Body = append(dst.Body, 0xfc, wasm.OpcodeMiscMemoryFill, 0x00)
case token.INS_I32_CONST:
ins := i.(ast.Ins_I32Const)
dst.Body = append(dst.Body, wasm.OpcodeI32Const)
Expand Down

0 comments on commit dce7e9e

Please sign in to comment.