Skip to content

Commit c1a3645

Browse files
[blocks] Move basic_blocks to block namespace and few other changes
This patch also moves the debug statements to a separate function and calls them outside of builder_context.cpp. It also cleans up stray debug statements.
1 parent 1743ea5 commit c1a3645

File tree

3 files changed

+49
-41
lines changed

3 files changed

+49
-41
lines changed

include/blocks/basic_blocks.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,29 @@
66
#include <string>
77
#include <map>
88

9+
namespace block {
910
class basic_block {
1011
public:
1112
typedef std::vector<std::shared_ptr<basic_block>> cfg_block;
1213
basic_block(std::string label): name(label) {};
1314

1415
cfg_block predecessor;
1516
cfg_block successor;
16-
block::expr::Ptr branch_expr;
17+
expr::Ptr branch_expr;
1718
std::shared_ptr<basic_block> then_branch;
1819
std::shared_ptr<basic_block> else_branch;
1920
std::shared_ptr<basic_block> exit_block;
2021
bool is_exit_block = false;
21-
block::stmt::Ptr parent;
22+
stmt::Ptr parent;
2223
unsigned int ast_index;
2324
unsigned int ast_depth;
2425
unsigned int id;
2526
std::string name;
26-
static std::map<block::stmt::Ptr, std::shared_ptr<basic_block>> ast_to_basic_block_map;
27+
static std::map<stmt::Ptr, std::shared_ptr<basic_block>> ast_to_basic_block_map;
2728
};
2829

29-
basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast);
30+
basic_block::cfg_block generate_basic_blocks(stmt_block::Ptr ast);
31+
void dump(basic_block::cfg_block basic_block_list);
32+
} // namespace block
3033

3134
#endif

src/blocks/basic_blocks.cpp

+39-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include "blocks/basic_blocks.h"
22
#include <algorithm>
33

4-
using namespace block;
5-
std::map<block::stmt::Ptr, std::shared_ptr<basic_block>> basic_block::ast_to_basic_block_map = {};
4+
namespace block {
5+
std::map<stmt::Ptr, std::shared_ptr<basic_block>> basic_block::ast_to_basic_block_map = {};
66

7-
basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
7+
basic_block::cfg_block generate_basic_blocks(stmt_block::Ptr ast) {
88
std::deque<std::shared_ptr<basic_block>> work_list;
99
basic_block::cfg_block return_list;
1010
int basic_block_count = 0;
@@ -29,7 +29,7 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
2929
while (work_list.size()) {
3030
auto bb = work_list.front();
3131

32-
if (isa<block::stmt_block>(bb->parent)) {
32+
if (isa<stmt_block>(bb->parent)) {
3333
ast_index_counter = 0;
3434
stmt_block::Ptr stmt_block_ = to<stmt_block>(bb->parent);
3535
bb->name = "stmt" + bb->name;
@@ -95,7 +95,6 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
9595
work_list.pop_front();
9696
// push the exit block to the work_list
9797
work_list.push_front(exit_bb);
98-
std::cerr << "inside if handler: " << bb->name << "\n";
9998
// if there is a then_stmt, create a basic block for it
10099
if (to<stmt_block>(if_stmt_->then_stmt)->stmts.size() != 0) {
101100
auto then_bb = std::make_shared<basic_block>(std::to_string(++basic_block_count));
@@ -111,7 +110,6 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
111110
bb->then_branch = then_bb;
112111
// push the block to the work_list, to expand it further
113112
work_list.push_front(then_bb);
114-
std::cerr << "inside then" << "\n";
115113
}
116114
// if there is a else_stmt, create a basic block for it
117115
if (to<stmt_block>(if_stmt_->else_stmt)->stmts.size() != 0) {
@@ -128,7 +126,6 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
128126
bb->else_branch = else_bb;
129127
// push the block to the work_list, to expand it further
130128
work_list.insert(work_list.begin() + 1, else_bb);
131-
std::cerr << "inside else" << "\n";
132129
}
133130

134131
// 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) {
143140

144141
return_list.push_back(bb);
145142
}
146-
else if (isa<block::expr_stmt>(bb->parent)) {
143+
else if (isa<expr_stmt>(bb->parent)) {
147144
bb->name = "expr" + bb->name;
148145
return_list.push_back(bb);
149146
work_list.pop_front();
150147
}
151-
else if (isa<block::decl_stmt>(bb->parent)) {
148+
else if (isa<decl_stmt>(bb->parent)) {
152149
bb->name = "decl" + bb->name;
153150
return_list.push_back(bb);
154151
work_list.pop_front();
155152
}
156-
else if (isa<block::label_stmt>(bb->parent)) {
153+
else if (isa<label_stmt>(bb->parent)) {
157154
bb->name = "label" + bb->name;
158155
return_list.push_back(bb);
159156
work_list.pop_front();
160157
}
161-
else if (isa<block::goto_stmt>(bb->parent)) {
158+
else if (isa<goto_stmt>(bb->parent)) {
162159
bb->name = "goto" + bb->name;
163160
return_list.push_back(bb);
164161
work_list.pop_front();
165162
}
166-
else if (isa<block::return_stmt>(bb->parent)) {
163+
else if (isa<return_stmt>(bb->parent)) {
167164
bb->name = "return" + bb->name;
168165
return_list.push_back(bb);
169166
work_list.pop_front();
@@ -174,7 +171,7 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
174171

175172
// step 4: resolve goto calls to successors of labels
176173
for (auto bb: return_list) {
177-
if (isa<block::goto_stmt>(bb->parent)) {
174+
if (isa<goto_stmt>(bb->parent)) {
178175
auto goto_source = std::find_if(return_list.begin(), return_list.end(),
179176
[bb](std::shared_ptr<basic_block> bb_l) {
180177
if (isa<label_stmt>(bb_l->parent)) {
@@ -206,5 +203,33 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
206203
bb->ast_to_basic_block_map[bb->parent] = bb;
207204
}
208205

206+
// print debug logs
207+
// #ifdef BASIC_BLOCK_DEBUG
208+
dump(return_list);
209+
// #endif
209210
return return_list;
210-
}
211+
}
212+
213+
void dump(basic_block::cfg_block basic_block_list) {
214+
std::cerr << "++++++ basic blocks ++++++ \n";
215+
for (auto bb: basic_block_list) {
216+
std::cerr << bb->id << ":" << bb->name << ":" << " ; ";
217+
for (auto pred: bb->predecessor) {
218+
std::cerr << pred->name << ", ";
219+
}
220+
std::cerr << bb->ast_depth;
221+
std::cerr << "\n";
222+
if (bb->branch_expr) {
223+
std::cerr << " ";
224+
bb->branch_expr->dump(std::cerr, 0);
225+
}
226+
std::cerr << " ";
227+
std::cerr << "br ";
228+
for (auto branches: bb->successor) {
229+
std::cerr << branches->name << ", ";
230+
}
231+
std::cerr << "\n";
232+
}
233+
std::cerr << "++++++ basic blocks ++++++ \n";
234+
}
235+
} // namespace block

src/builder/builder_context.cpp

+3-23
Original file line numberDiff line numberDiff line change
@@ -306,27 +306,7 @@ block::stmt::Ptr builder_context::extract_ast_from_function_impl(void) {
306306
if (feature_unstructured)
307307
return ast;
308308

309-
basic_block::cfg_block BBs = generate_basic_blocks(block::to<block::stmt_block>(ast));
310-
std::cerr << "++++++ basic blocks ++++++ \n";
311-
for (auto bb: BBs) {
312-
std::cerr << bb->id << ":" << bb->name << ":" << " ; ";
313-
for (auto pred: bb->predecessor) {
314-
std::cerr << pred->name << ", ";
315-
}
316-
std::cerr << bb->ast_depth;
317-
std::cerr << "\n";
318-
if (bb->branch_expr) {
319-
std::cerr << " ";
320-
bb->branch_expr->dump(std::cerr, 0);
321-
}
322-
std::cerr << " ";
323-
std::cerr << "br ";
324-
for (auto branches: bb->successor) {
325-
std::cerr << branches->name << ", ";
326-
}
327-
std::cerr << "\n";
328-
}
329-
std::cerr << "++++++ basic blocks ++++++ \n";
309+
block::basic_block::cfg_block BBs = generate_basic_blocks(block::to<block::stmt_block>(ast));
330310

331311
block::loop_finder finder;
332312
finder.ast = ast;
@@ -442,10 +422,10 @@ block::stmt::Ptr builder_context::extract_ast_from_function_internal(std::vector
442422
add_stmt_to_current_block(goto_stmt, false);
443423
} else {
444424
for (unsigned int i = e.child_id; i < e.parent->stmts.size(); i++) {
445-
if (isa<block::goto_stmt>(e.parent->stmts[i])) {
425+
if (block::isa<block::goto_stmt>(e.parent->stmts[i])) {
446426
block::goto_stmt::Ptr goto_stmt = std::make_shared<block::goto_stmt>();
447427
goto_stmt->static_offset.clear();
448-
goto_stmt->temporary_label_number = to<block::goto_stmt>(e.parent->stmts[i])->temporary_label_number;
428+
goto_stmt->temporary_label_number = block::to<block::goto_stmt>(e.parent->stmts[i])->temporary_label_number;
449429
add_stmt_to_current_block(goto_stmt, false);
450430
}
451431
else {

0 commit comments

Comments
 (0)