Skip to content

Commit

Permalink
WIP v2: Add infra to construct a while loop using the loop_info analy…
Browse files Browse the repository at this point in the history
…sis (34/48 tests pass)
  • Loading branch information
VedantParanjape committed Sep 3, 2023
1 parent 94be2dd commit 67ed12f
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 69 deletions.
2 changes: 2 additions & 0 deletions include/blocks/basic_blocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <vector>
#include <deque>
#include <string>
#include <map>

class basic_block {
public:
Expand All @@ -18,6 +19,7 @@ class basic_block {
unsigned int ast_depth;
unsigned int id;
std::string name;
// static std::map<block::stmt::Ptr, std::shared_ptr<basic_block>> ast_to_basic_block_map;
};

basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast);
Expand Down
2 changes: 2 additions & 0 deletions include/blocks/loops.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class loop_info {
std::shared_ptr<loop> allocate_loop(std::shared_ptr<basic_block> header);
block::stmt_block::Ptr convert_to_ast(block::stmt_block::Ptr ast);
std::map<unsigned int, std::vector<int>> postorder_loops_map;
std::map<unsigned int, std::vector<int>> preorder_loops_map;
std::vector<std::shared_ptr<loop>> loops;
std::vector<std::shared_ptr<loop>> top_level_loops;

Expand All @@ -48,6 +49,7 @@ class loop_info {
dominator_analysis dta;
std::map<int, std::shared_ptr<loop>> bb_loop_map;
void postorder_dfs_helper(std::vector<int> &postorder_loops_map, std::vector<bool> &visited_loops, int id);
void preorder_dfs_helper(std::vector<int> &preorder_loops_map, std::vector<bool> &visited_loops, int id);
// discover loops during traversal of the abstract syntax tree
void analyze();
};
Expand Down
8 changes: 8 additions & 0 deletions src/blocks/basic_blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
for (auto st: ast->stmts) {
auto bb = std::make_shared<basic_block>(std::to_string(basic_block_count));
bb->parent = st;
// bb->ast_to_basic_block_map[bb->parent] = bb;
bb->ast_index = ast_index_counter++;
bb->ast_depth = 0;
work_list.push_back(bb);
Expand Down Expand Up @@ -40,6 +41,7 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
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;
// stmt_block_list.back()->ast_to_basic_block_map[bb->parent] = stmt_block_list.back();
stmt_block_list.back()->ast_index = ast_index_counter++;
stmt_block_list.back()->ast_depth = bb->ast_depth + 1;
}
Expand Down Expand Up @@ -79,6 +81,8 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
auto exit_bb = std::make_shared<basic_block>("exit" + std::to_string(basic_block_count));
// assign it a empty stmt_block as parent
exit_bb->parent = std::make_shared<stmt_block>();
// add mapping in ast to bb map
// exit_bb->ast_to_basic_block_map[exit_bb->parent] = exit_bb;
// set the ast depth of the basic block
exit_bb->ast_depth = bb->ast_depth;
// check if this is the last block, if yes the successor will be empty
Expand All @@ -98,6 +102,8 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
auto then_bb = std::make_shared<basic_block>(std::to_string(++basic_block_count));
// set the parent of this block as the then stmts
then_bb->parent = if_stmt_->then_stmt;
// add mapping in ast to bb map
// then_bb->ast_to_basic_block_map[then_bb->parent] = then_bb;
// set the ast depth of the basic block
then_bb->ast_depth = bb->ast_depth;
// set the successor of this block to be the exit block
Expand All @@ -112,6 +118,8 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
auto else_bb = std::make_shared<basic_block>(std::to_string(++basic_block_count));
// set the parent of this block as the else stmts
else_bb->parent = if_stmt_->else_stmt;
// add mapping in ast to bb map
// else_bb->ast_to_basic_block_map[else_bb->parent] = else_bb;
// set the ast depth of the basic block
else_bb->ast_depth = bb->ast_depth;
// set the successor of this block to be the exit block
Expand Down
Loading

0 comments on commit 67ed12f

Please sign in to comment.