Skip to content

Commit

Permalink
Fix compiler warnings in wasm2c output (#1844)
Browse files Browse the repository at this point in the history
Use stricter compiler settings and fix the resulting warnings.
This is precursor to landing windows compiler support for wasm2c.
See #1843.
  • Loading branch information
sbc100 authored Mar 3, 2022
1 parent b1ea4ad commit 1c8efb3
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 65 deletions.
57 changes: 42 additions & 15 deletions src/c-writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -1082,17 +1099,27 @@ 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;
}

Write(CloseBrace(), Newline());
}

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 =
Expand Down
46 changes: 23 additions & 23 deletions src/prebuilt/wasm2c.include.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions src/prebuilt/wasm2c.include.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
;
Expand Down
46 changes: 23 additions & 23 deletions src/wasm2c.c.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/wasm2c.h.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions test/run-spec-wasm2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,11 @@ def _Action(self, command):

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


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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')

Expand Down
2 changes: 1 addition & 1 deletion wasm2c/wasm-rt-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ extern uint32_t g_saved_call_stack_depth;
}
#endif

#endif // WASM_RT_IMPL_H_
#endif /* WASM_RT_IMPL_H_ */

0 comments on commit 1c8efb3

Please sign in to comment.