Skip to content

Commit d75737b

Browse files
authored
wasm2c: support the custom-page-sizes proposal (#2508)
1 parent 40be47d commit d75737b

File tree

15 files changed

+99
-60
lines changed

15 files changed

+99
-60
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Wabt has been compiled to JavaScript via emscripten. Some of the functionality i
6262
| [multi-memory][] | `--enable-multi-memory` | ||||||
6363
| [extended-const][] | `--enable-extended-const` | ||||||
6464
| [relaxed-simd][] | `--enable-relaxed-simd` | ||||| |
65-
| [custom-page-sizes][] | `--enable-custom-page-sizes`| ||||| |
65+
| [custom-page-sizes][] | `--enable-custom-page-sizes`| ||||| |
6666

6767
[exception handling]: https://github.com/WebAssembly/exception-handling
6868
[mutable globals]: https://github.com/WebAssembly/mutable-global

src/c-writer.cc

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,18 +2248,17 @@ void CWriter::WriteDataInitializers() {
22482248
Index memory_idx = module_->num_memory_imports;
22492249
for (Index i = memory_idx; i < module_->memories.size(); i++) {
22502250
const Memory* memory = module_->memories[i];
2251-
uint64_t max;
2252-
if (memory->page_limits.has_max) {
2253-
max = memory->page_limits.max;
2254-
} else {
2255-
max = memory->page_limits.is_64 ? (static_cast<uint64_t>(1) << 48)
2256-
: 65536;
2257-
}
2251+
const uint64_t max =
2252+
memory->page_limits.has_max
2253+
? memory->page_limits.max
2254+
: WABT_BYTES_TO_MIN_PAGES(
2255+
(memory->page_limits.is_64 ? UINT64_MAX : UINT32_MAX),
2256+
memory->page_size);
22582257
std::string func = GetMemoryAPIString(*memory, "wasm_rt_allocate_memory");
2259-
Write(func, "(",
2260-
ExternalInstancePtr(ModuleFieldType::Memory, memory->name), ", ",
2261-
memory->page_limits.initial, ", ", max, ", ",
2262-
memory->page_limits.is_64, ");", Newline());
2258+
Write(
2259+
func, "(", ExternalInstancePtr(ModuleFieldType::Memory, memory->name),
2260+
", ", memory->page_limits.initial, ", ", max, ", ",
2261+
memory->page_limits.is_64, ", ", memory->page_size, ");", Newline());
22632262
}
22642263
}
22652264

@@ -2898,6 +2897,8 @@ void CWriter::WriteImportProperties(CWriterPhase kind) {
28982897
write_import_prop(import, "max", "u64",
28992898
limits->has_max ? limits->max : default_max);
29002899
write_import_prop(import, "is64", "u8", limits->is_64);
2900+
write_import_prop(import, "pagesize", "u32",
2901+
cast<MemoryImport>(import)->memory.page_size);
29012902
} else if (import->kind() == ExternalKind::Table) {
29022903
const Limits* limits = &(cast<TableImport>(import)->table.elem_limits);
29032904
const uint64_t default_max = std::numeric_limits<uint32_t>::max();

src/tools/wasm2c.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ static const char s_description[] =
5858
)";
5959

6060
static const std::string supported_features[] = {
61-
"multi-memory", "multi-value", "sign-extension", "saturating-float-to-int",
62-
"exceptions", "memory64", "extended-const", "simd",
63-
"threads", "tail-call"};
61+
"multi-memory", "multi-value",
62+
"sign-extension", "saturating-float-to-int",
63+
"exceptions", "memory64",
64+
"extended-const", "simd",
65+
"threads", "tail-call",
66+
"custom-page-sizes"};
6467

6568
static bool IsFeatureSupported(const std::string& feature) {
6669
return std::find(std::begin(supported_features), std::end(supported_features),

test/run-spec-wasm2c.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ def main(args):
551551
parser.add_argument('--enable-extended-const', action='store_true')
552552
parser.add_argument('--enable-threads', action='store_true')
553553
parser.add_argument('--enable-tail-call', action='store_true')
554+
parser.add_argument('--enable-custom-page-sizes', action='store_true')
554555
parser.add_argument('--disable-bulk-memory', action='store_true')
555556
parser.add_argument('--disable-reference-types', action='store_true')
556557
parser.add_argument('--debug-names', action='store_true')
@@ -572,6 +573,7 @@ def main(args):
572573
'--enable-extended-const': options.enable_extended_const,
573574
'--enable-threads': options.enable_threads,
574575
'--enable-tail-call': options.enable_tail_call,
576+
'--enable-custom-page-sizes': options.enable_custom_page_sizes,
575577
'--enable-multi-memory': options.enable_multi_memory,
576578
'--disable-bulk-memory': options.disable_bulk_memory,
577579
'--disable-reference-types': options.disable_reference_types,
@@ -591,6 +593,7 @@ def main(args):
591593
'--enable-extended-const': options.enable_extended_const,
592594
'--enable-threads': options.enable_threads,
593595
'--enable-tail-call': options.enable_tail_call,
596+
'--enable-custom-page-sizes': options.enable_custom_page_sizes,
594597
'--enable-multi-memory': options.enable_multi_memory})
595598

596599
options.cflags += shlex.split(os.environ.get('WASM2C_CFLAGS', ''))

test/spec-wasm2c-prefix.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ static void init_spectest_module(w2c_spectest* instance) {
427427
instance->spectest_global_i64 = 666l;
428428
instance->spectest_global_f32 = 666.6;
429429
instance->spectest_global_f64 = 666.6;
430-
wasm_rt_allocate_memory(&instance->spectest_memory, 1, 2, false);
430+
wasm_rt_allocate_memory(&instance->spectest_memory, 1, 2, false,
431+
WASM_DEFAULT_PAGE_SIZE);
431432
wasm_rt_allocate_funcref_table(&instance->spectest_table, 10, 20);
432433
wasm_rt_allocate_funcref_table(&instance->spectest_table64, 10, 20);
433434
}

test/wasm2c/check-imports.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ extern const u32 wasm2c_test_max_env_0x5F_indirect_function_table;
6363
extern const u64 wasm2c_test_min_env_0x5F_linear_memory;
6464
extern const u64 wasm2c_test_max_env_0x5F_linear_memory;
6565
extern const u8 wasm2c_test_is64_env_0x5F_linear_memory;
66+
extern const u32 wasm2c_test_pagesize_env_0x5F_linear_memory;
6667

6768
#ifdef __cplusplus
6869
}
@@ -857,6 +858,7 @@ const u32 wasm2c_test_max_env_0x5F_indirect_function_table = 4294967295;
857858
const u64 wasm2c_test_min_env_0x5F_linear_memory = 0;
858859
const u64 wasm2c_test_max_env_0x5F_linear_memory = 65536;
859860
const u8 wasm2c_test_is64_env_0x5F_linear_memory = 0;
861+
const u32 wasm2c_test_pagesize_env_0x5F_linear_memory = 65536;
860862

861863
void wasm2c_test_instantiate(w2c_test* instance, struct w2c_env* w2c_env_instance) {
862864
assert(wasm_rt_is_initialized());

test/wasm2c/export-names.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ wasm_rt_func_type_t wasm2c_test_get_func_type(uint32_t param_count, uint32_t res
4848
extern const u64 wasm2c_test_min_0x5Cmodule_import0x200x2A0x2F;
4949
extern const u64 wasm2c_test_max_0x5Cmodule_import0x200x2A0x2F;
5050
extern const u8 wasm2c_test_is64_0x5Cmodule_import0x200x2A0x2F;
51+
extern const u32 wasm2c_test_pagesize_0x5Cmodule_import0x200x2A0x2F;
5152

5253
/* export: '' */
5354
void w2c_test_(w2c_test*);
@@ -905,6 +906,7 @@ static void init_instance_import(w2c_test* instance, struct w2c_0x5Cmodule* w2c_
905906
const u64 wasm2c_test_min_0x5Cmodule_import0x200x2A0x2F = 0;
906907
const u64 wasm2c_test_max_0x5Cmodule_import0x200x2A0x2F = 65536;
907908
const u8 wasm2c_test_is64_0x5Cmodule_import0x200x2A0x2F = 0;
909+
const u32 wasm2c_test_pagesize_0x5Cmodule_import0x200x2A0x2F = 65536;
908910

909911
void wasm2c_test_instantiate(w2c_test* instance, struct w2c_0x5Cmodule* w2c_0x5Cmodule_instance) {
910912
assert(wasm_rt_is_initialized());

test/wasm2c/hello.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ static const u8 data_segment_data_w2c_test_d0[] = {
845845
};
846846

847847
static void init_memories(w2c_test* instance) {
848-
wasm_rt_allocate_memory(&instance->w2c_memory, 1, 65536, 0);
848+
wasm_rt_allocate_memory(&instance->w2c_memory, 1, 65536, 0, 65536);
849849
LOAD_DATA(instance->w2c_memory, 8u, data_segment_data_w2c_test_d0, 14);
850850
}
851851

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
;;; TOOL: run-spec-wasm2c
2+
;;; ARGS*: --enable-custom-page-sizes --enable-multi-memory
3+
;;; STDIN_FILE: third_party/testsuite/proposals/custom-page-sizes/custom-page-sizes-invalid.wast
4+
(;; STDOUT ;;;
5+
0/0 tests passed.
6+
;;; STDOUT ;;)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
;;; TOOL: run-spec-wasm2c
2+
;;; ARGS*: --enable-custom-page-sizes --enable-multi-memory
3+
;;; STDIN_FILE: third_party/testsuite/proposals/custom-page-sizes/custom-page-sizes.wast
4+
(;; STDOUT ;;;
5+
24/24 tests passed.
6+
;;; STDOUT ;;)

0 commit comments

Comments
 (0)