00001 00045 #ifndef IRHandles_H 00046 #define IRHandles_H 00047 00048 //#define OA_IRHANDLETYPE_UL /* should be in CXXFLAGS */ 00049 #include <sstream> 00050 #include <string> 00051 #include <list> 00052 00053 #include <inttypes.h> 00054 00055 #include <OpenAnalysis/Utils/OA_ptr.hpp> 00056 00058 namespace OA { 00059 00060 00061 //*************************************************************************** 00062 //----------------------------------------------------------------------------- 00063 // Below are generic types for handles to relate objects to a user's IR. 0 is 00064 // reserved as a NULL value. There are two sets of types: 00065 // 1) one whose type/size is relative to the current platform, and 00066 // 2) one whose type/size is maximum, which is useful for cross-platform tools 00067 // 00068 // OA_IRHANDLETYPE_UL: For handles on this platform 00069 // OA_IRHANDLETYPE_SZ64: Maximum size for cross-platform handles 00070 //----------------------------------------------------------------------------- 00071 #if defined(OA_IRHANDLETYPE_UL) 00072 typedef unsigned long irhandle_t; 00073 #elif defined(OA_IRHANDLETYPE_SZ64) 00074 typedef uint64_t irhandle_t; 00075 #else 00076 # error OpenAnalysis handle type must be specified! 00077 #endif 00078 00079 //*************************************************************************** 00080 00081 00088 class IRHandle { 00089 public: 00090 IRHandle() : h(0) { } 00091 IRHandle(irhandle_t h_) : h(h_) { } 00092 ~IRHandle() { } 00093 00094 IRHandle(const IRHandle& x) : h(x.h) { } 00095 IRHandle& operator=(const IRHandle& x) { h = x.h; return *this; } 00096 IRHandle& operator=(irhandle_t h_) { h = h_; return *this; } 00097 00098 bool operator==(const IRHandle& x) const { return (h == x.h); } 00099 bool operator!=(const IRHandle& x) const { return (h != x.h); } 00100 bool operator<(const IRHandle& x) const { return (h < x.h); } 00101 bool operator>(const IRHandle& x) const { return (h > x.h); } 00102 00103 operator bool() { return h != 0; } 00104 operator std::string() { std::ostringstream oss; oss << h; return oss.str(); } 00105 00106 00107 irhandle_t hval() const { return h; } 00108 00109 private: 00110 irhandle_t h; 00111 }; 00112 00113 00114 class ProcHandle : public IRHandle { 00115 public: 00116 ProcHandle() { } 00117 ProcHandle(irhandle_t h_) : IRHandle(h_) { } 00118 ~ProcHandle() { } 00119 00120 ProcHandle(const ProcHandle& x) : IRHandle(x) { } 00121 ProcHandle& operator=(const ProcHandle& x) 00122 { IRHandle::operator=(x); return *this; } 00123 }; 00124 00125 class StmtHandle : public IRHandle { 00126 public: 00127 StmtHandle() { } 00128 StmtHandle(irhandle_t h_) : IRHandle(h_) { } 00129 ~StmtHandle() { } 00130 00131 StmtHandle(const StmtHandle& x) : IRHandle(x) { } 00132 StmtHandle& operator=(const StmtHandle& x) 00133 { IRHandle::operator=(x); return *this; } 00134 }; 00135 00136 class StmtLabel : public IRHandle { 00137 public: 00138 StmtLabel() { } 00139 StmtLabel(irhandle_t h_) : IRHandle(h_) { } 00140 ~StmtLabel() { } 00141 00142 StmtLabel(const StmtLabel& x) : IRHandle(x) { } 00143 StmtLabel& operator=(const StmtLabel& x) 00144 { IRHandle::operator=(x); return *this; } 00145 }; 00146 00147 class SymHandle : public IRHandle { 00148 public: 00149 SymHandle() { } 00150 SymHandle(irhandle_t h_) : IRHandle(h_) { } 00151 ~SymHandle() { } 00152 00153 SymHandle(const SymHandle& x) : IRHandle(x) { } 00154 SymHandle& operator=(const SymHandle& x) 00155 { IRHandle::operator=(x); return *this; } 00156 }; 00157 00158 class ExprHandle : public IRHandle { 00159 public: 00160 ExprHandle() { } 00161 ExprHandle(irhandle_t h_) : IRHandle(h_) { } 00162 ~ExprHandle() { } 00163 00164 ExprHandle(const ExprHandle& x) : IRHandle(x) { } 00165 ExprHandle& operator=(const ExprHandle& x) 00166 { IRHandle::operator=(x); return *this; } 00167 00168 }; 00169 00170 class MemRefHandle : public ExprHandle { 00171 public: 00172 MemRefHandle() { } 00173 MemRefHandle(irhandle_t h_) : ExprHandle(h_) { } 00174 ~MemRefHandle() { } 00175 00176 MemRefHandle(const MemRefHandle& x) : ExprHandle(x) { } 00177 MemRefHandle& operator=(const MemRefHandle& x) 00178 { IRHandle::operator=(x); return *this; } 00179 }; 00180 00181 class CallHandle : public ExprHandle { 00182 public: 00183 CallHandle() { } 00184 CallHandle(irhandle_t h_) : ExprHandle(h_) { } 00185 ~CallHandle() { } 00186 00187 CallHandle(const CallHandle& x) : ExprHandle(x) { } 00188 CallHandle& operator=(const CallHandle& x) 00189 { IRHandle::operator=(x); return *this; } 00190 }; 00191 00192 class OpHandle : public IRHandle { 00193 public: 00194 OpHandle() { } 00195 OpHandle(irhandle_t h_) : IRHandle(h_) { } 00196 ~OpHandle() { } 00197 00198 OpHandle(const OpHandle& x) : IRHandle(x) { } 00199 OpHandle& operator=(const OpHandle& x) 00200 { IRHandle::operator=(x); return *this; } 00201 00202 }; 00203 00204 class ConstSymHandle : public ExprHandle { 00205 public: 00206 ConstSymHandle() { } 00207 ConstSymHandle(irhandle_t h_) : ExprHandle(h_) { } 00208 ~ConstSymHandle() { } 00209 00210 ConstSymHandle(const ConstSymHandle& x) : ExprHandle(x) { } 00211 ConstSymHandle& operator=(const ConstSymHandle& x) 00212 { IRHandle::operator=(x); return *this; } 00213 00214 }; 00215 00216 class ConstValHandle : public ExprHandle { 00217 public: 00218 ConstValHandle() { } 00219 ConstValHandle(irhandle_t h_) : ExprHandle(h_) { } 00220 ~ConstValHandle() { } 00221 00222 ConstValHandle(const ConstValHandle& x) : ExprHandle(x) { } 00223 ConstValHandle& operator=(const ConstValHandle& x) 00224 { IRHandle::operator=(x); return *this; } 00225 00226 }; 00227 00228 00229 00235 // FIXME: needed? 00236 class LeafHandle : public ExprHandle { 00237 public: 00238 LeafHandle() { } 00239 LeafHandle(irhandle_t h_) : ExprHandle(h_) { } 00240 LeafHandle(ExprHandle x) : ExprHandle(x.hval()) { } 00241 ~LeafHandle() { } 00242 00243 LeafHandle(const LeafHandle& x) : ExprHandle(x) { } 00244 LeafHandle& operator=(const LeafHandle& x) 00245 { IRHandle::operator=(x); return *this; } 00246 }; 00247 00248 00249 //********************************************** All the iterators 00250 00256 template <class T> 00257 class IRHandleSetIterator { 00258 public: 00259 IRHandleSetIterator (OA_ptr<std::set<T> > pSet) : mSet(pSet) 00260 { mIter = mSet->begin(); } 00261 virtual ~IRHandleSetIterator () {} 00262 00263 virtual void operator ++ () { if (mIter != mSet->end()) mIter++; } 00264 00266 virtual bool isValid() const { return (mIter != mSet->end()); } 00267 00269 virtual T current() const { return (*mIter); } 00270 00271 virtual void reset() { mIter = mSet->begin(); } 00272 00273 private: 00274 OA_ptr<std::set<T> > mSet; 00275 typename std::set<T>::iterator mIter; 00276 }; 00277 00279 template <class T> 00280 class IRHandleListIterator { 00281 public: 00282 IRHandleListIterator (OA_ptr<std::list<T> > pList) : mList(pList) 00283 { mIter = mList->begin(); } 00284 virtual ~IRHandleListIterator () {} 00285 00286 virtual void operator ++ () { if (mIter != mList->end()) mIter++; } 00287 00289 virtual bool isValid() const { return (mIter != mList->end()); } 00290 00292 virtual T current() const { return (*mIter); } 00293 00294 virtual void reset() { mIter = mList->begin(); } 00295 00296 private: 00297 OA_ptr<std::list<T> > mList; 00298 typename std::list<T>::iterator mIter; 00299 }; 00300 00302 class ProcHandleIterator { 00303 public: 00304 ProcHandleIterator() { } 00305 virtual ~ProcHandleIterator() { }; 00306 00307 virtual ProcHandle current() const = 0; // Returns the current item. 00308 virtual bool isValid() const = 0; // False when all items are exhausted. 00309 00310 virtual void operator++() = 0; // prefix 00311 void operator++(int) { ++*this; } // postfix 00312 // FIXME: doesn't seem to work, for example Open64IRProcIterator 00313 // doesn't see it, however InterActive.cpp:148 does see it 00314 00315 virtual void reset() = 0; 00316 }; 00317 00318 00322 typedef ProcHandleIterator IRProcIterator; 00323 00324 /* 00325 class IRProcIterator { 00326 public: 00327 IRProcIterator() { } 00328 virtual ~IRProcIterator() { }; 00329 00330 virtual ProcHandle current() = 0; // Returns the current item. 00331 virtual bool isValid() = 0; // False when all items are exhausted. 00332 00333 virtual void operator++() = 0; // prefix 00334 void operator++(int) { ++*this; } // postfix 00335 00336 virtual void reset() = 0; 00337 }; 00338 */ 00339 00341 class ExprHandleIterator { 00342 public: 00343 ExprHandleIterator() { } 00344 virtual ~ExprHandleIterator() { }; 00345 00346 virtual ExprHandle current() const = 0; // Returns the current item. 00347 virtual bool isValid() const = 0; // False when all items are exhausted. 00348 00349 virtual void operator++() = 0; // prefix 00350 void operator++(int) { ++*this; } // postfix 00351 00352 virtual void reset() = 0; 00353 }; 00354 00357 typedef ExprHandleIterator IRCallsiteParamIterator; 00358 00360 class StmtHandleIterator { 00361 public: 00362 StmtHandleIterator() { } 00363 virtual ~StmtHandleIterator() { }; 00364 00365 virtual StmtHandle current() const = 0; // Returns the current item. 00366 virtual bool isValid() const = 0; // False when all items are exhausted. 00367 00368 virtual void operator++() = 0; // prefix 00369 void operator++(int) { ++*this; } // postfix 00370 00371 virtual void reset() = 0; 00372 }; 00373 00374 00380 typedef StmtHandleIterator IRRegionStmtIterator; 00381 00386 typedef StmtHandleIterator IRStmtIterator; 00387 00389 class MemRefHandleIterator { 00390 public: 00391 MemRefHandleIterator() { } 00392 virtual ~MemRefHandleIterator() { }; 00393 00394 virtual MemRefHandle current() const = 0; // Returns the current item. 00395 virtual bool isValid() const = 0; // False when all items are exhausted. 00396 00397 virtual void operator++() = 0; // prefix 00398 void operator++(int) { ++*this; } // postfix 00399 00400 virtual void reset() = 0; 00401 }; 00402 00404 class CallHandleIterator { 00405 public: 00406 CallHandleIterator() { } 00407 virtual ~CallHandleIterator() { }; 00408 00409 virtual CallHandle current() const = 0; // Returns the current item. 00410 virtual bool isValid() const = 0; // False when all items are exhausted. 00411 00412 virtual void operator++() = 0; // prefix 00413 void operator++(int) { ++*this; } // postfix 00414 00415 virtual void reset() = 0; 00416 }; 00417 00421 typedef CallHandleIterator IRCallsiteIterator; 00422 00424 class SymHandleIterator { 00425 public: 00426 SymHandleIterator() { } 00427 virtual ~SymHandleIterator() { }; 00428 00429 virtual SymHandle current() const = 0; // Returns the current item. 00430 virtual bool isValid() const = 0; // False when all items are exhausted. 00431 00432 virtual void operator++() = 0; // prefix 00433 void operator++(int) { ++*this; } // postfix 00434 00435 virtual void reset() = 0; 00436 }; 00437 00438 typedef SymHandleIterator IRSymIterator; 00439 typedef SymHandleIterator IRFormalParamIterator; 00440 00441 00442 //******************************************* Used to output handle data 00443 00444 class IRHandlesIRInterface { 00445 00446 public: 00447 virtual ~IRHandlesIRInterface() {} 00448 00449 //-------------------------------------------------------- 00450 // create a string for the given handle, should be succinct 00451 // and there should be no newlines 00452 //-------------------------------------------------------- 00453 virtual std::string toString(const ProcHandle h) = 0; 00454 virtual std::string toString(const StmtHandle h) = 0; 00455 virtual std::string toString(const ExprHandle h) = 0; 00456 virtual std::string toString(const OpHandle h) = 0; 00457 virtual std::string toString(const MemRefHandle h) = 0; 00458 virtual std::string toString(const CallHandle h) = 0; 00459 virtual std::string toString(const SymHandle h) = 0; 00460 virtual std::string toString(const ConstSymHandle h) = 0; 00461 virtual std::string toString(const ConstValHandle h) = 0; 00462 00463 // should deprecate this 00464 //virtual const std::string getName(OA::SymHandle h) = 0; 00465 00466 //-------------------------------------------------------- 00467 // dumps that do pretty prints 00468 //-------------------------------------------------------- 00469 00471 virtual void dump(MemRefHandle h, std::ostream& os) = 0; 00472 00474 virtual void dump(StmtHandle stmt, std::ostream& os) = 0; 00475 00478 //virtual void currentProc(OA::ProcHandle p) = 0; 00479 00480 }; 00481 00482 //*************************************************************************** 00483 00484 /* 00485 #include <iostream> 00486 using std::cout; 00487 using std::endl; 00488 00489 int 00490 main() 00491 { 00492 ProcHandle p(1); 00493 ExprHandle e(2); 00494 LeafHandle l1((irhandle_t)&e); // This is a little awkward... 00495 LeafHandle l2 = (irhandle_t)&e; // This is a little awkward... 00496 00497 cout << "P: " << *p << endl; 00498 cout << "E: " << *e << endl; 00499 cout << "L1: " << *l1 << endl 00500 << "L2: " << *l2 << endl 00501 << "L1 == L2: " << ((l1 == l2) ? "true" : "false") << endl; 00502 00503 00504 ExprHandle ee1(l1); // create expr from leaf 00505 ExprHandle ee2 = l1; // assign a leaf to an expression 00506 ee2 = l1; // assign a leaf to an expression 00507 00508 LeafHandle ll1(ee1); // create leaf from expr 00509 LeafHandle ll2 = ee1; // assign expr to a leaf 00510 ll2 = ee1; // assign expr to a leaf 00511 00512 ExprHandle ee3 = *ll1; // initialize a expr with a leaf 00513 LeafHandle ll3 = *ee1; // initialize a leaf with an expr 00514 00515 ExprHandle* ee4 = dynamic_cast<ExprHandle*>(&ll1); // ok 00516 // LeafHandle* ll4 = dynamic_cast<LeafHandle*>(&ee1); // error 00517 00518 cout << "EE1: " << *ee1 << endl 00519 << "EE2: " << *ee2 << endl 00520 << "EE3: " << *ee3 << endl 00521 << "EE4: " << *(*ee4) << endl; 00522 cout << "LL1: " << *ll1 << endl 00523 << "LL2: " << *ll2 << endl 00524 << "LL3: " << *ll2 << endl; 00525 00526 return 0; 00527 } 00528 */ 00529 00530 } // end of namespace OA 00531 00532 #endif
1.6.1