aboutsummaryrefslogtreecommitdiff
path: root/tools/translate.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/translate.c')
-rw-r--r--tools/translate.c35
1 files changed, 24 insertions, 11 deletions
diff --git a/tools/translate.c b/tools/translate.c
index 27532ad..00191a3 100644
--- a/tools/translate.c
+++ b/tools/translate.c
@@ -276,7 +276,7 @@ static int typecheck_local_get_set(struct translation *data, uint32_t localidx,
/* Translate complex - those routines have to be defined manually */
#define TC(wasm_opcode, name, argtypes, restype)
-static int _translate_loop(struct translation *data)
+static int translate_block_loop(struct translation *data, bool loop)
{
struct resulttype block_args, block_results;
char type_storage;
@@ -291,7 +291,7 @@ static int _translate_loop(struct translation *data)
goto fail;
if (translate_expr(data, &block_args, &block_results,
- &loop_end_markers, NULL, true))
+ &loop_end_markers, NULL, loop))
goto fail;
return 0;
@@ -301,6 +301,16 @@ fail:
return -1;
}
+static int _translate_block(struct translation *data)
+{
+ return translate_block_loop(data, false);
+}
+
+static int _translate_loop(struct translation *data)
+{
+ return translate_block_loop(data, true);
+}
+
static int _translate_if(struct translation *data)
{
struct types *backed_stack;
@@ -343,7 +353,7 @@ static int _translate_if(struct translation *data)
data->types_stack = backed_stack;
if (retval)
- goto fail;
+ goto fail;
if (i_jump(ptr_after(&else_end->instr), expr))
goto fail;
@@ -422,7 +432,7 @@ static int _translate_br(struct translation *data)
i_load_p(im(-4 * (offset_dst - 1)), expr) ||
i_swap ( expr) ||
i_load_p(im(-4 * offset_dst), expr) ||
- i_add_sp(im(-4 * (shift + 2)), expr))
+ i_add_sp(im(4 * (shift + 2)), expr))
goto fail;
for (i = 0; i < arity; i++) {
@@ -615,11 +625,11 @@ static int translate_load_store(struct translation *data,
/** DEFINE TRANSLATION FUNCTIONS POINTER ARRAY **/
/* Translate complex */
-#define TC(wasm_opcode, name, argtypes, restype) \
+#define TC(wasm_opcode, name, argtypes, restype) \
[wasm_opcode] = _translate_##name,
/* Translate Simple */
-#define TS(wasm_opcode, sm_instr, argtypes, restype) \
+#define TS(wasm_opcode, sm_instr, argtypes, restype) \
TC(wasm_opcode, sm_instr, dummy, dummy)
/* Translate load/store */
@@ -734,6 +744,7 @@ static struct target *add_target(struct module *module)
}
static struct label *add_label(struct translation *data, struct target *target,
+ uint32_t values_popped,
struct resulttype *arity)
{
struct label *lbl;
@@ -750,6 +761,7 @@ static struct label *add_label(struct translation *data, struct target *target,
lbl->arity = arity;
lbl->values_on_stack = data->labels ? data->labels->values_on_stack : 0;
lbl->values_on_stack += stack_size(data->types_stack);
+ lbl->values_on_stack -= values_popped;
data->labels = lbl;
@@ -777,7 +789,7 @@ static int translate_expr(struct translation *data, struct resulttype *args,
if (continuation_at_start)
continuation->instr = data->function->translated_body->prev;
- label = add_label(data, continuation,
+ label = add_label(data, continuation, args ? args->count : 0,
continuation_at_start ? args : results);
if (!label)
@@ -918,9 +930,11 @@ int translate(FILE *handle, struct function *function, struct module *module)
goto fail;
/* function epilogue */
+ if (i_load (im(STACK_FRAME_BACKUP_ADDR), expr))
+ goto fail;
+
if (function->type->results.count) {
- if (i_load (im(STACK_FRAME_BACKUP_ADDR), expr) ||
- i_swap ( expr) ||
+ if (i_swap ( expr) ||
i_store_p(im(4 * (2 + all_locals_count - 1)), expr) ||
i_load (im(STACK_FRAME_BACKUP_ADDR), expr) ||
i_tee ( expr) ||
@@ -932,8 +946,7 @@ int translate(FILE *handle, struct function *function, struct module *module)
goto fail;
} else {
/* It's a bit shorter if we don't return anything */
- if (i_load (im(STACK_FRAME_BACKUP_ADDR), expr) ||
- i_tee ( expr) ||
+ if (i_tee ( expr) ||
i_tee ( expr) ||
i_load_p (im(4 * (1 + locals_count)), expr) ||
i_store_p(im(4 * (2 + all_locals_count - 1)), expr) ||