EvalToConstVisitor.cpp

Go to the documentation of this file.
00001 
00015 #include <OpenAnalysis/ExprTree/EvalToConstVisitor.hpp>
00016 
00017 static bool debug = false;
00018 
00019 namespace OA {
00020 
00021 
00022 EvalToConstVisitor::EvalToConstVisitor(OA_ptr<EvalToConstVisitorIRInterface> ir,
00023                                        OA_ptr<ReachConsts::Interface> rc)
00024     : mIR(ir), mReachConsts(rc)
00025 {
00026     mEvalResult = NULL;
00027 }
00028 
00031 void EvalToConstVisitor::visitNode(ExprTree::Node&)
00032 {
00033     mEvalResult = NULL;
00034 }
00035 
00036 void EvalToConstVisitor::visitOpNode(ExprTree::OpNode& n)
00037 {
00038   OpHandle opH = n.getHandle();
00039   OA_ptr<ConstValBasicInterface> ch1; ch1 = NULL;
00040   OA_ptr<ConstValBasicInterface> ch2; ch2 = NULL;
00041 
00042   if (debug){
00043       std::cout << "In EvalToConstVisitor::visitOpNode---> OP="
00044                 << mIR->returnOpEnumValInt(opH) 
00045                 << " (" << n.num_children() << " children), ";
00046   }
00047 
00048   // check that there are at most 2 children
00049   if ( n.num_children() <= 2 ) {
00050 
00051     // if so then visit each child
00052     OA_ptr<ExprTree::Node> cetNodePtr;
00053     ExprTree::ChildNodesIterator cNodesIter(n);
00054 
00055     // child1
00056     if (cNodesIter.isValid()) {
00057       cetNodePtr = cNodesIter.current();
00058 
00059       cetNodePtr->acceptVisitor(*this);
00060       ch1 = getConstVal();
00061     }
00062     ++cNodesIter;
00063 
00064     // child2
00065     if (cNodesIter.isValid()) {
00066       cetNodePtr = cNodesIter.current();
00067 
00068       cetNodePtr->acceptVisitor(*this);
00069       ch2 = getConstVal();
00070     }
00071   
00072     if (debug){
00073       std::cout << " with children { (";
00074       if (ch1.ptrEqual(NULL)) {
00075         std::cout << "NULL";
00076       } else {
00077         std::cout << ch1->toString();
00078       }
00079       std::cout << ") (";
00080       if (ch2.ptrEqual(NULL)) {
00081         std::cout << "NULL";
00082       } else {
00083         std::cout << ch2->toString();
00084       }
00085       std::cout << ")" << std::endl;
00086       std::cout.flush();
00087     }
00088 
00089     mEvalResult = mIR->evalOp(opH,ch1,ch2);
00090 
00091   // there are more than 2 children
00092   } else {
00093     mEvalResult = NULL;
00094   }
00095 
00096   if (debug) {
00097       if (mEvalResult.ptrEqual(NULL)) {
00098         std::cout << " } evalResult (NULL)" << std::endl;
00099       } else {
00100         std::cout << " } evalResult (" << mEvalResult->toString() 
00101                   << ")" << std::endl;
00102       }
00103   }
00104 
00105 }
00106 
00107 void EvalToConstVisitor::visitCallNode(ExprTree::CallNode& n)
00108 {
00109   mEvalResult = NULL;
00110 }
00111 
00112 void EvalToConstVisitor::visitMemRefNode(ExprTree::MemRefNode& n)
00113 {
00114   MemRefHandle memref = n.getHandle();
00115 
00116   //ConstValBasicInterface *cvbiPtr = cpPtr->getReachConst(memref);
00117   //std::cout << "In MemRefNode::eval---> got memref (" << mIR->toString(memref)
00118   //          << ") with ReachConst (" << cvbiPtr->toString() << ")"
00119   //          << std::endl;
00120   
00121   if (mReachConsts.ptrEqual(NULL)) {
00122     mEvalResult = NULL;
00123   } else {
00124     // if this memref has its address taken, then this should result 
00125     // in no constant value.  need to check this ... BK
00126     mEvalResult = mReachConsts->getReachConst(memref);
00127   }
00128 
00129 }
00130 
00131 void EvalToConstVisitor::visitConstSymNode(ExprTree::ConstSymNode& n)
00132 {
00133   mEvalResult = mIR->getConstValBasic(n.getHandle());
00134 }
00135 
00136 void EvalToConstVisitor::visitConstValNode(ExprTree::ConstValNode& n)
00137 {
00138   mEvalResult = mIR->getConstValBasic(n.getHandle());
00139 }
00140 
00141 
00142 } // end of OA namespace
00143