From 38b66bd46ff4d9b6b8787bf05054cb4628c73e07 Mon Sep 17 00:00:00 2001 From: xensik Date: Mon, 8 Aug 2022 11:55:33 +0200 Subject: [PATCH] optimize compiler test jumps --- src/h1/xsk/compiler.cpp | 36 ++++++++++++++++++++++++++++++------ src/h2/xsk/compiler.cpp | 36 ++++++++++++++++++++++++++++++------ src/iw5/xsk/compiler.cpp | 36 ++++++++++++++++++++++++++++++------ src/iw6/xsk/compiler.cpp | 36 ++++++++++++++++++++++++++++++------ src/iw7/xsk/compiler.cpp | 36 ++++++++++++++++++++++++++++++------ src/iw8/xsk/compiler.cpp | 36 ++++++++++++++++++++++++++++++------ src/s1/xsk/compiler.cpp | 36 ++++++++++++++++++++++++++++++------ src/s2/xsk/compiler.cpp | 36 ++++++++++++++++++++++++++++++------ src/s4/xsk/compiler.cpp | 36 ++++++++++++++++++++++++++++++------ 9 files changed, 270 insertions(+), 54 deletions(-) diff --git a/src/h1/xsk/compiler.cpp b/src/h1/xsk/compiler.cpp index 1f71ea07..d00e9d58 100644 --- a/src/h1/xsk/compiler.cpp +++ b/src/h1/xsk/compiler.cpp @@ -529,8 +529,16 @@ void compiler::emit_stmt_while(const ast::stmt_while::ptr& stmt, const block::pt if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_stmt(stmt->stmt, stmt->blk, false); @@ -587,8 +595,16 @@ void compiler::emit_stmt_dowhile(const ast::stmt_dowhile::ptr& stmt, const block if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_opcode(opcode::OP_jumpback, begin_loc); @@ -642,8 +658,16 @@ void compiler::emit_stmt_for(const ast::stmt_for::ptr& stmt, const block::ptr& b if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } can_break_ = true; diff --git a/src/h2/xsk/compiler.cpp b/src/h2/xsk/compiler.cpp index 84222c4f..5792d8d4 100644 --- a/src/h2/xsk/compiler.cpp +++ b/src/h2/xsk/compiler.cpp @@ -529,8 +529,16 @@ void compiler::emit_stmt_while(const ast::stmt_while::ptr& stmt, const block::pt if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_stmt(stmt->stmt, stmt->blk, false); @@ -587,8 +595,16 @@ void compiler::emit_stmt_dowhile(const ast::stmt_dowhile::ptr& stmt, const block if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_opcode(opcode::OP_jumpback, begin_loc); @@ -642,8 +658,16 @@ void compiler::emit_stmt_for(const ast::stmt_for::ptr& stmt, const block::ptr& b if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } can_break_ = true; diff --git a/src/iw5/xsk/compiler.cpp b/src/iw5/xsk/compiler.cpp index 7f574efe..57083b8c 100644 --- a/src/iw5/xsk/compiler.cpp +++ b/src/iw5/xsk/compiler.cpp @@ -521,8 +521,16 @@ void compiler::emit_stmt_while(const ast::stmt_while::ptr& stmt, const block::pt if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_stmt(stmt->stmt, stmt->blk, false); @@ -579,8 +587,16 @@ void compiler::emit_stmt_dowhile(const ast::stmt_dowhile::ptr& stmt, const block if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_opcode(opcode::OP_jumpback, begin_loc); @@ -634,8 +650,16 @@ void compiler::emit_stmt_for(const ast::stmt_for::ptr& stmt, const block::ptr& b if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } can_break_ = true; diff --git a/src/iw6/xsk/compiler.cpp b/src/iw6/xsk/compiler.cpp index cb6f000e..25b7a71a 100644 --- a/src/iw6/xsk/compiler.cpp +++ b/src/iw6/xsk/compiler.cpp @@ -521,8 +521,16 @@ void compiler::emit_stmt_while(const ast::stmt_while::ptr& stmt, const block::pt if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_stmt(stmt->stmt, stmt->blk, false); @@ -579,8 +587,16 @@ void compiler::emit_stmt_dowhile(const ast::stmt_dowhile::ptr& stmt, const block if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_opcode(opcode::OP_jumpback, begin_loc); @@ -634,8 +650,16 @@ void compiler::emit_stmt_for(const ast::stmt_for::ptr& stmt, const block::ptr& b if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } can_break_ = true; diff --git a/src/iw7/xsk/compiler.cpp b/src/iw7/xsk/compiler.cpp index 580ff2a8..ee86edd5 100644 --- a/src/iw7/xsk/compiler.cpp +++ b/src/iw7/xsk/compiler.cpp @@ -521,8 +521,16 @@ void compiler::emit_stmt_while(const ast::stmt_while::ptr& stmt, const block::pt if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_stmt(stmt->stmt, stmt->blk, false); @@ -579,8 +587,16 @@ void compiler::emit_stmt_dowhile(const ast::stmt_dowhile::ptr& stmt, const block if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_opcode(opcode::OP_jumpback, begin_loc); @@ -634,8 +650,16 @@ void compiler::emit_stmt_for(const ast::stmt_for::ptr& stmt, const block::ptr& b if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } can_break_ = true; diff --git a/src/iw8/xsk/compiler.cpp b/src/iw8/xsk/compiler.cpp index 089941f9..0186ed33 100644 --- a/src/iw8/xsk/compiler.cpp +++ b/src/iw8/xsk/compiler.cpp @@ -529,8 +529,16 @@ void compiler::emit_stmt_while(const ast::stmt_while::ptr& stmt, const block::pt if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_stmt(stmt->stmt, stmt->blk, false); @@ -587,8 +595,16 @@ void compiler::emit_stmt_dowhile(const ast::stmt_dowhile::ptr& stmt, const block if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_opcode(opcode::OP_jumpback, begin_loc); @@ -642,8 +658,16 @@ void compiler::emit_stmt_for(const ast::stmt_for::ptr& stmt, const block::ptr& b if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } can_break_ = true; diff --git a/src/s1/xsk/compiler.cpp b/src/s1/xsk/compiler.cpp index 09f816df..f570af95 100644 --- a/src/s1/xsk/compiler.cpp +++ b/src/s1/xsk/compiler.cpp @@ -529,8 +529,16 @@ void compiler::emit_stmt_while(const ast::stmt_while::ptr& stmt, const block::pt if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_stmt(stmt->stmt, stmt->blk, false); @@ -587,8 +595,16 @@ void compiler::emit_stmt_dowhile(const ast::stmt_dowhile::ptr& stmt, const block if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_opcode(opcode::OP_jumpback, begin_loc); @@ -642,8 +658,16 @@ void compiler::emit_stmt_for(const ast::stmt_for::ptr& stmt, const block::ptr& b if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } can_break_ = true; diff --git a/src/s2/xsk/compiler.cpp b/src/s2/xsk/compiler.cpp index 5c84699c..fb4515f2 100644 --- a/src/s2/xsk/compiler.cpp +++ b/src/s2/xsk/compiler.cpp @@ -529,8 +529,16 @@ void compiler::emit_stmt_while(const ast::stmt_while::ptr& stmt, const block::pt if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_stmt(stmt->stmt, stmt->blk, false); @@ -587,8 +595,16 @@ void compiler::emit_stmt_dowhile(const ast::stmt_dowhile::ptr& stmt, const block if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_opcode(opcode::OP_jumpback, begin_loc); @@ -642,8 +658,16 @@ void compiler::emit_stmt_for(const ast::stmt_for::ptr& stmt, const block::ptr& b if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } can_break_ = true; diff --git a/src/s4/xsk/compiler.cpp b/src/s4/xsk/compiler.cpp index 79e9dc3d..eb46c98e 100644 --- a/src/s4/xsk/compiler.cpp +++ b/src/s4/xsk/compiler.cpp @@ -529,8 +529,16 @@ void compiler::emit_stmt_while(const ast::stmt_while::ptr& stmt, const block::pt if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_stmt(stmt->stmt, stmt->blk, false); @@ -587,8 +595,16 @@ void compiler::emit_stmt_dowhile(const ast::stmt_dowhile::ptr& stmt, const block if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } emit_opcode(opcode::OP_jumpback, begin_loc); @@ -642,8 +658,16 @@ void compiler::emit_stmt_for(const ast::stmt_for::ptr& stmt, const block::ptr& b if (!const_cond) { - emit_expr(stmt->test, blk); - emit_opcode(opcode::OP_JumpOnFalse, break_loc); + if (stmt->test == ast::kind::expr_not) + { + emit_expr(stmt->test.as_not->rvalue, blk); + emit_opcode(opcode::OP_JumpOnTrue, break_loc); + } + else + { + emit_expr(stmt->test, blk); + emit_opcode(opcode::OP_JumpOnFalse, break_loc); + } } can_break_ = true;