Skip to content

Commit

Permalink
Basic implementation for defering jumps from the loop that cut throug…
Browse files Browse the repository at this point in the history
…h the outer loop
  • Loading branch information
AjayBrahmakshatriya committed Oct 3, 2023
1 parent d065b21 commit 521ca37
Show file tree
Hide file tree
Showing 5 changed files with 453 additions and 53 deletions.
11 changes: 11 additions & 0 deletions include/blocks/loop_finder.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class loop_finder : public block_visitor {
public:
using block_visitor::visit;
stmt::Ptr ast;

int loop_hook_counter = 0;
void visit_label(label_stmt::Ptr, stmt_block::Ptr);
virtual void visit(stmt_block::Ptr);
};
Expand All @@ -26,5 +28,14 @@ class continue_finder : public block_visitor {
bool has_continue = false;
virtual void visit(continue_stmt::Ptr);
};

class outer_jump_finder : public block_visitor {
public:
using block_visitor::visit;
int &loop_hook_counter;
outer_jump_finder(int &lc) : loop_hook_counter(lc) {}
std::vector<std::pair<var::Ptr, stmt::Ptr>> created_vars;
virtual void visit(stmt_block::Ptr);
};
} // namespace block
#endif
125 changes: 125 additions & 0 deletions samples/outputs.var_names/sample51
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
FUNC_DECL
SCALAR_TYPE (VOID)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (INT)
VAR (x_0)
INT_CONST (5)
WHILE_STMT
LT_EXPR
VAR_EXPR
VAR (x_0)
INT_CONST (10)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (INT)
VAR (control_guard0)
INT_CONST (0)
WHILE_STMT
GT_EXPR
VAR_EXPR
VAR (x_0)
INT_CONST (100)
STMT_BLOCK
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (control_guard0)
INT_CONST (0)
IF_STMT
EQUALS_EXPR
VAR_EXPR
VAR (x_0)
INT_CONST (0)
STMT_BLOCK
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (x_0)
PLUS_EXPR
VAR_EXPR
VAR (x_0)
INT_CONST (5)
WHILE_STMT
NE_EXPR
VAR_EXPR
VAR (x_0)
INT_CONST (-1)
STMT_BLOCK
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (x_0)
PLUS_EXPR
VAR_EXPR
VAR (x_0)
INT_CONST (6)
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (control_guard0)
INT_CONST (1)
BREAK_STMT
STMT_BLOCK
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (x_0)
PLUS_EXPR
VAR_EXPR
VAR (x_0)
INT_CONST (4)
IF_STMT
VAR_EXPR
VAR (control_guard0)
STMT_BLOCK
CONTINUE_STMT
STMT_BLOCK
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (x_0)
PLUS_EXPR
VAR_EXPR
VAR (x_0)
INT_CONST (5)
WHILE_STMT
NE_EXPR
VAR_EXPR
VAR (x_0)
INT_CONST (-1)
STMT_BLOCK
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (x_0)
PLUS_EXPR
VAR_EXPR
VAR (x_0)
INT_CONST (6)
void bar (void) {
int x_0 = 5;
while (x_0 < 10) {
int control_guard0 = 0;
while (x_0 > 100) {
control_guard0 = 0;
if (x_0 == 0) {
x_0 = x_0 + 5;
while (x_0 != -1) {
x_0 = x_0 + 6;
}
control_guard0 = 1;
break;
}
x_0 = x_0 + 4;
}
if (control_guard0) {
continue;
}
x_0 = x_0 + 5;
while (x_0 != -1) {
x_0 = x_0 + 6;
}
}
}

125 changes: 125 additions & 0 deletions samples/outputs/sample51
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
FUNC_DECL
SCALAR_TYPE (VOID)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (INT)
VAR (var0)
INT_CONST (5)
WHILE_STMT
LT_EXPR
VAR_EXPR
VAR (var0)
INT_CONST (10)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (INT)
VAR (control_guard0)
INT_CONST (0)
WHILE_STMT
GT_EXPR
VAR_EXPR
VAR (var0)
INT_CONST (100)
STMT_BLOCK
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (control_guard0)
INT_CONST (0)
IF_STMT
EQUALS_EXPR
VAR_EXPR
VAR (var0)
INT_CONST (0)
STMT_BLOCK
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (var0)
PLUS_EXPR
VAR_EXPR
VAR (var0)
INT_CONST (5)
WHILE_STMT
NE_EXPR
VAR_EXPR
VAR (var0)
INT_CONST (-1)
STMT_BLOCK
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (var0)
PLUS_EXPR
VAR_EXPR
VAR (var0)
INT_CONST (6)
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (control_guard0)
INT_CONST (1)
BREAK_STMT
STMT_BLOCK
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (var0)
PLUS_EXPR
VAR_EXPR
VAR (var0)
INT_CONST (4)
IF_STMT
VAR_EXPR
VAR (control_guard0)
STMT_BLOCK
CONTINUE_STMT
STMT_BLOCK
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (var0)
PLUS_EXPR
VAR_EXPR
VAR (var0)
INT_CONST (5)
WHILE_STMT
NE_EXPR
VAR_EXPR
VAR (var0)
INT_CONST (-1)
STMT_BLOCK
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (var0)
PLUS_EXPR
VAR_EXPR
VAR (var0)
INT_CONST (6)
void bar (void) {
int var0 = 5;
while (var0 < 10) {
int control_guard0 = 0;
while (var0 > 100) {
control_guard0 = 0;
if (var0 == 0) {
var0 = var0 + 5;
while (var0 != -1) {
var0 = var0 + 6;
}
control_guard0 = 1;
break;
}
var0 = var0 + 4;
}
if (control_guard0) {
continue;
}
var0 = var0 + 5;
while (var0 != -1) {
var0 = var0 + 6;
}
}
}

31 changes: 31 additions & 0 deletions samples/sample51.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "blocks/c_code_generator.h"
#include "builder/builder_context.h"
#include "builder/dyn_var.h"
#include "builder/static_var.h"
#include <iostream>
using builder::dyn_var;
using builder::static_var;

static void bar(void) {
// This example tests the case where a jump cuts
// through the parent loop
dyn_var<int> x = 5;
while (x < 10) {
while (x > 100) {
if (x == 0)
break;
x = x + 4;
}
x = x + 5;
while (x != -1)
x = x + 6;
}
}

int main(int argc, char *argv[]) {
builder::builder_context context;
auto ast = context.extract_function_ast(bar, "bar");
ast->dump(std::cout, 0);
block::c_code_generator::generate_code(ast, std::cout, 0);
return 0;
}
Loading

0 comments on commit 521ca37

Please sign in to comment.