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 }
0 commit comments