diff --git a/include/blocks/basic_blocks.h b/include/blocks/basic_blocks.h index ee4f6a9..f227a87 100644 --- a/include/blocks/basic_blocks.h +++ b/include/blocks/basic_blocks.h @@ -6,6 +6,7 @@ #include #include +namespace block { class basic_block { public: typedef std::vector> cfg_block; @@ -13,19 +14,21 @@ class basic_block { cfg_block predecessor; cfg_block successor; - block::expr::Ptr branch_expr; + expr::Ptr branch_expr; std::shared_ptr then_branch; std::shared_ptr else_branch; std::shared_ptr exit_block; bool is_exit_block = false; - block::stmt::Ptr parent; + stmt::Ptr parent; unsigned int ast_index; unsigned int ast_depth; unsigned int id; std::string name; - static std::map> ast_to_basic_block_map; + static std::map> ast_to_basic_block_map; }; -basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast); +basic_block::cfg_block generate_basic_blocks(stmt_block::Ptr ast); +void dump(basic_block::cfg_block basic_block_list); +} // namespace block #endif \ No newline at end of file diff --git a/src/blocks/basic_blocks.cpp b/src/blocks/basic_blocks.cpp index 90b41b8..09d2c3a 100644 --- a/src/blocks/basic_blocks.cpp +++ b/src/blocks/basic_blocks.cpp @@ -1,10 +1,10 @@ #include "blocks/basic_blocks.h" #include -using namespace block; -std::map> basic_block::ast_to_basic_block_map = {}; +namespace block { +std::map> basic_block::ast_to_basic_block_map = {}; -basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) { +basic_block::cfg_block generate_basic_blocks(stmt_block::Ptr ast) { std::deque> work_list; basic_block::cfg_block return_list; int basic_block_count = 0; @@ -29,7 +29,7 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) { while (work_list.size()) { auto bb = work_list.front(); - if (isa(bb->parent)) { + if (isa(bb->parent)) { ast_index_counter = 0; stmt_block::Ptr stmt_block_ = to(bb->parent); bb->name = "stmt" + bb->name; @@ -95,7 +95,6 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) { work_list.pop_front(); // push the exit block to the work_list work_list.push_front(exit_bb); - std::cerr << "inside if handler: " << bb->name << "\n"; // if there is a then_stmt, create a basic block for it if (to(if_stmt_->then_stmt)->stmts.size() != 0) { auto then_bb = std::make_shared(std::to_string(++basic_block_count)); @@ -111,7 +110,6 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) { bb->then_branch = then_bb; // push the block to the work_list, to expand it further work_list.push_front(then_bb); - std::cerr << "inside then" << "\n"; } // if there is a else_stmt, create a basic block for it if (to(if_stmt_->else_stmt)->stmts.size() != 0) { @@ -128,7 +126,6 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) { bb->else_branch = else_bb; // push the block to the work_list, to expand it further work_list.insert(work_list.begin() + 1, else_bb); - std::cerr << "inside else" << "\n"; } // if there is no then/else block, then have the exit block as successor as well. @@ -143,27 +140,27 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) { return_list.push_back(bb); } - else if (isa(bb->parent)) { + else if (isa(bb->parent)) { bb->name = "expr" + bb->name; return_list.push_back(bb); work_list.pop_front(); } - else if (isa(bb->parent)) { + else if (isa(bb->parent)) { bb->name = "decl" + bb->name; return_list.push_back(bb); work_list.pop_front(); } - else if (isa(bb->parent)) { + else if (isa(bb->parent)) { bb->name = "label" + bb->name; return_list.push_back(bb); work_list.pop_front(); } - else if (isa(bb->parent)) { + else if (isa(bb->parent)) { bb->name = "goto" + bb->name; return_list.push_back(bb); work_list.pop_front(); } - else if (isa(bb->parent)) { + else if (isa(bb->parent)) { bb->name = "return" + bb->name; return_list.push_back(bb); work_list.pop_front(); @@ -174,7 +171,7 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) { // step 4: resolve goto calls to successors of labels for (auto bb: return_list) { - if (isa(bb->parent)) { + if (isa(bb->parent)) { auto goto_source = std::find_if(return_list.begin(), return_list.end(), [bb](std::shared_ptr bb_l) { if (isa(bb_l->parent)) { @@ -206,5 +203,33 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) { bb->ast_to_basic_block_map[bb->parent] = bb; } + // print debug logs +#ifdef BASIC_BLOCK_DEBUG + dump(return_list); +#endif return return_list; -} \ No newline at end of file +} + +void dump(basic_block::cfg_block basic_block_list) { + std::cerr << "++++++ basic blocks ++++++ \n"; + for (auto bb: basic_block_list) { + std::cerr << bb->id << ":" << bb->name << ":" << " ; "; + for (auto pred: bb->predecessor) { + std::cerr << pred->name << ", "; + } + std::cerr << bb->ast_depth; + std::cerr << "\n"; + if (bb->branch_expr) { + std::cerr << " "; + bb->branch_expr->dump(std::cerr, 0); + } + std::cerr << " "; + std::cerr << "br "; + for (auto branches: bb->successor) { + std::cerr << branches->name << ", "; + } + std::cerr << "\n"; + } + std::cerr << "++++++ basic blocks ++++++ \n"; +} +} // namespace block \ No newline at end of file diff --git a/src/builder/builder_context.cpp b/src/builder/builder_context.cpp index 81211f3..94cc2b9 100644 --- a/src/builder/builder_context.cpp +++ b/src/builder/builder_context.cpp @@ -306,27 +306,7 @@ block::stmt::Ptr builder_context::extract_ast_from_function_impl(void) { if (feature_unstructured) return ast; - basic_block::cfg_block BBs = generate_basic_blocks(block::to(ast)); - std::cerr << "++++++ basic blocks ++++++ \n"; - for (auto bb: BBs) { - std::cerr << bb->id << ":" << bb->name << ":" << " ; "; - for (auto pred: bb->predecessor) { - std::cerr << pred->name << ", "; - } - std::cerr << bb->ast_depth; - std::cerr << "\n"; - if (bb->branch_expr) { - std::cerr << " "; - bb->branch_expr->dump(std::cerr, 0); - } - std::cerr << " "; - std::cerr << "br "; - for (auto branches: bb->successor) { - std::cerr << branches->name << ", "; - } - std::cerr << "\n"; - } - std::cerr << "++++++ basic blocks ++++++ \n"; + block::basic_block::cfg_block BBs = generate_basic_blocks(block::to(ast)); block::loop_finder finder; finder.ast = ast; @@ -442,10 +422,10 @@ block::stmt::Ptr builder_context::extract_ast_from_function_internal(std::vector add_stmt_to_current_block(goto_stmt, false); } else { for (unsigned int i = e.child_id; i < e.parent->stmts.size(); i++) { - if (isa(e.parent->stmts[i])) { + if (block::isa(e.parent->stmts[i])) { block::goto_stmt::Ptr goto_stmt = std::make_shared(); goto_stmt->static_offset.clear(); - goto_stmt->temporary_label_number = to(e.parent->stmts[i])->temporary_label_number; + goto_stmt->temporary_label_number = block::to(e.parent->stmts[i])->temporary_label_number; add_stmt_to_current_block(goto_stmt, false); } else {