/
pform.cc
3562 lines (3025 loc) · 105 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-2016 Stephen Williams (steve@icarus.com)
* Copyright CERN 2013 / Stephen Williams (steve@icarus.com)
*
* 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 "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 "discipline.h"
# include <list>
# include <map>
# include <cassert>
# include <stack>
# include <typeinfo>
# include <sstream>
# include <cstring>
# include <cstdlib>
# include "ivl_assert.h"
# include "ivl_alloc.h"
/*
* 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;
/*
* typedefs in the $root scope go here.
*/
map<perm_string,data_type_t*>pform_typedefs;
set<enum_type_t*>pform_enum_sets;
/*
* Class definitions in the $root scope go here.
*/
map<perm_string,PClass*> pform_classes;
/*
* Task and function definitions in the $root scope go here.
*/
map<perm_string,PTaskFunc*> pform_tasks;
std::string vlltype::get_fileline() const
{
ostringstream buf;
buf << (text? text : "") << ":" << first_line;
string res = buf.str();
return res;
}
/*
* 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)));
// Resolve value to PExpr class. Should support all kind of constant
// format including based number, dec number, real number and string.
if (*value == '"') { // string type
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);
}
else { // number type
char *num = strchr(value, '\'');
if (num != 0) {
verinum *val;
// BASED_NUMBER, something like - scope.parameter='b11
// make sure to check 'h' first because 'b'&'d' may be included
// in hex format
if (strchr(num, 'h') || strchr(num, 'H'))
val = make_unsized_hex(num);
else if (strchr(num, 'd') || strchr(num, 'D'))
if (strchr(num, 'x') || strchr(num, 'X') || strchr(num, 'z') || strchr(num, 'Z'))
val = make_undef_highz_dec(num);
else
val = make_unsized_dec(num);
else if (strchr(num, 'b') || strchr(num, 'B')) {
val = make_unsized_binary(num);
}
else if (strchr(num, 'o') || strchr(num, 'O'))
val = make_unsized_octal(num);
else {
cerr << "<command line>: error: value specify error for defparam: " << name << endl;
free(key);
free(value);
return;
}
// BASED_NUMBER with size, something like - scope.parameter=2'b11
if (num != value) {
*num = 0;
verinum *siz = make_unsized_dec(value);
val = pform_verinum_with_size(siz, val, "<command line>", 0);
}
PExpr* ndec = new PENumber(val);
Module::user_defparms.push_back( make_pair(name, ndec) );
}
else {
// REALTIME, something like - scope.parameter=1.22 or scope.parameter=1e2
if (strchr(value, '.') || strchr(value, 'e') || strchr(value, 'E')) {
verireal *val = new verireal(value);
PExpr* nreal = new PEFNumber(val);
Module::user_defparms.push_back( make_pair(name, nreal) );
}
else {
// DEC_NUMBER, something like - scope.parameter=3
verinum *val = make_unsized_dec(value);
PExpr* ndec = new PENumber(val);
Module::user_defparms.push_back( make_pair(name, ndec) );
}
}
}
free(key);
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;
/* increment this for generate schemes within a module, and set it
to zero when a new module starts. */
static unsigned scope_generate_counter = 1;
/* This tracks the current generate scheme being processed. This is
always within a module. */
static PGenerate*pform_cur_generate = 0;
/* 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 current time scale, as well as where the
* timescale was set. This supports warnings about tangled timescales.
*/
static int pform_time_unit;
static int pform_time_prec;
/* These two flags check the initial timeprecision and timeunit
* declaration inside a module.
*/
static bool tp_decl_flag = false;
static bool tu_decl_flag = false;
/*
* Flags used to set time_from_timescale based on timeunit and
* timeprecision.
*/
static bool tu_global_flag = false;
static bool tp_global_flag = false;
static bool tu_local_flag = false;
static bool tp_local_flag = false;
static char*pform_timescale_file = 0;
static unsigned pform_timescale_line;
static inline void FILE_NAME(LineInfo*obj, const char*file, unsigned lineno)
{
obj->set_lineno(lineno);
obj->set_file(filename_strings.make(file));
}
/*
* 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;
}
void pform_pop_scope()
{
assert(lexical_scope);
lexical_scope = lexical_scope->parent_scope();
}
static LexicalScope::lifetime_t find_lifetime(LexicalScope::lifetime_t lifetime)
{
if (lifetime != LexicalScope::INHERITED)
return lifetime;
if (lexical_scope != 0)
return lexical_scope->default_lifetime;
return LexicalScope::STATIC;
}
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;
}
PClass* pform_push_class_scope(const struct vlltype&loc, perm_string name,
LexicalScope::lifetime_t lifetime)
{
PClass*class_scope = new PClass(name, lexical_scope);
class_scope->default_lifetime = find_lifetime(lifetime);
FILE_NAME(class_scope, loc);
PScopeExtra*scopex = find_nearest_scopex(lexical_scope);
assert(!pform_cur_generate);
/* If no scope was found then this is being defined in the
* compilation unit scope. */
if (scopex == 0) {
pform_classes[name] = class_scope;
lexical_scope = class_scope;
return class_scope;
}
if (scopex->classes.find(name) != scopex->classes.end()) {
cerr << class_scope->get_fileline() << ": error: duplicate "
"definition for class '" << name << "' in '"
<< scopex->pscope_name() << "'." << endl;
error_count += 1;
}
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);
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);
if ((scopex == 0) && !gn_system_verilog()) {
cerr << task->get_fileline() << ": error: task declarations "
"must be contained within a module." << endl;
error_count += 1;
}
if (pform_cur_generate) {
// Check if the task is already in the dictionary.
if (pform_cur_generate->tasks.find(task->pscope_name()) !=
pform_cur_generate->tasks.end()) {
cerr << task->get_fileline() << ": error: duplicate "
"definition for task '" << name << "' in '"
<< pform_cur_module.front()->mod_name() << "' (generate)."
<< endl;
error_count += 1;
}
pform_cur_generate->tasks[task->pscope_name()] = task;
} else if (scopex) {
// Check if the task is already in the dictionary.
if (scopex->tasks.find(task->pscope_name()) != scopex->tasks.end()) {
cerr << task->get_fileline() << ": error: duplicate "
"definition for task '" << name << "' in '"
<< scopex->pscope_name() << "'." << endl;
error_count += 1;
}
scopex->tasks[task->pscope_name()] = task;
} else {
if (pform_tasks.find(task_name) != pform_tasks.end()) {
cerr << task->get_fileline() << ": error: "
<< "Duplicate definition for task '" << name
<< "' in $root scope." << endl;
error_count += 1;
}
pform_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);
if ((scopex == 0) && (generation_flag < GN_VER2005_SV)) {
cerr << func->get_fileline() << ": error: function declarations "
"must be contained within a module." << endl;
error_count += 1;
}
if (pform_cur_generate) {
// Check if the function is already in the dictionary.
if (pform_cur_generate->funcs.find(func->pscope_name()) !=
pform_cur_generate->funcs.end()) {
cerr << func->get_fileline() << ": error: duplicate "
"definition for function '" << name << "' in '"
<< pform_cur_module.front()->mod_name() << "' (generate)."
<< endl;
error_count += 1;
}
pform_cur_generate->funcs[func->pscope_name()] = func;
} else if (scopex != 0) {
// Check if the function is already in the dictionary.
if (scopex->funcs.find(func->pscope_name()) != scopex->funcs.end()) {
cerr << func->get_fileline() << ": error: duplicate "
"definition for function '" << name << "' in '"
<< scopex->pscope_name() << "'." << endl;
error_count += 1;
}
scopex->funcs[func->pscope_name()] = func;
} else {
if (pform_tasks.find(func_name) != pform_tasks.end()) {
cerr << func->get_fileline() << ": error: "
<< "Duplicate definition for function '" << name
<< "' in $root scope." << endl;
error_count += 1;
}
pform_tasks[func_name] = func;
}
lexical_scope = func;
return func;
}
PBlock* pform_push_block_scope(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);
block->default_lifetime = find_lifetime(LexicalScope::INHERITED);
lexical_scope = block;
return block;
}
/*
* Create a new identifier. Check if this is an imported name.
*/
PEIdent* pform_new_ident(const pform_name_t&name)
{
LexicalScope*scope = pform_peek_scope();
map<perm_string,PPackage*>::const_iterator pkg = scope->imports.find(name.front().name);
if (pkg == scope->imports.end())
return new PEIdent(name);
// XXXX For now, do not support indexed imported names.
assert(name.back().index.size() == 0);
return new PEIdent(pkg->second, name);
}
PGenerate* pform_parent_generate(void)
{
return pform_cur_generate;
}
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)
{
lexical_scope->wires[name] = net;
}
static void pform_put_enum_type_in_scope(enum_type_t*enum_set)
{
if (lexical_scope) {
ivl_assert(*enum_set, lexical_scope);
lexical_scope->enum_sets.insert(enum_set);
} else {
pform_enum_sets.insert(enum_set);
}
}
PWire*pform_get_make_wire_in_scope(perm_string name, NetNet::Type net_type, NetNet::PortType port_type, ivl_variable_type_t vt_type)
{
PWire*cur = pform_get_wire_in_scope(name);
if (cur == 0) {
cur = new PWire(name, net_type, port_type, vt_type);
pform_put_wire_in_scope(name, cur);
} else {
// If this is a duplicate wire, the data type has already
// been set, then return NULL.
if (cur->get_data_type() != IVL_VT_NO_TYPE) return 0;
bool rc = cur->set_wire_type(net_type);
assert(rc);
rc = cur->set_data_type(vt_type);
assert(rc);
}
return cur;
}
void pform_set_typedef(perm_string name, data_type_t*data_type, std::list<pform_range_t>*unp_ranges)
{
if(unp_ranges)
data_type = new uarray_type_t(data_type, unp_ranges);
// If we are in a lexical scope (i.e. a package or module)
// then put the typedef into that scope. Otherwise, put it
// into the $root scope.
data_type_t*&ref = lexical_scope
? lexical_scope->typedefs[name]
: pform_typedefs[name];
ivl_assert(*data_type, ref == 0);
ref = data_type;
if (enum_type_t*enum_type = dynamic_cast<enum_type_t*>(data_type)) {
pform_put_enum_type_in_scope(enum_type);
}
}
static data_type_t* test_type_identifier_in_root(perm_string name)
{
map<perm_string,data_type_t*>::iterator cur = pform_typedefs.find(name);
if (cur != pform_typedefs.end())
return cur->second;
else
return 0;
}
data_type_t* pform_test_type_identifier(const char*txt)
{
perm_string name = lex_strings.make(txt);
// If there is no lexical_scope yet, then look only in the
// $root scope for typedefs.
if (lexical_scope == 0) {
return test_type_identifier_in_root(name);
}
LexicalScope*cur_scope = lexical_scope;
do {
map<perm_string,data_type_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->imports.find(name);
if (cur_pkg != cur_scope->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;
cur_scope = cur_scope->parent_scope();
} while (cur_scope);
// See if there is a typedef in the $root scope.
if (data_type_t*tmp = test_type_identifier_in_root(name))
return tmp;
return 0;
}
/*
* The parser uses this function to test if the name is a typedef in
* the current scope. We use this to know if we can override the
* definition because it shadows a containing scope.
*/
bool pform_test_type_identifier_local(perm_string name)
{
if (lexical_scope == 0) {
if (test_type_identifier_in_root(name))
return true;
else
return false;
}
LexicalScope*cur_scope = lexical_scope;
map<perm_string,data_type_t*>::iterator cur;
cur = cur_scope->typedefs.find(name);
if (cur != cur_scope->typedefs.end())
return true;
return false;
}
PECallFunction* pform_make_call_function(const struct vlltype&loc,
const pform_name_t&name,
const list<PExpr*>&parms)
{
PECallFunction*tmp = 0;
// First try to get the function name from a package. Check
// the imports, and if the name is there, make the function as
// a package member.
do {
if (name.size() != 1)
break;
perm_string use_name = peek_tail_name(name);
map<perm_string,PPackage*>::iterator cur_pkg;
cur_pkg = lexical_scope->imports.find(use_name);
if (cur_pkg == lexical_scope->imports.end())
break;
tmp = new PECallFunction(cur_pkg->second, use_name, parms);
} while(0);
if (tmp == 0) {
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<PExpr*>&parms)
{
PCallTask*tmp = 0;
do {
if (name.size() != 1)
break;
perm_string use_name = peek_tail_name(name);
map<perm_string,PPackage*>::iterator cur_pkg;
cur_pkg = lexical_scope->imports.find(use_name);
if (cur_pkg == lexical_scope->imports.end())
break;
tmp = new PCallTask(cur_pkg->second, name, parms);
} while (0);
if (tmp == 0) {
tmp = new PCallTask(name, parms);
}
FILE_NAME(tmp, loc);
return tmp;
}
void pform_make_foreach_declarations(const struct vlltype&loc,
std::list<perm_string>*loop_vars)
{
static const struct str_pair_t str = { IVL_DR_STRONG, IVL_DR_STRONG };
list<decl_assignment_t*>assign_list;
for (list<perm_string>::const_iterator cur = loop_vars->begin()
; cur != loop_vars->end() ; ++ cur) {
decl_assignment_t*tmp_assign = new decl_assignment_t;
tmp_assign->name = lex_strings.make(*cur);
assign_list.push_back(tmp_assign);
}
pform_makewire(loc, 0, str, &assign_list, NetNet::REG, &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;
}
static void pform_put_behavior_in_scope(PProcess*pp)
{
lexical_scope->behaviors.push_back(pp);
}
void pform_put_behavior_in_scope(AProcess*pp)
{
lexical_scope->analog_behaviors.push_back(pp);
}
void pform_set_default_nettype(NetNet::Type type,
const char*file, unsigned lineno)
{
pform_default_nettype = type;
if (! pform_cur_module.empty()) {
cerr << file<<":"<<lineno << ": error: "
<< "`default_nettype directives must appear" << endl;
cerr << file<<":"<<lineno << ": : "
<< "outside module definitions. The containing" << endl;
cerr << file<<":"<<lineno << ": : "
<< "module " << pform_cur_module.back()->mod_name()
<< " starts on line "
<< pform_cur_module.back()->get_fileline() << "." << endl;
error_count += 1;
}
}
static void pform_declare_implicit_nets(PExpr*expr)
{
/* If implicit net creation is turned off, then stop now. */
if (pform_default_nettype == NetNet::NONE)
return;
if (expr)
expr->declare_implicit_nets(lexical_scope, pform_default_nettype);
}
/*
* The lexor calls this function to set the active timescale when it
* detects a `timescale directive. The function saves the directive
* values (for use by modules) and if warnings are enabled checks to
* see if some modules have no timescale.
*/
void pform_set_timescale(int unit, int prec,
const char*file, unsigned lineno)
{
bool first_flag = true;
assert(unit >= prec);
pform_time_unit = unit;
pform_time_prec = prec;
/* A `timescale clears the timeunit/timeprecision state. */
tu_global_flag = false;
tp_global_flag = false;
if (pform_timescale_file) {
free(pform_timescale_file);
first_flag = false;
}
if (file) pform_timescale_file = strdup(file);
else pform_timescale_file = 0;
pform_timescale_line = lineno;
if (!warn_timescale || !first_flag || !file) return;
/* Look to see if we have any modules without a timescale. */
bool have_no_ts = false;
map<perm_string,Module*>::iterator mod;
for (mod = pform_modules.begin(); mod != pform_modules.end(); ++ mod ) {
const Module*mp = (*mod).second;
if (mp->time_from_timescale ||
mp->timescale_warn_done) continue;
have_no_ts = true;
break;
}
/* If we do then print a message for the new ones. */
if (have_no_ts) {
cerr << file << ":" << lineno << ": warning: "
<< "Some modules have no timescale. This may cause"
<< endl;
cerr << file << ":" << lineno << ": : "
<< "confusing timing results. Affected modules are:"
<< endl;
for (mod = pform_modules.begin()
; mod != pform_modules.end() ; ++ mod ) {
Module*mp = (*mod).second;
if (mp->time_from_timescale ||
mp->timescale_warn_done) continue;
mp->timescale_warn_done = true;
cerr << file << ":" << lineno << ": : "
<< " -- module " << (*mod).first
<< " declared here: " << mp->get_fileline() << endl;
}
}
}
bool get_time_unit(const char*cp, int &unit)
{
const char *c;
bool rc = true;
if (strchr(cp, '_')) {
VLerror(yylloc, "Invalid timeunit constant ('_' is not "
"supported).");
return false;
}
c = strpbrk(cp, "munpfs");
if (!c)
return false;
if (*c == 's')
unit = 0;
else if (!strncmp(c, "ms", 2))
unit = -3;
else if (!strncmp(c, "us", 2))
unit = -6;
else if (!strncmp(c, "ns", 2))
unit = -9;
else if (!strncmp(c, "ps", 2))
unit = -12;
else if (!strncmp(c, "fs", 2))
unit = -15;
else {
rc = false;
ostringstream msg;
msg << "Invalid timeunit scale '" << cp << "'.";
VLerror(msg.str().c_str());
}
return rc;
}
/*
* Get a timeunit or timeprecision value from a string. This is
* similar to the code in lexor.lex for the `timescale directive.
*/
static bool get_time_unit_prec(const char*cp, int &res, bool is_unit)
{
/* We do not support a '_' in these time constants. */
if (strchr(cp, '_')) {
if (is_unit) {
VLerror(yylloc, "Invalid timeunit constant ('_' is not "
"supported).");
} else {
VLerror(yylloc, "Invalid timeprecision constant ('_' is not "
"supported).");
}
return true;
}
/* Check for the 1 digit. */
if (*cp != '1') {
if (is_unit) {
VLerror(yylloc, "Invalid timeunit constant (1st digit).");
} else {
VLerror(yylloc, "Invalid timeprecision constant (1st digit).");
}
return true;
}
cp += 1;
/* Check the number of zeros after the 1. */
res = strspn(cp, "0");
if (res > 2) {
if (is_unit) {
VLerror(yylloc, "Invalid timeunit constant (number of "
"zeros).");
} else {
VLerror(yylloc, "Invalid timeprecision constant (number of "
"zeros).");
}
return true;
}
cp += res;