Example #1
0
 virtual void visit(SgNode* node) {
     if (isSgBasicBlock(node)) {
         SgBasicBlock* c = isSgBasicBlock(node);
         SgStatementPtrList newStatements;
         for (SgStatementPtrList::const_iterator i = c->get_statements().begin();
                 i != c->get_statements().end(); ++i) {
             if (isSgBasicBlock(*i)) {
                 SgBasicBlock* c2 = isSgBasicBlock(*i);
                 const SgStatementPtrList& c2Stmts = c2->get_statements();
                 // We need to prevent a declaration from immediately following a label, as that is illegal
                 if (!newStatements.empty() && isSgLabelStatement(newStatements.back()) && !c2Stmts.empty() && isSgVariableDeclaration(c2Stmts.front())) {
                     newStatements.push_back(SageBuilder::buildExprStatement(SageBuilder::buildNullExpression()));
                 }
                 newStatements.insert(newStatements.end(), isSgBasicBlock(*i)->get_statements().begin(), isSgBasicBlock(*i)->get_statements().end());
             } else {
                 if (!newStatements.empty() && isSgLabelStatement(newStatements.back()) && isSgVariableDeclaration(*i)) {
                     newStatements.push_back(SageBuilder::buildExprStatement(SageBuilder::buildNullExpression()));
                 }
                 newStatements.push_back(*i);
             }
         }
         if (!newStatements.empty() && isSgLabelStatement(newStatements.back())) {
             // Prevent block from ending with a label
             newStatements.push_back(SageBuilder::buildExprStatement(SageBuilder::buildNullExpression()));
         }
         for (SgStatementPtrList::const_iterator i = newStatements.begin();
                 i != newStatements.end(); ++i) {
             (*i)->set_parent(c);
         }
         c->get_statements() = newStatements;
         c->get_symbol_table()->get_table()->clear();
         SageInterface::rebuildSymbolTable(c);
     }
 }
Example #2
0
void
RtedTransformation::insertVariableCreateInitForParams( SgFunctionDefinition& fndef)
{
    SgBasicBlock* body = fndef.get_body();
    ROSE_ASSERT( body);

    SgInitializedNamePtrList names = fndef.get_declaration()->get_parameterList()->get_args();

    BOOST_FOREACH( SgInitializedName* param, names)
    {
      SgType* initType = param->get_type();

      // nov2010 code:
      // reference variables don't allocate new memory
      // if we call createVariable the RTS will think it's a double
      // allocation fault

      // \pp we skip array types because they will be handled elsewhere
      // \todo not sure if this is correct here, b/c arrays would decay
      //       to pointers anyway. However, this decay is not represented
      //       in the nov2010 code, thus we skip these initializations
      //       here.
      if ( isSgReferenceType(initType) || isSgArrayType(skip_ModifierType(initType)) )
        continue;

      SgFunctionDeclaration* fndecl = fndef.get_declaration();
      std::cerr << ">>> " << fndecl->get_name() << std::endl;
      ROSE_ASSERT(isSgFunctionDefinition(param->get_scope()));

      body->prepend_statement( buildVariableCreateCallStmt(param, true) );
    }
Example #3
0
 virtual void visit(SgNode* n) {
     if (isSgBasicBlock(n)) {
         SgBasicBlock* bb = isSgBasicBlock(n);
         SgStatementPtrList& stmts = bb->get_statements();
         bool changes = true;
         while (changes) {
             changes = false;
             for (SgStatementPtrList::iterator i = stmts.begin();
                     i != stmts.end(); ++i) {
                 if (isSgExprStatement(*i)) {
                     SgExpression* expr = isSgExprStatement(*i)->get_expression();
                     if (isSgIntVal(expr) ||
                             isSgNullExpression(expr) ||
                             isSgVarRefExp(expr) ||
                             (isSgNotOp(expr) && isSgVarRefExp(isSgNotOp(expr)->get_operand()))) { // This is what null statements are
                         SgStatementPtrList::iterator inext = i, iprev = i;
                         ++inext;
                         if (iprev != stmts.begin()) --iprev;
                         if ((inext != stmts.end() && !isSgDeclarationStatement(*inext)) || // A label cannot precede a declaration
                                 !isSgLabelStatement(*iprev)) {
                             // Checking to be sure that this statement isn't ensuring
                             // that a label isn't the last statement in a block (which
                             // would be illegal)
                             SageInterface::myRemoveStatement(*i);
                             changes = true;
                             break; // To avoid iterator invalidation
                         }
                     }
                 }
             }
         }
     }
 }
Example #4
0
     virtual void visit(SgNode* n)
        {
          SgReturnStmt* rs = isSgReturnStmt(n);
          if (rs)
             {
               // std::cout << "Converting return statement " << rs->unparseToString();
               // std::cout << " into possible assignment to " << where_to_write_answer->unparseToString();
               // std::cout << " and jump to " << label->get_name().getString() << std::endl;
               SgExpression* return_expr = rs->get_expression();
               SgBasicBlock* block = SageBuilder::buildBasicBlock();
            // printf ("Building IR node #1: new SgBasicBlock = %p \n",block);
               if (return_expr)
                  {
                    SgExpression* assignment = generateAssignmentMaybe(where_to_write_answer,return_expr);
                    if (where_to_write_answer)
                      where_to_write_answer->set_parent(assignment);
                    if (return_expr != assignment)
                      return_expr->set_parent(assignment);
                    SgStatement* assign_stmt = SageBuilder::buildExprStatement(assignment);
                    SageInterface::appendStatement(assign_stmt, block);
                  }

            // block->get_statements().push_back(new SgGotoStatement(SgNULL_FILE, label));
               SgGotoStatement* gotoStatement = new SgGotoStatement(SgNULL_FILE, label);
               gotoStatement->set_endOfConstruct(SgNULL_FILE);
               ROSE_ASSERT(n->get_parent() != NULL);
               SageInterface::appendStatement(gotoStatement, block);
               isSgStatement(n->get_parent())->replace_statement(rs, block);
               block->set_parent(n->get_parent());
               ROSE_ASSERT(gotoStatement->get_parent() != NULL);
             }
        }
Example #5
0
void 
MyTraversal::visit ( SgNode* astNode )
   {
     SgBasicBlock* bb = isSgBasicBlock(astNode);
     SgGlobal *sg=isSgGlobal(astNode);
           
     string symbol_id="foo";
     string incstr="#include \"newadinc.h\"";
           
     if (bb!=NULL)
        {
       // insert declaration double foo;
       // on top of this block
          Sg_File_Info * finfo=new Sg_File_Info(bb->getFileName(), 1,1);
          SgName sgnm(symbol_id.c_str());
          SgType * ptype = new SgTypeDouble(); 
          SgVariableDeclaration *sgdecl=new SgVariableDeclaration(finfo,sgnm,
ptype);
          bb->prepend_statement(sgdecl);
       // MiddleLevelRewrite::insert(bb, sgdecl->unparseToString(),MidLevelCollectionTypedefs::TopOfCurrentScope);
        } 
       else 
        {
          if(sg!=NULL)
             {
               MiddleLevelRewrite::insert(sg,incstr, MidLevelCollectionTypedefs::TopOfCurrentScope);
             }
        }
   }
int main( int argc, char * argv[] ) 
{
        // Build the AST used by ROSE
        SgProject* project = frontend(argc,argv);
        
        generatePDF ( *project );
        
        Rose_STL_Container<SgNode*> functions = NodeQuery::querySubTree(project, V_SgFunctionDefinition);
        for (Rose_STL_Container<SgNode*>::const_iterator i = functions.begin(); i != functions.end(); ++i) {
                SgFunctionDefinition* curFunc = isSgFunctionDefinition(*i);
                ROSE_ASSERT(curFunc);
                                
                SgBasicBlock *funcBody = curFunc->get_body();
                InterestingNode funcCFGStart = (InterestingNode)funcBody->cfgForBeginning();;
                        
                // output the CFG to a file
                ofstream fileCFG;
                fileCFG.open((curFunc->get_declaration()->get_name().getString()+"_cfg.dot").c_str());
                cout << "writing to file "<<(curFunc->get_declaration()->get_name().getString()+"_cfg.dot")<<"\n";
                VirtualCFG::cfgToDot(fileCFG, curFunc->get_declaration()->get_name(), funcCFGStart);
                fileCFG.close();
        }
        
        // Unparse and compile the project (so this can be used for testing)
        return backend(project);
}
Example #7
0
void
CompassAnalyses::DefaultCase::Traversal::
visit(SgNode* node)
   {
     
        SgSwitchStatement* theSwitch = isSgSwitchStatement(node);
        if (!theSwitch) return;
        bool has_default = false;
        if (isSgBasicBlock(theSwitch->get_body())) {
          SgBasicBlock* BBlock = isSgBasicBlock(theSwitch->get_body());
          //I should maybe do more sanity checking for nulls here
          SgStatementPtrList BBlockStmts = BBlock->get_statements();
          for (Rose_STL_Container<SgStatement*>::iterator j = BBlockStmts.begin(); j != BBlockStmts.end(); j++)
            {
              if (isSgDefaultOptionStmt(*j)){
                has_default = true;
                break;
              }
            }
        } else {
          if (isSgDefaultOptionStmt(theSwitch->get_body())) {
            has_default = true;
          }
        }
        if (!has_default){
          output->addOutput(new CheckerOutput(node));
        }
  // Implement your traversal here.  

   } //End of the visit function.
Example #8
0
    virtual void visit(SgNode* n) {
        if (isSgBasicBlock(n)) {
            SgBasicBlock* bb = isSgBasicBlock(n);
            SgStatementPtrList& stmts = bb->get_statements();
            size_t initi;
            for (size_t decli = 0; decli < stmts.size(); ++decli) {
                if (isSgVariableDeclaration(stmts[decli])) {
                    SgVariableDeclaration* decl = isSgVariableDeclaration(stmts[decli]);
                    SgInitializedNamePtrList& vars = decl->get_variables();
                    for (size_t vari = 0; vari != vars.size(); ++vari) {
                        SgInitializedName* in = vars[vari];
                        if (in->get_initializer() == 0) {
                            bool used = false;
                            for (initi = decli + 1; initi < stmts.size();
                                    used |= containsVariableReference(stmts[initi], in),
                                    ++initi) {
                                SgExprStatement* initExprStmt = isSgExprStatement(stmts[initi]);
                                if (initExprStmt) {
                                    SgExpression* top = initExprStmt->get_expression();
                                    if (isSgAssignOp(top)) {
                                        SgVarRefExp* vr = isSgVarRefExp(isSgAssignOp(top)->get_lhs_operand());
                                        ROSE_ASSERT(isSgAssignOp(top) != NULL);
                                        SgExpression* newinit = isSgAssignOp(top)->get_rhs_operand();
                                        if (!used && vr && vr->get_symbol()->get_declaration() == in) {
                                            ROSE_ASSERT(newinit != NULL);
                                            // printf ("MoveDeclarationsToFirstUseVisitor::visit(): newinit = %p = %s \n",newinit,newinit->class_name().c_str());
                                            ROSE_ASSERT(newinit->get_type() != NULL);
                                            SgAssignInitializer* i = new SgAssignInitializer(SgNULL_FILE,newinit,newinit->get_type());
                                            i->set_endOfConstruct(SgNULL_FILE);
                                            // printf ("Built a SgAssignInitializer #1 \n");
                                            vars[vari]->set_initializer(i);
                                            stmts[initi] = decl;
                                            newinit->set_parent(i);

                                            // DQ (6/23/2006): Set the parent and file_info pointers
                                            // printf ("Setting parent of i = %p = %s to parent = %p = %s \n",i,i->class_name().c_str(),in,in->class_name().c_str());
                                            i->set_parent(in);
                                            ROSE_ASSERT(i->get_parent() != NULL);

                                            i->set_file_info(new Sg_File_Info(*(newinit->get_file_info())));
                                            ROSE_ASSERT(i->get_file_info() != NULL);

                                            // Assumes only one var per declaration FIXME
                                            ROSE_ASSERT (vars.size() == 1);
                                            stmts.erase(stmts.begin() + decli);
                                            --decli; // To counteract ++decli in loop header
                                            break; // To get out of initi loop
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
Example #9
0
void Jovial_to_C::translateProgramHeaderStatement(SgProgramHeaderStatement* programHeaderStatement)
{
// Get scopeStatement from SgProgramHeaderStatement
   SgScopeStatement* scopeStatement = programHeaderStatement->get_scope();
   ROSE_ASSERT(scopeStatement);
  
// Get ParameterList and DecoratorList
   SgFunctionParameterList* functionParameterList = buildFunctionParameterList(); 
   SgExprListExp* decoratorList = deepCopy(programHeaderStatement->get_decoratorList());
  
// Reuse FunctionDefinition from Fortran programHeaderStatement
   SgFunctionDefinition* functionDefinition = programHeaderStatement->get_definition();
  
// Get basicBlock from SgProgramHeaderStatement
   SgBasicBlock* basicBlock = functionDefinition->get_body();
   ROSE_ASSERT(basicBlock);

   SgSymbolTable* symbolTable = basicBlock->get_symbol_table();
   ROSE_ASSERT(symbolTable);
  
// The main function return type is int
   SgType* mainType = SgTypeInt::createType();
  
// Remove original function symbol.  Keep the new function symbol with name of "main"
   SgFunctionSymbol* functionSymbol = isSgFunctionSymbol(scopeStatement->lookup_symbol(programHeaderStatement->get_name()));
   SgSymbolTable* globalSymbolTable = isSgSymbolTable(functionSymbol->get_parent());
   globalSymbolTable->remove(functionSymbol);
   functionSymbol->set_parent(NULL);
   delete(functionSymbol);
  
// Create SgFunctionDeclaration for C main function. Name must be "main".
   SgFunctionDeclaration* cFunctionDeclaration = buildDefiningFunctionDeclaration("main",
                                                                                  mainType,
                                                                                  functionParameterList,
                                                                                  scopeStatement);
  
// Setup the C function declaration.
   removeList.push_back(cFunctionDeclaration->get_definition());
   functionDefinition->set_parent(cFunctionDeclaration);
   cFunctionDeclaration->set_definition(functionDefinition);
   programHeaderStatement->set_definition(NULL);
 
// Replace the SgProgramHeaderStatement with SgFunctionDeclaration.
   replaceStatement(programHeaderStatement,cFunctionDeclaration,true);
   cFunctionDeclaration->set_decoratorList(decoratorList);
// cFunctionDeclaration->set_startOfConstruct(functionDefinition->get_startOfConstruct());
// cFunctionDeclaration->set_endOfConstruct(functionDefinition->get_endOfConstruct());
// cFunctionDeclaration->get_file_info()->set_physical_filename(cFunctionDeclaration->get_file_info()->get_filenameString()); 
  
   programHeaderStatement->set_parent(NULL);
}  // End of Jovial_to_C::translateProgramHeaderStatement
Example #10
0
    virtual void visit(SgNode* n) {
        if (isSgBasicBlock(n)) {
            SgBasicBlock* bb = isSgBasicBlock(n);
            bool changes1 = true;
            while (changes1) {
                changes1 = false;
                for (size_t i = 0; i < bb->get_statements().size(); ++i) {
                    SgStatement* stmt = bb->get_statements()[i];
                    if (isSgVariableDeclaration(stmt)) {
                        SgInitializedNamePtrList& vars =
                            isSgVariableDeclaration(stmt)->get_variables();
                        bool changes = true;
                        while (changes) {
                            changes = false;
                            SgInitializedNamePtrList::iterator j;
                            for (j = vars.begin(); j != vars.end(); ++j)
                                if (used_decls.find(*j) == used_decls.end()) {
                                    SgInitializer* init = (*j)->get_initializer();
                                    bool shouldErase = false;
                                    if (!init)
                                        shouldErase = true;
                                    else if (isSgAssignInitializer(init)) {
                                        SgAssignInitializer* init2 = isSgAssignInitializer(init);
                                        // Ensure that init does not have side effects
                                        shouldErase = isSimpleInitializer(init2->get_operand());
                                    }
                                    if (shouldErase) {
                                        removeVariableDeclaration(*j);
                                        --i; // Counteract increment
                                        goto iLoopBottom;
                                        // changes = true;
                                        // break;
                                    }
                                }
                        }
#if 0
                        if (vars.empty()) {
                            bb->get_statements().erase(i);
                            changes1 = true;
                            break;
                        }
#endif
                    }
iLoopBottom:
                    ;
                }
            }
        }
    }
Example #11
0
void PreAndPostOrderTraversal::preOrderVisit(SgNode* n) {

        SgFunctionDeclaration * dec = isSgFunctionDeclaration(n);
        if (dec != NULL) {
                cout << "Found function declaration " << dec->get_name().getString();
                Sg_File_Info * start = dec->get_startOfConstruct();
                Sg_File_Info * end = dec->get_endOfConstruct();
                if(start->isCompilerGenerated()) {
                        cout << ", which is compiler-generated" << endl;
                } else {
                        cout << " in file " << start->get_raw_filename() << ", " << start->get_file_id() << " from line " << 
                            start->get_line() << ", col " << start->get_col() << " to line " << 
                            end->get_line() << ", col " << end->get_col() << endl;
                }
                SgFunctionType * type = dec->get_type();
                SgType * retType = type->get_return_type();
        cout << "Return type: " << retType->unparseToString() << endl;
        SgFunctionParameterList * params = dec->get_parameterList();
        SgInitializedNamePtrList & ptrList = params->get_args();
        if(!ptrList.empty()) {
            cout << "Parameter types: ";
            for(SgInitializedNamePtrList::iterator j = ptrList.begin(); j != ptrList.end(); j++) {
                SgType * pType = (*j)->get_type();
                cout << pType->unparseToString() << " ";
            }
            cout << endl;
        }
        cout << "Linkage: " << dec->get_linkage() << endl;
               cout << endl;
        }


        SgFunctionDefinition * def = isSgFunctionDefinition(n);
        if (def != NULL) {
                cout << "Found function definition " << def->get_declaration()->get_name().getString();
                Sg_File_Info * start = def->get_startOfConstruct();
                Sg_File_Info * end = def->get_endOfConstruct();
                if(start->isCompilerGenerated()) {
                        cout << ", which is compiler-generated" << endl;
                } else {
                        cout << " in file " << start->get_raw_filename() << " from line " << start->get_line() << ", col " << start->get_col() << " to line " << end->get_line() << ", col " << end->get_col() << endl;
                SgBasicBlock * body = def->get_body();
        Sg_File_Info * bodyStart = body->get_startOfConstruct();
        Sg_File_Info * bodyEnd =   body->get_endOfConstruct();
        cout << "Function body from line " << bodyStart->get_line() << ", col " << bodyStart->get_col() << " to line " << bodyEnd->get_line() << ", col " << bodyEnd->get_col() << endl; 
 }
        cout << endl;
        }
}
void
FindVariableDeclarations::visit ( SgNode* astNode )
   {
     SgBasicBlock* block = isSgBasicBlock(astNode);
     if (block != NULL)
        {
          SgStatementPtrList & listOfStatements = block->get_statements();
          for (size_t i = 0; i < listOfStatements.size(); i++)
             {
               SgVariableDeclaration* variableDeclaration = isSgVariableDeclaration(listOfStatements[i]);
               if (variableDeclaration != NULL)
                  {
                    printf ("Found a variable decaration in a SgBasicBlock at: \n");
                    variableDeclaration->get_file_info()->display("Found a variable decaration in a SgBasicBlock");
                  }
             }
        }
   }
void
SimpleInstrumentation::visit ( SgNode* astNode )
   {
     SgBasicBlock* block = isSgBasicBlock(astNode);
     if (block != NULL)
        {
       // Mark this as a transformation (required)
          Sg_File_Info* sourceLocation = Sg_File_Info::generateDefaultFileInfoForTransformationNode();
          ROSE_ASSERT(sourceLocation != NULL);

          SgType* type = new SgTypeInt();
          ROSE_ASSERT(type != NULL);

          SgName name = "newVariable";

          SgVariableDeclaration* variableDeclaration = new SgVariableDeclaration(sourceLocation,name,type);
          ROSE_ASSERT(variableDeclaration != NULL);

          SgInitializedName* initializedName = *(variableDeclaration->get_variables().begin());
          initializedName->set_file_info(Sg_File_Info::generateDefaultFileInfoForTransformationNode());

       // DQ (6/18/2007): The unparser requires that the scope be set (for name qualification to work).
          initializedName->set_scope(block);

       // Liao (2/13/2008): AstTests requires this to be set
          variableDeclaration->set_firstNondefiningDeclaration(variableDeclaration);

          ROSE_ASSERT(block->get_statements().size() > 0);

          block->get_statements().insert(block->get_statements().begin(),variableDeclaration);
          variableDeclaration->set_parent(block);

       // Add a symbol to the sybol table for the new variable
          SgVariableSymbol* variableSymbol = new SgVariableSymbol(initializedName);
          block->insert_symbol(name,variableSymbol);
        }
   }
void
PRE::addEdgeInsertionPoints(PRE::ControlFlowGraph& controlflow)
{
    // printf ("In addEdgeInsertionPoints: controlflow.graph.vertices().size()     = %zu \n",controlflow.graph.vertices().size());
    // printf ("In addEdgeInsertionPoints: controlflow.edge_insertion_point.size() = %zu \n",controlflow.edge_insertion_point.size());

    VertexIter i = controlflow.graph.vertices().begin(), iend = controlflow.graph.vertices().end();
    for (; i != iend; ++i)
    {
        // printf ("vertexCount = %d \n",vertexCount++);

        OutEdgeIter out    = controlflow.graph.out_edges(*i).begin(),
                    outend = controlflow.graph.out_edges(*i).end();

        // printf ("Problem location: controlflow.node_statements[*i] = %p \n",&(controlflow.node_statements[*i]));

        const vector<SgNode*>& stmts = controlflow.node_statements[*i];
        SgNode* last_stmt = stmts.empty() ? 0 : stmts[stmts.size() - 1];
        int out_edge_count = distance(out, outend);
        SgStatement* expr_parent = 0;

        // printf ("stmts.size() = %zu last_stmt = %p expr_parent = %p \n",stmts.size(),last_stmt,expr_parent);

        // Wouldn't this make more sense as a while loop?
        for (vector<SgNode*>::const_iterator j = stmts.begin(); j != stmts.end(); ++j)
        {
            ROSE_ASSERT((*j) != NULL);
#if 0
            if ((*j)->get_parent() == NULL)
            {
                (*j)->get_file_info()->display("Location of error: debug");
                printf ("Error: statement = %s \n",(*j)->unparseToString().c_str());
            }
            else
            {
                ROSE_ASSERT((*j)->get_parent() != NULL);
                // printf ("Statement at vertex (*i): (*j)->class_name() = %s (*j)->get_parent()->class_name()\n",(*j)->class_name().c_str(),(*j)->get_parent()->class_name().c_str());
            }
#endif
            if (expr_parent) continue;
            if (isSgExpression(*j))
            {
                expr_parent = isSgStatement((*j)->get_parent());
                ROSE_ASSERT (expr_parent);
            }
            else
            {
                if (isSgExprStatement(*j))
                {
                    expr_parent = isSgStatement((*j)->get_parent());
                    if (isSgBasicBlock(expr_parent) || isSgForInitStatement(expr_parent))
                        expr_parent = 0;
                }
            }
        }

        // printf ("out_edge_count = %d \n",out_edge_count);
        // printf ("expr_parent = %p = %s \n",expr_parent,(expr_parent != NULL) ? expr_parent->class_name().c_str() : "NULL");

        if (!expr_parent)
        {
            // printf ("Fixup the expr_parent to be the last_stmt = %p \n",last_stmt);
            expr_parent = isSgStatement(last_stmt);
        }
#if 0
        // DQ (3/17/2006): It is a problem if at this point the expr_parent is NULL
        if (expr_parent == NULL)
        {
            printf ("Warning expr_parent == NULL \n");
        }
#endif
        if (out_edge_count == 1 && !isSgForStatement(expr_parent))
        {
#if 0
            // printf ("Edge count == 1: and not a SgForStatement, adding and edge using %p \n",expr_parent);
            if (expr_parent == NULL)
            {
                printf ("Warning adding an edge using a NULL pointer! at %p \n",&(controlflow.edge_insertion_point[*out]));
            }
#endif
            controlflow.edge_insertion_point[*out] = make_pair(expr_parent,
                    (isSgGotoStatement(expr_parent) ?
                     /* Put insertion before goto statements but after other statement types */ true :
                     false));
        }
        else
        {
            if (out_edge_count == 1 && isSgForStatement(expr_parent))
            {
                // Increment of for statement
                controlflow.edge_insertion_point[*out] = make_pair(isSgForStatement(expr_parent)->get_increment(),false); // Put in after expression using operator,
            }
            else
            {
                if (out_edge_count >= 2)
                {
                    for (; out != outend; ++out)
                    {
                        pair<SgStatement*, bool> insert_point;
                        CFGConfig::EdgeType kind = controlflow.edge_type[*out];
                        // printf ("CFGConfig::EdgeType kind = %d \n",(int)kind);
try_next_ancestor:
                        ROSE_ASSERT(expr_parent != NULL);
                        switch (expr_parent->variantT())
                        {
                        case V_SgDoWhileStmt:
                            if (kind == CFGConfig::COND_FALSE)
                            {
                                insert_point = make_pair(expr_parent, false);
                            }
                            else
                            {
                                // DQ (3/13/2006): Make this an error (partly as just a test)
                                // FIXME -- We can't really use
                                // do-while in tests anymore, since
                                // there isn't a good way to put
                                // something on the true branch of
                                // the test
                                printf ("kind != CFGConfig::COND_FALSE in SgDoWhileStmt \n");
                                ROSE_ASSERT(false);
                            }
                            break;

                        case V_SgIfStmt:
                            if (kind == CFGConfig::COND_TRUE)
                            {
                                insert_point = make_pair(isSgIfStmt(expr_parent)->get_true_body(), true);
                            }
                            else
                            {
                                if (isSgIfStmt(expr_parent)->get_false_body() == NULL) {
                                    SgBasicBlock* bb = SageBuilder::buildBasicBlock();
                                    isSgIfStmt(expr_parent)->set_false_body(bb);
                                    bb->set_parent(expr_parent);
                                }
                                ROSE_ASSERT (isSgIfStmt(expr_parent)->get_false_body());
                                insert_point = make_pair(isSgIfStmt(expr_parent)->get_false_body(), true);
                            }
                            break;

                        case V_SgWhileStmt:
                            if (kind == CFGConfig::COND_TRUE)
                            {
                                insert_point = make_pair(isSgWhileStmt(expr_parent)->get_body(), true);
                            }
                            else
                            {
                                insert_point = make_pair(expr_parent, false);
                            }
                            break;

                        case V_SgSwitchStatement:
                            assert (!"switch FIXME");
                            break;

                        case V_SgForStatement:
                            // printf ("Found a SgForStatement, but this should not be used now the the conditional test is a SgExprStatement \n");
                            // ROSE_ASSERT(false);
                            if (kind == CFGConfig::COND_TRUE)
                            {
                                insert_point = make_pair(isSgForStatement(expr_parent)->get_loop_body(), true);
                            }
                            else
                            {
                                // DQ (3/17/2006): this effects the placement of the cachevar__10 = (_var_0 + 15); in pass3.C
                                ROSE_ASSERT(expr_parent != NULL);
                                insert_point = make_pair(expr_parent, false);
                                // insert_point = make_pair(expr_parent, true);
                            }
                            break;
#if 0
                        // DQ (3/13/2006): Added case for SgExpressionStatment, since it is now the conditonal
                        // test in a SgForStatement (due to a recent change in the IR to support such things
                        // which are leagal code in C and C++).
                        case V_SgExprStatement:
                            printf ("Found a SgExprStatement, likely the test in a SgForStatment or such \n");
                            ROSE_ASSERT (false);
                            ROSE_ASSERT(isSgForStatement(expr_parent->get_parent()) != NULL);
                            ROSE_ASSERT(isSgForStatement(expr_parent->get_parent())->get_test() == expr_parent);
                            if (kind == CFGConfig::COND_TRUE)
                            {
                                printf ("Building an edge to the for loop body \n");
                                insert_point = make_pair(isSgForStatement(expr_parent->get_parent())->get_loop_body(), true);
                            }
                            else
                            {
                                insert_point = make_pair(expr_parent, false);
                            }
                            break;
#endif
                        case V_SgExprStatement:
                        case V_SgGotoStatement:
                        case V_SgLabelStatement: {
                            expr_parent = isSgStatement(expr_parent->get_parent());
                            goto try_next_ancestor;
                        }
                        default:
                            cerr << "Unknown variant " << expr_parent->sage_class_name() << endl;
                            expr_parent->get_file_info()->display("Location in input code");
                            ROSE_ASSERT (false);
                            break;
                        }

                        if (insert_point.first)
                            controlflow.edge_insertion_point[*out] = insert_point;
                    }
                }
                else
                {
                    // out_edge_count == 0
                }
            }
        }
    }

    EdgeIter ei   = controlflow.graph.edges().begin(),
             eend = controlflow.graph.edges().end();
    for (; ei != eend; ++ei)
    {
        if (!controlflow.edge_insertion_point[*ei].first)
        {
            Vertex src = controlflow.graph.source(*ei);
            if (controlflow.graph.out_edges(src).size() == 1 && !controlflow.node_statements[src].empty())
            {
                vector<SgNode*> stmts = controlflow.node_statements[src];
                controlflow.edge_insertion_point[*ei] =
                    make_pair(stmts[stmts.size() - 1], false);
            }
        }
    }

    // printf ("Leaving addEdgeInsertionPoints: controlflow.graph.vertices().size()     = %zu \n",controlflow.graph.vertices().size());
    // printf ("Leaving addEdgeInsertionPoints: controlflow.edge_insertion_point.size() = %zu \n",controlflow.edge_insertion_point.size());
}
Example #15
0
ExprSynAttr *examineScopeStatement(SgScopeStatement* scope, string name, ostream &out) {
    SgSymbolTable* symbol_table = scope->get_symbol_table();
    set<SgNode*> symbol_nodes = symbol_table->get_symbols();
    set<SgNode*>::const_iterator symbol_iter;

    out << "{" << endl;
    /*
    for (symbol_iter = symbol_nodes.begin(); 
        symbol_iter != symbol_nodes.end(); 
        ++symbol_iter) {
        SgSymbol* symbol = isSgSymbol(*symbol_iter);
        if (isSgVariableSymbol(symbol)) {
            SgVariableSymbol *varsym = isSgVariableSymbol(symbol);
            SgInitializedName *initname = varsym->get_declaration();
            SgDeclarationStatement *decl = initname->get_declaration();
            if (isSgVariableDeclaration(decl)) {
                SgVariableDeclaration *vardecl = isSgVariableDeclaration(decl);
                examineVariableDeclaration(vardecl, out);
            }
        }
    }
    */

    SgBasicBlock *body;
    ExprSynAttr *expr_attr;
    ExprSynAttr *ret;
    stringstream fake;
    stringstream codestream;

    ret = new ExprSynAttr();

    if (scope->variantT() != V_SgBasicBlock) {
        out << "}" << endl;
        ret->code << "{" << endl;
        ret->code << "}" << endl;
        return ret;
    } else
        body = isSgBasicBlock(scope);

    SgStatementPtrList& stmt_list = body->get_statements();
    SgStatementPtrList::const_iterator stmt_iter;

    for (stmt_iter = stmt_list.begin();
            stmt_iter != stmt_list.end();
            stmt_iter++) {
        SgStatement *stmt = *stmt_iter;
        expr_attr = examineStatement(stmt, out);
        ret->union_tmp_decls(expr_attr);
        ret->code << expr_attr->code.str() << endl;
        delete expr_attr;
        out << endl;
    }

    expr_attr = new ExprSynAttr();
    expr_attr->code << "{" << endl;
    ret->output_tmp_decls(expr_attr->code);
    expr_attr->code << ret->code.str();
    expr_attr->code << "}" << endl;

    delete ret;

    out << "}" << endl;
    return expr_attr;
  /*
  int num_vars = 0;
  for (symbol_iter = symbol_nodes.begin(); 
       symbol_iter != symbol_nodes.end(); 
       ++symbol_iter) {
    SgSymbol* symbol = isSgSymbol(*symbol_iter);
    cout << "[Scope " << name << "] Symbol: "<<symbol->get_name().getString()<<endl;
    if (isSgVariableSymbol(symbol)) num_vars++;
  }
  cout << "[Scope " << name << "] Num symbols: " << symbol_nodes.size() << endl;      
  cout << "[Scope " << name << "] Num variable symbols: " << num_vars << endl;      
  */
}
Example #16
0
File: options.C Project: 8l/rose
void
TransformationSupport::getTransformationOptions ( SgNode* astNode, list<OptionDeclaration> & generatedList, string identifingTypeName )
   {
  // This function searches for variables of type ScopeBasedTransformationOptimization.  Variables
  // of type ScopeBasedTransformationOptimization are used to communicate optimizations from the
  // application to the preprocessor. If called from a project or file object it traverses down to
  // the global scope of the file and searches only the global scope, if called from and other
  // location within the AST it searches the current scope and then traverses the parent nodes to
  // find all enclosing scopes until in reaches the global scope.  At each scope it searches for
  // variables of type ScopeBasedTransformationOptimization.

  // printf ("######################### START OF TRANSFORMATION OPTION QUERY ######################## \n");

     ROSE_ASSERT (astNode != NULL);
     ROSE_ASSERT (identifingTypeName.c_str() != NULL);

#if 0
     printf ("In getTransformationOptions(): astNode->sage_class_name() = %s generatedList.size() = %d \n",
          astNode->sage_class_name(),generatedList.size());
     SgLocatedNode* locatedNode = isSgLocatedNode(astNode);
     if (locatedNode != NULL)
        {
          printf ("          locatedNode->get_file_info()->get_filename() = %s \n",locatedNode->get_file_info()->get_filename());
          printf ("          locatedNode->get_file_info()->get_line() = %d \n",locatedNode->get_file_info()->get_line());
        }
#endif

     switch (astNode->variant())
        {
          case ProjectTag:
             {
               SgProject* project = isSgProject(astNode);
               ROSE_ASSERT (project != NULL);

           //! Loop through all the files in the project and call the mainTransform function for each file
               int i = 0;
               for (i=0; i < project->numberOfFiles(); i++)
                  {
                    SgFile* file = &(project->get_file(i));

                 // printf ("Calling Query::traverse(SgFile,QueryFunctionType,QueryAssemblyFunctionType) \n");
                    getTransformationOptions ( file, generatedList, identifingTypeName );
                  }
               break;
             }

          case SourceFileTag:
             {
               SgSourceFile* file = isSgSourceFile(astNode);
               ROSE_ASSERT (file != NULL);
               SgGlobal* globalScope = file->get_globalScope();
               ROSE_ASSERT (globalScope != NULL);
               ROSE_ASSERT (isSgGlobal(globalScope) != NULL);
               getTransformationOptions ( globalScope, generatedList, identifingTypeName );
               break;
             }

       // Global Scope
          case GLOBAL_STMT:
             {
               SgGlobal* globalScope = isSgGlobal(astNode);
               ROSE_ASSERT (globalScope != NULL);

               SgSymbolTable* symbolTable = globalScope->get_symbol_table();
               ROSE_ASSERT (symbolTable != NULL);
               getTransformationOptions ( symbolTable, generatedList, identifingTypeName );

            // printf ("Processed global scope, exiting .. \n");
            // ROSE_ABORT();
               break;
             }

          case SymbolTableTag:
             {
            // List the variable in each scope
            // printf ("List all the variables in this symbol table! \n");
               SgSymbolTable* symbolTable = isSgSymbolTable(astNode);
               ROSE_ASSERT (symbolTable != NULL);

               bool foundTransformationOptimizationSpecifier = false;

            // printf ("Now print out the information in the symbol table for this scope: \n");
            // symbolTable->print();

#if 0
            // I don't know when a SymbolTable is given a name!
               printf ("SymbolTable has a name = %s \n",
                    (symbolTable->get_no_name()) ? "NO: it has no name" : "YES: it does have a name");
               if (!symbolTable->get_no_name())
                    printf ("SymbolTable name = %s \n",symbolTable->get_name().str());
                 else
                    ROSE_ASSERT (symbolTable->get_name().str() == NULL);
#endif

               if (symbolTable->get_table() != NULL)
                  {
                    SgSymbolTable::hash_iterator i = symbolTable->get_table()->begin();
                    int counter = 0;
                    while (i != symbolTable->get_table()->end())
                       {
                         ROSE_ASSERT ( isSgSymbol( (*i).second ) != NULL );

                      // printf ("Initial info: number: %d pair.first (SgName) = %s pair.second (SgSymbol) sage_class_name() = %s \n",
                      //      counter,(*i).first.str(),(*i).second->sage_class_name());

                         SgSymbol* symbol = isSgSymbol((*i).second);
                         ROSE_ASSERT ( symbol != NULL );
                         SgType* type = symbol->get_type();
                         ROSE_ASSERT ( type != NULL );

                         SgNamedType* namedType = isSgNamedType(type);
                         string typeName;
                         if (namedType != NULL)
                            {
                              SgName n = namedType->get_name();
                              typeName = namedType->get_name().str();
                           // char* nameString = namedType->get_name().str();
                           // printf ("Type is: (named type) = %s \n",nameString);
                              ROSE_ASSERT (identifingTypeName.c_str() != NULL);
                           // ROSE_ASSERT (typeName != NULL);
                           // printf ("In getTransformationOptions(): typeName = %s identifingTypeName = %s \n",typeName.c_str(),identifingTypeName.c_str());
                           // if ( (typeName != NULL) && ( typeName == identifingTypeName) )
                              if ( typeName == identifingTypeName )
                                 {
                                // Now look at the parameter list to the constructor and save the
                                // values into the list.

                                // printf ("Now save the constructor arguments! \n");

                                   SgVariableSymbol* variableSymbol = isSgVariableSymbol(symbol);

                                   if ( variableSymbol != NULL )
                                      {
                                        SgInitializedName* initializedNameDeclaration = variableSymbol->get_declaration();
                                        ROSE_ASSERT (initializedNameDeclaration != NULL);

                                        SgDeclarationStatement* declarationStatement = initializedNameDeclaration->get_declaration();
                                        ROSE_ASSERT (declarationStatement != NULL);

                                        SgVariableDeclaration* variableDeclaration = isSgVariableDeclaration(declarationStatement);
                                        ROSE_ASSERT (variableDeclaration != NULL);

                                        getTransformationOptionsFromVariableDeclarationConstructorArguments(variableDeclaration,generatedList);

                                        foundTransformationOptimizationSpecifier = true;

                                     // printf ("Exiting after saving the constructor arguments! \n");
                                     // ROSE_ABORT();
                                      }
                                     else
                                      {
#if 0
                                        printf ("Not a SgVariableSymbol: symbol->sage_class_name() = %s \n",
                                             symbol->sage_class_name());
#endif
                                      }
                                 }
                                else
                                 {
#if 0
                                   printf ("typeName != identifingTypeName : symbol->sage_class_name() = %s \n",
                                        symbol->sage_class_name());
#endif
#if 0
                                // I don't think this should ever be NULL (but it is sometimes)
                                   if (typeName != NULL)
                                        printf ("typeName == NULL \n");
#endif
                                 }
                            }
                           else
                            {
                              typeName = (char *)type->sage_class_name();
                            }

                      // printf ("In while loop at the base: counter = %d \n",counter);
                         i++;
                         counter++;
                       }
                  }
                 else
                  {
                 // printf ("Pointer to symbol table is NULL \n");
                  }

            // printf ("foundTransformationOptimizationSpecifier = %s \n",foundTransformationOptimizationSpecifier ? "true" : "false");

            // SgSymbolTable objects don't have a parent node (specifically they lack a get_parent
            // member function in the interface)!
               break;
             }

          case BASIC_BLOCK_STMT:
             {
            // List the variable in each scope
            // printf ("List all the variables in this scope! \n");
               SgBasicBlock* basicBlock = isSgBasicBlock(astNode);
               ROSE_ASSERT (basicBlock != NULL);

               SgSymbolTable* symbolTable = basicBlock->get_symbol_table();
               ROSE_ASSERT (symbolTable != NULL);
               getTransformationOptions ( symbolTable, generatedList, identifingTypeName );

            // Next go (fall through this case) to the default case so that we traverse the parent
            // of the SgBasicBlock.
            // break;
             }

          default:
            // Most cases will be the default (this is by design)
            // printf ("default in switch found in globalQueryGetListOperandStringFunction() (sage_class_name = %s) \n",astNode->sage_class_name());

            // Need to recursively backtrack through the parents until we reach the SgGlobal (global scope)
               SgStatement* statement = isSgStatement(astNode);
               if (statement != NULL)
                  {
                    SgNode* parentNode = statement->get_parent();
                    ROSE_ASSERT (parentNode != NULL);
//                  printf ("parent = %p parentNode->sage_class_name() = %s \n",parentNode,parentNode->sage_class_name());
                    SgStatement* parentStatement = isSgStatement(parentNode);
                    if (parentStatement == NULL)
                       {
                         printf ("parentStatement == NULL: statement (%p) is a %s \n",statement,statement->sage_class_name());
                         printf ("parentStatement == NULL: statement->get_file_info()->get_filename() = %s \n",statement->get_file_info()->get_filename());
                         printf ("parentStatement == NULL: statement->get_file_info()->get_line() = %d \n",statement->get_file_info()->get_line());
                       }
                    ROSE_ASSERT (parentStatement != NULL);

                 // Call this function recursively (directly rather than through the query mechanism)
                    getTransformationOptions ( parentStatement, generatedList, identifingTypeName );
                  }
                 else
                  {
                 // printf ("astNode is not a SgStatement! \n");
                  }

               break;
        }

#if 0
     printf ("At BASE of getTransformationOptions(): astNode->sage_class_name() = %s size of generatedList = %d \n",
          astNode->sage_class_name(),generatedList.size());
#endif

  // printf ("######################### END OF TRANSFORMATION OPTION QUERY ######################## \n");
   }
void RewriteFSM::visitSgFunctionDeclaration(SgFunctionDeclaration *FD)
{
  SgFunctionDefinition *fdef = FD->get_definition();
  if (!fdef) {
    return;
  }

  if (debugHooks) {
    std::cout << "Func decl: " << FD << " " << FD->get_name() << std::endl;
  }

  std::string modName = FD->get_name().getString();
  HtdInfoAttribute *htd = getHtdInfoAttribute(fdef);

  bool isStreamingStencil = false;
  size_t pos = 0;
  if ((pos = modName.find(StencilStreamPrefix)) != std::string::npos
      && pos == 0) {
    isStreamingStencil = true;
  }

#define hostEntryPrefix  "__HTC_HOST_ENTRY_"
  bool isHostEntry = false;
  if ((pos = modName.find(hostEntryPrefix)) != std::string::npos
      && pos == 0) {
    isHostEntry = true;
  }

  // Emit a default, unnamed thread group.
  std::string modWidth = boost::to_upper_copy(modName) + "_HTID_W";
  if (isStreamingStencil) {
    // The streaming version of a stencil must have width 0.
    htd->appendDefine(modWidth, "0");
  } else if (isHostEntry) {
    htd->appendDefine(modWidth, "1");
  } else if (htd->moduleWidth != -1) {
    htd->appendDefine(modWidth, boost::lexical_cast<std::string>(htd->moduleWidth));
  } else {
    DefaultModuleWidthAttribute *dwAttr = 
        getDefaultModuleWidthAttribute(SageInterface::getGlobalScope(fdef));
    if (dwAttr) {
      htd->appendDefine(modWidth, boost::lexical_cast<std::string>(dwAttr->width));
    } else { 
      htd->appendDefine(modWidth, "5");
    }
  }
  htd->appendModule(modName, "", modWidth);

  // For streaming stencils, ProcessStencils inserts a canned sequence,
  // so we bypass generating a normal FSM.
  if (isStreamingStencil) {
    return;
  }

  //
  // Create new case body blocks for each state.
  // The first executable statement starts the first state, and each
  // label starts a new state.
  //
  std::map<SgLabelStatement *, int> labelToState;
  std::map<int, std::string> stateToName;
  SgBasicBlock *funcBody = isSgBasicBlock(fdef->get_body());
  SgStatementPtrList &stmts = funcBody->get_statements();

  std::vector<SgStatement *>::iterator SI, SP;
  for (SI = stmts.begin(); SI != stmts.end(); ++SI) {
    if (!isSgDeclarationStatement(*SI)) {
      break;
    }
  }
  if (SI == stmts.end()) {
    return;
  }
  SP = SI;

  std::vector<SgBasicBlock *> newBlocks;
  SgBasicBlock *newbb = SageBuilder::buildBasicBlock();
  newBlocks.push_back(newbb);
  stateToName[1] = "__START";
  bool prevIsLabel = false;
  for (; SI != stmts.end(); ++SI) {
    SgStatement *stmt = *SI;
    SgLabelStatement *labstmt = isSgLabelStatement(stmt);
    if (labstmt) {
      if (!prevIsLabel) {
        newbb = SageBuilder::buildBasicBlock();
        newBlocks.push_back(newbb);
      }
      int snum = newBlocks.size();
      labelToState[labstmt] = snum;
      stateToName[snum] += "__" + labstmt->get_label().getString();
      prevIsLabel = true;
#if 1
      // TODO: these labels can carry preproc infos-- but the unparser
      // doesn't output them if the label is not actually output.
      AttachedPreprocessingInfoType *comments =
          labstmt->getAttachedPreprocessingInfo();
      if (comments && comments->size() > 0) {
        std::cerr << "DEVWARN: losing Preprocinfo on label" << std::endl;
        SageInterface::dumpPreprocInfo(labstmt);
      }
#endif
      stmt->unsetOutputInCodeGeneration();
      SageInterface::appendStatement(stmt, newbb);
    } else {
      prevIsLabel = false;
      SageInterface::appendStatement(stmt, newbb);
    }
  }
  stmts.erase(SP, stmts.end());

  // Add module name to each state name and create enum decl.
  SgEnumDeclaration *enumDecl = SageBuilder::buildEnumDeclaration("states",
    fdef);
  for (int i = 1; i <= newBlocks.size(); i++) {
    stateToName[i] = modName + stateToName[i];
    boost::to_upper(stateToName[i]);
    SgName nm(stateToName[i]);
    SgInitializedName *enumerator = SageBuilder::buildInitializedName(nm,
        SageBuilder::buildIntType(),
        SageBuilder::buildAssignInitializer(SageBuilder::buildIntVal(i)));
    enumerator->set_scope(funcBody);
    enumDecl->append_enumerator(enumerator);

    // Add the instruction to the htd info.
    htd->appendInst(nm.getString());
  }
  SageInterface::prependStatement(enumDecl, funcBody);
  if (!debugHooks) {
    enumDecl->unsetOutputInCodeGeneration();
  }

  SgGlobal *GS = SageInterface::getGlobalScope(FD);
  SgVariableDeclaration *declHtValid = 
      HtDeclMgr::buildHtlVarDecl("PR_htValid", GS);
  SgVariableDeclaration *declHtInst = 
      HtDeclMgr::buildHtlVarDecl("PR_htInst", GS);
  SgFunctionDeclaration *declHtContinue = 
      HtDeclMgr::buildHtlFuncDecl("HtContinue", GS);
  SgFunctionDeclaration *declHtAssert =
     HtDeclMgr::buildHtlFuncDecl("HtAssert", GS);

  //
  // Create the finite state machine switch statement "switch (PR_htInst)",
  // and insert guard "if (PR_htValid)".
  //
  SgBasicBlock *newSwitchBody = SageBuilder::buildBasicBlock();

  SgExpression *htInstExpr = SageBuilder::buildVarRefExp(declHtInst);
  SgSwitchStatement *newSwitch = 
      SageBuilder::buildSwitchStatement(htInstExpr, newSwitchBody);

  SgExpression *htValidExpr = SageBuilder::buildVarRefExp(declHtValid);
  SgIfStmt *newIfStmt = SageBuilder::buildIfStmt(htValidExpr,
      SageBuilder::buildBasicBlock(newSwitch), 0);
  SageInterface::appendStatement(newIfStmt, funcBody);

  int casenum = 1;
  foreach (SgBasicBlock *newCaseBody, newBlocks) {
    SgExpression *caseExpr = 
        SageBuilder::buildEnumVal_nfi(casenum, enumDecl, stateToName[casenum]);
    SgCaseOptionStmt *newCase = 
        SageBuilder::buildCaseOptionStmt(caseExpr, newCaseBody);
    SageInterface::appendStatement(newCase, newSwitchBody);
    casenum++;
  }
Example #18
0
POETCode* POETAstInterface::Ast2POET(const Ast& n)
{
  static SgTemplateInstantiationFunctionDecl* tmp=0;
  SgNode* input = (SgNode*) n;
  if (input == 0) return EMPTY;
  POETCode* res = POETAstInterface::find_Ast2POET(input);
  if (res != 0) return res;

  {
  SgProject* sageProject=isSgProject(input); 
  if (sageProject != 0) {
    int filenum = sageProject->numberOfFiles(); 
    for (int i = 0; i < filenum; ++i) { 
      SgSourceFile* sageFile = isSgSourceFile(sageProject->get_fileList()[i]); 
      SgGlobal *root = sageFile->get_globalScope(); 
      SgDeclarationStatementPtrList declList = root->get_declarations ();
      POETCode* curfile = ROSE_2_POET_list(declList, 0, tmp);
      curfile = new POETCode_ext(sageFile, curfile);
      POETAstInterface::set_Ast2POET(sageFile, curfile);
      res=LIST(curfile, res);
    }
    POETAstInterface::set_Ast2POET(sageProject,res); 
    return res;
  } }
  { 
  SgBasicBlock* block = isSgBasicBlock(input);
  if (block != 0) {
    res=ROSE_2_POET_list(block->get_statements(), res, tmp);
    POETAstInterface::set_Ast2POET(block, res); 
    return res;
  } }
  { 
  SgExprListExp* block = isSgExprListExp(input);
  if (block != 0) {
    res=ROSE_2_POET_list(block->get_expressions(), 0, tmp);
    POETAstInterface::set_Ast2POET(block, res); 
    return res;
  } }
 {
  SgForStatement *f = isSgForStatement(input);
  if (f != 0) {
      POETCode* init = ROSE_2_POET_list(f->get_for_init_stmt()->get_init_stmt(),0, tmp);
      POETCode* ctrl = new POETCode_ext(f, TUPLE3(init,Ast2POET(f->get_test_expr()), Ast2POET(f->get_increment())));
      res = CODE_ACC("Nest", PAIR(ctrl,Ast2POET(f->get_loop_body())));  
      POETAstInterface::set_Ast2POET(input, res); 
      return res;
  }
  }
  {
    SgVarRefExp * v = isSgVarRefExp(input);
    if (v != 0) {
       res = STRING(v->get_symbol()->get_name().str());
       POETAstInterface::set_Ast2POET(input, res); 
       return res;
    }
  }
  {
    SgMemberFunctionRefExp * v = isSgMemberFunctionRefExp(input);
    if (v != 0) {
       res = STRING(v->get_symbol()->get_name().str());
       POETAstInterface::set_Ast2POET(input, res); 
       return res;
    }
  }
  {
    SgIntVal * v = isSgIntVal(input);
    if (v != 0) {
       res = ICONST(v->get_value());
       POETAstInterface::set_Ast2POET(input, res); 
       return res;
    }
  }
  {
   SgInitializedName* var = isSgInitializedName(input);
   if (var != 0) {
     POETCode* name = STRING(var->get_name().str()); 
     POETCode* init = Ast2POET(var->get_initializer());
     res = new POETCode_ext(var, PAIR(name,init));
     POETAstInterface::set_Ast2POET(input, res); 
     return res;
   }
  }

/*
  {
  std::string fname;
  AstInterface::AstList params;
  AstNodeType returnType;
  AstNodePtr body;
  if (AstInterface :: IsFunctionDefinition( input, &fname, &params, (AstInterface::AstList*)0, &body, (AstInterface::AstTypeList*)0, &returnType)) {
if (body != AST_NULL)
 std::cerr << "body not empty:" << fname << "\n";
      POETCode* c = TUPLE4(STRING(fname), ROSE_2_POET_list(0,params,0), 
                 STRING(AstInterface::GetTypeName(returnType)), 
                 Ast2POET(body.get_ptr()));
      res = new POETCode_ext(input, c);
      POETAstInterface::set_Ast2POET(input,res);
      return res;
  } }   
*/

  AstInterface::AstList c = AstInterface::GetChildrenList(input);
  switch (input->variantT()) {
    case V_SgCastExp:
    case V_SgAssignInitializer:
      res = Ast2POET(c[0]); 
      POETAstInterface::set_Ast2POET(input, res); return res; 
    case V_SgDotExp:
     {
      POETCode* v1 = Ast2POET(c[1]);
      if (dynamic_cast<POETString*>(v1)->get_content() == "operator()") 
           return Ast2POET(c[0]);
      res = CODE_ACC("Bop",TUPLE3(STRING("."), Ast2POET(c[0]), v1)); return res;
     }
    case V_SgLessThanOp:
      res = CODE_ACC("Bop",TUPLE3(STRING("<"),Ast2POET(c[0]), Ast2POET(c[1])));
      POETAstInterface::set_Ast2POET(input, res); return res;
    case V_SgSubtractOp:
      res = CODE_ACC("Bop",TUPLE3(STRING("-"),Ast2POET(c[0]), Ast2POET(c[1])));
      POETAstInterface::set_Ast2POET(input, res); return res;
    case V_SgAddOp:
      res = CODE_ACC("Bop",TUPLE3(STRING("+"),Ast2POET(c[0]), Ast2POET(c[1])));
      POETAstInterface::set_Ast2POET(input, res); return res;
    case V_SgMultiplyOp:
      res = CODE_ACC("Bop",TUPLE3(STRING("*"),Ast2POET(c[0]), Ast2POET(c[1])));
      POETAstInterface::set_Ast2POET(input, res); return res;
    case V_SgDivideOp:
      res = CODE_ACC("Bop",TUPLE3(STRING("/"),Ast2POET(c[0]), Ast2POET(c[1])));
      POETAstInterface::set_Ast2POET(input, res); return res;
    case V_SgAssignOp:
      res = CODE_ACC("Assign",PAIR(Ast2POET(c[0]), Ast2POET(c[1])));
      POETAstInterface::set_Ast2POET(input, res); return res;
    case V_SgFunctionCallExp:
      res = CODE_ACC("FunctionCall",PAIR(Ast2POET(c[0]), Ast2POET(c[1])));
      POETAstInterface::set_Ast2POET(input, res); return res;
  } 
  POETCode * c2 = 0; 
  if (tmp == 0) tmp=isSgTemplateInstantiationFunctionDecl(input);
   switch (c.size()) {
   case 0: break;
   case 1: c2 = Ast2POET(c[0]); break;
   case 2: c2 = PAIR(Ast2POET(c[0]),Ast2POET(c[1])); break;
   case 3: c2 = TUPLE3(Ast2POET(c[0]),Ast2POET(c[1]),Ast2POET(c[2])); break;
   case 4: c2 = TUPLE4(Ast2POET(c[0]),Ast2POET(c[1]),Ast2POET(c[2]),Ast2POET(c[3])); break;
   default: 
     //std::cerr << "too many children: " << c.size() << ":" << input->unparseToString() << "\n";
     c2 = EMPTY;
   }
  if (tmp == input) tmp = 0;
  res = new POETCode_ext(input, c2); 
  POETAstInterface::set_Ast2POET(input,res);
  return res;
}
void
InsertFortranContainsStatement::visit ( SgNode* node )
   {
  // DQ (10/3/2008): This bug in OFP is now fixed so no fixup is required.
     printf ("Error: fixup of contains statement no longer required. \n");
     ROSE_ASSERT(false);

  // DQ (11/24/2007): Output the current IR node for debugging the traversal of the Fortran AST.
     ROSE_ASSERT(node != NULL);
#if 0
     Sg_File_Info* fileInfo = node->get_file_info();

     printf ("node = %s fileInfo = %p \n",node->class_name().c_str(),fileInfo);
     if (fileInfo != NULL)
        {
          bool isCompilerGenerated = fileInfo->isCompilerGenerated();
          std::string filename = fileInfo->get_filenameString();
          int line_number = fileInfo->get_line();
          int column_number = fileInfo->get_line();

          printf ("--- isCompilerGenerated = %s position = %d:%d filename = %s \n",isCompilerGenerated ? "true" : "false",line_number,column_number,filename.c_str());
        }
#endif

     SgFunctionDefinition* functionDefinition = isSgFunctionDefinition(node);

  // This is for handling where CONTAINS is required in a function
     if (functionDefinition != NULL)
        {
          SgBasicBlock* block = functionDefinition->get_body();
          SgStatementPtrList & statementList = block->get_statements();
          SgStatementPtrList::iterator i = statementList.begin();

          bool firstFunctionDeclaration = false;
          bool functionDeclarationSeen  = false;

          while (i != statementList.end() && firstFunctionDeclaration == false)
             {
               SgFunctionDeclaration* functionDeclaration = isSgFunctionDeclaration(*i);

            // DQ (1/20/2008): Note that entry statements should not cause introduction of a contains statement!
               if (isSgEntryStatement(functionDeclaration) != NULL)
                    functionDeclaration = NULL;

               if (functionDeclaration != NULL)
                  {
                    firstFunctionDeclaration = functionDeclarationSeen == false;
                    functionDeclarationSeen  = true;

                    if (firstFunctionDeclaration == true)
                       {
                      // Insert a CONTAINS statement.
                      // printf ("Building a contains statement (in function) \n");
                         SgContainsStatement* containsStatement = new SgContainsStatement();
                         SageInterface::setSourcePosition(containsStatement);
                         containsStatement->set_definingDeclaration(containsStatement);

                         block->get_statements().insert(i,containsStatement);
                         containsStatement->set_parent(block);
                         ROSE_ASSERT(containsStatement->get_parent() != NULL);
                       }
                  }

               i++;
             }
        }

#if 0
  // OFP now has better support for the CONTAINS statement so this code is not longer required.

  // The use of CONTAINS in modules appears to be handled by OFP, so no fixup is required.
     SgClassDefinition* classDefinition = isSgClassDefinition(node);

  // This is for handling where CONTAINS is required in a module
     if (classDefinition != NULL)
        {
          SgDeclarationStatementPtrList & statementList = classDefinition->get_members();
          SgDeclarationStatementPtrList::iterator i = statementList.begin();

          bool firstFunctionDeclaration = false;
          bool functionDeclarationSeen  = false;

          while (i != statementList.end() && firstFunctionDeclaration == false)
             {
               printf ("InsertFortranContainsStatement: *i in statementList in module = %p = %s \n",*i,(*i)->class_name().c_str());

               SgFunctionDeclaration* functionDeclaration = isSgFunctionDeclaration(*i);
               if (functionDeclaration != NULL)
                  {
                    firstFunctionDeclaration = functionDeclarationSeen == false;
                    functionDeclarationSeen  = true;

                    if (firstFunctionDeclaration == true)
                       {
                      // Insert a CONTAINS statement.
                      // printf ("Building a contains statement (in module) \n");
                         SgContainsStatement* containsStatement = new SgContainsStatement();
                         SageInterface::setSourcePosition(containsStatement);
                         containsStatement->set_definingDeclaration(containsStatement);

                      // This insert function does not set the parent (unlike for SgBasicBlock)
                         classDefinition->get_members().insert(i,containsStatement);
                         containsStatement->set_parent(classDefinition);
                         ROSE_ASSERT(containsStatement->get_parent() != NULL);
                       }
                  }

               i++;
             }
        }
#endif
   }
Example #20
0
set<SgInitializedName*> computeLiveVars(SgStatement* stmt, const X86CTranslationPolicy& conv, map<SgLabelStatement*, set<SgInitializedName*> >& liveVarsForLabels, set<SgInitializedName*> currentLiveVars, bool actuallyRemove) {
  switch (stmt->variantT()) {
    case V_SgBasicBlock: {
      const SgStatementPtrList& stmts = isSgBasicBlock(stmt)->get_statements();
      for (size_t i = stmts.size(); i > 0; --i) {
        currentLiveVars = computeLiveVars(stmts[i - 1], conv, liveVarsForLabels, currentLiveVars, actuallyRemove);
      }
      return currentLiveVars;
    }
    case V_SgPragmaDeclaration: return currentLiveVars;
    case V_SgDefaultOptionStmt: return currentLiveVars;
    case V_SgCaseOptionStmt: {
      return computeLiveVars(isSgCaseOptionStmt(stmt)->get_body(), conv, liveVarsForLabels, currentLiveVars, actuallyRemove);
    }
    case V_SgLabelStatement: {
      liveVarsForLabels[isSgLabelStatement(stmt)] = currentLiveVars;
      return currentLiveVars;
    }
    case V_SgGotoStatement: {
      return liveVarsForLabels[isSgGotoStatement(stmt)->get_label()];
    }
    case V_SgSwitchStatement: {
      SgSwitchStatement* s = isSgSwitchStatement(stmt);
      SgBasicBlock* swBody = isSgBasicBlock(s->get_body());
      ROSE_ASSERT (swBody);
      const SgStatementPtrList& bodyStmts = swBody->get_statements();
      set<SgInitializedName*> liveForBody; // Assumes any statement in the body is possible
      for (size_t i = 0; i < bodyStmts.size(); ++i) {
        setUnionInplace(liveForBody, computeLiveVars(bodyStmts[i], conv, liveVarsForLabels, currentLiveVars, actuallyRemove));
      }
      return computeLiveVars(s->get_item_selector(), conv, liveVarsForLabels, liveForBody, actuallyRemove);
    }
    case V_SgContinueStmt: {
      return makeAllPossibleVars(conv);
    }
    case V_SgIfStmt: {
      set<SgInitializedName*> liveForBranches = computeLiveVars(isSgIfStmt(stmt)->get_true_body(), conv, liveVarsForLabels, currentLiveVars, actuallyRemove);
      setUnionInplace(liveForBranches, (isSgIfStmt(stmt)->get_false_body() != NULL ? computeLiveVars(isSgIfStmt(stmt)->get_false_body(), conv, liveVarsForLabels, currentLiveVars, actuallyRemove) : set<SgInitializedName*>()));
      return computeLiveVars(isSgIfStmt(stmt)->get_conditional(), conv, liveVarsForLabels, liveForBranches, actuallyRemove);
    }
    case V_SgWhileStmt: {
      while (true) {
        set<SgInitializedName*> liveVarsSave = currentLiveVars;
        currentLiveVars = computeLiveVars(isSgWhileStmt(stmt)->get_body(), conv, liveVarsForLabels, currentLiveVars, false);
        currentLiveVars = computeLiveVars(isSgWhileStmt(stmt)->get_condition(), conv, liveVarsForLabels, currentLiveVars, false);
        setUnionInplace(currentLiveVars, liveVarsSave);
        if (liveVarsSave == currentLiveVars) break;
      }
      if (actuallyRemove) {
        set<SgInitializedName*> liveVarsSave = currentLiveVars;
        currentLiveVars = computeLiveVars(isSgWhileStmt(stmt)->get_body(), conv, liveVarsForLabels, currentLiveVars, true);
        currentLiveVars = computeLiveVars(isSgWhileStmt(stmt)->get_condition(), conv, liveVarsForLabels, currentLiveVars, true);
        setUnionInplace(currentLiveVars, liveVarsSave);
      }
      return currentLiveVars;
    }
    case V_SgBreakStmt: return set<SgInitializedName*>();
    case V_SgExprStatement: {
      SgExpression* e = isSgExprStatement(stmt)->get_expression();
      switch (e->variantT()) {
        case V_SgAssignOp: {
          SgVarRefExp* lhs = isSgVarRefExp(isSgAssignOp(e)->get_lhs_operand());
          ROSE_ASSERT (lhs);
          SgInitializedName* in = lhs->get_symbol()->get_declaration();
          if (currentLiveVars.find(in) == currentLiveVars.end()) {
            if (actuallyRemove) {
              // cerr << "Removing assignment " << e->unparseToString() << endl;
              isSgStatement(stmt->get_parent())->remove_statement(stmt);
            }
            return currentLiveVars;
          } else {
            currentLiveVars.erase(in);
            getUsedVariables(isSgAssignOp(e)->get_rhs_operand(), currentLiveVars);
            return currentLiveVars;
          }
        }
        case V_SgFunctionCallExp: {
          getUsedVariables(e, currentLiveVars);
          SgFunctionRefExp* fr = isSgFunctionRefExp(isSgFunctionCallExp(e)->get_function());
          ROSE_ASSERT (fr);
          if (fr->get_symbol()->get_declaration() == conv.interruptSym->get_declaration()) {
            setUnionInplace(currentLiveVars, makeAllPossibleVars(conv));
            return currentLiveVars;
          } else {
            return currentLiveVars;
          }
        }
        default: {
          getUsedVariables(e, currentLiveVars);
          return currentLiveVars;
        }
      }
    }
    case V_SgVariableDeclaration: {
      ROSE_ASSERT (isSgVariableDeclaration(stmt)->get_variables().size() == 1);
      SgInitializedName* in = isSgVariableDeclaration(stmt)->get_variables()[0];
      bool isConst = isConstType(in->get_type());
      if (currentLiveVars.find(in) == currentLiveVars.end() && isConst) {
        if (actuallyRemove) {
          // cerr << "Removing decl " << stmt->unparseToString() << endl;
          isSgStatement(stmt->get_parent())->remove_statement(stmt);
        }
        return currentLiveVars;
      } else {
        currentLiveVars.erase(in);
        if (in->get_initializer()) {
          getUsedVariables(in->get_initializer(), currentLiveVars);
        }
        return currentLiveVars;
      }
    }
    default: cerr << "computeLiveVars: " << stmt->class_name() << endl; abort();
  }
}
Example #21
0
ClastToSage::ClastToSage(SgScopeStatement* scopScope,
			 clast_stmt* root,
			 scoplib_scop_p scoplibScop,
			 PolyRoseOptions& options)
{
  //SgScopeStatement* scope = isSgScopeStatement((SgNode*) scoplibScop->usr);
  _polyoptions = options;
  SgScopeStatement* scope = scopScope;
  ROSE_ASSERT(scope);
  m_scopRoot = NULL;
  /// OLD:
  m_scope = scope;
  m_scoplib_scop = scoplibScop;
  m_verbose = _polyoptions.isVerbose();

  // 0- Retrive meta information stored as an annotation of the
  // SageAST root.
  SgStatement* scopRoot = isSgStatement((SgNode*)(scoplibScop->usr));
  ScopRootAnnotation* annot =
    (ScopRootAnnotation*)(scopRoot->getAttribute("ScopRoot"));
  ROSE_ASSERT(annot);
  _fakeSymbolMap = annot->fakeSymbolMap;

  // 1- Collect all iterators in the clast. They are of the from 'cXX'
  // where XX is an integer.
  _scoplibIterators = collectAllIterators(root);

  // 2- Map clast iterator to new variables that does not conflict
  // with existing names, and register the symbols in the symbol
  // table.
  _sageIterators = createNewIterators(_scoplibIterators, scope);

  // 3- Create the basic block containing the transformed scop.
  SgBasicBlock* bb = buildBasicBlock(root);
  // 4- Update the variable scope with the new bb, and insert the
  // declaration statement in the AST.
  std::map<const char*, SgVariableDeclaration*>::iterator iter;
  Rose_STL_Container<SgNode*> varRefs =
    NodeQuery::querySubTree(bb,V_SgVarRefExp);
  for(iter = _sageIterators.begin(); iter != _sageIterators.end(); ++iter)
    {
      // Deal with the symbol tables.
      SgInitializedNamePtrList& l = iter->second->get_variables();
      for (SgInitializedNamePtrList::iterator i = l.begin(); i != l.end(); i++)
	{
	  (*i)->set_scope(bb);
	  SgVariableSymbol* sym = new SgVariableSymbol(*i);
          bb->insert_symbol((*i)->get_name(), sym);
	  // Ugly hack: replace the old symbol with the new entry in
	  // the symbol table.
	  for (Rose_STL_Container<SgNode *>::iterator j = varRefs.begin();
	       j != varRefs.end(); j++)
	    {
	      SgVarRefExp *vRef = isSgVarRefExp((*j));
	      if (vRef->get_symbol()->get_name() == (*i)->get_name())
		vRef->set_symbol(sym);
	    }

	}
      // Insert the declaration statement in the BB.
      bb->prepend_statement(iter->second);
    }

  // Post-process for pragma insertion.
  if (options.getGeneratePragmas())
    insertPragmas(bb);

  m_scopRoot = bb;
}
Example #22
0
void
SgNode::insertSourceCode ( SgProject & project,
                           const char* sourceCodeString,
                           const char* localDeclaration,
                           const char* globalDeclaration,
                           bool locateNewCodeAtTop,
                           bool isADeclaration )
   {
  // If this function is useful only for SgBasicBlock then it should be put into that class directly.
  // This function is used to insert code into AST object for which insertion make sense:
  // (specifically any BASIC_BLOCK_STMT)

     if (variant() != BASIC_BLOCK_STMT)
        {
          printf ("ERROR: insert only make since for BASIC_BLOCK_STMT statements (variant() == variant()) \n");
          ROSE_ABORT();
        }

     SgBasicBlock* currentBlock = isSgBasicBlock(this);
     ROSE_ASSERT (currentBlock != NULL);

  // printf ("##### Calling SgNode::generateAST() \n");

#if 0
     printf ("In insertSourceCode(): globalDeclaration = \n%s\n",globalDeclaration);
     printf ("In insertSourceCode(): localDeclaration  = \n%s\n",localDeclaration);
     printf ("In insertSourceCode(): sourceCodeString  = \n%s\n",sourceCodeString);
#endif

  // SgNode* newTransformationAST = generateAST (project,sourceCodeString,globalDeclaration);
  // ROSE_ASSERT (newTransformationAST != NULL);
     SgStatementPtrList* newTransformationStatementListPtr =
          generateAST (project,sourceCodeString,localDeclaration,globalDeclaration,isADeclaration);
     ROSE_ASSERT (newTransformationStatementListPtr != NULL);
     ROSE_ASSERT (newTransformationStatementListPtr->size() > 0);

  // printf ("##### DONE: Calling SgNode::generateAST() \n");

  // get a reference to the statement list out of the basic block
     SgStatementPtrList & currentStatementList = currentBlock->get_statements();

     if (locateNewCodeAtTop == true)
        {
       // Insert at top of list (pull the elements off the bottom of the new statement list to get the order correct
       // printf ("Insert new statements (new statement list size = %d) at the top of the block (in reverse order to preset the order in the final block) \n",newTransformationStatementListPtr->size());
          SgStatementPtrList::reverse_iterator transformationStatementIterator;
          for (transformationStatementIterator = newTransformationStatementListPtr->rbegin();
               transformationStatementIterator != newTransformationStatementListPtr->rend();
               transformationStatementIterator++)
             {
            // Modify where a statement is inserted to avoid dependent variables from being inserted
            // before they are declared.

            // Get a list of the variables
            // Generate the list of types used within the target subtree of the AST
               list<string> typeNameStringList = NameQuery::getTypeNamesQuery ( *transformationStatementIterator );

               int statementCounter         = 0;
               int previousStatementCounter = 0;

            // Declaration furthest in source sequence of all variables referenced in code to be inserted (last in source sequence order)
//             SgStatementPtrList::iterator furthestDeclarationInSourceSequence = NULL;
               SgStatementPtrList::iterator furthestDeclarationInSourceSequence;

#if 0
               string unparsedDeclarationCodeString = (*transformationStatementIterator)->unparseToString();
               ROSE_ASSERT (unparsedDeclarationCodeString.c_str() != NULL);
               printf ("unparsedDeclarationCodeString = %s \n",unparsedDeclarationCodeString.c_str());
#endif
               if ( typeNameStringList.size() > 0 )
                  {
                 // There should be at least one type in the statement
                    ROSE_ASSERT (typeNameStringList.size() > 0);
                 // printf ("typeNameStringList.size() = %d \n",typeNameStringList.size());

                 // printf ("This statement has a dependence upon a variable of some type \n");

                 // Loop over all the types and get list of variables of each type
                 // (so they can be declared properly when the transformation is compiled)
                    list<string>::iterator typeListStringElementIterator;
                    for (typeListStringElementIterator = typeNameStringList.begin();
                         typeListStringElementIterator != typeNameStringList.end();
                         typeListStringElementIterator++)
                       {
                      // printf ("Type = %s \n",(*typeListStringElementIterator).c_str());

                      // Find a list of names of variable of type (*listStringElementIterator)
                         list<string> operandNameStringList =
                              NameQuery::getVariableNamesWithTypeNameQuery
                                 ( *transformationStatementIterator, *typeListStringElementIterator );

                      // There should be at least one variable of that type in the statement
                         ROSE_ASSERT (operandNameStringList.size() > 0);
                      // printf ("operandNameStringList.size() = %d \n",operandNameStringList.size());

                      // Loop over all the types and get list of variable of each type
                         list<string>::iterator variableListStringElementIterator;
                         for (variableListStringElementIterator = operandNameStringList.begin();
                              variableListStringElementIterator != operandNameStringList.end();
                              variableListStringElementIterator++)
                            {
#if 0
                              printf ("Type = %s Variable = %s \n",
                                   (*typeListStringElementIterator).c_str(),
                                   (*variableListStringElementIterator).c_str());
#endif
                              string variableName = *variableListStringElementIterator;
                              string typeName     = *typeListStringElementIterator;

                              SgName name = variableName.c_str();
                              SgVariableSymbol* symbol = currentBlock->lookup_var_symbol(name);
                              if ( symbol != NULL )
                                 {
                                // found a variable with name -- make sure that the declarations 
                                // represented by *transformationStatementIterator are inserted 
                                // after their declaration.
#if 0
                                   printf ("Found a valid symbol corresponding to Type = %s Variable = %s (must be defined in the local scope) \n",
                                        (*typeListStringElementIterator).c_str(),
                                        (*variableListStringElementIterator).c_str());
#endif
                                   ROSE_ASSERT (symbol != NULL);
                                   SgInitializedName* declarationInitializedName = symbol->get_declaration();
                                   ROSE_ASSERT (declarationInitializedName != NULL);
                                   SgDeclarationStatement* declarationStatement  =
                                        declarationInitializedName->get_declaration();
                                   ROSE_ASSERT (declarationStatement != NULL);
#if 0
                                   printf ("declarationStatementString located at line = %d of file = %s \n",
                                        rose::getLineNumber(declarationStatement),
                                        rose::getFileName(declarationStatement));
                                   string declarationStatementString = declarationStatement->unparseToString();
                                   printf ("declarationStatementString = %s \n",declarationStatementString.c_str());
#endif
                                   statementCounter = 1;

                                   SgStatementPtrList::iterator i = currentStatementList.begin();
                                   bool declarationFound = false;
                                   while ( ( i != currentStatementList.end() ) && ( declarationFound == false ) )
                                      {
                                     // searching for the declarationStatement
#if 0
                                        printf ("statementCounter = %d previousStatementCounter = %d \n",
                                             statementCounter,previousStatementCounter);
                                        string currentStatementString = (*i)->unparseToString();
                                        printf ("currentStatementString = %s \n",currentStatementString.c_str());
#endif
                                        if ( (*i == declarationStatement) &&
                                             (statementCounter > previousStatementCounter) )
                                           {
                                          // printf ("Found the declarationStatement at position (statementCounter = %d previousStatementCounter = %d) \n",statementCounter,previousStatementCounter);
                                             declarationFound = true;
                                           }
                                          else
                                           {
                                          // printf ("Not the declaration we are looking for! \n");
                                             i++;
                                             statementCounter++;
                                           }
                                      }

                                // Save a reference to the variable declaration that is furthest in
                                // the source sequence so that we can append the new statement just
                                // after it (so that variables referenced in the new statement will
                                // be defined).
                                   if ( (statementCounter > previousStatementCounter) && ( declarationFound == true ) )
                                      {
                                        previousStatementCounter = statementCounter;
                                        furthestDeclarationInSourceSequence = i;
                                      }
#if 0
                                   printf ("AFTER LOOP OVER STATEMENTS: previousStatementCounter = %d \n",previousStatementCounter);
                                   string lastStatementString = (*furthestDeclarationInSourceSequence)->unparseToString();
                                   printf ("lastStatementString = %s \n",lastStatementString.c_str());
#endif
                                 }
                                else
                                 {
                                // If the variable is not found then insert the new statement at the front of the list
#if 0
                                   printf ("Can NOT find a valid symbol corresponding to Type = %s Variable = %s (so it is not declared in the local scope) \n",
                                        (*typeListStringElementIterator).c_str(),
                                        (*variableListStringElementIterator).c_str());
#endif
                                // currentStatementList.push_front(*transformationStatementIterator);
                                 }
#if 0
                              printf ("BOTTOM OF LOOP OVER VARIABLES: previousStatementCounter = %d \n",previousStatementCounter);
#endif
                            }

                         if (statementCounter > previousStatementCounter)
                              previousStatementCounter = statementCounter;

#if 0
                         printf ("BOTTOM OF LOOP OVER TYPES: previousStatementCounter = %d \n",previousStatementCounter);
#endif
#if 0
                         printf ("Exiting in insertSourceCode(): transformationStatementIterator loop (type = %s) ... \n",(*typeListStringElementIterator).c_str());
                         ROSE_ABORT();
#endif
                       }

#if 0
                    printf ("Exiting in loop insertSourceCode (type = %s) ... \n",(*typeListStringElementIterator).c_str());
                    ROSE_ABORT();
#endif
                 // Now append the new statement AFTER the declaration that we have found
                 // currentStatementList.insert(*targetDeclarationStatementIterator,*transformationStatementIterator);
                 // currentStatementList.insert(lastStatementIterator,*transformationStatementIterator);
#if 1
                 // printf ("BEFORE ADDING NEW STATEMENT: previousStatementCounter = %d \n",previousStatementCounter);
                    if (previousStatementCounter == 0)
                       {
                         printf ("##### Prepend new statement to the top of the local scope \n");
                      // currentStatementList.push_front(*transformationStatementIterator);
                         currentBlock->prepend_statement (*transformationStatementIterator);
                       }
                      else
                       {
                      // printf ("##### Append the new statement after the last position where a dependent variable is declared in the local scope \n");
                      // Use new function added to append/prepend at a specified location in the list of statements
                         currentBlock->append_statement (furthestDeclarationInSourceSequence,*transformationStatementIterator);
                       }
#else
                    SgStatementPtrList::iterator tempIterator = furthestDeclarationInSourceSequence;
                    tempIterator++;
                 // Handle the case of appending at the end of the list
                    if ( tempIterator == currentStatementList.end() )
                       {
                         currentBlock->append_statement (*transformationStatementIterator);
                       }
                      else
                       {
                         currentBlock->insert_statement (tempIterator,*transformationStatementIterator);
                       }
#endif
                  }
                 else
                  {
                 // This statement has no type information (so it has no dependence upon any non-primative type)
                 // "int x;" would be an example of a statement that would not generate a type (though perhaps it should?)
                 // printf ("This statement has no type information (so it has no dependence upon any non-primative type) \n");

                 // So this statment can be places at the front of the list of statements in this block
                 // *** Note that we can't use the STL function directly since it does not set the parent information ***
                 // currentStatementList.push_front(*transformationStatementIterator);
                    currentBlock->insert_statement (currentStatementList.begin(),*transformationStatementIterator);
                  }

#if 0
               string bottomUnparsedDeclarationCodeString = (*transformationStatementIterator)->unparseToString();
               ROSE_ASSERT (bottomUnparsedDeclarationCodeString.c_str() != NULL);
               printf ("bottomUnparsedDeclarationCodeString = %s \n",bottomUnparsedDeclarationCodeString.c_str());
#endif
             }

#if 0
          printf ("Exiting in insertSourceCode(): case of locateNewCodeAtTop == true ... \n");
          ROSE_ABORT();
#endif
        }
       else
        {
       // Put the new statements at the end of the list (traverse the new statements from first to last)
       // But put it before any return statement! So find the last statement!
          SgStatementPtrList::iterator lastStatement = currentStatementList.begin();
          bool foundEndOfList = false;
          while ( (foundEndOfList == false) && (lastStatement != currentStatementList.end()) )
             {
               SgStatementPtrList::iterator tempStatement = lastStatement;
               tempStatement++;
               if (tempStatement == currentStatementList.end())
                    foundEndOfList = true;
                 else
                    lastStatement++;
             }
          ROSE_ASSERT ( *lastStatement != NULL );

       // printf ("(*lastStatement)->sage_class_name() = %s \n",(*lastStatement)->sage_class_name());

       // printf ("Insert new statements at the bottom of the block \n");
          SgStatementPtrList::iterator transformationStatementIterator;
          for (transformationStatementIterator = newTransformationStatementListPtr->begin();
               transformationStatementIterator != newTransformationStatementListPtr->end();
               transformationStatementIterator++)
             {
            // If there is a RETURN_STMT in the block then insert the new statement just before the
            // existing RETURN_STMT
               if ( (*lastStatement)->variant() == RETURN_STMT)
                  {
                 // printf ("Backing away from the end of the list to find the last non-return statement \n");
                 // lastStatement--;
                    currentBlock->insert_statement(lastStatement,*transformationStatementIterator);
                  }
                 else
                  {
                    currentStatementList.push_back(*transformationStatementIterator);
                  }
             }
        }

  // printf ("$CLASSNAME::insertSourceCode taking (SgProject,char*,char*) not implemented yet! \n");
  // ROSE_ABORT();
   }
void
FunctionCallNormalization::visit( SgNode *astNode )
   {
     SgStatement *stm = isSgStatement( astNode );

     // visiting all statements which may contain function calls;
     // Note 1: we do not look at the body of loops, or sequences of statements, but only
     // at statements which may contain directly function calls; all other statements will have their component parts visited in turn
     if ( isSgEnumDeclaration( astNode ) || isSgVariableDeclaration( astNode ) || isSgVariableDefinition( astNode ) ||
                               isSgExprStatement( astNode ) || isSgForStatement( astNode ) || isSgReturnStmt( astNode ) ||
                               isSgSwitchStatement( astNode ) )
        {
       // maintain the mappings from function calls to expressions (variables or dereferenced variables)
          map<SgFunctionCallExp *, SgExpression *> fct2Var;

       // list of Declaration structures, one structure per function call
          DeclarationPtrList declarations;
          bool variablesDefined = false;
             
       // list of function calls, in correnspondence with the inForTest list below
          list<SgNode*> functionCallExpList;
          list<bool> inForTest;

          SgForStatement *forStm = isSgForStatement( stm );
          SgSwitchStatement *swStm = isSgSwitchStatement( stm );
          list<SgNode*> temp1, temp2;

       // for-loops and Switch statements have conditions ( and increment ) expressed as expressions
       // and not as standalone statements; this will change in future Sage versions
       // TODO: when for-loops and switch statements have conditions expressed via SgStatements
       // these cases won't be treated separately; however, do-while will have condition expressed via expression
       // so that will be the only exceptional case to be treated separately
          if (forStm != NULL)
             {
            // create a list of function calls in the condition and increment expression
            // the order is important, the condition is evaluated after the increment expression
            // temp1 = FEOQueryForNodes( forStm->get_increment_expr_root(), V_SgFunctionCallExp );
            // temp2 = FEOQueryForNodes( forStm->get_test_expr_root(), V_SgFunctionCallExp );
               temp1 = FEOQueryForNodes( forStm->get_increment(), V_SgFunctionCallExp );
               temp2 = FEOQueryForNodes( forStm->get_test_expr(), V_SgFunctionCallExp );
               functionCallExpList = temp1;
               functionCallExpList.splice( functionCallExpList.end(), temp2 );
             }
            else
             {
               if (swStm != NULL)
                  {
                 // create a list of function calls in the condition in the order of function evaluation
                 // DQ (11/23/2005): Fixed SgSwitchStmt to have SgStatement for conditional.
                 // list<SgNode*> temp1 = FEOQueryForNodes( swStm->get_item_selector_root(), V_SgFunctionCallExp );
                    list<SgNode*> temp1 = FEOQueryForNodes( swStm->get_item_selector(), V_SgFunctionCallExp );
                    functionCallExpList = temp1;
                  }
                 else
                  {
                 // create a list of function calls in the statement in the order of function evaluation
                    functionCallExpList = FEOQueryForNodes( stm, V_SgFunctionCallExp );
                  }
             }

         // all function calls get replaced: this is because they can occur in expressions (e.g. for-loops)
         // which makes it difficult to build control flow graphs
         if ( functionCallExpList.size() > 0 )
           {
             cout << "--------------------------------------\nStatement ";
             cout << stm->unparseToString() << "\n";;
             
             // traverse the list of function calls in the current statement, generate a structure  Declaration for each call
             // put these structures in a list to be inserted in the code later
             for ( list<SgNode *>::iterator i = functionCallExpList.begin(); i != functionCallExpList.end(); i++ )
               {
                 variablesDefined = true;

                 // get function call exp
                 SgFunctionCallExp *exp = isSgFunctionCallExp( *i );
                 ROSE_ASSERT ( exp );
                 
                 // get type of expression, generate unique variable name
                 SgType *expType = exp->get_type();
                 ROSE_ASSERT ( expType );
                 Sg_File_Info *location = Sg_File_Info::generateDefaultFileInfoForTransformationNode();
                 ROSE_ASSERT ( location );
                 ostringstream os;
                 os << "__tempVar__" << location;
                 SgName name = os.str().c_str();

                 // replace previous variable bindings in the AST
                 SgExprListExp *paramsList = exp->get_args();
                 SgExpression *function = exp->get_function();
                 ROSE_ASSERT ( paramsList && function );
                 replaceFunctionCallsInExpression( paramsList, fct2Var );
                 replaceFunctionCallsInExpression( function, fct2Var );

                 // duplicate function call expression, for the initialization declaration and the assignment
                 SgTreeCopy treeCopy;
                 SgFunctionCallExp *newExpInit = isSgFunctionCallExp( exp->copy( treeCopy ) );
                 ROSE_ASSERT ( newExpInit );
                 SgFunctionCallExp *newExpAssign = isSgFunctionCallExp( exp->copy( treeCopy ) );
                 ROSE_ASSERT ( newExpAssign );

                 // variables
                 Sg_File_Info *initLoc = Sg_File_Info::generateDefaultFileInfoForTransformationNode(),
                   *nonInitLoc = Sg_File_Info::generateDefaultFileInfoForTransformationNode(),
                   *assignLoc = Sg_File_Info::generateDefaultFileInfoForTransformationNode();
                 Declaration *newDecl = new Declaration();
                 SgStatement *nonInitVarDeclaration, *initVarDeclaration, *assignStmt;
                 SgExpression *varRefExp;
                 SgVariableSymbol *varSymbol;
                 SgAssignOp *assignOp;
                 SgInitializedName *initName;

                 bool pointerTypeNeeded = false;

                 // mark whether to replace inside or outside of ForStatement due to the
                 // function call being inside the test or the increment for a for-loop statement
                 // the 'inForTest' list is in 1:1  ordered correpondence with the 'declarations' list
                 if ( forStm )
                   {
        // SgExpressionRoot
                  //   *testExp = isSgForStatement( astNode )->get_test_expr_root(),
                  //   *incrExp = isSgForStatement( astNode )->get_increment_expr_root();
                     SgExpression
                       *testExp = isSgForStatement( astNode )->get_test_expr(),
                       *incrExp = isSgForStatement( astNode )->get_increment();
                     SgNode *up = exp;
                     while ( up && up != testExp && up != incrExp )
                       up = up->get_parent();
                     ROSE_ASSERT ( up );

                     // function call is in the condition of the for-loop
                     if ( up == testExp )
                       inForTest.push_back( true );
                     // function call is in the increment expression
                     else
                       {
                         inForTest.push_back( false );

                         // for increment expressions we need to be able to reassign the return value
                         // of the function; if the ret value is a reference, we need to generate a
                         // pointer of that type (to be able to reassign it later)
                         if ( isSgReferenceType( expType ) )
                           pointerTypeNeeded = true;
                       }
                   }

                 // for do-while statements:  we need to generate declaration of type pointer to be able to have
                 // non-assigned references when looping and assign them at the end of the body of the loop
                 if ( isSgDoWhileStmt( stm->get_parent() ) && isSgReferenceType( expType ) )
                   pointerTypeNeeded = true;

                 // we have a function call returning a reference and we can't initialize the variable
                 // at the point of declaration; we need to define the variable as a pointer
                 if ( pointerTypeNeeded )
                   {
                     // create 'address of' term for function expression, so we can assign it to the pointer
                     SgAddressOfOp *addressOp = new SgAddressOfOp( assignLoc, newExpAssign, expType );

                     // create noninitialized declaration
                     SgType *base = isSgReferenceType( expType )->get_base_type();
                     ROSE_ASSERT( base );
                     SgPointerType *ptrType = SgPointerType::createType( isSgReferenceType( expType )->get_base_type() );
                     ROSE_ASSERT ( ptrType );
                     nonInitVarDeclaration = new SgVariableDeclaration ( nonInitLoc, name, ptrType );

                     // create assignment (symbol, varRefExp, assignment)
                     initName = isSgVariableDeclaration( nonInitVarDeclaration )->get_decl_item( name );
                     ROSE_ASSERT ( initName );

                     varSymbol = new SgVariableSymbol( initName );
                     ROSE_ASSERT ( varSymbol );
                     varRefExp = new SgVarRefExp( assignLoc, varSymbol );

                     SgPointerDerefExp *ptrDeref= new SgPointerDerefExp( assignLoc, varRefExp, expType );
                     ROSE_ASSERT ( isSgExpression( varRefExp ) && ptrDeref );
                     assignOp = new SgAssignOp( assignLoc, varRefExp, addressOp, ptrType );
                     assignStmt = new SgExprStatement( assignLoc, assignOp );
                     ROSE_ASSERT ( assignStmt &&  nonInitVarDeclaration );
           
                     // we don't need initialized declarations in this case
                     initVarDeclaration = NULL;

                     // save new mapping
                     fct2Var.insert( Fct2Var( exp, ptrDeref ) );
                   }
                 else
                   {
                     // create (non- &)initialized declarations, initialized name & symbol
                     SgAssignInitializer *declInit = new SgAssignInitializer( initLoc, newExpInit, expType );
                     ROSE_ASSERT ( declInit );
                     initVarDeclaration = new SgVariableDeclaration ( initLoc, name, expType, declInit );
                     nonInitVarDeclaration = new SgVariableDeclaration ( nonInitLoc, name, expType );
                     ROSE_ASSERT ( initVarDeclaration && nonInitVarDeclaration );

                     initName = isSgVariableDeclaration( nonInitVarDeclaration )->get_decl_item( name );
                     ROSE_ASSERT ( initName );
                     newExpInit->set_parent( initName );
                     varSymbol = new SgVariableSymbol( initName );
                     ROSE_ASSERT ( varSymbol );

                     // create variable ref exp
                     varRefExp = new SgVarRefExp( assignLoc, varSymbol );
                     ROSE_ASSERT ( isSgVarRefExp( varRefExp ) );

                     // create the assignment
                     assignOp = new SgAssignOp( assignLoc, varRefExp, newExpAssign, expType );
                     assignStmt = new SgExprStatement( assignLoc, assignOp );
                     ROSE_ASSERT ( assignStmt );

                     initVarDeclaration->set_parent( stm->get_parent() );
                     isSgVariableDeclaration( initVarDeclaration )->set_definingDeclaration( isSgDeclarationStatement( initVarDeclaration ) );

                     // save new mapping
                     fct2Var.insert( Fct2Var( exp, varRefExp ) );
                   }

                 // save the 'declaration' structure, with all 3 statements and the variable name
                 newDecl->nonInitVarDeclaration = nonInitVarDeclaration;
                 newDecl->initVarDeclaration = initVarDeclaration;
                 newDecl->assignment = assignStmt;
                 newDecl->name = name;
                 nonInitVarDeclaration->set_parent( stm->get_parent() );
                 isSgVariableDeclaration( nonInitVarDeclaration )->set_definingDeclaration( isSgVariableDeclaration( nonInitVarDeclaration ) );
                 assignStmt->set_parent( stm->get_parent() );
                 declarations.push_back( newDecl );
               } // end for
           } // end if  fct calls in crt stmt > 1

         SgScopeStatement *scope = stm->get_scope();
         ROSE_ASSERT ( scope );
         
         // insert function bindings to variables; each 'declaration' structure in the list
         // corresponds to one function call
         for ( DeclarationPtrList::iterator i = declarations.begin(); i != declarations.end(); i++ )
           {
             Declaration *d = *i;
             ROSE_ASSERT ( d && d->assignment && d->nonInitVarDeclaration );

             // if the current statement is a for-loop, we insert Declarations before & in the loop body, depending on the case
             if ( forStm )
               {
                 SgStatement *parentScope = isSgStatement( stm->get_scope() );
                 SgBasicBlock *body = SageInterface::ensureBasicBlockAsBodyOfFor(forStm);
                 ROSE_ASSERT ( !inForTest.empty() && body && parentScope );
                 // SgStatementPtrList &list = body->get_statements();

                 // if function call is in loop condition, we add initialized variable before the loop and at its end
                 // hoist initialized variable declarations outside the loop
                 if ( inForTest.front() )
                   {
                     ROSE_ASSERT ( d->initVarDeclaration );
                     parentScope->insert_statement( stm, d->initVarDeclaration );

                     // set the scope of the initializedName
                     SgInitializedName *initName = isSgVariableDeclaration( d->initVarDeclaration )->get_decl_item( d->name );
                     ROSE_ASSERT ( initName );
                     initName->set_scope( isSgScopeStatement( parentScope ) );
                     ROSE_ASSERT ( initName->get_scope() );
                   }
                 // function call is in loop post increment so add noninitialized variable decls above the loop
                 else
                   {
                     parentScope->insert_statement( stm, d->nonInitVarDeclaration );

                     // set the scope of the initializedName
                     SgInitializedName *initName = isSgVariableDeclaration( d->nonInitVarDeclaration )->get_decl_item( d->name );
                     ROSE_ASSERT ( initName );
                     initName->set_scope( isSgScopeStatement( parentScope ) );
                     ROSE_ASSERT ( initName->get_scope() );
                   }

                 // in a for-loop, always insert assignments at the end of the loop
                 body->get_statements().push_back( d->assignment );
                 d->assignment->set_parent( body );

                 // remove marker
                 inForTest.pop_front();
               }
             else
               {
                 // look at the type of the enclosing scope
                 switch ( scope->variantT() )
                   {

                     // while stmts have to repeat the function calls at the end of the loop;
                     // note there is no "break" statement, since we want to also add initialized
                     // declarations before the while-loop
                   case V_SgWhileStmt:
                     {
                       // assignments need to be inserted at the end of each while loop
                       SgBasicBlock *body = SageInterface::ensureBasicBlockAsBodyOfWhile(isSgWhileStmt( scope ) );
                       ROSE_ASSERT ( body );
                       d->assignment->set_parent( body );
                       body->get_statements().push_back( d->assignment );
                     }

                     // SgForInitStatement has scope SgForStatement, move declarations before the for loop;
                     // same thing if the enclosing scope is an If, or Switch statement
                   case V_SgForStatement:
                   case V_SgIfStmt:
                   case V_SgSwitchStatement:
                     {
                       // adding bindings (initialized variable declarations only, not assignments)
                       // outside the statement, in the parent scope
                       SgStatement *parentScope = isSgStatement( scope->get_parent() );
                       ROSE_ASSERT ( parentScope );
                       parentScope->insert_statement( scope, d->initVarDeclaration, true );\

                       // setting the scope of the initializedName
                       SgInitializedName *initName = isSgVariableDeclaration( d->initVarDeclaration )->get_decl_item( d->name );
                       ROSE_ASSERT ( initName );
                       initName->set_scope( scope->get_scope() );
                       ROSE_ASSERT ( initName->get_scope() );
                     }
                     break;

                     // do-while needs noninitialized declarations before the loop, with assignments inside the loop
                   case V_SgDoWhileStmt:
                     {
                       // adding noninitialized variable declarations before the body of the loop
                       SgStatement *parentScope = isSgStatement( scope->get_parent() );
                       ROSE_ASSERT ( parentScope );
                       parentScope->insert_statement( scope, d->nonInitVarDeclaration, true );

                       // initialized name scope setting
                       SgInitializedName *initName = isSgVariableDeclaration( d->nonInitVarDeclaration )->get_decl_item( d->name );
                       ROSE_ASSERT ( initName );
                       initName->set_scope( scope->get_scope() );
                       ROSE_ASSERT ( initName->get_scope() );

                       // adding assignemts at the end of the do-while loop
                       SgBasicBlock *body = SageInterface::ensureBasicBlockAsBodyOfDoWhile( isSgDoWhileStmt(scope) );
                       ROSE_ASSERT ( body );
                       body->get_statements().push_back( d->assignment );
                       d->assignment->set_parent(body);
                     }
                     break;

                     // for all other scopes, add bindings ( initialized declarations ) before the statement, in the same scope
                   default:
                     scope->insert_statement( stm, d->initVarDeclaration, true );

                     // initialized name scope setting
                     SgInitializedName *initName = isSgVariableDeclaration( d->initVarDeclaration )->get_decl_item( d->name );
                     ROSE_ASSERT ( initName );
                     initName->set_scope( scope->get_scope() );
                     ROSE_ASSERT ( initName->get_scope() );
                   }
               }
           }
         
         // once we have inserted all variable declarations, we need to replace top-level calls in the original statement
         if ( variablesDefined )
           {
             cout << "\tReplacing in the expression " << stm->unparseToString() << "\n";

             // for ForStatements, replace expressions in condition and increment expressions,
             // not in the body, since those get replace later
             if ( forStm )
               {
         // SgExpressionRoot *testExp = forStm->get_test_expr_root(), *incrExp = forStm->get_increment_expr_root();
            SgExpression *testExp = forStm->get_test_expr(), *incrExp = forStm->get_increment();
            replaceFunctionCallsInExpression( incrExp, fct2Var );
            replaceFunctionCallsInExpression( testExp, fct2Var );
               }
             else
               if ( swStm )
             {
            // DQ (11/23/2005): Fixed SgSwitch to permit use of declaration for conditional
            // replaceFunctionCallsInExpression( swStm->get_item_selector_root(), fct2Var );
               replaceFunctionCallsInExpression( swStm->get_item_selector(), fct2Var );
             }
               else
             replaceFunctionCallsInExpression( stm, fct2Var );
           }
       } // end if isSgStatement block
   }
in_list
objectsAllocated(SgStatement *statement, StopCond stopCond)
   {

     in_list objs;
     SgStatement *parent;
     do
        {
#if 0
          in_list::iterator startOfCurrent = objs.end();
#endif
          parent = isSgStatement(statement->get_parent());
          //cout << "parent = " << parent << endl;
          ROSE_ASSERT(parent);
          in_list blockObjs = blockObjectsAllocated(parent, statement);
          objs.insert(objs.end(), blockObjs.begin(), blockObjs.end());
#if 0
          switch (parent->variantT())
             {
               case V_SgBasicBlock:
                     {
                       SgBasicBlock *b = isSgBasicBlock(parent);
                       ROSE_ASSERT(b);
                       SgStatementPtrList &stmts = b->get_statements();
                       for (SgStatementPtrList::iterator i = stmts.begin(); i != stmts.end(); ++i)
                          {
                            if (*i == statement) break;
                            addStmtVarsIfAny(objs, startOfCurrent, *i);
                          }
                     }
                  break;
               case V_SgForStatement:
                     {
                       SgForStatement *fs = isSgForStatement(parent);
                       SgStatementPtrList &stmts = fs->get_init_stmt();
                       bool done = false;
                       if (!omitInit)
                          {
                            for (SgStatementPtrList::iterator i = stmts.begin(); i != stmts.end(); ++i)
                               {
                                 if (*i == statement)
                                    {
                                      done = true;
                                      break;
                                    }
                                 addStmtVarsIfAny(objs, startOfCurrent, *i);
                               }
                          }
                       if (!done)
                          {
                            if (fs->get_test() != statement)
                               {
                                 addStmtVarsIfAny(objs, startOfCurrent, fs->get_test());
                               }
                          }
                     }
                  break;
               default:
                  break;
             }
#endif
          statement = parent;
        } while (!stopCond(parent));
     return objs;
   }
in_list
blockObjectsAllocated(SgStatement *block, SgStatement *stop)
   {
     in_list objs;
     ROSE_ASSERT(block);
     switch (block->variantT())
        {
          case V_SgBasicBlock:
                {
                  SgBasicBlock *b = isSgBasicBlock(block);
                  ROSE_ASSERT(b);
                  
                  SgNode *parent = block->get_parent();
                  if (SgForStatement *fs = isSgForStatement(parent))
                     {
                        addStmtVarsIfAny(objs, fs->get_test());
                     }
                  else if (SgWhileStmt *ws = isSgWhileStmt(parent))
                     {
                       addStmtVarsIfAny(objs, ws->get_condition());
                     }
                  else if (SgIfStmt *is = isSgIfStmt(parent))
                     {
                       addStmtVarsIfAny(objs, is->get_conditional());

                       if (IsSCGenerated(is))
                          {
                            FindTemporaries ft(objs);
                            ft.traverse(is->get_conditional(), preorder);

                            if (b->get_statements().size() > 1)
                               {
                              // the first statement is the left conjunct of a ,
                              // expression... grab temporaries from this as well
                                 ft.traverse(b->get_statements().front(), preorder);
                               }
                          }
                     }

                  SgStatementPtrList &stmts = b->get_statements();
                  for (SgStatementPtrList::iterator i = stmts.begin(); i != stmts.end(); ++i)
                     {
                       if (*i == stop) break;
                       addStmtVarsIfAny(objs, *i);
                     }
                }
             break;
          case V_SgForStatement:
                {
                  SgForStatement *fs = isSgForStatement(block);
                  SgStatementPtrList &stmts = fs->get_init_stmt();
                  for (SgStatementPtrList::iterator i = stmts.begin(); i != stmts.end(); ++i)
                     {
                       if (*i == stop)
                          {
                            break;
                          }
                       addStmtVarsIfAny(objs, *i);
                     }
               // addStmtVarsIfAny(objs, fs->get_test());
                }
             break;
          default:
             break;
        }
     return objs;
   }
// Move variables declared in a for statement to just outside that statement.
void moveForDeclaredVariables(SgNode* root)
   {
     vector<SgForStatement*> for_statements;
     FindForStatementsVisitor(for_statements).traverse(root, preorder);

     for (unsigned int i = 0; i < for_statements.size(); ++i)
        {
          SgForStatement* stmt = for_statements[i];
#ifdef FD_DEBUG
          cout << "moveForDeclaredVariables: " << stmt->unparseToString() << endl;
#endif
          SgForInitStatement* init = stmt->get_for_init_stmt();
          if (!init) continue;
          SgStatementPtrList& inits = init->get_init_stmt();
          vector<SgVariableDeclaration*> decls;
          for (SgStatementPtrList::iterator j = inits.begin(); j != inits.end(); ++j) {
            SgStatement* one_init = *j;
            if (isSgVariableDeclaration(one_init))
            {
              decls.push_back(isSgVariableDeclaration(one_init));
            }
          }
          if (decls.empty()) continue;
          SgStatement* parent = isSgStatement(stmt->get_parent());
          assert (parent);
          SgBasicBlock* bb = new SgBasicBlock(SgNULL_FILE);
          stmt->set_parent(bb);
          bb->set_parent(parent);
          SgStatementPtrList ls;
          for (unsigned int j = 0; j < decls.size(); ++j)
             {
               for (SgInitializedNamePtrList::iterator k = decls[j]->get_variables().begin(); k != decls[j]->get_variables().end(); ++k)
                  {
#ifdef FD_DEBUG
                    cout << "Working on variable " << (*k)->get_name().getString() << endl;
#endif
                    SgVariableSymbol* sym = new SgVariableSymbol(*k);
                    bb->insert_symbol((*k)->get_name(), sym);
                    (*k)->set_scope(bb);
                    SgAssignInitializer* kinit = 0;
                    if (isSgAssignInitializer((*k)->get_initializer()))
                       {
                         kinit = isSgAssignInitializer((*k)->get_initializer());
                         (*k)->set_initializer(0);
                       }

                    if (kinit)
                       {
                         SgVarRefExp* vr = new SgVarRefExp(SgNULL_FILE, sym);
                         vr->set_endOfConstruct(SgNULL_FILE);
                         vr->set_lvalue(true);
                         SgAssignOp* assignment = new SgAssignOp(SgNULL_FILE,vr,kinit->get_operand());
                         vr->set_parent(assignment);
                         kinit->get_operand()->set_parent(assignment);
                         SgExprStatement* expr = new SgExprStatement(SgNULL_FILE, assignment);
                         assignment->set_parent(expr);
                         ls.push_back(expr);
                         expr->set_parent(init);
                       }
                  }

#if 0
               SgStatementPtrList::iterator fiiter = std::find(inits.begin(), inits.end(), decls[j]);
               assert (fiiter != inits.end());
               size_t idx = fiiter - inits.begin();
               inits.erase(inits.begin() + idx);
               inits.insert(inits.begin() + idx, ls.begin(), ls.end());
#endif
               bb->get_statements().push_back(decls[j]);
               decls[j]->set_parent(bb);
             }
          inits = ls;
          bb->get_statements().push_back(stmt);
       // printf ("In moveForDeclaredVariables(): parent = %p = %s bb = %p stmt = %p = %s \n",parent,parent->class_name().c_str(),bb,stmt,stmt->class_name().c_str());
          ROSE_ASSERT(stmt->get_parent() == bb);
          parent->replace_statement(stmt, bb);
        }
   }
Example #27
0
bool ClangToSageTranslator::VisitFunctionDecl(clang::FunctionDecl * function_decl, SgNode ** node) {
#if DEBUG_VISIT_DECL
    std::cerr << "ClangToSageTranslator::VisitFunctionDecl" << std::endl;
#endif
    bool res = true;

    // FIXME: There is something weird here when try to Traverse a function reference in a recursive function (when first Traverse is not complete)
    //        It seems that it tries to instantiate the decl inside the function...
    //        It may be faster to recode from scratch...
    //   If I am not wrong this have been fixed....

    SgName name(function_decl->getNameAsString());

    SgType * ret_type = buildTypeFromQualifiedType(function_decl->getResultType());

    SgFunctionParameterList * param_list = SageBuilder::buildFunctionParameterList_nfi();
      applySourceRange(param_list, function_decl->getSourceRange()); // FIXME find the good SourceRange (should be stored by Clang...)

    for (unsigned i = 0; i < function_decl->getNumParams(); i++) {
        SgNode * tmp_init_name = Traverse(function_decl->getParamDecl(i));
        SgInitializedName * init_name = isSgInitializedName(tmp_init_name);
        if (tmp_init_name != NULL && init_name == NULL) {
            std::cerr << "Runtime error: tmp_init_name != NULL && init_name == NULL" << std::endl;
            res = false;
            continue;
        }

        param_list->append_arg(init_name);
    }

    if (function_decl->isVariadic()) {
        SgName empty = "";
        SgType * ellipses_type = SgTypeEllipse::createType();
        param_list->append_arg(SageBuilder::buildInitializedName_nfi(empty, ellipses_type, NULL));
    }

    SgFunctionDeclaration * sg_function_decl;

    if (function_decl->isThisDeclarationADefinition()) {
        sg_function_decl = SageBuilder::buildDefiningFunctionDeclaration(name, ret_type, param_list, NULL);
        sg_function_decl->set_definingDeclaration(sg_function_decl);

        if (function_decl->isVariadic()) {
            sg_function_decl->hasEllipses();
        }

        if (!function_decl->hasBody()) {
            std::cerr << "Defining function declaration without body..." << std::endl;
            res = false;
        }
/*
        if (sg_function_decl->get_definition() != NULL) SageInterface::deleteAST(sg_function_decl->get_definition());

        SgFunctionDefinition * function_definition = new SgFunctionDefinition(sg_function_decl, NULL);

        SgInitializedNamePtrList & init_names = param_list->get_args();
        SgInitializedNamePtrList::iterator it;
        for (it = init_names.begin(); it != init_names.end(); it++) {
            (*it)->set_scope(function_definition);
            SgSymbolTable * st = function_definition->get_symbol_table();
            ROSE_ASSERT(st != NULL);
            SgVariableSymbol * tmp_sym  = new SgVariableSymbol(*it);
            st->insert((*it)->get_name(), tmp_sym);
        }
*/
        SgFunctionDefinition * function_definition = sg_function_decl->get_definition();

        if (sg_function_decl->get_definition()->get_body() != NULL)
            SageInterface::deleteAST(sg_function_decl->get_definition()->get_body());

        SageBuilder::pushScopeStack(function_definition);

        SgNode * tmp_body = Traverse(function_decl->getBody());
        SgBasicBlock * body = isSgBasicBlock(tmp_body);

        SageBuilder::popScopeStack();

        if (tmp_body != NULL && body == NULL) {
            std::cerr << "Runtime error: tmp_body != NULL && body == NULL" << std::endl;
            res = false;
        }
        else {
            function_definition->set_body(body);
            body->set_parent(function_definition);
            applySourceRange(function_definition, function_decl->getSourceRange());
        }

        sg_function_decl->set_definition(function_definition);
        function_definition->set_parent(sg_function_decl);

        SgFunctionDeclaration * first_decl;
        if (function_decl->getFirstDeclaration() == function_decl) {
            SgFunctionParameterList * param_list_ = SageBuilder::buildFunctionParameterList_nfi();
              setCompilerGeneratedFileInfo(param_list_);
            SgInitializedNamePtrList & init_names = param_list->get_args();
            SgInitializedNamePtrList::iterator it;
            for (it = init_names.begin(); it != init_names.end(); it++) {
                SgInitializedName * init_param = new SgInitializedName(**it);
                setCompilerGeneratedFileInfo(init_param);
                param_list_->append_arg(init_param);
            }

            first_decl = SageBuilder::buildNondefiningFunctionDeclaration(name, ret_type, param_list_, NULL);
            setCompilerGeneratedFileInfo(first_decl);
            first_decl->set_parent(SageBuilder::topScopeStack());
            first_decl->set_firstNondefiningDeclaration(first_decl);
            if (function_decl->isVariadic()) first_decl->hasEllipses();
        }
        else {
            SgSymbol * tmp_symbol = GetSymbolFromSymbolTable(function_decl->getFirstDeclaration());
            SgFunctionSymbol * symbol = isSgFunctionSymbol(tmp_symbol);
            if (tmp_symbol != NULL && symbol == NULL) {
                std::cerr << "Runtime error: tmp_symbol != NULL && symbol == NULL" << std::endl;
                res = false;
            }
            if (symbol != NULL)
                first_decl = isSgFunctionDeclaration(symbol->get_declaration());
        }

        sg_function_decl->set_firstNondefiningDeclaration(first_decl);
        first_decl->set_definingDeclaration(sg_function_decl);
    }
    else {
        sg_function_decl = SageBuilder::buildNondefiningFunctionDeclaration(name, ret_type, param_list, NULL);

        if (function_decl->isVariadic()) sg_function_decl->hasEllipses();

        SgInitializedNamePtrList & init_names = param_list->get_args();
        SgInitializedNamePtrList::iterator it;
        for (it = init_names.begin(); it != init_names.end(); it++) {
             (*it)->set_scope(SageBuilder::topScopeStack());
        }

        if (function_decl->getFirstDeclaration() != function_decl) {
            SgSymbol * tmp_symbol = GetSymbolFromSymbolTable(function_decl->getFirstDeclaration());
            SgFunctionSymbol * symbol = isSgFunctionSymbol(tmp_symbol);
            if (tmp_symbol != NULL && symbol == NULL) {
                std::cerr << "Runtime error: tmp_symbol != NULL && symbol == NULL" << std::endl;
                res = false;
            }
            SgFunctionDeclaration * first_decl = NULL;
            if (symbol != NULL) {
                first_decl = isSgFunctionDeclaration(symbol->get_declaration());
            }
            else {
                // FIXME Is it correct?
                SgNode * tmp_first_decl = Traverse(function_decl->getFirstDeclaration());
                first_decl = isSgFunctionDeclaration(tmp_first_decl);
                ROSE_ASSERT(first_decl != NULL);
                // ROSE_ASSERT(!"We should have see the first declaration already");
            }

            if (first_decl != NULL) {
                if (first_decl->get_firstNondefiningDeclaration() != NULL)
                    sg_function_decl->set_firstNondefiningDeclaration(first_decl->get_firstNondefiningDeclaration());
                else {
                    ROSE_ASSERT(first_decl->get_firstNondefiningDeclaration() != NULL);
                }
            }
            else {
                ROSE_ASSERT(!"First declaration not found!");
            }
        }
        else {
            sg_function_decl->set_firstNondefiningDeclaration(sg_function_decl);
        }
    }

    ROSE_ASSERT(sg_function_decl->get_firstNondefiningDeclaration() != NULL);
/* // TODO Fix problem with function symbols...
    SgSymbol * symbol = GetSymbolFromSymbolTable(function_decl);
    if (symbol == NULL) {
        SgFunctionSymbol * func_sym = new SgFunctionSymbol(isSgFunctionDeclaration(sg_function_decl->get_firstNondefiningDeclaration()));
        SageBuilder::topScopeStack()->insert_symbol(name, func_sym);        
    }
*/
//  ROSE_ASSERT(GetSymbolFromSymbolTable(function_decl) != NULL);

    *node = sg_function_decl;

    return VisitDeclaratorDecl(function_decl, node) && res;
}
Example #28
0
  void instr(SgGlobal* global) {

    // Create the lock and key variables in global scope.
    // In main:
    // EnterScope();
    // lock = getTopLock();
    // key = getTopKey();
    // .... rest of main
    // ExitScope();
    // return;
    // FIXME: Add case where we handle arbitrary exits from main
    // This can be handled similar to the way returns are handled
    // for basic blocks.

    SgScopeStatement* scope = isSgScopeStatement(global);

    // Insert lock and key variables at the top of the global scope
    // lock variable
    std::cout << "VarCounter: " << Util::VarCounter << std::endl;
    SgName lock_name("lock_var" + boost::lexical_cast<std::string>(Util::VarCounter));
    SgVariableDeclaration* lock_var = Util::createLocalVariable(lock_name, getLockType(), NULL, scope);
    // Add declaration at the top of the scope
    scope->prepend_statement(lock_var);

    // key variable
    // **** IMPORTANT: Using same counter value for lock and key
    SgName key_name("key_var" + boost::lexical_cast<std::string>(Util::VarCounter));
    Util::VarCounter++;
    SgVariableDeclaration* key_var = Util::createLocalVariable(key_name, getKeyType(), NULL, scope);
    // Insert this key decl after the lock decl
    SI::insertStatementAfter(lock_var, key_var);


    // Now, find the main function and insert...
    // EnterScope();
    // lock = getTopLock();
    // key = getTopKey();
    // .... rest of main
    // ExitScope()
    // return; -- this already exists...
    // see FIXME above

    // find main function...
    SgFunctionDeclaration* MainFn = SI::findMain(global);
    if(!MainFn) {
#ifdef HANDLE_GLOBAL_SCOPE_DEBUG
      printf("Can't find Main function. Not inserting Global Enter and Exit Scopes\n");
#endif
      return;
    }

    SgBasicBlock *bb = Util::getBBForFn(MainFn);

    // insert EnterScope()
#if 0
    SgExpression* overload = buildOverloadFn("EnterScope", NULL, NULL, SgTypeVoid::createType(), scope,
        GEFD(bb));
#endif
    SgExpression* overload = buildMultArgOverloadFn("EnterScope", SB::buildExprListExp(), SgTypeVoid::createType(), scope,
        GEFD(bb));

    SgStatement* enter_scope = SB::buildExprStatement(overload);
    Util::insertAtTopOfBB(bb, enter_scope);

    // insert lock = getTopLock();
    //overload = buildOverloadFn("getTopLock", NULL, NULL, getLockType(), scope, GEFD(bb));
    overload = buildMultArgOverloadFn("getTopLock", SB::buildExprListExp(), getLockType(), scope, GEFD(bb));
    //SgStatement* lock_assign = SB::buildExprStatement(SB::buildAssignOp(SB::buildVarRefExp(lock_var), overload));
    //SI::insertStatementAfter(enter_scope, lock_assign); //RMM COMMENTED OUT

    // insert key = getTopKey();
    // overload = buildOverloadFn("getTopKey", NULL, NULL, getKeyType(), scope, GEFD(bb));
    overload = buildMultArgOverloadFn("getTopKey", SB::buildExprListExp(), getKeyType(), scope, GEFD(bb));
    //SgStatement* key_assign = SB::buildExprStatement(SB::buildAssignOp(SB::buildVarRefExp(key_var), overload));
    //SI::insertStatementAfter(lock_assign, key_assign); //RMM COMMENTED OUT

    // add to scope -> lock and key map... SLKM
    LockKeyPair lock_key = std::make_pair(lock_var, key_var);
    scopeLockMap[scope] = lock_key;

    ROSE_ASSERT(existsInSLKM(scope));

    // Insert ExitScope if last stmt is not return.
    SgStatementPtrList& stmts = bb->get_statements();
    SgStatementPtrList::iterator it = stmts.begin();
    it += (stmts.size() - 1);

    // A little iffy on the scope part here... lets check that.
    if(!isSgReturnStmt(*it)) {
      // Last statement is not return. So, add exit scope...
      // If its a break/continue statement, insert statement before,
      // otherwise, add exit_scope afterwards.
      //SgExpression* overload = buildOverloadFn("ExitScope", NULL, NULL, SgTypeVoid::createType(), scope, GEFD(bb));
      SgExpression* overload = buildMultArgOverloadFn("ExitScope", SB::buildExprListExp(), SgTypeVoid::createType(), scope, GEFD(bb));

      // check if its break/continue
      if(isSgBreakStmt(*it) || isSgContinueStmt(*it)) {
        SI::insertStatementBefore(*it, SB::buildExprStatement(overload));
      }
      else {
        SI::insertStatementAfter(*it, SB::buildExprStatement(overload));
      }
    }

  }