@@ -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 ();
0 commit comments