Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[blocks] Add basic blocks for CFG representation of the AST #64

Merged

Conversation

VedantParanjape
Copy link
Contributor

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.

          |----<then_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 ++++++

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.

              |----<then_block>----|
<if_block>----|                    |----<exit_block>
	      |----<else_block>----|

* buildit input source code
dyn_var<int> a = 0;
for (dyn_var<int> c = 0; c < 100; c = c + 3) {
	for (dyn_var<int> 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 ++++++
@VedantParanjape
Copy link
Contributor Author

VedantParanjape commented Jan 30, 2024

@AjayBrahmakshatriya GitHub is not able to render the commit message correctly, it looks fine in the commit tab.

This patch also moves the debug statements to a separate function and
calls them outside of builder_context.cpp. It also cleans up stray debug
statements.
@AjayBrahmakshatriya AjayBrahmakshatriya merged commit c37ad9a into BuildIt-lang:master Feb 26, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants