summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Clegg <sbc@chromium.org>2022-03-02 19:28:56 -0800
committerGitHub <noreply@github.com>2022-03-02 19:28:56 -0800
commit1c8efb359749c40cbe31f49af9459e2fdf48b5a8 (patch)
tree946976f49c4e9511e053fe97a27d0cbaacf2cca9
parentb1ea4ad34e942f6bc42b156e21634f0871027704 (diff)
downloadwabt-1c8efb359749c40cbe31f49af9459e2fdf48b5a8.tar.gz
wabt-1c8efb359749c40cbe31f49af9459e2fdf48b5a8.tar.bz2
wabt-1c8efb359749c40cbe31f49af9459e2fdf48b5a8.zip
Fix compiler warnings in wasm2c output (#1844)
Use stricter compiler settings and fix the resulting warnings. This is precursor to landing windows compiler support for wasm2c. See #1843.
-rw-r--r--src/c-writer.cc57
-rw-r--r--src/prebuilt/wasm2c.include.c46
-rw-r--r--src/prebuilt/wasm2c.include.h3
-rw-r--r--src/wasm2c.c.tmpl46
-rw-r--r--src/wasm2c.h.tmpl3
-rwxr-xr-xtest/run-spec-wasm2c.py11
-rw-r--r--wasm2c/wasm-rt-impl.h2
7 files changed, 103 insertions, 65 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
diff --git a/test/run-spec-wasm2c.py b/test/run-spec-wasm2c.py
index b3e66c6a..8da98461 100755
--- a/test/run-spec-wasm2c.py
+++ b/test/run-spec-wasm2c.py
@@ -335,7 +335,11 @@ class CWriter(object):
def Compile(cc, c_filename, out_dir, *args):
o_filename = utils.ChangeDir(utils.ChangeExt(c_filename, '.o'), out_dir)
- cc.RunWithArgs('-c', c_filename, '-o', o_filename, *args)
+ cc.RunWithArgs('-c', c_filename, '-o', o_filename,
+ '-Wall', '-Werror', '-Wno-unused',
+ '-Wno-tautological-constant-out-of-range-compare',
+ '-std=c99', '-D_DEFAULT_SOURCE',
+ *args)
return o_filename
@@ -401,7 +405,8 @@ def main(args):
wasm2c.AppendOptionalArgs({
'--enable-multi-memory': options.enable_multi_memory})
- cc = utils.Executable(options.cc, *options.cflags)
+ cc = utils.Executable(options.cc, *options.cflags, forward_stderr=True,
+ forward_stdout=True)
cc.verbose = options.print_cmd
with open(json_file_path) as json_file:
@@ -436,7 +441,7 @@ def main(args):
o_filenames.append(Compile(cc, c_filename, out_dir, includes, defines))
if options.compile:
- o_filenames.append(Compile(cc, main_filename, out_dir, includes, defines))
+ o_filenames.append(Compile(cc, main_filename, out_dir, includes))
main_exe = utils.ChangeExt(json_file_path, '')
Link(cc, o_filenames, main_exe, '-lm')
diff --git a/wasm2c/wasm-rt-impl.h b/wasm2c/wasm-rt-impl.h
index 39d5ed93..e784fb24 100644
--- a/wasm2c/wasm-rt-impl.h
+++ b/wasm2c/wasm-rt-impl.h
@@ -62,4 +62,4 @@ extern uint32_t g_saved_call_stack_depth;
}
#endif
-#endif // WASM_RT_IMPL_H_
+#endif /* WASM_RT_IMPL_H_ */