From 18e8c34978a20dbb74c92e7cd54594c533943b7c Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Fri, 7 Jun 2019 23:16:51 +0200 Subject: [PATCH] :art: Improve Flow --- src/emit/emit.c | 73 ++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/src/emit/emit.c b/src/emit/emit.c index 2d9a3e0c..ae120fac 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -1151,37 +1151,6 @@ ANN static void emit_pop_stack(const Emitter emit, const m_uint index) { emit_pop_scope(emit); } -ANN static Instr _flow(const Emitter emit, const Exp e, const m_bool b) { - if(e->next) { - free_exp(emit->gwion->mp, e->next); - e->next = NULL; - env_err(emit->env, e->pos, "expression after comma won't be executed"); - } - CHECK_BO(emit_exp(emit, e, 0)) - const f_instr instr_i = b ? BranchEqInt : BranchNeqInt; - const f_instr instr_f = b ? BranchEqFloat : BranchNeqFloat; - return emit_flow(emit, e->type, instr_i, instr_f); -} - -ANN static m_bool emit_stmt_flow(const Emitter emit, const Stmt_Flow stmt) { - const m_uint index = emit_code_size(emit); - Instr op = NULL; - emit_push_stack(emit); - if(!stmt->is_do) - op = _flow(emit, stmt->cond, stmt_self(stmt)->stmt_type == ae_stmt_while); - CHECK_BB(scoped_stmt(emit, stmt->body, 1)) - if(stmt->is_do) { - CHECK_OB((op = _flow(emit, stmt->cond, stmt_self(stmt)->stmt_type != ae_stmt_while))) - op->m_val = index; - } else { - const Instr goto_ = emit_add_instr(emit, Goto); - goto_->m_val = index; - op->m_val = emit_code_size(emit); - } - emit_pop_stack(emit, index); - return GW_OK; -} - ANN static m_uint get_decl_size(Var_Decl_List a) { m_uint size = 0; do if(GET_FLAG(a->self->value, used)) @@ -1204,17 +1173,53 @@ ANN static m_uint pop_exp_size(const Emitter emit, Exp e) { return size; } -ANN static void pop_exp(const Emitter emit, Exp e) { +ANN static inline void pop_exp(const Emitter emit, Exp e) { const m_uint size = pop_exp_size(emit, e); if(size) regpop(emit, size); } +ANN static inline m_bool emit_exp_pop_next(const Emitter emit, Exp e) { + CHECK_BB(emit_exp(emit, e, 0)) + if(e->next) + pop_exp(emit, e->next); + return GW_OK; +} + +ANN static Instr _flow(const Emitter emit, const Exp e, const m_bool b) { + CHECK_BO(emit_exp_pop_next(emit, e)) + const f_instr instr_i = b ? BranchEqInt : BranchNeqInt; + const f_instr instr_f = b ? BranchEqFloat : BranchNeqFloat; + return emit_flow(emit, e->type, instr_i, instr_f); +} + +ANN static m_bool emit_stmt_flow(const Emitter emit, const Stmt_Flow stmt) { + const m_uint index = emit_code_size(emit); + Instr op = NULL; + emit_push_stack(emit); + if(!stmt->is_do) + op = _flow(emit, stmt->cond, stmt_self(stmt)->stmt_type == ae_stmt_while); + CHECK_BB(scoped_stmt(emit, stmt->body, 1)) + if(stmt->is_do) { + CHECK_OB((op = _flow(emit, stmt->cond, stmt_self(stmt)->stmt_type != ae_stmt_while))) + op->m_val = index; + } else { + const Instr goto_ = emit_add_instr(emit, Goto); + goto_->m_val = index; + op->m_val = emit_code_size(emit); + } + emit_pop_stack(emit, index); + return GW_OK; +} + ANN static m_bool emit_stmt_for(const Emitter emit, const Stmt_For stmt) { emit_push_stack(emit); CHECK_BB(emit_stmt(emit, stmt->c1, 1)) const m_uint index = emit_code_size(emit); - CHECK_BB(emit_stmt(emit, stmt->c2, 0)) + if(stmt->c2->stmt_type == ae_stmt_exp) + emit_exp_pop_next(emit, stmt->c2->d.stmt_exp.val); + else + CHECK_BB(emit_stmt(emit, stmt->c2, 0)) const Instr op = emit_flow(emit, stmt->c2->d.stmt_exp.val->type, BranchEqInt, BranchEqFloat); CHECK_BB(scoped_stmt(emit, stmt->body, 1)) @@ -1265,7 +1270,7 @@ ANN static m_bool emit_stmt_auto(const Emitter emit, const Stmt_Auto stmt) { ANN static m_bool emit_stmt_loop(const Emitter emit, const Stmt_Loop stmt) { emit_push_stack(emit); - CHECK_BB(emit_exp(emit, stmt->cond, 0)) + CHECK_BB(emit_exp_pop_next(emit, stmt->cond)) const m_uint index = emit_code_size(emit); const Instr cpy = emit_add_instr(emit, Reg2RegAddr); cpy->m_val2 = -SZ_INT; -- 2.43.0