Skip to content

Commit

Permalink
Move the CFG generator driver to builder_context.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
VedantParanjape committed Jun 20, 2023
1 parent 853ea5d commit 7590972
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 40 deletions.
1 change: 1 addition & 0 deletions include/blocks/basic_blocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class basic_block {
std::vector<std::shared_ptr<basic_block>> successor;
block::expr::Ptr branch_expr;
block::stmt::Ptr parent;
unsigned int index;
std::string name;
};

Expand Down
1 change: 0 additions & 1 deletion include/blocks/loop_finder.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define LOOP_FINDER_H
#include "blocks/block_visitor.h"
#include "blocks/stmt.h"
#include "blocks/basic_blocks.h"

namespace block {
class loop_finder : public block_visitor {
Expand Down
1 change: 1 addition & 0 deletions include/builder/builder_context.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef BUILDER_CONTEXT
#define BUILDER_CONTEXT
#include "blocks/basic_blocks.h"
#include "blocks/expr.h"
#include "blocks/stmt.h"
#include "builder/forward_declarations.h"
Expand Down
17 changes: 13 additions & 4 deletions src/blocks/basic_blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ std::vector<std::shared_ptr<basic_block>> generate_basic_blocks(block::stmt_bloc
for (auto st: ast->stmts) {
auto bb = std::make_shared<basic_block>(std::to_string(basic_block_count));
bb->parent = st;
// bb->index = ;
work_list.push_back(bb);
basic_block_count++;
}
Expand All @@ -21,7 +22,7 @@ std::vector<std::shared_ptr<basic_block>> generate_basic_blocks(block::stmt_bloc
work_list[i]->successor.push_back(work_list[i+1]);
}

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

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

if (stmt_block_->stmts.size() > 0) {
std::vector<std::shared_ptr<basic_block>> stmt_block_list;


// convert all statements of this stmt_block into a basic block
for (auto st: stmt_block_->stmts) {
stmt_block_list.push_back(std::make_shared<basic_block>(std::to_string(basic_block_count++)));
stmt_block_list.back()->parent = st;
}


// set the basic block successors
for (unsigned i = 0; stmt_block_list.size() != 0 && i < stmt_block_list.size() - 1; i++) {
stmt_block_list[i]->successor.push_back(stmt_block_list[i+1]);
}

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

// push a rather empty-ish basic block, which will branch to the next basic block, or the next statement.
return_list.push_back(bb);
work_list.pop_front();
// now insert the pending blocks to be processed at the front of the work_list
work_list.insert(work_list.begin(), stmt_block_list.begin(), stmt_block_list.end());
}
else {
Expand All @@ -67,7 +76,7 @@ std::vector<std::shared_ptr<basic_block>> generate_basic_blocks(block::stmt_bloc
exit_bb->parent = std::make_shared<stmt_block>();
// check if this is the last block, if yes the successor will be empty
if (bb->successor.size()) {
// set the successor to the block that if_stmt pointer to earlier
// set the successor to the block that if_stmt successor pointer to earlier
exit_bb->successor.push_back(bb->successor.front());
// clear the successor block from the if_stmt
bb->successor.clear();
Expand Down
48 changes: 13 additions & 35 deletions src/blocks/loop_finder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,43 +129,21 @@ static void trim_from_parents(std::vector<stmt_block::Ptr> &parents, std::vector
}

void loop_finder::visit(stmt_block::Ptr a) {
std::vector<std::shared_ptr<basic_block>> BBs = generate_basic_blocks(a);

std::cout << "++++++ basic blocks ++++++ \n";
for (auto bb: BBs) {
std::cout << bb->name << ":" << " ; ";
for (auto pred: bb->predecessor) {
std::cout << pred->name << ", ";
}
std::cout << "\n";
if (bb->branch_expr) {
std::cout << " ";
bb->branch_expr->dump(std::cout, 0);
}
std::cout << " ";
std::cout << "br ";
for (auto branches: bb->successor) {
std::cout << branches->name << ", ";
while (1) {
label_stmt::Ptr found_label = nullptr;
for (auto stmt : a->stmts) {
if (isa<label_stmt>(stmt)) {
found_label = to<label_stmt>(stmt);
}
}
std::cout << "\n";
if (found_label == nullptr)
break;
visit_label(found_label, a);
}
// Once all labels are done, visit the instructions normally
for (auto stmt : a->stmts) {
stmt->accept(this);
}
std::cout << "++++++ basic blocks ++++++ \n";

// while (1) {
// label_stmt::Ptr found_label = nullptr;
// for (auto stmt : a->stmts) {
// if (isa<label_stmt>(stmt)) {
// found_label = to<label_stmt>(stmt);
// }
// }
// if (found_label == nullptr)
// break;
// visit_label(found_label, a);
// }
// // Once all labels are done, visit the instructions normally
// for (auto stmt : a->stmts) {
// stmt->accept(this);
// }
}
void loop_finder::visit_label(label_stmt::Ptr a, stmt_block::Ptr parent) {

Expand Down
22 changes: 22 additions & 0 deletions src/builder/builder_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,28 @@ block::stmt::Ptr builder_context::extract_ast_from_function_impl(void) {
block::eliminate_redundant_vars(ast);
}

std::vector<std::shared_ptr<basic_block>> BBs = generate_basic_blocks(block::to<block::stmt_block>(ast));

std::cerr << "++++++ basic blocks ++++++ \n";
for (auto bb: BBs) {
std::cerr << bb->name << ":" << " ; ";
for (auto pred: bb->predecessor) {
std::cerr << pred->name << ", ";
}
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";

if (feature_unstructured)
return ast;

Expand Down

0 comments on commit 7590972

Please sign in to comment.