Skip to content

Commit ac1e310

Browse files
committed
Block申请内存的入口统一至runtime.Block.HeapAlloc,并同时返回 b, d
1 parent 0a23e2b commit ac1e310

File tree

6 files changed

+18
-37
lines changed

6 files changed

+18
-37
lines changed

internal/backends/compiler_wat/js_binding_tmpl.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,17 @@ class WaApp {
2323
set_string: (s) => {
2424
const bytes = new TextEncoder("utf-8").encode(s);
2525
const l = bytes.length;
26-
const b = this._wasm_inst.exports["runtime.Block.HeapAlloc"](l, 0, 1);
27-
const d = b + 16;
28-
this._mem_util.mem_array_u8(d, l).set(bytes);
29-
return [b, d, l];
26+
const bd = this._wasm_inst.exports["runtime.Block.HeapAlloc"](l, 0, 1);
27+
this._mem_util.mem_array_u8(bd[1], l).set(bytes);
28+
return [bd[0], bd[1], l];
3029
},
3130
get_bytes: (d, l) => { return this._mem_util.mem_array_u8(d, l).slice(0); },
3231
set_bytes: (bytes) => {
3332
const l = bytes.length;
3433
const c = l;
35-
const b = this._wasm_inst.exports["runtime.Block.HeapAlloc"](l, 0, 1);
36-
const d = b + 16;
37-
this._mem_util.mem_array_u8(d, l).set(bytes);
38-
return [b, d, l, c];
34+
const bd = this._wasm_inst.exports["runtime.Block.HeapAlloc"](l, 0, 1);
35+
this._mem_util.mem_array_u8(bd[1], l).set(bytes);
36+
return [bd[0], bd[1], l, c];
3937
},
4038
block_release: (addr) => { this._wasm_inst.exports["runtime.Block.Release"](addr); },
4139
//基本类型直接读写:

internal/backends/compiler_wat/wir/value_block.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,33 +81,24 @@ func (t *Block) EmitLoadFromAddrNoRetain(addr Value, offset int) (insts []wat.In
8181
func (t *Block) emitHeapAlloc(item_count Value) (insts []wat.Inst) {
8282
switch item_count.Kind() {
8383
case ValueKindConst:
84-
c, err := strconv.Atoi(item_count.Name())
84+
_, err := strconv.Atoi(item_count.Name())
8585
if err != nil {
8686
logger.Fatalf("%v\n", err)
8787
return nil
8888
}
89-
insts = append(insts, NewConst(strconv.Itoa(t.Base.Size()*c+16), t._uint).EmitPush()...)
90-
insts = append(insts, wat.NewInstCall("runtime.HeapAlloc"))
9189

9290
default:
9391
if !item_count.Type().Equal(t._uint) && !item_count.Type().Equal(t._int) {
9492
logger.Fatal("item_count should be uint|int")
9593
return nil
9694
}
9795

98-
insts = append(insts, item_count.EmitPush()...)
99-
insts = append(insts, NewConst(strconv.Itoa(t.Base.Size()), t._uint).EmitPush()...)
100-
insts = append(insts, wat.NewInstMul(wat.U32{}))
101-
insts = append(insts, NewConst("16", t._uint).EmitPush()...)
102-
insts = append(insts, wat.NewInstAdd(wat.U32{}))
103-
insts = append(insts, wat.NewInstCall("runtime.HeapAlloc"))
104-
10596
}
10697

10798
insts = append(insts, item_count.EmitPush()...) //item_count
10899
insts = append(insts, NewConst(strconv.Itoa(t.Base.OnFree()), t._uint).EmitPush()...) //free_method
109100
insts = append(insts, NewConst(strconv.Itoa(t.Base.Size()), t._uint).EmitPush()...) //item_size
110-
insts = append(insts, wat.NewInstCall("runtime.Block.Init"))
101+
insts = append(insts, wat.NewInstCall("runtime.Block.HeapAlloc"))
111102

112103
return
113104
}

internal/backends/compiler_wat/wir/value_ref.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ func (t *Ref) emitHeapAlloc() (insts []wat.Inst) {
6868
//insts = append(insts, wat.NewComment("Ref.emitHeapAlloc start"))
6969

7070
insts = append(insts, t._base_block.emitHeapAlloc(NewConst("1", t.underlying._u32))...)
71-
insts = append(insts, wat.NewInstCall("runtime.DupI32"))
72-
insts = append(insts, NewConst("16", t.underlying._u32).EmitPush()...)
73-
insts = append(insts, wat.NewInstAdd(wat.U32{}))
7471

7572
//insts = append(insts, wat.NewComment("Ref.emitHeapAlloc end"))
7673
//insts = append(insts, wat.NewBlank())

internal/backends/compiler_wat/wir/value_slice.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,9 @@ func (t *Slice) emitGenFromRefOfArray(x *aRef, low, high, max Value) (insts []wa
151151
}
152152

153153
func (t *Slice) emitGenMake(Len, Cap Value) (insts []wat.Inst) {
154-
//block:
154+
//block, data:
155155
insts = append(insts, t._base_block.emitHeapAlloc(Cap)...)
156156

157-
//data
158-
insts = append(insts, wat.NewInstCall("runtime.DupI32"))
159-
insts = append(insts, NewConst("16", t._u32).EmitPush()...)
160-
insts = append(insts, wat.NewInstAdd(wat.U32{}))
161-
162157
//len:
163158
if !Len.Type().Equal(t._u32) && !Len.Type().Equal(t._i32) {
164159
logger.Fatal("Len should be u32")
@@ -285,11 +280,8 @@ func (t *Slice) genAppendFunc(m *Module) string {
285280
if_false = append(if_false, NewConst("2", t._u32).EmitPush()...)
286281
if_false = append(if_false, wat.NewInstMul(wat.U32{}))
287282
if_false = append(if_false, new_cap.EmitPop()...)
288-
if_false = append(if_false, t._base_block.emitHeapAlloc(new_cap)...) //block
283+
if_false = append(if_false, t._base_block.emitHeapAlloc(new_cap)...) //block, data
289284

290-
if_false = append(if_false, wat.NewInstCall("runtime.DupI32"))
291-
if_false = append(if_false, NewConst("16", t._u32).EmitPush()...)
292-
if_false = append(if_false, wat.NewInstAdd(wat.U32{})) //data
293285
if_false = append(if_false, wat.NewInstCall("runtime.DupI32"))
294286
if_false = append(if_false, dest.EmitPop()...) //dest
295287
if_false = append(if_false, new_len.EmitPush()...) //len

internal/backends/compiler_wat/wir/value_string.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,8 @@ func (t *String) genFunc_append(m *Module) string {
111111

112112
{ //if_false
113113
//gen new string
114-
f.Insts = append(f.Insts, t._u8_block.emitHeapAlloc(new_len)...) //block
114+
f.Insts = append(f.Insts, t._u8_block.emitHeapAlloc(new_len)...) // block, data
115115

116-
f.Insts = append(f.Insts, wat.NewInstCall("runtime.DupI32"))
117-
f.Insts = append(f.Insts, NewConst("16", t._u32).EmitPush()...)
118-
f.Insts = append(f.Insts, wat.NewInstAdd(wat.U32{})) //data
119116
f.Insts = append(f.Insts, wat.NewInstCall("runtime.DupI32"))
120117
f.Insts = append(f.Insts, dest.EmitPop()...) //dest
121118
f.Insts = append(f.Insts, new_len.EmitPush()...) //len

waroot/src/runtime/heap.wat.ws

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@
117117
end
118118
)
119119

120-
(func $runtime.Block.HeapAlloc (export "runtime.Block.HeapAlloc") (param $item_count i32) (param $release_func i32) (param $item_size i32) (result i32) ;;result = ptr_block
120+
(func $runtime.Block.HeapAlloc (export "runtime.Block.HeapAlloc") (param $item_count i32) (param $release_func i32) (param $item_size i32) (result i32 i32) ;;result = ptr_block, ptr_data
121+
(local $b i32)
121122
local.get $item_count
122123
local.get $item_size
123124
i32.mul
@@ -129,6 +130,11 @@
129130
local.get $release_func
130131
local.get $item_size
131132
call $runtime.Block.Init
133+
134+
local.tee $b
135+
local.get $b
136+
i32.const 16
137+
i32.add
132138
)
133139

134140
(func $runtime.DupI32 (param $a i32) (result i32 i32) ;;r0 = r1 = p0

0 commit comments

Comments
 (0)