3 * Copyright (c) 1998-2017 Stephen Williams (steve@icarus.com)
5 * This source code is free software; you can redistribute it
6 * and/or modify it in source code form under the terms of the GNU
7 * General Public License as published by the Free Software
8 * Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 //# define YYSTYPE lexval
26 # include "compiler.h"
27 # include "parse_misc.h"
28 # include "parse_api.h"
32 # include "lexor_keyword.h"
33 # include "discipline.h"
36 # define YY_USER_INIT reset_lexor();
37 # define yylval VLlval
42 * Lexical location information is passed in the yylloc variable to th
43 * parser. The file names, strings, are kept in a list so that I can
44 * re-use them. The set_file_name function will return a pointer to
45 * the name as it exists in the list (and delete the passed string.)
46 * If the name is new, it will be added to the list.
48 extern YYLTYPE yylloc;
50 char* strdupnew(char const *str)
52 return str ? strcpy(new char [strlen(str)+1], str) : 0;
55 static const char* set_file_name(char*text)
57 perm_string path = filename_strings.make(text);
60 /* Check this file name with the list of library file
61 names. If there is a match, then turn on the
62 pform_library_flag. This is how the parser knows that
63 modules declared in this file are library modules. */
64 pform_library_flag = library_file_map[path];
69 static void line_directive();
70 static void line_directive2();
71 static void reset_all();
73 verinum*make_unsized_binary(const char*txt);
74 verinum*make_undef_highz_dec(const char*txt);
75 verinum*make_unsized_dec(const char*txt);
76 verinum*make_unsized_octal(const char*txt);
77 verinum*make_unsized_hex(const char*txt);
79 static int dec_buf_div2(char *buf);
81 static void process_timescale(const char*txt);
82 static void process_ucdrive(const char*txt);
84 static list<int> keyword_mask_stack;
86 static int comment_enter;
87 static bool in_module = false;
88 static bool in_UDP = false;
89 bool in_celldefine = false;
90 UCDriveType uc_drive = UCD_NONE;
93 * The parser sometimes needs to indicate to the lexor that the next
94 * identifier needs to be understood in the context of a package. The
95 * parser feeds back that left context with calls to the
96 * lex_in_package_scope.
98 static PPackage* in_package_scope = 0;
99 void lex_in_package_scope(PPackage*pkg)
101 in_package_scope = pkg;
128 /* Recognize the various line directives. */
129 ^"#line"[ \t]+.+ { line_directive(); }
130 ^[ \t]?"`line"[ \t]+.+ { line_directive2(); }
133 \n { yylloc.first_line += 1; }
135 /* C++ style comments start with / / and run to the end of the
136 current line. These are very easy to handle. The meta-comments
137 format is a little more tricky to handle, but do what we can. */
139 /* The lexor detects "// synthesis translate_on/off" meta-comments,
140 we handle them here by turning on/off a flag. The pform uses
141 that flag to attach implicit attributes to "initial" and
142 "always" statements. */
144 "//"{W}*"synthesis"{W}+"translate_on"{W}*\n { pform_mc_translate_on(true); }
145 "//"{W}*"synthesis"{W}+"translate_off"{W}*\n { pform_mc_translate_on(false); }
146 "//" { comment_enter = YY_START; BEGIN(LCOMMENT); }
147 <LCOMMENT>. { yymore(); }
148 <LCOMMENT>\n { yylloc.first_line += 1; BEGIN(comment_enter); }
151 /* The contents of C-style comments are ignored, like white space. */
153 "/*" { comment_enter = YY_START; BEGIN(CCOMMENT); }
155 <CCOMMENT>\n { yylloc.first_line += 1; }
156 <CCOMMENT>"*/" { BEGIN(comment_enter); }
159 "(*" { return K_PSTAR; }
160 "*)" { return K_STARP; }
161 ".*" { return K_DOTSTAR; }
162 "<<" { return K_LS; }
163 "<<<" { return K_LS; /* Note: Functionally, <<< is the same as <<. */}
164 ">>" { return K_RS; }
165 ">>>" { return K_RSS; }
166 "**" { return K_POW; }
167 "<=" { return K_LE; }
168 ">=" { return K_GE; }
169 "=>" { return K_EG; }
172 * Resolve the ambiguity between the += assignment
173 * operator and +=> polarity edge path operator
175 * +=> should be treated as two separate tokens '+' and
176 * '=>' (K_EG), therefore we only consume the first
177 * character of the matched pattern i.e. either + or -
178 * and push back the rest of the matches text (=>) in
184 "*>" { return K_SG; }
185 "==" { return K_EQ; }
186 "!=" { return K_NE; }
187 "===" { return K_CEQ; }
188 "!==" { return K_CNE; }
189 "==?" { return K_WEQ; }
190 "!=?" { return K_WNE; }
191 "||" { return K_LOR; }
192 "&&" { return K_LAND; }
193 "&&&" { return K_TAND; }
194 "~|" { return K_NOR; }
195 "~^" { return K_NXOR; }
196 "^~" { return K_NXOR; }
197 "~&" { return K_NAND; }
198 "->" { return K_TRIGGER; }
199 "+:" { return K_PO_POS; }
200 "-:" { return K_PO_NEG; }
201 "<+" { return K_CONTRIBUTE; }
202 "+=" { return K_PLUS_EQ; }
203 "-=" { return K_MINUS_EQ; }
204 "*=" { return K_MUL_EQ; }
205 "/=" { return K_DIV_EQ; }
206 "%=" { return K_MOD_EQ; }
207 "&=" { return K_AND_EQ; }
208 "|=" { return K_OR_EQ; }
209 "^=" { return K_XOR_EQ; }
210 "<<=" { return K_LS_EQ; }
211 ">>=" { return K_RS_EQ; }
212 "<<<=" { return K_LS_EQ; }
213 ">>>=" { return K_RSS_EQ; }
214 "++" { return K_INCR; }
215 "--" {return K_DECR; }
216 "'{" { return K_LP; }
217 "::" { return K_SCOPE_RES; }
219 /* Watch out for the tricky case of (*). Cannot parse this as "(*"
220 and ")", but since I know that this is really ( * ), replace it
221 with "*" and return that. */
222 "("{W}*"*"{W}*")" { return '*'; }
224 <EDGES>"]" { BEGIN(0); return yytext[0]; }
225 [}{;:\[\],()#=.@&!?<>%|^~+*/-] { return yytext[0]; }
227 \" { BEGIN(CSTRING); }
228 <CSTRING>\\\\ { yymore(); /* Catch \\, which is a \ escaping itself */ }
229 <CSTRING>\\\" { yymore(); /* Catch \", which is an escaped quote */ }
230 <CSTRING>\n { BEGIN(0);
231 yylval.text = strdupnew(yytext);
232 VLerror(yylloc, "Missing close quote of string.");
233 yylloc.first_line += 1;
235 <CSTRING>\" { BEGIN(0);
236 yylval.text = strdupnew(yytext);
237 yylval.text[strlen(yytext)-1] = 0;
239 <CSTRING>. { yymore(); }
241 /* The UDP Table is a unique lexical environment. These are most
242 tokens that we can expect in a table. */
243 <UDPTABLE>\(\?0\) { return '_'; }
244 <UDPTABLE>\(\?1\) { return '+'; }
245 <UDPTABLE>\(\?[xX]\) { return '%'; }
246 <UDPTABLE>\(\?\?\) { return '*'; }
247 <UDPTABLE>\(01\) { return 'r'; }
248 <UDPTABLE>\(0[xX]\) { return 'Q'; }
249 <UDPTABLE>\(b[xX]\) { return 'q'; }
250 <UDPTABLE>\(b0\) { return 'f'; /* b0 is 10|00, but only 10 is meaningful */}
251 <UDPTABLE>\(b1\) { return 'r'; /* b1 is 11|01, but only 01 is meaningful */}
252 <UDPTABLE>\(0\?\) { return 'P'; }
253 <UDPTABLE>\(10\) { return 'f'; }
254 <UDPTABLE>\(1[xX]\) { return 'M'; }
255 <UDPTABLE>\(1\?\) { return 'N'; }
256 <UDPTABLE>\([xX]0\) { return 'F'; }
257 <UDPTABLE>\([xX]1\) { return 'R'; }
258 <UDPTABLE>\([xX]\?\) { return 'B'; }
259 <UDPTABLE>[bB] { return 'b'; }
260 <UDPTABLE>[lL] { return 'l'; /* IVL extension */ }
261 <UDPTABLE>[hH] { return 'h'; /* IVL extension */ }
262 <UDPTABLE>[fF] { return 'f'; }
263 <UDPTABLE>[rR] { return 'r'; }
264 <UDPTABLE>[xX] { return 'x'; }
265 <UDPTABLE>[nN] { return 'n'; }
266 <UDPTABLE>[pP] { return 'p'; }
267 <UDPTABLE>[01\?\*\-:;] { return yytext[0]; }
269 <EDGES>"01" { return K_edge_descriptor; }
270 <EDGES>"0x" { return K_edge_descriptor; }
271 <EDGES>"0z" { return K_edge_descriptor; }
272 <EDGES>"10" { return K_edge_descriptor; }
273 <EDGES>"1x" { return K_edge_descriptor; }
274 <EDGES>"1z" { return K_edge_descriptor; }
275 <EDGES>"x0" { return K_edge_descriptor; }
276 <EDGES>"x1" { return K_edge_descriptor; }
277 <EDGES>"z0" { return K_edge_descriptor; }
278 <EDGES>"z1" { return K_edge_descriptor; }
280 [a-zA-Z_][a-zA-Z0-9$_]* {
281 int rc = lexor_keyword_code(yytext, yyleng);
284 yylval.text = strdupnew(yytext);
285 if (strncmp(yylval.text,"PATHPULSE$", 10) == 0)
286 rc = PATHPULSE_IDENTIFIER;
319 /* Special case: If this is part of a scoped name, then check
320 the package for identifier details. For example, if the
321 source file is foo::bar, the parse.y will note the
322 PACKAGE_IDENTIFIER and "::" token and mark the
323 "in_package_scope" variable. Then this lexor will see the
324 identifier here and interpret it in the package scope. */
325 if (in_package_scope) {
326 if (rc == IDENTIFIER) {
327 if (data_type_t*type = pform_test_type_identifier(in_package_scope, yylval.text)) {
328 yylval.type_identifier.text = yylval.text;
329 yylval.type_identifier.type = type;
330 rc = TYPE_IDENTIFIER;
333 in_package_scope = 0;
337 /* If this identifier names a discipline, then return this as
338 a DISCIPLINE_IDENTIFIER and return the discipline as the
340 if (rc == IDENTIFIER && gn_verilog_ams_flag) {
341 perm_string tmp = lex_strings.make(yylval.text);
342 map<perm_string,ivl_discipline_t>::iterator cur = disciplines.find(tmp);
343 if (cur != disciplines.end()) {
345 yylval.discipline = (*cur).second;
346 rc = DISCIPLINE_IDENTIFIER;
350 /* If this identifier names a previously declared package, then
351 return this as a PACKAGE_IDENTIFIER instead. */
352 if (rc == IDENTIFIER && gn_system_verilog()) {
353 if (PPackage*pkg = pform_test_package_identifier(yylval.text)) {
355 yylval.package = pkg;
356 rc = PACKAGE_IDENTIFIER;
360 /* If this identifier names a previously declared type, then
361 return this as a TYPE_IDENTIFIER instead. */
362 if (rc == IDENTIFIER && gn_system_verilog()) {
363 if (data_type_t*type = pform_test_type_identifier(yylval.text)) {
364 yylval.type_identifier.text = yylval.text;
365 yylval.type_identifier.type = type;
366 rc = TYPE_IDENTIFIER;
375 yylval.text = strdupnew(yytext+1);
376 if (gn_system_verilog()) {
377 if (PPackage*pkg = pform_test_package_identifier(yylval.text)) {
379 yylval.package = pkg;
380 return PACKAGE_IDENTIFIER;
383 if (gn_system_verilog()) {
384 if (data_type_t*type = pform_test_type_identifier(yylval.text)) {
385 yylval.type_identifier.text = yylval.text;
386 yylval.type_identifier.type = type;
387 return TYPE_IDENTIFIER;
394 /* The 1364-1995 timing checks. */
395 if (strcmp(yytext,"$hold") == 0)
397 if (strcmp(yytext,"$nochange") == 0)
399 if (strcmp(yytext,"$period") == 0)
401 if (strcmp(yytext,"$recovery") == 0)
403 if (strcmp(yytext,"$setup") == 0)
405 if (strcmp(yytext,"$setuphold") == 0)
407 if (strcmp(yytext,"$skew") == 0)
409 if (strcmp(yytext,"$width") == 0)
411 /* The new 1364-2001 timing checks. */
412 if (strcmp(yytext,"$fullskew") == 0)
414 if (strcmp(yytext,"$recrem") == 0)
416 if (strcmp(yytext,"$removal") == 0)
418 if (strcmp(yytext,"$timeskew") == 0)
421 if (strcmp(yytext,"$attribute") == 0)
424 if (gn_system_verilog() && strcmp(yytext,"$unit") == 0) {
425 yylval.package = pform_units.back();
426 return PACKAGE_IDENTIFIER;
429 yylval.text = strdupnew(yytext);
430 return SYSTEM_IDENTIFIER; }
433 \'[sS]?[dD][ \t]*[0-9][0-9_]* {
434 yylval.number = make_unsized_dec(yytext);
437 \'[sS]?[dD][ \t]*[xzXZ?]_* {
438 yylval.number = make_undef_highz_dec(yytext);
441 \'[sS]?[bB][ \t]*[0-1xzXZ?][0-1xzXZ?_]* {
442 yylval.number = make_unsized_binary(yytext);
445 \'[sS]?[oO][ \t]*[0-7xzXZ?][0-7xzXZ?_]* {
446 yylval.number = make_unsized_octal(yytext);
449 \'[sS]?[hH][ \t]*[0-9a-fA-FxzXZ?][0-9a-fA-FxzXZ?_]* {
450 yylval.number = make_unsized_hex(yytext);
454 if (!gn_system_verilog()) {
455 cerr << yylloc.text << ":" << yylloc.first_line << ": warning: "
456 << "Using SystemVerilog 'N bit vector. Use at least "
457 << "-g2005-sv to remove this warning." << endl;
459 generation_t generation_save = generation_flag;
460 generation_flag = GN_VER2005_SV;
461 yylval.number = make_unsized_binary(yytext);
462 generation_flag = generation_save;
463 return UNBASED_NUMBER; }
465 /* Decimal numbers are the usual. But watch out for the UDPTABLE
466 mode, where there are no decimal numbers. Reject the match if we
467 are in the UDPTABLE state. */
469 if (YY_START==UDPTABLE) {
472 yylval.number = make_unsized_dec(yytext);
473 based_size = yylval.number->as_ulong();
478 /* This rule handles scaled time values for SystemVerilog. */
479 [0-9][0-9_]*(\.[0-9][0-9_]*)?{TU}?s {
480 if (gn_system_verilog()) {
481 yylval.text = strdupnew(yytext);
485 /* These rules handle the scaled real literals from Verilog-AMS. The
486 value is a number with a single letter scale factor. If
487 verilog-ams is not enabled, then reject this rule. If it is
488 enabled, then collect the scale and use it to scale the value. */
489 [0-9][0-9_]*\.[0-9][0-9_]*/{S} {
490 if (!gn_verilog_ams_flag) REJECT;
495 if (!gn_verilog_ams_flag) REJECT;
500 size_t token_len = strlen(yytext);
501 char*tmp = new char[token_len + 5];
504 switch (tmp[token_len-1]) {
505 case 'a': scale = -18; break; /* atto- */
506 case 'f': scale = -15; break; /* femto- */
507 case 'p': scale = -12; break; /* pico- */
508 case 'n': scale = -9; break; /* nano- */
509 case 'u': scale = -6; break; /* micro- */
510 case 'm': scale = -3; break; /* milli- */
511 case 'k': scale = 3; break; /* kilo- */
512 case 'K': scale = 3; break; /* kilo- */
513 case 'M': scale = 6; break; /* mega- */
514 case 'G': scale = 9; break; /* giga- */
515 case 'T': scale = 12; break; /* tera- */
516 default: assert(0); break;
518 snprintf(tmp+token_len-1, 5, "e%d", scale);
519 yylval.realtime = new verireal(tmp);
525 [0-9][0-9_]*\.[0-9][0-9_]*([Ee][+-]?[0-9][0-9_]*)? {
526 yylval.realtime = new verireal(yytext);
529 [0-9][0-9_]*[Ee][+-]?[0-9][0-9_]* {
530 yylval.realtime = new verireal(yytext);
534 /* Notice and handle the `timescale directive. */
536 ^{W}?`timescale { BEGIN(PPTIMESCALE); }
537 <PPTIMESCALE>.* { process_timescale(yytext); }
540 cerr << yylloc.text << ":" << yylloc.first_line << ": error: "
541 "`timescale directive can not be inside a module "
542 "definition." << endl;
545 yylloc.first_line += 1;
548 /* Notice and handle the `celldefine and `endcelldefine directives. */
550 ^{W}?`celldefine{W}? { in_celldefine = true; }
551 ^{W}?`endcelldefine{W}? { in_celldefine = false; }
553 /* Notice and handle the resetall directive. */
557 cerr << yylloc.text << ":" << yylloc.first_line << ": error: "
558 "`resetall directive can not be inside a module "
559 "definition." << endl;
562 cerr << yylloc.text << ":" << yylloc.first_line << ": error: "
563 "`resetall directive can not be inside a UDP "
564 "definition." << endl;
570 /* Notice and handle the `unconnected_drive directive. */
571 ^{W}?`unconnected_drive { BEGIN(PPUCDRIVE); }
572 <PPUCDRIVE>.* { process_ucdrive(yytext); }
575 cerr << yylloc.text << ":" << yylloc.first_line << ": error: "
576 "`unconnected_drive directive can not be inside a "
577 "module definition." << endl;
580 yylloc.first_line += 1;
583 ^{W}?`nounconnected_drive{W}? {
585 cerr << yylloc.text << ":" << yylloc.first_line << ": error: "
586 "`nounconnected_drive directive can not be inside a "
587 "module definition." << endl;
590 uc_drive = UCD_NONE; }
592 /* These are directives that I do not yet support. I think that IVL
593 should handle these, not an external preprocessor. */
594 /* From 1364-2005 Chapter 19. */
595 ^{W}?`pragme{W}?.* { }
597 /* From 1364-2005 Annex D. */
598 ^{W}?`default_decay_time{W}?.* { }
599 ^{W}?`default_trireg_strength{W}?.* { }
600 ^{W}?`delay_mode_distributed{W}?.* { }
601 ^{W}?`delay_mode_path{W}?.* { }
602 ^{W}?`delay_mode_unit{W}?.* { }
603 ^{W}?`delay_mode_zero{W}?.* { }
605 /* From other places. */
606 ^{W}?`disable_portfaults{W}?.* { }
607 ^{W}?`enable_portfaults{W}?.* { }
609 ^{W}?`nosuppress_faults{W}?.* { }
611 ^{W}?`suppress_faults{W}?.* { }
612 ^{W}?`uselib{W}?.* { }
614 ^{W}?`begin_keywords{W}? { BEGIN(PPBEGIN_KEYWORDS); }
616 <PPBEGIN_KEYWORDS>\"[a-zA-Z0-9 -\.]*\".* {
617 keyword_mask_stack.push_front(lexor_keyword_mask);
619 char*word = yytext+1;
620 char*tail = strchr(word, '"');
622 if (strcmp(word,"1364-1995") == 0) {
623 lexor_keyword_mask = GN_KEYWORDS_1364_1995;
624 } else if (strcmp(word,"1364-2001") == 0) {
625 lexor_keyword_mask = GN_KEYWORDS_1364_1995
626 |GN_KEYWORDS_1364_2001
627 |GN_KEYWORDS_1364_2001_CONFIG;
628 } else if (strcmp(word,"1364-2001-noconfig") == 0) {
629 lexor_keyword_mask = GN_KEYWORDS_1364_1995
630 |GN_KEYWORDS_1364_2001;
631 } else if (strcmp(word,"1364-2005") == 0) {
632 lexor_keyword_mask = GN_KEYWORDS_1364_1995
633 |GN_KEYWORDS_1364_2001
634 |GN_KEYWORDS_1364_2001_CONFIG
635 |GN_KEYWORDS_1364_2005;
636 } else if (strcmp(word,"1800-2005") == 0) {
637 lexor_keyword_mask = GN_KEYWORDS_1364_1995
638 |GN_KEYWORDS_1364_2001
639 |GN_KEYWORDS_1364_2001_CONFIG
640 |GN_KEYWORDS_1364_2005
641 |GN_KEYWORDS_1800_2005;
642 } else if (strcmp(word,"1800-2009") == 0) {
643 lexor_keyword_mask = GN_KEYWORDS_1364_1995
644 |GN_KEYWORDS_1364_2001
645 |GN_KEYWORDS_1364_2001_CONFIG
646 |GN_KEYWORDS_1364_2005
647 |GN_KEYWORDS_1800_2005
648 |GN_KEYWORDS_1800_2009;
649 } else if (strcmp(word,"1800-2012") == 0) {
650 lexor_keyword_mask = GN_KEYWORDS_1364_1995
651 |GN_KEYWORDS_1364_2001
652 |GN_KEYWORDS_1364_2001_CONFIG
653 |GN_KEYWORDS_1364_2005
654 |GN_KEYWORDS_1800_2005
655 |GN_KEYWORDS_1800_2009
656 |GN_KEYWORDS_1800_2012;
657 } else if (strcmp(word,"VAMS-2.3") == 0) {
658 lexor_keyword_mask = GN_KEYWORDS_1364_1995
659 |GN_KEYWORDS_1364_2001
660 |GN_KEYWORDS_1364_2001_CONFIG
661 |GN_KEYWORDS_1364_2005
662 |GN_KEYWORDS_VAMS_2_3;
664 fprintf(stderr, "%s:%d: Ignoring unknown keywords string: %s\n",
665 yylloc.text, yylloc.first_line, word);
670 <PPBEGIN_KEYWORDS>.* {
671 fprintf(stderr, "%s:%d: Malformed keywords specification: %s\n",
672 yylloc.text, yylloc.first_line, yytext);
676 ^{W}?`end_keywords{W}?.* {
677 if (!keyword_mask_stack.empty()) {
678 lexor_keyword_mask = keyword_mask_stack.front();
679 keyword_mask_stack.pop_front();
681 fprintf(stderr, "%s:%d: Mismatched end_keywords directive\n",
682 yylloc.text, yylloc.first_line);
686 /* Notice and handle the default_nettype directive. The lexor
687 detects the default_nettype keyword, and the second part of the
688 rule collects the rest of the line and processes it. We only need
689 to look for the first work, and interpret it. */
691 `default_nettype{W}? { BEGIN(PPDEFAULT_NETTYPE); }
692 <PPDEFAULT_NETTYPE>.* {
693 NetNet::Type net_type;
694 size_t wordlen = strcspn(yytext, " \t\f\r\n");
696 /* Add support for other wire types and better error detection. */
697 if (strcmp(yytext,"wire") == 0) {
698 net_type = NetNet::WIRE;
700 } else if (strcmp(yytext,"tri") == 0) {
701 net_type = NetNet::TRI;
703 } else if (strcmp(yytext,"tri0") == 0) {
704 net_type = NetNet::TRI0;
706 } else if (strcmp(yytext,"tri1") == 0) {
707 net_type = NetNet::TRI1;
709 } else if (strcmp(yytext,"wand") == 0) {
710 net_type = NetNet::WAND;
712 } else if (strcmp(yytext,"triand") == 0) {
713 net_type = NetNet::TRIAND;
715 } else if (strcmp(yytext,"wor") == 0) {
716 net_type = NetNet::WOR;
718 } else if (strcmp(yytext,"trior") == 0) {
719 net_type = NetNet::TRIOR;
721 } else if (strcmp(yytext,"none") == 0) {
722 net_type = NetNet::NONE;
725 cerr << yylloc.text << ":" << yylloc.first_line
726 << ": error: Net type " << yytext
727 << " is not a valid (or supported)"
728 << " default net type." << endl;
729 net_type = NetNet::WIRE;
732 pform_set_default_nettype(net_type, yylloc.text, yylloc.first_line);
734 <PPDEFAULT_NETTYPE>\n {
735 yylloc.first_line += 1;
739 /* These are directives that are not supported by me and should have
740 been handled by an external preprocessor such as ivlpp. */
743 cerr << yylloc.text << ":" << yylloc.first_line <<
744 ": warning: `define not supported. Use an external preprocessor."
749 cerr << yylloc.text << ":" << yylloc.first_line <<
750 ": warning: `else not supported. Use an external preprocessor."
755 cerr << yylloc.text << ":" << yylloc.first_line <<
756 ": warning: `elsif not supported. Use an external preprocessor."
761 cerr << yylloc.text << ":" << yylloc.first_line <<
762 ": warning: `endif not supported. Use an external preprocessor."
767 cerr << yylloc.text << ":" << yylloc.first_line <<
768 ": warning: `ifdef not supported. Use an external preprocessor."
773 cerr << yylloc.text << ":" << yylloc.first_line <<
774 ": warning: `ifndef not supported. Use an external preprocessor."
779 cerr << yylloc.text << ":" << yylloc.first_line <<
780 ": warning: `include not supported. Use an external preprocessor."
785 cerr << yylloc.text << ":" << yylloc.first_line <<
786 ": warning: `undef not supported. Use an external preprocessor."
791 `{W} { cerr << yylloc.text << ":" << yylloc.first_line << ": error: "
792 << "Stray tic (`) here. Perhaps you put white space" << endl;
793 cerr << yylloc.text << ":" << yylloc.first_line << ": : "
794 << "between the tic and preprocessor directive?"
798 . { return yytext[0]; }
800 /* Final catchall. something got lost or mishandled. */
801 /* XXX Should we tell the user something about the lexical state? */
803 <*>.|\n { cerr << yylloc.text << ":" << yylloc.first_line
804 << ": error: unmatched character (";
805 if (isprint(yytext[0]))
808 cerr << "hex " << hex << ((unsigned char) yytext[0]);
816 * The UDP state table needs some slightly different treatment by the
817 * lexor. The level characters are normally accepted as other things,
818 * so the parser needs to switch my mode when it believes in needs to.
825 static unsigned truncate_to_integer_width(verinum::V*bits, unsigned size)
827 if (size <= integer_width) return size;
829 verinum::V pad = bits[size-1];
830 if (pad == verinum::V1) pad = verinum::V0;
832 for (unsigned idx = integer_width; idx < size; idx += 1) {
833 if (bits[idx] != pad) {
834 yywarn(yylloc, "Unsized numeric constant truncated to integer width.");
838 return integer_width;
841 verinum*make_unsized_binary(const char*txt)
843 bool sign_flag = false;
844 bool single_flag = false;
845 const char*ptr = txt;
846 assert(*ptr == '\'');
849 if (tolower(*ptr) == 's') {
854 assert((tolower(*ptr) == 'b') || gn_system_verilog());
855 if (tolower(*ptr) == 'b') {
858 assert(sign_flag == false);
862 while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
866 for (const char*idx = ptr ; *idx ; idx += 1)
867 if (*idx != '_') size += 1;
870 VLerror(yylloc, "Numeric literal has no digits in it.");
871 verinum*out = new verinum();
872 out->has_sign(sign_flag);
873 out->is_single(single_flag);
877 if ((based_size > 0) && (size > based_size)) yywarn(yylloc,
878 "extra digits given for sized binary constant.");
880 verinum::V*bits = new verinum::V[size];
886 bits[--idx] = verinum::V0;
889 bits[--idx] = verinum::V1;
891 case 'z': case 'Z': case '?':
892 bits[--idx] = verinum::Vz;
895 bits[--idx] = verinum::Vx;
900 fprintf(stderr, "%c\n", ptr[0]);
906 if (gn_strict_expr_width_flag && (based_size == 0))
907 size = truncate_to_integer_width(bits, size);
909 verinum*out = new verinum(bits, size, false);
910 out->has_sign(sign_flag);
911 out->is_single(single_flag);
917 verinum*make_unsized_octal(const char*txt)
919 bool sign_flag = false;
920 const char*ptr = txt;
921 assert(*ptr == '\'');
924 if (tolower(*ptr) == 's') {
929 assert(tolower(*ptr) == 'o');
932 while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
936 for (const char*idx = ptr ; *idx ; idx += 1)
937 if (*idx != '_') size += 3;
939 if (based_size > 0) {
940 int rem = based_size % 3;
941 if (rem != 0) based_size += 3 - rem;
942 if (size > based_size) yywarn(yylloc,
943 "extra digits given for sized octal constant.");
946 verinum::V*bits = new verinum::V[size];
952 case '0': case '1': case '2': case '3':
953 case '4': case '5': case '6': case '7':
955 bits[--idx] = (val&4) ? verinum::V1 : verinum::V0;
956 bits[--idx] = (val&2) ? verinum::V1 : verinum::V0;
957 bits[--idx] = (val&1) ? verinum::V1 : verinum::V0;
960 bits[--idx] = verinum::Vx;
961 bits[--idx] = verinum::Vx;
962 bits[--idx] = verinum::Vx;
964 case 'z': case 'Z': case '?':
965 bits[--idx] = verinum::Vz;
966 bits[--idx] = verinum::Vz;
967 bits[--idx] = verinum::Vz;
977 if (gn_strict_expr_width_flag && (based_size == 0))
978 size = truncate_to_integer_width(bits, size);
980 verinum*out = new verinum(bits, size, false);
981 out->has_sign(sign_flag);
987 verinum*make_unsized_hex(const char*txt)
989 bool sign_flag = false;
990 const char*ptr = txt;
991 assert(*ptr == '\'');
994 if (tolower(*ptr) == 's') {
998 assert(tolower(*ptr) == 'h');
1001 while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
1005 for (const char*idx = ptr ; *idx ; idx += 1)
1006 if (*idx != '_') size += 4;
1008 if (based_size > 0) {
1009 int rem = based_size % 4;
1010 if (rem != 0) based_size += 4 - rem;
1011 if (size > based_size) yywarn(yylloc,
1012 "extra digits given for sized hex constant.");
1015 verinum::V*bits = new verinum::V[size];
1017 unsigned idx = size;
1021 case '0': case '1': case '2': case '3': case '4':
1022 case '5': case '6': case '7': case '8': case '9':
1024 bits[--idx] = (val&8) ? verinum::V1 : verinum::V0;
1025 bits[--idx] = (val&4) ? verinum::V1 : verinum::V0;
1026 bits[--idx] = (val&2) ? verinum::V1 : verinum::V0;
1027 bits[--idx] = (val&1) ? verinum::V1 : verinum::V0;
1029 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1030 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1031 val = tolower(*ptr) - 'a' + 10;
1032 bits[--idx] = (val&8) ? verinum::V1 : verinum::V0;
1033 bits[--idx] = (val&4) ? verinum::V1 : verinum::V0;
1034 bits[--idx] = (val&2) ? verinum::V1 : verinum::V0;
1035 bits[--idx] = (val&1) ? verinum::V1 : verinum::V0;
1038 bits[--idx] = verinum::Vx;
1039 bits[--idx] = verinum::Vx;
1040 bits[--idx] = verinum::Vx;
1041 bits[--idx] = verinum::Vx;
1043 case 'z': case 'Z': case '?':
1044 bits[--idx] = verinum::Vz;
1045 bits[--idx] = verinum::Vz;
1046 bits[--idx] = verinum::Vz;
1047 bits[--idx] = verinum::Vz;
1057 if (gn_strict_expr_width_flag && (based_size == 0))
1058 size = truncate_to_integer_width(bits, size);
1060 verinum*out = new verinum(bits, size, false);
1061 out->has_sign(sign_flag);
1067 /* Divide the integer given by the string by 2. Return the remainder bit. */
1068 static int dec_buf_div2(char *buf)
1071 int len = strlen(buf);
1078 /* dst_ptr overwrites buf, but all characters that are overwritten
1079 were already used by the reader. */
1082 while(buf[pos] == '0')
1085 for(; pos<len; ++pos){
1089 assert(isdigit(buf[pos]));
1091 partial= partial*10 + (buf[pos]-'0');
1094 *dst_ptr = partial/2 + '0';
1095 partial = partial & 1;
1105 // If result of division was zero string, it should remain that way.
1106 // Don't eat the last zero...
1107 if (dst_ptr == buf){
1116 /* Support a single x, z or ? as a decimal constant (from 1364-2005). */
1117 verinum* make_undef_highz_dec(const char* ptr)
1119 bool signed_flag = false;
1121 assert(*ptr == '\'');
1122 /* The number may have decorations of the form 'sd<code>,
1123 possibly with space between the d and the <code>.
1124 Also, the 's' is optional, and marks the number as signed. */
1127 if (tolower(*ptr) == 's') {
1132 assert(tolower(*ptr) == 'd');
1135 while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
1138 /* Process the code. */
1139 verinum::V* bits = new verinum::V[1];
1143 bits[0] = verinum::Vx;
1148 bits[0] = verinum::Vz;
1154 while (*ptr == '_') ptr += 1;
1157 verinum*out = new verinum(bits, 1, false);
1158 out->has_sign(signed_flag);
1164 * Making a decimal number is much easier than the other base numbers
1165 * because there are no z or x values to worry about. It is much
1166 * harder than other base numbers because the width needed in bits is
1167 * hard to calculate.
1170 verinum*make_unsized_dec(const char*ptr)
1173 bool signed_flag = false;
1176 if (ptr[0] == '\'') {
1177 /* The number has decorations of the form 'sd<digits>,
1178 possibly with space between the d and the <digits>.
1179 Also, the 's' is optional, and marks the number as
1183 if (tolower(*ptr) == 's') {
1188 assert(tolower(*ptr) == 'd');
1191 while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
1195 /* ... or an undecorated decimal number is passed
1196 it. These numbers are treated as signed decimal. */
1197 assert(isdigit(*ptr));
1202 /* Copy the digits into a buffer that I can use to do in-place
1205 while ((idx < sizeof buf) && (*ptr != 0)) {
1211 buf[idx++] = *ptr++;
1214 if (idx == sizeof buf) {
1215 fprintf(stderr, "Ridiculously long"
1216 " decimal constant will be truncated!\n");
1221 unsigned tmp_size = idx * 4 + 1;
1222 verinum::V *bits = new verinum::V[tmp_size];
1225 while (idx < tmp_size) {
1226 int rem = dec_buf_div2(buf);
1227 bits[idx++] = (rem == 1) ? verinum::V1 : verinum::V0;
1230 assert(strcmp(buf, "0") == 0);
1232 /* Now calculate the minimum number of bits needed to
1233 represent this unsigned number. */
1234 unsigned size = tmp_size;
1235 while ((size > 1) && (bits[size-1] == verinum::V0))
1238 /* Now account for the signedness. Don't leave a 1 in the high
1239 bit if this is a signed number. */
1240 if (signed_flag && (bits[size-1] == verinum::V1)) {
1242 assert(size <= tmp_size);
1245 /* Since we never have the real number of bits that a decimal
1246 number represents we do not check for extra bits. */
1247 // if (based_size > 0) { }
1249 if (gn_strict_expr_width_flag && (based_size == 0))
1250 size = truncate_to_integer_width(bits, size);
1252 verinum*res = new verinum(bits, size, false);
1253 res->has_sign(signed_flag);
1260 * Convert the string to a time unit or precision.
1261 * Returns true on failure.
1263 static bool get_timescale_const(const char *&cp, int &res, bool is_unit)
1265 /* Check for the 1 digit. */
1268 VLerror(yylloc, "Invalid `timescale unit constant "
1271 VLerror(yylloc, "Invalid `timescale precision constant "
1278 /* Check the number of zeros after the 1. */
1279 res = strspn(cp, "0");
1282 VLerror(yylloc, "Invalid `timescale unit constant "
1283 "(number of zeros)");
1285 VLerror(yylloc, "Invalid `timescale precision constant "
1286 "(number of zeros)");
1292 /* Skip any space between the digits and the scaling string. */
1293 cp += strspn(cp, " \t");
1295 /* Now process the scaling string. */
1296 if (strncmp("s", cp, 1) == 0) {
1301 } else if (strncmp("ms", cp, 2) == 0) {
1306 } else if (strncmp("us", cp, 2) == 0) {
1311 } else if (strncmp("ns", cp, 2) == 0) {
1316 } else if (strncmp("ps", cp, 2) == 0) {
1321 } else if (strncmp("fs", cp, 2) == 0) {
1329 VLerror(yylloc, "Invalid `timescale unit scale");
1331 VLerror(yylloc, "Invalid `timescale precision scale");
1338 * process either a pull0 or a pull1.
1340 static void process_ucdrive(const char*txt)
1342 UCDriveType ucd = UCD_NONE;
1343 const char*cp = txt + strspn(txt, " \t");
1345 /* Skip the space after the `unconnected_drive directive. */
1347 VLerror(yylloc, "Space required after `unconnected_drive "
1352 /* Check for the pull keyword. */
1353 if (strncmp("pull", cp, 4) != 0) {
1354 VLerror(yylloc, "pull required for `unconnected_drive "
1359 if (*cp == '0') ucd = UCD_PULL0;
1360 else if (*cp == '1') ucd = UCD_PULL1;
1362 cerr << yylloc.text << ":" << yylloc.first_line << ": error: "
1363 "`unconnected_drive does not support 'pull" << *cp
1370 /* Verify that only space and/or a single line comment is left. */
1371 cp += strspn(cp, " \t");
1372 if (strncmp(cp, "//", 2) != 0 &&
1373 (size_t)(cp-yytext) != strlen(yytext)) {
1374 VLerror(yylloc, "Invalid `unconnected_drive directive (extra "
1375 "garbage after precision).");
1383 * The timescale parameter has the form:
1384 * " <num> xs / <num> xs"
1386 static void process_timescale(const char*txt)
1388 const char*cp = txt + strspn(txt, " \t");
1390 /* Skip the space after the `timescale directive. */
1392 VLerror(yylloc, "Space required after `timescale directive.");
1399 /* Get the time units. */
1400 if (get_timescale_const(cp, unit, true)) return;
1402 /* Skip any space after the time units, the '/' and any
1403 * space after the '/'. */
1404 cp += strspn(cp, " \t");
1406 VLerror(yylloc, "`timescale separator '/' appears to be missing.");
1410 cp += strspn(cp, " \t");
1412 /* Get the time precision. */
1413 if (get_timescale_const(cp, prec, false)) return;
1415 /* Verify that only space and/or a single line comment is left. */
1416 cp += strspn(cp, " \t");
1417 if (strncmp(cp, "//", 2) != 0 &&
1418 (size_t)(cp-yytext) != strlen(yytext)) {
1419 VLerror(yylloc, "Invalid `timescale directive (extra garbage "
1420 "after precision).");
1424 /* The time unit must be greater than or equal to the precision. */
1426 VLerror(yylloc, "error: `timescale unit must not be less than "
1431 pform_set_timescale(unit, prec, yylloc.text, yylloc.first_line);
1440 * The line directive matches lines of the form #line "foo" N and
1441 * calls this function. Here I parse out the file name and line
1442 * number, and change the yylloc to suite.
1444 static void line_directive()
1447 /* Skip any leading space. */
1448 char *cp = strchr(yytext, '#');
1449 /* Skip the #line directive. */
1450 assert(strncmp(cp, "#line", 5) == 0);
1452 /* Skip the space after the #line directive. */
1453 cp += strspn(cp, " \t");
1455 /* Find the starting " and skip it. */
1456 char*fn_start = strchr(cp, '"');
1457 if (cp != fn_start) {
1458 VLerror(yylloc, "Invalid #line directive (file name start).");
1463 /* Find the last ". */
1464 char*fn_end = strrchr(fn_start, '"');
1466 VLerror(yylloc, "Invalid #line directive (file name end).");
1470 /* Copy the file name and assign it to yylloc. */
1471 char*buf = new char[fn_end-fn_start+1];
1472 strncpy(buf, fn_start, fn_end-fn_start);
1473 buf[fn_end-fn_start] = 0;
1475 /* Skip the space after the file name. */
1479 cpr += strspn(cp, " \t");
1481 VLerror(yylloc, "Invalid #line directive (missing space after "
1488 /* Get the line number and verify that it is correct. */
1489 unsigned long lineno = strtoul(cp, &cpr, 10);
1491 VLerror(yylloc, "Invalid line number for #line directive.");
1497 /* Verify that only space is left. */
1498 cpr += strspn(cp, " \t");
1499 if ((size_t)(cpr-yytext) != strlen(yytext)) {
1500 VLerror(yylloc, "Invalid #line directive (extra garbage after "
1506 /* Now we can assign the new values to yyloc. */
1507 yylloc.text = set_file_name(buf);
1508 yylloc.first_line = lineno;
1512 * The line directive matches lines of the form `line N "foo" M and
1513 * calls this function. Here I parse out the file name and line
1514 * number, and change the yylloc to suite. M is ignored.
1516 static void line_directive2()
1519 /* Skip any leading space. */
1520 char *cp = strchr(yytext, '`');
1521 /* Skip the `line directive. */
1522 assert(strncmp(cp, "`line", 5) == 0);
1525 /* strtoul skips leading space. */
1526 unsigned long lineno = strtoul(cp, &cpr, 10);
1528 VLerror(yylloc, "Invalid line number for `line directive.");
1534 /* Skip the space between the line number and the file name. */
1535 cpr += strspn(cp, " \t");
1537 VLerror(yylloc, "Invalid `line directive (missing space after "
1543 /* Find the starting " and skip it. */
1544 char*fn_start = strchr(cp, '"');
1545 if (cp != fn_start) {
1546 VLerror(yylloc, "Invalid `line directive (file name start).");
1551 /* Find the last ". */
1552 char*fn_end = strrchr(fn_start, '"');
1554 VLerror(yylloc, "Invalid `line directive (file name end).");
1558 /* Skip the space after the file name. */
1561 cpr += strspn(cp, " \t");
1563 VLerror(yylloc, "Invalid `line directive (missing space after "
1569 /* Check that the level is correct, we do not need the level. */
1570 if (strspn(cp, "012") != 1) {
1571 VLerror(yylloc, "Invalid level for `line directive.");
1576 /* Verify that only space and/or a single line comment is left. */
1577 cp += strspn(cp, " \t");
1578 if (strncmp(cp, "//", 2) != 0 &&
1579 (size_t)(cp-yytext) != strlen(yytext)) {
1580 VLerror(yylloc, "Invalid `line directive (extra garbage after "
1585 /* Copy the file name and assign it and the line number to yylloc. */
1586 char*buf = new char[fn_end-fn_start+1];
1587 strncpy(buf, fn_start, fn_end-fn_start);
1588 buf[fn_end-fn_start] = 0;
1590 yylloc.text = set_file_name(buf);
1591 yylloc.first_line = lineno;
1595 * Reset all compiler directives. This will be called when a `resetall
1596 * directive is encountered or when a new compilation unit is started.
1598 static void reset_all()
1600 pform_set_default_nettype(NetNet::WIRE, yylloc.text, yylloc.first_line);
1601 in_celldefine = false;
1602 uc_drive = UCD_NONE;
1603 pform_set_timescale(def_ts_units, def_ts_prec, 0, 0);
1606 extern FILE*vl_input;
1609 yyrestart(vl_input);
1610 yylloc.first_line = 1;
1612 /* Announce the first file name. */
1613 yylloc.text = set_file_name(strdupnew(vl_file.c_str()));
1615 if (separate_compilation) {
1617 if (!keyword_mask_stack.empty()) {
1618 lexor_keyword_mask = keyword_mask_stack.back();
1619 keyword_mask_stack.clear();
1625 * Modern version of flex (>=2.5.9) can clean up the scanner data.
1627 void destroy_lexor()
1629 # ifdef FLEX_SCANNER
1630 # if YY_FLEX_MAJOR_VERSION >= 2 && YY_FLEX_MINOR_VERSION >= 5
1631 # if YY_FLEX_MINOR_VERSION > 5 || defined(YY_FLEX_SUBMINOR_VERSION) && YY_FLEX_SUBMINOR_VERSION >= 9