Skip to content

Commit 574bc4f

Browse files
authored
Merge pull request verilog-to-routing#942 from CAS-Atlantic/labels
ODIN II: initial hierarchy for named and generate blocks
2 parents e2f3f72 + 5b01866 commit 574bc4f

12 files changed

+652
-371
lines changed

ODIN_II/SRC/ast_elaborate.cpp

Lines changed: 331 additions & 234 deletions
Large diffs are not rendered by default.

ODIN_II/SRC/ast_loop_unroll.cpp

Lines changed: 87 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,105 @@
88
#include "ast_util.h"
99
#include "ast_elaborate.h"
1010
#include "parse_making_ast.h"
11+
#include "netlist_create_from_ast.h"
1112
#include "odin_util.h"
1213
#include "vtr_memory.h"
1314
#include "vtr_util.h"
1415

1516
/* This files header */
1617
#include "ast_loop_unroll.h"
1718

18-
ast_node_t *unroll_for_loop(ast_node_t* node, ast_node_t *parent, sc_hierarchy *local_ref)
19+
ast_node_t *unroll_for_loop(ast_node_t* node, ast_node_t *parent, int *num_unrolled, sc_hierarchy *local_ref, bool is_generate)
1920
{
2021
oassert(node && node->type == FOR);
2122

22-
char *module_id = local_ref->instance_name_prefix;
23-
long sc_spot = sc_lookup_string(module_names_to_idx, module_id);
24-
oassert(sc_spot > -1);
25-
ast_node_t *ast_module = (ast_node_t *)module_names_to_idx->data[sc_spot];
26-
27-
ast_node_t* unrolled_for = resolve_for(ast_module, node);
23+
ast_node_t* unrolled_for = resolve_for(node);
2824
oassert(unrolled_for != nullptr);
25+
26+
*num_unrolled = unrolled_for->num_children;
2927

3028
/* update parent */
3129
int i;
30+
int this_genblk = 0;
3231
for (i = 0; i < parent->num_children; i++)
3332
{
3433
if (node == parent->children[i])
3534
{
3635
int j;
3736
for (j = i; j < (unrolled_for->num_children + i); j++)
3837
{
39-
add_child_to_node_at_index(parent, unrolled_for->children[j-i], j);
38+
ast_node_t *child = unrolled_for->children[j-i];
39+
add_child_to_node_at_index(parent, child, j);
4040
unrolled_for->children[j-i] = NULL;
41+
42+
/* create scopes as necessary */
43+
if (is_generate)
44+
{
45+
oassert(child->type == BLOCK);
46+
47+
/* generate blocks always have scopes; parent has access to named block
48+
but not unnamed, and child always has access to parent */
49+
sc_hierarchy *child_hierarchy = init_sc_hierarchy();
50+
child->types.hierarchy = child_hierarchy;
51+
52+
child_hierarchy->top_node = child;
53+
child_hierarchy->parent = local_ref;
54+
55+
if (child->types.identifier != NULL)
56+
{
57+
local_ref->block_children = (sc_hierarchy **)vtr::realloc(local_ref->block_children, sizeof(sc_hierarchy)*(local_ref->num_block_children + 1));
58+
local_ref->block_children[local_ref->num_block_children] = child_hierarchy;
59+
local_ref->num_block_children++;
60+
61+
/* add an array reference to this label */
62+
std::string new_id(child->types.identifier);
63+
new_id = new_id + "[" + std::to_string(j-i) + "]";
64+
vtr::free(child->types.identifier);
65+
child->types.identifier = vtr::strdup(new_id.c_str());
66+
67+
child_hierarchy->scope_id = node->types.identifier;
68+
child_hierarchy->instance_name_prefix = make_full_ref_name(local_ref->instance_name_prefix, NULL, child->types.identifier, NULL, -1);
69+
}
70+
else
71+
{
72+
/* create a unique scope id/instance name prefix for internal use */
73+
this_genblk = local_ref->num_unnamed_genblks + 1;
74+
std::string new_scope_id("genblk");
75+
new_scope_id = new_scope_id + std::to_string(this_genblk) + "[" + std::to_string(j-i) + "]";
76+
child_hierarchy->scope_id = vtr::strdup(new_scope_id.c_str());
77+
child_hierarchy->instance_name_prefix = make_full_ref_name(local_ref->instance_name_prefix, NULL, child_hierarchy->scope_id, NULL, -1);
78+
}
79+
80+
/* string caches */
81+
create_param_table_for_scope(child, child_hierarchy);
82+
create_symbol_table_for_scope(child, child_hierarchy);
83+
}
84+
else if (child->type == BLOCK && child->types.identifier != NULL)
85+
{
86+
/* only create scope if child is named block */
87+
sc_hierarchy *child_hierarchy = init_sc_hierarchy();
88+
child->types.hierarchy = child_hierarchy;
89+
90+
child_hierarchy->top_node = child;
91+
child_hierarchy->parent = local_ref;
92+
93+
local_ref->block_children = (sc_hierarchy **)vtr::realloc(local_ref->block_children, sizeof(sc_hierarchy)*(local_ref->num_block_children + 1));
94+
local_ref->block_children[local_ref->num_block_children] = child_hierarchy;
95+
local_ref->num_block_children++;
96+
97+
/* add an array reference to this label */
98+
std::string new_id(child->types.identifier);
99+
new_id = new_id + "[" + std::to_string(j-i) + "]";
100+
vtr::free(child->types.identifier);
101+
child->types.identifier = vtr::strdup(new_id.c_str());
102+
103+
child_hierarchy->scope_id = node->types.identifier;
104+
child_hierarchy->instance_name_prefix = make_full_ref_name(local_ref->instance_name_prefix, NULL, child->types.identifier, NULL, -1);
105+
106+
/* string caches */
107+
create_param_table_for_scope(child, child_hierarchy);
108+
create_symbol_table_for_scope(child, child_hierarchy);
109+
}
41110
}
42111

43112
oassert(j == (unrolled_for->num_children + i) && parent->children[j] == node);
@@ -46,6 +115,11 @@ ast_node_t *unroll_for_loop(ast_node_t* node, ast_node_t *parent, sc_hierarchy *
46115
break;
47116
}
48117
}
118+
119+
if (this_genblk > 0)
120+
{
121+
local_ref->num_unnamed_genblks++;
122+
}
49123

50124
free_whole_tree(unrolled_for);
51125
return parent->children[i];
@@ -54,7 +128,7 @@ ast_node_t *unroll_for_loop(ast_node_t* node, ast_node_t *parent, sc_hierarchy *
54128
/*
55129
* (function: resolve_for)
56130
*/
57-
ast_node_t* resolve_for(ast_node_t *ast_module, ast_node_t* node)
131+
ast_node_t* resolve_for(ast_node_t* node)
58132
{
59133
oassert(is_for_node(node));
60134
oassert(node != nullptr);
@@ -87,7 +161,7 @@ ast_node_t* resolve_for(ast_node_t *ast_module, ast_node_t* node)
87161
bool dup_body = cond_func(value->types.vnumber->get_value());
88162
while(dup_body)
89163
{
90-
ast_node_t* new_body = dup_and_fill_body(ast_module, body, pre, &value, &error_code);
164+
ast_node_t* new_body = dup_and_fill_body(body, pre, &value, &error_code);
91165
if(error_code)
92166
{
93167
error_message(PARSE_ERROR, pre->line_number, pre->file_number, "%s", "Unsupported pre-condition node in for loop");
@@ -355,26 +429,7 @@ post_condition_function resolve_post_condition(ast_node_t* assignment, ast_node_
355429
return resolve_binary_operation(node);
356430
}
357431

358-
ast_node_t* replace_named_module(ast_node_t* module, ast_node_t** value)
359-
{
360-
ast_node_t* copy = ast_node_deep_copy(module);
361-
362-
oassert( value && "Value node reference is NULL");
363-
oassert( *value && "Value node is NULL");
364-
oassert( (*value)->type == NUMBERS && "Value node type is not a NUMBER");
365-
366-
long int val = (*value)->types.vnumber->get_value();
367-
std::string concat_string(copy->children[0]->types.identifier);
368-
concat_string = concat_string + "[" + std::to_string(val) + "]";
369-
370-
vtr::free(copy->children[0]->types.identifier);
371-
copy->children[0]->types.identifier = vtr::strdup(concat_string.c_str());
372-
373-
free_whole_tree(module);
374-
return copy;
375-
}
376-
377-
ast_node_t* dup_and_fill_body(ast_node_t *ast_module, ast_node_t* body, ast_node_t* pre, ast_node_t** value, int* error_code)
432+
ast_node_t* dup_and_fill_body(ast_node_t* body, ast_node_t* pre, ast_node_t** value, int* error_code)
378433
{
379434
ast_node_t* copy = ast_node_deep_copy(body);
380435
for(long i = 0; i<copy->num_children; i++)
@@ -395,13 +450,9 @@ ast_node_t* dup_and_fill_body(ast_node_t *ast_module, ast_node_t* body, ast_node
395450
}
396451
else if(child->type == MODULE_INSTANCE && child->children[0]->type != MODULE_INSTANCE)
397452
{
398-
/* give this unrolled instance a unique name */
399-
copy->children[i]->children[1] = replace_named_module(child->children[1], value);
400-
oassert(copy->children[i]->children[1]);
401-
402453
/* find and replace iteration symbol for port connections and parameters */
403454
ast_node_t *named_instance = child->children[1];
404-
copy->children[i]->children[1] = dup_and_fill_body(ast_module, named_instance, pre, value, error_code);
455+
copy->children[i]->children[1] = dup_and_fill_body(named_instance, pre, value, error_code);
405456
free_whole_tree(named_instance);
406457

407458
is_unrolled = true;
@@ -416,7 +467,7 @@ ast_node_t* dup_and_fill_body(ast_node_t *ast_module, ast_node_t* body, ast_node
416467
if (copy->children[i]->children[j] != child->children[j]) free_whole_tree(copy->children[i]->children[j]);
417468
}
418469

419-
copy->children[i] = dup_and_fill_body(ast_module, child, pre, value, error_code);
470+
copy->children[i] = dup_and_fill_body(child, pre, value, error_code);
420471
free_whole_tree(child);
421472
}
422473
}

ODIN_II/SRC/ast_util.cpp

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,6 @@ void make_concat_into_list_of_strings(ast_node_t *concat_top, char *instance_nam
386386
int j;
387387
ast_node_t *rnode[3] = { 0 };
388388

389-
STRING_CACHE *local_symbol_table_sc = local_ref->local_symbol_table_sc;
390-
391389
concat_top->types.concat.num_bit_strings = 0;
392390
concat_top->types.concat.bit_strings = NULL;
393391

@@ -405,14 +403,13 @@ void make_concat_into_list_of_strings(ast_node_t *concat_top, char *instance_nam
405403
if (concat_top->children[i]->type == IDENTIFIERS)
406404
{
407405
char *temp_string = make_full_ref_name(NULL, NULL, NULL, concat_top->children[i]->types.identifier, -1);
408-
long sc_spot;
409-
if ((sc_spot = sc_lookup_string(local_symbol_table_sc, temp_string)) == -1)
406+
ast_node_t *var_declare = resolve_hierarchical_name_reference(local_ref, temp_string);
407+
if (var_declare == NULL)
410408
{
411409
error_message(NETLIST_ERROR, concat_top->line_number, concat_top->file_number, "Missing declaration of this symbol %s\n", temp_string);
412410
}
413411
else
414412
{
415-
ast_node_t *var_declare = (ast_node_t*)local_symbol_table_sc->data[sc_spot];
416413
if (var_declare->children[1] == NULL)
417414
{
418415
concat_top->types.concat.num_bit_strings ++;
@@ -566,7 +563,7 @@ char *get_name_of_pin_at_bit(ast_node_t *var_node, int bit, char *instance_name_
566563
char *return_string = NULL;
567564
ast_node_t *rnode[3] = { 0 };
568565

569-
STRING_CACHE *local_symbol_table_sc = local_ref->local_symbol_table_sc;
566+
// STRING_CACHE *local_symbol_table_sc = local_ref->local_symbol_table_sc;
570567

571568
if (var_node->type == ARRAY_REF)
572569
{
@@ -594,22 +591,22 @@ char *get_name_of_pin_at_bit(ast_node_t *var_node, int bit, char *instance_name_
594591
}
595592
else if (var_node->type == IDENTIFIERS)
596593
{
597-
long sc_spot;
594+
ast_node_t *symbol_node;
598595
int pin_index = 0;
599596

600-
if ((sc_spot = sc_lookup_string(local_symbol_table_sc, var_node->types.identifier)) == -1)
597+
if ((symbol_node = resolve_hierarchical_name_reference(local_ref, var_node->types.identifier)) == NULL)
601598
{
602599
error_message(NETLIST_ERROR, var_node->line_number, var_node->file_number, "Missing declaration of this symbol %s\n", var_node->types.identifier);
603600
}
604601

605-
if (((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[1] == NULL)
602+
if (symbol_node->children[1] == NULL)
606603
{
607604
pin_index = bit;
608605
}
609-
else if (((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[3] == NULL)
606+
else if (symbol_node->children[3] == NULL)
610607
{
611-
oassert(((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[2]->type == NUMBERS);
612-
pin_index = ((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[2]->types.vnumber->get_value() + bit;
608+
oassert(symbol_node->children[2]->type == NUMBERS);
609+
pin_index = symbol_node->children[2]->types.vnumber->get_value() + bit;
613610
}
614611
else
615612
oassert(false);
@@ -704,8 +701,6 @@ char_list_t *get_name_of_pins(ast_node_t *var_node, char *instance_name_prefix,
704701
int i;
705702
int width = 0;
706703

707-
STRING_CACHE *local_symbol_table_sc = local_ref->local_symbol_table_sc;
708-
709704
if (var_node->type == ARRAY_REF)
710705
{
711706
width = 1;
@@ -740,16 +735,11 @@ char_list_t *get_name_of_pins(ast_node_t *var_node, char *instance_name_prefix,
740735
else if (var_node->type == IDENTIFIERS)
741736
{
742737
/* need to look in the symbol table for details about this identifier (i.e. is it a port) */
743-
long sc_spot;
744738

745-
ast_node_t *sym_node = NULL;
746739
char *temp_string = make_full_ref_name(NULL, NULL, NULL, var_node->types.identifier, -1);
740+
ast_node_t *sym_node = resolve_hierarchical_name_reference(local_ref, temp_string);
747741

748-
if ((sc_spot = sc_lookup_string(local_symbol_table_sc, temp_string)) > -1)
749-
{
750-
sym_node = (ast_node_t*)local_symbol_table_sc->data[sc_spot];
751-
}
752-
else
742+
if (sym_node == NULL)
753743
{
754744
error_message(NETLIST_ERROR, var_node->line_number, var_node->file_number, "Missing declaration of this symbol %s\n", temp_string);
755745
}
@@ -859,7 +849,6 @@ long get_size_of_variable(ast_node_t *node, sc_hierarchy *local_ref)
859849
long sc_spot = 0;
860850
ast_node_t *var_declare = NULL;
861851

862-
STRING_CACHE *local_symbol_table_sc = local_ref->local_symbol_table_sc;
863852
STRING_CACHE *local_param_table_sc = local_ref->local_param_table_sc;
864853

865854
switch(node->type)
@@ -884,10 +873,10 @@ long get_size_of_variable(ast_node_t *node, sc_hierarchy *local_ref)
884873
return assignment_size;
885874
}
886875

887-
sc_spot = sc_lookup_string(local_symbol_table_sc, node->types.identifier);
888-
if (sc_spot > -1)
876+
ast_node_t *sym_node = resolve_hierarchical_name_reference(local_ref, node->types.identifier);
877+
if (sym_node != NULL)
889878
{
890-
var_declare = (ast_node_t *)local_symbol_table_sc->data[sc_spot];
879+
var_declare = sym_node; // (ast_node_t *)local_symbol_table_sc->data[sc_spot];
891880
break;
892881
}
893882

@@ -897,10 +886,10 @@ long get_size_of_variable(ast_node_t *node, sc_hierarchy *local_ref)
897886

898887
case ARRAY_REF:
899888
{
900-
sc_spot = sc_lookup_string(local_symbol_table_sc, node->children[0]->types.identifier);
901-
if (sc_spot > -1)
889+
ast_node_t *sym_node = resolve_hierarchical_name_reference(local_ref, node->children[0]->types.identifier);
890+
if (sym_node != NULL)
902891
{
903-
var_declare = (ast_node_t *)local_symbol_table_sc->data[sc_spot];
892+
var_declare = sym_node;
904893
break;
905894
}
906895

@@ -1421,6 +1410,7 @@ void initial_node(ast_node_t *new_node, ids id, int line_number, int file_number
14211410
new_node->net_node = 0;
14221411
new_node->types.vnumber = nullptr;
14231412
new_node->types.identifier = NULL;
1413+
new_node->types.hierarchy = NULL;
14241414
new_node->chunk_size = 1;
14251415
}
14261416

0 commit comments

Comments
 (0)