diff --git a/include/builder/lib/utils.h b/include/builder/lib/utils.h index 53271cb..0ef8233 100644 --- a/include/builder/lib/utils.h +++ b/include/builder/lib/utils.h @@ -7,7 +7,7 @@ template static_var up_cast_range(dyn_var &v, T range) { static_var s; for (s = 0; s < range - 1; s++) { - if (s == v) { + if (v == s) { return s; } } diff --git a/samples/outputs.var_names/sample40 b/samples/outputs.var_names/sample40 index 9874de1..c1363c1 100644 --- a/samples/outputs.var_names/sample40 +++ b/samples/outputs.var_names/sample40 @@ -26,35 +26,28 @@ int match_re (char* arg1) { if (to_match_2 < str_len_1) { var3 = 0; return var3; - } else { - return 1; - } - } else { - var3 = 0; - return var3; - } - } else { - var5 = 0; - return var5; - } - } else { - if (arg1[to_match_2] == 100) { - goto label2; + } + return 1; + } + var3 = 0; + return var3; } - var3 = 0; - return var3; - } - } else { - var5 = 0; - return var5; - } - } else { - var3 = 0; - return var3; - } - } else { - var5 = 0; - return var5; - } + var5 = 0; + return var5; + } + if (arg1[to_match_2] == 100) { + goto label2; + } + var3 = 0; + return var3; + } + var5 = 0; + return var5; + } + var3 = 0; + return var3; + } + var5 = 0; + return var5; } diff --git a/samples/outputs.var_names/sample52 b/samples/outputs.var_names/sample52 new file mode 100644 index 0000000..009efe3 --- /dev/null +++ b/samples/outputs.var_names/sample52 @@ -0,0 +1,49 @@ +int isEven (int arg0) { + if (arg0 == 0) { + return 1; + } + if (arg0 == 1) { + return 0; + } + if (arg0 == 2) { + return 1; + } + if (arg0 == 3) { + return 0; + } + if (arg0 == 4) { + return 1; + } + if (arg0 == 5) { + return 0; + } + if (arg0 == 6) { + return 1; + } + if (arg0 == 7) { + return 0; + } + if (arg0 == 8) { + return 1; + } + if (arg0 == 9) { + return 0; + } + if (arg0 == 10) { + return 1; + } + if (arg0 == 11) { + return 0; + } + if (arg0 == 12) { + return 1; + } + if (arg0 == 13) { + return 0; + } + if (arg0 == 14) { + return 1; + } + return 0; +} + diff --git a/samples/outputs/sample b/samples/outputs/sample new file mode 100644 index 0000000..009efe3 --- /dev/null +++ b/samples/outputs/sample @@ -0,0 +1,49 @@ +int isEven (int arg0) { + if (arg0 == 0) { + return 1; + } + if (arg0 == 1) { + return 0; + } + if (arg0 == 2) { + return 1; + } + if (arg0 == 3) { + return 0; + } + if (arg0 == 4) { + return 1; + } + if (arg0 == 5) { + return 0; + } + if (arg0 == 6) { + return 1; + } + if (arg0 == 7) { + return 0; + } + if (arg0 == 8) { + return 1; + } + if (arg0 == 9) { + return 0; + } + if (arg0 == 10) { + return 1; + } + if (arg0 == 11) { + return 0; + } + if (arg0 == 12) { + return 1; + } + if (arg0 == 13) { + return 0; + } + if (arg0 == 14) { + return 1; + } + return 0; +} + diff --git a/samples/outputs/sample40 b/samples/outputs/sample40 index 4305c1a..002d27f 100644 --- a/samples/outputs/sample40 +++ b/samples/outputs/sample40 @@ -26,35 +26,28 @@ int match_re (char* arg1) { if (var2 < var1) { var3 = 0; return var3; - } else { - return 1; - } - } else { - var3 = 0; - return var3; - } - } else { - var5 = 0; - return var5; - } - } else { - if (arg1[var2] == 100) { - goto label2; + } + return 1; + } + var3 = 0; + return var3; } - var3 = 0; - return var3; - } - } else { - var5 = 0; - return var5; - } - } else { - var3 = 0; - return var3; - } - } else { - var5 = 0; - return var5; - } + var5 = 0; + return var5; + } + if (arg1[var2] == 100) { + goto label2; + } + var3 = 0; + return var3; + } + var5 = 0; + return var5; + } + var3 = 0; + return var3; + } + var5 = 0; + return var5; } diff --git a/samples/outputs/sample52 b/samples/outputs/sample52 new file mode 100644 index 0000000..009efe3 --- /dev/null +++ b/samples/outputs/sample52 @@ -0,0 +1,49 @@ +int isEven (int arg0) { + if (arg0 == 0) { + return 1; + } + if (arg0 == 1) { + return 0; + } + if (arg0 == 2) { + return 1; + } + if (arg0 == 3) { + return 0; + } + if (arg0 == 4) { + return 1; + } + if (arg0 == 5) { + return 0; + } + if (arg0 == 6) { + return 1; + } + if (arg0 == 7) { + return 0; + } + if (arg0 == 8) { + return 1; + } + if (arg0 == 9) { + return 0; + } + if (arg0 == 10) { + return 1; + } + if (arg0 == 11) { + return 0; + } + if (arg0 == 12) { + return 1; + } + if (arg0 == 13) { + return 0; + } + if (arg0 == 14) { + return 1; + } + return 0; +} + diff --git a/samples/sample52.cpp b/samples/sample52.cpp new file mode 100644 index 0000000..9cd638c --- /dev/null +++ b/samples/sample52.cpp @@ -0,0 +1,22 @@ +// Include the headers +#include "blocks/c_code_generator.h" +#include "builder/dyn_var.h" +#include "builder/lib/utils.h" +#include "builder/static_var.h" +#include + +// Include the BuildIt types +using builder::dyn_var; +using builder::static_var; + +static dyn_var isEven(dyn_var x) { + static_var xs = builder::up_cast_range(x, 16); + return (xs % 2) == 0; +} + +int main(int argc, char *argv[]) { + builder::builder_context context; + context.run_rce = true; + block::c_code_generator::generate_code(context.extract_function_ast(isEven, "isEven"), std::cout, 0); + return 0; +} diff --git a/src/builder/builder_context.cpp b/src/builder/builder_context.cpp index 0a3ccd0..13ee079 100644 --- a/src/builder/builder_context.cpp +++ b/src/builder/builder_context.cpp @@ -243,14 +243,17 @@ trim_common_from_back(block::stmt::Ptr ast1, block::stmt::Ptr ast2) { // the common part This has to be checked only in the end because gotos // can appear on both the sides and should be trimmed of before + // Also allow this optimization if one of the branch ends in return if (ast1_stmts.size() != 0 && ast2_stmts.size() != 0) { - if (block::isa(ast1_stmts.back())) { + if (block::isa(ast1_stmts.back()) || + block::isa(ast1_stmts.back())) { while (ast2_stmts.size() > 0) { block::stmt::Ptr trimmed_stmt = ast2_stmts.back(); ast2_stmts.pop_back(); trimmed_stmts.push_back(trimmed_stmt); } - } else if (block::isa(ast2_stmts.back())) { + } else if (block::isa(ast2_stmts.back()) || + block::isa(ast2_stmts.back())) { while (ast1_stmts.size() > 0) { block::stmt::Ptr trimmed_stmt = ast1_stmts.back(); ast1_stmts.pop_back(); diff --git a/src/builder/builder_dynamic.cpp b/src/builder/builder_dynamic.cpp index 0667b6d..50ecc2c 100644 --- a/src/builder/builder_dynamic.cpp +++ b/src/builder/builder_dynamic.cpp @@ -33,8 +33,8 @@ void *compile_and_return_ptr(builder_context &context, std::string source_name, compiler_name += " -std=c++11 -fPIC "; } - std::string compile_command = compiler_name + " -shared -O3 " + source_name + " -o " + compiled_name + " " - + context.dynamic_compiler_flags; + std::string compile_command = compiler_name + " -shared -O3 " + source_name + " -o " + compiled_name + " " + + context.dynamic_compiler_flags; int err = system(compile_command.c_str()); if (err != 0) {