forked from steveicarus/iverilog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelab_type.cc
498 lines (420 loc) · 13.9 KB
/
elab_type.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
* Copyright (c) 2012-2024 Stephen Williams ([email protected])
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
# include "PExpr.h"
# include "PScope.h"
# include "pform_types.h"
# include "netlist.h"
# include "netclass.h"
# include "netdarray.h"
# include "netenum.h"
# include "netqueue.h"
# include "netparray.h"
# include "netscalar.h"
# include "netstruct.h"
# include "netvector.h"
# include "netmisc.h"
# include <typeinfo>
# include "ivl_assert.h"
using namespace std;
/*
* Elaborations of types may vary depending on the scope that it is
* done in, so keep a per-scope cache of the results.
*/
ivl_type_t data_type_t::elaborate_type(Design*des, NetScope*scope)
{
scope = find_scope(des, scope);
Definitions*use_definitions = scope;
map<Definitions*,ivl_type_t>::iterator pos = cache_type_elaborate_.lower_bound(use_definitions);
if (pos != cache_type_elaborate_.end() && pos->first == use_definitions)
return pos->second;
ivl_type_t tmp;
if (elaborating) {
des->errors++;
cerr << get_fileline() << ": error: "
<< "Circular type definition found involving `" << *this << "`."
<< endl;
// Try to recover
tmp = netvector_t::integer_type();
} else {
elaborating = true;
tmp = elaborate_type_raw(des, scope);
elaborating = false;
}
cache_type_elaborate_.insert(pos, pair<NetScope*,ivl_type_t>(scope, tmp));
return tmp;
}
NetScope *data_type_t::find_scope(Design *, NetScope *scope) const
{
return scope;
}
ivl_type_t data_type_t::elaborate_type_raw(Design*des, NetScope*) const
{
cerr << get_fileline() << ": internal error: "
<< "Elaborate method not implemented for " << typeid(*this).name()
<< "." << endl;
des->errors += 1;
return 0;
}
ivl_type_t atom_type_t::elaborate_type_raw(Design*des, NetScope*) const
{
switch (type_code) {
case INTEGER:
return netvector_t::integer_type(signed_flag);
case TIME:
if (signed_flag)
return &netvector_t::time_signed;
else
return &netvector_t::time_unsigned;
case LONGINT:
if (signed_flag)
return &netvector_t::atom2s64;
else
return &netvector_t::atom2u64;
case INT:
if (signed_flag)
return &netvector_t::atom2s32;
else
return &netvector_t::atom2u32;
case SHORTINT:
if (signed_flag)
return &netvector_t::atom2s16;
else
return &netvector_t::atom2u16;
case BYTE:
if (signed_flag)
return &netvector_t::atom2s8;
else
return &netvector_t::atom2u8;
default:
cerr << get_fileline() << ": internal error: "
<< "atom_type_t type_code=" << type_code << "." << endl;
des->errors += 1;
return 0;
}
}
ivl_type_t class_type_t::elaborate_type_raw(Design*des, NetScope*scope) const
{
return scope->find_class(des, name);
}
/*
* elaborate_type_raw for enumerations is actually mostly performed
* during scope elaboration so that the enumeration literals are
* available at the right time. At that time, the netenum_t* object is
* stashed in the scope so that I can retrieve it here.
*/
ivl_type_t enum_type_t::elaborate_type_raw(Design *des, NetScope *scope) const
{
ivl_type_t base = base_type->elaborate_type(des, scope);
const class netvector_t *vec_type = dynamic_cast<const netvector_t*>(base);
if (!vec_type && !dynamic_cast<const netparray_t*>(base)) {
cerr << get_fileline() << ": error: "
<< "Invalid enum base type `" << *base << "`."
<< endl;
des->errors++;
} else if (base->slice_dimensions().size() > 1) {
cerr << get_fileline() << ": error: "
<< "Enum type must not have more than 1 packed dimension."
<< endl;
des->errors++;
}
bool integer_flag = false;
if (vec_type)
integer_flag = vec_type->get_isint();
netenum_t *type = new netenum_t(base, names->size(), integer_flag);
type->set_line(*this);
scope->add_enumeration_set(this, type);
return type;
}
ivl_type_t vector_type_t::elaborate_type_raw(Design*des, NetScope*scope) const
{
netranges_t packed;
if (pdims.get())
evaluate_ranges(des, scope, this, packed, *pdims);
netvector_t*tmp = new netvector_t(packed, base_type);
tmp->set_signed(signed_flag);
tmp->set_isint(integer_flag);
tmp->set_implicit(implicit_flag);
return tmp;
}
ivl_type_t real_type_t::elaborate_type_raw(Design*, NetScope*) const
{
switch (type_code_) {
case REAL:
return &netreal_t::type_real;
case SHORTREAL:
return &netreal_t::type_shortreal;
}
return 0;
}
ivl_type_t string_type_t::elaborate_type_raw(Design*, NetScope*) const
{
return &netstring_t::type_string;
}
ivl_type_t parray_type_t::elaborate_type_raw(Design*des, NetScope*scope) const
{
netranges_t packed;
if (dims.get())
evaluate_ranges(des, scope, this, packed, *dims);
ivl_type_t etype = base_type->elaborate_type(des, scope);
if (!etype->packed()) {
cerr << this->get_fileline() << " error: Packed array ";
cerr << "base-type `";
cerr << *base_type;
cerr << "` is not packed." << endl;
des->errors++;
}
return new netparray_t(packed, etype);
}
ivl_type_t struct_type_t::elaborate_type_raw(Design*des, NetScope*scope) const
{
netstruct_t*res = new netstruct_t;
res->set_line(*this);
res->packed(packed_flag);
res->set_signed(signed_flag);
if (union_flag)
res->union_flag(true);
for (list<struct_member_t*>::iterator cur = members->begin()
; cur != members->end() ; ++ cur) {
// Elaborate the type of the member.
struct_member_t*curp = *cur;
ivl_type_t mem_vec = curp->type->elaborate_type(des, scope);
if (mem_vec == 0)
continue;
// There may be several names that are the same type:
// <data_type> name1, name2, ...;
// Process all the member, and give them a type.
for (list<decl_assignment_t*>::iterator cur_name = curp->names->begin()
; cur_name != curp->names->end() ; ++ cur_name) {
decl_assignment_t*namep = *cur_name;
if (packed_flag && namep->expr) {
cerr << namep->expr->get_fileline() << " error: "
<< "Packed structs must not have default member values."
<< endl;
des->errors++;
}
netstruct_t::member_t memb;
memb.name = namep->name.first;
memb.net_type = elaborate_array_type(des, scope, *this,
mem_vec, namep->index);
res->append_member(des, memb);
}
}
return res;
}
static ivl_type_t elaborate_darray_check_type(Design *des, const LineInfo &li,
ivl_type_t type,
const char *darray_type)
{
if (dynamic_cast<const netvector_t*>(type) ||
dynamic_cast<const netparray_t*>(type) ||
dynamic_cast<const netreal_t*>(type) ||
dynamic_cast<const netstring_t*>(type))
return type;
cerr << li.get_fileline() << ": Sorry: "
<< darray_type << " of type `" << *type
<< "` is not yet supported." << endl;
des->errors++;
// Return something to recover
return new netvector_t(IVL_VT_LOGIC);
}
static ivl_type_t elaborate_queue_type(Design *des, NetScope *scope,
const LineInfo &li, ivl_type_t base_type,
PExpr *ridx)
{
base_type = elaborate_darray_check_type(des, li, base_type, "Queue");
long max_idx = -1;
if (ridx) {
NetExpr*tmp = elab_and_eval(des, scope, ridx, -1, true);
NetEConst*cv = dynamic_cast<NetEConst*>(tmp);
if (cv == 0) {
cerr << li.get_fileline() << ": error: "
<< "queue bound must be constant."
<< endl;
des->errors++;
} else {
verinum res = cv->value();
if (res.is_defined()) {
max_idx = res.as_long();
if (max_idx < 0) {
cerr << li.get_fileline() << ": error: "
<< "queue bound must be positive ("
<< max_idx << ")." << endl;
des->errors++;
max_idx = -1;
}
} else {
cerr << li.get_fileline() << ": error: "
<< "queue bound must be defined."
<< endl;
des->errors++;
}
}
delete cv;
}
return new netqueue_t(base_type, max_idx);
}
// If dims is not empty create a unpacked array type and clear dims, otherwise
// return the base type. Also check that we actually support the base type.
static ivl_type_t elaborate_static_array_type(Design *des, const LineInfo &li,
ivl_type_t base_type,
netranges_t &dims)
{
if (dims.empty())
return base_type;
if (dynamic_cast<const netqueue_t*>(base_type)) {
cerr << li.get_fileline() << ": sorry: "
<< "array of queue type is not yet supported."
<< endl;
des->errors++;
// Recover
base_type = new netvector_t(IVL_VT_LOGIC);
} else if (dynamic_cast<const netdarray_t*>(base_type)) {
cerr << li.get_fileline() << ": sorry: "
<< "array of dynamic array type is not yet supported."
<< endl;
des->errors++;
// Recover
base_type = new netvector_t(IVL_VT_LOGIC);
}
ivl_type_t type = new netuarray_t(dims, base_type);
dims.clear();
return type;
}
ivl_type_t elaborate_array_type(Design *des, NetScope *scope,
const LineInfo &li, ivl_type_t base_type,
const list<pform_range_t> &dims)
{
const long warn_dimension_size = 1 << 30;
netranges_t dimensions;
dimensions.reserve(dims.size());
ivl_type_t type = base_type;
for (list<pform_range_t>::const_iterator cur = dims.begin();
cur != dims.end() ; ++cur) {
PExpr *lidx = cur->first;
PExpr *ridx = cur->second;
if (lidx == 0 && ridx == 0) {
// Special case: If we encounter an undefined dimensions,
// then turn this into a dynamic array and put all the
// packed dimensions there.
type = elaborate_static_array_type(des, li, type, dimensions);
type = elaborate_darray_check_type(des, li, type, "Dynamic array");
type = new netdarray_t(type);
continue;
} else if (dynamic_cast<PENull*>(lidx)) {
// Special case: Detect the mark for a QUEUE declaration,
// which is the dimensions [null:max_idx].
type = elaborate_static_array_type(des, li, type, dimensions);
type = elaborate_queue_type(des, scope, li, type, ridx);
continue;
}
long index_l, index_r;
evaluate_range(des, scope, &li, *cur, index_l, index_r);
if (abs(index_r - index_l) > warn_dimension_size) {
cerr << li.get_fileline() << ": warning: "
<< "Array dimension is greater than "
<< warn_dimension_size << "."
<< endl;
}
dimensions.push_back(netrange_t(index_l, index_r));
}
return elaborate_static_array_type(des, li, type, dimensions);
}
ivl_type_t uarray_type_t::elaborate_type_raw(Design*des, NetScope*scope) const
{
ivl_type_t btype = base_type->elaborate_type(des, scope);
return elaborate_array_type(des, scope, *this, btype, *dims.get());
}
ivl_type_t typeref_t::elaborate_type_raw(Design*des, NetScope*s) const
{
if (!s) {
// Try to recover
return new netvector_t(IVL_VT_LOGIC);
}
return type->elaborate_type(des, s);
}
NetScope *typeref_t::find_scope(Design *des, NetScope *s) const
{
// If a scope has been specified use that as a starting point for the
// search
if (scope)
s = des->find_package(scope->pscope_name());
return s;
}
ivl_type_t typedef_t::elaborate_type(Design *des, NetScope *scope)
{
if (!data_type.get()) {
cerr << get_fileline() << ": error: Undefined type `" << name << "`."
<< endl;
des->errors++;
// Try to recover
return netvector_t::integer_type();
}
// Search upwards from where the type was referenced
scope = scope->find_typedef_scope(des, this);
if (!scope) {
cerr << get_fileline() << ": sorry: "
<< "Can not find the scope type definition `" << name << "`."
<< endl;
des->errors++;
// Try to recover
return netvector_t::integer_type();
}
ivl_type_t elab_type = data_type->elaborate_type(des, scope);
if (!elab_type)
return netvector_t::integer_type();
bool type_ok = true;
switch (basic_type) {
case ENUM:
type_ok = dynamic_cast<const netenum_t *>(elab_type);
break;
case STRUCT: {
const netstruct_t *struct_type = dynamic_cast<const netstruct_t *>(elab_type);
type_ok = struct_type && !struct_type->union_flag();
break;
}
case UNION: {
const netstruct_t *struct_type = dynamic_cast<const netstruct_t *>(elab_type);
type_ok = struct_type && struct_type->union_flag();
break;
}
case CLASS:
type_ok = dynamic_cast<const netclass_t *>(elab_type);
break;
default:
break;
}
if (!type_ok) {
cerr << data_type->get_fileline() << " error: "
<< "Unexpected type `" << *elab_type << "` for `" << name
<< "`. It was forward declared as `" << basic_type
<< "` at " << get_fileline() << "."
<< endl;
des->errors++;
}
return elab_type;
}
ivl_type_t type_parameter_t::elaborate_type_raw(Design *des, NetScope*scope) const
{
ivl_type_t type;
scope->get_parameter(des, name, type);
// Recover
if (!type)
return netvector_t::integer_type();
return type;
}