abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
verStream.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [verStream.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Verilog parser.]
8 
9  Synopsis [Input file stream, which knows nothing about Verilog.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - August 19, 2006.]
16 
17  Revision [$Id: verStream.c,v 1.00 2006/08/19 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "ver.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 #define VER_BUFFER_SIZE 1048576 // 1M - size of the data chunk stored in memory
31 #define VER_OFFSET_SIZE 65536 // 64K - load new data when less than this is left
32 #define VER_WORD_SIZE 65536 // 64K - the largest token that can be returned
33 
34 #define VER_MINIMUM(a,b) (((a) < (b))? (a) : (b))
35 
37 {
38  // the input file
39  char * pFileName; // the input file name
40  FILE * pFile; // the input file pointer
41  iword nFileSize; // the total number of bytes in the file
42  iword nFileRead; // the number of bytes currently read from file
43  iword nLineCounter; // the counter of lines processed
44  // temporary storage for data
45  iword nBufferSize; // the size of the buffer
46  char * pBuffer; // the buffer
47  char * pBufferCur; // the current reading position
48  char * pBufferEnd; // the first position not used by currently loaded data
49  char * pBufferStop; // the position where loading new data will be done
50  // tokens given to the user
51  char pChars[VER_WORD_SIZE+5]; // temporary storage for a word (plus end-of-string and two parantheses)
52  int nChars; // the total number of characters in the word
53  // status of the parser
54  int fStop; // this flag goes high when the end of file is reached
55 };
56 
57 static void Ver_StreamReload( Ver_Stream_t * p );
58 
59 ////////////////////////////////////////////////////////////////////////
60 /// FUNCTION DEFINITIONS ///
61 ////////////////////////////////////////////////////////////////////////
62 
63 /**Function*************************************************************
64 
65  Synopsis [Starts the file reader for the given file.]
66 
67  Description []
68 
69  SideEffects []
70 
71  SeeAlso []
72 
73 ***********************************************************************/
74 Ver_Stream_t * Ver_StreamAlloc( char * pFileName )
75 {
76  Ver_Stream_t * p;
77  FILE * pFile;
78  int nCharsToRead;
79  int RetValue;
80  // check if the file can be opened
81  pFile = fopen( pFileName, "rb" );
82  if ( pFile == NULL )
83  {
84  printf( "Ver_StreamAlloc(): Cannot open input file \"%s\".\n", pFileName );
85  return NULL;
86  }
87  // start the file reader
88  p = ABC_ALLOC( Ver_Stream_t, 1 );
89  memset( p, 0, sizeof(Ver_Stream_t) );
90  p->pFileName = pFileName;
91  p->pFile = pFile;
92  // get the file size, in bytes
93  fseek( pFile, 0, SEEK_END );
94  p->nFileSize = ftell( pFile );
95  rewind( pFile );
96  // allocate the buffer
97  p->pBuffer = ABC_ALLOC( char, VER_BUFFER_SIZE+1 );
99  p->pBufferCur = p->pBuffer;
100  // determine how many chars to read
101  nCharsToRead = VER_MINIMUM(p->nFileSize, VER_BUFFER_SIZE);
102  // load the first part into the buffer
103  RetValue = fread( p->pBuffer, nCharsToRead, 1, p->pFile );
104  p->nFileRead = nCharsToRead;
105  // set the ponters to the end and the stopping point
106  p->pBufferEnd = p->pBuffer + nCharsToRead;
108  // start the arrays
109  p->nLineCounter = 1; // 1-based line counting
110  return p;
111 }
112 
113 /**Function*************************************************************
114 
115  Synopsis [Loads new data into the file reader.]
116 
117  Description []
118 
119  SideEffects []
120 
121  SeeAlso []
122 
123 ***********************************************************************/
125 {
126  int nCharsUsed, nCharsToRead;
127  int RetValue;
128  assert( !p->fStop );
129  assert( p->pBufferCur > p->pBufferStop );
130  assert( p->pBufferCur < p->pBufferEnd );
131  // figure out how many chars are still not processed
132  nCharsUsed = p->pBufferEnd - p->pBufferCur;
133  // move the remaining data to the beginning of the buffer
134  memmove( p->pBuffer, p->pBufferCur, nCharsUsed );
135  p->pBufferCur = p->pBuffer;
136  // determine how many chars we will read
137  nCharsToRead = VER_MINIMUM( p->nBufferSize - nCharsUsed, p->nFileSize - p->nFileRead );
138  // read the chars
139  RetValue = fread( p->pBuffer + nCharsUsed, nCharsToRead, 1, p->pFile );
140  p->nFileRead += nCharsToRead;
141  // set the ponters to the end and the stopping point
142  p->pBufferEnd = p->pBuffer + nCharsUsed + nCharsToRead;
144 }
145 
146 /**Function*************************************************************
147 
148  Synopsis [Stops the file reader.]
149 
150  Description []
151 
152  SideEffects []
153 
154  SeeAlso []
155 
156 ***********************************************************************/
158 {
159  if ( p->pFile )
160  fclose( p->pFile );
161  ABC_FREE( p->pBuffer );
162  ABC_FREE( p );
163 }
164 
165 /**Function*************************************************************
166 
167  Synopsis [Returns the file size.]
168 
169  Description []
170 
171  SideEffects []
172 
173  SeeAlso []
174 
175 ***********************************************************************/
177 {
178  return p->pFileName;
179 }
180 
181 /**Function*************************************************************
182 
183  Synopsis [Returns the file size.]
184 
185  Description []
186 
187  SideEffects []
188 
189  SeeAlso []
190 
191 ***********************************************************************/
193 {
194  return p->nFileSize;
195 }
196 
197 /**Function*************************************************************
198 
199  Synopsis [Returns the current reading position.]
200 
201  Description []
202 
203  SideEffects []
204 
205  SeeAlso []
206 
207 ***********************************************************************/
209 {
210  return p->nFileRead - (p->pBufferEnd - p->pBufferCur);
211 }
212 
213 /**Function*************************************************************
214 
215  Synopsis [Returns the line number for the given token.]
216 
217  Description []
218 
219  SideEffects []
220 
221  SeeAlso []
222 
223 ***********************************************************************/
225 {
226  return p->nLineCounter;
227 }
228 
229 
230 
231 /**Function*************************************************************
232 
233  Synopsis [Returns current symbol.]
234 
235  Description []
236 
237  SideEffects []
238 
239  SeeAlso []
240 
241 ***********************************************************************/
243 {
244  return !p->fStop;
245 }
246 
247 /**Function*************************************************************
248 
249  Synopsis [Returns current symbol.]
250 
251  Description []
252 
253  SideEffects []
254 
255  SeeAlso []
256 
257 ***********************************************************************/
259 {
260  assert( !p->fStop );
261  return *p->pBufferCur;
262 }
263 
264 /**Function*************************************************************
265 
266  Synopsis [Returns current symbol and moves to the next.]
267 
268  Description []
269 
270  SideEffects []
271 
272  SeeAlso []
273 
274 ***********************************************************************/
276 {
277  assert( !p->fStop );
278  // check if the new data should to be loaded
279  if ( p->pBufferCur > p->pBufferStop )
280  Ver_StreamReload( p );
281  // check if there are symbols left
282  if ( p->pBufferCur == p->pBufferEnd ) // end of file
283  {
284  p->fStop = 1;
285  return -1;
286  }
287  // count the lines
288  if ( *p->pBufferCur == '\n' )
289  p->nLineCounter++;
290  return *p->pBufferCur++;
291 }
292 
293 /**Function*************************************************************
294 
295  Synopsis [Skips the current symbol and all symbols from the list.]
296 
297  Description []
298 
299  SideEffects []
300 
301  SeeAlso []
302 
303 ***********************************************************************/
304 void Ver_StreamSkipChars( Ver_Stream_t * p, char * pCharsToSkip )
305 {
306  char * pChar, * pTemp;
307  assert( !p->fStop );
308  assert( pCharsToSkip != NULL );
309  // check if the new data should to be loaded
310  if ( p->pBufferCur > p->pBufferStop )
311  Ver_StreamReload( p );
312  // skip the symbols
313  for ( pChar = p->pBufferCur; pChar < p->pBufferEnd; pChar++ )
314  {
315  // skip symbols as long as they are in the list
316  for ( pTemp = pCharsToSkip; *pTemp; pTemp++ )
317  if ( *pChar == *pTemp )
318  break;
319  if ( *pTemp == 0 ) // pChar is not found in the list
320  {
321  p->pBufferCur = pChar;
322  return;
323  }
324  // count the lines
325  if ( *pChar == '\n' )
326  p->nLineCounter++;
327  }
328  // the file is finished or the last part continued
329  // through VER_OFFSET_SIZE chars till the end of the buffer
330  if ( p->pBufferStop == p->pBufferEnd ) // end of file
331  {
332  p->fStop = 1;
333  return;
334  }
335  printf( "Ver_StreamSkipSymbol() failed to parse the file \"%s\".\n", p->pFileName );
336 }
337 
338 /**Function*************************************************************
339 
340  Synopsis [Skips all symbols until encountering one from the list.]
341 
342  Description []
343 
344  SideEffects []
345 
346  SeeAlso []
347 
348 ***********************************************************************/
349 void Ver_StreamSkipToChars( Ver_Stream_t * p, char * pCharsToStop )
350 {
351  char * pChar, * pTemp;
352  assert( !p->fStop );
353  assert( pCharsToStop != NULL );
354  // check if the new data should to be loaded
355  if ( p->pBufferCur > p->pBufferStop )
356  Ver_StreamReload( p );
357  // skip the symbols
358  for ( pChar = p->pBufferCur; pChar < p->pBufferEnd; pChar++ )
359  {
360  // skip symbols as long as they are NOT in the list
361  for ( pTemp = pCharsToStop; *pTemp; pTemp++ )
362  if ( *pChar == *pTemp )
363  break;
364  if ( *pTemp == 0 ) // pChar is not found in the list
365  {
366  // count the lines
367  if ( *pChar == '\n' )
368  p->nLineCounter++;
369  continue;
370  }
371  // the symbol is found - move position and return
372  p->pBufferCur = pChar;
373  return;
374  }
375  // the file is finished or the last part continued
376  // through VER_OFFSET_SIZE chars till the end of the buffer
377  if ( p->pBufferStop == p->pBufferEnd ) // end of file
378  {
379  p->fStop = 1;
380  return;
381  }
382  printf( "Ver_StreamSkipToSymbol() failed to parse the file \"%s\".\n", p->pFileName );
383 }
384 
385 /**Function*************************************************************
386 
387  Synopsis [Returns current word delimited by the set of symbols.]
388 
389  Description [Modifies the stream by inserting 0 at the first encounter
390  of one of the symbols in the list.]
391 
392  SideEffects []
393 
394  SeeAlso []
395 
396 ***********************************************************************/
397 char * Ver_StreamGetWord( Ver_Stream_t * p, char * pCharsToStop )
398 {
399  char * pChar, * pTemp;
400  if ( p->fStop )
401  return NULL;
402  assert( pCharsToStop != NULL );
403  // check if the new data should to be loaded
404  if ( p->pBufferCur > p->pBufferStop )
405  Ver_StreamReload( p );
406  // skip the symbols
407  p->nChars = 0;
408  for ( pChar = p->pBufferCur; pChar < p->pBufferEnd; pChar++ )
409  {
410  // skip symbols as long as they are NOT in the list
411  for ( pTemp = pCharsToStop; *pTemp; pTemp++ )
412  if ( *pChar == *pTemp )
413  break;
414  if ( *pTemp == 0 ) // pChar is not found in the list
415  {
416  p->pChars[p->nChars++] = *pChar;
417  if ( p->nChars == VER_WORD_SIZE )
418  {
419  printf( "Ver_StreamGetWord(): The buffer size is exceeded.\n" );
420  return NULL;
421  }
422  // count the lines
423  if ( *pChar == '\n' )
424  p->nLineCounter++;
425  continue;
426  }
427  // the symbol is found - move the position, set the word end, return the word
428  p->pBufferCur = pChar;
429  p->pChars[p->nChars] = 0;
430  return p->pChars;
431  }
432  // the file is finished or the last part continued
433  // through VER_OFFSET_SIZE chars till the end of the buffer
434  if ( p->pBufferStop == p->pBufferEnd ) // end of file
435  {
436  p->fStop = 1;
437  p->pChars[p->nChars] = 0;
438  return p->pChars;
439  }
440  printf( "Ver_StreamGetWord() failed to parse the file \"%s\".\n", p->pFileName );
441  return NULL;
442 }
443 
444 ////////////////////////////////////////////////////////////////////////
445 /// END OF FILE ///
446 ////////////////////////////////////////////////////////////////////////
447 
448 
450 
char * memset()
Ver_Stream_t * Ver_StreamAlloc(char *pFileName)
FUNCTION DEFINITIONS ///.
Definition: verStream.c:74
void Ver_StreamSkipToChars(Ver_Stream_t *p, char *pCharsToStop)
Definition: verStream.c:349
char Ver_StreamPopChar(Ver_Stream_t *p)
Definition: verStream.c:275
char * pBufferEnd
Definition: verStream.c:48
void Ver_StreamSkipChars(Ver_Stream_t *p, char *pCharsToSkip)
Definition: verStream.c:304
static Llb_Mgr_t * p
Definition: llb3Image.c:950
char * Ver_StreamGetWord(Ver_Stream_t *p, char *pCharsToStop)
Definition: verStream.c:397
iword nLineCounter
Definition: verStream.c:43
#define SEEK_END
Definition: zconf.h:392
static void Ver_StreamReload(Ver_Stream_t *p)
Definition: verStream.c:124
char * pBuffer
Definition: verStream.c:46
iword nBufferSize
Definition: verStream.c:45
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
char Ver_StreamScanChar(Ver_Stream_t *p)
Definition: verStream.c:258
void Ver_StreamFree(Ver_Stream_t *p)
Definition: verStream.c:157
char pChars[VER_WORD_SIZE+5]
Definition: verStream.c:51
char * memmove()
#define VER_BUFFER_SIZE
DECLARATIONS ///.
Definition: verStream.c:30
int Ver_StreamIsOkey(Ver_Stream_t *p)
Definition: verStream.c:242
iword nFileSize
Definition: verStream.c:41
char * pBufferCur
Definition: verStream.c:47
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
FILE * pFile
Definition: verStream.c:40
char * pBufferStop
Definition: verStream.c:49
ABC_INT64_T iword
Definition: abc_global.h:210
int Ver_StreamGetCurPosition(Ver_Stream_t *p)
Definition: verStream.c:208
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
iword nFileRead
Definition: verStream.c:42
#define VER_MINIMUM(a, b)
Definition: verStream.c:34
char * Ver_StreamGetFileName(Ver_Stream_t *p)
Definition: verStream.c:176
#define ABC_FREE(obj)
Definition: abc_global.h:232
int Ver_StreamGetFileSize(Ver_Stream_t *p)
Definition: verStream.c:192
char * pFileName
Definition: verStream.c:39
#define VER_WORD_SIZE
Definition: verStream.c:32
#define assert(ex)
Definition: util_old.h:213
#define VER_OFFSET_SIZE
Definition: verStream.c:31
VOID_HACK rewind()
int Ver_StreamGetLineNumber(Ver_Stream_t *p)
Definition: verStream.c:224