Skip to content

Commit

Permalink
Merge pull request #54 from AjayBrahmakshatriya/master
Browse files Browse the repository at this point in the history
Fixed the bug with labels not being duplicated across branches
  • Loading branch information
AjayBrahmakshatriya authored Sep 24, 2023
2 parents dd0a2cf + dd8fb87 commit d065b21
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 13 deletions.
11 changes: 11 additions & 0 deletions include/blocks/label_inserter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,19 @@ class label_creator : public block_visitor {
class label_inserter : public block_visitor {
public:
using block_visitor::visit;

// The main table to hold static tag to label mapping
// this table holds the jump target that is in the parent of the
// jump statement
std::unordered_map<std::string, label::Ptr> offset_to_label;
// A backup table which has atleast one label that we can jump to
// Only used when feature_unstructured is used
std::unordered_map<std::string, label::Ptr> backup_offset_to_label;

bool feature_unstructured;

virtual void visit(goto_stmt::Ptr);
virtual void visit(label_stmt::Ptr);
};
} // namespace block
#endif
56 changes: 56 additions & 0 deletions samples/outputs.var_names/sample50
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
FUNC_DECL
SCALAR_TYPE (VOID)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (INT)
VAR (k_0)
INT_CONST (0)
DECL_STMT
SCALAR_TYPE (INT)
VAR (t_1)
INT_CONST (0)
WHILE_STMT
INT_CONST (1)
STMT_BLOCK
IF_STMT
NE_EXPR
VAR_EXPR
VAR (k_0)
INT_CONST (1)
STMT_BLOCK
IF_STMT
EQUALS_EXPR
VAR_EXPR
VAR (k_0)
INT_CONST (2)
STMT_BLOCK
WHILE_STMT
NE_EXPR
VAR_EXPR
VAR (k_0)
INT_CONST (3)
STMT_BLOCK
STMT_BLOCK
STMT_BLOCK
WHILE_STMT
NE_EXPR
VAR_EXPR
VAR (k_0)
INT_CONST (3)
STMT_BLOCK
void bar (void) {
int k_0 = 0;
int t_1 = 0;
while (1) {
if (k_0 != 1) {
if (k_0 == 2) {
while (k_0 != 3) {
}
}
} else {
while (k_0 != 3) {
}
}
}
}

56 changes: 56 additions & 0 deletions samples/outputs/sample50
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
FUNC_DECL
SCALAR_TYPE (VOID)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (INT)
VAR (var0)
INT_CONST (0)
DECL_STMT
SCALAR_TYPE (INT)
VAR (var1)
INT_CONST (0)
WHILE_STMT
INT_CONST (1)
STMT_BLOCK
IF_STMT
NE_EXPR
VAR_EXPR
VAR (var0)
INT_CONST (1)
STMT_BLOCK
IF_STMT
EQUALS_EXPR
VAR_EXPR
VAR (var0)
INT_CONST (2)
STMT_BLOCK
WHILE_STMT
NE_EXPR
VAR_EXPR
VAR (var0)
INT_CONST (3)
STMT_BLOCK
STMT_BLOCK
STMT_BLOCK
WHILE_STMT
NE_EXPR
VAR_EXPR
VAR (var0)
INT_CONST (3)
STMT_BLOCK
void bar (void) {
int var0 = 0;
int var1 = 0;
while (1) {
if (var0 != 1) {
if (var0 == 2) {
while (var0 != 3) {
}
}
} else {
while (var0 != 3) {
}
}
}
}

30 changes: 30 additions & 0 deletions samples/sample50.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "blocks/c_code_generator.h"
#include "builder/builder.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) {
dyn_var<int> k = 0;
dyn_var<int> t = 0;
while (1) {
while (k != 1) {

if (k == 2)
break;
}
while (k != 3) {
}
}
}

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;
}
24 changes: 12 additions & 12 deletions src/blocks/label_inserter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ namespace block {
void label_collector::visit(goto_stmt::Ptr a) {
collected_labels.push_back(a->temporary_label_number);
}
static void erase_tag(std::vector<tracer::tag> &list, tracer::tag &erase) {
std::vector<tracer::tag> new_list;
for (unsigned int i = 0; i < list.size(); i++) {
if (list[i] != erase) {
new_list.push_back(list[i]);
}
}
list = new_list;
}
void label_creator::visit(stmt_block::Ptr a) {
std::vector<stmt::Ptr> new_stmts;

Expand All @@ -30,16 +21,25 @@ void label_creator::visit(stmt_block::Ptr a) {
new_label_stmt->static_offset.clear();
new_label_stmt->label1 = new_label;
new_stmts.push_back(new_label_stmt);
// collected_labels.erase(stmt->static_offset);
erase_tag(collected_labels, stmt->static_offset);

offset_to_label[stmt->static_offset.stringify()] = new_label;
}
new_stmts.push_back(stmt);
stmt->accept(this);
}
a->stmts = new_stmts;
}

void label_inserter::visit(label_stmt::Ptr a) {
offset_to_label[a->label1->static_offset.stringify()] = a->label1;
}

void label_inserter::visit(goto_stmt::Ptr a) {
a->label1 = offset_to_label[a->temporary_label_number.stringify()];
// Pick any jump target with feature unstructured
// otherwise pick the one that is in the parent block
if (feature_unstructured)
a->label1 = backup_offset_to_label[a->temporary_label_number.stringify()];
else
a->label1 = offset_to_label[a->temporary_label_number.stringify()];
}
} // namespace block
4 changes: 3 additions & 1 deletion src/builder/builder_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ static void trim_ast_at_offset(block::stmt::Ptr ast, tracer::tag offset) {

static std::pair<std::vector<block::stmt::Ptr>, std::vector<block::stmt::Ptr>>
trim_common_from_back(block::stmt::Ptr ast1, block::stmt::Ptr ast2) {

std::vector<block::stmt::Ptr> trimmed_stmts;
std::vector<block::stmt::Ptr> &ast1_stmts = block::to<block::stmt_block>(ast1)->stmts;
std::vector<block::stmt::Ptr> &ast2_stmts = block::to<block::stmt_block>(ast2)->stmts;
Expand Down Expand Up @@ -280,7 +281,8 @@ block::stmt::Ptr builder_context::extract_ast_from_function_impl(void) {
ast->accept(&creator);

block::label_inserter inserter;
inserter.offset_to_label = creator.offset_to_label;
inserter.backup_offset_to_label = creator.offset_to_label;
inserter.feature_unstructured = feature_unstructured;
ast->accept(&inserter);

// At this point it is safe to remove statements that are
Expand Down

0 comments on commit d065b21

Please sign in to comment.