Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support out of order destruction of static_vars #83

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions include/builder/static_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,27 @@ class static_var : static_var_base {

assert(builder_context::current_builder_context != nullptr);
assert(builder_context::current_builder_context->static_var_tuples.size() > 0);
assert(builder_context::current_builder_context->static_var_tuples.back().ptr == (unsigned char *)&val);
builder_context::current_builder_context->static_var_tuples.pop_back();

/* Instead of assuming that the last variable is the static_var we are destroying,
we find the appropriate one and set its size to 0. Then we clean up all the trailing 0s

This allows out of order destruction while still maintaining good static tags */

int index = -1;
for (int i = builder_context::current_builder_context->static_var_tuples.size() - 1; i >= 0; i--) {
if (builder_context::current_builder_context->static_var_tuples[i].ptr == (unsigned char*) &val) {
index = i;
builder_context::current_builder_context->static_var_tuples[i].size = 0;
break;
}
}
assert(index != -1 && "Static variable to destroy not valid");

while (!builder_context::current_builder_context->static_var_tuples.empty()
&& builder_context::current_builder_context->static_var_tuples.back().size == 0) {
builder_context::current_builder_context->static_var_tuples.pop_back();
}

}
operator builder() {
try_get_name();
Expand Down Expand Up @@ -188,8 +207,20 @@ class static_var<T[]> : static_var_base {
~static_var() {
assert(builder_context::current_builder_context != nullptr);
assert(builder_context::current_builder_context->static_var_tuples.size() > 0);
assert(builder_context::current_builder_context->static_var_tuples.back().ptr == (unsigned char *)val);
builder_context::current_builder_context->static_var_tuples.pop_back();
int index = -1;
for (int i = builder_context::current_builder_context->static_var_tuples.size() - 1; i >= 0; i--) {
if (builder_context::current_builder_context->static_var_tuples[i].ptr == (unsigned char*) val) {
index = i;
builder_context::current_builder_context->static_var_tuples[i].size = 0;
break;
}
}
assert(index != -1 && "Static variable to destroy not valid");

while (!builder_context::current_builder_context->static_var_tuples.empty()
&& builder_context::current_builder_context->static_var_tuples.back().size == 0) {
builder_context::current_builder_context->static_var_tuples.pop_back();
}
delete[] val;
}
virtual std::string serialize() override {
Expand Down
74 changes: 74 additions & 0 deletions samples/outputs.var_names/sample58
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
FUNC_DECL
SCALAR_TYPE (VOID)
VAR (arg0)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (INT)
VAR (var0)
VAR_EXPR
VAR (arg0)
DECL_STMT
SCALAR_TYPE (INT)
VAR (y_1)
NO_INITIALIZATION
DECL_STMT
SCALAR_TYPE (INT)
VAR (c_2)
VAR_EXPR
VAR (var0)
IF_STMT
EQUALS_EXPR
INT_CONST (0)
VAR_EXPR
VAR (c_2)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (FLOAT)
VAR (c_3)
FLOAT_CONST (2)
STMT_BLOCK
IF_STMT
EQUALS_EXPR
INT_CONST (1)
VAR_EXPR
VAR (c_2)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (FLOAT)
VAR (c_4)
FLOAT_CONST (3)
STMT_BLOCK
IF_STMT
EQUALS_EXPR
INT_CONST (2)
VAR_EXPR
VAR (c_2)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (FLOAT)
VAR (c_5)
FLOAT_CONST (5)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (FLOAT)
VAR (c_6)
FLOAT_CONST (2)
void bar (int arg0) {
int var0 = arg0;
int y_1;
int c_2 = var0;
if (0 == c_2) {
float c_3 = 2.0f;
} else {
if (1 == c_2) {
float c_4 = 3.0f;
} else {
if (2 == c_2) {
float c_5 = 5.0f;
} else {
float c_6 = 2.0f;
}
}
}
}

74 changes: 74 additions & 0 deletions samples/outputs/sample58
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
FUNC_DECL
SCALAR_TYPE (VOID)
VAR (arg0)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (INT)
VAR (var0)
VAR_EXPR
VAR (arg0)
DECL_STMT
SCALAR_TYPE (INT)
VAR (var1)
NO_INITIALIZATION
DECL_STMT
SCALAR_TYPE (INT)
VAR (var2)
VAR_EXPR
VAR (var0)
IF_STMT
EQUALS_EXPR
INT_CONST (0)
VAR_EXPR
VAR (var2)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (FLOAT)
VAR (var3)
FLOAT_CONST (2)
STMT_BLOCK
IF_STMT
EQUALS_EXPR
INT_CONST (1)
VAR_EXPR
VAR (var2)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (FLOAT)
VAR (var4)
FLOAT_CONST (3)
STMT_BLOCK
IF_STMT
EQUALS_EXPR
INT_CONST (2)
VAR_EXPR
VAR (var2)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (FLOAT)
VAR (var5)
FLOAT_CONST (5)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (FLOAT)
VAR (var6)
FLOAT_CONST (2)
void bar (int arg0) {
int var0 = arg0;
int var1;
int var2 = var0;
if (0 == var2) {
float var3 = 2.0f;
} else {
if (1 == var2) {
float var4 = 3.0f;
} else {
if (2 == var2) {
float var5 = 5.0f;
} else {
float var6 = 2.0f;
}
}
}
}

56 changes: 56 additions & 0 deletions samples/sample58.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#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 "builder/lib/utils.h"
#include <iostream>
using builder::dyn_var;
using builder::static_var;

static static_var<int> magic(dyn_var<int> x, int range) {
for (static_var<int> y = 0; y < range; y++) {
if (y == x) return y;
}
return 0;
}


float arr[] = {2.0, 3.0, 5.0};


static void bar(dyn_var<int> x) {
/* Test out of order deinitialization of static variables and arrays */

auto sv1 = new static_var<int>();
auto sv2 = new static_var<int>();

auto sva1 = new static_var<int[]>();
sva1->resize(5);
auto sva2 = new static_var<int[]>();
sva2->resize(5);


dyn_var<int> y;

delete sv1;
delete sv2;
delete sva1;
delete sva2;


// Test case that breaks without out of order deinitialization
static_var<int> z = magic(x, 3);
dyn_var<float> c = arr[z];



}

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