Skip to content

Commit

Permalink
Merge pull request #87 from AjayBrahmakshatriya/master
Browse files Browse the repository at this point in the history
Fixed promotion of [0] to * and -> only in specific cases
  • Loading branch information
AjayBrahmakshatriya authored Dec 12, 2024
2 parents d7852a3 + 2cb815d commit c74cd27
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 25 deletions.
4 changes: 3 additions & 1 deletion include/builder/builder_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ class builder {
return ret_builder;
}
BT operator*(void) {
return (*this)[0];
auto b = (*this)[0];
b.block_expr->template setMetadata<bool>("deref_is_star", true);
return b;
}

BT assign(const BT &a) {
Expand Down
8 changes: 5 additions & 3 deletions include/builder/dyn_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class dyn_var_impl : public var {
return ((builder) * this)[a];
}
builder operator*(void) {
return ((builder) * this)[0];
return *((builder) * this);
}
builder operator!() {
return !(builder) * this;
Expand Down Expand Up @@ -436,12 +436,14 @@ class dyn_var<T *>
return (dyn_var_mimic<T>)(cast)this->dyn_var_impl<T *>::operator[](bt);
}
dyn_var_mimic<T> operator*() {
return this->operator[](0);
return (cast)(this->dyn_var_impl<T*>::operator*());
}
// Hack for creating a member that's live across return site
dyn_var<T> _p = as_member(this, "_p");
dyn_var<T> *operator->() {
_p = (cast)this->operator[](0);
auto b = this->operator[](0);
b.encompassing_expr->template setMetadata<bool>("deref_is_star", true);
_p = (cast)b;
return _p.addr();
}
};
Expand Down
4 changes: 2 additions & 2 deletions samples/outputs.var_names/sample33
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ STMT_BLOCK
MEMBER_ACCESS_EXPR (member)
SQ_BKT_EXPR
VAR_EXPR
VAR (g_0)
VAR (ptr_3)
INT_CONST (0)
INT_CONST (0)
{
Expand All @@ -50,5 +50,5 @@ STMT_BLOCK
FooT h_2 = g_0;
h_2 = g_0;
FooT* ptr_3 = (&(g_0));
g_0->member = 0;
(ptr_3[0]).member = 0;
}
4 changes: 2 additions & 2 deletions samples/outputs.var_names/sample59
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ FUNC_DECL
SQ_BKT_EXPR
VAR_EXPR
VAR (x_0)
INT_CONST (1)
INT_CONST (0)
INT_CONST (1)
void bar (void) {
std::vector<std::vector<int>> x_0;
x_0.resize(2);
(x_0[1]).resize(1);
(x_0[0]).resize(1);
}

4 changes: 2 additions & 2 deletions samples/outputs/sample33
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ STMT_BLOCK
MEMBER_ACCESS_EXPR (member)
SQ_BKT_EXPR
VAR_EXPR
VAR (var0)
VAR (var3)
INT_CONST (0)
INT_CONST (0)
{
Expand All @@ -50,5 +50,5 @@ STMT_BLOCK
FooT var2 = var0;
var2 = var0;
FooT* var3 = (&(var0));
var0->member = 0;
(var3[0]).member = 0;
}
4 changes: 2 additions & 2 deletions samples/outputs/sample59
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ FUNC_DECL
SQ_BKT_EXPR
VAR_EXPR
VAR (var0)
INT_CONST (1)
INT_CONST (0)
INT_CONST (1)
void bar (void) {
std::vector<std::vector<int>> var0;
var0.resize(2);
(var0[1]).resize(1);
(var0[0]).resize(1);
}

2 changes: 1 addition & 1 deletion samples/sample33.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static void bar(void) {
FooT h = g;
h = g;
dyn_var<foo_t *> ptr = &g;
((FooT)(builder::cast)g[0]).member = 0;
((FooT)(builder::cast)ptr[0]).member = 0;
}

int main(int argc, char *argv[]) {
Expand Down
2 changes: 1 addition & 1 deletion samples/sample59.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct vector: public builder::custom_type<T> {
static void bar(void) {
dyn_var<vector<vector<int>>> x;
x.resize(2);
x[1].resize(1);
x[0].resize(1);
}

int main(int argc, char *argv[]) {
Expand Down
24 changes: 13 additions & 11 deletions src/blocks/c_code_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,18 +576,20 @@ void c_code_generator::visit(return_stmt::Ptr a) {
void c_code_generator::visit(member_access_expr::Ptr a) {
if (isa<sq_bkt_expr>(a->parent_expr)) {
sq_bkt_expr::Ptr parent = to<sq_bkt_expr>(a->parent_expr);
if (isa<int_const>(parent->index)) {
auto index = to<int_const>(parent->index);
if (index->value == 0) {
if (!isa<var_expr>(parent->var_expr)) {
oss << "(";
if (parent->getBoolMetadata("deref_is_star")) {
if (isa<int_const>(parent->index)) {
auto index = to<int_const>(parent->index);
if (index->value == 0) {
if (!isa<var_expr>(parent->var_expr)) {
oss << "(";
}
parent->var_expr->accept(this);
if (!isa<var_expr>(parent->var_expr)) {
oss << ")";
}
oss << "->" << a->member_name;
return;
}
parent->var_expr->accept(this);
if (!isa<var_expr>(parent->var_expr)) {
oss << ")";
}
oss << "->" << a->member_name;
return;
}
}
}
Expand Down

0 comments on commit c74cd27

Please sign in to comment.