summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/wasm-apply-names.c5
-rw-r--r--src/wasm-ast-checker.c99
-rw-r--r--src/wasm-ast-parser-lexer-shared.c2
-rw-r--r--src/wasm-ast-parser.y34
-rw-r--r--src/wasm-ast-writer.c4
-rw-r--r--src/wasm-ast.h53
-rw-r--r--src/wasm-binary-reader-ast.c6
-rw-r--r--src/wasm-binary-writer-spec.c2
-rw-r--r--src/wasm-binary-writer.c76
-rw-r--r--test/parse/func/bad-sig-result-type-mismatch.txt4
-rw-r--r--test/parse/func/bad-sig-result-type-not-void.txt5
-rw-r--r--test/parse/func/bad-sig-result-type-void.txt5
12 files changed, 161 insertions, 134 deletions
diff --git a/src/wasm-apply-names.c b/src/wasm-apply-names.c
index 3f14cfcf..1489e134 100644
--- a/src/wasm-apply-names.c
+++ b/src/wasm-apply-names.c
@@ -115,9 +115,10 @@ static WasmResult use_name_for_param_and_local_var(Context* ctx,
WasmVar* var) {
int local_index = wasm_get_local_index_by_var(func, var);
assert(local_index >= 0 &&
- (size_t)local_index < wasm_get_num_params_and_locals(func));
+ (size_t)local_index <
+ wasm_get_num_params_and_locals(ctx->module, func));
- uint32_t num_params = wasm_get_num_params(func);
+ uint32_t num_params = wasm_get_num_params(ctx->module, func);
WasmStringSlice* name;
if ((uint32_t)local_index < num_params) {
/* param */
diff --git a/src/wasm-ast-checker.c b/src/wasm-ast-checker.c
index 9729e05a..dbd63712 100644
--- a/src/wasm-ast-checker.c
+++ b/src/wasm-ast-checker.c
@@ -227,13 +227,15 @@ static WasmResult check_func_type_var(Context* ctx,
static WasmResult check_local_var(Context* ctx,
const WasmVar* var,
WasmType* out_type) {
- int max_index = wasm_get_num_params_and_locals(ctx->current_func);
- int index = wasm_get_local_index_by_var(ctx->current_func, var);
+ const WasmModule* module = ctx->current_module;
+ const WasmFunc* func = ctx->current_func;
+ int max_index = wasm_get_num_params_and_locals(module, func);
+ int index = wasm_get_local_index_by_var(func, var);
if (index >= 0 && index < max_index) {
if (out_type) {
- int num_params = wasm_get_num_params(ctx->current_func);
+ int num_params = wasm_get_num_params(module, func);
if (index < num_params) {
- *out_type = wasm_get_param_type(ctx->current_func, index);
+ *out_type = wasm_get_param_type(module, func, index);
} else {
*out_type = ctx->current_func->local_types.data[index - num_params];
}
@@ -506,12 +508,17 @@ static void check_expr(Context* ctx,
case WASM_EXPR_TYPE_CALL: {
const WasmFunc* callee;
if (WASM_SUCCEEDED(check_func_var(ctx, &expr->call.var, &callee))) {
- if (wasm_decl_has_func_type(&callee->decl))
+ const WasmFuncSignature* sig =
+ wasm_decl_get_signature(ctx->current_module, &callee->decl);
+ if (sig) {
+ check_call(ctx, &expr->loc, &callee->name, sig, expr->call.first_arg,
+ expr->call.num_args, expected_type, "call");
+ } else {
+ assert(wasm_decl_has_func_type(&callee->decl));
+ /* We already know this will fail. Call anyway to print and log the
+ * error. */
check_func_type_var(ctx, &callee->decl.type_var, NULL);
-
- check_call(ctx, &expr->loc, &callee->name, &callee->decl.sig,
- expr->call.first_arg, expr->call.num_args, expected_type,
- "call");
+ }
}
break;
}
@@ -519,12 +526,17 @@ static void check_expr(Context* ctx,
case WASM_EXPR_TYPE_CALL_IMPORT: {
const WasmImport* import;
if (WASM_SUCCEEDED(check_import_var(ctx, &expr->call.var, &import))) {
- if (wasm_decl_has_func_type(&import->decl))
+ const WasmFuncSignature* sig =
+ wasm_decl_get_signature(ctx->current_module, &import->decl);
+ if (sig) {
+ check_call(ctx, &expr->loc, &import->name, sig, expr->call.first_arg,
+ expr->call.num_args, expected_type, "call_import");
+ } else {
+ assert(wasm_decl_has_func_type(&import->decl));
+ /* We already know this will fail. Call anyway to print and log the
+ * error. */
check_func_type_var(ctx, &import->decl.type_var, NULL);
-
- check_call(ctx, &expr->loc, &import->name, &import->decl.sig,
- expr->call.first_arg, expr->call.num_args, expected_type,
- "call_import");
+ }
}
break;
}
@@ -642,7 +654,8 @@ static void check_expr(Context* ctx,
break;
case WASM_EXPR_TYPE_RETURN: {
- WasmType result_type = wasm_get_result_type(ctx->current_func);
+ WasmType result_type =
+ wasm_get_result_type(ctx->current_module, ctx->current_func);
check_expr_opt(ctx, &expr->loc, expr->return_.expr, result_type,
" of return");
break;
@@ -704,21 +717,22 @@ static void check_expr(Context* ctx,
static void check_func_signature_matches_func_type(
Context* ctx,
- const WasmFunc* func,
+ const WasmLocation* loc,
+ const WasmFuncSignature* sig,
const WasmFuncType* func_type) {
- size_t num_params = wasm_get_num_params(func);
- check_type_exact(ctx, &func->loc, wasm_get_result_type(func),
+ size_t num_params = sig->param_types.size;
+ check_type_exact(ctx, loc, sig->result_type,
wasm_get_func_type_result_type(func_type),
- " of function signature result");
+ " between function result and function type result");
if (num_params == wasm_get_func_type_num_params(func_type)) {
size_t i;
for (i = 0; i < num_params; ++i) {
- check_type_arg_exact(ctx, &func->loc, wasm_get_param_type(func, i),
+ check_type_arg_exact(ctx, loc, sig->param_types.data[i],
wasm_get_func_type_param_type(func_type, i),
"function", i);
}
} else {
- print_error(ctx, CHECK_TYPE_FULL, &func->loc,
+ print_error(ctx, CHECK_TYPE_FULL, loc,
"expected %" PRIzd " parameters, got %" PRIzd,
wasm_get_func_type_num_params(func_type), num_params);
}
@@ -733,22 +747,25 @@ static void check_func(Context* ctx,
if (WASM_SUCCEEDED(
check_func_type_var(ctx, &func->decl.type_var, &func_type))) {
if (wasm_decl_has_signature(&func->decl))
- check_func_signature_matches_func_type(ctx, ctx->current_func,
+ check_func_signature_matches_func_type(ctx, &func->loc, &func->decl.sig,
func_type);
}
}
check_duplicate_bindings(ctx, &func->param_bindings, "parameter");
check_duplicate_bindings(ctx, &func->local_bindings, "local");
- WasmType result_type = wasm_get_result_type(func);
- /* The function has an implicit label; branching to it is equivalent to the
- * returning from the function. */
- LabelNode node;
- WasmLabel label = wasm_empty_string_slice();
- push_label(ctx, &func->loc, &node, &label, result_type, "func");
- check_expr_list(ctx, &func->loc, func->first_expr, result_type,
- " of function result");
- pop_label(ctx);
+ const WasmFuncSignature* sig =
+ wasm_decl_get_signature(ctx->current_module, &func->decl);
+ if (sig) {
+ /* The function has an implicit label; branching to it is equivalent to the
+ * returning from the function. */
+ LabelNode node;
+ WasmLabel label = wasm_empty_string_slice();
+ push_label(ctx, &func->loc, &node, &label, sig->result_type, "func");
+ check_expr_list(ctx, &func->loc, func->first_expr, sig->result_type,
+ " of function result");
+ pop_label(ctx);
+ }
ctx->current_func = NULL;
}
@@ -872,12 +889,13 @@ static void check_module(Context* ctx, const WasmModule* module) {
const WasmFunc* start_func = NULL;
check_func_var(ctx, &field->start, &start_func);
if (start_func) {
- if (wasm_get_num_params(start_func) != 0) {
+ if (wasm_get_num_params(ctx->current_module, start_func) != 0) {
print_error(ctx, CHECK_TYPE_FULL, &field->loc,
"start function must be nullary");
}
- if (wasm_get_result_type(start_func) != WASM_TYPE_VOID) {
+ if (wasm_get_result_type(ctx->current_module, start_func) !=
+ WASM_TYPE_VOID) {
print_error(ctx, CHECK_TYPE_FULL, &field->loc,
"start function must not return anything");
}
@@ -943,14 +961,15 @@ static void check_raw_module(Context* ctx, WasmRawModule* raw) {
static void check_invoke(Context* ctx,
const WasmCommandInvoke* invoke,
TypeSet return_type) {
- if (!ctx->current_module) {
+ const WasmModule* module = ctx->current_module;
+ if (!module) {
print_error(ctx, CHECK_TYPE_FULL, &invoke->loc,
"invoke must occur after a module definition");
return;
}
WasmExport* export =
- wasm_get_export_by_name(ctx->current_module, &invoke->name);
+ wasm_get_export_by_name(module, &invoke->name);
if (!export) {
print_error(ctx, CHECK_TYPE_NAME, &invoke->loc,
"unknown function export \"" PRIstringslice "\"",
@@ -958,14 +977,14 @@ static void check_invoke(Context* ctx,
return;
}
- WasmFunc* func = wasm_get_func_by_var(ctx->current_module, &export->var);
+ WasmFunc* func = wasm_get_func_by_var(module, &export->var);
if (!func) {
/* this error will have already been reported, just skip it */
return;
}
size_t actual_args = invoke->args.size;
- size_t expected_args = wasm_get_num_params(func);
+ size_t expected_args = wasm_get_num_params(module, func);
if (expected_args != actual_args) {
print_error(ctx, CHECK_TYPE_FULL, &invoke->loc,
"too %s parameters to function. got %" PRIzd
@@ -974,13 +993,13 @@ static void check_invoke(Context* ctx,
expected_args);
return;
}
- check_type_set(ctx, &invoke->loc, wasm_get_result_type(func), return_type,
- "");
+ check_type_set(ctx, &invoke->loc, wasm_get_result_type(module, func),
+ return_type, "");
size_t i;
for (i = 0; i < actual_args; ++i) {
WasmConst* const_ = &invoke->args.data[i];
check_type_arg_exact(ctx, &const_->loc, const_->type,
- wasm_get_param_type(func, i), "invoke", i);
+ wasm_get_param_type(module, func, i), "invoke", i);
}
}
diff --git a/src/wasm-ast-parser-lexer-shared.c b/src/wasm-ast-parser-lexer-shared.c
index 865bff25..63eb44e7 100644
--- a/src/wasm-ast-parser-lexer-shared.c
+++ b/src/wasm-ast-parser-lexer-shared.c
@@ -51,7 +51,7 @@ void wasm_ast_format_error(WasmSourceErrorHandler* error_handler,
size_t source_line_length = 0;
int source_line_column_offset = 0;
size_t source_line_max_length = error_handler->source_line_max_length;
- if (loc) {
+ if (loc && lexer) {
source_line = alloca(source_line_max_length + 1);
WasmResult result = wasm_ast_lexer_get_source_line(
lexer, loc, source_line_max_length, source_line, &source_line_length,
diff --git a/src/wasm-ast-parser.y b/src/wasm-ast-parser.y
index 8c44ca95..c6e193a8 100644
--- a/src/wasm-ast-parser.y
+++ b/src/wasm-ast-parser.y
@@ -81,9 +81,6 @@ static WasmResult parse_const(WasmType type, WasmLiteralType literal_type,
static void dup_text_list(WasmAllocator*, WasmTextList* text_list,
void** out_data, size_t* out_size);
-static void copy_signature_from_func_type(
- WasmAllocator* allocator, WasmModule* module, WasmFuncDeclaration* decl);
-
typedef struct BinaryErrorCallbackData {
WasmLocation* loc;
WasmAstLexer* lexer;
@@ -944,18 +941,6 @@ raw_module :
$$.type = WASM_RAW_MODULE_TYPE_TEXT;
$$.text = $3;
$$.loc = @2;
- WasmModule* module = $$.text;
-
- size_t i;
- for (i = 0; i < module->funcs.size; ++i) {
- WasmFunc* func = module->funcs.data[i];
- copy_signature_from_func_type(parser->allocator, module, &func->decl);
- }
-
- for (i = 0; i < module->imports.size; ++i) {
- WasmImport* import = module->imports.data[i];
- copy_signature_from_func_type(parser->allocator, module, &import->decl);
- }
}
| LPAR MODULE text_list RPAR {
$$.type = WASM_RAW_MODULE_TYPE_BINARY;
@@ -1194,25 +1179,6 @@ WasmResult wasm_parse_ast(WasmAstLexer* lexer,
return result == 0 && parser.errors == 0 ? WASM_OK : WASM_ERROR;
}
-static void copy_signature_from_func_type(WasmAllocator* allocator,
- WasmModule* module,
- WasmFuncDeclaration* decl) {
- /* if a function or import only defines a func type (and no explicit
- * signature), copy the signature over for convenience */
- if (wasm_decl_has_func_type(decl) && !wasm_decl_has_signature(decl)) {
- int index = wasm_get_func_type_index_by_var(module, &decl->type_var);
- if (index >= 0 && (size_t)index < module->func_types.size) {
- WasmFuncType* func_type = module->func_types.data[index];
- decl->sig.result_type = func_type->sig.result_type;
- wasm_extend_types(allocator, &decl->sig.param_types,
- &func_type->sig.param_types);
- } else {
- /* technically not OK, but we'll catch this error later in the AST
- * checker */
- }
- }
-}
-
static void on_read_binary_error(uint32_t offset, const char* error,
void* user_data) {
BinaryErrorCallbackData* data = user_data;
diff --git a/src/wasm-ast-writer.c b/src/wasm-ast-writer.c
index ff0a1d93..794fe4d1 100644
--- a/src/wasm-ast-writer.c
+++ b/src/wasm-ast-writer.c
@@ -587,9 +587,9 @@ static void write_func(Context* ctx,
if (wasm_decl_has_signature(&func->decl)) {
write_type_bindings(ctx, "param", func, &func->decl.sig.param_types,
&func->param_bindings);
- if (wasm_get_result_type(func) != WASM_TYPE_VOID) {
+ if (wasm_get_result_type(module, func) != WASM_TYPE_VOID) {
write_open_space(ctx, "result");
- write_type(ctx, wasm_get_result_type(func), NEXT_CHAR_NONE);
+ write_type(ctx, wasm_get_result_type(module, func), NEXT_CHAR_NONE);
write_close_space(ctx);
}
}
diff --git a/src/wasm-ast.h b/src/wasm-ast.h
index 3939ec92..4988bcd2 100644
--- a/src/wasm-ast.h
+++ b/src/wasm-ast.h
@@ -509,23 +509,58 @@ wasm_decl_has_signature(const WasmFuncDeclaration* decl) {
return decl->flags & WASM_FUNC_DECLARATION_FLAG_HAS_SIGNATURE;
}
+static WASM_INLINE const WasmFuncSignature* wasm_decl_get_signature(
+ const WasmModule* module,
+ const WasmFuncDeclaration* decl) {
+ if (wasm_decl_has_func_type(decl)) {
+ const WasmFuncType* func_type =
+ wasm_get_func_type_by_var(module, &decl->type_var);
+ if (!func_type)
+ return NULL;
+ return &func_type->sig;
+ } else if (wasm_decl_has_signature(decl)) {
+ return &decl->sig;
+ } else {
+ return NULL;
+ }
+}
+
+static WASM_INLINE size_t wasm_get_num_params(const WasmModule* module,
+ const WasmFunc* func) {
+ const WasmFuncSignature* sig = wasm_decl_get_signature(module, &func->decl);
+ assert(sig);
+ return sig->param_types.size;
+}
-static WASM_INLINE size_t wasm_get_num_params(const WasmFunc* func) {
- return func->decl.sig.param_types.size;
+static WASM_INLINE size_t wasm_get_num_locals(const WasmFunc* func) {
+ return func->local_types.size;
}
-static WASM_INLINE size_t wasm_get_num_params_and_locals(const WasmFunc* func) {
- return wasm_get_num_params(func) + func->local_types.size;
+static WASM_INLINE size_t
+wasm_get_num_params_and_locals(const WasmModule* module, const WasmFunc* func) {
+ return wasm_get_num_params(module, func) + wasm_get_num_locals(func);
+}
+
+static WASM_INLINE WasmType wasm_get_param_type(const WasmModule* module,
+ const WasmFunc* func,
+ int index) {
+ const WasmFuncSignature* sig = wasm_decl_get_signature(module, &func->decl);
+ assert(sig);
+ assert((size_t)index < sig->param_types.size);
+ return sig->param_types.data[index];
}
-static WASM_INLINE WasmType wasm_get_param_type(const WasmFunc* func,
+static WASM_INLINE WasmType wasm_get_local_type(const WasmFunc* func,
int index) {
- assert((size_t)index < wasm_get_num_params(func));
- return func->decl.sig.param_types.data[index];
+ assert((size_t)index < wasm_get_num_locals(func));
+ return func->local_types.data[index];
}
-static WASM_INLINE WasmType wasm_get_result_type(const WasmFunc* func) {
- return func->decl.sig.result_type;
+static WASM_INLINE WasmType wasm_get_result_type(const WasmModule* module,
+ const WasmFunc* func) {
+ const WasmFuncSignature* sig = wasm_decl_get_signature(module, &func->decl);
+ assert(sig);
+ return sig->result_type;
}
static WASM_INLINE WasmType
diff --git a/src/wasm-binary-reader-ast.c b/src/wasm-binary-reader-ast.c
index b29c06e8..50513774 100644
--- a/src/wasm-binary-reader-ast.c
+++ b/src/wasm-binary-reader-ast.c
@@ -53,7 +53,7 @@
do { \
assert(wasm_decl_has_func_type(&ctx->current_func->decl)); \
uint32_t max_local_index = \
- wasm_get_num_params_and_locals(ctx->current_func); \
+ wasm_get_num_params_and_locals(ctx->module, ctx->current_func); \
if ((local_index) >= max_local_index) { \
print_error(ctx, "invalid local_index: %d (max %d)", (local_index), \
max_local_index); \
@@ -990,7 +990,7 @@ static WasmResult on_local_names_count(uint32_t index,
WasmModule* module = ctx->module;
assert(index < module->funcs.size);
WasmFunc* func = module->funcs.data[index];
- uint32_t num_params_and_locals = wasm_get_num_params_and_locals(func);
+ uint32_t num_params_and_locals = wasm_get_num_params_and_locals(module, func);
if (count > num_params_and_locals) {
print_error(ctx, "expected local name count (%d) <= local count (%d)",
count, num_params_and_locals);
@@ -1006,7 +1006,7 @@ static WasmResult on_local_name(uint32_t func_index,
Context* ctx = user_data;
WasmModule* module = ctx->module;
WasmFunc* func = module->funcs.data[func_index];
- uint32_t num_params = wasm_get_num_params(func);
+ uint32_t num_params = wasm_get_num_params(module, func);
WasmStringSlice new_name;
dup_name(ctx, &name, &new_name);
WasmBindingHash* bindings;
diff --git a/src/wasm-binary-writer-spec.c b/src/wasm-binary-writer-spec.c
index 47b98a04..b5f749a3 100644
--- a/src/wasm-binary-writer-spec.c
+++ b/src/wasm-binary-writer-spec.c
@@ -309,7 +309,7 @@ static void write_commands(Context* ctx, WasmScript* script) {
int func_index = wasm_get_func_index_by_var(last_module, &export->var);
assert(func_index >= 0 && (size_t)func_index < last_module->funcs.size);
WasmFunc* callee = last_module->funcs.data[func_index];
- WasmType result_type = wasm_get_result_type(callee);
+ WasmType result_type = wasm_get_result_type(last_module, callee);
/* these pointers will be invalidated later, so we can't use them */
export = NULL;
callee = NULL;
diff --git a/src/wasm-binary-writer.c b/src/wasm-binary-writer.c
index 385c773a..5019d6f5 100644
--- a/src/wasm-binary-writer.c
+++ b/src/wasm-binary-writer.c
@@ -59,7 +59,7 @@ typedef struct WasmLabelNode {
struct WasmLabelNode* next;
} WasmLabelNode;
-typedef struct WasmContext {
+typedef struct Context {
WasmAllocator* allocator;
WasmStream stream;
WasmStream* log_stream;
@@ -74,7 +74,7 @@ typedef struct WasmContext {
int* func_sig_indexes;
uint32_t* remapped_locals; /* from unpacked -> packed index */
uint32_t* reverse_remapped_locals; /* from packed -> unpacked index */
-} WasmContext;
+} Context;
WASM_DEFINE_VECTOR(func_signature, WasmFuncSignature);
@@ -84,7 +84,7 @@ static void destroy_func_signature_vector_and_elements(
WASM_DESTROY_VECTOR_AND_ELEMENTS(allocator, *sigs, func_signature);
}
-static void write_header(WasmContext* ctx, const char* name, int index) {
+static void write_header(Context* ctx, const char* name, int index) {
if (ctx->log_stream) {
if (index == PRINT_HEADER_NO_INDEX) {
wasm_writef(ctx->log_stream, "; %s\n", name);
@@ -185,7 +185,7 @@ static uint32_t size_u32_leb128(uint32_t value) {
}
/* returns offset of leb128 */
-static uint32_t write_u32_leb128_space(WasmContext* ctx,
+static uint32_t write_u32_leb128_space(Context* ctx,
uint32_t leb_size_guess,
const char* desc) {
assert(leb_size_guess <= MAX_U32_LEB128_BYTES);
@@ -197,7 +197,7 @@ static uint32_t write_u32_leb128_space(WasmContext* ctx,
return result;
}
-static void write_fixup_u32_leb128_size(WasmContext* ctx,
+static void write_fixup_u32_leb128_size(Context* ctx,
uint32_t offset,
uint32_t leb_size_guess,
const char* desc) {
@@ -231,7 +231,7 @@ static void write_opcode(WasmStream* stream, uint8_t opcode) {
wasm_write_u8(stream, opcode, s_opcode_name[opcode]);
}
-static void begin_section(WasmContext* ctx,
+static void begin_section(Context* ctx,
const char* name,
size_t leb_size_guess) {
assert(ctx->last_section_leb_size_guess == 0);
@@ -245,7 +245,7 @@ static void begin_section(WasmContext* ctx,
write_u32_leb128_space(ctx, leb_size_guess, "section size (guess)");
}
-static void end_section(WasmContext* ctx) {
+static void end_section(Context* ctx) {
assert(ctx->last_section_leb_size_guess != 0);
write_fixup_u32_leb128_size(ctx, ctx->last_section_offset,
ctx->last_section_leb_size_guess,
@@ -278,7 +278,7 @@ static WasmLabelNode* find_label_by_var(WasmLabelNode* top_label,
return node;
}
-static void push_unused_label(WasmContext* ctx,
+static void push_unused_label(Context* ctx,
WasmLabelNode* node,
const WasmLabel* label) {
assert(label);
@@ -288,19 +288,19 @@ static void push_unused_label(WasmContext* ctx,
ctx->top_label = node;
}
-static void pop_unused_label(WasmContext* ctx, const WasmLabel* label) {
+static void pop_unused_label(Context* ctx, const WasmLabel* label) {
if (ctx->top_label && ctx->top_label->label == label)
ctx->top_label = ctx->top_label->next;
}
-static void push_label(WasmContext* ctx,
+static void push_label(Context* ctx,
WasmLabelNode* node,
const WasmLabel* label) {
push_unused_label(ctx, node, label);
ctx->max_depth++;
}
-static void pop_label(WasmContext* ctx, const WasmLabel* label) {
+static void pop_label(Context* ctx, const WasmLabel* label) {
ctx->max_depth--;
pop_unused_label(ctx, label);
}
@@ -326,7 +326,7 @@ static int find_func_signature(const WasmFuncSignatureVector* sigs,
return -1;
}
-static void get_or_create_func_signature(WasmContext* ctx,
+static void get_or_create_func_signature(Context* ctx,
WasmFuncSignatureVector* sigs,
WasmType result_type,
const WasmTypeVector* param_types,
@@ -342,7 +342,7 @@ static void get_or_create_func_signature(WasmContext* ctx,
*out_index = index;
}
-static int get_signature_index(WasmContext* ctx,
+static int get_signature_index(Context* ctx,
const WasmModule* module,
WasmFuncSignatureVector* sigs,
const WasmFuncDeclaration* decl) {
@@ -358,7 +358,7 @@ static int get_signature_index(WasmContext* ctx,
return index;
}
-static void get_func_signatures(WasmContext* ctx,
+static void get_func_signatures(Context* ctx,
const WasmModule* module,
WasmFuncSignatureVector* sigs) {
/* function types are not deduped; we don't want the signature index to match
@@ -392,9 +392,11 @@ static void get_func_signatures(WasmContext* ctx,
}
}
-static void remap_locals(WasmContext* ctx, const WasmFunc* func) {
+static void remap_locals(Context* ctx,
+ const WasmModule* module,
+ const WasmFunc* func) {
uint32_t i;
- uint32_t num_params = wasm_get_num_params(func);
+ uint32_t num_params = wasm_get_num_params(module, func);
uint32_t num_locals = func->local_types.size;
uint32_t num_params_and_locals = num_params + num_locals;
ctx->remapped_locals = wasm_realloc(ctx->allocator, ctx->remapped_locals,
@@ -444,17 +446,17 @@ static void remap_locals(WasmContext* ctx, const WasmFunc* func) {
}
}
-static void write_expr_list(WasmContext* ctx,
+static void write_expr_list(Context* ctx,
const WasmModule* module,
const WasmFunc* func,
const WasmExpr* first_expr);
-static void write_expr_opt(WasmContext* ctx,
+static void write_expr_opt(Context* ctx,
const WasmModule* module,
const WasmFunc* func,
const WasmExpr* expr);
-static void write_expr(WasmContext* ctx,
+static void write_expr(Context* ctx,
const WasmModule* module,
const WasmFunc* func,
const WasmExpr* expr) {
@@ -692,7 +694,7 @@ static void write_expr(WasmContext* ctx,
}
}
-static void write_expr_list(WasmContext* ctx,
+static void write_expr_list(Context* ctx,
const WasmModule* module,
const WasmFunc* func,
const WasmExpr* first) {
@@ -701,7 +703,7 @@ static void write_expr_list(WasmContext* ctx,
write_expr(ctx, module, func, expr);
}
-static void write_expr_opt(WasmContext* ctx,
+static void write_expr_opt(Context* ctx,
const WasmModule* module,
const WasmFunc* func,
const WasmExpr* expr) {
@@ -709,18 +711,18 @@ static void write_expr_opt(WasmContext* ctx,
write_expr(ctx, module, func, expr);
}
-static void write_func_locals(WasmContext* ctx,
+static void write_func_locals(Context* ctx,
const WasmModule* module,
const WasmFunc* func,
const WasmTypeVector* local_types) {
- remap_locals(ctx, func);
+ remap_locals(ctx, module, func);
if (local_types->size == 0) {
write_u32_leb128(&ctx->stream, 0, "local decl count");
return;
}
- uint32_t num_params = wasm_get_num_params(func);
+ uint32_t num_params = wasm_get_num_params(module, func);
#define FIRST_LOCAL_INDEX (num_params)
#define LAST_LOCAL_INDEX (num_params + local_types->size)
@@ -757,7 +759,7 @@ static void write_func_locals(WasmContext* ctx,
}
}
-static void write_func(WasmContext* ctx,
+static void write_func(Context* ctx,
const WasmModule* module,
const WasmFunc* func) {
WasmLabelNode node;
@@ -768,7 +770,7 @@ static void write_func(WasmContext* ctx,
pop_label(ctx, &label);
}
-static void write_module(WasmContext* ctx, const WasmModule* module) {
+static void write_module(Context* ctx, const WasmModule* module) {
/* TODO(binji): better leb size guess. Some sections we know will only be 1
byte, but others we can be fairly certain will be larger. */
const size_t leb_size_guess = 1;
@@ -851,7 +853,8 @@ static void write_module(WasmContext* ctx, const WasmModule* module) {
begin_section(ctx, WASM_SECTION_NAME_MEMORY, leb_size_guess);
write_u32_leb128(&ctx->stream, (uint32_t)module->memory->initial_pages,
"min mem pages");
- write_u32_leb128(&ctx->stream, (uint32_t)module->memory->max_pages, "max mem pages");
+ write_u32_leb128(&ctx->stream, (uint32_t)module->memory->max_pages,
+ "max mem pages");
wasm_write_u8(&ctx->stream, export_memory, "export mem");
end_section(ctx);
}
@@ -924,9 +927,10 @@ static void write_module(WasmContext* ctx, const WasmModule* module) {
write_u32_leb128(&ctx->stream, module->funcs.size, "num functions");
for (i = 0; i < module->funcs.size; ++i) {
const WasmFunc* func = module->funcs.data[i];
- uint32_t num_params = wasm_get_num_params(func);
+ uint32_t num_params = wasm_get_num_params(module, func);
uint32_t num_locals = func->local_types.size;
- uint32_t num_params_and_locals = wasm_get_num_params_and_locals(func);
+ uint32_t num_params_and_locals =
+ wasm_get_num_params_and_locals(module, func);
wasm_snprintf(desc, sizeof(desc), "func name %" PRIzd, i);
write_str(&ctx->stream, func->name.start, func->name.length,
@@ -934,10 +938,12 @@ static void write_module(WasmContext* ctx, const WasmModule* module) {
write_u32_leb128(&ctx->stream, num_params_and_locals, "num locals");
if (num_params_and_locals) {
- remap_locals(ctx, func);
+ const WasmFuncSignature* sig =
+ wasm_decl_get_signature(module, &func->decl);
+ remap_locals(ctx, module, func);
wasm_make_type_binding_reverse_mapping(
- ctx->allocator, &func->decl.sig.param_types, &func->param_bindings,
+ ctx->allocator, &sig->param_types, &func->param_bindings,
&index_to_name);
size_t j;
for (j = 0; j < num_params; ++j) {
@@ -968,7 +974,7 @@ static void write_module(WasmContext* ctx, const WasmModule* module) {
destroy_func_signature_vector_and_elements(ctx->allocator, &sigs);
}
-static void write_commands(WasmContext* ctx, const WasmScript* script) {
+static void write_commands(Context* ctx, const WasmScript* script) {
size_t i;
WasmBool wrote_module = WASM_FALSE;
for (i = 0; i < script->commands.size; ++i) {
@@ -988,7 +994,7 @@ static void write_commands(WasmContext* ctx, const WasmScript* script) {
}
}
-static void cleanup_context(WasmContext* ctx) {
+static void cleanup_context(Context* ctx) {
wasm_free(ctx->allocator, ctx->import_sig_indexes);
wasm_free(ctx->allocator, ctx->func_sig_indexes);
wasm_free(ctx->allocator, ctx->remapped_locals);
@@ -999,7 +1005,7 @@ WasmResult wasm_write_binary_module(WasmAllocator* allocator,
WasmWriter* writer,
const WasmModule* module,
const WasmWriteBinaryOptions* options) {
- WasmContext ctx;
+ Context ctx;
WASM_ZERO_MEMORY(ctx);
ctx.allocator = allocator;
ctx.options = options;
@@ -1016,7 +1022,7 @@ WasmResult wasm_write_binary_script(WasmAllocator* allocator,
WasmWriter* writer,
const WasmScript* script,
const WasmWriteBinaryOptions* options) {
- WasmContext ctx;
+ Context ctx;
WASM_ZERO_MEMORY(ctx);
ctx.allocator = allocator;
ctx.options = options;
diff --git a/test/parse/func/bad-sig-result-type-mismatch.txt b/test/parse/func/bad-sig-result-type-mismatch.txt
index cb727d3d..5725abd4 100644
--- a/test/parse/func/bad-sig-result-type-mismatch.txt
+++ b/test/parse/func/bad-sig-result-type-mismatch.txt
@@ -3,10 +3,10 @@
(type $t (func (param i32) (result f32)))
(func (type $t) (param i32) (result i64)))
(;; STDERR ;;;
-parse/func/bad-sig-result-type-mismatch.txt:4:4: type mismatch of function signature result. got i64, expected f32
+parse/func/bad-sig-result-type-mismatch.txt:4:4: type mismatch between function result and function type result. got i64, expected f32
(func (type $t) (param i32) (result i64)))
^^^^
-parse/func/bad-sig-result-type-mismatch.txt:4:4: type mismatch of function result. got void, expected i64
+parse/func/bad-sig-result-type-mismatch.txt:4:4: type mismatch of function result. got void, expected f32
(func (type $t) (param i32) (result i64)))
^^^^
;;; STDERR ;;)
diff --git a/test/parse/func/bad-sig-result-type-not-void.txt b/test/parse/func/bad-sig-result-type-not-void.txt
index b987fe97..1e72a194 100644
--- a/test/parse/func/bad-sig-result-type-not-void.txt
+++ b/test/parse/func/bad-sig-result-type-not-void.txt
@@ -3,10 +3,7 @@
(type $t (func (param i32)))
(func (type $t) (param i32) (result f32)))
(;; STDERR ;;;
-parse/func/bad-sig-result-type-not-void.txt:4:4: type mismatch of function signature result. got f32, expected void
- (func (type $t) (param i32) (result f32)))
- ^^^^
-parse/func/bad-sig-result-type-not-void.txt:4:4: type mismatch of function result. got void, expected f32
+parse/func/bad-sig-result-type-not-void.txt:4:4: type mismatch between function result and function type result. got f32, expected void
(func (type $t) (param i32) (result f32)))
^^^^
;;; STDERR ;;)
diff --git a/test/parse/func/bad-sig-result-type-void.txt b/test/parse/func/bad-sig-result-type-void.txt
index cd46e6b7..5c0c9ae3 100644
--- a/test/parse/func/bad-sig-result-type-void.txt
+++ b/test/parse/func/bad-sig-result-type-void.txt
@@ -3,7 +3,10 @@
(type $t (func (param i32) (result f32)))
(func (type $t) (param i32)))
(;; STDERR ;;;
-parse/func/bad-sig-result-type-void.txt:4:4: type mismatch of function signature result. got void, expected f32
+parse/func/bad-sig-result-type-void.txt:4:4: type mismatch between function result and function type result. got void, expected f32
+ (func (type $t) (param i32)))
+ ^^^^
+parse/func/bad-sig-result-type-void.txt:4:4: type mismatch of function result. got void, expected f32
(func (type $t) (param i32)))
^^^^
;;; STDERR ;;)