This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecl.cc
403 lines (353 loc) · 10.4 KB
/
decl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*****************************************************************************
* Copyright 1994-2005, Elliot Mednick and Mark Hummel
* This file is part of Veriwell.
*
* Veriwell is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Veriwell is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************/
/* DECL.C - Declaration node generation and utilities */
#include "glue.h"
#include <stddef.h>
#include "vtypes.h"
#include "tree.h"
#include "decl.h"
#include "flags.h" /* needed for "default_net_type" */
#include "scope.h"
#include "schedule.h"
#include "pass2.h"
/* Node for the integer constant 0 */
tree integer_zero_node;
/* Node for the integer constant 1 */
tree integer_one_node;
/* Node for the single-bit 0 */
tree bit_zero_node;
/* Node for the single-bit 1 */
tree bit_one_node;
/* Node to mark an error */
tree error_mark_node;
static void replace_port(tree here, tree ident, tree node)
{
switch (TREE_CODE(TREE_PURPOSE(here))) {
case IDENTIFIER_NODE:
if (TREE_PURPOSE(here) == ident) {
TREE_PURPOSE(here) = node;
}
break;
case REG_SCALAR_DECL:
case REG_VECTOR_DECL:
case NET_SCALAR_DECL:
case NET_VECTOR_DECL:
if (DECL_NAME(TREE_PURPOSE(here)) == ident) {
PORT_OUTPUT_ATTR(node) = PORT_OUTPUT_ATTR(TREE_PURPOSE(here));
PORT_INPUT_ATTR(node) = PORT_INPUT_ATTR(TREE_PURPOSE(here));
TREE_PURPOSE(here) = node;
}
break;
case PART_REF:
if (TREE_PURPOSE(here) == ident) {
PART_DECL(TREE_PURPOSE(here)) = node;
PORT_OUTPUT_ATTR(TREE_PURPOSE(here)) = PORT_OUTPUT_ATTR(node);
PORT_INPUT_ATTR(TREE_PURPOSE(here)) = PORT_INPUT_ATTR(node);
HIERARCHICAL_ATTR(TREE_PURPOSE(here)) = 0; /* set in check */
}
break;
case BIT_REF:
if (TREE_PURPOSE(here) == ident) {
BIT_REF_DECL(TREE_PURPOSE(here)) = node;
PORT_OUTPUT_ATTR(node) = PORT_OUTPUT_ATTR(TREE_PURPOSE(here));
PORT_INPUT_ATTR(node) = PORT_INPUT_ATTR(TREE_PURPOSE(here));
HIERARCHICAL_ATTR(TREE_PURPOSE(here)) = 0; /* set in check */
}
break;
}
}
/* Search list of ports, find IDENT, and replace with NODE. Ident may
been replaced once before with a NET. */
void redirect_port(tree ident, tree node)
{
tree t;
tree t1;
for (t = MODULE_PORT_LIST(current_scope); t; t = TREE_CHAIN(t)) {
if (TREE_CODE(TREE_PURPOSE(t)) == TREE_LIST) {
for (t1 = TREE_PURPOSE(t); t1; t1 = TREE_CHAIN(t1)) {
replace_port(t1, ident, node);
}
} else {
replace_port(t, ident, node);
}
}
}
tree make_net_spec(enum tree_type net_type, tree range, tree delay)
{
tree node;
if (range == NULL) {
node = make_node(NET_SCALAR_DECL);
} else {
node = make_node(NET_VECTOR_DECL);
DECL_MSB(node) = TREE_OPERAND(range, 0);
DECL_LSB(node) = TREE_OPERAND(range, 1);
}
DECL_CONTEXT(node) = current_scope;
NET_DELAY(node) = delay;
TREE_TYPE(node) = net_type;
NET_SOURCE(node) = NULL_TREE;
return node;
}
tree make_reg_spec(tree range)
{
tree node;
if (range == NULL) {
node = make_node(REG_SCALAR_DECL);
} else {
node = make_node(REG_VECTOR_DECL);
DECL_MSB(node) = TREE_OPERAND(range, 0);
DECL_LSB(node) = TREE_OPERAND(range, 1);
}
DECL_CONTEXT(node) = current_scope;
return node;
}
tree make_integer_spec(tree range)
{
tree node;
node = make_node(INTEGER_DECL);
if (range == NULL) {
DECL_MSB(node) = NULL_TREE;
DECL_LSB(node) = NULL_TREE;
} else {
DECL_MSB(node) = TREE_OPERAND(range, 0);
DECL_LSB(node) = TREE_OPERAND(range, 1);
}
DECL_CONTEXT(node) = current_scope;
TREE_INTEGER_ATTR(node) = 1;
return node;
}
tree make_real_spec(tree range)
{
tree node;
node = make_node(REAL_DECL);
if (range == NULL) {
DECL_MSB(node) = NULL_TREE;
DECL_LSB(node) = NULL_TREE;
} else {
DECL_MSB(node) = TREE_OPERAND(range, 0);
DECL_LSB(node) = TREE_OPERAND(range, 1);
}
DECL_CONTEXT(node) = current_scope;
TREE_REAL_ATTR(node) = 1;
return node;
}
tree make_time_spec(tree range)
{
tree node;
node = make_node(TIME_DECL);
if (range == NULL) {
DECL_MSB(node) = NULL_TREE;
DECL_LSB(node) = NULL_TREE;
} else {
DECL_MSB(node) = TREE_OPERAND(range, 0);
DECL_LSB(node) = TREE_OPERAND(range, 1);
}
DECL_CONTEXT(node) = current_scope;
return node;
}
tree make_event_spec()
{
tree node = make_node(EVENT_DECL);
DECL_CONTEXT(node) = current_scope;
return node;
}
tree make_param_spec(tree range)
{
tree node = make_node(PARAM_DECL);
TREE_CONSTANT_ATTR(node) = 1; /* treat parameter as a constant */
if (range == NULL) {
DECL_MSB(node) = NULL_TREE;
DECL_LSB(node) = NULL_TREE;
} else {
DECL_MSB(node) = TREE_OPERAND(range, 0);
DECL_LSB(node) = TREE_OPERAND(range, 1);
}
DECL_CONTEXT(node) = current_scope;
return node;
}
/* Take a declaration and assign a name to it and possible array bounds.
If more than one name associated with a declarations (reg [31:0] a, b, c)
copy the original node. */
tree make_decl(tree ident, tree spec, tree hibounds, tree lobounds)
{
tree node = spec;
tree decl;
if (ident == error_mark_node) {
return copy_node(error_mark_node);
}
/*
* if no name associated with declaration,
* use this node, else make a copy
*/
if (DECL_NAME(node) != NULL_TREE) {
node = copy_node(node);
}
DECL_NAME(node) = root_port_name(ident); /* Give it a name */
if (TREE_CODE(node) == NET_SCALAR_DECL ||
TREE_CODE(node) == NET_VECTOR_DECL) {
NET_SOURCE(node) = NULL_TREE;
/* Make sure TRI1 and TRI0 get scheduled to be eval'ed in time 0,
* Do this by making a dummy driver; con't assgn initialization
* will know what to do.
*/
if (TREE_TYPE(node) == NET_TRI1_TYPE ||
TREE_TYPE(node) == NET_TRI0_TYPE ||
TREE_TYPE(node) == NET_SUPPLY0_TYPE ||
TREE_TYPE(node) == NET_SUPPLY1_TYPE) {
add_cont_assign_list(make_net_source(node));
}
}
/* If this is an array, build a new node with array bounds */
if (hibounds) { /* implies both hi and lo are non-null */
node = build_array(ident, node, hibounds, lobounds);
}
/* If current_decl of ident is non-NULL, then it is either a port
being declared for the first time (with INPUT, OUTPUT, INOUT),
or it is a redeclaration of a declared port (to qualify the net type,
the size, or to turn it into a REG). */
/* If this is a redeclaration of a port, go back to the port list and
make it point to the new DECL node. */
decl = IDENT_CURRENT_DECL(ident);
if (decl) {
/* Case 1, we are at the INPUT, OUTPUT, or INOUT decl;
* search for the port and redeclare as a net with the
* proper width, etc.
*/
if (TREE_CODE(root_port_decl(ident)) == IDENTIFIER_NODE) {
redirect_port(ident, node);
/* Case 2: previously declared as a port,
* this is the explicit declaration (WIRE, REG, etc.)
*/
} else if (TREE_CODE(decl) != BLOCK_DECL && DECL_CONTEXT(decl) == DECL_CONTEXT(node)) { /* 2nd time decl */
if (REFERENCED_ATTR(decl)) {
error("Redeclared port '%s' has already been referenced",
IDENT(ident), NULL_CHAR);
}
DECL_THREAD(decl) = node;
PORT_REDEFINED_ATTR(decl) = 1;
PORT_INPUT_ATTR(node) = PORT_INPUT_ATTR(decl);
PORT_OUTPUT_ATTR(node) = PORT_OUTPUT_ATTR(decl);
}
}
set_decl(root_port_name(ident), node);
return node;
}
tree make_param_decl(tree ident, tree spec, tree rval)
{
tree node = spec;
/*
* if no name associated with declaration,
* use this node, else make a copy
*/
if (DECL_NAME(node) != NULL_TREE) {
node = copy_node(node);
}
DECL_NAME(node) = ident; /* Give it a name */
set_decl(ident, node);
DECL_PARAM_RVAL(node) = rval;
DECL_PARAM_REDIRECT(node) = NULL_TREE;
TREE_CONSTANT_ATTR(node) = 1;
return node;
}
/* make a decl for a named BLOCK or task or function */
tree make_block_decl(tree ident, tree context, tree block)
{
tree decl = make_node(BLOCK_DECL);
if (ident == error_mark_node) {
return ident;
}
DECL_CONTEXT(decl) = context;
DECL_THREAD(decl) = block;
DECL_NAME(decl) = ident;
set_decl(ident, decl);
TREE_CHAIN(decl) = BLOCK_DECL(context);
BLOCK_DECL(context) = decl;
return ident;
}
/* Used for implied declarations of nets */
tree make_default_net(tree ident)
{
tree spec = make_net_spec(default_net_type, NULL_TREE, NULL_TREE);
tree decl = make_decl(ident, spec, NULL_TREE, NULL_TREE);
BLOCK_DECL(current_scope) = chainon(decl, BLOCK_DECL(current_scope));
REFERENCED_ATTR(decl) = 1;
return decl;
}
/* Make a TMP_DECL node for statements that use intermediate values */
static tree tmp_decl_free = NULL;
tree make_tmp_decl(tree lval)
{
tree tmp;
if (tmp_decl_free) {
tmp = tmp_decl_free;
tmp_decl_free = DECL_THREAD(tmp_decl_free);
DECL_THREAD(tmp) = NULL;
} else {
tmp = make_node(TMP_DECL);
}
TREE_CHAIN(tmp) = lval;
pass3_decl(tmp);
return tmp;
}
void free_tmp_decl(tree tmp)
{
free(DECL_STORAGE(tmp));
DECL_THREAD(tmp) = tmp_decl_free;
tmp_decl_free = tmp;
}
tree root_port_decl(tree node)
{
switch (TREE_CODE(node)) {
case PART_REF:
return PART_DECL(node);
case BIT_REF:
return BIT_REF_DECL(node);
case IDENTIFIER_NODE:
return IDENT_CURRENT_DECL(node);
}
return node;
}
tree root_port_name(tree node)
{
switch (TREE_CODE(node)) {
case PART_REF:
return PART_NAME(node);
case BIT_REF:
return BIT_REF_NAME(node);
case IDENTIFIER_NODE:
return node;
}
return node;
}
/**************************************************************
*
* init_decl
* - initialize global and static variables in decl.c
*
**************************************************************
*/
void init_decl()
{
tmp_decl_free = NULL;
integer_zero_node = NULL_TREE;
integer_one_node = NULL_TREE;
bit_zero_node = NULL_TREE;
bit_one_node = NULL_TREE;
error_mark_node = NULL_TREE;
}