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
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #ifndef SCV_EXPRESSION_H
00051 #define SCV_EXPRESSION_H
00052
00053 #include "scv/scv_object_if.h"
00054 #include "scv/scv_shared_ptr.h"
00055 #include <list>
00056
00057 class sc_interface;
00058 template <class T> class sc_signal_in_if;
00059
00060 class scv_expression_core_base;
00061
00062 class scv_expression : public scv_object_if {
00063 public:
00064 enum operatorT {
00065 EMPTY,
00066 EXTENSION,
00067 INT_CONSTANT,
00068 BOOLEAN_CONSTANT,
00069 UNSIGNED_CONSTANT,
00070 DOUBLE_CONSTANT,
00071 SC_BIGINT_CONSTANT,
00072 SC_BIGUINT_CONSTANT,
00073 SC_BV_CONSTANT,
00074 STRING_CONSTANT,
00075 SC_STRING_CONSTANT,
00076 SC_SIGNAL,
00077 EQUAL,
00078 NOT_EQUAL,
00079 GREATER_THAN,
00080 LESS_THAN,
00081 GREATER_OR_EQUAL,
00082 LESS_OR_EQUAL,
00083 AND,
00084 OR,
00085 NOT,
00086 PLUS,
00087 MINUS,
00088 MULTIPLY
00089 };
00090 public:
00091 scv_expression(scv_expression_core_base * core = NULL);
00092 scv_expression(const scv_expression& rhs);
00093 scv_expression(bool b);
00094 scv_expression(int i);
00095 scv_expression(long long i);
00096 scv_expression(unsigned u);
00097 scv_expression(unsigned long long i);
00098 scv_expression(double d);
00099 scv_expression(string);
00100 scv_expression(sc_string);
00101 template <int W>
00102 static scv_expression create_constant(const sc_int<W>& v) {
00103 return _scv_create_expression(v);
00104 }
00105 template <int W>
00106 static scv_expression create_constant(const sc_uint<W>& v) {
00107 return _scv_create_expression(v);
00108 }
00109 template <int W>
00110 static scv_expression create_constant(const sc_bigint<W>& v) {
00111 return _scv_create_expression(v);
00112 }
00113 template <int W>
00114 static scv_expression create_constant(const sc_biguint<W>& v) {
00115 return _scv_create_expression(v);
00116 }
00117 template <int W>
00118 static scv_expression create_constant(const sc_bv<W>& v) {
00119 return _scv_create_expression(v);
00120 }
00121 template <class T>
00122 static scv_expression create_reference(sc_signal_in_if<T>& s){
00123 return _scv_create_expression(s);
00124 }
00125
00126 virtual ~scv_expression();
00127
00128
00129 friend scv_expression operator==(const scv_expression& a,
00130 const scv_expression& b);
00131 friend scv_expression operator!=(const scv_expression& a,
00132 const scv_expression& b);
00133 friend scv_expression operator>(const scv_expression& a,
00134 const scv_expression& b);
00135 friend scv_expression operator<(const scv_expression& a,
00136 const scv_expression& b);
00137 friend scv_expression operator>=(const scv_expression& a,
00138 const scv_expression& b);
00139 friend scv_expression operator<=(const scv_expression& a,
00140 const scv_expression& b);
00141 friend scv_expression operator&&(const scv_expression& a,
00142 const scv_expression& b);
00143 friend scv_expression operator||(const scv_expression& a,
00144 const scv_expression& b);
00145 friend scv_expression operator!(const scv_expression& a);
00146 friend scv_expression operator+(const scv_expression& a,
00147 const scv_expression& b);
00148 friend scv_expression operator-(const scv_expression& a,
00149 const scv_expression& b);
00150 friend scv_expression operator*(const scv_expression& a,
00151 const scv_expression& b);
00152 public:
00153 scv_expression& operator=(bool);
00154 scv_expression& operator&=(const scv_expression& e);
00155 public:
00156 bool evaluate(void) const;
00157 public:
00158 long long get_int_value(void) const ;
00159 bool get_bool_value(void) const ;
00160 unsigned long long get_unsigned_value(void) const;
00161 double get_double_value(void) const;
00162 int get_bit_width(void) const;
00163 scv_extensions_if* get_extension(void) const;
00164 sc_interface* get_signal(void) const;
00165 void get_extension_list(list<scv_extensions_if*>& ext_list);
00166 void get_signal_list(list<sc_interface*>& sig_list);
00167 public:
00168 void get_value(bool&) const;
00169 void get_value(char&) const;
00170 void get_value(short&) const;
00171 void get_value(unsigned short&) const;
00172 void get_value(int&) const;
00173 void get_value(unsigned int&) const;
00174 void get_value(long&) const;
00175 void get_value(unsigned long&) const;
00176 void get_value(long long&) const;
00177 void get_value(unsigned long long&) const;
00178 void get_value(float&) const;
00179 void get_value(double&) const;
00180 void get_value(string&) const;
00181 void get_value(sc_string&) const;
00182 void get_value(sc_bv_base&) const;
00183 void get_value(sc_lv_base&) const;
00184 public:
00185 const scv_expression& get_left(void) const;
00186 const scv_expression& get_right(void) const;
00187 scv_expression::operatorT get_operator(void) const;
00188 const char* get_expression_string(void) const;
00189 public:
00190 const char *get_name() const;
00191 const char *kind() const;
00192 void print(ostream& o=scv_out, int details=0, int indent=0) const;
00193 void show(int details=0, int indent=0) const;
00194 static int get_debug();
00195 static void set_debug(int i);
00196 public:
00197 template <class T>
00198 static scv_expression create(sc_signal_in_if<T>& s){
00199 return _scv_create_expression(s);
00200 }
00201 private:
00202 void update_signal_value(void) const;
00203 friend void _scv_update_signal_value(const scv_expression& e);
00204 private:
00205 scv_shared_ptr<scv_expression_core_base> core_;
00206 static int _debug;
00207 };
00208
00209 class _scv_expression_error {
00210 public:
00211 static void illegalAccess(const char * msgP) {
00212 _scv_message::message(_scv_message::EXPRESSION_ILLEGAL_EXTRACTION,msgP);
00213 }
00214 };
00215
00216 class scv_expression_core_base {
00217 public:
00218 virtual ~scv_expression_core_base() {};
00219 virtual const char *get_name(void) const = 0;
00220 virtual const scv_expression& get_left(void) const = 0;
00221 virtual const scv_expression& get_right(void) const = 0;
00222 virtual long long get_int_value(void) const = 0;
00223 virtual bool get_bool_value(void) const = 0;
00224 virtual unsigned long long get_unsigned_value(void) const = 0;
00225 virtual double get_double_value(void) const = 0;
00226 virtual int get_bit_width(void) const = 0;
00227 virtual scv_extensions_if * get_extension(void) const = 0;
00228 virtual sc_interface * get_signal(void) const = 0;
00229 virtual scv_expression::operatorT get_operator(void) const = 0;
00230 virtual void update_signal_value(void) const = 0;
00231 virtual void get_value(bool&) const = 0;
00232 virtual void get_value(char&) const = 0;
00233 virtual void get_value(short&) const = 0;
00234 virtual void get_value(unsigned short&) const = 0;
00235 virtual void get_value(int&) const = 0;
00236 virtual void get_value(unsigned int&) const = 0;
00237 virtual void get_value(long&) const = 0;
00238 virtual void get_value(unsigned long&) const = 0;
00239 virtual void get_value(long long&) const = 0;
00240 virtual void get_value(unsigned long long&) const = 0;
00241 virtual void get_value(float&) const = 0;
00242 virtual void get_value(double&) const = 0;
00243 virtual void get_value(string&) const = 0;
00244 virtual void get_value(sc_string&) const = 0;
00245 virtual void get_value(sc_bv_base&) const = 0;
00246 virtual void get_value(sc_lv_base&) const = 0;
00247 };
00248
00249 template <typename T>
00250 scv_extensions<T> scv_get_extensions(T& d);
00251
00252 template <typename T>
00253 const scv_extensions<T> scv_get_const_extensions(const T& d);
00254
00255
00256
00257 class scv_expression_core : public scv_expression_core_base {
00258 protected:
00259 mutable scv_extensions_if * core_;
00260 union {
00261 long long _intValue;
00262 int _boolValue;
00263 unsigned long long _unsignedValue;
00264 double _doubleValue;
00265 string* _str;
00266 sc_string* _sc_str;
00267 }_value;
00268 sc_bv_base *_data;
00269 int _bit_width;
00270 scv_expression::operatorT _operator;
00271 scv_expression _left;
00272 scv_expression _right;
00273 public:
00274 scv_expression_core(scv_extensions_if * core);
00275 scv_expression_core(int i);
00276 scv_expression_core(long long i);
00277 scv_expression_core(bool i);
00278 scv_expression_core(unsigned u);
00279 scv_expression_core(unsigned long long u);
00280 scv_expression_core(double d);
00281 scv_expression_core(string s);
00282 scv_expression_core(sc_string s);
00283 template <int W>
00284 scv_expression_core(sc_int<W> v) : core_(NULL), _data(NULL) {
00285 _value._intValue = v;
00286 _bit_width = W;
00287 _operator = scv_expression::INT_CONSTANT;
00288 }
00289 template <int W>
00290 scv_expression_core(sc_uint<W> v) : core_(NULL), _data(NULL) {
00291 _value._unsignedValue = v;
00292 _bit_width = W;
00293 _operator = scv_expression::UNSIGNED_CONSTANT;
00294 }
00295 template <int W>
00296 scv_expression_core(sc_bigint<W> v) : core_(NULL) {
00297 _data = new sc_bv_base(W);
00298 _bit_width = W;
00299 *_data = v;
00300 _operator = scv_expression::SC_BIGINT_CONSTANT;
00301 }
00302 template <int W>
00303 scv_expression_core(sc_biguint<W> v) : core_(NULL) {
00304 _data = new sc_bv_base(W);
00305 *_data = v;
00306 _bit_width = W;
00307 _operator = scv_expression::SC_BIGUINT_CONSTANT;
00308 }
00309 template <int W>
00310 scv_expression_core(sc_bv<W> v) : core_(NULL) {
00311 _data = new sc_bv_base(W);
00312 _bit_width = W;
00313 *_data = v;
00314 _operator = scv_expression::SC_BV_CONSTANT;
00315 }
00316 virtual ~scv_expression_core();
00317 public:
00318 const char *get_name(void) const;
00319 virtual void update_signal_value(void) const;
00320 long long get_int_value(void) const;
00321 bool get_bool_value(void) const;
00322 unsigned long long get_unsigned_value(void) const;
00323 double get_double_value(void) const;
00324 int get_bit_width(void) const {
00325 return _bit_width;
00326 }
00327 scv_extensions_if * get_extension(void) const;
00328 virtual sc_interface * get_signal(void) const;
00329 const scv_expression& get_left(void) const {
00330 return _left;
00331 }
00332 const scv_expression& get_right(void) const {
00333 return _right;
00334 }
00335 scv_expression::operatorT get_operator(void) const {
00336 return _operator;
00337 }
00338 scv_expression_core(scv_expression::operatorT op,
00339 const scv_expression& a , const scv_expression& b) :
00340 _operator(op), _left(a), _right(b) {}
00341 public:
00342 void get_value(bool&) const;
00343 void get_value(char&) const;
00344 void get_value(short&) const;
00345 void get_value(unsigned short&) const;
00346 void get_value(int&) const;
00347 void get_value(unsigned int&) const;
00348 void get_value(long&) const;
00349 void get_value(unsigned long&) const;
00350 void get_value(long long&) const;
00351 void get_value(unsigned long long&) const;
00352 void get_value(float&) const;
00353 void get_value(double&) const;
00354 void get_value(string&) const;
00355 void get_value(sc_string&) const;
00356 void get_value(sc_bv_base&) const;
00357 void get_value(sc_lv_base&) const;
00358 protected:
00359 scv_expression_core(scv_expression::operatorT op) : _data(NULL), _operator(op) {
00360 }
00361 };
00362
00363 template<typename T>
00364 class scv_expression_core_signal : public scv_expression_core {
00365 sc_signal_in_if<T>* sig_;
00366 public:
00367 scv_expression_core_signal(sc_signal_in_if<T>& s) : scv_expression_core(scv_expression::SC_SIGNAL) {
00368 const scv_extensions<T> e = scv_get_const_extensions(s.read());
00369 core_ = new scv_extensions<T>(e);
00370 sig_ = &s;
00371 }
00372
00373 virtual void update_signal_value(void) const {
00374 assert(_operator == scv_expression::SC_SIGNAL);
00375 const scv_extensions<T> e = scv_get_const_extensions(sig_->read());
00376 *((scv_extensions<T>*) core_) = e;
00377 }
00378
00379 virtual sc_interface * get_signal(void) const {
00380 return sig_;
00381 }
00382 };
00383
00384
00385
00386 #define _SCV_GET_CONSTANT_VALUE() \
00387 if (_operator == scv_expression::BOOLEAN_CONSTANT) { \
00388 val = _value._boolValue; \
00389 } else if (_operator == scv_expression::INT_CONSTANT) { \
00390 val = _value._intValue; \
00391 } else if (_operator == scv_expression::UNSIGNED_CONSTANT) { \
00392 val = _value._unsignedValue; \
00393 } else if (_operator == scv_expression::SC_BIGINT_CONSTANT || \
00394 _operator == scv_expression::SC_BIGUINT_CONSTANT || \
00395 _operator == scv_expression::SC_BV_CONSTANT ) { \
00396 sc_signed value(_bit_width); \
00397 value = (*_data);
00398
00399 #define _SCV_GET_CONSTANT_ERROR(type_name) \
00400 } else { \
00401 _scv_expression_error::illegalAccess("get_value(#type_name&)");\
00402 return ; \
00403 }
00404
00405 #define _SCV_GET_SC_VAL(type_name, method) \
00406 _SCV_GET_CONSTANT_VALUE(); \
00407 val = (type_name)value.method(); \
00408 _SCV_GET_CONSTANT_ERROR(type_name); \
00409
00410
00411
00412 template <class T>
00413 inline scv_expression _scv_create_expression(sc_signal_in_if<T>& s)
00414 {
00415 return scv_expression(new scv_expression_core_signal<T>(s));
00416 }
00417
00418 template <int W>
00419 inline scv_expression _scv_create_expression(const sc_int<W>& v)
00420 {
00421 return scv_expression(new scv_expression_core(v));
00422 }
00423
00424 template <int W>
00425 inline scv_expression _scv_create_expression(const sc_uint<W>& v)
00426 {
00427 return scv_expression(new scv_expression_core(v));
00428 }
00429
00430 template <int W>
00431 inline scv_expression _scv_create_expression(const sc_bigint<W>& v)
00432 {
00433 return scv_expression(new scv_expression_core(v));
00434 }
00435
00436 template <int W>
00437 inline scv_expression _scv_create_expression(const sc_biguint<W>& v)
00438 {
00439 return scv_expression(new scv_expression_core(v));
00440 }
00441
00442 template <int W>
00443 inline scv_expression _scv_create_expression(const sc_bv<W>& v)
00444 {
00445 return scv_expression(new scv_expression_core(v));
00446 }
00447
00448
00449 #endif