Skip to content

Commit 7590972

Browse files
Move the CFG generator driver to builder_context.cpp
1 parent 853ea5d commit 7590972

File tree

6 files changed

+50
-40
lines changed

6 files changed

+50
-40
lines changed

include/blocks/basic_blocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class basic_block {
1414
std::vector<std::shared_ptr<basic_block>> successor;
1515
block::expr::Ptr branch_expr;
1616
block::stmt::Ptr parent;
17+
unsigned int index;
1718
std::string name;
1819
};
1920

include/blocks/loop_finder.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define LOOP_FINDER_H
33
#include "blocks/block_visitor.h"
44
#include "blocks/stmt.h"
5-
#include "blocks/basic_blocks.h"
65

76
namespace block {
87
class loop_finder : public block_visitor {

include/builder/builder_context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef BUILDER_CONTEXT
22
#define BUILDER_CONTEXT
3+
#include "blocks/basic_blocks.h"
34
#include "blocks/expr.h"
45
#include "blocks/stmt.h"
56
#include "builder/forward_declarations.h"

src/blocks/basic_blocks.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ std::vector<std::shared_ptr<basic_block>> generate_basic_blocks(block::stmt_bloc
1212
for (auto st: ast->stmts) {
1313
auto bb = std::make_shared<basic_block>(std::to_string(basic_block_count));
1414
bb->parent = st;
15+
// bb->index = ;
1516
work_list.push_back(bb);
1617
basic_block_count++;
1718
}
@@ -21,7 +22,7 @@ std::vector<std::shared_ptr<basic_block>> generate_basic_blocks(block::stmt_bloc
2122
work_list[i]->successor.push_back(work_list[i+1]);
2223
}
2324

24-
// step 3: process blocks
25+
// step 3: process blocks: every xx_stmt type statement is made out into a basic block
2526
while (work_list.size()) {
2627
auto bb = work_list.front();
2728

@@ -31,22 +32,30 @@ std::vector<std::shared_ptr<basic_block>> generate_basic_blocks(block::stmt_bloc
3132

3233
if (stmt_block_->stmts.size() > 0) {
3334
std::vector<std::shared_ptr<basic_block>> stmt_block_list;
34-
35+
36+
// convert all statements of this stmt_block into a basic block
3537
for (auto st: stmt_block_->stmts) {
3638
stmt_block_list.push_back(std::make_shared<basic_block>(std::to_string(basic_block_count++)));
3739
stmt_block_list.back()->parent = st;
3840
}
39-
41+
42+
// set the basic block successors
4043
for (unsigned i = 0; stmt_block_list.size() != 0 && i < stmt_block_list.size() - 1; i++) {
4144
stmt_block_list[i]->successor.push_back(stmt_block_list[i+1]);
4245
}
4346

47+
// since we insert these stmts between bb1 ---> bb2 ==> bb1 ---> (bb-a1...bb-an) ---> bb2
48+
// point the successor of the stmt_block_list to the basic block that bb1's successor
49+
// pointed to. After this, clear the bb1's successor and push the front of stmt_block_list
50+
// to bb1's successor list.
4451
stmt_block_list.back()->successor.push_back(bb->successor.front());
4552
bb->successor.clear();
4653
bb->successor.push_back(stmt_block_list.front());
4754

55+
// push a rather empty-ish basic block, which will branch to the next basic block, or the next statement.
4856
return_list.push_back(bb);
4957
work_list.pop_front();
58+
// now insert the pending blocks to be processed at the front of the work_list
5059
work_list.insert(work_list.begin(), stmt_block_list.begin(), stmt_block_list.end());
5160
}
5261
else {
@@ -67,7 +76,7 @@ std::vector<std::shared_ptr<basic_block>> generate_basic_blocks(block::stmt_bloc
6776
exit_bb->parent = std::make_shared<stmt_block>();
6877
// check if this is the last block, if yes the successor will be empty
6978
if (bb->successor.size()) {
70-
// set the successor to the block that if_stmt pointer to earlier
79+
// set the successor to the block that if_stmt successor pointer to earlier
7180
exit_bb->successor.push_back(bb->successor.front());
7281
// clear the successor block from the if_stmt
7382
bb->successor.clear();

src/blocks/loop_finder.cpp

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -129,43 +129,21 @@ static void trim_from_parents(std::vector<stmt_block::Ptr> &parents, std::vector
129129
}
130130

131131
void loop_finder::visit(stmt_block::Ptr a) {
132-
std::vector<std::shared_ptr<basic_block>> BBs = generate_basic_blocks(a);
133-
134-
std::cout << "++++++ basic blocks ++++++ \n";
135-
for (auto bb: BBs) {
136-
std::cout << bb->name << ":" << " ; ";
137-
for (auto pred: bb->predecessor) {
138-
std::cout << pred->name << ", ";
139-
}
140-
std::cout << "\n";
141-
if (bb->branch_expr) {
142-
std::cout << " ";
143-
bb->branch_expr->dump(std::cout, 0);
144-
}
145-
std::cout << " ";
146-
std::cout << "br ";
147-
for (auto branches: bb->successor) {
148-
std::cout << branches->name << ", ";
132+
while (1) {
133+
label_stmt::Ptr found_label = nullptr;
134+
for (auto stmt : a->stmts) {
135+
if (isa<label_stmt>(stmt)) {
136+
found_label = to<label_stmt>(stmt);
137+
}
149138
}
150-
std::cout << "\n";
139+
if (found_label == nullptr)
140+
break;
141+
visit_label(found_label, a);
142+
}
143+
// Once all labels are done, visit the instructions normally
144+
for (auto stmt : a->stmts) {
145+
stmt->accept(this);
151146
}
152-
std::cout << "++++++ basic blocks ++++++ \n";
153-
154-
// while (1) {
155-
// label_stmt::Ptr found_label = nullptr;
156-
// for (auto stmt : a->stmts) {
157-
// if (isa<label_stmt>(stmt)) {
158-
// found_label = to<label_stmt>(stmt);
159-
// }
160-
// }
161-
// if (found_label == nullptr)
162-
// break;
163-
// visit_label(found_label, a);
164-
// }
165-
// // Once all labels are done, visit the instructions normally
166-
// for (auto stmt : a->stmts) {
167-
// stmt->accept(this);
168-
// }
169147
}
170148
void loop_finder::visit_label(label_stmt::Ptr a, stmt_block::Ptr parent) {
171149

src/builder/builder_context.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,28 @@ block::stmt::Ptr builder_context::extract_ast_from_function_impl(void) {
292292
block::eliminate_redundant_vars(ast);
293293
}
294294

295+
std::vector<std::shared_ptr<basic_block>> BBs = generate_basic_blocks(block::to<block::stmt_block>(ast));
296+
297+
std::cerr << "++++++ basic blocks ++++++ \n";
298+
for (auto bb: BBs) {
299+
std::cerr << bb->name << ":" << " ; ";
300+
for (auto pred: bb->predecessor) {
301+
std::cerr << pred->name << ", ";
302+
}
303+
std::cerr << "\n";
304+
if (bb->branch_expr) {
305+
std::cerr << " ";
306+
bb->branch_expr->dump(std::cerr, 0);
307+
}
308+
std::cerr << " ";
309+
std::cerr << "br ";
310+
for (auto branches: bb->successor) {
311+
std::cerr << branches->name << ", ";
312+
}
313+
std::cerr << "\n";
314+
}
315+
std::cerr << "++++++ basic blocks ++++++ \n";
316+
295317
if (feature_unstructured)
296318
return ast;
297319

0 commit comments

Comments
 (0)