Skip to content

Commit a0dcf45

Browse files
gekyOthers
authored andcommitted
Added codegen for memory.size and memory.grow instructions
These simply call the externally linked instruction_memory_grow and instruction_memory_size functions in the runtime. Once connected, these runtime functions can use the existing expand_memory logic that is specific to each runtime-memory implementation.
1 parent 2d654da commit a0dcf45

File tree

12 files changed

+143
-0
lines changed

12 files changed

+143
-0
lines changed

runtime/memory/64bit_nix.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ void expand_memory() {
3737
memory_size += WASM_PAGE_SIZE;
3838
}
3939

40+
i32 instruction_memory_size() {
41+
return memory_size / WASM_PAGE_SIZE;
42+
}
43+
44+
i32 instruction_memory_grow(i32 count) {
45+
i32 prev_size = instruction_memory_size();
46+
for (int i = 0; i < count; i++) {
47+
expand_memory();
48+
}
49+
50+
return prev_size;
51+
}
52+
4053
INLINE char* get_memory_ptr_for_runtime(u32 offset, u32 bounds_check) {
4154
// Due to how we setup memory for x86, the virtual memory mechanism will catch the error, if bounds < WASM_PAGE_SIZE
4255
assert(bounds_check < WASM_PAGE_SIZE || (memory_size > bounds_check && offset <= memory_size - bounds_check));

runtime/memory/cortex_m.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ void expand_memory() {
3232
memory_size += WASM_PAGE_SIZE;
3333
}
3434

35+
i32 instruction_memory_size() {
36+
return memory_size / WASM_PAGE_SIZE;
37+
}
38+
39+
i32 instruction_memory_grow(i32 count) {
40+
i32 prev_size = instruction_memory_size();
41+
for (int i = 0; i < count; i++) {
42+
expand_memory();
43+
}
44+
45+
return prev_size;
46+
}
47+
3548
INLINE char* get_memory_ptr_for_runtime(u32 offset, u32 bounds_check) {
3649
silverfish_assert(offset <= memory_size - bounds_check);
3750

runtime/memory/cortex_m_no_protection.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ void expand_memory() {
3333
memory_size += WASM_PAGE_SIZE;
3434
}
3535

36+
i32 instruction_memory_size() {
37+
return memory_size / WASM_PAGE_SIZE;
38+
}
39+
40+
i32 instruction_memory_grow(i32 count) {
41+
i32 prev_size = instruction_memory_size();
42+
for (int i = 0; i < count; i++) {
43+
expand_memory();
44+
}
45+
46+
return prev_size;
47+
}
48+
3649
INLINE char* get_memory_ptr_for_runtime(u32 offset, u32 bounds_check) {
3750
char* mem_as_chars = (char *) memory;
3851
char* address = &mem_as_chars[offset];

runtime/memory/cortex_m_spt.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ void expand_memory() {
4343
memory_size += WASM_PAGE_SIZE;
4444
}
4545

46+
i32 instruction_memory_size() {
47+
return memory_size / WASM_PAGE_SIZE;
48+
}
49+
50+
i32 instruction_memory_grow(i32 count) {
51+
i32 prev_size = instruction_memory_size();
52+
for (int i = 0; i < count; i++) {
53+
expand_memory();
54+
}
55+
56+
return prev_size;
57+
}
58+
4659
INLINE char* get_memory_ptr_for_runtime(u32 offset, u32 bounds_check) {
4760
char* mem_as_chars = (char *) memory;
4861
char* address = &mem_as_chars[offset];

runtime/memory/cortex_m_wrapping.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ void expand_memory() {
2727
memory_size += WASM_PAGE_SIZE;
2828
}
2929

30+
i32 instruction_memory_size() {
31+
return memory_size / WASM_PAGE_SIZE;
32+
}
33+
34+
i32 instruction_memory_grow(i32 count) {
35+
i32 prev_size = instruction_memory_size();
36+
for (int i = 0; i < count; i++) {
37+
expand_memory();
38+
}
39+
40+
return prev_size;
41+
}
42+
3043
INLINE char* get_memory_ptr_for_runtime(u32 offset, u32 bounds_check) {
3144
silverfish_assert(offset <= memory_size - bounds_check);
3245

runtime/memory/generic.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ void expand_memory() {
2222
memory_size += WASM_PAGE_SIZE;
2323
}
2424

25+
i32 instruction_memory_size() {
26+
return memory_size / WASM_PAGE_SIZE;
27+
}
28+
29+
i32 instruction_memory_grow(i32 count) {
30+
i32 prev_size = instruction_memory_size();
31+
for (int i = 0; i < count; i++) {
32+
expand_memory();
33+
}
34+
35+
return prev_size;
36+
}
37+
2538
INLINE char* get_memory_ptr_for_runtime(u32 offset, u32 bounds_check) {
2639
silverfish_assert(memory_size > bounds_check && offset <= memory_size - bounds_check);
2740

runtime/memory/mpx.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ void expand_memory() {
2323
memory_size += WASM_PAGE_SIZE;
2424
}
2525

26+
i32 instruction_memory_size() {
27+
return memory_size / WASM_PAGE_SIZE;
28+
}
29+
30+
i32 instruction_memory_grow(i32 count) {
31+
i32 prev_size = instruction_memory_size();
32+
for (int i = 0; i < count; i++) {
33+
expand_memory();
34+
}
35+
36+
return prev_size;
37+
}
38+
2639
INLINE char* get_memory_ptr_for_runtime(u32 offset, u32 bounds_check) {
2740
assert(memory_size > bounds_check && offset <= memory_size - bounds_check);
2841

runtime/memory/no_protection.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ void expand_memory() {
2020
memory_size += WASM_PAGE_SIZE;
2121
}
2222

23+
i32 instruction_memory_size() {
24+
return memory_size / WASM_PAGE_SIZE;
25+
}
26+
27+
i32 instruction_memory_grow(i32 count) {
28+
i32 prev_size = instruction_memory_size();
29+
for (int i = 0; i < count; i++) {
30+
expand_memory();
31+
}
32+
33+
return prev_size;
34+
}
35+
2336
INLINE char* get_memory_ptr_for_runtime(u32 offset, u32 bounds_check) {
2437
char* mem_as_chars = (char *) memory;
2538
return &mem_as_chars[offset];

runtime/memory/segmented.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ void expand_memory() {
8787
set_seg_registers();
8888
}
8989

90+
i32 instruction_memory_size() {
91+
return memory_size / WASM_PAGE_SIZE;
92+
}
93+
94+
i32 instruction_memory_grow(i32 count) {
95+
i32 prev_size = instruction_memory_size();
96+
for (int i = 0; i < count; i++) {
97+
expand_memory();
98+
}
99+
100+
return prev_size;
101+
}
102+
90103
INLINE char* get_memory_ptr_for_runtime(u32 offset, u32 bounds_check) {
91104
assert(memory_size > bounds_check && offset <= memory_size - bounds_check);
92105

src/codegen/block.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,17 @@ pub fn compile_block<'a, 'b>(
10321032
let v = stack.pop().unwrap();
10331033
store_val::<f64>(m_ctx, b, &mut stack, offset, v);
10341034
}
1035+
1036+
Instruction::MemorySize => {
1037+
let result = b.build_call(get_stub_function(m_ctx, MEMORY_SIZE), &[]);
1038+
stack.push(result);
1039+
}
1040+
Instruction::MemoryGrow => {
1041+
let v = stack.pop().unwrap();
1042+
assert_type(m_ctx, v, Type::I32);
1043+
let result = b.build_call(get_stub_function(m_ctx, MEMORY_GROW), &[v]);
1044+
stack.push(result);
1045+
}
10351046
}
10361047
}
10371048
}

0 commit comments

Comments
 (0)