vrq
cvector.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  * cvector.hpp
25  * - abstract class for bit vector declaration
26  *
27  *
28  ******************************************************************************
29  */
30 
31 #ifndef CVECTOR_HPP
32 #define CVECTOR_HPP
33 
34 #include <stdio.h>
35 #include <math.h>
36 #include "glue.h"
37 #include "cuint.h"
38 #include "cdecl.h"
39 
40 
58 class CVector
59 {
60 private:
61  CUInt aval;
62  CUInt bval;
63  CUInt hook;
64  INT32 width;
65  int sized;
66  int based;
68  int unbased;
70  int preferredBase;
72  int _signed;
74  int overflowed; // set to 1 if string conversion overflowed
75 
76  static int bufferSize;
77  static char* buffer;
78 public:
85  static CVector* AllocFromHeap( CObstack* aHeap, int width ) {
86  CVector* v = new(aHeap) CVector( width );
87  v->SetHeap( aHeap );
88  return v;
89  }
90 
95  CVector( INT32 aWidth );
99  ~CVector();
106  unsigned Hash();
111  int HasXZ() { return bval != 0; }
116  int HasZ( void ) { return (~aval & bval) != 0; }
121  int HasX( void ) { return (aval & bval) != 0; }
126  int IsNegative()
127  { return _signed && ((aval>>(width-1)) & 1) != 0 && !HasXZ(); }
132  void SetPreferredBase( int base ) { preferredBase = base; }
137  int GetPreferredBase() { return preferredBase; }
143  int Sized() { return sized; }
149  void Sized( int sized ) { this->sized = sized; }
155  int Based() { return based; }
161  void Based( int based ) { this->based = based; }
167  int Unbased() { return unbased; }
173  void Unbased( int unbased ) { this->unbased = unbased; }
178  int Signed() const { return _signed; }
183  void Signed( int _signed ) { this->_signed = _signed; }
189  int Overflowed() { return overflowed; }
194  INT32 GetWidth( void );
202  void SetWidth( INT32 newWidth );
206  void SetToX();
211  const CVector& operator=( const CVector& v );
216  UINT32 operator=( UINT32 v );
221  int LoadDecimal( const char* string );
226  int LoadBinary( const char* string );
231  int LoadOctal( const char* string );
236  int LoadHex( const char* string );
241  int LoadString( const char* string );
248  char* GetVString( void );
255  char* GetDecimal( void );
262  char* GetBinary( void );
269  char* GetOctal( void );
276  char* GetHex( void );
284  char* GetString( void );
290  int operator==( CVector& v );
296  int operator==( UINT32 i );
302  int operator!=( CVector& v ) { return !(*this == v); }
308  int operator!=( UINT32 i ) { return !(*this == i); }
314  INT32 GetINT32( void )
315  {
316  if( IsNegative() ) {
317  CVector n(GetWidth());
318  n.Signed(1);
319  Neg( &n, this );
320  return -(INT32)n.aval.GetUINT32();
321  }
322 
323  return aval.GetUINT32();
324  }
330  INT64 GetINT64( void )
331  {
332  if( IsNegative() ) {
333  CVector n(GetWidth());
334  n.Signed(1);
335  Neg( &n, this );
336  return -(INT64)n.aval.GetUINT64();
337  }
338 
339  return aval.GetUINT64();
340  }
345  void LoadINT32( INT32 v )
346  {
347  aval = (unsigned int)v;
348  bval = 0;
349  }
354  void LoadReal( double d );
359  double GetReal();
364  const CUInt& Aval() {
365  return aval;
366  }
371  const CUInt& Bval() {
372  return bval;
373  }
378  void Aval( const CUInt& v ) {
379  aval = v;
380  }
385  void Bval( const CUInt& v ) {
386  bval = v;
387  }
388 
392  friend void Add( CVector* r, CVector* a, CVector* b );
393  friend void Sub( CVector* r, CVector* a, CVector* b );
394  friend void Mul( CVector* r, CVector* a, CVector* b );
395  friend void Div( CVector* r, CVector* a, CVector* b );
396  friend void Pow( CVector* r, CVector* a, CVector* b );
397  friend void Lsha( CVector* r, CVector* a, CVector* b );
398  friend void Rsha( CVector* r, CVector* a, CVector* b );
399  friend void Lsh( CVector* r, CVector* a, CVector* b );
400  friend void Rsh( CVector* r, CVector* a, CVector* b );
401  friend void Cat( CVector* r, CVector* a, CVector* b );
402  friend void Rep( CVector* r, CVector* a, CVector* b );
403  friend void Mod( CVector* r, CVector* a, CVector* b );
404  friend void And( CVector* r, CVector* a, CVector* b );
405  friend void Or( CVector* r, CVector* a, CVector* b );
406  friend void Xor( CVector* r, CVector* a, CVector* b );
407  friend void Xnor( CVector* r, CVector* a, CVector* b );
408  friend void Com( CVector* r, CVector* a );
409  friend void Neg( CVector* r, CVector* a );
410  friend void Plus( CVector* r, CVector* a );
411  friend void Not( CVector* r, CVector* a );
412  friend void Le( CVector* r, CVector* a, CVector* b );
413  friend void Lt( CVector* r, CVector* a, CVector* b );
414  friend void Ge( CVector* r, CVector* a, CVector* b );
415  friend void Gt( CVector* r, CVector* a, CVector* b );
416  friend void Eq( CVector* r, CVector* a, CVector* b );
417  friend void Ne( CVector* r, CVector* a, CVector* b );
418  friend void Ceq( CVector* r, CVector* a, CVector* b );
419  friend void Cne( CVector* r, CVector* a, CVector* b );
420  friend void Lor( CVector* r, CVector* a, CVector* b );
421  friend void Land( CVector* r, CVector* a, CVector* b );
422  friend void Rand( CVector* r, CVector* a );
423  friend void Ror( CVector* r, CVector* a );
424  friend void Rnand( CVector* r, CVector* a );
425  friend void Rnor( CVector* r, CVector* a );
426  friend void Rxor( CVector* r, CVector* a );
427  friend void Rxnor( CVector* r, CVector* a );
428  friend void Hook( CVector* r, CVector* c, CVector* a, CVector* b );
432  int IsWidthConstant() { return TRUE; }
433  int IsWidthVolatile() { return FALSE; }
434  int IsWidthEvaluateable() { return TRUE; }
435  CNode* GetWidthExp();
436  virtual NodeType_t GetNodeType( void );
437 private:
438  void Adjust( void );
439  void GrowBuffer( int bytes )
440  {
441  if( bytes <= bufferSize ) {
442  return;
443  }
444  if( buffer != NULL ) {
445  shell_xfree( buffer, bufferSize );
446  }
447  bufferSize = bytes * 2;
448  buffer = (char*) shell_xmalloc( bufferSize );
449  }
450  void SetHeap( CObstack* aHeap ) {
451  aval.SetHeap( aHeap );
452  bval.SetHeap( aHeap );
453  }
460  void* operator new(size_t size, CObstack* heap) {
461  return heap->Alloc( size );
462  }
466 };
467 
473 inline void Le( CVector* r, double* a, double* b )
474 {
475  r->LoadINT32( *a <= *b );
476 }
477 
478 
479 inline void Le( CVector* r, INT32 a, INT32 b )
480 {
481  r->LoadINT32( a <= b );
482 }
483 
484 inline void Lt( CVector* r, double* a, double* b )
485 {
486  r->LoadINT32( *a < *b );
487 }
488 
489 
490 inline void Lt( CVector* r, INT32 a, INT32 b )
491 {
492  r->LoadINT32( a < b );
493 }
494 
495 inline void Ge( CVector* r, double* a, double* b )
496 {
497  r->LoadINT32( *a >= *b );
498 }
499 
500 
501 inline void Ge( CVector* r, INT32 a, INT32 b )
502 {
503  r->LoadINT32( a >= b );
504 }
505 
506 inline void Gt( CVector* r, double* a, double* b )
507 {
508  r->LoadINT32( *a > *b );
509 }
510 
511 
512 inline void Gt( CVector* r, INT32 a, INT32 b )
513 {
514  r->LoadINT32( a > b );
515 }
516 
517 
518 inline void Ne( CVector* r, double* a, double* b )
519 {
520  r->LoadINT32( *a != *b );
521 }
522 
523 inline void Ne( CVector* r, INT32 a, INT32 b )
524 {
525  r->LoadINT32( a != b );
526 }
527 
528 inline void Eq( CVector* r, double* a, double* b )
529 {
530  r->LoadINT32( *a == *b );
531 }
532 
533 inline void Eq( CVector* r, INT32 a, INT32 b )
534 {
535  r->LoadINT32( a == b );
536 }
537 
538 inline void Cne( CVector*, double*, double* )
539 {
540  fatal( NULL, "!== illegal for reals" );
541 }
542 
543 inline void Cne( CVector* r, INT32 a, INT32 b )
544 {
545  r->LoadINT32( a != b );
546 }
547 
548 inline void Ceq( CVector*, double*, double* )
549 {
550  fatal( NULL, "=== illegal for reals" );
551 }
552 
553 inline void Ceq( CVector* r, INT32 a, INT32 b )
554 {
555  r->LoadINT32( a == b );
556 }
557 
561 inline void LogicalValue( CVector* dst, CVector* a )
562 {
563  CVector zero(a->GetWidth());
564  Ne( dst, a, &zero );
565 }
566 
567 inline void Hook( double* r, CVector* c, double* a, double* b )
568 {
569  CVector cond(1);
570  LogicalValue( &cond, c );
571  if( cond.HasXZ() ) {
572  *r = 0;
573  } else if( cond == 0) {
574  *r = *b;
575  } else {
576  *r = *a;
577  }
578 }
579 
580 inline void Cat( double*, double*, double* )
581 {
582  fatal( NULL, "=== illegal for reals" );
583 }
588 #endif // CVECTOR_HPP
void * shell_xmalloc(int s)
Definition: main.cc:369
Infinite precision unsigned arithmetic class Storage within object will be used whenever possible...
Definition: cuint.h:50
void Unbased(int unbased)
Set unbased attribute.
Definition: cvector.h:173
void Add(double *r, double *a, double *b)
Definition: cnode.h:682
void fatal(struct Coord_t *location, const char *format,...)
This routine should not be used by plugins.
void SetPreferredBase(int base)
Set preferred base for printing value.
Definition: cvector.h:132
int Overflowed()
Determine if conversion from ascii overflowed the give vector width.
Definition: cvector.h:189
void Neg(double *r, double *a)
Definition: cnode.h:702
void SetToX()
Set vector value to X.
int HasX(void)
Determine if any bit is a x.
Definition: cvector.h:121
char * GetDecimal(void)
Convert vector to decimal string.
void SetWidth(INT32 newWidth)
Set width of vector in bits.
void shell_xfree(void *p, int s)
Definition: main.cc:365
long INT32
Short cut for signed 32 bit integer.
Definition: glue.h:38
int operator!=(CVector &v)
Inequality operator.
Definition: cvector.h:302
void Signed(int _signed)
set signed attribute.
Definition: cvector.h:183
static CVector * AllocFromHeap(CObstack *aHeap, int width)
Create vector allocating all storage from given heap.
Definition: cvector.h:85
void Bval(const CUInt &v)
set bval part of this vector.
Definition: cvector.h:385
signed long long INT64
Short cut for signed 64 bit integer.
Definition: glue.h:51
unsigned Hash()
Calculate a hash for vector value.
void Based(int based)
Set based attribute.
Definition: cvector.h:161
int HasXZ()
Determine if any bit is either x or z.
Definition: cvector.h:111
void Sized(int sized)
Set sized attribute.
Definition: cvector.h:149
int Based()
Get based attribute.
Definition: cvector.h:155
int HasZ(void)
Determine if any bit is a z.
Definition: cvector.h:116
Bulk object allocation object.
Definition: cobstack.h:46
void LoadReal(double d)
Load vector with integer part of real value.
int operator!=(UINT32 i)
Inequality operator.
Definition: cvector.h:308
void Aval(const CUInt &v)
set aval part of this vector.
Definition: cvector.h:378
Primary data structure representing parse tree nodes.
Definition: cnode.h:197
const CUInt & Aval()
return aval part of this vector.
Definition: cvector.h:364
INT32 GetINT32(void)
Get value as a 32 bit integer.
Definition: cvector.h:314
UINT64 GetUINT64() const
int Unbased()
Get unbased attribute.
Definition: cvector.h:167
void Pow(double *r, double *a, double *b)
Definition: cnode.h:712
char * GetString(void)
Convert vector to ascii string.
int LoadDecimal(const char *string)
Load decimal value from string.
char * GetBinary(void)
Convert vector to binary string.
void Div(double *r, double *a, double *b)
Definition: cnode.h:697
UINT32 GetUINT32() const
void SetHeap(CObstack *aHeap)
Set heap to be used for storage allocation.
Definition: cuint.h:119
int IsNegative()
Determine if value is negative.
Definition: cvector.h:126
void Sub(double *r, double *a, double *b)
Definition: cnode.h:687
const CUInt & Bval()
return bval part of this vector.
Definition: cvector.h:371
int Sized()
Get sized attribute.
Definition: cvector.h:143
char * GetOctal(void)
Convert vector to octal string.
double GetReal()
Get vector value as a real.
CVector(INT32 aWidth)
Create bit vector of given width.
Bit vector class for implementing 4 state verilog signed and unsigned arithmetic. ...
Definition: cvector.h:58
void LoadINT32(INT32 v)
Load vector with integer value.
Definition: cvector.h:345
INT64 GetINT64(void)
Get value as a 64 bit integer.
Definition: cvector.h:330
unsigned long UINT32
Short cut for unsigned 32 bit integer.
Definition: glue.h:47
void Mul(double *r, double *a, double *b)
Definition: cnode.h:692
int LoadOctal(const char *string)
Load octal value from string.
int GetPreferredBase()
Get preferred base for printing value.
Definition: cvector.h:137
void Plus(double *r, double *a)
Definition: cnode.h:707
INT32 GetWidth(void)
Get vector bit width.
char * GetVString(void)
Convert vector to string using preferred base.
int LoadString(const char *string)
Load string value from string.
~CVector()
Destroy vector freeing storage if possible.
NodeType_t
Expression node type.
Definition: cdatatype.h:101
int Signed() const
Get signed attribute.
Definition: cvector.h:178
char * GetHex(void)
Convert vector to hex string.
const CVector & operator=(const CVector &v)
Assignment operator.
int operator==(CVector &v)
Equality operator.
int LoadHex(const char *string)
Load hex value from string.
int LoadBinary(const char *string)
Load binary value from string.