Skip to content

Commit d76c684

Browse files
committed
Odin: fix init value memleak
1 parent 964a578 commit d76c684

File tree

4 files changed

+37
-37
lines changed

4 files changed

+37
-37
lines changed

ODIN_II/SRC/ast_util.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ ast_node_t* create_node_w_type(ids id, loc_t loc) {
156156
new_node->types.hierarchy = NULL;
157157
new_node->chunk_size = 1;
158158
new_node->identifier_node = NULL;
159+
/* init value */
160+
new_node->types.variable.initial_value = nullptr;
159161
/* reset flags */
160162
new_node->types.variable.is_parameter = false;
161163
new_node->types.variable.is_string = false;
@@ -171,6 +173,9 @@ ast_node_t* create_node_w_type(ids id, loc_t loc) {
171173
new_node->types.variable.is_memory = false;
172174
new_node->types.variable.signedness = UNSIGNED;
173175

176+
new_node->types.concat.num_bit_strings = 0;
177+
new_node->types.concat.bit_strings = NULL;
178+
174179
return new_node;
175180
}
176181

@@ -182,27 +187,29 @@ void free_assignement_of_node_keep_tree(ast_node_t* node) {
182187
if (node) {
183188
/* nuke the identifier node first */
184189
node->identifier_node = free_single_node(node->identifier_node);
185-
/* then free up the actual ID */
190+
191+
/* free the identifier */
186192
vtr::free(node->types.identifier);
187193
node->types.identifier = NULL;
188-
/* check the typ and free acordingly afterwards*/
189-
switch (node->type) {
190-
case NUMBERS:
191-
if (node->types.vnumber != nullptr)
192-
delete node->types.vnumber;
193-
node->types.vnumber = nullptr;
194-
break;
195194

196-
case CONCATENATE:
197-
for (int i = 0; i < node->types.concat.num_bit_strings; i++) {
198-
if (node->types.concat.bit_strings[i])
199-
vtr::free(node->types.concat.bit_strings[i]);
200-
}
201-
vtr::free(node->types.concat.bit_strings);
195+
/* initialization vlalues */
196+
if (node->types.variable.initial_value != nullptr)
197+
delete node->types.variable.initial_value;
198+
node->types.variable.initial_value = nullptr;
202199

203-
default:
204-
break;
200+
/* numbered values */
201+
if (node->types.vnumber != nullptr)
202+
delete node->types.vnumber;
203+
node->types.vnumber = nullptr;
204+
205+
/* concats */
206+
for (int i = 0; i < node->types.concat.num_bit_strings; i++) {
207+
if (node->types.concat.bit_strings[i])
208+
vtr::free(node->types.concat.bit_strings[i]);
205209
}
210+
vtr::free(node->types.concat.bit_strings);
211+
node->types.concat.bit_strings = NULL;
212+
node->types.concat.num_bit_strings = 0;
206213
}
207214
}
208215
/*---------------------------------------------------------------------------
@@ -915,12 +922,14 @@ ast_node_t* ast_node_copy(ast_node_t* node) {
915922
}
916923

917924
//Copy node
918-
node_copy = (ast_node_t*)vtr::calloc(1, sizeof(ast_node_t));
925+
node_copy = create_node_w_type(node->type, node->loc);
919926
memcpy(node_copy, node, sizeof(ast_node_t));
920927

921928
//Copy contents
922-
if (node->type == NUMBERS && node->types.vnumber)
929+
if (node->types.vnumber)
923930
node_copy->types.vnumber = new VNumber((*node->types.vnumber));
931+
if (node->types.variable.initial_value)
932+
node_copy->types.variable.initial_value = new VNumber((*node->types.variable.initial_value));
924933

925934
node_copy->types.identifier = vtr::strdup(node->types.identifier);
926935
node_copy->identifier_node = ast_node_deep_copy(node_copy->identifier_node);

ODIN_II/SRC/implicit_memory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ implicit_memory* create_implicit_memory_block(int data_width, long memory_depth,
125125
node->name = hard_node_name(node, instance_name_prefix, implicit_string, name);
126126

127127
// Create a fake ast node.
128-
node->related_ast_node = (ast_node_t*)vtr::calloc(1, sizeof(ast_node_t));
128+
node->related_ast_node = create_node_w_type(RAM, node->loc);
129129
node->related_ast_node->children = (ast_node_t**)vtr::calloc(1, sizeof(ast_node_t*));
130130
node->related_ast_node->identifier_node = create_tree_node_id(vtr::strdup(DUAL_PORT_RAM_string), loc);
131131

ODIN_II/SRC/netlist_create_from_ast.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,6 @@ void create_symbol_table_for_scope(ast_node_t* module_items, sc_hierarchy* local
12591259
temp_string = make_full_ref_name(NULL, NULL, NULL, var_declare->identifier_node->types.identifier, -1);
12601260
/* look for that element */
12611261
sc_spot = sc_add_string(local_symbol_table_sc, temp_string);
1262-
VNumber* init_value = get_init_value(var_declare->children[4]);
12631262

12641263
if (local_symbol_table_sc->data[sc_spot] != NULL) {
12651264
/* ERROR checks here
@@ -1276,19 +1275,13 @@ void create_symbol_table_for_scope(ast_node_t* module_items, sc_hierarchy* local
12761275
else if (var_declare->types.variable.is_output) {
12771276
/* copy all the reg and wire info over */
12781277
((ast_node_t*)local_symbol_table_sc->data[sc_spot])->types.variable.is_output = true;
1279-
1280-
/* check for an initial value and copy it over if found */
1281-
((ast_node_t*)local_symbol_table_sc->data[sc_spot])->types.variable.initial_value = init_value;
12821278
} else if ((var_declare->types.variable.is_reg) || (var_declare->types.variable.is_wire)
12831279
|| (var_declare->types.variable.is_genvar)) {
12841280
/* copy the output status over */
12851281
((ast_node_t*)local_symbol_table_sc->data[sc_spot])->types.variable.is_wire = var_declare->types.variable.is_wire;
12861282
((ast_node_t*)local_symbol_table_sc->data[sc_spot])->types.variable.is_reg = var_declare->types.variable.is_reg;
1287-
1288-
/* check for an initial value and copy it over if found */
1289-
((ast_node_t*)local_symbol_table_sc->data[sc_spot])->types.variable.initial_value = init_value;
12901283
} else {
1291-
abort();
1284+
oassert(false && "invalid type");
12921285
}
12931286
} else {
12941287
/* store the data which is an idx here */
@@ -1299,10 +1292,13 @@ void create_symbol_table_for_scope(ast_node_t* module_items, sc_hierarchy* local
12991292
local_symbol_table[num_local_symbol_table] = (ast_node_t*)var_declare;
13001293
num_local_symbol_table++;
13011294

1302-
/* check for an initial value and store it if found */
1303-
var_declare->types.variable.initial_value = init_value;
13041295
module_items->children[i]->children[j] = NULL;
13051296
}
1297+
1298+
/* check for an initial value and copy it over if found */
1299+
VNumber* init_value = get_init_value(var_declare->children[4]);
1300+
((ast_node_t*)local_symbol_table_sc->data[sc_spot])->types.variable.initial_value = init_value;
1301+
13061302
vtr::free(temp_string);
13071303

13081304
remove_child_from_node_at_index(module_items->children[i], j);
@@ -1389,13 +1385,8 @@ VNumber* get_init_value(ast_node_t* node) {
13891385
// by default it is always undefined
13901386
VNumber* init_value = nullptr;
13911387
// Initial value is always the last child, if one exists
1392-
if (node != NULL) {
1393-
if (node->type == NUMBERS) {
1394-
init_value = new VNumber(node->types.vnumber);
1395-
} else {
1396-
warning_message(NETLIST, node->loc,
1397-
"%s", "Could not resolve initial assignment to a constant value, skipping\n");
1398-
}
1388+
if (node != NULL && node->types.vnumber != nullptr) {
1389+
init_value = new VNumber(node->types.vnumber);
13991390
}
14001391
return init_value;
14011392
}

ODIN_II/SRC/read_blif.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ void create_hard_block_nodes(hard_block_models* models, FILE* file, Hashtable* o
565565
}
566566

567567
// Create a fake ast node.
568-
new_node->related_ast_node = (ast_node_t*)vtr::calloc(1, sizeof(ast_node_t));
568+
new_node->related_ast_node = create_node_w_type(HARD_BLOCK, my_location);
569569
new_node->related_ast_node->children = (ast_node_t**)vtr::calloc(1, sizeof(ast_node_t*));
570570
new_node->related_ast_node->identifier_node = create_tree_node_id(vtr::strdup(subcircuit_name), my_location);
571571

0 commit comments

Comments
 (0)