diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/c-writer.cc | 57 | ||||
-rw-r--r-- | src/prebuilt/wasm2c.include.c | 46 | ||||
-rw-r--r-- | src/prebuilt/wasm2c.include.h | 3 | ||||
-rw-r--r-- | src/wasm2c.c.tmpl | 46 | ||||
-rw-r--r-- | src/wasm2c.h.tmpl | 3 |
5 files changed, 94 insertions, 61 deletions
diff --git a/src/c-writer.cc b/src/c-writer.cc index 388d8168..cf0a2d71 100644 --- a/src/c-writer.cc +++ b/src/c-writer.cc @@ -845,10 +845,15 @@ void CWriter::WriteMultivalueTypes() { } void CWriter::WriteFuncTypes() { - Write(Newline()); - Writef("static u32 func_types[%" PRIzd "];", module_->types.size()); - Write(Newline(), Newline()); + if (module_->types.size()) { + Writef("static u32 func_types[%" PRIzd "];", module_->types.size()); + Write(Newline()); + } Write("static void init_func_types(void) {", Newline()); + if (!module_->types.size()) { + Write("}", Newline()); + return; + } Index func_type_index = 0; for (TypeEntry* type : module_->types) { FuncType* func_type = cast<FuncType>(type); @@ -1017,8 +1022,15 @@ void CWriter::WriteMemory(const std::string& name) { } void CWriter::WriteTables() { - if (module_->tables.size() == module_->num_table_imports) + if (module_->tables.size() == module_->num_table_imports) { return; + } + + if (!module_->types.size()) { + // If no types are defined then there is no way to use the table + // for anything. + return; + } Write(Newline()); @@ -1048,17 +1060,22 @@ void CWriter::WriteDataInitializers() { Write(Newline()); } else { for (const DataSegment* data_segment : module_->data_segments) { - Write(Newline(), "static const u8 data_segment_data_", - data_segment_index, "[] = ", OpenBrace()); - size_t i = 0; - for (uint8_t x : data_segment->data) { - Writef("0x%02x, ", x); - if ((++i % 12) == 0) + if (!data_segment->data.size()) { + Write(Newline(), "static const u8* data_segment_data_", + data_segment_index, " = NULL;", Newline()); + } else { + Write(Newline(), "static const u8 data_segment_data_", + data_segment_index, "[] = ", OpenBrace()); + size_t i = 0; + for (uint8_t x : data_segment->data) { + Writef("0x%02x, ", x); + if ((++i % 12) == 0) + Write(Newline()); + } + if (i > 0) Write(Newline()); + Write(CloseBrace(), ";", Newline()); } - if (i > 0) - Write(Newline()); - Write(CloseBrace(), ";", Newline()); ++data_segment_index; } } @@ -1082,7 +1099,8 @@ void CWriter::WriteDataInitializers() { Write("LOAD_DATA(", ExternalRef(memory->name), ", "); WriteInitExpr(data_segment->offset); Write(", data_segment_data_", data_segment_index, ", ", - data_segment->data.size(), ");", Newline()); + data_segment->data.size()); + Write(");", Newline()); ++data_segment_index; } @@ -1090,9 +1108,18 @@ void CWriter::WriteDataInitializers() { } void CWriter::WriteElemInitializers() { + Write(Newline(), "static void init_table(void) ", OpenBrace()); + + if (!module_->types.size()) { + // If there are no types there cannot be any table entries either. + for (const ElemSegment* elem_segment : module_->elem_segments) { + assert(elem_segment->elem_exprs.size() == 0); + } + Write(CloseBrace(), Newline()); + return; + } const Table* table = module_->tables.empty() ? nullptr : module_->tables[0]; - Write(Newline(), "static void init_table(void) ", OpenBrace()); Write("uint32_t offset;", Newline()); if (table && module_->num_table_imports == 0) { uint32_t max = diff --git a/src/prebuilt/wasm2c.include.c b/src/prebuilt/wasm2c.include.c index a004a10e..bb23139b 100644 --- a/src/prebuilt/wasm2c.include.c +++ b/src/prebuilt/wasm2c.include.c @@ -87,29 +87,29 @@ const char SECTION_NAME(declarations)[] = " }\n" "#endif\n" "\n" -"DEFINE_LOAD(i32_load, u32, u32, u32);\n" -"DEFINE_LOAD(i64_load, u64, u64, u64);\n" -"DEFINE_LOAD(f32_load, f32, f32, f32);\n" -"DEFINE_LOAD(f64_load, f64, f64, f64);\n" -"DEFINE_LOAD(i32_load8_s, s8, s32, u32);\n" -"DEFINE_LOAD(i64_load8_s, s8, s64, u64);\n" -"DEFINE_LOAD(i32_load8_u, u8, u32, u32);\n" -"DEFINE_LOAD(i64_load8_u, u8, u64, u64);\n" -"DEFINE_LOAD(i32_load16_s, s16, s32, u32);\n" -"DEFINE_LOAD(i64_load16_s, s16, s64, u64);\n" -"DEFINE_LOAD(i32_load16_u, u16, u32, u32);\n" -"DEFINE_LOAD(i64_load16_u, u16, u64, u64);\n" -"DEFINE_LOAD(i64_load32_s, s32, s64, u64);\n" -"DEFINE_LOAD(i64_load32_u, u32, u64, u64);\n" -"DEFINE_STORE(i32_store, u32, u32);\n" -"DEFINE_STORE(i64_store, u64, u64);\n" -"DEFINE_STORE(f32_store, f32, f32);\n" -"DEFINE_STORE(f64_store, f64, f64);\n" -"DEFINE_STORE(i32_store8, u8, u32);\n" -"DEFINE_STORE(i32_store16, u16, u32);\n" -"DEFINE_STORE(i64_store8, u8, u64);\n" -"DEFINE_STORE(i64_store16, u16, u64);\n" -"DEFINE_STORE(i64_store32, u32, u64);\n" +"DEFINE_LOAD(i32_load, u32, u32, u32)\n" +"DEFINE_LOAD(i64_load, u64, u64, u64)\n" +"DEFINE_LOAD(f32_load, f32, f32, f32)\n" +"DEFINE_LOAD(f64_load, f64, f64, f64)\n" +"DEFINE_LOAD(i32_load8_s, s8, s32, u32)\n" +"DEFINE_LOAD(i64_load8_s, s8, s64, u64)\n" +"DEFINE_LOAD(i32_load8_u, u8, u32, u32)\n" +"DEFINE_LOAD(i64_load8_u, u8, u64, u64)\n" +"DEFINE_LOAD(i32_load16_s, s16, s32, u32)\n" +"DEFINE_LOAD(i64_load16_s, s16, s64, u64)\n" +"DEFINE_LOAD(i32_load16_u, u16, u32, u32)\n" +"DEFINE_LOAD(i64_load16_u, u16, u64, u64)\n" +"DEFINE_LOAD(i64_load32_s, s32, s64, u64)\n" +"DEFINE_LOAD(i64_load32_u, u32, u64, u64)\n" +"DEFINE_STORE(i32_store, u32, u32)\n" +"DEFINE_STORE(i64_store, u64, u64)\n" +"DEFINE_STORE(f32_store, f32, f32)\n" +"DEFINE_STORE(f64_store, f64, f64)\n" +"DEFINE_STORE(i32_store8, u8, u32)\n" +"DEFINE_STORE(i32_store16, u16, u32)\n" +"DEFINE_STORE(i64_store8, u8, u64)\n" +"DEFINE_STORE(i64_store16, u16, u64)\n" +"DEFINE_STORE(i64_store32, u32, u64)\n" "\n" "#define I32_CLZ(x) ((x) ? __builtin_clz(x) : 32)\n" "#define I64_CLZ(x) ((x) ? __builtin_clzll(x) : 64)\n" diff --git a/src/prebuilt/wasm2c.include.h b/src/prebuilt/wasm2c.include.h index 4ba24751..97d4aea5 100644 --- a/src/prebuilt/wasm2c.include.h +++ b/src/prebuilt/wasm2c.include.h @@ -18,6 +18,8 @@ const char SECTION_NAME(top)[] = "#define WASM_RT_ADD_PREFIX(x) WASM_RT_PASTE(WASM_RT_MODULE_PREFIX, x)\n" "\n" "/* TODO(binji): only use stdint.h types in header */\n" +"#ifndef WASM_RT_CORE_TYPES_DEFINED\n" +"#define WASM_RT_CORE_TYPES_DEFINED\n" "typedef uint8_t u8;\n" "typedef int8_t s8;\n" "typedef uint16_t u16;\n" @@ -28,6 +30,7 @@ const char SECTION_NAME(top)[] = "typedef int64_t s64;\n" "typedef float f32;\n" "typedef double f64;\n" +"#endif\n" "\n" "extern void WASM_RT_ADD_PREFIX(init)(void);\n" ; diff --git a/src/wasm2c.c.tmpl b/src/wasm2c.c.tmpl index 2637ca13..f6bcc941 100644 --- a/src/wasm2c.c.tmpl +++ b/src/wasm2c.c.tmpl @@ -84,29 +84,29 @@ static inline void load_data(void *dest, const void *src, size_t n) { } #endif -DEFINE_LOAD(i32_load, u32, u32, u32); -DEFINE_LOAD(i64_load, u64, u64, u64); -DEFINE_LOAD(f32_load, f32, f32, f32); -DEFINE_LOAD(f64_load, f64, f64, f64); -DEFINE_LOAD(i32_load8_s, s8, s32, u32); -DEFINE_LOAD(i64_load8_s, s8, s64, u64); -DEFINE_LOAD(i32_load8_u, u8, u32, u32); -DEFINE_LOAD(i64_load8_u, u8, u64, u64); -DEFINE_LOAD(i32_load16_s, s16, s32, u32); -DEFINE_LOAD(i64_load16_s, s16, s64, u64); -DEFINE_LOAD(i32_load16_u, u16, u32, u32); -DEFINE_LOAD(i64_load16_u, u16, u64, u64); -DEFINE_LOAD(i64_load32_s, s32, s64, u64); -DEFINE_LOAD(i64_load32_u, u32, u64, u64); -DEFINE_STORE(i32_store, u32, u32); -DEFINE_STORE(i64_store, u64, u64); -DEFINE_STORE(f32_store, f32, f32); -DEFINE_STORE(f64_store, f64, f64); -DEFINE_STORE(i32_store8, u8, u32); -DEFINE_STORE(i32_store16, u16, u32); -DEFINE_STORE(i64_store8, u8, u64); -DEFINE_STORE(i64_store16, u16, u64); -DEFINE_STORE(i64_store32, u32, u64); +DEFINE_LOAD(i32_load, u32, u32, u32) +DEFINE_LOAD(i64_load, u64, u64, u64) +DEFINE_LOAD(f32_load, f32, f32, f32) +DEFINE_LOAD(f64_load, f64, f64, f64) +DEFINE_LOAD(i32_load8_s, s8, s32, u32) +DEFINE_LOAD(i64_load8_s, s8, s64, u64) +DEFINE_LOAD(i32_load8_u, u8, u32, u32) +DEFINE_LOAD(i64_load8_u, u8, u64, u64) +DEFINE_LOAD(i32_load16_s, s16, s32, u32) +DEFINE_LOAD(i64_load16_s, s16, s64, u64) +DEFINE_LOAD(i32_load16_u, u16, u32, u32) +DEFINE_LOAD(i64_load16_u, u16, u64, u64) +DEFINE_LOAD(i64_load32_s, s32, s64, u64) +DEFINE_LOAD(i64_load32_u, u32, u64, u64) +DEFINE_STORE(i32_store, u32, u32) +DEFINE_STORE(i64_store, u64, u64) +DEFINE_STORE(f32_store, f32, f32) +DEFINE_STORE(f64_store, f64, f64) +DEFINE_STORE(i32_store8, u8, u32) +DEFINE_STORE(i32_store16, u16, u32) +DEFINE_STORE(i64_store8, u8, u64) +DEFINE_STORE(i64_store16, u16, u64) +DEFINE_STORE(i64_store32, u32, u64) #define I32_CLZ(x) ((x) ? __builtin_clz(x) : 32) #define I64_CLZ(x) ((x) ? __builtin_clzll(x) : 64) diff --git a/src/wasm2c.h.tmpl b/src/wasm2c.h.tmpl index 8478d81b..2a334fd7 100644 --- a/src/wasm2c.h.tmpl +++ b/src/wasm2c.h.tmpl @@ -17,6 +17,8 @@ extern "C" { #define WASM_RT_ADD_PREFIX(x) WASM_RT_PASTE(WASM_RT_MODULE_PREFIX, x) /* TODO(binji): only use stdint.h types in header */ +#ifndef WASM_RT_CORE_TYPES_DEFINED +#define WASM_RT_CORE_TYPES_DEFINED typedef uint8_t u8; typedef int8_t s8; typedef uint16_t u16; @@ -27,6 +29,7 @@ typedef uint64_t u64; typedef int64_t s64; typedef float f32; typedef double f64; +#endif extern void WASM_RT_ADD_PREFIX(init)(void); %%bottom |