[blocks] Add basic blocks for CFG representation of the AST #64
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This patch adds a basic_block class and a function to translate the AST to a CFG representation. It also dumps the basic block to std::cerr in builder_context.cpp for debugging
The algorithm to convert the AST to CFG uses a worklist to do so, it first creates basic blocks for all the top level AST elements, and then using a worklist iteratively expands these top level AST elements, adding more basic blocks between them.
bb1 ---> bb2 ==> bb1 ---> (bb-a1...bb-an) ---> bb2
It also pads if statement blocks with an exit blocks. This makes it easier to handle loops, as we now have a single entry/exit into the if block.
<if_block>----| |----<exit_block>
|----<else_block>----|
buildit input source code dyn_var a = 0;
for (dyn_var c = 0; c < 100; c = c + 3) {
for (dyn_var b = 0; b < 10; b = b + 1) {
a = a + b; } }
output of std::cerr, dump of the generated basic blocks ++++++ basic blocks ++++++
0:decl0: ; 0
br decl1, 1:decl1: ; decl0, 0
br label2, 2:label2: ; decl1, goto13, 0
br if3, 3:if3: ; label2, 0
LT_EXPR VAR_EXPR VAR (var1) INT_CONST (100) br stmt8, stmtexit7, 4:stmt8: ; if3, 0
br decl9, 5:decl9: ; stmt8, 1
br label10, 6:label10: ; decl9, goto21, 1
br if11, 7:if11: ; label10, 1
LT_EXPR VAR_EXPR VAR (var2) INT_CONST (10) br stmt18, stmtexit17, 8:stmt18: ; if11, 1
br expr19, 9:expr19: ; stmt18, 2
br expr20, 10:expr20: ; expr19, 2
br goto21, 11:goto21: ; expr20, 2
br label10, 12:stmtexit17: ; if11, 1
br expr12, 13:expr12: ; stmtexit17, 1
br goto13, 14:goto13: ; expr12, 1
br label2, 15:stmtexit7: ; if3, 0
br ++++++ basic blocks ++++++