forked from steveicarus/iverilog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpform.cc
3494 lines (2931 loc) · 101 KB
/
pform.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) 1998-2024 Stephen Williams ([email protected])
* Copyright CERN 2013 / 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 <cstdarg>
# include "compiler.h"
# include "pform.h"
# include "parse_misc.h"
# include "parse_api.h"
# include "PClass.h"
# include "PEvent.h"
# include "PPackage.h"
# include "PUdp.h"
# include "PGenerate.h"
# include "PModport.h"
# include "PSpec.h"
# include "PTimingCheck.h"
# include "discipline.h"
# include <list>
# include <map>
# include <cassert>
# include <stack>
# include <typeinfo>
# include <sstream>
# include <cstring>
# include <cstdlib>
# include <cctype>
# include "ivl_assert.h"
# include "ivl_alloc.h"
using namespace std;
/*
* The "// synthesis translate_on/off" meta-comments cause this flag
* to be turned off or on. The pform_make_behavior and similar
* functions look at this flag and may choose to add implicit ivl
* synthesis flags.
*/
static bool pform_mc_translate_flag = true;
void pform_mc_translate_on(bool flag) { pform_mc_translate_flag = flag; }
/*
* The pform_modules is a map of the modules that have been defined in
* the top level. This should not contain nested modules/programs.
* pform_primitives is similar, but for UDP primitives.
*/
map<perm_string,Module*> pform_modules;
map<perm_string,PUdp*> pform_primitives;
/*
* The pform_units is a list of the SystemVerilog compilation unit scopes.
* The current compilation unit is the last element in the list. All items
* declared or defined at the top level (outside any design element) are
* added to the current compilation unit scope.
*/
vector<PPackage*> pform_units;
static bool is_compilation_unit(LexicalScope*scope)
{
// A compilation unit is the only scope that doesn't have a parent.
assert(scope);
return scope->parent_scope() == 0;
}
std::string vlltype::get_fileline() const
{
ostringstream buf;
buf << (text? text : "") << ":" << first_line;
string res = buf.str();
return res;
}
static bool is_hex_digit_str(const char *str)
{
while (*str) {
if (!isxdigit(*str)) return false;
str++;
}
return true;
}
static bool is_dec_digit_str(const char *str)
{
while (*str) {
if (!isdigit(*str)) return false;
str++;
}
return true;
}
static bool is_oct_digit_str(const char *str)
{
while (*str) {
if (*str < '0' || *str > '7') return false;
str++;
}
return true;
}
static bool is_bin_digit_str(const char *str)
{
while (*str) {
if (*str != '0' && *str != '1') return false;
str++;
}
return true;
}
/*
* Parse configuration file with format <key>=<value>, where key
* is the hierarchical name of a valid parameter name, and value
* is the value user wants to assign to. The value should be constant.
*/
void parm_to_defparam_list(const string¶m)
{
char* key;
char* value;
unsigned off = param.find('=');
if (off > param.size()) {
key = strdup(param.c_str());
value = (char*)malloc(1);
*value = '\0';
} else {
key = strdup(param.substr(0, off).c_str());
value = strdup(param.substr(off+1).c_str());
}
// Resolve hierarchical name for defparam. Remember
// to deal with bit select for generate scopes. Bit
// select expression should be constant integer.
pform_name_t name;
char *nkey = key;
char *ptr = strchr(key, '.');
while (ptr != 0) {
*ptr++ = '\0';
// Find if bit select is applied, this would be something
// like - scope[2].param = 10
char *bit_l = strchr(nkey, '[');
if (bit_l !=0) {
*bit_l++ = '\0';
char *bit_r = strchr(bit_l, ']');
if (bit_r == 0) {
cerr << "<command line>: error: missing ']' for defparam: " << nkey << endl;
free(key);
free(value);
return;
}
*bit_r = '\0';
int i = 0;
while (*(bit_l+i) != '\0')
if (!isdigit(*(bit_l+i++))) {
cerr << "<command line>: error: scope index expression is not constant: " << nkey << endl;
free(key);
free(value);
return;
}
name_component_t tmp(lex_strings.make(nkey));
index_component_t index;
index.sel = index_component_t::SEL_BIT;
verinum *seln = new verinum(atoi(bit_l));
PENumber *sel = new PENumber(seln);
index.msb = sel;
index.lsb = sel;
tmp.index.push_back(index);
name.push_back(tmp);
}
else // no bit select
name.push_back(name_component_t(lex_strings.make(nkey)));
nkey = ptr;
ptr = strchr(nkey, '.');
}
name.push_back(name_component_t(lex_strings.make(nkey)));
free(key);
// Resolve value to PExpr class. Should support all kind of constant
// format including based number, dec number, real number and string.
// Is it a string?
if (*value == '"') {
char *buf = strdup (value);
char *buf_ptr = buf+1;
// Parse until another '"' or '\0'
while (*buf_ptr != '"' && *buf_ptr != '\0') {
buf_ptr++;
// Check for escape, especially '\"', which does not mean the
// end of string.
if (*buf_ptr == '\\' && *(buf_ptr+1) != '\0')
buf_ptr += 2;
}
if (*buf_ptr == '\0') // String end without '"'
cerr << "<command line>: error: missing close quote of string for defparam: " << name << endl;
else if (*(buf_ptr+1) != 0) { // '"' appears within string with no escape
cerr << buf_ptr << endl;
cerr << "<command line>: error: \'\"\' appears within string value for defparam: " << name
<< ". Ignore characters after \'\"\'" << endl;
}
*buf_ptr = '\0';
buf_ptr = buf+1;
// Remember to use 'new' to allocate string for PEString
// because 'delete' is used by its destructor.
char *nchar = strcpy(new char [strlen(buf_ptr)+1], buf_ptr);
PExpr* ndec = new PEString(nchar);
Module::user_defparms.push_back( make_pair(name, ndec) );
free(buf);
free(value);
return;
}
// Is it a based number?
char *num = strchr(value, '\'');
if (num != 0) {
verinum *val;
const char *base = num + 1;
if (*base == 's' || *base == 'S')
base++;
switch (*base) {
case 'h':
case 'H':
if (is_hex_digit_str(base+1)) {
val = make_unsized_hex(num);
} else {
cerr << "<command line>: error: invalid digit in hex value specified for defparam: " << name << endl;
free(value);
return;
}
break;
case 'd':
case 'D':
if (is_dec_digit_str(base+1)) {
val = make_unsized_dec(num);
} else {
cerr << "<command line>: error: invalid digit in decimal value specified for defparam: " << name << endl;
free(value);
return;
}
break;
case 'o':
case 'O':
if (is_oct_digit_str(base+1)) {
val = make_unsized_octal(num);
} else {
cerr << "<command line>: error: invalid digit in octal value specified for defparam: " << name << endl;
free(value);
return;
}
break;
case 'b':
case 'B':
if (is_bin_digit_str(base+1)) {
val = make_unsized_binary(num);
} else {
cerr << "<command line>: error: invalid digit in binary value specified for defparam: " << name << endl;
free(value);
return;
}
break;
default:
cerr << "<command line>: error: invalid numeric base specified for defparam: " << name << endl;
free(value);
return;
}
if (num != value) { // based number with size
*num = 0;
if (is_dec_digit_str(value)) {
verinum *siz = make_unsized_dec(value);
val = pform_verinum_with_size(siz, val, "<command line>", 0);
} else {
cerr << "<command line>: error: invalid size for value specified for defparam: " << name << endl;
free(value);
return;
}
}
PExpr* ndec = new PENumber(val);
Module::user_defparms.push_back( make_pair(name, ndec) );
free(value);
return;
}
// Is it a decimal number?
num = (value[0] == '-') ? value + 1 : value;
if (num[0] != '\0' && is_dec_digit_str(num)) {
verinum *val = make_unsized_dec(num);
if (value[0] == '-') *val = -(*val);
PExpr* ndec = new PENumber(val);
Module::user_defparms.push_back( make_pair(name, ndec) );
free(value);
return;
}
// Is it a real number?
char *end = 0;
double rval = strtod(value, &end);
if (end != value && *end == 0) {
verireal *val = new verireal(rval);
PExpr* nreal = new PEFNumber(val);
Module::user_defparms.push_back( make_pair(name, nreal) );
free(value);
return;
}
// None of the above.
cerr << "<command line>: error: invalid value specified for defparam: " << name << endl;
free(value);
}
/*
* The lexor accesses the vl_* variables.
*/
string vl_file = "";
extern int VLparse();
/* This tracks the current module being processed. There can only be
exactly one module currently being parsed, since Verilog does not
allow nested module definitions. */
static list<Module*>pform_cur_module;
bool pform_library_flag = false;
/*
* Give each unnamed block that has a variable declaration a unique name.
*/
static unsigned scope_unnamed_block_with_decl = 1;
/* This tracks the current generate scheme being processed. This is
always within a module. */
static PGenerate*pform_cur_generate = 0;
/* This indicates whether a new generate construct can be directly
nested in the current generate construct. */
bool pform_generate_single_item = false;
/* Blocks within the same conditional generate construct may have
the same name. Here we collect the set of names used in each
construct, so they can be added to the local scope without
conflicting with each other. Generate constructs may nest, so
we need a stack. */
static list<set<perm_string> > conditional_block_names;
/* This tracks the current modport list being processed. This is
always within an interface. */
static PModport*pform_cur_modport = 0;
static NetNet::Type pform_default_nettype = NetNet::WIRE;
/*
* These variables track the time scale set by the most recent `timescale
* directive. Time scales set by SystemVerilog timeunit and timeprecision
* declarations are stored directly in the current lexical scope.
*/
static int pform_time_unit;
static int pform_time_prec;
/*
* These variables track where the most recent `timescale directive
* occurred. This allows us to warn about time scales that are inherited
* from another file.
*/
static char*pform_timescale_file = 0;
static unsigned pform_timescale_line;
/*
* These variables track whether we can accept new timeunits declarations.
*/
bool allow_timeunit_decl = true;
bool allow_timeprec_decl = true;
// Track whether the current parameter declaration is in a parameter port list
static bool pform_in_parameter_port_list = false;
/*
* The lexical_scope keeps track of the current lexical scope that is
* being parsed. The lexical scope may stack, so the current scope may
* have a parent, that is restored when the current scope ends.
*
* Items that have scoped names are put in the lexical_scope object.
*/
static LexicalScope* lexical_scope = 0;
LexicalScope* pform_peek_scope(void)
{
assert(lexical_scope);
return lexical_scope;
}
static void pform_check_possible_imports(LexicalScope *scope)
{
map<perm_string,PPackage*>::const_iterator cur;
for (cur = scope->possible_imports.begin(); cur != scope->possible_imports.end(); ++cur) {
if (scope->local_symbols.find(cur->first) == scope->local_symbols.end())
scope->explicit_imports[cur->first] = cur->second;
}
scope->possible_imports.clear();
}
void pform_pop_scope()
{
LexicalScope*scope = lexical_scope;
assert(scope);
pform_check_possible_imports(scope);
lexical_scope = scope->parent_scope();
assert(lexical_scope);
}
static LexicalScope::lifetime_t find_lifetime(LexicalScope::lifetime_t lifetime)
{
if (lifetime != LexicalScope::INHERITED)
return lifetime;
return lexical_scope->default_lifetime;
}
static PScopeExtra* find_nearest_scopex(LexicalScope*scope)
{
PScopeExtra*scopex = dynamic_cast<PScopeExtra*> (scope);
while (scope && !scopex) {
scope = scope->parent_scope();
scopex = dynamic_cast<PScopeExtra*> (scope);
}
return scopex;
}
static void add_local_symbol(LexicalScope*scope, perm_string name, PNamedItem*item)
{
assert(scope);
// Check for conflict with another local symbol.
map<perm_string,PNamedItem*>::const_iterator cur_sym
= scope->local_symbols.find(name);
if (cur_sym != scope->local_symbols.end()) {
cerr << item->get_fileline() << ": error: "
"'" << name << "' has already been declared "
"in this scope." << endl;
cerr << cur_sym->second->get_fileline() << ": : "
"It was declared here as "
<< cur_sym->second->symbol_type() << "." << endl;
error_count += 1;
return;
}
// Check for conflict with an explicit import.
map<perm_string,PPackage*>::const_iterator cur_pkg
= scope->explicit_imports.find(name);
if (cur_pkg != scope->explicit_imports.end()) {
cerr << item->get_fileline() << ": error: "
"'" << name << "' has already been "
"imported into this scope from package '"
<< cur_pkg->second->pscope_name() << "'." << endl;
error_count += 1;
return;
}
scope->local_symbols[name] = item;
}
static void check_potential_imports(const struct vlltype&loc, perm_string name, bool tf_call)
{
LexicalScope*scope = lexical_scope;
while (scope) {
if (scope->local_symbols.find(name) != scope->local_symbols.end())
return;
if (scope->explicit_imports.find(name) != scope->explicit_imports.end())
return;
if (pform_find_potential_import(loc, scope, name, tf_call, true))
return;
scope = scope->parent_scope();
}
}
/*
* Set the local time unit/precision. This version is used for setting
* the time scale for design elements (modules, packages, etc.) and is
* called after any initial timeunit and timeprecision declarations
* have been parsed.
*/
void pform_set_scope_timescale(const struct vlltype&loc)
{
PScopeExtra*scope = dynamic_cast<PScopeExtra*>(lexical_scope);
ivl_assert(loc, scope);
PScopeExtra*parent = find_nearest_scopex(scope->parent_scope());
bool used_global_timescale = false;
if (scope->time_unit_is_default) {
if (is_compilation_unit(scope)) {
scope->time_unit = def_ts_units;
} else if (!is_compilation_unit(parent)) {
scope->time_unit = parent->time_unit;
scope->time_unit_is_default = parent->time_unit_is_default;
} else if (pform_timescale_file != 0) {
scope->time_unit = pform_time_unit;
scope->time_unit_is_default = false;
used_global_timescale = true;
} else /* parent is compilation unit */ {
scope->time_unit = parent->time_unit;
scope->time_unit_is_default = parent->time_unit_is_default;
}
}
if (scope->time_prec_is_default) {
if (is_compilation_unit(scope)) {
scope->time_precision = def_ts_prec;
} else if (!is_compilation_unit(parent)) {
scope->time_precision = parent->time_precision;
scope->time_prec_is_default = parent->time_prec_is_default;
} else if (pform_timescale_file != 0) {
scope->time_precision = pform_time_prec;
scope->time_prec_is_default = false;
used_global_timescale = true;
} else {
scope->time_precision = parent->time_precision;
scope->time_prec_is_default = parent->time_prec_is_default;
}
}
if (gn_system_verilog() && (scope->time_unit < scope->time_precision)) {
if (scope->time_unit_is_local || scope->time_prec_is_local) {
VLerror("error: A timeprecision is missing or is too large!");
}
} else {
ivl_assert(loc, scope->time_unit >= scope->time_precision);
}
if (warn_timescale && used_global_timescale
&& (strcmp(pform_timescale_file, loc.text) != 0)) {
cerr << loc.get_fileline() << ": warning: "
<< "timescale for " << scope->pscope_name()
<< " inherited from another file." << endl;
cerr << pform_timescale_file << ":" << pform_timescale_line
<< ": ...: The inherited timescale is here." << endl;
}
allow_timeunit_decl = false;
allow_timeprec_decl = false;
}
/*
* Set the local time unit/precision. This version is used for setting
* the time scale for subsidiary items (classes, subroutines, etc.),
* which simply inherit their time scale from their parent scope.
*/
static void pform_set_scope_timescale(PScope*scope, const PScope*parent)
{
scope->time_unit = parent->time_unit;
scope->time_precision = parent->time_precision;
scope->time_unit_is_default = parent->time_unit_is_default;
scope->time_prec_is_default = parent->time_prec_is_default;
}
PClass* pform_push_class_scope(const struct vlltype&loc, perm_string name)
{
PClass*class_scope = new PClass(name, lexical_scope);
class_scope->default_lifetime = LexicalScope::AUTOMATIC;
FILE_NAME(class_scope, loc);
PScopeExtra*scopex = find_nearest_scopex(lexical_scope);
ivl_assert(loc, scopex);
ivl_assert(loc, !pform_cur_generate);
pform_set_scope_timescale(class_scope, scopex);
scopex->classes[name] = class_scope;
scopex->classes_lexical .push_back(class_scope);
lexical_scope = class_scope;
return class_scope;
}
PPackage* pform_push_package_scope(const struct vlltype&loc, perm_string name,
LexicalScope::lifetime_t lifetime)
{
PPackage*pkg_scope = new PPackage(name, lexical_scope);
pkg_scope->default_lifetime = find_lifetime(lifetime);
FILE_NAME(pkg_scope, loc);
allow_timeunit_decl = true;
allow_timeprec_decl = true;
lexical_scope = pkg_scope;
return pkg_scope;
}
PTask* pform_push_task_scope(const struct vlltype&loc, char*name,
LexicalScope::lifetime_t lifetime)
{
perm_string task_name = lex_strings.make(name);
LexicalScope::lifetime_t default_lifetime = find_lifetime(lifetime);
bool is_auto = default_lifetime == LexicalScope::AUTOMATIC;
PTask*task = new PTask(task_name, lexical_scope, is_auto);
task->default_lifetime = default_lifetime;
FILE_NAME(task, loc);
PScopeExtra*scopex = find_nearest_scopex(lexical_scope);
ivl_assert(loc, scopex);
if (is_compilation_unit(scopex) && !gn_system_verilog()) {
cerr << task->get_fileline() << ": error: task declarations "
"must be contained within a module." << endl;
error_count += 1;
}
pform_set_scope_timescale(task, scopex);
if (pform_cur_generate) {
add_local_symbol(pform_cur_generate, task_name, task);
pform_cur_generate->tasks[task_name] = task;
} else {
add_local_symbol(scopex, task_name, task);
scopex->tasks[task_name] = task;
}
lexical_scope = task;
return task;
}
PFunction* pform_push_function_scope(const struct vlltype&loc, const char*name,
LexicalScope::lifetime_t lifetime)
{
perm_string func_name = lex_strings.make(name);
LexicalScope::lifetime_t default_lifetime = find_lifetime(lifetime);
bool is_auto = default_lifetime == LexicalScope::AUTOMATIC;
PFunction*func = new PFunction(func_name, lexical_scope, is_auto);
func->default_lifetime = default_lifetime;
FILE_NAME(func, loc);
PScopeExtra*scopex = find_nearest_scopex(lexical_scope);
ivl_assert(loc, scopex);
if (is_compilation_unit(scopex) && !gn_system_verilog()) {
cerr << func->get_fileline() << ": error: function declarations "
"must be contained within a module." << endl;
error_count += 1;
}
pform_set_scope_timescale(func, scopex);
if (pform_cur_generate) {
add_local_symbol(pform_cur_generate, func_name, func);
pform_cur_generate->funcs[func_name] = func;
} else {
add_local_symbol(scopex, func_name, func);
scopex->funcs[func_name] = func;
}
lexical_scope = func;
return func;
}
PBlock* pform_push_block_scope(const struct vlltype&loc, char*name,
PBlock::BL_TYPE bt)
{
perm_string block_name;
if (name) block_name = lex_strings.make(name);
else {
// Create a unique name for this unnamed block.
char tmp[32];
snprintf(tmp, sizeof tmp, "$unm_blk_%u",
scope_unnamed_block_with_decl);
block_name = lex_strings.make(tmp);
scope_unnamed_block_with_decl += 1;
}
PBlock*block = new PBlock(block_name, lexical_scope, bt);
FILE_NAME(block, loc);
block->default_lifetime = find_lifetime(LexicalScope::INHERITED);
if (name) add_local_symbol(lexical_scope, block_name, block);
lexical_scope = block;
return block;
}
/*
* Create a new identifier.
*/
PEIdent* pform_new_ident(const struct vlltype&loc, const pform_name_t&name)
{
if (gn_system_verilog())
check_potential_imports(loc, name.front().name, false);
return new PEIdent(name, loc.lexical_pos);
}
PTrigger* pform_new_trigger(const struct vlltype&loc, PPackage*pkg,
const pform_name_t&name, unsigned lexical_pos)
{
if (gn_system_verilog())
check_potential_imports(loc, name.front().name, false);
PTrigger*tmp = new PTrigger(pkg, name, lexical_pos);
FILE_NAME(tmp, loc);
return tmp;
}
PNBTrigger* pform_new_nb_trigger(const struct vlltype&loc,
const list<PExpr*>*dly,
const pform_name_t&name,
unsigned lexical_pos)
{
if (gn_system_verilog())
check_potential_imports(loc, name.front().name, false);
PExpr*tmp_dly = 0;
if (dly) {
ivl_assert(loc, dly->size() == 1);
tmp_dly = dly->front();
}
PNBTrigger*tmp = new PNBTrigger(name, lexical_pos, tmp_dly);
FILE_NAME(tmp, loc);
return tmp;
}
PGenerate* pform_parent_generate(void)
{
return pform_cur_generate;
}
bool pform_error_in_generate(const vlltype&loc, const char *type)
{
if (!pform_parent_generate())
return false;
VLerror(loc, "error: %s is not allowed in generate block.", type);
return true;
}
void pform_bind_attributes(map<perm_string,PExpr*>&attributes,
list<named_pexpr_t>*attr, bool keep_attrs)
{
if (attr == 0)
return;
while (! attr->empty()) {
named_pexpr_t tmp = attr->front();
attr->pop_front();
attributes[tmp.name] = tmp.parm;
}
if (!keep_attrs)
delete attr;
}
bool pform_in_program_block()
{
if (pform_cur_module.empty())
return false;
if (pform_cur_module.front()->program_block)
return true;
return false;
}
bool pform_in_interface()
{
if (pform_cur_module.empty())
return false;
if (pform_cur_module.front()->is_interface)
return true;
return false;
}
static bool pform_at_module_level()
{
return (lexical_scope == pform_cur_module.front())
|| (lexical_scope == pform_cur_generate);
}
PWire*pform_get_wire_in_scope(perm_string name)
{
return lexical_scope->wires_find(name);
}
static void pform_put_wire_in_scope(perm_string name, PWire*net)
{
add_local_symbol(lexical_scope, name, net);
lexical_scope->wires[name] = net;
}
void pform_put_enum_type_in_scope(enum_type_t*enum_set)
{
if (std::find(lexical_scope->enum_sets.begin(),
lexical_scope->enum_sets.end(), enum_set) !=
lexical_scope->enum_sets.end())
return;
set<perm_string> enum_names;
list<named_pexpr_t>::const_iterator cur;
for (cur = enum_set->names->begin(); cur != enum_set->names->end(); ++cur) {
if (enum_names.count(cur->name)) {
cerr << enum_set->get_fileline() << ": error: "
"Duplicate enumeration name '"
<< cur->name << "'." << endl;
error_count += 1;
} else {
add_local_symbol(lexical_scope, cur->name, enum_set);
enum_names.insert(cur->name);
}
}
lexical_scope->enum_sets.push_back(enum_set);
}
static typedef_t *pform_get_typedef(const struct vlltype&loc, perm_string name)
{
typedef_t *&td = lexical_scope->typedefs[name];
if (!td) {
td = new typedef_t(name);
FILE_NAME(td, loc);
add_local_symbol(lexical_scope, name, td);
}
return td;
}
void pform_forward_typedef(const struct vlltype&loc, perm_string name,
enum typedef_t::basic_type basic_type)
{
typedef_t *td = pform_get_typedef(loc, name);
if (!td->set_basic_type(basic_type)) {
cout << loc << " error: Incompatible basic type `" << basic_type
<< "` for `" << name
<< "`. Previously declared in this scope as `"
<< td->get_basic_type() << "` at " << td->get_fileline() << "."
<< endl;
error_count++;
}
}
void pform_set_typedef(const struct vlltype&loc, perm_string name,
data_type_t*data_type,
std::list<pform_range_t>*unp_ranges)
{
typedef_t *td = pform_get_typedef(loc, name);
if(unp_ranges)
data_type = new uarray_type_t(data_type, unp_ranges);
if (!td->set_data_type(data_type)) {
cerr << loc << " error: Type identifier `" << name
<< "` has already been declared in this scope at "
<< td->get_data_type()->get_fileline() << "."
<< endl;
error_count++;
delete data_type;
}
}
void pform_set_type_referenced(const struct vlltype&loc, const char*name)
{
perm_string lex_name = lex_strings.make(name);
check_potential_imports(loc, lex_name, false);
}
typedef_t* pform_test_type_identifier(const struct vlltype&loc, const char*txt)
{
perm_string name = lex_strings.make(txt);
LexicalScope*cur_scope = lexical_scope;
do {
LexicalScope::typedef_map_t::iterator cur;
// First look to see if this identifier is imported from
// a package. If it is, see if it is a type in that
// package. If it is, then great. If imported as
// something other than a type, then give up now because
// the name has at least shadowed any other possible
// meaning for this name.
map<perm_string,PPackage*>::iterator cur_pkg;
cur_pkg = cur_scope->explicit_imports.find(name);
if (cur_pkg != cur_scope->explicit_imports.end()) {
PPackage*pkg = cur_pkg->second;
cur = pkg->typedefs.find(name);
if (cur != pkg->typedefs.end())
return cur->second;
// Not a type. Give up.
return 0;
}
cur = cur_scope->typedefs.find(name);
if (cur != cur_scope->typedefs.end())
return cur->second;
PPackage*pkg = pform_find_potential_import(loc, cur_scope, name, false, false);
if (pkg) {
cur = pkg->typedefs.find(name);
if (cur != pkg->typedefs.end())
return cur->second;
// Not a type. Give up.
return 0;
}
cur_scope = cur_scope->parent_scope();
} while (cur_scope);
return 0;
}
PECallFunction* pform_make_call_function(const struct vlltype&loc,
const pform_name_t&name,
const list<named_pexpr_t> &parms)
{
if (gn_system_verilog())
check_potential_imports(loc, name.front().name, true);
PECallFunction*tmp = new PECallFunction(name, parms);
FILE_NAME(tmp, loc);
return tmp;
}
PCallTask* pform_make_call_task(const struct vlltype&loc,
const pform_name_t&name,
const list<named_pexpr_t> &parms)
{
if (gn_system_verilog())
check_potential_imports(loc, name.front().name, true);
PCallTask*tmp = new PCallTask(name, parms);
FILE_NAME(tmp, loc);
return tmp;
}
void pform_make_var(const struct vlltype&loc,
std::list<decl_assignment_t*>*assign_list,
data_type_t*data_type, std::list<named_pexpr_t>*attr,
bool is_const)
{
static const struct str_pair_t str = { IVL_DR_STRONG, IVL_DR_STRONG };
pform_makewire(loc, 0, str, assign_list, NetNet::REG, data_type, attr,
is_const);
}
void pform_make_foreach_declarations(const struct vlltype&loc,
std::list<perm_string>*loop_vars)
{
list<decl_assignment_t*>assign_list;
for (list<perm_string>::const_iterator cur = loop_vars->begin()
; cur != loop_vars->end() ; ++ cur) {
if (cur->nil())
continue;
decl_assignment_t*tmp_assign = new decl_assignment_t;
tmp_assign->name = { lex_strings.make(*cur), 0 };
assign_list.push_back(tmp_assign);
}
pform_make_var(loc, &assign_list, &size_type);
}
PForeach* pform_make_foreach(const struct vlltype&loc,
char*name,
list<perm_string>*loop_vars,
Statement*stmt)
{
perm_string use_name = lex_strings.make(name);
delete[]name;
if (loop_vars==0 || loop_vars->empty()) {
cerr << loc.get_fileline() << ": error: "
<< "No loop variables at all in foreach index." << endl;
error_count += 1;
}
ivl_assert(loc, loop_vars);
PForeach*fe = new PForeach(use_name, *loop_vars, stmt);
FILE_NAME(fe, loc);
delete loop_vars;
return fe;