abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cbaReadVer.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [cbaReadVer.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Verilog parser.]
8 
9  Synopsis [Parses several flavors of word-level Verilog.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - November 29, 2014.]
16 
17  Revision [$Id: cbaReadVer.c,v 1.00 2014/11/29 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "cba.h"
22 #include "cbaPrs.h"
23 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 // Verilog keywords
31 typedef enum {
32  CBA_VER_NONE = 0, // 0: unused
33  CBA_VER_MODULE, // 1: module
34  CBA_VER_INOUT, // 2: inout
35  CBA_VER_INPUT, // 3: input
36  CBA_VER_OUTPUT, // 4: output
37  CBA_VER_WIRE, // 5: wire
38  CBA_VER_ASSIGN, // 6: assign
39  CBA_VER_REG, // 7: reg
40  CBA_VER_ALWAYS, // 8: always
41  CBA_VER_DEFPARAM, // 9: always
42  CBA_VER_BEGIN, // 10: begin
43  CBA_VER_END, // 11: end
44  CBA_VER_ENDMODULE, // 12: endmodule
45  CBA_VER_UNKNOWN // 13: unknown
47 
48 const char * s_VerTypes[CBA_VER_UNKNOWN+1] = {
49  NULL, // 0: unused
50  "module", // 1: module
51  "inout", // 2: inout
52  "input", // 3: input
53  "output", // 4: output
54  "wire", // 5: wire
55  "assign", // 6: assign
56  "reg", // 7: reg
57  "always", // 8: always
58  "defparam", // 9: defparam
59  "begin", // 10: begin
60  "end", // 11: end
61  "endmodule", // 12: endmodule
62  NULL // 13: unknown
63 };
64 
65 static inline void Cba_PrsAddVerilogDirectives( Cba_Prs_t * p )
66 {
67  int i;
68  for ( i = 1; s_VerTypes[i]; i++ )
69  Abc_NamStrFindOrAdd( p->pDesign->pNames, (char *)s_VerTypes[i], NULL );
71 }
72 
73 
74 // character recognition
75 static inline int Cba_IsSpace( char c ) { return (c == ' ' || c == '\t' || c == '\r' || c == '\n'); }
76 static inline int Cba_IsDigit( char c ) { return (c >= '0' && c <= '9'); }
77 static inline int Cba_IsDigitB( char c ) { return (c == '0' || c == '1' || c == 'x' || c == 'z'); }
78 static inline int Cba_IsDigitH( char c ) { return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); }
79 static inline int Cba_IsChar( char c ) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
80 static inline int Cba_IsSymb1( char c ) { return Cba_IsChar(c) || c == '_'; }
81 static inline int Cba_IsSymb2( char c ) { return Cba_IsSymb1(c) || Cba_IsDigit(c) || c == '$'; }
82 
83 static inline int Cba_PrsIsChar( Cba_Prs_t * p, char c ) { return *p->pCur == c; }
84 static inline int Cba_PrsIsChar1( Cba_Prs_t * p, char c ) { return p->pCur[1] == c; }
85 static inline int Cba_PrsIsDigit( Cba_Prs_t * p ) { return Cba_IsDigit(*p->pCur); }
86 
87 ////////////////////////////////////////////////////////////////////////
88 /// FUNCTION DEFINITIONS ///
89 ////////////////////////////////////////////////////////////////////////
90 
91 /**Function*************************************************************
92 
93  Synopsis []
94 
95  Description []
96 
97  SideEffects []
98 
99  SeeAlso []
100 
101 ***********************************************************************/
102 
103 // collect predefined modules names
104 const char * s_KnownModules[100] = {
105  NULL, // 0: unused
106  "const", // 1: constant
107  "buf", // 2: buffer
108  "not", // 3: inverter
109  "and", // 4: AND
110  "nand", // 5: OR
111  "or", // 6: XOR
112  "nor", // 7: NAND
113  "xor", // 8: NOR
114  "xnor", // 9: .XNOR
115  "mux", // 10: MUX
116  "maj", // 11: MAJ
117 
118  "VERIFIC_",
119  "wide_",
120  "reduce_",
121  "equal_",
122  "not_equal_",
123  "sub_",
124  "add_",
125  "mult_",
126  "mux_",
127  "Mux_",
128  "Select_",
129  "Decoder_",
130  "LessThan_",
131  "ReadPort_",
132  "WritePort_",
133  "ClockedWritePort_",
134  NULL
135 };
136 
137 // check if it is a known module
138 static inline int Cba_PrsIsKnownModule( Cba_Prs_t * p, char * pName )
139 {
140  int i;
141  for ( i = 1; s_KnownModules[i]; i++ )
142  if ( !strncmp(pName, s_KnownModules[i], strlen(s_KnownModules[i])) )
143  return i;
144  return 0;
145 }
146 
147 
148 /**Function*************************************************************
149 
150  Synopsis []
151 
152  Description []
153 
154  SideEffects []
155 
156  SeeAlso []
157 
158 ***********************************************************************/
159 
160 // skips Verilog comments (returns 1 if some comments were skipped)
161 static inline int Cba_PrsUtilSkipComments( Cba_Prs_t * p )
162 {
163  if ( !Cba_PrsIsChar(p, '/') )
164  return 0;
165  if ( Cba_PrsIsChar1(p, '/') )
166  {
167  for ( p->pCur += 2; p->pCur < p->pLimit; p->pCur++ )
168  if ( Cba_PrsIsChar(p, '\n') )
169  { p->pCur++; return 1; }
170  }
171  else if ( Cba_PrsIsChar1(p, '*') )
172  {
173  for ( p->pCur += 2; p->pCur < p->pLimit; p->pCur++ )
174  if ( Cba_PrsIsChar(p, '*') && Cba_PrsIsChar1(p, '/') )
175  { p->pCur++; p->pCur++; return 1; }
176  }
177  return 0;
178 }
179 static inline int Cba_PrsUtilSkipName( Cba_Prs_t * p )
180 {
181  if ( !Cba_PrsIsChar(p, '\\') )
182  return 0;
183  for ( p->pCur++; p->pCur < p->pLimit; p->pCur++ )
184  if ( Cba_PrsIsChar(p, ' ') )
185  { p->pCur++; return 1; }
186  return 0;
187 }
188 
189 // skip any number of spaces and comments
190 static inline int Cba_PrsUtilSkipSpaces( Cba_Prs_t * p )
191 {
192  while ( p->pCur < p->pLimit )
193  {
194  while ( Cba_IsSpace(*p->pCur) )
195  p->pCur++;
196  if ( !*p->pCur )
197  return Cba_PrsErrorSet(p, "Unexpectedly reached end-of-file.", 1);
198  if ( !Cba_PrsUtilSkipComments(p) )
199  return 0;
200  }
201  return Cba_PrsErrorSet(p, "Unexpectedly reached end-of-file.", 1);
202 }
203 // skip everything including comments until the given char
204 static inline int Cba_PrsUtilSkipUntil( Cba_Prs_t * p, char c )
205 {
206  while ( p->pCur < p->pLimit )
207  {
208  if ( Cba_PrsIsChar(p, c) )
209  return 1;
210  if ( Cba_PrsUtilSkipComments(p) )
211  continue;
212  if ( Cba_PrsUtilSkipName(p) )
213  continue;
214  p->pCur++;
215  }
216  return 0;
217 }
218 // skip everything including comments until the given word
219 static inline int Cba_PrsUtilSkipUntilWord( Cba_Prs_t * p, char * pWord )
220 {
221  char * pPlace = strstr( p->pCur, pWord );
222  if ( pPlace == NULL ) return 1;
223  p->pCur = pPlace + strlen(pWord);
224  return 0;
225 }
226 
227 /*
228 signal is a pair {NameId; RangeId}
229 if ( RangeId == 0 ) this is name without range
230 if ( RangeId == -1 ) this is constant
231 if ( RangeId == -2 ) this is concatenation
232 */
233 
234 /**Function*************************************************************
235 
236  Synopsis []
237 
238  Description []
239 
240  SideEffects []
241 
242  SeeAlso []
243 
244 ***********************************************************************/
245 static inline int Cba_PrsReadName( Cba_Prs_t * p )
246 {
247  char * pStart = p->pCur;
248  if ( Cba_PrsIsChar(p, '\\') ) // escaped name
249  {
250  pStart = ++p->pCur;
251  while ( !Cba_PrsIsChar(p, ' ') )
252  p->pCur++;
253  }
254  else if ( Cba_IsSymb1(*p->pCur) ) // simple name
255  {
256  p->pCur++;
257  while ( Cba_IsSymb2(*p->pCur) )
258  p->pCur++;
259  }
260  else
261  return 0;
262  return Abc_NamStrFindOrAddLim( p->pDesign->pNames, pStart, p->pCur, NULL );
263 }
264 static inline int Cba_PrsReadConstant( Cba_Prs_t * p )
265 {
266  char * pStart = p->pCur;
267  assert( Cba_PrsIsDigit(p) );
268  while ( Cba_PrsIsDigit(p) )
269  p->pCur++;
270  if ( !Cba_PrsIsChar(p, '\'') ) return Cba_PrsErrorSet(p, "Cannot read constant.", 0);
271  p->pCur++;
272  if ( Cba_PrsIsChar(p, 'b') )
273  {
274  p->pCur++;
275  while ( Cba_IsDigitB(*p->pCur) )
276  p->pCur++;
277  }
278  else if ( Cba_PrsIsChar(p, 'h') )
279  {
280  p->pCur++;
281  while ( Cba_IsDigitH(*p->pCur) )
282  p->pCur++;
283  }
284  else if ( Cba_PrsIsChar(p, 'd') )
285  {
286  p->pCur++;
287  while ( Cba_PrsIsDigit(p) )
288  p->pCur++;
289  }
290  else return Cba_PrsErrorSet(p, "Cannot read radix of constant.", 0);
291  return Abc_NamStrFindOrAddLim( p->pDesign->pNames, pStart, p->pCur, NULL );
292 }
293 static inline int Cba_PrsReadRange( Cba_Prs_t * p )
294 {
295  assert( Cba_PrsIsChar(p, '[') );
296  Vec_StrClear( &p->vCover );
297  Vec_StrPush( &p->vCover, *p->pCur++ );
298  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
299  if ( !Cba_PrsIsDigit(p) ) return Cba_PrsErrorSet(p, "Cannot read digit in range specification.", 0);
300  while ( Cba_PrsIsDigit(p) )
301  Vec_StrPush( &p->vCover, *p->pCur++ );
302  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
303  if ( Cba_PrsIsChar(p, ':') )
304  {
305  Vec_StrPush( &p->vCover, *p->pCur++ );
306  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
307  if ( !Cba_PrsIsDigit(p) ) return Cba_PrsErrorSet(p, "Cannot read digit in range specification.", 0);
308  while ( Cba_PrsIsDigit(p) )
309  Vec_StrPush( &p->vCover, *p->pCur++ );
310  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
311  }
312  if ( !Cba_PrsIsChar(p, ']') ) return Cba_PrsErrorSet(p, "Cannot read closing brace in range specification.", 0);
313  Vec_StrPush( &p->vCover, *p->pCur++ );
314  Vec_StrPush( &p->vCover, '\0' );
315  return Abc_NamStrFindOrAdd( p->pDesign->pNames, Vec_StrArray(&p->vCover), NULL );
316 }
317 static inline int Cba_PrsReadSignal( Cba_Prs_t * p, int * pName, int * pRange )
318 {
319  *pName = *pRange = 0;
320  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
321  if ( Cba_PrsIsDigit(p) )
322  {
323  *pName = Cba_PrsReadConstant(p);
324  if ( *pName == 0 ) return 0;
325  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
326  *pRange = -1;
327  return 1;
328  }
329  *pName = Cba_PrsReadName( p );
330  if ( *pName == 0 )
331  return 1;
332  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
333  if ( !Cba_PrsIsChar(p, '[') )
334  return 1;
335  *pRange = Cba_PrsReadRange(p);
336  if ( *pRange == 0 ) return 0;
337  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
338  return 1;
339 }
340 static inline int Cba_PrsReadSignalList( Cba_Prs_t * p, Vec_Int_t * vTemp, char LastSymb )
341 {
342  Vec_IntClear( vTemp );
343  while ( 1 )
344  {
345  int NameId, RangeId;
346  if ( !Cba_PrsReadSignal(p, &NameId, &RangeId) ) return 0;
347  if ( NameId == 0 ) return Cba_PrsErrorSet(p, "Cannot read signal in the list.", 0);
348  Vec_IntPushTwo( vTemp, NameId, RangeId );
349  if ( Cba_PrsIsChar(p, LastSymb) ) break;
350  if ( !Cba_PrsIsChar(p, ',') ) return Cba_PrsErrorSet(p, "Expecting comma in the list.", 0);
351  p->pCur++;
352  }
353  assert( Vec_IntSize(vTemp) > 0 );
354  assert( Vec_IntSize(vTemp) % 2 == 0 );
355  return 1;
356 }
357 static inline int Cba_PrsReadConcat( Cba_Prs_t * p, Vec_Int_t * vTemp2 )
358 {
359  assert( Cba_PrsIsChar(p, '{') );
360  p->pCur++;
361  if ( !Cba_PrsReadSignalList(p, vTemp2, '}') ) return 0;
362  // check final
363  assert( Cba_PrsIsChar(p, '}') );
364  p->pCur++;
365  // return special case
366  if ( Vec_IntSize(vTemp2) == 2 ) return -1; // trivial concatentation
367  assert( Vec_IntSize(vTemp2) > 2 );
368  assert( Vec_IntSize(vTemp2) % 2 == 0 );
369  // create new concatentation
371  Vec_IntPush( &p->vFuncsCur, 0 );
372  Vec_IntPush( &p->vInstIdsCur, 0 );
374  // return the result
375  assert( Vec_WecSize(&p->vFaninsCur) > 0 );
376  return Vec_WecSize(&p->vFaninsCur);
377 }
378 static inline int Cba_PrsReadSignalOrConcat( Cba_Prs_t * p, int * pName, int * pRange )
379 {
380  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
381  if ( Cba_PrsIsChar(p, '{') )
382  {
383  int Status = Cba_PrsReadConcat(p, &p->vTemp2);
384  if ( Status == 0 ) return 0;
385  *pName = Status == -1 ? Vec_IntEntry( &p->vTemp2, 0 ) : Status;
386  *pRange = Status == -1 ? Vec_IntEntry( &p->vTemp2, 1 ) : -2;
387  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
388  }
389  else
390  {
391  if ( !Cba_PrsReadSignal(p, pName, pRange) ) return 0;
392  if ( *pName == 0 ) return Cba_PrsErrorSet(p, "Cannot read formal name in the list.", 0);
393  }
394  return 1;
395 }
396 static inline int Cba_PrsReadSignalList1( Cba_Prs_t * p, Vec_Int_t * vTemp )
397 {
398  Vec_IntClear( vTemp );
399  while ( 1 )
400  {
401  int NameId, RangeId;
402  if ( !Cba_PrsReadSignalOrConcat(p, &NameId, &RangeId) ) return 0;
403  if ( NameId == 0 ) return Cba_PrsErrorSet(p, "Cannot read signal or concatenation in the list.", 0);
404  Vec_IntPushTwo( vTemp, NameId, RangeId );
405  if ( Cba_PrsIsChar(p, ')') ) break;
406  if ( !Cba_PrsIsChar(p, ',') ) return Cba_PrsErrorSet(p, "Expecting comma in the list.", 0);
407  p->pCur++;
408  }
409  p->pCur++;
410  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
411  if ( !Cba_PrsIsChar(p, ';') ) return Cba_PrsErrorSet(p, "Expecting semicolon in the instance.", 0);
412  assert( Vec_IntSize(vTemp) > 0 );
413  assert( Vec_IntSize(vTemp) % 2 == 0 );
414  return 1;
415 }
416 static inline int Cba_PrsReadSignalList2( Cba_Prs_t * p, Vec_Int_t * vTemp )
417 {
418  int FormId, NameId, RangeId;
419  Vec_IntClear( vTemp );
420  assert( Cba_PrsIsChar(p, '.') );
421  while ( Cba_PrsIsChar(p, '.') )
422  {
423  p->pCur++;
424  if ( !Cba_PrsReadSignal(p, &FormId, &RangeId) ) return 0;
425  if ( FormId == 0 ) return Cba_PrsErrorSet(p, "Cannot read formal name of the instance.", 0);
426  if ( RangeId != 0 ) return Cba_PrsErrorSet(p, "Formal signal cannot have range.", 0);
427  if ( !Cba_PrsIsChar(p, '(') ) return Cba_PrsErrorSet(p, "Cannot read \"(\" in the instance.", 0);
428  p->pCur++;
429  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
430  if ( !Cba_PrsReadSignalOrConcat(p, &NameId, &RangeId) ) return 0;
431  if ( NameId == 0 ) return Cba_PrsErrorSet(p, "Cannot read actual name of the instance.", 0);
432  if ( !Cba_PrsIsChar(p, ')') ) return Cba_PrsErrorSet(p, "Cannot read \")\" in the instance.", 0);
433  p->pCur++;
434  Vec_IntPush( vTemp, FormId );
435  Vec_IntPushTwo( vTemp, NameId, RangeId );
436  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
437  if ( Cba_PrsIsChar(p, ')') ) break;
438  if ( !Cba_PrsIsChar(p, ',') ) return Cba_PrsErrorSet(p, "Expecting comma in the instance.", 0);
439  p->pCur++;
440  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
441  }
442  p->pCur++;
443  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
444  if ( !Cba_PrsIsChar(p, ';') ) return Cba_PrsErrorSet(p, "Expecting semicolon in the instance.", 0);
445  assert( Vec_IntSize(vTemp) > 0 );
446  assert( Vec_IntSize(vTemp) % 3 == 0 );
447  return 1;
448 }
449 
450 /**Function*************************************************************
451 
452  Synopsis []
453 
454  Description []
455 
456  SideEffects []
457 
458  SeeAlso []
459 
460 ***********************************************************************/
461 static inline int Cba_PrsReadDeclaration( Cba_Prs_t * p, int Type )
462 {
463  int NameId, RangeId, RangeIdTemp, i;
464  Vec_Int_t * vSigs[4] = { &p->vInoutsCur, &p->vInputsCur, &p->vOutputsCur, &p->vWiresCur };
465  assert( Type >= CBA_VER_INOUT && Type <= CBA_VER_WIRE );
466  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
467  RangeId = 0;
468  if ( Cba_PrsIsChar(p, '[') && !(RangeId = Cba_PrsReadRange(p)) ) return 0;
469  if ( !Cba_PrsReadSignalList( p, &p->vTemp, ';' ) ) return 0;
470  Vec_IntForEachEntryDouble( &p->vTemp, NameId, RangeIdTemp, i )
471  {
472  if ( RangeIdTemp ) return Cba_PrsErrorSet(p, "Range is specified twice in the declaration.", 0);
473  Vec_IntPushTwo( vSigs[Type - CBA_VER_INOUT], NameId, RangeId );
474  }
475  return 1;
476 }
477 static inline int Cba_PrsReadAssign( Cba_Prs_t * p )
478 {
479  int OutName = 0, InName = 0, RangeId = 0, fCompl = 0, Oper = 0;
480  Vec_IntClear( &p->vTemp );
481  // read output name
482  if ( !Cba_PrsReadSignal(p, &OutName, &RangeId) ) return 0;
483  if ( OutName == 0 ) return Cba_PrsErrorSet(p, "Cannot read output in assign-statement.", 0);
484  if ( !Cba_PrsIsChar(p, '=') ) return Cba_PrsErrorSet(p, "Expecting \"=\" in assign-statement.", 0);
485  p->pCur++;
486  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
487  if ( Cba_PrsIsChar(p, '~') )
488  {
489  fCompl = 1;
490  p->pCur++;
491  }
492  Vec_IntPush( &p->vTemp, OutName );
493  Vec_IntPush( &p->vTemp, RangeId );
494  // read first name
495  if ( !Cba_PrsReadSignal(p, &InName, &RangeId)) return 0;
496  if ( InName == 0 ) return Cba_PrsErrorSet(p, "Cannot read first input name in the assign-statement.", 0);
497  Vec_IntPush( &p->vTemp, InName );
498  Vec_IntPush( &p->vTemp, RangeId );
499  // check unary operator
500  if ( Cba_PrsIsChar(p, ';') )
501  {
503  Vec_IntPush( &p->vFuncsCur, fCompl ? CBA_NODE_INV : CBA_NODE_BUF );
504  Vec_IntPush( &p->vInstIdsCur, 0 );
506  return 1;
507  }
508  if ( Cba_PrsIsChar(p, '&') )
509  Oper = CBA_NODE_AND;
510  else if ( Cba_PrsIsChar(p, '|') )
511  Oper = CBA_NODE_OR;
512  else if ( Cba_PrsIsChar(p, '^') )
513  Oper = fCompl ? CBA_NODE_XNOR : CBA_NODE_XOR;
514  else if ( Cba_PrsIsChar(p, '?') )
515  Oper = CBA_NODE_MUX;
516  else return Cba_PrsErrorSet(p, "Unrecognized operator in the assign-statement.", 0);
517  p->pCur++;
518  // read second name
519  if ( !Cba_PrsReadSignal(p, &InName, &RangeId)) return 0;
520  if ( InName == 0 ) return Cba_PrsErrorSet(p, "Cannot read second input name in the assign-statement.", 0);
521  Vec_IntPush( &p->vTemp, InName );
522  Vec_IntPush( &p->vTemp, RangeId );
523  // read third argument
524  if ( Oper == CBA_NODE_MUX )
525  {
526  assert( fCompl == 0 );
527  if ( !Cba_PrsIsChar(p, ':') ) return Cba_PrsErrorSet(p, "Expected colon in the MUX assignment.", 0);
528  p->pCur++;
529  // read third name
530  if ( !Cba_PrsReadSignal(p, &InName, &RangeId)) return 0;
531  if ( InName == 0 ) return Cba_PrsErrorSet(p, "Cannot read third input name in the assign-statement.", 0);
532  Vec_IntPush( &p->vTemp, InName );
533  Vec_IntPush( &p->vTemp, RangeId );
534  if ( !Cba_PrsIsChar(p, ';') ) return Cba_PrsErrorSet(p, "Expected semicolon at the end of the assign-statement.", 0);
535  }
536  // write binary operator
538  Vec_IntPush( &p->vFuncsCur, Oper );
539  Vec_IntPush( &p->vInstIdsCur, 0 );
541  return 1;
542 }
543 static inline int Cba_PrsReadInstance( Cba_Prs_t * p, int Func )
544 {
545  // have to assign Type, Func, InstId, vFanins
546  int InstId, Status, Type;
547  Vec_IntClear( &p->vTemp );
548  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
549  InstId = Cba_PrsReadName( p );
550  if ( InstId && Cba_PrsUtilSkipSpaces(p) ) return 0;
551  if ( !Cba_PrsIsChar(p, '(') ) return Cba_PrsErrorSet(p, "Expecting \"(\" in module instantiation.", 0);
552  p->pCur++;
553  if ( Cba_PrsUtilSkipSpaces(p) ) return 0;
554  if ( Cba_PrsIsChar(p, '.') ) // node
555  Status = Cba_PrsReadSignalList2(p, &p->vTemp), Type = CBA_PRS_BOX;
556  else
557  Status = Cba_PrsReadSignalList1(p, &p->vTemp), Type = CBA_PRS_NODE;
558  if ( Status == 0 ) return 0;
559  // translate elementary gate
560  if ( Type == CBA_PRS_NODE )
561  {
562  int iFuncNew = Cba_PrsIsKnownModule(p, Abc_NamStr(p->pDesign->pNames, Func));
563  if ( iFuncNew == 0 ) return Cba_PrsErrorSet(p, "Cannot find elementary gate.", 0);
564  Func = iFuncNew;
565  }
566  // assign
567  Vec_IntPush( &p->vTypesCur, Type );
568  Vec_IntPush( &p->vFuncsCur, Func );
569  Vec_IntPush( &p->vInstIdsCur, InstId );
571  return 1;
572 }
573 
574 // this procedure can return:
575 // 0 = reached end-of-file; 1 = successfully parsed; 2 = recognized as primitive; 3 = failed and skipped; 4 = error (failed and could not skip)
576 static inline int Cba_PrsReadModule( Cba_Prs_t * p )
577 {
578  int iToken, Status;
579  if ( p->iModuleName != 0 ) return Cba_PrsErrorSet(p, "Parsing previous module is unfinished.", 4);
580  if ( Cba_PrsUtilSkipSpaces(p) )
581  {
582  Cba_PrsErrorClear( p );
583  return 0;
584  }
585  // read keyword
586  iToken = Cba_PrsReadName( p );
587  if ( iToken != CBA_VER_MODULE ) return Cba_PrsErrorSet(p, "Cannot read \"module\" keyword.", 4);
588  if ( Cba_PrsUtilSkipSpaces(p) ) return 4;
589  // read module name
590  p->iModuleName = Cba_PrsReadName( p );
591  if ( p->iModuleName == 0 ) return Cba_PrsErrorSet(p, "Cannot read module name.", 4);
593  {
594  if ( Cba_PrsUtilSkipUntilWord( p, "endmodule" ) ) return Cba_PrsErrorSet(p, "Cannot find \"endmodule\" keyword.", 4);
595  //printf( "Warning! Skipped known module \"%s\".\n", Abc_NamStr(p->pDesign->pNames, p->iModuleName) );
596  Vec_IntPush( &p->vKnown, p->iModuleName );
597  p->iModuleName = 0;
598  return 2;
599  }
600  // skip arguments
601  if ( Cba_PrsUtilSkipSpaces(p) ) return 4;
602  if ( !Cba_PrsIsChar(p, '(') ) return Cba_PrsErrorSet(p, "Cannot find \"(\" in the argument declaration.", 4);
603  if ( !Cba_PrsUtilSkipUntil(p,')') ) return 4;
604  assert( *p->pCur == ')' );
605  p->pCur++;
606  if ( Cba_PrsUtilSkipSpaces(p) ) return 4;
607  // read declarations and instances
608  while ( Cba_PrsIsChar(p, ';') )
609  {
610  p->pCur++;
611  if ( Cba_PrsUtilSkipSpaces(p) ) return 4;
612  iToken = Cba_PrsReadName( p );
613  if ( iToken == CBA_VER_ENDMODULE )
614  {
617  p->iModuleName = 0;
618  return 1;
619  }
620  if ( iToken >= CBA_VER_INOUT && iToken <= CBA_VER_WIRE ) // declaration
621  Status = Cba_PrsReadDeclaration( p, iToken );
622  else if ( iToken == CBA_VER_REG || iToken == CBA_VER_DEFPARAM ) // unsupported keywords
623  Status = Cba_PrsUtilSkipUntil( p, ';' );
624  else // read instance
625  {
626  if ( iToken == CBA_VER_ASSIGN )
627  Status = Cba_PrsReadAssign( p );
628  else
629  Status = Cba_PrsReadInstance( p, iToken );
630  if ( Status == 0 )
631  {
632  if ( Cba_PrsUtilSkipUntilWord( p, "endmodule" ) ) return Cba_PrsErrorSet(p, "Cannot find \"endmodule\" keyword.", 4);
633  //printf( "Warning! Failed to parse \"%s\". Adding module \"%s\" as blackbox.\n",
634  // Abc_NamStr(p->pDesign->pNames, iToken), Abc_NamStr(p->pDesign->pNames, p->iModuleName) );
635  Vec_IntPush( &p->vFailed, p->iModuleName );
636  // cleanup
637  Vec_IntClear( &p->vWiresCur );
638  ABC_FREE( p->vFaninsCur.pArray );
639  Vec_WecZero( &p->vFaninsCur );
640  Vec_IntClear( &p->vTypesCur );
641  Vec_IntClear( &p->vFuncsCur );
642  Vec_IntClear( &p->vInstIdsCur );
643  // add
645  Cba_PrsErrorClear( p );
646  p->iModuleName = 0;
647  return 3;
648  }
649  }
650  if ( !Status ) return 4;
651  if ( Cba_PrsUtilSkipSpaces(p) ) return 4;
652  }
653  return Cba_PrsErrorSet(p, "Cannot find \";\" in the module definition.", 4);
654 }
655 static inline int Cba_PrsReadDesign( Cba_Prs_t * p )
656 {
657  while ( 1 )
658  {
659  int RetValue = Cba_PrsReadModule( p );
660  if ( RetValue == 0 ) // end of file
661  break;
662  if ( RetValue == 1 ) // successfully parsed
663  continue;
664  if ( RetValue == 2 ) // recognized as primitive
665  continue;
666  if ( RetValue == 3 ) // failed and skipped
667  continue;
668  if ( RetValue == 4 ) // error
669  return 0;
670  assert( 0 );
671  }
672  return 1;
673 }
674 
675 /**Function*************************************************************
676 
677  Synopsis []
678 
679  Description []
680 
681  SideEffects []
682 
683  SeeAlso []
684 
685 ***********************************************************************/
687 {
688  char * pName; int i;
689  printf( "Succeeded parsing %d models:\n", Vec_IntSize(&p->vSucceeded) );
690  Cba_PrsForEachModelVec( &p->vSucceeded, p, pName, i )
691  printf( " %s", pName );
692  printf( "\n" );
693  printf( "Skipped %d known models:\n", Vec_IntSize(&p->vKnown) );
694  Cba_PrsForEachModelVec( &p->vKnown, p, pName, i )
695  printf( " %s", pName );
696  printf( "\n" );
697  printf( "Skipped %d failed models:\n", Vec_IntSize(&p->vFailed) );
698  Cba_PrsForEachModelVec( &p->vFailed, p, pName, i )
699  printf( " %s", pName );
700  printf( "\n" );
701 }
702 
703 /**Function*************************************************************
704 
705  Synopsis []
706 
707  Description []
708 
709  SideEffects []
710 
711  SeeAlso []
712 
713 ***********************************************************************/
714 Cba_Man_t * Cba_PrsReadVerilog( char * pFileName )
715 {
716  Cba_Man_t * pDesign = NULL;
717  Cba_Prs_t * p = Cba_PrsAlloc( pFileName );
718  if ( p == NULL )
719  return NULL;
721  Cba_PrsReadDesign( p );
722  Cba_PrsPrintModules( p );
723  if ( Cba_PrsErrorPrint(p) )
724  ABC_SWAP( Cba_Man_t *, pDesign, p->pDesign );
725  Cba_PrsFree( p );
726  return pDesign;
727 }
728 
729 void Cba_PrsReadVerilogTest( char * pFileName )
730 {
731  abctime clk = Abc_Clock();
732  extern void Cba_PrsWriteVerilog( char * pFileName, Cba_Man_t * pDes );
733 // Cba_Man_t * p = Cba_PrsReadVerilog( "c/hie/dump/1/netlist_1.v" );
734 // Cba_Man_t * p = Cba_PrsReadVerilog( "aga/me/me_wide.v" );
735  Cba_Man_t * p = Cba_PrsReadVerilog( "aga/ray/ray_wide.v" );
736  if ( !p ) return;
737  printf( "Finished reading %d networks. ", Cba_ManNtkNum(p) );
738  printf( "NameIDs = %d. ", Abc_NamObjNumMax(p->pNames) );
739  printf( "Memory = %.2f MB. ", 1.0*Cba_ManMemory(p)/(1<<20) );
740  Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
741 // Abc_NamPrint( p->pDesign->pNames );
742 // Cba_PrsWriteVerilog( "c/hie/dump/1/netlist_1_out.v", p );
743 // Cba_PrsWriteVerilog( "aga/me/me_wide_out.v", p );
744  Cba_PrsWriteVerilog( "aga/ray/ray_wide_out.v", p );
745  Cba_ManFree( p );
746 }
747 
748 
749 
750 ////////////////////////////////////////////////////////////////////////
751 /// END OF FILE ///
752 ////////////////////////////////////////////////////////////////////////
753 
754 
756 
static int Cba_PrsReadSignalList2(Cba_Prs_t *p, Vec_Int_t *vTemp)
Definition: cbaReadVer.c:416
static void Vec_IntPushTwo(Vec_Int_t *p, int Entry1, int Entry2)
Definition: vecInt.h:701
static int Cba_PrsReadConcat(Cba_Prs_t *p, Vec_Int_t *vTemp2)
Definition: cbaReadVer.c:357
void Cba_PrsPrintModules(Cba_Prs_t *p)
Definition: cbaReadVer.c:686
static int Cba_PrsErrorSet(Cba_Prs_t *p, char *pError, int Value)
MACRO DEFINITIONS ///.
Definition: cbaPrs.h:92
void Cba_PrsWriteVerilog(char *pFileName, Cba_Man_t *pDes)
Definition: cbaWriteVer.c:207
static int Cba_PrsReadInstance(Cba_Prs_t *p, int Func)
Definition: cbaReadVer.c:543
static Llb_Mgr_t * p
Definition: llb3Image.c:950
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
static char * Vec_StrArray(Vec_Str_t *p)
Definition: vecStr.h:272
Vec_Int_t vTypesCur
Definition: cbaPrs.h:68
Vec_Int_t vKnown
Definition: cbaPrs.h:77
Vec_Int_t vSucceeded
Definition: cbaPrs.h:79
static int Cba_PrsReadSignal(Cba_Prs_t *p, int *pName, int *pRange)
Definition: cbaReadVer.c:317
static int Cba_IsSymb2(char c)
Definition: cbaReadVer.c:81
static int Cba_IsDigitH(char c)
Definition: cbaReadVer.c:78
int Abc_NamObjNumMax(Abc_Nam_t *p)
Definition: utilNam.c:186
static int Cba_IsSpace(char c)
Definition: cbaReadVer.c:75
static Vec_Int_t * Vec_WecPushLevel(Vec_Wec_t *p)
Definition: vecWec.h:284
Vec_Int_t vTemp2
Definition: cbaPrs.h:75
static int Cba_PrsUtilSkipUntil(Cba_Prs_t *p, char c)
Definition: cbaReadVer.c:204
static int Cba_PrsReadAssign(Cba_Prs_t *p)
Definition: cbaReadVer.c:477
static void Vec_StrClear(Vec_Str_t *p)
Definition: vecStr.h:519
static int Cba_PrsReadRange(Cba_Prs_t *p)
Definition: cbaReadVer.c:293
const char * s_KnownModules[100]
FUNCTION DEFINITIONS ///.
Definition: cbaReadVer.c:104
int Abc_NamStrFindOrAdd(Abc_Nam_t *p, char *pStr, int *pfFound)
Definition: utilNam.c:392
static abctime Abc_Clock()
Definition: abc_global.h:279
static int Cba_PrsUtilSkipUntilWord(Cba_Prs_t *p, char *pWord)
Definition: cbaReadVer.c:219
static int Cba_IsDigit(char c)
Definition: cbaReadVer.c:76
static void Vec_StrPush(Vec_Str_t *p, char Entry)
Definition: vecStr.h:535
Abc_Nam_t * pNames
Definition: cba.h:87
Vec_Int_t vFailed
Definition: cbaPrs.h:78
static int Vec_WecSize(Vec_Wec_t *p)
Definition: vecWec.h:193
static int Cba_PrsReadDesign(Cba_Prs_t *p)
Definition: cbaReadVer.c:655
#define ABC_SWAP(Type, a, b)
Definition: abc_global.h:218
Vec_Int_t vInoutsCur
Definition: cbaPrs.h:63
static int Cba_PrsIsDigit(Cba_Prs_t *p)
Definition: cbaReadVer.c:85
int Abc_NamStrFindOrAddLim(Abc_Nam_t *p, char *pStr, char *pLim, int *pfFound)
Definition: utilNam.c:431
Vec_Int_t vInputsCur
Definition: cbaPrs.h:64
Vec_Str_t vCover
Definition: cbaPrs.h:73
static void Vec_WecZero(Vec_Wec_t *p)
Definition: vecWec.h:330
static void Abc_PrintTime(int level, const char *pStr, abctime time)
Definition: abc_global.h:367
int iModuleName
Definition: cbaPrs.h:62
void Cba_PrsReadVerilogTest(char *pFileName)
Definition: cbaReadVer.c:729
Vec_Int_t vFuncsCur
Definition: cbaPrs.h:69
static void Cba_ManFree(Cba_Man_t *p)
Definition: cba.h:214
char * strstr()
static void Cba_PrsErrorClear(Cba_Prs_t *p)
Definition: cbaPrs.h:99
static void Cba_PrsSetupVecInt(Cba_Prs_t *p, Vec_Int_t *vTo, Vec_Int_t *vFrom)
Definition: cbaPrs.h:116
static int Cba_PrsReadConstant(Cba_Prs_t *p)
Definition: cbaReadVer.c:264
static int Cba_PrsReadDeclaration(Cba_Prs_t *p, int Type)
Definition: cbaReadVer.c:461
static int Vec_IntEntry(Vec_Int_t *p, int i)
Definition: bblif.c:268
Cba_Man_t * pDesign
Definition: cbaPrs.h:60
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
static int Cba_PrsIsChar1(Cba_Prs_t *p, char c)
Definition: cbaReadVer.c:84
Vec_Int_t vWiresCur
Definition: cbaPrs.h:66
char * pCur
Definition: cbaPrs.h:57
static Cba_Prs_t * Cba_PrsAlloc(char *pFileName)
Definition: cbaPrs.h:168
static int Cba_PrsUtilSkipName(Cba_Prs_t *p)
Definition: cbaReadVer.c:179
static void Vec_IntPush(Vec_Int_t *p, int Entry)
Definition: bblif.c:468
static int Cba_PrsReadSignalList1(Cba_Prs_t *p, Vec_Int_t *vTemp)
Definition: cbaReadVer.c:396
Vec_Wec_t vFaninsCur
Definition: cbaPrs.h:71
static int Cba_PrsErrorPrint(Cba_Prs_t *p)
Definition: cbaPrs.h:104
static int Cba_IsSymb1(char c)
Definition: cbaReadVer.c:80
Vec_Int_t vOutputsCur
Definition: cbaPrs.h:65
static Cba_Ntk_t * Cba_PrsAddCurrentModel(Cba_Prs_t *p, int iNameId)
Definition: cbaPrs.h:125
static int Cba_ManMemory(Cba_Man_t *p)
Definition: cba.h:229
#define Vec_IntForEachEntryDouble(vVec, Entry1, Entry2, i)
Definition: vecInt.h:66
static int Cba_PrsUtilSkipSpaces(Cba_Prs_t *p)
Definition: cbaReadVer.c:190
char * Abc_NamStr(Abc_Nam_t *p, int NameId)
Definition: utilNam.c:479
Cba_Man_t * Cba_PrsReadVerilog(char *pFileName)
Definition: cbaReadVer.c:714
Definition: cba.h:82
static int Cba_PrsReadModule(Cba_Prs_t *p)
Definition: cbaReadVer.c:576
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
static int Cba_ManNtkNum(Cba_Man_t *p)
Definition: cba.h:121
static int Cba_PrsUtilSkipComments(Cba_Prs_t *p)
Definition: cbaReadVer.c:161
const char * s_VerTypes[CBA_VER_UNKNOWN+1]
Definition: cbaReadVer.c:48
static int Cba_PrsIsChar(Cba_Prs_t *p, char c)
Definition: cbaReadVer.c:83
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
Vec_Int_t vInstIdsCur
Definition: cbaPrs.h:70
Cba_VerType_t
DECLARATIONS ///.
Definition: cbaReadVer.c:31
char * pLimit
Definition: cbaPrs.h:56
static void Cba_PrsFree(Cba_Prs_t *p)
Definition: cbaPrs.h:183
#define ABC_FREE(obj)
Definition: abc_global.h:232
static int Cba_PrsReadSignalList(Cba_Prs_t *p, Vec_Int_t *vTemp, char LastSymb)
Definition: cbaReadVer.c:340
static int Cba_PrsReadSignalOrConcat(Cba_Prs_t *p, int *pName, int *pRange)
Definition: cbaReadVer.c:378
Vec_Int_t vTemp
Definition: cbaPrs.h:74
int strncmp()
static int Cba_PrsReadName(Cba_Prs_t *p)
Definition: cbaReadVer.c:245
#define assert(ex)
Definition: util_old.h:213
int strlen()
static int Cba_IsChar(char c)
Definition: cbaReadVer.c:79
static void Vec_IntClear(Vec_Int_t *p)
Definition: bblif.c:452
ABC_INT64_T abctime
Definition: abc_global.h:278
static int Cba_IsDigitB(char c)
Definition: cbaReadVer.c:77
static void Cba_PrsAddVerilogDirectives(Cba_Prs_t *p)
Definition: cbaReadVer.c:65
static int Cba_PrsIsKnownModule(Cba_Prs_t *p, char *pName)
Definition: cbaReadVer.c:138
#define Cba_PrsForEachModelVec(vVec, p, pName, i)
Definition: cbaPrs.h:84