ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
bacReadVer.c
Go to the documentation of this file.
1
20
21#include "bac.h"
22#include "bacPrs.h"
23
25
29
30// Verilog keywords
31typedef enum {
32 PRS_VER_NONE = 0, // 0: unused
33 PRS_VER_INPUT, // 1: input
34 PRS_VER_OUTPUT, // 2: output
35 PRS_VER_INOUT, // 3: inout
36 PRS_VER_WIRE, // 4: wire
37 PRS_VER_MODULE, // 5: module
38 PRS_VER_ASSIGN, // 6: assign
39 PRS_VER_REG, // 7: reg
40 PRS_VER_ALWAYS, // 8: always
41 PRS_VER_DEFPARAM, // 9: always
42 PRS_VER_BEGIN, // 10: begin
43 PRS_VER_END, // 11: end
44 PRS_VER_ENDMODULE, // 12: endmodule
45 PRS_VER_UNKNOWN // 13: unknown
47
48static const char * s_VerTypes[PRS_VER_UNKNOWN+1] = {
49 NULL, // 0: unused
50 "input", // 1: input
51 "output", // 2: output
52 "inout", // 3: inout
53 "wire", // 4: wire
54 "module", // 5: module
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
65static inline void Psr_NtkAddVerilogDirectives( Psr_Man_t * p )
66{
67 int i;
68 for ( i = 1; s_VerTypes[i]; i++ )
69 Abc_NamStrFindOrAdd( p->pStrs, (char *)s_VerTypes[i], NULL );
70 assert( Abc_NamObjNumMax(p->pStrs) == i );
71}
72
73
74// character recognition
75static inline int Psr_CharIsSpace( char c ) { return (c == ' ' || c == '\t' || c == '\r' || c == '\n'); }
76static inline int Psr_CharIsDigit( char c ) { return (c >= '0' && c <= '9'); }
77static inline int Psr_CharIsDigitB( char c ) { return (c == '0' || c == '1' || c == 'x' || c == 'z'); }
78static inline int Psr_CharIsDigitH( char c ) { return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); }
79static inline int Psr_CharIsChar( char c ) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
80static inline int Psr_CharIsSymb1( char c ) { return Psr_CharIsChar(c) || c == '_'; }
81static inline int Psr_CharIsSymb2( char c ) { return Psr_CharIsSymb1(c) || Psr_CharIsDigit(c) || c == '$'; }
82
83static inline int Psr_ManIsChar( Psr_Man_t * p, char c ) { return p->pCur[0] == c; }
84static inline int Psr_ManIsChar1( Psr_Man_t * p, char c ) { return p->pCur[1] == c; }
85static inline int Psr_ManIsDigit( Psr_Man_t * p ) { return Psr_CharIsDigit(*p->pCur); }
86
90
102
103// collect predefined modules names
104static const char * s_VerilogModules[100] =
105{
106 "const0", // BAC_BOX_CF,
107 "const1", // BAC_BOX_CT,
108 "constX", // BAC_BOX_CX,
109 "constZ", // BAC_BOX_CZ,
110 "buf", // BAC_BOX_BUF,
111 "not", // BAC_BOX_INV,
112 "and", // BAC_BOX_AND,
113 "nand", // BAC_BOX_NAND,
114 "or", // BAC_BOX_OR,
115 "nor", // BAC_BOX_NOR,
116 "xor", // BAC_BOX_XOR,
117 "xnor", // BAC_BOX_XNOR,
118 "sharp", // BAC_BOX_SHARP,
119 "mux", // BAC_BOX_MUX,
120 "maj", // BAC_BOX_MAJ,
121 NULL
122};
123static const char * s_KnownModules[100] =
124{
125 "VERIFIC_",
126 "add_",
127 "mult_",
128 "div_",
129 "mod_",
130 "rem_",
131 "shift_left_",
132 "shift_right_",
133 "rotate_left_",
134 "rotate_right_",
135 "reduce_and_",
136 "reduce_or_",
137 "reduce_xor_",
138 "reduce_nand_",
139 "reduce_nor_",
140 "reduce_xnor_",
141 "LessThan_",
142 "Mux_",
143 "Select_",
144 "Decoder_",
145 "EnabledDecoder_",
146 "PrioSelect_",
147 "DualPortRam_",
148 "ReadPort_",
149 "WritePort_",
150 "ClockedWritePort_",
151 "lut",
152 "and_",
153 "or_",
154 "xor_",
155 "nand_",
156 "nor_",
157 "xnor_",
158 "buf_",
159 "inv_",
160 "tri_",
161 "sub_",
162 "unary_minus_",
163 "equal_",
164 "not_equal_",
165 "mux_",
166 "wide_mux_",
167 "wide_select_",
168 "wide_dff_",
169 "wide_dlatch_",
170 "wide_dffrs_",
171 "wide_dlatchrs_",
172 "wide_prio_select_",
173 "pow_",
174 "PrioEncoder_",
175 "abs",
176 NULL
177};
178
179// check if it is a Verilog predefined module
180static inline int Psr_ManIsVerilogModule( Psr_Man_t * p, char * pName )
181{
182 int i;
183 for ( i = 0; s_VerilogModules[i]; i++ )
184 if ( !strcmp(pName, s_VerilogModules[i]) )
185 return BAC_BOX_CF + i;
186 return 0;
187}
188// check if it is a known module
189static inline int Psr_ManIsKnownModule( Psr_Man_t * p, char * pName )
190{
191 int i;
192 for ( i = 0; s_KnownModules[i]; i++ )
193 if ( !strncmp(pName, s_KnownModules[i], strlen(s_KnownModules[i])) )
194 return i;
195 return 0;
196}
197
198
210
211// skips Verilog comments (returns 1 if some comments were skipped)
212static inline int Psr_ManUtilSkipComments( Psr_Man_t * p )
213{
214 if ( !Psr_ManIsChar(p, '/') )
215 return 0;
216 if ( Psr_ManIsChar1(p, '/') )
217 {
218 for ( p->pCur += 2; p->pCur < p->pLimit; p->pCur++ )
219 if ( Psr_ManIsChar(p, '\n') )
220 { p->pCur++; return 1; }
221 }
222 else if ( Psr_ManIsChar1(p, '*') )
223 {
224 for ( p->pCur += 2; p->pCur < p->pLimit; p->pCur++ )
225 if ( Psr_ManIsChar(p, '*') && Psr_ManIsChar1(p, '/') )
226 { p->pCur++; p->pCur++; return 1; }
227 }
228 return 0;
229}
230static inline int Psr_ManUtilSkipName( Psr_Man_t * p )
231{
232 if ( !Psr_ManIsChar(p, '\\') )
233 return 0;
234 for ( p->pCur++; p->pCur < p->pLimit; p->pCur++ )
235 if ( Psr_ManIsChar(p, ' ') )
236 { p->pCur++; return 1; }
237 return 0;
238}
239
240// skip any number of spaces and comments
241static inline int Psr_ManUtilSkipSpaces( Psr_Man_t * p )
242{
243 while ( p->pCur < p->pLimit )
244 {
245 while ( Psr_CharIsSpace(*p->pCur) )
246 p->pCur++;
247 if ( !*p->pCur )
248 return Psr_ManErrorSet(p, "Unexpectedly reached end-of-file.", 1);
249 if ( !Psr_ManUtilSkipComments(p) )
250 return 0;
251 }
252 return Psr_ManErrorSet(p, "Unexpectedly reached end-of-file.", 1);
253}
254// skip everything including comments until the given char
255static inline int Psr_ManUtilSkipUntil( Psr_Man_t * p, char c )
256{
257 while ( p->pCur < p->pLimit )
258 {
259 if ( Psr_ManIsChar(p, c) )
260 return 1;
261 if ( Psr_ManUtilSkipComments(p) )
262 continue;
263 if ( Psr_ManUtilSkipName(p) )
264 continue;
265 p->pCur++;
266 }
267 return 0;
268}
269// skip everything including comments until the given word
270static inline int Psr_ManUtilSkipUntilWord( Psr_Man_t * p, char * pWord )
271{
272 char * pPlace = strstr( p->pCur, pWord );
273 if ( pPlace == NULL ) return 1;
274 p->pCur = pPlace + strlen(pWord);
275 return 0;
276}
277
289static inline int Psr_ManReadName( Psr_Man_t * p )
290{
291 char * pStart = p->pCur;
292 if ( Psr_ManIsChar(p, '\\') ) // escaped name
293 {
294 pStart = ++p->pCur;
295 while ( !Psr_ManIsChar(p, ' ') )
296 p->pCur++;
297 }
298 else if ( Psr_CharIsSymb1(*p->pCur) ) // simple name
299 {
300 p->pCur++;
301 while ( Psr_CharIsSymb2(*p->pCur) )
302 p->pCur++;
303 }
304 else
305 return 0;
306 return Abc_NamStrFindOrAddLim( p->pStrs, pStart, p->pCur, NULL );
307}
308static inline int Psr_ManReadNameList( Psr_Man_t * p, Vec_Int_t * vTemp, char LastSymb )
309{
310 Vec_IntClear( vTemp );
311 while ( 1 )
312 {
313 int Item = Psr_ManReadName(p);
314 if ( Item == 0 ) return Psr_ManErrorSet(p, "Cannot read name in the list.", 0);
315 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 1.", 0);
316 if ( Item == PRS_VER_WIRE )
317 continue;
318 Vec_IntPush( vTemp, Item );
319 if ( Psr_ManIsChar(p, LastSymb) ) break;
320 if ( !Psr_ManIsChar(p, ',') ) return Psr_ManErrorSet(p, "Expecting comma in the list.", 0);
321 p->pCur++;
322 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 2.", 0);
323 }
324 return 1;
325}
326static inline int Psr_ManReadConstant( Psr_Man_t * p )
327{
328 char * pStart = p->pCur;
329 assert( Psr_ManIsDigit(p) );
330 while ( Psr_ManIsDigit(p) )
331 p->pCur++;
332 if ( !Psr_ManIsChar(p, '\'') ) return Psr_ManErrorSet(p, "Cannot read constant.", 0);
333 p->pCur++;
334 if ( Psr_ManIsChar(p, 'b') )
335 {
336 p->pCur++;
337 while ( Psr_CharIsDigitB(*p->pCur) )
338 {
339 if ( *p->pCur == '0' )
340 p->pNtk->fHasC0s = 1;
341 else if ( *p->pCur == '1' )
342 p->pNtk->fHasC1s = 1;
343 else if ( *p->pCur == 'x' )
344 p->pNtk->fHasCXs = 1;
345 else if ( *p->pCur == 'z' )
346 p->pNtk->fHasCZs = 1;
347 p->pCur++;
348 }
349 }
350 else if ( Psr_ManIsChar(p, 'h') )
351 {
352 p->pCur++;
353 p->pNtk->fHasC0s = 1;
354 while ( Psr_CharIsDigitH(*p->pCur) )
355 {
356 if ( *p->pCur != '0' )
357 p->pNtk->fHasC1s = 1;
358 p->pCur++;
359 }
360 }
361 else if ( Psr_ManIsChar(p, 'd') )
362 {
363 p->pCur++;
364 p->pNtk->fHasC0s = 1;
365 while ( Psr_ManIsDigit(p) )
366 {
367 if ( *p->pCur != '0' )
368 p->pNtk->fHasC1s = 1;
369 p->pCur++;
370 }
371 }
372 else return Psr_ManErrorSet(p, "Cannot read radix of constant.", 0);
373 return Abc_NamStrFindOrAddLim( p->pStrs, pStart, p->pCur, NULL );
374}
375static inline int Psr_ManReadRange( Psr_Man_t * p )
376{
377 assert( Psr_ManIsChar(p, '[') );
378 Vec_StrClear( &p->vCover );
379 Vec_StrPush( &p->vCover, *p->pCur++ );
380 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 3.", 0);
381 if ( !Psr_ManIsDigit(p) ) return Psr_ManErrorSet(p, "Cannot read digit in range specification.", 0);
382 while ( Psr_ManIsDigit(p) )
383 Vec_StrPush( &p->vCover, *p->pCur++ );
384 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 4.", 0);
385 if ( Psr_ManIsChar(p, ':') )
386 {
387 Vec_StrPush( &p->vCover, *p->pCur++ );
388 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 5.", 0);
389 if ( !Psr_ManIsDigit(p) ) return Psr_ManErrorSet(p, "Cannot read digit in range specification.", 0);
390 while ( Psr_ManIsDigit(p) )
391 Vec_StrPush( &p->vCover, *p->pCur++ );
392 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 6.", 0);
393 }
394 if ( !Psr_ManIsChar(p, ']') ) return Psr_ManErrorSet(p, "Cannot read closing brace in range specification.", 0);
395 Vec_StrPush( &p->vCover, *p->pCur++ );
396 Vec_StrPush( &p->vCover, '\0' );
397 return Abc_NamStrFindOrAdd( p->pStrs, Vec_StrArray(&p->vCover), NULL );
398}
399static inline int Psr_ManReadConcat( Psr_Man_t * p, Vec_Int_t * vTemp2 )
400{
401 extern int Psr_ManReadSignalList( Psr_Man_t * p, Vec_Int_t * vTemp, char LastSymb, int fAddForm );
402 assert( Psr_ManIsChar(p, '{') );
403 p->pCur++;
404 if ( !Psr_ManReadSignalList( p, vTemp2, '}', 0 ) ) return Psr_ManErrorSet(p, "Error number 7.", 0);
405 // check final
406 assert( Psr_ManIsChar(p, '}') );
407 p->pCur++;
408 // return special case
409 assert( Vec_IntSize(vTemp2) > 0 );
410 if ( Vec_IntSize(vTemp2) == 1 )
411 return Vec_IntEntry(vTemp2, 0);
412 return Abc_Var2Lit2( Psr_NtkAddConcat(p->pNtk, vTemp2), BAC_PRS_CONCAT );
413}
414static inline int Psr_ManReadSignal( Psr_Man_t * p )
415{
416 int Item;
417 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 8.", 0);
418 if ( Psr_ManIsDigit(p) )
419 {
420 Item = Psr_ManReadConstant(p);
421 if ( Item == 0 ) return Psr_ManErrorSet(p, "Error number 9.", 0);
422 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 10.", 0);
423 return Abc_Var2Lit2( Item, BAC_PRS_CONST );
424 }
425 if ( Psr_ManIsChar(p, '{') )
426 {
427 if ( p->fUsingTemp2 ) return Psr_ManErrorSet(p, "Cannot read nested concatenations.", 0);
428 p->fUsingTemp2 = 1;
429 Item = Psr_ManReadConcat(p, &p->vTemp2);
430 p->fUsingTemp2 = 0;
431 if ( Item == 0 ) return Psr_ManErrorSet(p, "Error number 11.", 0);
432 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 12.", 0);
433 return Item;
434 }
435 else
436 {
437 Item = Psr_ManReadName( p );
438 if ( Item == 0 ) return Psr_ManErrorSet(p, "Error number 13.", 0); // was return 1;
439 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 14.", 0);
440 if ( Psr_ManIsChar(p, '[') )
441 {
442 int Range = Psr_ManReadRange(p);
443 if ( Range == 0 ) return Psr_ManErrorSet(p, "Error number 15.", 0);
444 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 16.", 0);
445 return Abc_Var2Lit2( Psr_NtkAddSlice(p->pNtk, Item, Range), BAC_PRS_SLICE );
446 }
447 return Abc_Var2Lit2( Item, BAC_PRS_NAME );
448 }
449}
450int Psr_ManReadSignalList( Psr_Man_t * p, Vec_Int_t * vTemp, char LastSymb, int fAddForm )
451{
452 Vec_IntClear( vTemp );
453 while ( 1 )
454 {
455 int Item = Psr_ManReadSignal(p);
456 if ( Item == 0 ) return Psr_ManErrorSet(p, "Cannot read signal in the list.", 0);
457 if ( fAddForm )
458 Vec_IntPush( vTemp, 0 );
459 Vec_IntPush( vTemp, Item );
460 if ( Psr_ManIsChar(p, LastSymb) ) break;
461 if ( !Psr_ManIsChar(p, ',') ) return Psr_ManErrorSet(p, "Expecting comma in the list.", 0);
462 p->pCur++;
463 }
464 return 1;
465}
466static inline int Psr_ManReadSignalList2( Psr_Man_t * p, Vec_Int_t * vTemp )
467{
468 int FormId, ActItem;
469 Vec_IntClear( vTemp );
470 assert( Psr_ManIsChar(p, '.') );
471 while ( Psr_ManIsChar(p, '.') )
472 {
473 p->pCur++;
474 FormId = Psr_ManReadName( p );
475 if ( FormId == 0 ) return Psr_ManErrorSet(p, "Cannot read formal name of the instance.", 0);
476 if ( !Psr_ManIsChar(p, '(') ) return Psr_ManErrorSet(p, "Cannot read \"(\" in the instance.", 0);
477 p->pCur++;
478 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 17.", 0);
479 ActItem = Psr_ManReadSignal( p );
480 if ( ActItem == 0 ) return Psr_ManErrorSet(p, "Cannot read actual name of the instance.", 0);
481 if ( !Psr_ManIsChar(p, ')') ) return Psr_ManErrorSet(p, "Cannot read \")\" in the instance.", 0);
482 p->pCur++;
483 Vec_IntPushTwo( vTemp, FormId, ActItem );
484 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 18.", 0);
485 if ( Psr_ManIsChar(p, ')') ) break;
486 if ( !Psr_ManIsChar(p, ',') ) return Psr_ManErrorSet(p, "Expecting comma in the instance.", 0);
487 p->pCur++;
488 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 19.", 0);
489 }
490 assert( Vec_IntSize(vTemp) > 0 );
491 assert( Vec_IntSize(vTemp) % 2 == 0 );
492 return 1;
493}
494
506static inline int Psr_ManReadDeclaration( Psr_Man_t * p, int Type )
507{
508 int i, NameId, RangeId = 0;
509 Vec_Int_t * vNames[4] = { &p->pNtk->vInputs, &p->pNtk->vOutputs, &p->pNtk->vInouts, &p->pNtk->vWires };
510 Vec_Int_t * vNamesR[4] = { &p->pNtk->vInputsR, &p->pNtk->vOutputsR, &p->pNtk->vInoutsR, &p->pNtk->vWiresR };
511 assert( Type >= PRS_VER_INPUT && Type <= PRS_VER_WIRE );
512 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 20.", 0);
513 if ( Psr_ManIsChar(p, '[') && !(RangeId = Psr_ManReadRange(p)) ) return Psr_ManErrorSet(p, "Error number 21.", 0);
514 if ( !Psr_ManReadNameList( p, &p->vTemp, ';' ) ) return Psr_ManErrorSet(p, "Error number 22.", 0);
515 Vec_IntForEachEntry( &p->vTemp, NameId, i )
516 {
517 Vec_IntPush( vNames[Type - PRS_VER_INPUT], NameId );
518 Vec_IntPush( vNamesR[Type - PRS_VER_INPUT], RangeId );
519 if ( Type < PRS_VER_WIRE )
520 Vec_IntPush( &p->pNtk->vOrder, Abc_Var2Lit2(NameId, Type) );
521 }
522 return 1;
523}
524static inline int Psr_ManReadAssign( Psr_Man_t * p )
525{
526 int OutItem, InItem, fCompl = 0, fCompl2 = 0, Oper = 0;
527 // read output name
528 OutItem = Psr_ManReadSignal( p );
529 if ( OutItem == 0 ) return Psr_ManErrorSet(p, "Cannot read output in assign-statement.", 0);
530 if ( !Psr_ManIsChar(p, '=') ) return Psr_ManErrorSet(p, "Expecting \"=\" in assign-statement.", 0);
531 p->pCur++;
532 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 23.", 0);
533 if ( Psr_ManIsChar(p, '~') )
534 {
535 fCompl = 1;
536 p->pCur++;
537 }
538 // read first name
539 InItem = Psr_ManReadSignal( p );
540 if ( InItem == 0 ) return Psr_ManErrorSet(p, "Cannot read first input name in the assign-statement.", 0);
541 Vec_IntClear( &p->vTemp );
542 Vec_IntPush( &p->vTemp, 0 );
543 Vec_IntPush( &p->vTemp, InItem );
544 // check unary operator
545 if ( Psr_ManIsChar(p, ';') )
546 {
547 Vec_IntPush( &p->vTemp, 0 );
548 Vec_IntPush( &p->vTemp, OutItem );
549 Oper = fCompl ? BAC_BOX_INV : BAC_BOX_BUF;
550 Psr_NtkAddBox( p->pNtk, Oper, 0, &p->vTemp );
551 return 1;
552 }
553 if ( Psr_ManIsChar(p, '&') )
554 Oper = BAC_BOX_AND;
555 else if ( Psr_ManIsChar(p, '|') )
556 Oper = BAC_BOX_OR;
557 else if ( Psr_ManIsChar(p, '^') )
558 Oper = BAC_BOX_XOR;
559 else if ( Psr_ManIsChar(p, '?') )
560 Oper = BAC_BOX_MUX;
561 else return Psr_ManErrorSet(p, "Unrecognized operator in the assign-statement.", 0);
562 p->pCur++;
563 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 24.", 0);
564 if ( Psr_ManIsChar(p, '~') )
565 {
566 fCompl2 = 1;
567 p->pCur++;
568 }
569 // read second name
570 InItem = Psr_ManReadSignal( p );
571 if ( InItem == 0 ) return Psr_ManErrorSet(p, "Cannot read second input name in the assign-statement.", 0);
572 Vec_IntPush( &p->vTemp, 0 );
573 Vec_IntPush( &p->vTemp, InItem );
574 // read third argument
575 if ( Oper == BAC_BOX_MUX )
576 {
577 assert( fCompl == 0 );
578 if ( !Psr_ManIsChar(p, ':') ) return Psr_ManErrorSet(p, "Expected colon in the MUX assignment.", 0);
579 p->pCur++;
580 // read third name
581 InItem = Psr_ManReadSignal( p );
582 if ( InItem == 0 ) return Psr_ManErrorSet(p, "Cannot read third input name in the assign-statement.", 0);
583 Vec_IntPush( &p->vTemp, 0 );
584 Vec_IntPush( &p->vTemp, InItem );
585 if ( !Psr_ManIsChar(p, ';') ) return Psr_ManErrorSet(p, "Expected semicolon at the end of the assign-statement.", 0);
586 }
587 else
588 {
589 // figure out operator
590 if ( Oper == BAC_BOX_AND )
591 {
592 if ( fCompl && !fCompl2 )
593 Oper = BAC_BOX_SHARPL;
594 else if ( !fCompl && fCompl2 )
595 Oper = BAC_BOX_SHARP;
596 else if ( fCompl && fCompl2 )
597 Oper = BAC_BOX_NOR;
598 }
599 else if ( Oper == BAC_BOX_OR )
600 {
601 if ( fCompl && fCompl2 )
602 Oper = BAC_BOX_NAND;
603 else assert( !fCompl && !fCompl2 );
604 }
605 else if ( Oper == BAC_BOX_XOR )
606 {
607 if ( fCompl && !fCompl2 )
608 Oper = BAC_BOX_XNOR;
609 else assert( !fCompl && !fCompl2 );
610 }
611 }
612 // write binary operator
613 Vec_IntPush( &p->vTemp, 0 );
614 Vec_IntPush( &p->vTemp, OutItem );
615 Psr_NtkAddBox( p->pNtk, Oper, 0, &p->vTemp );
616 return 1;
617}
618static inline int Psr_ManReadInstance( Psr_Man_t * p, int Func )
619{
620 int InstId, Status;
621/*
622 static Counter = 0;
623 if ( ++Counter == 7 )
624 {
625 int s=0;
626 }
627*/
628 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 25.", 0);
629 if ( (InstId = Psr_ManReadName(p)) )
630 if (Psr_ManUtilSkipSpaces(p)) return Psr_ManErrorSet(p, "Error number 26.", 0);
631 if ( !Psr_ManIsChar(p, '(') ) return Psr_ManErrorSet(p, "Expecting \"(\" in module instantiation.", 0);
632 p->pCur++;
633 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 27.", 0);
634 if ( Psr_ManIsChar(p, '.') ) // box
635 Status = Psr_ManReadSignalList2(p, &p->vTemp);
636 else // node
637 {
638 //char * s = Abc_NamStr(p->pStrs, Func);
639 // translate elementary gate
640 int iFuncNew = Psr_ManIsVerilogModule(p, Abc_NamStr(p->pStrs, Func));
641 if ( iFuncNew == 0 ) return Psr_ManErrorSet(p, "Cannot find elementary gate.", 0);
642 Func = iFuncNew;
643 Status = Psr_ManReadSignalList( p, &p->vTemp, ')', 1 );
644 }
645 if ( Status == 0 ) return Psr_ManErrorSet(p, "Error number 28.", 0);
646 assert( Psr_ManIsChar(p, ')') );
647 p->pCur++;
648 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 29.", 0);
649 if ( !Psr_ManIsChar(p, ';') ) return Psr_ManErrorSet(p, "Expecting semicolon in the instance.", 0);
650 // add box
651 Psr_NtkAddBox( p->pNtk, Func, InstId, &p->vTemp );
652 return 1;
653}
654static inline int Psr_ManReadArguments( Psr_Man_t * p )
655{
656 int iRange = 0, iType = -1;
657 Vec_Int_t * vSigs[3] = { &p->pNtk->vInputs, &p->pNtk->vOutputs, &p->pNtk->vInouts };
658 Vec_Int_t * vSigsR[3] = { &p->pNtk->vInputsR, &p->pNtk->vOutputsR, &p->pNtk->vInoutsR };
659 assert( Psr_ManIsChar(p, '(') );
660 p->pCur++;
661 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 30.", 0);
662 while ( 1 )
663 {
664 int iName = Psr_ManReadName( p );
665 if ( iName == 0 ) return Psr_ManErrorSet(p, "Error number 31.", 0);
666 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 32.", 0);
667 if ( iName >= PRS_VER_INPUT && iName <= PRS_VER_INOUT ) // declaration
668 {
669 iType = iName;
670 if ( Psr_ManIsChar(p, '[') )
671 {
672 iRange = Psr_ManReadRange(p);
673 if ( iRange == 0 ) return Psr_ManErrorSet(p, "Error number 33.", 0);
674 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 34.", 0);
675 }
676 iName = Psr_ManReadName( p );
677 if ( iName == 0 ) return Psr_ManErrorSet(p, "Error number 35.", 0);
678 }
679 if ( iType > 0 )
680 {
681 Vec_IntPush( vSigs[iType - PRS_VER_INPUT], iName );
682 Vec_IntPush( vSigsR[iType - PRS_VER_INPUT], iRange );
683 Vec_IntPush( &p->pNtk->vOrder, Abc_Var2Lit2(iName, iType) );
684 }
685 if ( Psr_ManIsChar(p, ')') )
686 break;
687 if ( !Psr_ManIsChar(p, ',') ) return Psr_ManErrorSet(p, "Expecting comma in the instance.", 0);
688 p->pCur++;
689 if ( Psr_ManUtilSkipSpaces(p) ) return Psr_ManErrorSet(p, "Error number 36.", 0);
690 }
691 // check final
692 assert( Psr_ManIsChar(p, ')') );
693 return 1;
694}
695// this procedure can return:
696// 0 = reached end-of-file; 1 = successfully parsed; 2 = recognized as primitive; 3 = failed and skipped; 4 = error (failed and could not skip)
697static inline int Psr_ManReadModule( Psr_Man_t * p )
698{
699 int iToken, Status;
700 if ( p->pNtk != NULL ) return Psr_ManErrorSet(p, "Parsing previous module is unfinished.", 4);
701 if ( Psr_ManUtilSkipSpaces(p) )
702 {
703 Psr_ManErrorClear( p );
704 return 0;
705 }
706 // read keyword
707 iToken = Psr_ManReadName( p );
708 if ( iToken != PRS_VER_MODULE ) return Psr_ManErrorSet(p, "Cannot read \"module\" keyword.", 4);
709 if ( Psr_ManUtilSkipSpaces(p) ) return 4;
710 // read module name
711 iToken = Psr_ManReadName( p );
712 if ( iToken == 0 ) return Psr_ManErrorSet(p, "Cannot read module name.", 4);
713 if ( Psr_ManIsKnownModule(p, Abc_NamStr(p->pStrs, iToken)) )
714 {
715 if ( Psr_ManUtilSkipUntilWord( p, "endmodule" ) ) return Psr_ManErrorSet(p, "Cannot find \"endmodule\" keyword.", 4);
716 //printf( "Warning! Skipped known module \"%s\".\n", Abc_NamStr(p->pStrs, iToken) );
717 Vec_IntPush( &p->vKnown, iToken );
718 return 2;
719 }
720 Psr_ManInitializeNtk( p, iToken, 1 );
721 // skip arguments
722 if ( Psr_ManUtilSkipSpaces(p) ) return 4;
723 if ( !Psr_ManIsChar(p, '(') ) return Psr_ManErrorSet(p, "Cannot find \"(\" in the argument declaration.", 4);
724 if ( !Psr_ManReadArguments(p) ) return 4;
725 assert( *p->pCur == ')' );
726 p->pCur++;
727 if ( Psr_ManUtilSkipSpaces(p) ) return 4;
728 // read declarations and instances
729 while ( Psr_ManIsChar(p, ';') )
730 {
731 p->pCur++;
732 if ( Psr_ManUtilSkipSpaces(p) ) return 4;
733 iToken = Psr_ManReadName( p );
734 if ( iToken == PRS_VER_ENDMODULE )
735 {
736 Vec_IntPush( &p->vSucceeded, p->pNtk->iModuleName );
737 Psr_ManFinalizeNtk( p );
738 return 1;
739 }
740 if ( iToken >= PRS_VER_INPUT && iToken <= PRS_VER_WIRE ) // declaration
741 Status = Psr_ManReadDeclaration( p, iToken );
742 else if ( iToken == PRS_VER_REG || iToken == PRS_VER_DEFPARAM ) // unsupported keywords
743 Status = Psr_ManUtilSkipUntil( p, ';' );
744 else // read instance
745 {
746 if ( iToken == PRS_VER_ASSIGN )
747 Status = Psr_ManReadAssign( p );
748 else
749 Status = Psr_ManReadInstance( p, iToken );
750 if ( Status == 0 )
751 {
752 if ( Psr_ManUtilSkipUntilWord( p, "endmodule" ) ) return Psr_ManErrorSet(p, "Cannot find \"endmodule\" keyword.", 4);
753 //printf( "Warning! Failed to parse \"%s\". Adding module \"%s\" as blackbox.\n",
754 // Abc_NamStr(p->pStrs, iToken), Abc_NamStr(p->pStrs, p->pNtk->iModuleName) );
755 Vec_IntPush( &p->vFailed, p->pNtk->iModuleName );
756 // cleanup
757 Vec_IntErase( &p->pNtk->vWires );
758 Vec_IntErase( &p->pNtk->vWiresR );
759 Vec_IntErase( &p->pNtk->vSlices );
760 Vec_IntErase( &p->pNtk->vConcats );
761 Vec_IntErase( &p->pNtk->vBoxes );
762 Vec_IntErase( &p->pNtk->vObjs );
763 p->fUsingTemp2 = 0;
764 // add
765 Psr_ManFinalizeNtk( p );
766 Psr_ManErrorClear( p );
767 return 3;
768 }
769 }
770 if ( !Status ) return 4;
771 if ( Psr_ManUtilSkipSpaces(p) ) return 4;
772 }
773 return Psr_ManErrorSet(p, "Cannot find \";\" in the module definition.", 4);
774}
775static inline int Psr_ManReadDesign( Psr_Man_t * p )
776{
777 while ( 1 )
778 {
779 int RetValue = Psr_ManReadModule( p );
780 if ( RetValue == 0 ) // end of file
781 break;
782 if ( RetValue == 1 ) // successfully parsed
783 continue;
784 if ( RetValue == 2 ) // recognized as primitive
785 continue;
786 if ( RetValue == 3 ) // failed and skipped
787 continue;
788 if ( RetValue == 4 ) // error
789 return 0;
790 assert( 0 );
791 }
792 return 1;
793}
794
807{
808 char * pName; int i;
809 printf( "Succeeded parsing %d models:\n", Vec_IntSize(&p->vSucceeded) );
810 Psr_ManForEachNameVec( &p->vSucceeded, p, pName, i )
811 printf( " %s", pName );
812 printf( "\n" );
813 printf( "Skipped %d known models:\n", Vec_IntSize(&p->vKnown) );
814 Psr_ManForEachNameVec( &p->vKnown, p, pName, i )
815 printf( " %s", pName );
816 printf( "\n" );
817 printf( "Skipped %d failed models:\n", Vec_IntSize(&p->vFailed) );
818 Psr_ManForEachNameVec( &p->vFailed, p, pName, i )
819 printf( " %s", pName );
820 printf( "\n" );
821}
822
834Vec_Ptr_t * Psr_ManReadVerilog( char * pFileName )
835{
836 Vec_Ptr_t * vPrs = NULL;
837 Psr_Man_t * p = Psr_ManAlloc( pFileName );
838 if ( p == NULL )
839 return NULL;
840 Psr_NtkAddVerilogDirectives( p );
841 Psr_ManReadDesign( p );
842 //Psr_ManPrintModules( p );
843 if ( Psr_ManErrorPrint(p) )
844 ABC_SWAP( Vec_Ptr_t *, vPrs, p->vNtks );
845 Psr_ManFree( p );
846 return vPrs;
847}
848
849void Psr_ManReadVerilogTest( char * pFileName )
850{
851 abctime clk = Abc_Clock();
852 extern void Psr_ManWriteVerilog( char * pFileName, Vec_Ptr_t * p );
853 Vec_Ptr_t * vPrs = Psr_ManReadVerilog( "c/hie/dump/1/netlist_1.v" );
854// Vec_Ptr_t * vPrs = Psr_ManReadVerilog( "aga/me/me_wide.v" );
855// Vec_Ptr_t * vPrs = Psr_ManReadVerilog( "aga/ray/ray_wide.v" );
856 if ( !vPrs ) return;
857 printf( "Finished reading %d networks. ", Vec_PtrSize(vPrs) );
858 printf( "NameIDs = %d. ", Abc_NamObjNumMax(Psr_ManNameMan(vPrs)) );
859 printf( "Memory = %.2f MB. ", 1.0*Psr_ManMemory(vPrs)/(1<<20) );
860 Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
861 Psr_ManWriteVerilog( "c/hie/dump/1/netlist_1_out_new.v", vPrs );
862// Psr_ManWriteVerilog( "aga/me/me_wide_out.v", vPrs );
863// Psr_ManWriteVerilog( "aga/ray/ray_wide_out.v", vPrs );
864// Abc_NamPrint( p->pStrs );
865 Psr_ManVecFree( vPrs );
866}
867
868
872
873
875
#define ABC_SWAP(Type, a, b)
Definition abc_global.h:253
ABC_INT64_T abctime
Definition abc_global.h:332
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
@ BAC_PRS_CONST
Definition bacPrs.h:42
@ BAC_PRS_CONCAT
Definition bacPrs.h:43
@ BAC_PRS_NAME
Definition bacPrs.h:40
@ BAC_PRS_SLICE
Definition bacPrs.h:41
#define Psr_ManForEachNameVec(vVec, p, pName, i)
Definition bacPrs.h:138
struct Psr_Man_t_ Psr_Man_t
Definition bacPrs.h:83
void Psr_ManReadVerilogTest(char *pFileName)
Definition bacReadVer.c:849
void Psr_ManPrintModules(Psr_Man_t *p)
Definition bacReadVer.c:806
Vec_Ptr_t * Psr_ManReadVerilog(char *pFileName)
Definition bacReadVer.c:834
int Psr_ManReadSignalList(Psr_Man_t *p, Vec_Int_t *vTemp, char LastSymb, int fAddForm)
Definition bacReadVer.c:450
Bac_VerType_t
DECLARATIONS ///.
Definition bacReadVer.c:31
@ PRS_VER_INPUT
Definition bacReadVer.c:33
@ PRS_VER_ENDMODULE
Definition bacReadVer.c:44
@ PRS_VER_UNKNOWN
Definition bacReadVer.c:45
@ PRS_VER_END
Definition bacReadVer.c:43
@ PRS_VER_ASSIGN
Definition bacReadVer.c:38
@ PRS_VER_OUTPUT
Definition bacReadVer.c:34
@ PRS_VER_DEFPARAM
Definition bacReadVer.c:41
@ PRS_VER_INOUT
Definition bacReadVer.c:35
@ PRS_VER_REG
Definition bacReadVer.c:39
@ PRS_VER_BEGIN
Definition bacReadVer.c:42
@ PRS_VER_WIRE
Definition bacReadVer.c:36
@ PRS_VER_MODULE
Definition bacReadVer.c:37
@ PRS_VER_ALWAYS
Definition bacReadVer.c:40
@ PRS_VER_NONE
Definition bacReadVer.c:32
void Psr_ManWriteVerilog(char *pFileName, Vec_Ptr_t *p)
@ BAC_BOX_SHARP
Definition bac.h:63
@ BAC_BOX_XNOR
Definition bac.h:62
@ BAC_BOX_AND
Definition bac.h:57
@ BAC_BOX_INV
Definition bac.h:56
@ BAC_BOX_BUF
Definition bac.h:55
@ BAC_BOX_CF
Definition bac.h:51
@ BAC_BOX_XOR
Definition bac.h:61
@ BAC_BOX_MUX
Definition bac.h:65
@ BAC_BOX_OR
Definition bac.h:59
@ BAC_BOX_NAND
Definition bac.h:58
@ BAC_BOX_SHARPL
Definition bac.h:64
@ BAC_BOX_NOR
Definition bac.h:60
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition bblif.c:37
Cube * p
Definition exorList.c:222
int Abc_NamStrFindOrAddLim(Abc_Nam_t *p, char *pStr, char *pLim, int *pfFound)
Definition utilNam.c:492
int Abc_NamStrFindOrAdd(Abc_Nam_t *p, char *pStr, int *pfFound)
Definition utilNam.c:453
int Abc_NamObjNumMax(Abc_Nam_t *p)
Definition utilNam.c:231
char * Abc_NamStr(Abc_Nam_t *p, int NameId)
Definition utilNam.c:555
#define assert(ex)
Definition util_old.h:213
int strncmp()
int strlen()
int strcmp()
char * strstr()
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
Definition vecInt.h:54
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition vecPtr.h:42