1
1
#include " blocks/basic_blocks.h"
2
2
#include < algorithm>
3
3
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 = {};
6
6
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) {
8
8
std::deque<std::shared_ptr<basic_block>> work_list;
9
9
basic_block::cfg_block return_list;
10
10
int basic_block_count = 0 ;
@@ -29,7 +29,7 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
29
29
while (work_list.size ()) {
30
30
auto bb = work_list.front ();
31
31
32
- if (isa<block:: stmt_block>(bb->parent )) {
32
+ if (isa<stmt_block>(bb->parent )) {
33
33
ast_index_counter = 0 ;
34
34
stmt_block::Ptr stmt_block_ = to<stmt_block>(bb->parent );
35
35
bb->name = " stmt" + bb->name ;
@@ -95,7 +95,6 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
95
95
work_list.pop_front ();
96
96
// push the exit block to the work_list
97
97
work_list.push_front (exit_bb);
98
- std::cerr << " inside if handler: " << bb->name << " \n " ;
99
98
// if there is a then_stmt, create a basic block for it
100
99
if (to<stmt_block>(if_stmt_->then_stmt )->stmts .size () != 0 ) {
101
100
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) {
111
110
bb->then_branch = then_bb;
112
111
// push the block to the work_list, to expand it further
113
112
work_list.push_front (then_bb);
114
- std::cerr << " inside then" << " \n " ;
115
113
}
116
114
// if there is a else_stmt, create a basic block for it
117
115
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) {
128
126
bb->else_branch = else_bb;
129
127
// push the block to the work_list, to expand it further
130
128
work_list.insert (work_list.begin () + 1 , else_bb);
131
- std::cerr << " inside else" << " \n " ;
132
129
}
133
130
134
131
// 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) {
143
140
144
141
return_list.push_back (bb);
145
142
}
146
- else if (isa<block:: expr_stmt>(bb->parent )) {
143
+ else if (isa<expr_stmt>(bb->parent )) {
147
144
bb->name = " expr" + bb->name ;
148
145
return_list.push_back (bb);
149
146
work_list.pop_front ();
150
147
}
151
- else if (isa<block:: decl_stmt>(bb->parent )) {
148
+ else if (isa<decl_stmt>(bb->parent )) {
152
149
bb->name = " decl" + bb->name ;
153
150
return_list.push_back (bb);
154
151
work_list.pop_front ();
155
152
}
156
- else if (isa<block:: label_stmt>(bb->parent )) {
153
+ else if (isa<label_stmt>(bb->parent )) {
157
154
bb->name = " label" + bb->name ;
158
155
return_list.push_back (bb);
159
156
work_list.pop_front ();
160
157
}
161
- else if (isa<block:: goto_stmt>(bb->parent )) {
158
+ else if (isa<goto_stmt>(bb->parent )) {
162
159
bb->name = " goto" + bb->name ;
163
160
return_list.push_back (bb);
164
161
work_list.pop_front ();
165
162
}
166
- else if (isa<block:: return_stmt>(bb->parent )) {
163
+ else if (isa<return_stmt>(bb->parent )) {
167
164
bb->name = " return" + bb->name ;
168
165
return_list.push_back (bb);
169
166
work_list.pop_front ();
@@ -174,7 +171,7 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
174
171
175
172
// step 4: resolve goto calls to successors of labels
176
173
for (auto bb: return_list) {
177
- if (isa<block:: goto_stmt>(bb->parent )) {
174
+ if (isa<goto_stmt>(bb->parent )) {
178
175
auto goto_source = std::find_if (return_list.begin (), return_list.end (),
179
176
[bb](std::shared_ptr<basic_block> bb_l) {
180
177
if (isa<label_stmt>(bb_l->parent )) {
@@ -206,5 +203,33 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
206
203
bb->ast_to_basic_block_map [bb->parent ] = bb;
207
204
}
208
205
206
+ // print debug logs
207
+ #ifdef BASIC_BLOCK_DEBUG
208
+ dump (return_list);
209
+ #endif
209
210
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
0 commit comments