vrq
cdecl.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (C) 1997-2007, Mark Hummel
3  * This file is part of Vrq.
4  *
5  * Vrq is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * Vrq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301 USA
19  *****************************************************************************
20  */
21 /******************************************************************************
22  *
23  *
24  * cdecl.hpp
25  * - abstract class for declaration
26  *
27  *
28  ******************************************************************************
29  */
30 
31 #ifndef CDECL_HPP
32 #define CDECL_HPP
33 
34 #include <stdio.h>
35 #include <vector>
36 #include <list>
37 #include "glue.h"
38 #include "csymbol.h"
39 #include "csimpletype.h"
40 
41 class CNode;
42 class CDecl;
43 
47 struct Coord_t {
48  int lineno;
49  const char* filename;
50 };
51 
52 
56 enum Wire_t {
59  eTRI,
62  eWOR,
69 };
70 
74 enum Decl_t {
75  eVAR = 0,
94  // NOTE this are class types only
95  eNET,
98  eUDP,
102 };
103 
104 #if defined CDECL_CPP
105 
108 const char* wireName[] = {
109  "no_wire_type",
110  "wire",
111  "tri",
112  "wand",
113  "triand",
114  "wor",
115  "trior",
116  "tri1",
117  "tri0",
118  "trireg",
119  "supply0",
120  "supply1"
121 };
122 
126 const char* declName[] = {
127  "variable",
128  "param",
129  "localparam",
130  "block",
131  "parallel block",
132  "genvar block",
133  "module",
134  "macro",
135  "instance",
136  "input",
137  "output",
138  "inout",
139  "nodir",
140  "port",
141  "fref",
142  "function",
143  "specify block",
144  "genvar",
145  "typedef",
146  "net",
147  "portdir",
148  "attr",
149  "udp",
150  "enum",
151  "package",
152  "none"
153 };
154 #else
155 extern char* declName[];
156 extern char* wireName[];
157 #endif
158 
159 
160 
164 class CDecl : public CObject
165 {
166 public:
167  enum Flag {
168  eFLAG_NONE = 0,
169  eFLAG_PRAGMA = 1,
170  eFLAG_ARRAY = 2,
171  eFLAG_DATATYPE = 4
172  };
173  static Flag Or( Flag f1, Flag f2 )
174  { return static_cast<Flag>(((unsigned)f1 | (unsigned)f2)); }
175  static Flag Or( Flag f1, Flag f2, Flag f3 )
176  { return static_cast<Flag>(((unsigned)f1 | (unsigned)f2) | (unsigned)f3); }
177 private:
178  Coord_t loc;
179  CSymbol* symbol;
180  Decl_t type;
181  int declStatement;
182  CNode* attributes;
184  CNode* pragmas;
185  vector<CNode*> unpackedRange;
186  int numberOfUnpackedDimensions;
188  Flag flags;
189  int automatic;
190  Wire_t wireAttr;
191  int constAttr;
192  int varAttr;
193  CDataType* dataType;
194 public:
200  virtual CDecl* Clone( CObstack* heap ) = 0;
205  virtual void SetDataType( CDataType* dt ) {
206  MASSERT( flags & eFLAG_DATATYPE );
207  dataType = dt;
208  }
213  virtual CDataType* GetDataType() { return dataType; }
218  virtual void SetWireAttr( Wire_t v ) { wireAttr = v; }
223  virtual Wire_t GetWireAttr() { return wireAttr; }
229  virtual int IsWidthConstant( void ) const {
230  if( numberOfUnpackedDimensions ) {
231  return FALSE;
232  }
233  MASSERT( dataType );
234  return dataType->IsPackedWidthConstant();
235  }
241  virtual int IsWidthVolatile( void ) const {
242  if( numberOfUnpackedDimensions ) {
243  return TRUE;
244  }
245  MASSERT( dataType );
246  return dataType->IsPackedWidthVolatile();
247  }
252  virtual int IsWidthEvaluateable( void ) const {
253  if( numberOfUnpackedDimensions ) {
254  return FALSE;
255  }
256  MASSERT( dataType );
257  return dataType->IsPackedWidthEvaluateable();
258  }
263  virtual INT32 GetWidth( void ) const {
264  if( numberOfUnpackedDimensions ) {
265  return 0;
266  }
267  MASSERT( dataType );
268  return dataType->GetPackedWidth();
269  }
274  virtual CNode* GetWidthExp( void ) const {
275  if( numberOfUnpackedDimensions ) {
276  return NULL;
277  }
278  MASSERT( dataType );
279  if( !dataType->IsPacked() ) {
280  return NULL;
281  }
282  return dataType->GetPackedWidthExp();
283  }
288  virtual CNode* GetMsb() const {
289  MASSERT( dataType );
290  return dataType->GetPackedMsb();
291  }
296  virtual CNode* GetLsb() const {
297  MASSERT( dataType );
298  return dataType->GetPackedLsb();
299  }
304  virtual int WidthDirection( void ) const {
305  MASSERT( dataType );
306  return dataType->PackedWidthDirection();
307  }
313  return numberOfUnpackedDimensions;
314  }
320  virtual CNode* GetUnpackedMsi( INT32 dim );
326  virtual CNode* GetUnpackedLsi( INT32 dim );
331  virtual void SetNumberOfUnpackedDimensions( INT32 dim ) {
332  MASSERT( flags & eFLAG_ARRAY );
333  numberOfUnpackedDimensions = dim;
334  unpackedRange.reserve( dim );
335  }
341  virtual CNode* GetUnpackedRange( INT32 dim ) { return unpackedRange[dim]; }
347  virtual void SetUnpackedRange( INT32 dim, CNode* v )
348  { MASSERT(flags & eFLAG_ARRAY); unpackedRange[dim] = v; }
354  virtual void SetConstAttr( int v ) { constAttr = v; }
359  virtual int GetConstAttr() { return constAttr; }
365  virtual void SetVarAttr( int v ) { varAttr = v; }
370  virtual int GetVarAttr() { return varAttr; }
376  virtual void SetAutomatic( int v ) { automatic = v; }
381  virtual int GetAutomatic() { return automatic; }
386  virtual void SetVectored( int v ) { MASSERT( FALSE ); }
391  virtual int GetVectored() { MASSERT( FALSE ); return 0; }
396  virtual void SetScalared( int v ) { MASSERT( FALSE ); }
401  virtual int GetScalared() { MASSERT( FALSE ); return 0; }
406  void SetAttributes( CNode* attr ) { attributes = attr; }
411  CNode* GetAttributes() { return attributes; }
418  int HasAttribute( char* name, CNode* n=NULL, int init = 1 );
424  MASSERT( dataType );
425  return dataType->GetNodeType();
426  }
431  Decl_t GetClass( void )
432  {
433  switch( type ) {
434  case ePARAM:
435  case eLOCALPARAM:
436  return ePARAM;
437  case eINPUT:
438  case eOUTPUT:
439  case eINOUT:
440  return ePORTDIR;
441  default:
442  return type;
443  }
444  }
445 
451  static void GetMembers( Decl_t type, list<Decl_t>& result )
452  {
453  switch( type ) {
454  case ePARAM:
455  result.push_back( ePARAM );
456  result.push_back( eLOCALPARAM );
457  break;
458  case ePORTDIR:
459  result.push_back( eINPUT );
460  result.push_back( eOUTPUT );
461  result.push_back( eINOUT );
462  break;
463  default:
464  result.push_back( type );
465  break;
466  }
467  }
468 
474  void SetDeclStatementCreated( void ) { declStatement = TRUE; }
475 
480  int DeclStatementCreated( void ) { return declStatement; }
481 
486  Decl_t GetType( void ) { return type; }
487 
492  const char* GetTypeName( void ) { return declName[type]; }
493 
498  void SetCoord( Coord_t* aLoc ) { loc = *aLoc; }
499 
504  Coord_t* GetCoord( void ) {
505  return &loc;
506  }
507 
512  virtual void Dump( FILE* f )
513  { fprintf( f, "'%s' line %d", loc.filename , loc.lineno ); }
514 
519  virtual void DumpDeclInfo( FILE* f )
520  {
521  fprintf( f, "%s: %s, defined in '%s' line %d\n",
522  declName[GetType()], GetName(), loc.filename , loc.lineno );
523  }
524 
529  const char* GetName( void ) { return symbol->GetName(); }
534  void SetSymbol( CSymbol* aSymbol ) { symbol = aSymbol; }
539  CSymbol* GetSymbol( void ) { return symbol; }
544  void SetPragmas( CNode* p )
545  { MASSERT( flags & eFLAG_PRAGMA );pragmas = p; }
550  CNode* GetPragmas() { return pragmas; }
551 protected:
561  CDecl( CSymbol* aSymbol, Coord_t* aLoc,
562  Decl_t aType, CDataType* dataType, Flag flags ) {
563  loc = *aLoc;
564  symbol = aSymbol;
565  type = aType;
566  numberOfUnpackedDimensions = 0;
567  declStatement = FALSE;
568  attributes = NULL;
569  pragmas = NULL;
570  automatic = FALSE;
571  constAttr = FALSE;
572  wireAttr = eNO_WIRE_TYPE;
573  varAttr = FALSE;
574  this->flags = flags;
575  this->dataType = dataType;
576  }
583  void Copy( CObstack* heap, const CDecl& o );
589 private:
590  /*
591  * disable copy constructor
592  */
593  CDecl( const CDecl& o );
594 
595 public:
596  virtual void PreVisit1( int (*func)(CNode*,void*), void* data );
597  virtual void PostVisit1( void (*func)(CNode*, void*), void* data );
598  virtual void PostSubVisit1( CNode* (*func)(CNode*, void*), void* data );
599 };
600 
601 #endif // CDECL_HPP
602 
virtual int GetVarAttr()
Get declaration&#39;s var attribute.
Definition: cdecl.h:370
Decl_t GetType(void)
Get declaration type.
Definition: cdecl.h:486
declaration superclass for all net types
Definition: cdecl.h:95
void SetDeclStatementCreated(void)
Set declaration statement created attribute.
Definition: cdecl.h:474
fork/join block declaration
Definition: cdecl.h:79
virtual void SetWireAttr(Wire_t v)
Set declaration&#39;s wire attribute.
Definition: cdecl.h:218
int lineno
file line number
Definition: cdecl.h:48
triand type
Definition: cdecl.h:61
virtual INT32 GetNumberOfUnpackedDimensions(void)
Get number of unpacked dimensions of declaration.
Definition: cdecl.h:312
virtual int IsPackedWidthEvaluateable(void) const
Determine if packed or vector width of declaration can be evaluated.
virtual void SetDataType(CDataType *dt)
Set declartion&#39;s data type property.
Definition: cdecl.h:205
virtual void DumpDeclInfo(FILE *f)
Dump declaration name, type and location to file descriptor.
Definition: cdecl.h:519
virtual int PackedWidthDirection(void) const
Evaluate current decl width direction.
Flag
Definition: cdecl.h:167
Decl_t
Declaration types.
Definition: cdecl.h:74
tri1 type
Definition: cdecl.h:64
virtual void SetScalared(int v)
Set declartion&#39;s scalared property.
Definition: cdecl.h:396
virtual CNode * GetPackedMsb() const
Get expression for declaration&#39;s msb.
attribute declaration
Definition: cdecl.h:97
named block declaration
Definition: cdecl.h:78
supply1 type
Definition: cdecl.h:68
CDecl(CSymbol *aSymbol, Coord_t *aLoc, Decl_t aType, CDataType *dataType, Flag flags)
Create instance of declaration.
Definition: cdecl.h:561
long INT32
Short cut for signed 32 bit integer.
Definition: glue.h:38
virtual NodeType_t GetNodeType(void) const =0
Get data type.
genvar declaration
Definition: cdecl.h:92
module declaration
Definition: cdecl.h:81
trior type
Definition: cdecl.h:63
virtual void SetUnpackedRange(INT32 dim, CNode *v)
Set expression for range of unpacked array for declaration.
Definition: cdecl.h:347
virtual CNode * GetWidthExp(void) const
Get expression representing width of declaration.
Definition: cdecl.h:274
port declaration
Definition: cdecl.h:88
Base class for describing data types.
Definition: cdatatype.h:112
const char * wireName[]
Array to convert Wire_t to character string.
Definition: cdecl.h:108
inout declaration
Definition: cdecl.h:86
generate block declaration
Definition: cdecl.h:80
virtual int IsPacked() const =0
Determine if complete data structure is packed.
const char * filename
file name
Definition: cdecl.h:49
virtual int IsWidthVolatile(void) const
Determine if packed width of declaration is volatile, ie depend upon parameters or variables...
Definition: cdecl.h:241
Structure to hold file coordinates.
Definition: cdecl.h:47
Holder for character strings.
Definition: csymbol.h:44
virtual CNode * GetPackedLsb() const
Get expression for declaration&#39;s lsb.
static Flag Or(Flag f1, Flag f2)
Definition: cdecl.h:173
virtual int IsWidthConstant(void) const
Determine if packed width of declaration is constant, ie dependent upon only constants and parameters...
Definition: cdecl.h:229
Bulk object allocation object.
Definition: cobstack.h:46
void SetAttributes(CNode *attr)
Set declarations&#39;s attributes.
Definition: cdecl.h:406
virtual void Dump(FILE *f)
Dump declaration info to file descriptor.
Definition: cdecl.h:512
package declaration
Definition: cdecl.h:100
CNode * GetAttributes()
Get declaration&#39;s attributes.
Definition: cdecl.h:411
int DeclStatementCreated(void)
Get declaration statement create attibute.
Definition: cdecl.h:480
const char * declName[]
Array to convert Decl_t to character string.
Definition: cdecl.h:126
virtual void SetVectored(int v)
Set declartion&#39;s vectored property.
Definition: cdecl.h:386
Primary data structure representing parse tree nodes.
Definition: cnode.h:197
instance declaration
Definition: cdecl.h:83
virtual void SetVarAttr(int v)
Set declaration&#39;s var attribute.
Definition: cdecl.h:365
virtual INT32 GetPackedWidth(void) const
Evaluate packed or vector width of declaration.
virtual void SetAutomatic(int v)
Set declaration&#39;s automatic property.
Definition: cdecl.h:376
tri type
Definition: cdecl.h:59
static void GetMembers(Decl_t type, list< Decl_t > &result)
Get a list of members of the given declaration class/type.
Definition: cdecl.h:451
output declaration
Definition: cdecl.h:85
unspecified declaration
Definition: cdecl.h:87
type declaration
Definition: cdecl.h:93
virtual CNode * GetUnpackedRange(INT32 dim)
Get expression for range of array for dimension.
Definition: cdecl.h:341
virtual int GetConstAttr()
Get declaration&#39;s const attribute.
Definition: cdecl.h:359
virtual int IsPackedWidthConstant(void) const
Determine if packed or vector width of declaration is constant, ie dependent upon only constants and ...
Decl_t GetClass(void)
Get class of declaration.
Definition: cdecl.h:431
virtual CNode * GetLsb() const
Get expression for declaration&#39;s lsb.
Definition: cdecl.h:296
virtual int IsPackedWidthVolatile(void) const
Determine if packed or vector width of declaration is volatile, ie depend upon parameters or variable...
declaration superclass for all port dirs
Definition: cdecl.h:96
Base class for describing declaration objects.
Definition: cdecl.h:164
virtual Wire_t GetWireAttr()
Get declartion&#39;s wire type property.
Definition: cdecl.h:223
virtual int GetAutomatic()
Get declaration&#39;s automatic property.
Definition: cdecl.h:381
supply0 type
Definition: cdecl.h:67
trireg type
Definition: cdecl.h:66
Coord_t * GetCoord(void)
Get file coordinates for declaration.
Definition: cdecl.h:504
static Flag Or(Flag f1, Flag f2, Flag f3)
Definition: cdecl.h:175
void SetSymbol(CSymbol *aSymbol)
Set declaration&#39;s symbol.
Definition: cdecl.h:534
forward reference declaration (used by parser only)
Definition: cdecl.h:89
const char * GetTypeName(void)
Get declaration type as a string.
Definition: cdecl.h:492
void SetPragmas(CNode *p)
Set declaration pragmas.
Definition: cdecl.h:544
enum declaration
Definition: cdecl.h:99
virtual int WidthDirection(void) const
Evaluate current decl width direction.
Definition: cdecl.h:304
parameter declaration
Definition: cdecl.h:76
Base class for vrq objects.
Definition: cobject.h:41
virtual int IsWidthEvaluateable(void) const
Determine if packed width of declaration can be evaluated.
Definition: cdecl.h:252
virtual int GetScalared()
Get declartion&#39;s scalared property.
Definition: cdecl.h:401
macro module declaration
Definition: cdecl.h:82
CSymbol * GetSymbol(void)
Get declaration&#39;s symbol.
Definition: cdecl.h:539
list< string > pragmas
Definition: main.cc:268
wor type
Definition: cdecl.h:62
unspecified declaration used for wildcard
Definition: cdecl.h:101
void SetCoord(Coord_t *aLoc)
Set declaration coordinate.
Definition: cdecl.h:498
virtual CDataType * GetDataType()
Get declartion&#39;s data type property.
Definition: cdecl.h:213
NodeType_t GetNodeType(void)
Get node type of decl.
Definition: cdecl.h:423
function/task declaration
Definition: cdecl.h:90
CNode * GetPragmas()
Get declaration pragmas.
Definition: cdecl.h:550
virtual void SetConstAttr(int v)
Set declaration&#39;s const attribute.
Definition: cdecl.h:354
input declaration
Definition: cdecl.h:84
localparam declaration
Definition: cdecl.h:77
NodeType_t
Expression node type.
Definition: cdatatype.h:101
specify block declaration
Definition: cdecl.h:91
const char * GetName(void)
Shortcut to get declaration&#39;s name.
Definition: cdecl.h:529
Wire_t
Wire types.
Definition: cdecl.h:56
virtual void SetNumberOfUnpackedDimensions(INT32 dim)
Set number of unpacked dimensions for declaration.
Definition: cdecl.h:331
virtual int GetVectored()
Get declartion&#39;s vectored property.
Definition: cdecl.h:391
variable declaration
Definition: cdecl.h:75
virtual INT32 GetWidth(void) const
Evaluate packed width of declaration.
Definition: cdecl.h:263
no wire type
Definition: cdecl.h:57
virtual CNode * GetPackedWidthExp() const
Get expression for datatype&#39;s overall packed or vector width.
tri0 type
Definition: cdecl.h:65
wire type
Definition: cdecl.h:58
wand type
Definition: cdecl.h:60
virtual CNode * GetMsb() const
Get expression for declaration&#39;s msb.
Definition: cdecl.h:288
const char * GetName(void) const
Get symbol&#39;s text.
udp declaration
Definition: cdecl.h:98