Skip to content

Commit

Permalink
Fixed bug with dynamic variables escaping static scopes getting diffe…
Browse files Browse the repository at this point in the history
…rent names
  • Loading branch information
AjayBrahmakshatriya committed Dec 21, 2023
1 parent 0d198fd commit 47a9b59
Show file tree
Hide file tree
Showing 20 changed files with 297 additions and 75 deletions.
19 changes: 17 additions & 2 deletions include/blocks/var_namer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@

namespace block {

class var_gather_escapes : public block_visitor {
public:
using block_visitor::visit;
std::vector<std::string> &escaping_tags;
var_gather_escapes(std::vector<std::string> &e) : escaping_tags(e) {}
virtual void visit(decl_stmt::Ptr) override;
};

class var_namer : public block_visitor {
public:
using block_visitor::visit;
int var_counter = 0;
std::map<std::string, var::Ptr> collected_decls;
std::map<std::string, decl_stmt::Ptr> decls_to_hoist;
std::vector<std::string> decl_tags_to_hoist;

std::vector<std::string> escaping_tags;

virtual void visit(decl_stmt::Ptr) override;

static void name_vars(block::Ptr ast);
Expand All @@ -24,7 +35,9 @@ class var_replacer : public block_visitor {
public:
using block_visitor::visit;
std::map<std::string, var::Ptr> &collected_decls;
var_replacer(std::map<std::string, var::Ptr> &d) : collected_decls(d) {}
std::vector<std::string> &escaping_tags;
var_replacer(std::map<std::string, var::Ptr> &d, std::vector<std::string> &e)
: collected_decls(d), escaping_tags(e) {}

virtual void visit(var_expr::Ptr) override;
};
Expand All @@ -33,7 +46,9 @@ class var_hoister : public block_replacer {
public:
using block_replacer::visit;
std::map<std::string, decl_stmt::Ptr> &decls_to_hoist;
var_hoister(std::map<std::string, decl_stmt::Ptr> &d) : decls_to_hoist(d) {}
std::vector<std::string> &escaping_tags;
var_hoister(std::map<std::string, decl_stmt::Ptr> &d, std::vector<std::string> &e)
: decls_to_hoist(d), escaping_tags(e) {}
virtual void visit(decl_stmt::Ptr) override;
};

Expand Down
18 changes: 18 additions & 0 deletions include/builder/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ class dyn_arr {
for (static_var<size_t> i = 0; i < actual_size; i++) {
new (m_arr + i) dyn_var<T>();
}
// static tags for array nodes need to be adjusted
// so they are treated different from each other despite
// being declared at the same location.
// dyn_arr are special case of vars that escape their static scope but still
// shouldn't be treated together
// We do this by adding additional metadata on all of them
for (static_var<size_t> i = 0; i < actual_size; i++) {
m_arr[i].block_var->template setMetadata<int>("allow_escape_scope", 1);
}
}
}
dyn_arr(const std::initializer_list<builder> &init) {
Expand All @@ -34,6 +43,9 @@ class dyn_arr {
else
new (m_arr + i) dyn_var<T>();
}
for (static_var<size_t> i = 0; i < actual_size; i++) {
m_arr[i].block_var->template setMetadata<int>("allow_escape_scope", 1);
}
}
void set_size(size_t new_size) {
assert(size == 0 && "set_size should be only called for dyn_arr without size");
Expand All @@ -43,6 +55,9 @@ class dyn_arr {
for (static_var<size_t> i = 0; i < actual_size; i++) {
new (m_arr + i) dyn_var<T>();
}
for (static_var<size_t> i = 0; i < actual_size; i++) {
m_arr[i].block_var->template setMetadata<int>("allow_escape_scope", 1);
}
}

template <typename T2, size_t N>
Expand All @@ -59,6 +74,9 @@ class dyn_arr {
else
new (m_arr + i) dyn_var<T>();
}
for (static_var<size_t> i = 0; i < actual_size; i++) {
m_arr[i].block_var->template setMetadata<int>("allow_escape_scope", 1);
}
}

dyn_arr(const dyn_arr &other) {
Expand Down
6 changes: 3 additions & 3 deletions include/builder/dyn_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ class dyn_var_impl : public var {
var_name = v.name;
}

dyn_var_impl(const defer_init&) {
dyn_var_impl(const defer_init &) {
// Do nothing here
}
// The function to actually initialize a dyn_var, if it
// The function to actually initialize a dyn_var, if it
// has been deferred. It is OKAY to call this even if defer_init
// is not used, but is not adviced. This can definitely be called multiple
// is not used, but is not adviced. This can definitely be called multiple
// times and will produce the same dyn_var based on the static tag at the
// time of this call
// Currently we don't support init val, but can be added if needed
Expand Down
27 changes: 27 additions & 0 deletions include/util/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ class tag {

return output_string;
}

std::string stringify_loc(void) {
std::string output_string = "[";
for (unsigned int i = 0; i < pointers.size(); i++) {
char temp[128];
sprintf(temp, "%llx", pointers[i]);
output_string += temp;
if (i != pointers.size() - 1)
output_string += ", ";
}
output_string += "]:[";
output_string += "]";

return output_string;
}
std::string stringify_stat(void) {
std::string output_string = "[";
output_string += "]:[";
for (unsigned int i = 0; i < static_var_snapshots.size(); i++) {
output_string += static_var_snapshots[i];
if (i != static_var_snapshots.size() - 1)
output_string += ", ";
}
output_string += "]";

return output_string;
}
};

tag get_unique_tag(void);
Expand Down
17 changes: 9 additions & 8 deletions samples/outputs.var_names/sample40
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <string.h>
int match_re (char* arg1) {
int var3;
int var5;
int var4;
int str_len_1 = strlen(arg1);
int to_match_2 = 0;
if (to_match_2 < str_len_1) {
Expand All @@ -27,27 +27,28 @@ int match_re (char* arg1) {
var3 = 0;
return var3;
}
return 1;
var4 = 1;
return var4;
}
var3 = 0;
return var3;
}
var5 = 0;
return var5;
var4 = 0;
return var4;
}
if (arg1[to_match_2] == 100) {
goto label2;
}
var3 = 0;
return var3;
}
var5 = 0;
return var5;
var4 = 0;
return var4;
}
var3 = 0;
return var3;
}
var5 = 0;
return var5;
var4 = 0;
return var4;
}

24 changes: 20 additions & 4 deletions samples/outputs.var_names/sample48
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,38 @@ FUNC_DECL
SCALAR_TYPE (INT)
VAR (x_0)
NO_INITIALIZATION
DECL_STMT
SCALAR_TYPE (INT)
VAR (var1)
INT_CONST (0)
DECL_STMT
SCALAR_TYPE (INT)
VAR (var2)
INT_CONST (0)
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (var1)
INT_CONST (1)
WHILE_STMT
INT_CONST (1)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (INT)
VAR (var1)
VAR (var3)
INT_CONST (1)
DECL_STMT
SCALAR_TYPE (INT)
VAR (var2)
VAR (var4)
INT_CONST (2)
void my_bar (void) {
int x_0;
int var1 = 0;
int var2 = 0;
var1 = 1;
while (1) {
int var1 = 1;
int var2 = 2;
int var3 = 1;
int var4 = 2;
}
}

49 changes: 33 additions & 16 deletions samples/outputs.var_names/sample52
Original file line number Diff line number Diff line change
@@ -1,49 +1,66 @@
int isEven (int arg0) {
int var1;
if (arg0 == 0) {
return 1;
var1 = 1;
return var1;
}
if (arg0 == 1) {
return 0;
var1 = 0;
return var1;
}
if (arg0 == 2) {
return 1;
var1 = 1;
return var1;
}
if (arg0 == 3) {
return 0;
var1 = 0;
return var1;
}
if (arg0 == 4) {
return 1;
var1 = 1;
return var1;
}
if (arg0 == 5) {
return 0;
var1 = 0;
return var1;
}
if (arg0 == 6) {
return 1;
var1 = 1;
return var1;
}
if (arg0 == 7) {
return 0;
var1 = 0;
return var1;
}
if (arg0 == 8) {
return 1;
var1 = 1;
return var1;
}
if (arg0 == 9) {
return 0;
var1 = 0;
return var1;
}
if (arg0 == 10) {
return 1;
var1 = 1;
return var1;
}
if (arg0 == 11) {
return 0;
var1 = 0;
return var1;
}
if (arg0 == 12) {
return 1;
var1 = 1;
return var1;
}
if (arg0 == 13) {
return 0;
var1 = 0;
return var1;
}
if (arg0 == 14) {
return 1;
var1 = 1;
return var1;
}
return 0;
var1 = 0;
return var1;
}

12 changes: 12 additions & 0 deletions samples/outputs.var_names/sample54
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
void bar (void) {
int z_1;
int y_0 = 0;
if (y_0) {
z_1 = 1;
} else {
z_1 = 2;
}
int b_2;
int a_3 = z_1;
}

17 changes: 9 additions & 8 deletions samples/outputs/sample40
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <string.h>
int match_re (char* arg1) {
int var3;
int var5;
int var4;
int var1 = strlen(arg1);
int var2 = 0;
if (var2 < var1) {
Expand All @@ -27,27 +27,28 @@ int match_re (char* arg1) {
var3 = 0;
return var3;
}
return 1;
var4 = 1;
return var4;
}
var3 = 0;
return var3;
}
var5 = 0;
return var5;
var4 = 0;
return var4;
}
if (arg1[var2] == 100) {
goto label2;
}
var3 = 0;
return var3;
}
var5 = 0;
return var5;
var4 = 0;
return var4;
}
var3 = 0;
return var3;
}
var5 = 0;
return var5;
var4 = 0;
return var4;
}

Loading

0 comments on commit 47a9b59

Please sign in to comment.