forked from steveicarus/iverilog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_design.cc
1151 lines (967 loc) · 32.7 KB
/
net_design.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2000-2022 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 "config.h"
# include <iostream>
# include <set>
# include <cstdlib>
/*
* This source file contains all the implementations of the Design
* class declared in netlist.h.
*/
# include "netlist.h"
# include "netscalar.h"
# include "netvector.h"
# include "util.h"
# include "compiler.h"
# include "netmisc.h"
# include "PExpr.h"
# include "PTask.h"
# include <sstream>
# include "ivl_assert.h"
using namespace std;
Design:: Design()
: errors(0), nodes_(0), procs_(0), aprocs_(0)
{
branches_ = 0;
procs_idx_ = 0;
des_precision_ = 0;
nodes_functor_cur_ = 0;
nodes_functor_nxt_ = 0;
des_delay_sel_ = Design::TYP;
}
Design::~Design()
{
}
void Design::set_precision(int val)
{
if (val < des_precision_)
des_precision_ = val;
}
int Design::get_precision() const
{
return des_precision_;
}
void Design::set_delay_sel(delay_sel_t sel)
{
des_delay_sel_ = sel;
}
const char* Design::get_delay_sel() const
{
switch (des_delay_sel_) {
case Design::MIN:
return "MINIMUM";
break;
case Design::TYP:
return "TYPICAL";
break;
case Design::MAX:
return "MAXIMUM";
break;
default:
assert(0);
return "TYPICAL";
}
}
uint64_t Design::scale_to_precision(uint64_t val,
const NetScope*scope) const
{
int units = scope->time_unit();
assert( units >= des_precision_ );
while (units > des_precision_) {
units -= 1;
val *= 10;
}
return val;
}
NetScope* Design::make_root_scope(perm_string root, NetScope*unit_scope,
bool program_block, bool is_interface)
{
NetScope *root_scope_;
root_scope_ = new NetScope(0, hname_t(root), NetScope::MODULE, unit_scope,
false, program_block, is_interface);
/* This relies on the fact that the basename return value is
permallocated. */
root_scope_->set_module_name(root_scope_->basename());
root_scopes_.push_back(root_scope_);
return root_scope_;
}
NetScope* Design::find_root_scope()
{
assert(root_scopes_.front());
return root_scopes_.front();
}
list<NetScope*> Design::find_root_scopes() const
{
return root_scopes_;
}
NetScope* Design::make_package_scope(perm_string name, NetScope*unit_scope,
bool is_unit)
{
NetScope*scope;
scope = new NetScope(0, hname_t(name), NetScope::PACKAGE, unit_scope,
false, false, false, is_unit);
scope->set_module_name(scope->basename());
packages_[name] = scope;
return scope;
}
NetScope* Design::find_package(perm_string name) const
{
map<perm_string,NetScope*>::const_iterator cur = packages_.find(name);
if (cur == packages_.end())
return 0;
return cur->second;
}
list<NetScope*> Design::find_package_scopes() const
{
list<NetScope*>res;
for (map<perm_string,NetScope*>::const_iterator cur = packages_.begin()
; cur != packages_.end() ; ++cur) {
res.push_back (cur->second);
}
return res;
}
/*
* This method locates a scope in the design, given its rooted
* hierarchical name. Each component of the key is used to scan one
* more step down the tree until the name runs out or the search
* fails.
*/
NetScope* Design::find_scope(const std::list<hname_t>&path) const
{
if (path.empty())
return 0;
for (list<NetScope*>::const_iterator scope = root_scopes_.begin()
; scope != root_scopes_.end(); ++ scope ) {
NetScope*cur = *scope;
if (path.front() != cur->fullname())
continue;
std::list<hname_t> tmp = path;
tmp.pop_front();
while (cur) {
if (tmp.empty()) return cur;
cur = cur->child( tmp.front() );
tmp.pop_front();
}
}
return 0;
}
/*
* This method locates a scope in the design, given its rooted
* hierarchical name. Each component of the key is used to scan one
* more step down the tree until the name runs out or the search
* fails.
*/
NetScope* Design::find_scope(const hname_t&path) const
{
for (list<NetScope*>::const_iterator scope = root_scopes_.begin()
; scope != root_scopes_.end(); ++ scope ) {
NetScope*cur = *scope;
if (path.peek_name() == cur->basename())
return cur;
}
return 0;
}
static bool is_design_unit(NetScope*scope)
{
return (scope->type() == NetScope::MODULE && !scope->nested_module())
|| (scope->type() == NetScope::PACKAGE);
}
static bool is_subroutine(NetScope::TYPE type)
{
return type == NetScope::TASK || type == NetScope::FUNC;
}
/*
* This method locates a scope within another scope, given its relative
* hierarchical name. Each component of the key is used to scan one
* more step down the tree until the name runs out or the search
* fails.
*/
NetScope* Design::find_scope_(NetScope*scope, const std::list<hname_t>&path,
NetScope::TYPE type) const
{
std::list<hname_t> tmp = path;
do {
hname_t key = tmp.front();
/* If we are looking for a module or we are not
* looking at the last path component check for
* a name match (second line). */
if (scope->type() == NetScope::MODULE
&& (type == NetScope::MODULE || tmp.size() > 1)
&& scope->module_name()==key.peek_name()) {
/* Up references may match module name */
} else {
NetScope*found_scope = scope->child(key);
if (found_scope == 0) {
found_scope = scope->find_import(this, key.peek_name());
if (found_scope)
found_scope = found_scope->child(key);
}
scope = found_scope;
if (scope == 0) break;
}
tmp.pop_front();
} while (! tmp.empty());
return scope;
}
/*
* This is a relative lookup of a scope by name. The starting point is
* the scope parameter within which I start looking for the scope. If
* I do not find the scope within the passed scope, start looking in
* parent scopes until I find it, or I run out of parent scopes.
*/
NetScope* Design::find_scope(NetScope*scope, const std::list<hname_t>&path,
NetScope::TYPE type) const
{
assert(scope);
if (path.empty())
return scope;
// Record the compilation unit scope for use later.
NetScope*unit_scope = scope->unit();
// First search upwards through the hierarchy.
while (scope) {
NetScope*found_scope = find_scope_(scope, path, type);
if (found_scope)
return found_scope;
// Avoid searching the unit scope twice.
if (scope == unit_scope)
unit_scope = 0;
// Special case - see IEEE 1800-2012 section 23.8.1.
if (unit_scope && is_design_unit(scope) && is_subroutine(type)) {
found_scope = find_scope_(unit_scope, path, type);
if (found_scope)
return found_scope;
unit_scope = 0;
}
scope = scope->parent();
}
// If we haven't already done so, search the compilation unit scope.
if (unit_scope) {
NetScope*found_scope = find_scope_(unit_scope, path, type);
if (found_scope)
return found_scope;
}
// Last chance. Look for the name starting at the root.
return find_scope(path);
}
/*
* This method locates a scope within another scope, given its relative
* hierarchical name. Each component of the key is used to scan one
* more step down the tree until the name runs out or the search
* fails.
*/
NetScope* Design::find_scope_(NetScope*scope, const hname_t&path,
NetScope::TYPE type) const
{
/* If we are looking for a module or we are not
* looking at the last path component check for
* a name match (second line). */
if ((scope->type() == NetScope::MODULE) && (type == NetScope::MODULE)
&& (scope->module_name() == path.peek_name())) {
/* Up references may match module name */
return scope;
}
NetScope*found_scope = scope->child(path);
if (found_scope == 0) {
found_scope = scope->find_import(this, path.peek_name());
if (found_scope)
found_scope = found_scope->child(path);
}
return found_scope;
}
/*
* This is a relative lookup of a scope by name. The starting point is
* the scope parameter within which I start looking for the scope. If
* I do not find the scope within the passed scope, start looking in
* parent scopes until I find it, or I run out of parent scopes.
*/
NetScope* Design::find_scope(NetScope*scope, const hname_t&path,
NetScope::TYPE type) const
{
assert(scope);
// Record the compilation unit scope for use later.
NetScope*unit_scope = scope->unit();
// First search upwards through the hierarchy.
while (scope) {
NetScope*found_scope = find_scope_(scope, path, type);
if (found_scope)
return found_scope;
// Avoid searching the unit scope twice.
if (scope == unit_scope)
unit_scope = 0;
// Special case - see IEEE 1800-2012 section 23.8.1.
if (unit_scope && is_design_unit(scope) && is_subroutine(type)) {
found_scope = find_scope_(unit_scope, path, type);
if (found_scope)
return found_scope;
unit_scope = 0;
}
scope = scope->parent();
}
// If we haven't already done so, search the compilation unit scope.
if (unit_scope) {
NetScope*found_scope = find_scope_(unit_scope, path, type);
if (found_scope)
return found_scope;
}
// Last chance. Look for the name starting at the root.
list<hname_t>path_list;
path_list.push_back(path);
return find_scope(path_list);
}
/*
* This method runs through the scope, noticing the defparam
* statements that were collected during the elaborate_scope pass and
* applying them to the target parameters. The implementation actually
* works by using a specialized method from the NetScope class that
* does all the work for me.
*/
void Design::run_defparams()
{
for (list<NetScope*>::const_iterator scope = root_scopes_.begin();
scope != root_scopes_.end(); ++ scope )
(*scope)->run_defparams(this);
}
void NetScope::run_defparams(Design*des)
{
for (map<hname_t,NetScope*>::const_iterator cur = children_.begin()
; cur != children_.end() ; ++ cur )
cur->second->run_defparams(des);
while (! defparams.empty()) {
pair<pform_name_t,PExpr*> pp = defparams.front();
defparams.pop_front();
pform_name_t path = pp.first;
PExpr*val = pp.second;
perm_string perm_name = peek_tail_name(path);
path.pop_back();
list<hname_t> eval_path = eval_scope_path(des, this, path);
/* If there is no path on the name, then the targ_scope
is the current scope. */
NetScope*targ_scope = des->find_scope(this, eval_path);
if (targ_scope == 0) {
// Push the defparam onto a list for retry
// later. It is possible for the scope lookup to
// fail if the scope being defparam'd into is
// generated by an index array for generate.
eval_path.push_back(hname_t(perm_name));
defparams_later.push_back(make_pair(eval_path,val));
continue;
}
targ_scope->replace_parameter(des, perm_name, val, this, true);
}
// If some of the defparams didn't find a scope in the name,
// then try again later. It may be that the target scope is
// created later by generate scheme or instance array.
if (! defparams_later.empty())
des->defparams_later.insert(this);
}
void NetScope::run_defparams_later(Design*des)
{
set<NetScope*> target_scopes;
list<pair<list<hname_t>,PExpr*> > defparams_even_later;
while (! defparams_later.empty()) {
pair<list<hname_t>,PExpr*> cur = defparams_later.front();
defparams_later.pop_front();
list<hname_t>eval_path = cur.first;
perm_string name = eval_path.back().peek_name();
eval_path.pop_back();
PExpr*val = cur.second;
NetScope*targ_scope = des->find_scope(this, eval_path);
if (targ_scope == 0) {
// If a scope in the target path is not found,
// then push this defparam for handling even
// later. Maybe a later generate scheme or
// instance array will create the scope.
defparams_even_later.push_back(cur);
continue;
}
targ_scope->replace_parameter(des, name, val, this, true);
// We'll need to re-evaluate parameters in this scope
target_scopes.insert(targ_scope);
}
// The scopes that this defparam set touched will be
// re-evaluated later it a top_defparams work item. So do not
// do the evaluation now.
// If there are some scopes that still have missing scopes,
// then save them back into the defparams_later list for a
// later pass.
defparams_later = defparams_even_later;
if (! defparams_later.empty())
des->defparams_later.insert(this);
}
void Design::evaluate_parameters()
{
for (map<perm_string,NetScope*>::const_iterator cur = packages_.begin()
; cur != packages_.end() ; ++ cur) {
cur->second->evaluate_parameters(this);
}
for (list<NetScope*>::const_iterator scope = root_scopes_.begin()
; scope != root_scopes_.end() ; ++ scope ) {
(*scope)->evaluate_parameters(this);
}
}
void NetScope::evaluate_parameter_logic_(Design*des, param_ref_t cur)
{
/* Evaluate the parameter expression. */
PExpr*val_expr = (*cur).second.val_expr;
NetScope*val_scope = (*cur).second.val_scope;
// The param_type may be nil if the parameter has no declared type. In
// this case, we'll try to take our type from the r-value.
ivl_type_t param_type = cur->second.ivl_type;
// Most parameter declarations are packed vectors, of the form:
// parameter [H:L] foo == bar;
// so get the netvector_t. Note that this may also be the special
// case of a netvector_t with no dimensions, that exists only to carry
// signed-ness, e.g.:
// parameter signed foo = bar;
// These will be marked as scalar, but also have the implict flag set.
const netvector_t* param_vect = dynamic_cast<const netvector_t*> (param_type);
if (debug_elaborate) {
cerr << val_expr->get_fileline() << ": " << __func__ << ": "
<< "parameter=" << cur->first << endl;
if (param_type)
cerr << val_expr->get_fileline() << ": " << __func__ << ": "
<< "param_type=" << *param_type << endl;
else
cerr << val_expr->get_fileline() << ": " << __func__ << ": "
<< "param_type=(nil)" << endl;
cerr << val_expr->get_fileline() << ": " << __func__ << ": "
<< "val_expr=" << *val_expr << endl;
}
ivl_variable_type_t use_type;
int lv_width = -2;
if (param_type) {
use_type = param_type->base_type();
// Is this an implicit netvector_t with no dimenions?
if (param_vect && param_vect->get_implicit() &&
param_vect->get_scalar())
lv_width = -2;
else if (param_type->packed())
lv_width = param_type->packed_width();
} else {
use_type = val_expr->expr_type();
}
if (debug_elaborate) {
cerr << val_expr->get_fileline() << ": " << __func__ << ": "
<< "use_type = " << use_type << endl;
}
NetExpr *expr;
// Handle assignment patterns as a special case as they need the type to
// be evaluated correctly.
if (param_type && dynamic_cast<PEAssignPattern*>(val_expr)) {
expr = elab_and_eval(des, val_scope, val_expr, param_type, true);
} else {
expr = elab_and_eval(des, val_scope, val_expr, lv_width, true,
cur->second.is_annotatable, use_type);
}
if (! expr)
return;
// Make sure to carry the signed-ness from a vector type.
if (param_vect)
expr->cast_signed(param_vect->get_signed());
if (debug_elaborate) {
cerr << val_expr->get_fileline() << ": " << __func__ << ": "
<< "expr = " << *expr << endl;
cerr << val_expr->get_fileline() << ": " << __func__ << ": "
<< "expr type = " << expr->expr_type() << endl;
}
switch (expr->expr_type()) {
case IVL_VT_REAL:
if (! dynamic_cast<const NetECReal*>(expr)) {
cerr << expr->get_fileline()
<< ": error: Unable to evaluate real parameter "
<< (*cur).first << " value: " << *expr << endl;
des->errors += 1;
return;
}
// If the parameter has no type, then infer its type from the
// r-value expression.
if (param_type==0) {
param_type = &netreal_t::type_real;
cur->second.ivl_type = param_type;
}
break;
case IVL_VT_LOGIC:
case IVL_VT_BOOL:
if (! dynamic_cast<const NetEConst*>(expr)) {
cerr << expr->get_fileline()
<< ": error: Unable to evaluate parameter "
<< (*cur).first << " value: " << *expr << endl;
des->errors += 1;
return;
}
// If the parameter has no type, then infer its type from the
// r-value expression.
if (param_type==0) {
param_type = new netvector_t(expr->expr_type(), expr->expr_width()-1,
0, expr->has_sign());
cur->second.ivl_type = param_type;
}
if (param_type->base_type() != IVL_VT_NO_TYPE)
expr->cast_signed(param_type->get_signed());
if (!expr->has_width()) {
expr = pad_to_width(expr, integer_width, *expr);
} else if (param_type->slice_dimensions().size()==0 && !expr->has_width()) {
expr = pad_to_width(expr, integer_width, *expr);
}
break;
default:
cerr << expr->get_fileline()
<< ": internal error: "
<< "Unhandled expression type "
<< expr->expr_type() << "?" << endl;
cerr << expr->get_fileline()
<< ": : "
<< "param_type: " << *param_type << endl;
des->errors += 1;
return;
}
// By the time we're done with the above, we should certainly know the
// type of the parameter.
ivl_assert(*expr, cur->second.ivl_type);
cur->second.val = expr;
// If there are no value ranges to test the value against,
// then we are done.
if ((*cur).second.range == 0)
return;
NetEConst*val = dynamic_cast<NetEConst*>((*cur).second.val);
ivl_assert(*expr, val);
verinum value = val->value();
bool from_flag = (*cur).second.range == 0? true : false;
for (range_t*value_range = (*cur).second.range
; value_range ; value_range = value_range->next) {
// If we already know that the value is
// within a "from" range, then do not test
// any more of the from ranges.
if (from_flag && value_range->exclude_flag==false)
continue;
if (value_range->low_expr) {
NetEConst*tmp = dynamic_cast<NetEConst*>(value_range->low_expr);
ivl_assert(*value_range->low_expr, tmp);
if (value_range->low_open_flag && value <= tmp->value())
continue;
else if (value < tmp->value())
continue;
}
if (value_range->high_expr) {
NetEConst*tmp = dynamic_cast<NetEConst*>(value_range->high_expr);
ivl_assert(*value_range->high_expr, tmp);
if (value_range->high_open_flag && value >= tmp->value())
continue;
else if (value > tmp->value())
continue;
}
// Within the range. If this is a "from"
// range, then set the from_flag and continue.
if (value_range->exclude_flag == false) {
from_flag = true;
continue;
}
// OH NO! In an excluded range. signal an error.
from_flag = false;
break;
}
// If we found no from range that contains the
// value, then report an error.
if (! from_flag) {
cerr << val->get_fileline() << ": error: "
<< "Parameter value " << value
<< " is out of range for parameter " << (*cur).first
<< "." << endl;
des->errors += 1;
}
}
void NetScope::evaluate_parameter_real_(Design*des, param_ref_t cur)
{
PExpr*val_expr = (*cur).second.val_expr;
NetScope*val_scope = (*cur).second.val_scope;
ivl_type_t param_type = cur->second.ivl_type;
ivl_assert(*val_expr, param_type);
NetExpr*expr = elab_and_eval(des, val_scope, val_expr, -1, true,
cur->second.is_annotatable,
param_type->base_type());
if (! expr)
return;
NetECReal*res = 0;
switch (expr->expr_type()) {
case IVL_VT_REAL:
if (NetECReal*tmp = dynamic_cast<NetECReal*>(expr)) {
res = tmp;
} else {
cerr << expr->get_fileline()
<< ": error: "
<< "Unable to evaluate real parameter "
<< (*cur).first << " value: " << *expr << endl;
des->errors += 1;
return;
}
break;
default:
cerr << expr->get_fileline()
<< ": internal error: "
<< "Failed to cast expression?" << endl;
des->errors += 1;
return;
break;
}
(*cur).second.val = res;
double value = res->value().as_double();
bool from_flag = (*cur).second.range == 0? true : false;
for (range_t*value_range = (*cur).second.range
; value_range ; value_range = value_range->next) {
if (from_flag && value_range->exclude_flag==false)
continue;
if (value_range->low_expr) {
double tmp;
bool flag = eval_as_double(tmp, value_range->low_expr);
ivl_assert(*value_range->low_expr, flag);
if (value_range->low_open_flag && value <= tmp)
continue;
else if (value < tmp)
continue;
}
if (value_range->high_expr) {
double tmp;
bool flag = eval_as_double(tmp, value_range->high_expr);
ivl_assert(*value_range->high_expr, flag);
if (value_range->high_open_flag && value >= tmp)
continue;
else if (value > tmp)
continue;
}
if (value_range->exclude_flag == false) {
from_flag = true;
continue;
}
// All the above tests failed, so we must have tripped
// an exclude rule.
from_flag = false;
break;
}
if (! from_flag) {
cerr << res->get_fileline() << ": error: "
<< "Parameter value " << value
<< " is out of range for real parameter " << (*cur).first
<< "." << endl;
des->errors += 1;
}
}
/*
* Evaluate a parameter that is forced to type string. This comes to pass when
* the input is something like this:
*
* parameter string foo = <expr>;
*
* The param_type should be netstring_t, the val_expr is the pform of the
* input <expr>, and we try to elaborate/evaluate down to a IVL_VT_STRING
* expression.
*/
void NetScope::evaluate_parameter_string_(Design*des, param_ref_t cur)
{
PExpr*val_expr = (*cur).second.val_expr;
NetScope*val_scope = (*cur).second.val_scope;
ivl_type_t param_type = cur->second.ivl_type;
ivl_assert(cur->second, val_expr);
ivl_assert(cur->second, param_type);
NetExpr*expr = elab_and_eval(des, val_scope, val_expr, param_type, true);
if (! expr)
return;
cur->second.val = expr;
if (debug_elaborate) {
cerr << cur->second.get_fileline() << ": " << __func__ << ": "
<< "Parameter type: " << *param_type << endl;
cerr << cur->second.get_fileline() << ": " << __func__ << ": "
<< "Parameter value: " << *val_expr << endl;
cerr << cur->second.get_fileline() << ": " << __func__ << ": "
<< "Elaborated value: " << *expr << endl;
}
}
void NetScope::evaluate_type_parameter_(Design *des, param_ref_t cur)
{
const PETypename *type_expr = dynamic_cast<const PETypename*>(cur->second.val_expr);
if (!type_expr) {
cerr << this->get_fileline() << ": error: "
<< "Type parameter `" << cur->first << "` value `"
<< *cur->second.val_expr << "` is not a type."
<< endl;
des->errors++;
// Recover
cur->second.ivl_type = netvector_t::integer_type();
return;
}
data_type_t *ptype = type_expr->get_type();
NetScope *type_scope = cur->second.val_scope;
cur->second.ivl_type = ptype->elaborate_type(des, type_scope);
}
void NetScope::evaluate_parameter_(Design*des, param_ref_t cur)
{
// If the parameter has already been evaluated, quietly return.
if (cur->second.val || cur->second.ivl_type)
return;
if (cur->second.val_expr == 0) {
cerr << this->get_fileline() << ": error: "
<< "Missing value for parameter `"
<< cur->first << "`." << endl;
des->errors += 1;
cur->second.val = new NetEConst(verinum(verinum::Vx));
return;
}
if (cur->second.type_flag) {
evaluate_type_parameter_(des, cur);
return;
}
ivl_type_t param_type = cur->second.ivl_type;
// If the parameter type is present, then elaborate it now. Elaborate
// the type in the current scope, and not the scope of the expression.
if (cur->second.val_type) {
param_type = cur->second.val_type->elaborate_type(des, this);
cur->second.ivl_type = param_type;
cur->second.val_type = 0;
}
// Guess the varaiable type of the parameter. If the parameter has no
// given type, then guess the type from the expression and use that to
// evaluate (this is currently handled in evaluate_parameter_logic_()).
ivl_variable_type_t use_type;
if (param_type)
use_type = param_type->base_type();
else
use_type = IVL_VT_NO_TYPE;
if (cur->second.solving) {
cerr << cur->second.get_fileline() << ": error: "
<< "Recursive parameter reference found involving "
<< cur->first << "." << endl;
des->errors += 1;
} else {
cur->second.solving = true;
switch (use_type) {
case IVL_VT_NO_TYPE:
case IVL_VT_BOOL:
case IVL_VT_LOGIC:
evaluate_parameter_logic_(des, cur);
break;
case IVL_VT_REAL:
evaluate_parameter_real_(des, cur);
break;
case IVL_VT_STRING:
evaluate_parameter_string_(des, cur);
break;
default:
cerr << cur->second.get_fileline() << ": internal error: "
<< "Unexpected parameter type " << use_type
<< "." << endl;
cerr << cur->second.get_fileline() << ": : "
<< "Parameter name: " << cur->first << endl;
if (param_type)
cerr << cur->second.get_fileline() << ": : "
<< "Parameter ivl_type: " << *param_type << endl;
cerr << cur->second.get_fileline() << ": : "
<< "Expression is: " << *cur->second.val_expr << endl;
ivl_assert(cur->second, 0);
break;
}
cur->second.solving = false;
}
// If we have failed to evaluate the expression, create a dummy
// value. This prevents spurious error messages being output.
if (cur->second.val == 0) {
verinum val(verinum::Vx);
cur->second.val = new NetEConst(val);
}
// Flag that the expression has been evaluated.
cur->second.val_expr = 0;
}
void NetScope::evaluate_parameters(Design*des)
{
for (map<hname_t,NetScope*>::const_iterator cur = children_.begin()
; cur != children_.end() ; ++ cur )
cur->second->evaluate_parameters(des);
if (debug_scopes)
cerr << "debug: "
<< "Evaluating parameters in " << scope_path(this) << endl;
for (param_ref_t cur = parameters.begin()
; cur != parameters.end() ; ++ cur) {
evaluate_parameter_(des, cur);
}
}
void Design::residual_defparams()
{
for (list<NetScope*>::const_iterator scope = root_scopes_.begin();
scope != root_scopes_.end(); ++ scope )
(*scope)->residual_defparams(this);
}
void NetScope::residual_defparams(Design*des)
{
// Clean out the list of defparams that never managed to match
// a scope. Print a warning for each.
while (! defparams_later.empty()) {
pair<list<hname_t>,PExpr*> cur = defparams_later.front();
defparams_later.pop_front();
cerr << cur.second->get_fileline() << ": warning: "
<< "Scope of " << cur.first << " not found." << endl;
}
for (map<hname_t,NetScope*>::const_iterator cur = children_.begin()
; cur != children_.end() ; ++ cur )
cur->second->residual_defparams(des);
}
const char* Design::get_flag(const string&key) const
{
map<string,const char*>::const_iterator tmp = flags_.find(key);
if (tmp == flags_.end())
return "";
else
return (*tmp).second;
}
/*
* This method looks for a signal (reg, wire, whatever) starting at
* the specified scope. If the name is hierarchical, it is split into
* scope and name and the scope used to find the proper starting point
* for the real search.
*
* It is the job of this function to properly implement Verilog scope
* rules as signals are concerned.
*/
NetNet* Design::find_signal(NetScope*scope, pform_name_t path)
{
assert(scope);
perm_string key = peek_tail_name(path);