示例#1
0
InheritedAttribute
visitorTraversal::evaluateInheritedAttribute(SgNode* n, InheritedAttribute inheritedAttribute)
   {
    Sg_File_Info* s = n->get_startOfConstruct();
    Sg_File_Info* e = n->get_endOfConstruct();
    Sg_File_Info* f = n->get_file_info();
    for(int x=0; x < inheritedAttribute.depth; ++x) {
        printf(" ");
    }
    if(s != NULL && e != NULL && !isSgLabelStatement(n)) { 
        printf ("%s (%d, %d, %d)->(%d, %d): %s",n->sage_class_name(),s->get_file_id()+1,s->get_raw_line(),s->get_raw_col(),e->get_raw_line(),e->get_raw_col(),  verbose ? n->unparseToString().c_str() : "" );
        if(isSgAsmDwarfConstruct(n)) {
            printf(" [DWARF construct name: %s]", isSgAsmDwarfConstruct(n)->get_name().c_str());
        }
        SgExprStatement * exprStmt = isSgExprStatement(n);
        if(exprStmt != NULL) {
            printf(" [expr type: %s]", exprStmt->get_expression()->sage_class_name());           
            SgFunctionCallExp * fcall = isSgFunctionCallExp(exprStmt->get_expression());
            if(fcall != NULL) {
               SgExpression * funcExpr = fcall->get_function();
               if(funcExpr != NULL) {
                    printf(" [function expr: %s]", funcExpr->class_name().c_str());
               }
               SgFunctionDeclaration * fdecl = fcall->getAssociatedFunctionDeclaration();
               if(fdecl != NULL) {
                    printf(" [called function: %s]", fdecl->get_name().str());
               }
            }
        }
        if(isSgFunctionDeclaration(n)) {
            printf(" [declares function: %s]", isSgFunctionDeclaration(n)->get_name().str());
        }
        SgStatement * sgStmt = isSgStatement(n);
        if(sgStmt != NULL) {
            printf(" [scope: %s, %p]", sgStmt->get_scope()->sage_class_name(), sgStmt->get_scope());
        }
        //SgLabelStatement * lblStmt = isSgLabelStatement(n);
        //if(lblStmt != NULL) {
        //    SgStatement * lblStmt2 = lblStmt->get_statement();
        //}
    } else if (f != NULL) {
		SgInitializedName * iname = isSgInitializedName(n);
		if(iname != NULL) {
            SgType* inameType = iname->get_type();
			printf("%s (%d, %d, %d): %s [type: %s", n->sage_class_name(),f->get_file_id()+1,f->get_raw_line(),f->get_raw_col(),n->unparseToString().c_str(),inameType->class_name().c_str());
			SgDeclarationStatement * ds = isSgDeclarationStatement(iname->get_parent());
			if(ds != NULL) {
				if(ds->get_declarationModifier().get_storageModifier().isStatic()) {
					printf(" static");
				}
			}
			
			SgArrayType * art = isSgArrayType(iname->get_type());
			if(art != NULL) {
				printf(" %d", art->get_rank());
			}
			
			printf("]");
            if(isSgAsmDwarfConstruct(n)) {
                printf(" [DWARF construct name: %s]", isSgAsmDwarfConstruct(n)->get_name().c_str());
            }
            } else {
        	printf("%s (%d, %d, %d): %s", n->sage_class_name(),f->get_file_id()+1,f->get_raw_line(),f->get_raw_col(), verbose ? n->unparseToString().c_str() : "");
		}
    } else {
        printf("%s : %s", n->sage_class_name(), verbose ? n->unparseToString().c_str() : "");
        if(isSgAsmDwarfConstruct(n)) {
            printf(" [DWARF construct name: %s]", isSgAsmDwarfConstruct(n)->get_name().c_str());
        }
    }
    printf(" succ# %lu", n->get_numberOfTraversalSuccessors());
	printf("\n");
     return InheritedAttribute(inheritedAttribute.depth+1);
   }
/** Visits AST nodes in pre-order */
FunctionCallInheritedAttribute FunctionEvaluationOrderTraversal::evaluateInheritedAttribute(SgNode* astNode, FunctionCallInheritedAttribute parentAttribute)
{
    FunctionCallInheritedAttribute result = parentAttribute;
    SgForStatement* parentForLoop = isSgForStatement(parentAttribute.currentScope);
    SgWhileStmt* parentWhileLoop = isSgWhileStmt(parentAttribute.currentScope);
    SgDoWhileStmt* parentDoWhileLoop = isSgDoWhileStmt(parentAttribute.currentScope);
    
    SgConditionalExp* parentSgConditionalExp = astNode->get_parent() ? isSgConditionalExp(astNode->get_parent()) : NULL;
    SgAndOp* parentAndOp =  astNode->get_parent() ?isSgAndOp(astNode->get_parent()) : NULL;
    SgOrOp* parentOrOp =  astNode->get_parent() ? isSgOrOp(astNode->get_parent()) : NULL;

    if (isSgForStatement(astNode))
        result.currentScope = isSgForStatement(astNode);
    else if (isSgWhileStmt(astNode))
        result.currentScope = isSgWhileStmt(astNode);
    else if (isSgDoWhileStmt(astNode))
        result.currentScope = isSgDoWhileStmt(astNode);
    //else if (isSgConditionalExp(astNode))
    //    result.currentScope = isSgConditionalExp(astNode);
    //else if (isSgAndOp(astNode))
    //    result.currentScope = isSgAndOp(astNode);
    //else if (isSgOrOp(astNode))
    //    result.currentScope = isSgOrOp(astNode);
    else if (isSgForInitStatement(astNode)) {
        ROSE_ASSERT(result.scopeStatus == FunctionCallInheritedAttribute::IN_SAFE_PLACE);
        result.scopeStatus = FunctionCallInheritedAttribute::INSIDE_FOR_INIT;
        ROSE_ASSERT(isSgForStatement(result.currentScope));
    } else if (parentForLoop != NULL && parentForLoop->get_test() == astNode) {
        ROSE_ASSERT(result.scopeStatus == FunctionCallInheritedAttribute::IN_SAFE_PLACE);
        result.scopeStatus = FunctionCallInheritedAttribute::INSIDE_FOR_TEST;
    } else if (parentForLoop != NULL && parentForLoop->get_increment() == astNode) {
        ROSE_ASSERT(result.scopeStatus == FunctionCallInheritedAttribute::IN_SAFE_PLACE);
        result.scopeStatus = FunctionCallInheritedAttribute::INSIDE_FOR_INCREMENT;
    } else if (parentWhileLoop != NULL && parentWhileLoop->get_condition() == astNode) {
        ROSE_ASSERT(result.scopeStatus == FunctionCallInheritedAttribute::IN_SAFE_PLACE);
        result.scopeStatus = FunctionCallInheritedAttribute::INSIDE_WHILE_CONDITION;
    } else if (parentDoWhileLoop != NULL && parentDoWhileLoop->get_condition() == astNode) {
        ROSE_ASSERT(result.scopeStatus == FunctionCallInheritedAttribute::IN_SAFE_PLACE);
        result.scopeStatus = FunctionCallInheritedAttribute::INSIDE_DO_WHILE_CONDITION;
    } else if( parentSgConditionalExp != NULL && parentSgConditionalExp->get_true_exp() == astNode) {
        // if the scope status was safe, turn it into unsafe
        if (IsStatusSafe(result.scopeStatus))
            result.scopeStatus = FunctionCallInheritedAttribute::INSIDE_CONDITIONAL_EXP_TRUE_ARM;
    } else if(parentSgConditionalExp != NULL && parentSgConditionalExp->get_false_exp() == astNode) {
        // if the scope status was safe, turn it into unsafe
        if (IsStatusSafe(result.scopeStatus))
            result.scopeStatus = FunctionCallInheritedAttribute::INSIDE_CONDITIONAL_EXP_FALSE_ARM;
    } else if( parentOrOp != NULL && parentOrOp->get_rhs_operand () == astNode) {
        // if the scope status was safe, turn it into unsafe
        if (IsStatusSafe(result.scopeStatus))
            result.scopeStatus = FunctionCallInheritedAttribute::INSIDE_SHORT_CIRCUIT_EXP_RHS;
    } else if( parentAndOp != NULL && parentAndOp->get_rhs_operand () == astNode)  {
        // if the scope status was safe, turn it into unsafe
        if (IsStatusSafe(result.scopeStatus))
            result.scopeStatus = FunctionCallInheritedAttribute::INSIDE_SHORT_CIRCUIT_EXP_RHS;
    }
    
    //We can't insert variables before an expression statement that appears inside if(), switch, throw, etc.
    if (isSgExprStatement(astNode) && !isSgBasicBlock(astNode->get_parent())) {
        //We can't insert a variable declaration at these locations. Use the parent statement
    } else if (isSgStatement(astNode)) {
        result.lastStatement = isSgStatement(astNode);
    }

    return result;
}
// ScalarIndexingStatementQuerySynthesizedAttributeType
SynthesizedAttributeBaseClassType
ScalarIndexingStatementTransformation::transformation
   ( const ArrayStatementQueryInheritedAttributeType & X, SgNode* astNode )
   {
  // This function returns the string representing the array statement transformation
     ROSE_ASSERT (isSgExprStatement(astNode) != NULL);

     ScalarIndexingStatementQueryInheritedAttributeType scalarIndexingStatementQueryInheritedData(X,astNode);

  // Declare the list to place transformation options into from each scope as we traverse backward
  // (up) the AST to the Global scope.
  // list<int> transformationOptions; // inherited attribute
     list<int> & transformationOptions = scalarIndexingStatementQueryInheritedData.getTransformationOptions();

     printf ("ScalarIndexingStatementQueryInheritedData.arrayStatementDimension = %d \n",
          scalarIndexingStatementQueryInheritedData.arrayStatementDimension);

  // We have to use a special function here instead of the Query mechanism since the Query mechanism
  // searches down through he AST (parents traverse their children) and we want to traverse up
  // through the AST (and not the whole AST) from the child to the parent stopping at the root of
  // the AST (the global scope: SgGlobal)
     printf ("############### CALLING TRANSFORMATION OPTION QUERY FROM ASSIGNMENT TRANSFORMATION ############ \n");

  // Find all the hints specified as enum values constructor parameters for declarations of
  // variables of type "TransformationAssertion".  The current scope and all parent scopes are
  // searched back to the global scope. (Maybe this should return a value rather than modify a
  // reference parameter???)
     TransformationSupport::getTransformationOptions ( astNode, transformationOptions, "TransformationAssertion");

#if 1
  // List the different transformation options that are specified in the user's code
  // list<TransformationAssertion::TransformationOption>::iterator transformationOptionListIterator;
     list<int>::iterator transformationOptionListIterator;
     for (transformationOptionListIterator  = transformationOptions.begin();
          transformationOptionListIterator != transformationOptions.end();
          transformationOptionListIterator++)
        {
       // display each value
       // TransformationAssertion::TransformationOption i = *transformationOptionListIterator;
          int i = *transformationOptionListIterator;
          printf ("     Value in transformation option = %d name = %s \n",i,TransformationAssertion::getOptionString(i));
        }
#endif

  // If there are any index object in use then we can't do the transformation (so make sure there are none)
     ROSE_ASSERT (NameQuery::getVariableNamesWithTypeNameQuery (astNode,"Internal_Index").size() == 0);

     printf ("######################### END OF INTERNALINDEX NAME QUERY ######################## \n");

     printf ("################# START OF ARRAY STATEMENT DIMENSION QUERY ################ \n");

  // The dimension of the array statement must be computed on the way down (in the traversal of the AST).
  // This query gets the list of integer associated with the dimension of each array operand (array
  // operands using the doubleArray::operator() member function).
     list<int> operandDimensionList =
          NumberQuery::getNumberOfArgumentsToParenthesisOperatorQuery (astNode, "doubleArray" );
     ROSE_ASSERT (operandDimensionList.size() >= 0);

#if 1
     printf ("operandDimensionList.size() = %d \n",operandDimensionList.size());
     printf ("operandDimensionList = \n%s\n",StringUtility::listToString(operandDimensionList).c_str());
#endif

  // Now use STL to build a list of unique names
     operandDimensionList.unique();
#if 0
     printf ("Unique Names: operandDimensionList = \n%s\n",StringUtility::listToString(operandDimensionList).c_str());
#endif

  // If there is no dimension computed for the query then it means that there were no operator()
  // used in which case we have to assume 6D array operations
     int dimensionOfArrayStatement = (operandDimensionList.size() == 0) ? 6 : *(operandDimensionList.begin());
     printf ("Array statement is %d dimensional \n",dimensionOfArrayStatement);

  // Make sure that it has the default value before we change it (error checking)
     printf ("ScalarIndexingStatementQueryInheritedData.arrayStatementDimension = %d \n",
          scalarIndexingStatementQueryInheritedData.arrayStatementDimension);
     ROSE_ASSERT (scalarIndexingStatementQueryInheritedData.arrayStatementDimension == -1);

  // Modify the inherited attribute using the array statement dimension data
     scalarIndexingStatementQueryInheritedData.arrayStatementDimensionDefined = TRUE;
     scalarIndexingStatementQueryInheritedData.arrayStatementDimension        = dimensionOfArrayStatement;

#if 0
     printf ("ScalarIndexingStatementQueryInheritedData.arrayStatementDimension = %d \n",
          scalarIndexingStatementQueryInheritedData.arrayStatementDimension);
     printf ("Exiting as part of testing (after search for array statement dimension) ... \n");
     ROSE_ABORT();
#endif

     ScalarIndexingStatementTransformation transformation;

  // Build a return value for this function
     ScalarIndexingStatementQuerySynthesizedAttributeType returnScalarIndexingStatementTransformation(astNode);

  // We need these until we have a new interface which will allow us to skip them (they are not used)
  // ScalarIndexingStatementQueryAccumulatorType        accumulatorValue;

  // WARNING: It is a design problem that we have the dimension set in two locations

  // To to implement this transformation withouth a database mechanism
  // Set the dimension of the array statement in the array operand database
     transformation.accumulatorValue.operandDataBase.setDimension(dimensionOfArrayStatement);
     ROSE_ASSERT (transformation.accumulatorValue.operandDataBase.getDimension() > 0);

  // Setup the data base with the options specified by the use and extracted from the current scope
  // of the application code.
     transformation.accumulatorValue.operandDataBase.setUserOptimizationAssertions
          (scalarIndexingStatementQueryInheritedData.transformationOptions);
     ROSE_ASSERT ( transformation.accumulatorValue.operandDataBase.transformationOption > 
                   ArrayTransformationSupport::UnknownIndexingAccess );

  // Make sure the data base has been setup properly
     ROSE_ASSERT ( transformation.accumulatorValue.operandDataBase.transformationOption >
          ArrayTransformationSupport::UnknownIndexingAccess );
     ROSE_ASSERT ( transformation.accumulatorValue.operandDataBase.dimension > -1 );

  // We must call this preorder because we modify the inherited attribute on the way down into the
  // AST so that the assembly will have the correct array statment dimension on the way back up (the
  // assembly of attributes always happens postorder).
     returnScalarIndexingStatementTransformation =
          transformation.traverse ( astNode, scalarIndexingStatementQueryInheritedData );

  // printf ("accumulatorValue.operandDataBase.displayString() = %s \n",accumulatorValue.operandDataBase.displayString().c_str());
  // ROSE_ASSERT (accumulatorValue.operandDataBase.size() > 0);

  // Copy the operand data base out of the accumulator attribute and into the ScalarIndexingStatementTransformation object
  // operandDataBase = transformation.accumulatorValue.operandDataBase;

  // printf ("transformation.accumulatorValue.operandDataBase.displayString() = %s \n",
  //      transformation.accumulatorValue.operandDataBase.displayString().c_str());

#if 0
  // Verify that we have saved the global and variable declarations and variable initializations
     ROSE_ASSERT (returnScalarIndexingStatementTransformation.globalDeclarationStrings.isEmpty()      == false);
     ROSE_ASSERT (returnScalarIndexingStatementTransformation.variableDeclarationStrings.isEmpty()    == false);
     ROSE_ASSERT (returnScalarIndexingStatementTransformation.variableInitializationStrings.isEmpty() == false);
#endif

  // Verify that we have a valid string
     ROSE_ASSERT (returnScalarIndexingStatementTransformation.isEmpty() == false);

     printf ("######################### END OF SCALAR INDEXING TRANSFORMATION QUERY ######################## \n");

     printf ("returnScalarIndexingStatementTransformation.getTransformationSourceCode().c_str() = \n%s \n",
          returnScalarIndexingStatementTransformation.getWorkSpace().c_str());

#if 0
  // NOTE: I think we need to get the variable declarations out of returnArrayStatementTransformation
     list<string> stringList = returnArrayStatementTransformation.getVariableDeclarationStrings();
     ROSE_ASSERT (stringList.size() > 0);
     for (list<string>::iterator i = stringList.begin(); i != stringList.end(); i++)
          printf ("Base of ScalarIndexingStatementTransformation::transformation(): getVariableDeclarationStrings() = %s \n",(*i).c_str());
#endif

#if 0
     printf ("Exiting as part of testing (after SCALAR INDEXING TRANSFORMATION QUERY) ... \n");
     ROSE_ABORT();
#endif

     return returnScalarIndexingStatementTransformation;
   }
示例#4
0
文件: smtQueryLib.cpp 项目: 8l/rose
std::string getSgStatement(SgStatement* stat) {
	VariantT var = stat->variantT();
	std::string statStr = "";
	if (isSgDeclarationStatement(stat)) {
		getSgDeclarationStatement(isSgDeclarationStatement(stat));
	}
	else if (isSgScopeStatement(stat)) {
		getSgScopeStatement(isSgScopeStatement(stat));
	}
	else if (isSgIOStatement(stat)) {
		statStr = getSgIOStatement(isSgIOStatement(stat));
	}
	else {
	switch (var) {
		case V_SgAllocateStatement:
			{
			std::cout << "SgAllocateStatement is not yet implemented" << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgArithmeticIfStatement:
			{
			std::cout << "SgArithmeticIfStatement is not yet implemented" << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgAssertStmt:
			{
			std::cout << "SgAssertStmt is not yet implemented" << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgAssignedGotoStatement:
			{
			std::cout << "SgAssignedGotoStatement is not yet implemented" << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgAssignStatement:
			{
			std::cout << "SgAssignStatement is not yet implemented" << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgBreakStmt:
			{
			std::cout << "SgBreakStmt is not yet implemented" << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgCaseOptionStmt:
			{
			std::cout << "SgCaseOptionStmt is not yet implemented" << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgCatchStatementSeq:
			{
			std::cout << "SgCatchStatementSeq is not yet implemented" << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgComputedGotoStatement:
			{
			std::cout << "SgComputedGotoStatement is not yet implemented" << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgContinueStmt:
			{
			std::cout << "SgContinueStmt is not yet implemented" << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgDeallocateStatement:
			{
			std::cout << "SgDeallocateStatement is not yet implemented" << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgDefaultOptionStmt:
			{
			std::cout << "SgDefaultOptionStmt is not yet implemented" << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgExprStatement:
			{
			statStr = getSgExprStatement(isSgExprStatement(stat));
			break;
			}

		case V_SgForInitStatement:
			{
			std::cout << "SgForInitStatement is not yet implemented" << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgFunctionTypeTable:
			{
			std::cout << "SgFunctionTypeTable is not yet implemented" << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgGotoStatement:
			{
			std::cout << "SgGotoStatement is not yet implemented" << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgLabelStatement:
			{
			std::cout << "SgLabelStatement is not yet implemented" << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgReturnStmt:
			{
			SgReturnStmt* ret = isSgReturnStmt(stat);
			SgExpression* retExp = ret->get_expression();
			std::string retExpStr = getSgExpressionString(retExp);
			statStr = "; return statement not yet linked to function\n ;" + retExpStr + "\n";	
			break;
			}

		case V_SgSequenceStatement:

			{
			std::cout << "SgSequenceStatement is not yet implemented" << std::endl; ;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgPassStatement:

			{
			std::cout << "SgPassStatement is not yet implemented" << std::endl; ;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgNullStatement:

			{
			std::cout << "SgNullStatement is not yet implemented" << std::endl; ;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgNullifyStatement:

			{
			std::cout << "SgNullifyStatement is not yet implemented" << std::endl; ;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgExecStatement:

			{
			std::cout << "SgExecStatement is not yet implemented" << std::endl; ;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgElseWhereStatement:

			{
			std::cout << "SgElseWhereStatement is not yet implemented" << std::endl; ;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgScopeStatement:
			{
			std::cout << "SgScopeStatement should not be found here! " << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgDeclarationStatement:
			{
			std::cout << "SgDeclarationStatement should not be found here! " << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgIOStatement:
			{
			std::cout << "SgIOStatement should not be found here! " << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgJavaThrowStatement:
			{
			std::cout << "SgJavaThrowStatement should not be found here! " << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgJavaSynchronizedStatement:
			{
			std::cout << "SgJavaSynchronizedStatement should not be found here! " << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgOmpBarrierStatement:
			{
			std::cout << "SgOmpBarrierStatement should not be found here! " << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgOmpBodyStatement:
			{
			std::cout << "SgOmpBodyStatement should not be found here! " << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgOmpFlushStatement:
			{
			std::cout << "SgOmpFlushStatement should not be found here! " << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		case V_SgOmpTaskwaitStatement:
			{
			std::cout << "SgOmpTaskwaitStatement should not be found here! " << std::endl;
			ROSE_ASSERT(false);
			break;
			}

		default:
			std::cout << "unknown SgStatement type!: " << stat->class_name() << std::endl;
			ROSE_ASSERT(false);		
	}
	}
	return statStr;
}