8
8
#include " ast_util.h"
9
9
#include " ast_elaborate.h"
10
10
#include " parse_making_ast.h"
11
+ #include " netlist_create_from_ast.h"
11
12
#include " odin_util.h"
12
13
#include " vtr_memory.h"
13
14
#include " vtr_util.h"
14
15
15
16
/* This files header */
16
17
#include " ast_loop_unroll.h"
17
18
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 )
19
20
{
20
21
oassert (node && node->type == FOR);
21
22
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);
28
24
oassert (unrolled_for != nullptr );
25
+
26
+ *num_unrolled = unrolled_for->num_children ;
29
27
30
28
/* update parent */
31
29
int i;
30
+ int this_genblk = 0 ;
32
31
for (i = 0 ; i < parent->num_children ; i++)
33
32
{
34
33
if (node == parent->children [i])
35
34
{
36
35
int j;
37
36
for (j = i; j < (unrolled_for->num_children + i); j++)
38
37
{
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);
40
40
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
+ }
41
110
}
42
111
43
112
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 *
46
115
break ;
47
116
}
48
117
}
118
+
119
+ if (this_genblk > 0 )
120
+ {
121
+ local_ref->num_unnamed_genblks ++;
122
+ }
49
123
50
124
free_whole_tree (unrolled_for);
51
125
return parent->children [i];
@@ -54,7 +128,7 @@ ast_node_t *unroll_for_loop(ast_node_t* node, ast_node_t *parent, sc_hierarchy *
54
128
/*
55
129
* (function: resolve_for)
56
130
*/
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)
58
132
{
59
133
oassert (is_for_node (node));
60
134
oassert (node != nullptr );
@@ -87,7 +161,7 @@ ast_node_t* resolve_for(ast_node_t *ast_module, ast_node_t* node)
87
161
bool dup_body = cond_func (value->types .vnumber ->get_value ());
88
162
while (dup_body)
89
163
{
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);
91
165
if (error_code)
92
166
{
93
167
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_
355
429
return resolve_binary_operation (node);
356
430
}
357
431
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)
378
433
{
379
434
ast_node_t * copy = ast_node_deep_copy (body);
380
435
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
395
450
}
396
451
else if (child->type == MODULE_INSTANCE && child->children [0 ]->type != MODULE_INSTANCE)
397
452
{
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
-
402
453
/* find and replace iteration symbol for port connections and parameters */
403
454
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);
405
456
free_whole_tree (named_instance);
406
457
407
458
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
416
467
if (copy->children [i]->children [j] != child->children [j]) free_whole_tree (copy->children [i]->children [j]);
417
468
}
418
469
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);
420
471
free_whole_tree (child);
421
472
}
422
473
}
0 commit comments