00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef CVECTOR_HPP
00032 #define CVECTOR_HPP
00033
00034 #include <stdio.h>
00035 #include <math.h>
00036 #include "glue.h"
00037 #include "cuint.h"
00038 #include "cdecl.h"
00039
00040
00058 class CVector
00059 {
00060 private:
00061 CUInt aval;
00062 CUInt bval;
00063 CUInt hook;
00064 INT32 width;
00065 int sized;
00066
00067 int based;
00068
00069 int unbased;
00070
00071 int preferredBase;
00072
00073 int _signed;
00074 int overflowed;
00075
00076 static int bufferSize;
00077 static char* buffer;
00078 public:
00085 static CVector* AllocFromHeap( CObstack* aHeap, int width ) {
00086 CVector* v = new(aHeap) CVector( width );
00087 v->SetHeap( aHeap );
00088 return v;
00089 }
00090
00095 CVector( INT32 aWidth );
00099 ~CVector();
00106 unsigned Hash();
00111 int HasXZ() { return bval != 0; }
00116 int HasZ( void ) { return (~aval & bval) != 0; }
00121 int IsNegative()
00122 { return _signed && ((aval>>(width-1)) & 1) != 0 && !HasXZ(); }
00127 void SetPreferredBase( int base ) { preferredBase = base; }
00132 int GetPreferredBase() { return preferredBase; }
00138 int Sized() { return sized; }
00144 void Sized( int sized ) { this->sized = sized; }
00150 int Based() { return based; }
00156 void Based( int based ) { this->based = based; }
00162 int Unbased() { return unbased; }
00168 void Unbased( int unbased ) { this->unbased = unbased; }
00173 int Signed() const { return _signed; }
00178 void Signed( int _signed ) { this->_signed = _signed; }
00184 int Overflowed() { return overflowed; }
00189 INT32 GetWidth( void );
00197 void SetWidth( INT32 newWidth );
00201 void SetToX();
00206 const CVector& operator=( const CVector& v );
00211 UINT32 operator=( UINT32 v );
00216 int LoadDecimal( const char* string );
00221 int LoadBinary( const char* string );
00226 int LoadOctal( const char* string );
00231 int LoadHex( const char* string );
00236 int LoadString( const char* string );
00243 char* GetVString( void );
00250 char* GetDecimal( void );
00257 char* GetBinary( void );
00264 char* GetOctal( void );
00271 char* GetHex( void );
00279 char* GetString( void );
00285 int operator==( CVector& v );
00291 int operator==( UINT32 i );
00297 int operator!=( CVector& v ) { return !(*this == v); }
00303 int operator!=( UINT32 i ) { return !(*this == i); }
00309 INT32 GetINT32( void )
00310 {
00311 if( IsNegative() ) {
00312 CVector n(GetWidth());
00313 n.Signed(1);
00314 Neg( &n, this );
00315 return -(INT32)n.aval.GetUINT32();
00316 }
00317
00318 return aval.GetUINT32();
00319 }
00325 INT64 GetINT64( void )
00326 {
00327 if( IsNegative() ) {
00328 CVector n(GetWidth());
00329 n.Signed(1);
00330 Neg( &n, this );
00331 return -(INT64)n.aval.GetUINT64();
00332 }
00333
00334 return aval.GetUINT64();
00335 }
00340 void LoadINT32( INT32 v )
00341 {
00342 aval = (unsigned int)v;
00343 bval = 0;
00344 }
00349 void LoadReal( double d );
00354 double GetReal();
00359 void GetAval( CVector& v ) {
00360 v.aval = aval;
00361 bval = 0;
00362 }
00367 void GetBval( CVector& v ) {
00368 v.aval = bval;
00369 bval = 0;
00370 }
00371
00375 friend void Add( CVector* r, CVector* a, CVector* b );
00376 friend void Sub( CVector* r, CVector* a, CVector* b );
00377 friend void Mul( CVector* r, CVector* a, CVector* b );
00378 friend void Div( CVector* r, CVector* a, CVector* b );
00379 friend void Pow( CVector* r, CVector* a, CVector* b );
00380 friend void Lsha( CVector* r, CVector* a, CVector* b );
00381 friend void Rsha( CVector* r, CVector* a, CVector* b );
00382 friend void Lsh( CVector* r, CVector* a, CVector* b );
00383 friend void Rsh( CVector* r, CVector* a, CVector* b );
00384 friend void Cat( CVector* r, CVector* a, CVector* b );
00385 friend void Rep( CVector* r, CVector* a, CVector* b );
00386 friend void Mod( CVector* r, CVector* a, CVector* b );
00387 friend void And( CVector* r, CVector* a, CVector* b );
00388 friend void Or( CVector* r, CVector* a, CVector* b );
00389 friend void Xor( CVector* r, CVector* a, CVector* b );
00390 friend void Xnor( CVector* r, CVector* a, CVector* b );
00391 friend void Com( CVector* r, CVector* a );
00392 friend void Neg( CVector* r, CVector* a );
00393 friend void Plus( CVector* r, CVector* a );
00394 friend void Not( CVector* r, CVector* a );
00395 friend void Le( CVector* r, CVector* a, CVector* b );
00396 friend void Lt( CVector* r, CVector* a, CVector* b );
00397 friend void Ge( CVector* r, CVector* a, CVector* b );
00398 friend void Gt( CVector* r, CVector* a, CVector* b );
00399 friend void Eq( CVector* r, CVector* a, CVector* b );
00400 friend void Ne( CVector* r, CVector* a, CVector* b );
00401 friend void Ceq( CVector* r, CVector* a, CVector* b );
00402 friend void Cne( CVector* r, CVector* a, CVector* b );
00403 friend void Lor( CVector* r, CVector* a, CVector* b );
00404 friend void Land( CVector* r, CVector* a, CVector* b );
00405 friend void Rand( CVector* r, CVector* a );
00406 friend void Ror( CVector* r, CVector* a );
00407 friend void Rnand( CVector* r, CVector* a );
00408 friend void Rnor( CVector* r, CVector* a );
00409 friend void Rxor( CVector* r, CVector* a );
00410 friend void Rxnor( CVector* r, CVector* a );
00411 friend void Hook( CVector* r, CVector* c, CVector* a, CVector* b );
00415 int IsWidthConstant() { return TRUE; }
00416 int IsWidthVolatile() { return FALSE; }
00417 int IsWidthEvaluateable() { return TRUE; }
00418 CNode* GetWidthExp();
00419 virtual NodeType_t GetNodeType( void );
00420 private:
00421 void Adjust( void );
00422 void GrowBuffer( int bytes )
00423 {
00424 if( bytes <= bufferSize ) {
00425 return;
00426 }
00427 if( buffer != NULL ) {
00428 shell_xfree( buffer, bufferSize );
00429 }
00430 bufferSize = bytes * 2;
00431 buffer = (char*) shell_xmalloc( bufferSize );
00432 }
00433 void SetHeap( CObstack* aHeap ) {
00434 aval.SetHeap( aHeap );
00435 bval.SetHeap( aHeap );
00436 }
00443 void* operator new(size_t size, CObstack* heap) {
00444 return heap->Alloc( size );
00445 }
00449 };
00450
00456 inline void Le( CVector* r, double* a, double* b )
00457 {
00458 r->LoadINT32( *a <= *b );
00459 }
00460
00461
00462 inline void Le( CVector* r, INT32 a, INT32 b )
00463 {
00464 r->LoadINT32( a <= b );
00465 }
00466
00467 inline void Lt( CVector* r, double* a, double* b )
00468 {
00469 r->LoadINT32( *a < *b );
00470 }
00471
00472
00473 inline void Lt( CVector* r, INT32 a, INT32 b )
00474 {
00475 r->LoadINT32( a < b );
00476 }
00477
00478 inline void Ge( CVector* r, double* a, double* b )
00479 {
00480 r->LoadINT32( *a >= *b );
00481 }
00482
00483
00484 inline void Ge( CVector* r, INT32 a, INT32 b )
00485 {
00486 r->LoadINT32( a >= b );
00487 }
00488
00489 inline void Gt( CVector* r, double* a, double* b )
00490 {
00491 r->LoadINT32( *a > *b );
00492 }
00493
00494
00495 inline void Gt( CVector* r, INT32 a, INT32 b )
00496 {
00497 r->LoadINT32( a > b );
00498 }
00499
00500
00501 inline void Ne( CVector* r, double* a, double* b )
00502 {
00503 r->LoadINT32( *a != *b );
00504 }
00505
00506 inline void Ne( CVector* r, INT32 a, INT32 b )
00507 {
00508 r->LoadINT32( a != b );
00509 }
00510
00511 inline void Eq( CVector* r, double* a, double* b )
00512 {
00513 r->LoadINT32( *a == *b );
00514 }
00515
00516 inline void Eq( CVector* r, INT32 a, INT32 b )
00517 {
00518 r->LoadINT32( a == b );
00519 }
00520
00521 inline void Cne( CVector*, double*, double* )
00522 {
00523 fatal( NULL, "!== illegal for reals" );
00524 }
00525
00526 inline void Cne( CVector* r, INT32 a, INT32 b )
00527 {
00528 r->LoadINT32( a != b );
00529 }
00530
00531 inline void Ceq( CVector*, double*, double* )
00532 {
00533 fatal( NULL, "=== illegal for reals" );
00534 }
00535
00536 inline void Ceq( CVector* r, INT32 a, INT32 b )
00537 {
00538 r->LoadINT32( a == b );
00539 }
00540
00544 inline void LogicalValue( CVector* dst, CVector* a )
00545 {
00546 CVector zero(a->GetWidth());
00547 Ne( dst, a, &zero );
00548 }
00549
00550 inline void Hook( double* r, CVector* c, double* a, double* b )
00551 {
00552 CVector cond(1);
00553 LogicalValue( &cond, c );
00554 if( cond.HasXZ() ) {
00555 *r = 0;
00556 } else if( cond == 0) {
00557 *r = *b;
00558 } else {
00559 *r = *a;
00560 }
00561 }
00562
00563 inline void Cat( double*, double*, double* )
00564 {
00565 fatal( NULL, "=== illegal for reals" );
00566 }
00571 #endif // CVECTOR_HPP