Fix for assertion error when expanding macro.
[iverilog.git] / lexor.lex
blob6a93cc283c7778232560696a92f436e7b345ac64
2 %option never-interactive
4 %{
5 /*
6  * Copyright (c) 1998-2000 Stephen Williams (steve@icarus.com)
7  *
8  *    This source code is free software; you can redistribute it
9  *    and/or modify it in source code form under the terms of the GNU
10  *    General Public License as published by the Free Software
11  *    Foundation; either version 2 of the License, or (at your option)
12  *    any later version.
13  *
14  *    This program is distributed in the hope that it will be useful,
15  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *    GNU General Public License for more details.
18  *
19  *    You should have received a copy of the GNU General Public License
20  *    along with this program; if not, write to the Free Software
21  *    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22  */
23 #ifdef HAVE_CVS_IDENT
24 #ident "$Id: lexor.lex,v 1.96 2007/06/14 03:50:00 steve Exp $"
25 #endif
27 # include "config.h"
29       //# define YYSTYPE lexval
31 # include  <iostream>
32 # include  "compiler.h"
33 # include  "parse_misc.h"
34 # include  "parse_api.h"
35 # include  "parse.h"
36 # include  <ctype.h>
37 # include  <string.h>
38 # include  "lexor_keyword.h"
41 # define YY_USER_INIT reset_lexor();
42 # define yylval VLlval
45  * Lexical location information is passed in the yylloc variable to th
46  * parser. The file names, strings, are kept in a list so that I can
47  * re-use them. The set_file_name function will return a pointer to
48  * the name as it exists in the list (and delete the passed string.)
49  * If the name is new, it will be added to the list.
50  */
51 extern YYLTYPE yylloc;
53 struct file_name_cell {
54       const char*text;
55       struct file_name_cell*next;
56       bool library_flag;
59 static struct file_name_cell*file_names = 0;
61 static const char* set_file_name(char*text)
63       struct file_name_cell*cur = file_names;
64       while (cur) {
65             if (strcmp(cur->text, text) == 0) {
66                   delete[]text;
67                   return cur->text;
68             }
70             cur = cur->next;
71       }
73       cur = new struct file_name_cell;
74       cur->text = text;
75       cur->next = file_names;
77         /* Check this file name with the list of library file
78            names. If there is a match, then turn on the
79            pform_library_flag. This is how the parser knows that
80            modules declared in this file are library modules. */
81       cur->library_flag = library_file_map[cur->text];
82       pform_library_flag = cur->library_flag;
83       return text;
87 extern void pform_set_timescale(int, int, const char*file, unsigned line);
89 void reset_lexor();
90 static void line_directive();
91 static void line_directive2();
93 static verinum*make_unsized_binary(const char*txt);
94 static verinum*make_undef_highz_dec(const char*txt);
95 static verinum*make_unsized_dec(const char*txt);
96 static verinum*make_unsized_octal(const char*txt);
97 static verinum*make_unsized_hex(const char*txt);
99 static int dec_buf_div2(char *buf);
101 static void process_timescale(const char*txt);
103 static int comment_enter;
106 %x CCOMMENT
107 %x PCOMMENT
108 %x LCOMMENT
109 %x CSTRING
110 %s UDPTABLE
111 %x PPTIMESCALE
112 %x PPDEFAULT_NETTYPE
113 %s EDGES
115 W [ \t\b\f\r]+
119 ^"#line"[ ]+\"[^\"]*\"[ ]+[0-9]+.* { line_directive(); }
120 ^"`line"[ ]+[0-9]+[ ]+\"[^\"]*\".* { line_directive2(); }
122 [ \t\b\f\r] { ; }
123 \n { yylloc.first_line += 1; }
125   /* C++ style comments start with / / and run to the end of the
126      current line. These are very easy to handle. */
128 "//".* { comment_enter = YY_START; BEGIN(LCOMMENT); }
129 <LCOMMENT>.    { yymore(); }
130 <LCOMMENT>\n   { yylloc.first_line += 1; BEGIN(comment_enter); }
133   /* The contents of C-style comments are ignored, like white space. */
135 "/*" { comment_enter = YY_START; BEGIN(CCOMMENT); }
136 <CCOMMENT>.    { yymore(); }
137 <CCOMMENT>\n   { yylloc.first_line += 1; yymore(); }
138 <CCOMMENT>"*/" { BEGIN(comment_enter); }
141 "(*" { return K_PSTAR; }
142 "*)" { return K_STARP; }
143 "<<" { return K_LS; }
144 "<<<" { return K_LS; /* Note: Functionally, <<< is the same as <<. */}
145 ">>"  { return K_RS; }
146 ">>>" { return K_RSS; }
147 "**" { return K_POW; }
148 "<=" { return K_LE; }
149 ">=" { return K_GE; }
150 "=>" { return K_EG; }
151 "*>" { return K_SG; }
152 "==" { return K_EQ; }
153 "!=" { return K_NE; }
154 "===" { return K_CEQ; }
155 "!==" { return K_CNE; }
156 "||" { return K_LOR; }
157 "&&" { return K_LAND; }
158 "&&&" { return K_TAND; }
159 "~|" { return K_NOR; }
160 "~^" { return K_NXOR; }
161 "^~" { return K_NXOR; }
162 "~&" { return K_NAND; }
163 "->" { return K_TRIGGER; }
164 "+:" { return K_PO_POS; }
165 "-:" { return K_PO_NEG; }
167   /* Watch out for the tricky case of (*). Cannot parse this as "(*"
168      and ")", but since I know that this is really ( * ), replace it
169      with "*" and return that. */
170 "("{W}*"*"{W}*")" { return '*'; }
172 <EDGES>"]" { BEGIN(0); return yytext[0]; }
173 [}{;:\[\],()#=.@&!?<>%|^~+*/-] { return yytext[0]; }
175 \"            { BEGIN(CSTRING); }
176 <CSTRING>\\\\ { yymore(); /* Catch \\, which is a \ escaping itself */ }
177 <CSTRING>\\\" { yymore(); /* Catch \", which is an escaped quote */ }
178 <CSTRING>\n   { BEGIN(0);
179                 yylval.text = strdup(yytext);
180                 VLerror(yylloc, "Missing close quote of string.");
181                 yylloc.first_line += 1;
182                 return STRING; }
183 <CSTRING>\"   { BEGIN(0);
184                 yylval.text = strdup(yytext);
185                 yylval.text[strlen(yytext)-1] = 0;
186                 return STRING; }
187 <CSTRING>.    { yymore(); }
189 <UDPTABLE>\(\?0\)    { return '_'; }
190 <UDPTABLE>\(\?1\)    { return '+'; }
191 <UDPTABLE>\(\?[xX]\) { return '%'; }
192 <UDPTABLE>\(\?\?\)  { return '*'; }
193 <UDPTABLE>\(01\)    { return 'r'; }
194 <UDPTABLE>\(0[xX]\) { return 'Q'; }
195 <UDPTABLE>\(b[xX]\) { return 'q'; }
196 <UDPTABLE>\(b0\)    { return 'f'; /* b0 is 10|00, but only 10 is meaningful */}
197 <UDPTABLE>\(b1\)    { return 'r'; /* b1 is 11|01, but only 01 is meaningful */}
198 <UDPTABLE>\(0\?\)   { return 'P'; }
199 <UDPTABLE>\(10\)    { return 'f'; }
200 <UDPTABLE>\(1[xX]\) { return 'M'; }
201 <UDPTABLE>\(1\?\)   { return 'N'; }
202 <UDPTABLE>\([xX]0\) { return 'F'; }
203 <UDPTABLE>\([xX]1\) { return 'R'; }
204 <UDPTABLE>\([xX]\?\) { return 'B'; }
205 <UDPTABLE>[bB]     { return 'b'; }
206 <UDPTABLE>[lL]     { return 'l'; /* IVL extension */ }
207 <UDPTABLE>[hH]     { return 'h'; /* IVL extension */ }
208 <UDPTABLE>[fF]     { return 'f'; }
209 <UDPTABLE>[rR]     { return 'r'; }
210 <UDPTABLE>[xX]     { return 'x'; }
211 <UDPTABLE>[nN]     { return 'n'; }
212 <UDPTABLE>[pP]     { return 'p'; }
213 <UDPTABLE>[01\?\*\-] { return yytext[0]; }
215 <EDGES>"01" { return K_edge_descriptor; }
216 <EDGES>"0x" { return K_edge_descriptor; }
217 <EDGES>"0z" { return K_edge_descriptor; }
218 <EDGES>"10" { return K_edge_descriptor; }
219 <EDGES>"1x" { return K_edge_descriptor; }
220 <EDGES>"1z" { return K_edge_descriptor; }
221 <EDGES>"x0" { return K_edge_descriptor; }
222 <EDGES>"x1" { return K_edge_descriptor; }
223 <EDGES>"z0" { return K_edge_descriptor; }
224 <EDGES>"z1" { return K_edge_descriptor; }
226 [a-zA-Z_][a-zA-Z0-9$_]* {
227       int rc = lexor_keyword_code(yytext, yyleng);
228       switch (rc) {
229           case IDENTIFIER:
230             yylval.text = strdup(yytext);
231             if (strncmp(yylval.text,"PATHPULSE$", 10) == 0)
232                   rc = PATHPULSE_IDENTIFIER;
233             break;
235           case K_bool:
236           case K_logic:
237           case K_wone:
238             if (! gn_cadence_types_enabled()) {
239                   yylval.text = strdup(yytext);
240                   rc = IDENTIFIER;
241             } else {
242                   yylval.text = 0;
243             }
244             break;
246           case K_edge:
247             BEGIN(EDGES);
248             break;
250           default:
251             yylval.text = 0;
252             break;
253       }
255       return rc;
259 \\[^ \t\b\f\r\n]+         {
260       yylval.text = strdup(yytext+1);
261       return IDENTIFIER; }
263 \$([a-zA-Z0-9$_]+)        {
264       if (strcmp(yytext,"$setuphold") == 0)
265             return K_Ssetuphold;
266       if (strcmp(yytext,"$attribute") == 0)
267             return KK_attribute;
268       if (strcmp(yytext,"$hold") == 0)
269             return K_Shold;
270       if (strcmp(yytext,"$period") == 0)
271             return K_Speriod;
272       if (strcmp(yytext,"$recovery") == 0)
273             return K_Srecovery;
274       if (strcmp(yytext,"$recrem") == 0)
275             return K_Srecrem;
276       if (strcmp(yytext,"$setup") == 0)
277             return K_Ssetup;
278       if (strcmp(yytext,"$width") == 0)
279             return K_Swidth;
280       yylval.text = strdup(yytext);
281       return SYSTEM_IDENTIFIER; }
284 \'[sS]?[dD][ \t]*[0-9][0-9_]*  { yylval.number = make_unsized_dec(yytext);
285                             return BASED_NUMBER; }
286 \'[sS]?[dD][ \t]*[xzXZ?]_* { yylval.number = make_undef_highz_dec(yytext);
287                              return BASED_NUMBER; }
288 \'[sS]?[bB][ \t]*[0-1xzXZ_\?]+ { yylval.number = make_unsized_binary(yytext);
289                         return BASED_NUMBER; }
290 \'[sS]?[oO][ \t]*[0-7xzXZ_\?]+ { yylval.number = make_unsized_octal(yytext);
291                         return BASED_NUMBER; }
292 \'[sS]?[hH][ \t]*[0-9a-fA-FxzXZ_\?]+ { yylval.number = make_unsized_hex(yytext);
293                               return BASED_NUMBER; }
295 [0-9][0-9_]* {
296       yylval.number = make_unsized_dec(yytext);
297       based_size = yylval.number->as_ulong();
298       return DEC_NUMBER; }
300 [0-9][0-9_]*\.[0-9][0-9_]*([Ee][+-]?[0-9][0-9_]*)? {
301       yylval.realtime = new verireal(yytext);
302       return REALTIME; }
304 [0-9][0-9_]*[Ee][+-]?[0-9][0-9_]* {
305       yylval.realtime = new verireal(yytext);
306       return REALTIME; }
309   /* Notice and handle the timescale directive. */
311 ^{W}?`timescale { BEGIN(PPTIMESCALE); }
312 <PPTIMESCALE>.* { process_timescale(yytext); }
313 <PPTIMESCALE>\n {
314       yylloc.first_line += 1;
315       BEGIN(0); }
318   /* These are directives that I do not yet support. I think that IVL
319      should handle these, not an external preprocessor. */
321 ^{W}?`celldefine{W}?.*           {  }
322 ^{W}?`delay_mode_distributed{W}?.*  {  }
323 ^{W}?`delay_mode_unit{W}?.*      {  }
324 ^{W}?`delay_mode_path{W}?.*      {  }
325 ^{W}?`disable_portfaults{W}?.*   {  }
326 ^{W}?`enable_portfaults{W}?.*    {  }
327 ^{W}?`endcelldefine{W}?.*        {  }
328 `endprotect                      {  }
329 ^{W}?`nosuppress_faults{W}?.*    {  }
330 ^{W}?`nounconnected_drive{W}?.*  {  }
331 `protect                         {  }
332 ^{W}?`resetall{W}?.*             {  }
333 ^{W}?`suppress_faults{W}?.*      {  }
334 ^{W}?`unconnected_drive{W}?.*    {  }
335 ^{W}?`uselib{W}?.*               {  }
337   /* Notice and handle the default_nettype directive. The lexor
338      detects the default_nettype keyword, and the second part of the
339      rule collects the rest of the line and processes it. We only need
340      to look for the first work, and interpret it. */
342 `default_nettype{W}? { BEGIN(PPDEFAULT_NETTYPE); }
343 <PPDEFAULT_NETTYPE>.* {
344       NetNet::Type net_type;
345       size_t wordlen = strcspn(yytext, " \t\f\r\n");
346       yytext[wordlen] = 0;
347       if (strcmp(yytext,"wire") == 0) {
348             net_type = NetNet::WIRE;
350       } else if (strcmp(yytext,"none") == 0) {
351             net_type = NetNet::NONE;
353       } else {
354             cerr << yylloc.text << ":" << yylloc.first_line
355                  << " error: Net type " << yytext
356                  << " is not a valid (and supported)"
357                  << " default net type." << endl;
358             net_type = NetNet::WIRE;
359             error_count += 1;
360       }
361       pform_set_default_nettype(net_type, yylloc.text, yylloc.first_line);
363 <PPDEFAULT_NETTYPE>\n {
364       yylloc.first_line += 1;
365       BEGIN(0); }
368   /* These are directives that are not supported by me and should have
369      been handled by an external preprocessor such as ivlpp. */
371 ^{W}?`define{W}?.* {
372       cerr << yylloc.text << ":" << yylloc.first_line <<
373             ": `define not supported. Use an external preprocessor."
374            << endl;
375   }
377 ^{W}?`else{W}?.* {
378       cerr << yylloc.text << ":" << yylloc.first_line <<
379             ": `else not supported. Use an external preprocessor."
380            << endl;
381   }
383 ^{W}?`endif{W}?.* {
384       cerr << yylloc.text << ":" << yylloc.first_line <<
385             ": `endif not supported. Use an external preprocessor."
386            << endl;
387   }
389 ^{W}?`ifdef{W}?.* {
390       cerr << yylloc.text << ":" << yylloc.first_line <<
391             ": `ifdef not supported. Use an external preprocessor."
392            << endl;
393   }
395 ^`include{W}?.* {
396       cerr << yylloc.text << ":" << yylloc.first_line <<
397             ": `include not supported. Use an external preprocessor."
398            << endl;
399   }
401 ^`undef{W}?.* {
402       cerr << yylloc.text << ":" << yylloc.first_line <<
403             ": `undef not supported. Use an external preprocessor."
404            << endl;
405   }
408 `{W} { cerr << yylloc.text << ":" << yylloc.first_line << ": error: "
409             << "Stray tic (`) here. Perhaps you put white space" << endl;
410        cerr << yylloc.text << ":" << yylloc.first_line << ":      : "
411             << "between the tic and preprocessor directive?"
412             << endl;
413        error_count += 1; }
415   /* Final catchall. something got lost or mishandled. */
417 . {   cerr << yylloc.text << ":" << yylloc.first_line
418            << ": error: unmatched character (";
419       if (isgraph(yytext[0]))
420             cerr << yytext[0];
421       else
422             cerr << "hex " << hex << (0xffU & ((unsigned) (yytext[0])));
424       cerr << ")" << endl;
425       error_count += 1; }
430  * The UDP state table needs some slightly different treatment by the
431  * lexor. The level characters are normally accepted as other things,
432  * so the parser needs to switch my mode when it believes in needs to.
433  */
434 void lex_start_table()
436       BEGIN(UDPTABLE);
439 void lex_end_table()
441       BEGIN(INITIAL);
444 static verinum*make_unsized_binary(const char*txt)
446       bool sign_flag = false;
447       const char*ptr = txt;
448       assert(*ptr == '\'');
449       ptr += 1;
451       if (tolower(*ptr) == 's') {
452             sign_flag = true;
453             ptr += 1;
454       }
456       assert(tolower(*ptr) == 'b');
457       ptr += 1;
459       while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
460             ptr += 1;
462       unsigned size = 0;
463       for (const char*idx = ptr ;  *idx ;  idx += 1)
464             if (*idx != '_') size += 1;
466       if ((based_size > 0) && (size > based_size)) yywarn(yylloc,
467           "extra digits given for sized binary constant.");
469       verinum::V*bits = new verinum::V[size];
471       unsigned idx = size;
472       while (*ptr) {
473             switch (ptr[0]) {
474                 case '0':
475                   bits[--idx] = verinum::V0;
476                   break;
477                 case '1':
478                   bits[--idx] = verinum::V1;
479                   break;
480                 case 'z': case 'Z': case '?':
481                   bits[--idx] = verinum::Vz;
482                   break;
483                 case 'x': case 'X':
484                   bits[--idx] = verinum::Vx;
485                   break;
486                   case '_':
487                   break;
488                 default:
489                   fprintf(stderr, "%c\n", ptr[0]);
490                   assert(0);
491             }
492             ptr += 1;
493       }
495       verinum*out = new verinum(bits, size, false);
496       out->has_sign(sign_flag);
497       delete[]bits;
498       return out;
502 static verinum*make_unsized_octal(const char*txt)
504       bool sign_flag = false;
505       const char*ptr = txt;
506       assert(*ptr == '\'');
507       ptr += 1;
509       if (tolower(*ptr) == 's') {
510             sign_flag = true;
511             ptr += 1;
512       }
514       assert(tolower(*ptr) == 'o');
515       ptr += 1;
517       while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
518             ptr += 1;
520       unsigned size = 0;
521       for (const char*idx = ptr ;  *idx ;  idx += 1)
522             if (*idx != '_') size += 3;
524       if (based_size > 0) {
525             int rem = based_size % 3;
526             if (rem != 0) based_size += 3 - rem;
527             if (size > based_size) yywarn(yylloc,
528                 "extra digits given for sized octal constant.");
529       }
531       verinum::V*bits = new verinum::V[size];
533       unsigned idx = size;
534       while (*ptr) {
535             unsigned val;
536             switch (ptr[0]) {
537                 case '0': case '1': case '2': case '3':
538                 case '4': case '5': case '6': case '7':
539                   val = *ptr - '0';
540                   bits[--idx] = (val&4) ? verinum::V1 : verinum::V0;
541                   bits[--idx] = (val&2) ? verinum::V1 : verinum::V0;
542                   bits[--idx] = (val&1) ? verinum::V1 : verinum::V0;
543                   break;
544                 case 'x': case 'X':
545                   bits[--idx] = verinum::Vx;
546                   bits[--idx] = verinum::Vx;
547                   bits[--idx] = verinum::Vx;
548                   break;
549                 case 'z': case 'Z': case '?':
550                   bits[--idx] = verinum::Vz;
551                   bits[--idx] = verinum::Vz;
552                   bits[--idx] = verinum::Vz;
553                   break;
554                 case '_':
555                   break;
556                 default:
557                   assert(0);
558             }
559             ptr += 1;
560       }
562       verinum*out = new verinum(bits, size, false);
563       out->has_sign(sign_flag);
564       delete[]bits;
565       return out;
569 static verinum*make_unsized_hex(const char*txt)
571       bool sign_flag = false;
572       const char*ptr = txt;
573       assert(*ptr == '\'');
574       ptr += 1;
576       if (tolower(*ptr) == 's') {
577             sign_flag = true;
578             ptr += 1;
579       }
580       assert(tolower(*ptr) == 'h');
582       ptr += 1;
583       while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
584             ptr += 1;
586       unsigned size = 0;
587       for (const char*idx = ptr ;  *idx ;  idx += 1)
588             if (*idx != '_') size += 4;
590       if (based_size > 0) {
591             int rem = based_size % 4;
592             if (rem != 0) based_size += 4 - rem;
593             if (size > based_size) yywarn(yylloc,
594                 "extra digits given for sized hex constant.");
595       }
597       verinum::V*bits = new verinum::V[size];
599       unsigned idx = size;
600       while (*ptr) {
601             unsigned val;
602             switch (ptr[0]) {
603                 case '0': case '1': case '2': case '3': case '4':
604                 case '5': case '6': case '7': case '8': case '9':
605                   val = *ptr - '0';
606                   bits[--idx] = (val&8) ? verinum::V1 : verinum::V0;
607                   bits[--idx] = (val&4) ? verinum::V1 : verinum::V0;
608                   bits[--idx] = (val&2) ? verinum::V1 : verinum::V0;
609                   bits[--idx] = (val&1) ? verinum::V1 : verinum::V0;
610                   break;
611                 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
612                 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
613                   val = tolower(*ptr) - 'a' + 10;
614                   bits[--idx] = (val&8) ? verinum::V1 : verinum::V0;
615                   bits[--idx] = (val&4) ? verinum::V1 : verinum::V0;
616                   bits[--idx] = (val&2) ? verinum::V1 : verinum::V0;
617                   bits[--idx] = (val&1) ? verinum::V1 : verinum::V0;
618                   break;
619                 case 'x': case 'X':
620                   bits[--idx] = verinum::Vx;
621                   bits[--idx] = verinum::Vx;
622                   bits[--idx] = verinum::Vx;
623                   bits[--idx] = verinum::Vx;
624                   break;
625                 case 'z': case 'Z': case '?':
626                   bits[--idx] = verinum::Vz;
627                   bits[--idx] = verinum::Vz;
628                   bits[--idx] = verinum::Vz;
629                   bits[--idx] = verinum::Vz;
630                   break;
631                 case '_':
632                   break;
633                 default:
634                   assert(0);
635             }
636             ptr += 1;
637       }
639       verinum*out = new verinum(bits, size, false);
640       out->has_sign(sign_flag);
641       delete[]bits;
642       return out;
646 /* Divide the integer given by the string by 2. Return the remainder bit. */
647 static int dec_buf_div2(char *buf)
649     int partial;
650     int len = strlen(buf);
651     char *dst_ptr;
652     int pos;
654     partial = 0;
655     pos = 0;
657     /* dst_ptr overwrites buf, but all characters that are overwritten
658        were already used by the reader. */
659     dst_ptr = buf;
661     while(buf[pos] == '0')
662         ++pos;
664     for(; pos<len; ++pos){
665         if (buf[pos]=='_')
666             continue;
668         assert(isdigit(buf[pos]));
670         partial= partial*10 + (buf[pos]-'0');
672         if (partial >= 2){
673             *dst_ptr = partial/2 + '0';
674             partial = partial & 1;
676             ++dst_ptr;
677         }
678         else{
679             *dst_ptr = '0';
680             ++dst_ptr;
681         }
682     }
684     // If result of division was zero string, it should remain that way.
685     // Don't eat the last zero...
686     if (dst_ptr == buf){
687         *dst_ptr = '0';
688         ++dst_ptr;
689     }
690     *dst_ptr = 0;
692     return partial;
695 /* Support a single x, z or ? as a decimal constant (from 1364-2005). */
696 static verinum* make_undef_highz_dec(const char* ptr)
698       bool signed_flag = false;
700       assert(*ptr == '\'');
701       /* The number may have decorations of the form 'sd<code>,
702          possibly with space between the d and the <code>.
703          Also, the 's' is optional, and marks the number as signed. */
704       ptr += 1;
706       if (tolower(*ptr) == 's') {
707           signed_flag = true;
708           ptr += 1;
709       }
711       assert(tolower(*ptr) == 'd');
712       ptr += 1;
714       while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
715           ptr += 1;
717         /* Process the code. */
718       verinum::V* bits = new verinum::V[1];
719       switch (*ptr) {
720           case 'x':
721           case 'X':
722             bits[0] = verinum::Vx;
723             break;
724           case 'z':
725           case 'Z':
726           case '?':
727             bits[0] = verinum::Vz;
728             break;
729           default:
730             assert(0);
731       }
732       ptr += 1;
733       while (*ptr == '_') ptr += 1;
734       assert(*ptr == 0);
736       verinum*out = new verinum(bits, 1, false);
737       out->has_sign(signed_flag);
738       delete[]bits;
739       return out;
743  * Making a decimal number is much easier then the other base numbers
744  * because there are no z or x values to worry about. It is much
745  * harder then other base numbers because the width needed in bits is
746  * hard to calculate.
747  */
749 static verinum*make_unsized_dec(const char*ptr)
751       char buf[4096];
752       bool signed_flag = false;
753       unsigned idx;
755       if (ptr[0] == '\'') {
756               /* The number has decorations of the form 'sd<digits>,
757                  possibly with space between the d and the <digits>.
758                  Also, the 's' is optional, and markes the number as
759                  signed. */
760             ptr += 1;
762             if (tolower(*ptr) == 's') {
763                   signed_flag = true;
764                   ptr += 1;
765             }
767             assert(tolower(*ptr) == 'd');
768             ptr += 1;
770             while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
771                   ptr += 1;
773       } else {
774               /* ... or an undecorated decimal number is passed
775                  it. These numbers are treated as signed decimal. */
776             assert(isdigit(*ptr));
777             signed_flag = true;
778       }
781         /* Copy the digits into a buffer that I can use to do in-place
782            decimal divides. */
783       idx = 0;
784       while ((idx < sizeof buf) && (*ptr != 0)) {
785             if (*ptr == '_') {
786                   ptr += 1;
787                   continue;
788             }
790             buf[idx++] = *ptr++;
791       }
793       if (idx == sizeof buf) {
794             fprintf(stderr, "Ridiculously long"
795                     " decimal constant will be truncated!\n");
796             idx -= 1;
797       }
799       buf[idx] = 0;
800       unsigned tmp_size = idx * 4 + 1;
801       verinum::V *bits = new verinum::V[tmp_size];
803       idx = 0;
804       while (idx < tmp_size) {
805             int rem = dec_buf_div2(buf);
806             bits[idx++] = (rem == 1) ? verinum::V1 : verinum::V0;
807       }
809       assert(strcmp(buf, "0") == 0);
811         /* Now calculate the minimum number of bits needed to
812            represent this unsigned number. */
813       unsigned size = tmp_size;
814       while ((size > 1) && (bits[size-1] == verinum::V0))
815             size -= 1;
817         /* Now account for the signedness. Don't leave a 1 in the high
818            bit if this is a signed number. */
819       if (signed_flag && (bits[size-1] == verinum::V1)) {
820             size += 1;
821             assert(size <= tmp_size);
822       }
824         /* Since we never have the real number of bits that a decimal
825            number represents we do not check for extra bits. */
826 //      if (based_size > 0) { }
828       verinum*res = new verinum(bits, size, false);
829       res->has_sign(signed_flag);
831       delete[]bits;
832       return res;
837  * The timescale parameter has the form:
838  *      " <num> xs / <num> xs"
839  */
840 static void process_timescale(const char*txt)
842       unsigned num;
843       const char*cp = txt + strspn(txt, " \t");
844       char*tmp;
845       const char*ctmp;
847       int unit = 0;
848       int prec = 0;
850       num = strtoul(cp, &tmp, 10);
851       if (num == 0) {
852             VLerror(yylloc, "Invalid timescale string.");
853             return;
854       }
856       while (num >= 10) {
857             unit += 1;
858             num  /= 10;
859       }
860       if (num != 1) {
861             VLerror(yylloc, "Invalid timescale unit number.");
862             return;
863       }
865       cp = tmp;
866       cp += strspn(cp, " \t");
867       ctmp = cp + strcspn(cp, " \t/");
869       if (strncmp("s", cp, ctmp-cp) == 0) {
870             unit -= 0;
872       } else if (strncmp("ms", cp, ctmp-cp) == 0) {
873             unit -= 3;
875       } else if (strncmp("us", cp, ctmp-cp) == 0) {
876             unit -= 6;
878       } else if (strncmp("ns", cp, ctmp-cp) == 0) {
879             unit -= 9;
881       } else if (strncmp("ps", cp, ctmp-cp) == 0) {
882             unit -= 12;
884       } else if (strncmp("fs", cp, ctmp-cp) == 0) {
885             unit -= 15;
887       } else {
888             VLerror(yylloc, "Invalid timescale unit of measurement");
889             return;
890       }
892       cp = ctmp;
893       cp += strspn(cp, " \t/");
895       num = strtoul(cp, &tmp, 10);
896       if (num == 0) {
897             VLerror(yylloc, "Invalid timescale string.");
898             return;
899       }
900       assert(num);
901       while (num >= 10) {
902             prec += 1;
903             num  /= 10;
904       }
905       if (num != 1) {
906             VLerror(yylloc, "Invalid timescale precision number.");
907             return;
908       }
910       cp = tmp;
911       cp += strspn(cp, " \t");
912       ctmp = cp + strcspn(cp, " \t\r");
914       if (strncmp("s", cp, ctmp-cp) == 0) {
915             prec -= 0;
917       } else if (strncmp("ms", cp, ctmp-cp) == 0) {
918             prec -= 3;
920       } else if (strncmp("us", cp, ctmp-cp) == 0) {
921             prec -= 6;
923       } else if (strncmp("ns", cp, ctmp-cp) == 0) {
924             prec -= 9;
926       } else if (strncmp("ps", cp, ctmp-cp) == 0) {
927             prec -= 12;
929       } else if (strncmp("fs", cp, ctmp-cp) == 0) {
930             prec -= 15;
932       } else {
933             VLerror(yylloc, "Invalid timescale precision units of measurement");
934             return;
935       }
937       pform_set_timescale(unit, prec, yylloc.text, yylloc.first_line);
940 int yywrap()
942       return 1;
946  * The line directive matches lines of the form #line "foo" N and
947  * calls this function. Here I parse out the file name and line
948  * number, and change the yylloc to suite.
949  */
950 static void line_directive()
952       char*qt1 = strchr(yytext, '"');
953       assert(qt1);
954       qt1 += 1;
956       char*qt2 = strchr(qt1, '"');
957       assert(qt2);
959       char*buf = new char[qt2-qt1+1];
960       strncpy(buf, qt1, qt2-qt1);
961       buf[qt2-qt1] = 0;
963       yylloc.text = set_file_name(buf);
965       qt2 += 1;
966       yylloc.first_line = strtoul(qt2,0,0);
969 static void line_directive2()
971       assert(strncmp(yytext,"`line",5) == 0);
972       char*cp = yytext + strlen("`line");
973       cp += strspn(cp, " ");
974       yylloc.first_line = strtoul(cp,&cp,10);
976       yylloc.first_line -= 1;
978       cp += strspn(cp, " ");
979       if (*cp == 0) return;
981       char*qt1 = strchr(yytext, '"');
982       assert(qt1);
983       qt1 += 1;
985       char*qt2 = strchr(qt1, '"');
986       assert(qt2);
988       char*buf = new char[qt2-qt1+1];
989       strncpy(buf, qt1, qt2-qt1);
990       buf[qt2-qt1] = 0;
992       yylloc.text = set_file_name(buf);
995 extern FILE*vl_input;
996 void reset_lexor()
998       yyrestart(vl_input);
999       yylloc.first_line = 1;
1001         /* Start the file_names list. From here on, as I get a file
1002            name, I will add it to this list. Only add the name if it
1003            is not already in the list. */
1004       file_names = new struct file_name_cell;
1005       file_names->text = strdup(vl_file.c_str());
1006       file_names->next = 0;
1007       yylloc.text = file_names->text;