From cbf60b8886ab2499976da727a53f36ad65d7a1d6 Mon Sep 17 00:00:00 2001 From: Ajay Brahmakshatriya Date: Thu, 12 Dec 2024 08:45:14 -0500 Subject: [PATCH 1/2] Fixed promotion of [0] to * and -> only in specific cases --- include/builder/builder_base.h | 4 +++- include/builder/dyn_var.h | 8 +++++--- samples/outputs.var_names/sample33 | 4 ++-- samples/outputs.var_names/sample59 | 4 ++-- samples/outputs/sample33 | 4 ++-- samples/outputs/sample59 | 4 ++-- samples/sample33.cpp | 2 +- samples/sample59.cpp | 2 +- samples/sample60.cpp | 24 ++++++++++++++++++++++++ src/blocks/c_code_generator.cpp | 24 +++++++++++++----------- 10 files changed, 55 insertions(+), 25 deletions(-) create mode 100644 samples/sample60.cpp diff --git a/include/builder/builder_base.h b/include/builder/builder_base.h index a5ad682..f0f0188 100644 --- a/include/builder/builder_base.h +++ b/include/builder/builder_base.h @@ -231,7 +231,9 @@ class builder { return ret_builder; } BT operator*(void) { - return (*this)[0]; + auto b = (*this)[0]; + b.block_expr->template setMetadata("deref_is_star", true); + return b; } BT assign(const BT &a) { diff --git a/include/builder/dyn_var.h b/include/builder/dyn_var.h index 08dde56..cc7a2e2 100644 --- a/include/builder/dyn_var.h +++ b/include/builder/dyn_var.h @@ -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; @@ -436,12 +436,14 @@ class dyn_var return (dyn_var_mimic)(cast)this->dyn_var_impl::operator[](bt); } dyn_var_mimic operator*() { - return this->operator[](0); + return (cast)(this->dyn_var_impl::operator*()); } // Hack for creating a member that's live across return site dyn_var _p = as_member(this, "_p"); dyn_var *operator->() { - _p = (cast)this->operator[](0); + auto b = this->operator[](0); + b.encompassing_expr->template setMetadata("deref_is_star", true); + _p = (cast)b; return _p.addr(); } }; diff --git a/samples/outputs.var_names/sample33 b/samples/outputs.var_names/sample33 index 2493311..9bd7044 100644 --- a/samples/outputs.var_names/sample33 +++ b/samples/outputs.var_names/sample33 @@ -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) { @@ -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; } diff --git a/samples/outputs.var_names/sample59 b/samples/outputs.var_names/sample59 index 26a7f37..fed79f5 100644 --- a/samples/outputs.var_names/sample59 +++ b/samples/outputs.var_names/sample59 @@ -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> x_0; x_0.resize(2); - (x_0[1]).resize(1); + (x_0[0]).resize(1); } diff --git a/samples/outputs/sample33 b/samples/outputs/sample33 index dafddcf..dfe2565 100644 --- a/samples/outputs/sample33 +++ b/samples/outputs/sample33 @@ -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) { @@ -50,5 +50,5 @@ STMT_BLOCK FooT var2 = var0; var2 = var0; FooT* var3 = (&(var0)); - var0->member = 0; + (var3[0]).member = 0; } diff --git a/samples/outputs/sample59 b/samples/outputs/sample59 index 3c3ab16..631fde6 100644 --- a/samples/outputs/sample59 +++ b/samples/outputs/sample59 @@ -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> var0; var0.resize(2); - (var0[1]).resize(1); + (var0[0]).resize(1); } diff --git a/samples/sample33.cpp b/samples/sample33.cpp index 6e23db4..bcad996 100644 --- a/samples/sample33.cpp +++ b/samples/sample33.cpp @@ -32,7 +32,7 @@ static void bar(void) { FooT h = g; h = g; dyn_var ptr = &g; - ((FooT)(builder::cast)g[0]).member = 0; + ((FooT)(builder::cast)ptr[0]).member = 0; } int main(int argc, char *argv[]) { diff --git a/samples/sample59.cpp b/samples/sample59.cpp index 60b8942..254ed66 100644 --- a/samples/sample59.cpp +++ b/samples/sample59.cpp @@ -19,7 +19,7 @@ struct vector: public builder::custom_type { static void bar(void) { dyn_var>> x; x.resize(2); - x[1].resize(1); + x[0].resize(1); } int main(int argc, char *argv[]) { diff --git a/samples/sample60.cpp b/samples/sample60.cpp new file mode 100644 index 0000000..3faec81 --- /dev/null +++ b/samples/sample60.cpp @@ -0,0 +1,24 @@ +#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 "blocks/rce.h" +#include +using builder::dyn_var; +using builder::static_var; + +dyn_var create_int = builder::as_global("create_int"); + +static void bar(void) { + dyn_var x = create_int(); +} + +int main(int argc, char *argv[]) { + builder::builder_context context; + auto ast = context.extract_function_ast(bar, "bar"); + block::eliminate_redundant_vars(ast); + ast->dump(std::cout, 0); + block::c_code_generator::generate_code(ast, std::cout, 0); + return 0; +} diff --git a/src/blocks/c_code_generator.cpp b/src/blocks/c_code_generator.cpp index 1ed5a83..16fa4a2 100644 --- a/src/blocks/c_code_generator.cpp +++ b/src/blocks/c_code_generator.cpp @@ -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(a->parent_expr)) { sq_bkt_expr::Ptr parent = to(a->parent_expr); - if (isa(parent->index)) { - auto index = to(parent->index); - if (index->value == 0) { - if (!isa(parent->var_expr)) { - oss << "("; + if (parent->getBoolMetadata("deref_is_star")) { + if (isa(parent->index)) { + auto index = to(parent->index); + if (index->value == 0) { + if (!isa(parent->var_expr)) { + oss << "("; + } + parent->var_expr->accept(this); + if (!isa(parent->var_expr)) { + oss << ")"; + } + oss << "->" << a->member_name; + return; } - parent->var_expr->accept(this); - if (!isa(parent->var_expr)) { - oss << ")"; - } - oss << "->" << a->member_name; - return; } } } From 2cb815da74591d075731b57ad7aee8fde4336849 Mon Sep 17 00:00:00 2001 From: Ajay Brahmakshatriya Date: Thu, 12 Dec 2024 08:48:38 -0500 Subject: [PATCH 2/2] Removed junk test case --- samples/sample60.cpp | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 samples/sample60.cpp diff --git a/samples/sample60.cpp b/samples/sample60.cpp deleted file mode 100644 index 3faec81..0000000 --- a/samples/sample60.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#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 "blocks/rce.h" -#include -using builder::dyn_var; -using builder::static_var; - -dyn_var create_int = builder::as_global("create_int"); - -static void bar(void) { - dyn_var x = create_int(); -} - -int main(int argc, char *argv[]) { - builder::builder_context context; - auto ast = context.extract_function_ast(bar, "bar"); - block::eliminate_redundant_vars(ast); - ast->dump(std::cout, 0); - block::c_code_generator::generate_code(ast, std::cout, 0); - return 0; -}