Skip to content

Commit eb9039e

Browse files
Merge pull request #47 from AjayBrahmakshatriya/master
Implemented basic support for arrays of static_vars
2 parents c78b8b2 + b1cb544 commit eb9039e

File tree

4 files changed

+264
-0
lines changed

4 files changed

+264
-0
lines changed

include/builder/static_var.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,69 @@ class static_var {
5555
}
5656
};
5757

58+
template <typename T>
59+
class static_var<T[]> {
60+
public:
61+
static_assert(std::is_same<T, short int>::value || std::is_same<T, unsigned short int>::value ||
62+
std::is_same<T, int>::value || std::is_same<T, unsigned int>::value ||
63+
std::is_same<T, long int>::value || std::is_same<T, unsigned long int>::value ||
64+
std::is_same<T, long long int>::value || std::is_same<T, unsigned long long int>::value ||
65+
std::is_same<T, char>::value || std::is_same<T, unsigned char>::value ||
66+
std::is_same<T, float>::value || std::is_same<T, double>::value || std::is_pointer<T>::value,
67+
"Currently builder::static_var arrays is only supported for basic types\n");
68+
static_assert(sizeof(T) < MAX_TRACKING_VAR_SIZE, "Currently builder::static_var supports variables of max size "
69+
"= " TOSTRING(MAX_TRACKING_VARIABLE_SIZE));
70+
// Disable copy-assignment and initialization
71+
static_var(const static_var &x) = delete;
72+
static_var &operator=(const static_var &x) = delete;
73+
T *val = nullptr;
74+
75+
T &operator[](size_t index) {
76+
return val[index];
77+
}
78+
const T &operator[](size_t index) const {
79+
return val[index];
80+
}
81+
static_var() {
82+
assert(builder_context::current_builder_context != nullptr);
83+
// This val _should_ not be used. But we will insert it to hold place
84+
// for this static var in the list of tuples, otherwise destructor order will be weird
85+
val = new T[1];
86+
87+
builder_context::current_builder_context->static_var_tuples.push_back(
88+
tracking_tuple((unsigned char *)val, 1));
89+
}
90+
static_var(const std::initializer_list<T> &list) {
91+
assert(builder_context::current_builder_context != nullptr);
92+
val = new T[list.size()];
93+
builder_context::current_builder_context->static_var_tuples.push_back(
94+
tracking_tuple((unsigned char *)val, sizeof(T) * list.size()));
95+
for (int i = 0; i < list.size(); i++) {
96+
val[i] = list[i];
97+
}
98+
}
99+
void resize(size_t s) {
100+
T *new_ptr = new T[s];
101+
assert(builder_context::current_builder_context != nullptr);
102+
assert(builder_context::current_builder_context->static_var_tuples.size() > 0);
103+
for (size_t i = 0; i < builder_context::current_builder_context->static_var_tuples.size(); i++) {
104+
if (builder_context::current_builder_context->static_var_tuples[i].ptr ==
105+
(unsigned char *)val) {
106+
builder_context::current_builder_context->static_var_tuples[i] =
107+
tracking_tuple((unsigned char *)new_ptr, sizeof(T) * s);
108+
break;
109+
}
110+
}
111+
delete[] val;
112+
val = new_ptr;
113+
}
114+
~static_var() {
115+
assert(builder_context::current_builder_context != nullptr);
116+
assert(builder_context::current_builder_context->static_var_tuples.size() > 0);
117+
assert(builder_context::current_builder_context->static_var_tuples.back().ptr == (unsigned char *)val);
118+
builder_context::current_builder_context->static_var_tuples.pop_back();
119+
delete[] val;
120+
}
121+
};
58122
} // namespace builder
59123
#endif

samples/outputs.var_names/sample47

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
FUNC_DECL
2+
SCALAR_TYPE (VOID)
3+
STMT_BLOCK
4+
DECL_STMT
5+
SCALAR_TYPE (INT)
6+
VAR (c_0)
7+
INT_CONST (0)
8+
DECL_STMT
9+
SCALAR_TYPE (INT)
10+
VAR (c_1)
11+
INT_CONST (0)
12+
DECL_STMT
13+
SCALAR_TYPE (INT)
14+
VAR (c_2)
15+
INT_CONST (0)
16+
DECL_STMT
17+
SCALAR_TYPE (INT)
18+
VAR (c_3)
19+
INT_CONST (0)
20+
DECL_STMT
21+
SCALAR_TYPE (INT)
22+
VAR (c_4)
23+
INT_CONST (0)
24+
DECL_STMT
25+
SCALAR_TYPE (INT)
26+
VAR (c_5)
27+
INT_CONST (0)
28+
DECL_STMT
29+
SCALAR_TYPE (INT)
30+
VAR (c_6)
31+
INT_CONST (0)
32+
DECL_STMT
33+
SCALAR_TYPE (INT)
34+
VAR (c_7)
35+
INT_CONST (0)
36+
DECL_STMT
37+
SCALAR_TYPE (INT)
38+
VAR (c_8)
39+
INT_CONST (0)
40+
DECL_STMT
41+
SCALAR_TYPE (INT)
42+
VAR (c_9)
43+
INT_CONST (0)
44+
DECL_STMT
45+
SCALAR_TYPE (INT)
46+
VAR (c_10)
47+
INT_CONST (0)
48+
DECL_STMT
49+
SCALAR_TYPE (INT)
50+
VAR (c_11)
51+
INT_CONST (0)
52+
DECL_STMT
53+
SCALAR_TYPE (INT)
54+
VAR (c_12)
55+
INT_CONST (0)
56+
DECL_STMT
57+
SCALAR_TYPE (INT)
58+
VAR (c_13)
59+
INT_CONST (0)
60+
DECL_STMT
61+
SCALAR_TYPE (INT)
62+
VAR (c_14)
63+
INT_CONST (0)
64+
DECL_STMT
65+
SCALAR_TYPE (INT)
66+
VAR (c_15)
67+
INT_CONST (0)
68+
void my_bar (void) {
69+
int c_0 = 0;
70+
int c_1 = 0;
71+
int c_2 = 0;
72+
int c_3 = 0;
73+
int c_4 = 0;
74+
int c_5 = 0;
75+
int c_6 = 0;
76+
int c_7 = 0;
77+
int c_8 = 0;
78+
int c_9 = 0;
79+
int c_10 = 0;
80+
int c_11 = 0;
81+
int c_12 = 0;
82+
int c_13 = 0;
83+
int c_14 = 0;
84+
int c_15 = 0;
85+
}
86+

samples/outputs/sample47

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
FUNC_DECL
2+
SCALAR_TYPE (VOID)
3+
STMT_BLOCK
4+
DECL_STMT
5+
SCALAR_TYPE (INT)
6+
VAR (var0)
7+
INT_CONST (0)
8+
DECL_STMT
9+
SCALAR_TYPE (INT)
10+
VAR (var1)
11+
INT_CONST (0)
12+
DECL_STMT
13+
SCALAR_TYPE (INT)
14+
VAR (var2)
15+
INT_CONST (0)
16+
DECL_STMT
17+
SCALAR_TYPE (INT)
18+
VAR (var3)
19+
INT_CONST (0)
20+
DECL_STMT
21+
SCALAR_TYPE (INT)
22+
VAR (var4)
23+
INT_CONST (0)
24+
DECL_STMT
25+
SCALAR_TYPE (INT)
26+
VAR (var5)
27+
INT_CONST (0)
28+
DECL_STMT
29+
SCALAR_TYPE (INT)
30+
VAR (var6)
31+
INT_CONST (0)
32+
DECL_STMT
33+
SCALAR_TYPE (INT)
34+
VAR (var7)
35+
INT_CONST (0)
36+
DECL_STMT
37+
SCALAR_TYPE (INT)
38+
VAR (var8)
39+
INT_CONST (0)
40+
DECL_STMT
41+
SCALAR_TYPE (INT)
42+
VAR (var9)
43+
INT_CONST (0)
44+
DECL_STMT
45+
SCALAR_TYPE (INT)
46+
VAR (var10)
47+
INT_CONST (0)
48+
DECL_STMT
49+
SCALAR_TYPE (INT)
50+
VAR (var11)
51+
INT_CONST (0)
52+
DECL_STMT
53+
SCALAR_TYPE (INT)
54+
VAR (var12)
55+
INT_CONST (0)
56+
DECL_STMT
57+
SCALAR_TYPE (INT)
58+
VAR (var13)
59+
INT_CONST (0)
60+
DECL_STMT
61+
SCALAR_TYPE (INT)
62+
VAR (var14)
63+
INT_CONST (0)
64+
DECL_STMT
65+
SCALAR_TYPE (INT)
66+
VAR (var15)
67+
INT_CONST (0)
68+
void my_bar (void) {
69+
int var0 = 0;
70+
int var1 = 0;
71+
int var2 = 0;
72+
int var3 = 0;
73+
int var4 = 0;
74+
int var5 = 0;
75+
int var6 = 0;
76+
int var7 = 0;
77+
int var8 = 0;
78+
int var9 = 0;
79+
int var10 = 0;
80+
int var11 = 0;
81+
int var12 = 0;
82+
int var13 = 0;
83+
int var14 = 0;
84+
int var15 = 0;
85+
}
86+

samples/sample47.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "blocks/c_code_generator.h"
2+
#include "builder/dyn_var.h"
3+
#include "builder/static_var.h"
4+
5+
using builder::dyn_var;
6+
using builder::static_var;
7+
8+
static void foo(void) {
9+
static_var<int[]> states;
10+
states.resize(4);
11+
for (states[0] = 0; states[0] < 2; states[0]++) {
12+
for (states[1] = 0; states[1] < 2; states[1]++) {
13+
for (states[2] = 0; states[2] < 2; states[2]++) {
14+
for (states[3] = 0; states[3] < 2; states[3]++) {
15+
dyn_var<int> c = 0;
16+
}
17+
}
18+
}
19+
}
20+
}
21+
int main(int argc, char *argv[]) {
22+
builder::builder_context context;
23+
auto ast = context.extract_function_ast(foo, "my_bar");
24+
ast->dump(std::cout, 0);
25+
26+
block::c_code_generator::generate_code(ast, std::cout, 0);
27+
return 0;
28+
}

0 commit comments

Comments
 (0)