void BasicProgmemTransform::transformCharArrayInitialization(SgFunctionDeclaration *func) {
	/* *
	 * Translates statements of the form:
	 * char arr[n] = "some string"; to:
	 * char arr[n];
	 * strcpy_P(arr, <progmem placeholder>);
	 * */
	Rose_STL_Container<SgNode *> initNames = NodeQuery::querySubTree(func, V_SgInitializedName);
	for(auto &item: initNames) {
		SgInitializedName *initName = isSgInitializedName(item);
		if(initName->get_initializer() == NULL) {
			continue;
		}
		SgVariableDeclaration * varDecl = isSgVariableDeclaration(initName->get_declaration());
		if(varDecl == NULL) {
			continue;
		}
		SgAssignInitializer *assignInit = isSgAssignInitializer(initName->get_initializer());
		if(assignInit == NULL) {
			continue;
		}
		SgType *type = initName->get_type();
		SgType *eleType = SageInterface::getElementType(type);
		if(isSgArrayType(type) && eleType != NULL && isSgTypeChar(eleType)) {
			SgStringVal* strVal = isSgStringVal(assignInit->get_operand());
			std::string str = strVal->get_value();
			int arrSize = getDeclaredArraySize(isSgArrayType(type));
			if(arrSize == 0) {
				//char arr[] = "something";
				int size = str.length() + 1;
				SgArrayType *type = SageBuilder::buildArrayType(SageBuilder::buildCharType(), SageBuilder::buildIntVal(size));
				initName->set_type(type);
			}
			varDecl->reset_initializer(NULL);
			SgVariableDeclaration *placeholder = getVariableDeclPlaceholderForString(str);
			SgVarRefExp *ref = SageBuilder::buildVarRefExp(placeholder);
			std::stringstream instr;
			instr << "\n strcpy_P(" << initName->get_name().getString();
			instr <<  ", " << ref->get_symbol()->get_name().getString() << ");\n";
			SageInterface::attachComment(varDecl, instr.str(), PreprocessingInfo::after);
			printf("transformed %s\n", initName->unparseToString().c_str());
		}

	}
}
SgVariableDeclaration *BasicProgmemTransform::getVariableDeclPlaceholderForString(const std::string& str) {
	for(auto &varDecl: varDeclsToShift) {
		SgInitializedName *initName = varDecl->get_variables().at(0);
		SgAssignInitializer *assign = isSgAssignInitializer(initName->get_initializer());
		if(assign == NULL) {
			continue;
		}
		SgStringVal* strVal = isSgStringVal(assign->get_operand());
		if(strVal->get_value() == str) {
			return varDecl;
		}
	}
	if(additionalProgmemStrings.find(str) != additionalProgmemStrings.end()) {
		return additionalProgmemStrings[str];
	}
	std::string placeholder = sla->getStringLiteralLabel(str);
	SgType *type = SageBuilder::buildPointerType(SageBuilder::buildConstType(SageBuilder::buildCharType()));
	SgAssignInitializer *initializer = SageBuilder::buildAssignInitializer(SageBuilder::buildStringVal(str));
	SgGlobal *global = SageInterface::getFirstGlobalScope(project);
	SgVariableDeclaration *varDec = SageBuilder::buildVariableDeclaration( "ar" +placeholder, type, initializer, global);
	additionalProgmemStrings[str] = varDec;
	return varDec;
}
Example #3
0
void
CompassAnalyses::VariableNameEqualsDatabaseName::Traversal::
visit(SgNode* node)
   { 
     if( isSgAssignInitializer(node) != NULL )
          assignExp = node;
  
     if( isSgAssignOp(node) != NULL )
          assignExp = node; 

     SgFunctionCallExp* funcCall = isSgFunctionCallExp(node);

  // See if we have a dot expression or arrow expression which
  // accesses the desired member function in the class we are looking for.
     if ( funcCall != NULL  )
        {
          SgExpression* funcExp = funcCall->get_function();

          if ( ( isSgDotExp(funcExp) != NULL ) | ( isSgArrowExp(funcExp) != NULL ) )
             {
               SgBinaryOp*     binOp = isSgBinaryOp(funcExp);
               SgExpression*   rhsOp = binOp->get_rhs_operand();
            // SgExpression*   lhsOp = binOp->get_lhs_operand();
               if ( SgMemberFunctionRefExp* funcRef = isSgMemberFunctionRefExp(rhsOp)  )
                  {
                 // std::cout << "c1\n" ;

                    SgMemberFunctionSymbol*      funcSymbol = funcRef->get_symbol();
                    ROSE_ASSERT(funcSymbol->get_declaration() != NULL);

                 // DQ (1/16/2008): Note that the defining declaration need not exist (see test2001_11.C)
                 // ROSE_ASSERT(funcSymbol->get_declaration()->get_definingDeclaration() != NULL);
                    if (funcSymbol->get_declaration()->get_definingDeclaration() != NULL)
                       {
                         SgMemberFunctionDeclaration* funcDecl   = isSgMemberFunctionDeclaration(funcSymbol->get_declaration()->get_definingDeclaration());
                         ROSE_ASSERT( funcDecl != NULL );

                         SgClassDefinition* clDef = isSgClassDefinition(funcDecl->get_scope());
                         SgClassDeclaration*          clDecl     = isSgClassDeclaration(clDef->get_declaration());  

                      // SgClassDeclaration*          clDecl     = funcDecl->get_associatedClassDeclaration();

                         ROSE_ASSERT( clDecl != NULL );
                         std::string className    = clDecl->get_name().getString();

                         ROSE_ASSERT(funcDecl != NULL);
                         std::string functionName = funcDecl->get_name().getString();
 
                      // If the class is the class we are looking for see if the member function
                      // access is to the member function we are interested in.
                      // std::cout << "className = " << className << std::endl;
                      // std::cout << "functionName = " << functionName << std::endl;

                         if ( (className == classToLookFor) && ( functionName == memberFunctionToLookFor ) )
                            {
                              SgExprListExp*         actualArgs    = funcCall->get_args();
                              SgExpressionPtrList&   actualExpArgs = actualArgs->get_expressions ();

                              ROSE_ASSERT(actualExpArgs.size() == 1);
                              Rose_STL_Container<SgNode*> nodeLst = NodeQuery::querySubTree(*actualExpArgs.begin(), V_SgStringVal);

                              ROSE_ASSERT( nodeLst.size() > 0);
                              SgStringVal* actualArg = isSgStringVal(*nodeLst.begin());
                              ROSE_ASSERT(actualArg != NULL);

                              std::string  stringArg = actualArg->get_value();

                              std::cout << "arg:" << stringArg << std::endl;

                              std::string varName;

                           // SgInitializedName* initName = NULL; 
                              if ( SgAssignInitializer* assignInit =  isSgAssignInitializer(assignExp) )
                                 {
                                   SgInitializedName* initName = isSgInitializedName(assignInit->get_parent());
                                   ROSE_ASSERT(initName != NULL);
                                 
                                   varName = initName->get_name().getString();
                                 }
                                else
                                 {
                                   if ( SgAssignOp* assignOp = isSgAssignOp(assignExp) )
                                      {
                                        SgExpression*     lhsOp  = assignOp->get_lhs_operand();
                                        SgVarRefExp*      varRef = isSgVarRefExp(lhsOp);
                                        ROSE_ASSERT(varRef!=NULL);
                                        SgVariableSymbol* varSymbol = varRef->get_symbol();
                                        ROSE_ASSERT(varSymbol != NULL);
                                        SgInitializedName* initName = varSymbol->get_declaration();
                                        varName = initName->get_name().getString();
                                      }
                                 }
 
                              if (varName != "")
                                 {
                                // we are only interested in the part of the argument after the last ":"
                                // Database scopes in ALE3D are separated by ":"

                                   size_t posCol = stringArg.find_last_of(':');
                                 
                                   if (posCol != std::string::npos)
                                        stringArg = stringArg.substr(posCol+1);

                                 //Find violations to the rule
                                   if ( stringArg != varName)
                                      {
                                        output->addOutput(new CheckerOutput(assignExp));
                                        std::cout << "violation" << varName << std::endl;
                                      }
                                     else 
                                      {
                                        std::cout << "non=violation" << varName << std::endl;
                                      }
                                 }
                            }
                       }
                  }
             } 
        }
   } // End of the visit function.
Example #4
0
	bool
checkIfNodeMaps(token_type tok, SgNode* node)
{
	bool nodeMaps = false;

	using namespace boost::wave;

        //By definition a compiler generated node can
        //not map to the token stream
        SgLocatedNode* compilerGeneratedNode = isSgLocatedNode(node);
        if( compilerGeneratedNode != NULL  )
          if(compilerGeneratedNode->get_file_info()->isCompilerGenerated() == true)
            return false;

//	     std::cout << get_token_name(tok) << " " << std::string(tok.get_value().c_str()) << " " << node->class_name() << " " << node->get_file_info()->get_line() << std::endl;
	//Mapping literal token id's
	switch(token_id(tok)){
		case T_PP_NUMBER:{

					 if(isSgValueExp(node)!=NULL)
						 nodeMaps=true;
					 break;
				 }
		case T_CHARLIT:{
				       if( ( isSgCharVal(node) != NULL ) |
						       ( isSgUnsignedCharVal(node) != NULL )
					 )
					       nodeMaps = true;
				       break;
			       }
		case T_FLOATLIT:{
					if( isSgFloatVal(node) != NULL )
						nodeMaps = true;

					break;
				}
		case T_INTLIT:{
				      if( ( isSgIntVal(node) != NULL ) || 
						      ( isSgUnsignedIntVal(node) != NULL ) 	       
					)
					      nodeMaps = true;
				      break;
			      }
		case T_LONGINTLIT:
			      {
				      if( ( isSgLongIntVal(node) != NULL ) |
						      ( isSgLongLongIntVal(node) != NULL ) |
						      ( isSgUnsignedLongLongIntVal(node) != NULL )

					)
					      nodeMaps = true;
				      break;
			      }
		case T_STRINGLIT:
			      {
				      if( isSgStringVal(node) != NULL )
					      nodeMaps = true;
				      break;
			      }
                case T_QUESTION_MARK:
                              {
                   		      if( isSgConditionalExp(node) != NULL )
                                        nodeMaps = true;
                                      break;
                              }
		case T_FALSE:
		case T_TRUE:
			      if( isSgBoolValExp(node) != NULL  )
				      nodeMaps = true;
			      break;
		default:
			      break;
	}

	//map keyword token id's
	switch(token_id(tok)){

		case T_ASM:
			if( isSgAsmStmt(node) != NULL  )
				nodeMaps = true;
			break;

		case T_AUTO: //auto
			//dont know
			break;
			/*case T_BOOL:
			//dont think this can be mapped
			break;*/
		case T_BREAK:
			if( isSgBreakStmt(node) != NULL  )
				nodeMaps = true;
			break;

		case T_CASE:
			if( isSgCaseOptionStmt(node) != NULL  )
				nodeMaps = true;
			break;

		case T_CATCH:
			if( isSgCatchOptionStmt(node) != NULL )
				nodeMaps = true;
			break;
			/*
			   case T_CHAR:
			//dont know
			break;
			*/	    

		case boost::wave::T_CLASS:
			if( isSgClassDeclaration(node) != NULL )
				nodeMaps = true;
			break;

		case T_CONST:
			// is it SgConstVolatileModifier?
			//dont know
			break;
		case T_CONTINUE:
			if( isSgContinueStmt(node) != NULL )
				nodeMaps = true;
			break;

			//case T_DEFAULT:
			//Dont know
			//  break;


		case T_DELETE:
			if( isSgDeleteExp(node) != NULL  )
				nodeMaps = true;
			break;

		case T_DO:
			if( isSgDoWhileStmt(node) != NULL  )
				nodeMaps = true;
			break;

		case T_ELSE:
			//dont know
			break;

		case T_EXPLICIT:
			//dont know
			break;
		case T_EXPORT:
		case T_EXTERN:
			break;
		case T_FOR:

			if( isSgForStatement(node) != NULL  )
				nodeMaps = true;
			break;
		case T_FRIEND:
			//dont know
			break;
		case T_GOTO:

			if( isSgGotoStatement(node) != NULL  )
				nodeMaps = true;
			break;

		case T_IF:
			//dont know how to handle this because if if-else
			break;
		case T_INLINE:
			//dont know
			break;
		case T_MUTABLE:
			//dont know
			break;
		case T_NAMESPACE:

			if( ( isSgNamespaceAliasDeclarationStatement(node) != NULL  ) |
					(isSgNamespaceDeclarationStatement(node) != NULL )
			  )
				nodeMaps = true;
			break;
		case T_NEW:

			if( isSgNewExp(node) != NULL  )
				nodeMaps = true;
			break;
		case T_OPERATOR:
		case T_PRIVATE:
		case T_PROTECTED:
		case T_PUBLIC:
		case T_REGISTER:
		case T_REINTERPRETCAST:
			//dont know
			break;

		case T_RETURN:
			if( isSgReturnStmt(node) != NULL  )
				nodeMaps = true;
			break;

		case T_SIZEOF:
			if( isSgSizeOfOp(node) != NULL  )
				nodeMaps = true;
			break;
		case T_STATIC:
		case T_STATICCAST:
			//dont know
			break;
		case T_STRUCT:
			if( isSgClassDeclaration(node) != NULL  )
				nodeMaps = true;
			break;
		case T_SWITCH:
			if( isSgSwitchStatement(node) != NULL  )
				nodeMaps = true;
			break;
			//case T_TEMPLATE:
			//dont know
			//   break;
		case T_THIS:

			if( isSgThisExp(node) != NULL  )
				nodeMaps = true;

			break;
		case T_THROW:

			if( isSgThrowOp(node) != NULL  )
				nodeMaps = true;

			break;
		case T_TRY:

			if( isSgTryStmt(node) != NULL  )
				nodeMaps = true;

			break;
		case boost::wave::T_TYPEDEF:

			if( isSgTypedefDeclaration(node) != NULL  )
				nodeMaps = true;

			break;

		case T_TYPEID:
			if( isSgTypeIdOp(node) != NULL )
				nodeMaps = true;
			break;

		case T_TYPENAME:
			//dont know
			break;
		case T_UNION:
			if( isSgClassDeclaration(node) != NULL  )
				nodeMaps = true;

			break;
		case T_USING:
			if( isSgUsingDeclarationStatement(node) != NULL  )
				nodeMaps = true;

			break;
		case T_VIRTUAL:
			//dont know
			break;
		case T_VOLATILE:

			//is it SgConstVolatileModifier ?
			break;

		case T_WHILE:
			if( isSgWhileStmt(node) != NULL  )
				nodeMaps = true;

			break;

		default:
			break;

	}


	//map operator token id's
	switch(token_id(tok)){
		case T_AND:
          	case T_ANDAND:
			if( isSgAndOp(node) != NULL | isSgBitAndOp(node) != NULL )
				nodeMaps = true;
			break;
		case T_ASSIGN:
			if ( isSgAssignOp(node) != NULL | isSgAssignInitializer(node) != NULL )
				nodeMaps = true;
			break;
		case T_ANDASSIGN:
			//do not know
			break;
		case T_OR:
			if ( isSgBitOrOp(node) != NULL || isSgOrOp(node) != NULL )
				nodeMaps = true;
			break;
		case T_ORASSIGN:
			//do not know
			break;
		case T_XOR:
			if ( isSgBitXorOp(node) != NULL )
				nodeMaps = true;
			break;
		case T_XORASSIGN:
			if ( isSgXorAssignOp(node) != NULL )
				nodeMaps = true;
			break;	
		case T_COMMA:
			if ( isSgCommaOpExp(node) != NULL )
				nodeMaps = true;
			break;
		case T_COLON:
			//dont know
			break;

		case T_DIVIDE:
			if ( isSgDivideOp(node) != NULL )
				nodeMaps = true;
			break;
		case T_DIVIDEASSIGN:
			if ( isSgDivAssignOp(node) != NULL )
				nodeMaps = true;
			break;
		case T_DOT:
			if ( isSgDotExp(node) != NULL )
				nodeMaps = true;
			break;


		case T_DOTSTAR:
			if ( isSgDotExp(node) != NULL )
				nodeMaps = true;
			break;


		case T_ELLIPSIS:
			//Dont know
			break;
		case T_EQUAL:
			if ( isSgEqualityOp(node) != NULL )
				nodeMaps = true;
			break;
		case T_GREATER:
			if ( isSgGreaterThanOp(node) != NULL )
				nodeMaps = true;
			break;

		case T_GREATEREQUAL:
			if ( isSgGreaterOrEqualOp(node) != NULL )
				nodeMaps = true;
			break;

		case T_LEFTBRACE:
			//Dont know
			break;
		case T_LESS:
			if ( isSgLessThanOp(node) != NULL )
				nodeMaps = true;
			break;

		case T_LESSEQUAL:
			if ( isSgLessOrEqualOp(node) != NULL )
				nodeMaps = true;
			break;

		case T_LEFTPAREN:
			//Dont know
			break;

		case T_LEFTBRACKET:
		case T_RIGHTBRACKET:
			if ( isSgPntrArrRefExp(node) != NULL ) 
                             nodeMaps = true;
			break;
		case T_MINUS:
			if ( ( isSgSubtractOp(node) != NULL ) |
					( isSgMinusOp(node) != NULL ) )
				nodeMaps = true;
			break;

		case T_MINUSASSIGN:
			if ( isSgMinusAssignOp(node) != NULL )
				nodeMaps = true;
			break;

		case T_MINUSMINUS:
			if ( isSgMinusMinusOp(node) != NULL )
				nodeMaps = true;
			break;

		case T_PERCENT:
			if ( isSgModOp(node) != NULL )
				nodeMaps = true;
			break;

		case T_PERCENTASSIGN:
			if ( isSgModAssignOp(node) != NULL )
				nodeMaps = true;
			break;

		case T_NOT:
			if ( isSgNotOp(node) != NULL )
				nodeMaps = true;
			break;

		case T_NOTEQUAL:
			if ( isSgNotEqualOp(node) != NULL )
				nodeMaps = true;
			break;

		case T_OROR:
			if ( isSgOrOp(node) != NULL )
				nodeMaps = true;
			break;

		case T_PLUS:
			if ( isSgAddOp(node) != NULL )
				nodeMaps = true;
			break;
		case T_PLUSASSIGN:
			if ( isSgPlusAssignOp(node) != NULL )
				nodeMaps = true;
			break;
		case T_PLUSPLUS:
			if ( isSgPlusPlusOp(node) != NULL )
				nodeMaps = true;
			break;


		case T_ARROW:
			if ( isSgArrowExp(node) != NULL )
				nodeMaps = true;
			break;

		case T_ARROWSTAR:
			if ( isSgArrowStarOp(node) != NULL )
				nodeMaps = true;
			break;
		case T_QUESTION_MARK:
			//dont know
			break;

		case T_RIGHTBRACE:
		case T_RIGHTPAREN:
		case T_COLON_COLON:
		case T_SEMICOLON:
			//dont know
			break;
		case T_SHIFTLEFT:
			if ( isSgLshiftOp(node) != NULL )
				nodeMaps = true;
			break;

		case T_SHIFTLEFTASSIGN:
			if ( isSgLshiftAssignOp(node) != NULL )
				nodeMaps = true;
			break;
		case T_SHIFTRIGHT:
			if ( isSgRshiftOp(node) != NULL )
				nodeMaps = true;
			break;
		case T_SHIFTRIGHTASSIGN:
			if ( isSgRshiftAssignOp(node) != NULL )
				nodeMaps = true;
			break;

		case T_STAR:
			//dont know
                        if ( isSgMultiplyOp(node) != NULL || isSgPointerType(node) )
                          nodeMaps = true;
			break;
		case T_COMPL://~

                        if( isSgBitComplementOp(node)  != NULL  )
                          nodeMaps = true;
			//Dont know
			break;
		case T_STARASSIGN:
			if ( isSgMultAssignOp(node) != NULL )
				nodeMaps = true;

			break;
		case T_POUND_POUND:
		case T_POUND:
			//dont know
			break;

		case T_AND_ALT:
		case T_ANDASSIGN_ALT:
		case T_OR_ALT:
		case T_ORASSIGN_ALT:
		case T_XOR_ALT:
		case T_XORASSIGN_ALT:
		case T_LEFTBRACE_ALT:
		case T_LEFTBRACKET_ALT:
		case T_NOT_ALT:
		case T_NOTEQUAL_ALT:
		case T_RIGHTBRACE_ALT:
		case T_RIGHTBRACKET_ALT:
		case T_COMPL_ALT:
		case T_POUND_POUND_ALT:
		case T_POUND_ALT:
		case T_OR_TRIGRAPH:
		case T_XOR_TRIGRAPH:
		case T_LEFTBRACE_TRIGRAPH:
		case T_LEFTBRACKET_TRIGRAPH:
		case T_RIGHTBRACE_TRIGRAPH:
		case T_RIGHTBRACKET_TRIGRAPH:
		case T_COMPL_TRIGRAPH:
		case T_POUND_POUND_TRIGRAPH:
		case T_POUND_TRIGRAPH:
			//dont know
			break;
		default:
			break;
	}

	switch(token_id(tok)){
		case T_FALSE:
		case T_TRUE:
			break;
		case T_CHARLIT:
			if(SgProject::get_verbose() >= 1)
				std::cout << "char " << std::string(tok.get_value().c_str()) << std::endl;

			if( isSgCharVal(node) != NULL ){
				SgCharVal* charVal = isSgCharVal(node);
				if(SgProject::get_verbose() >= 1)
					std::cout << std::string(tok.get_value().c_str()) << std::endl;
				char tmp = charVal->get_value();
				if(SgProject::get_verbose() >= 1)
					std::cout << "From charlit: " << tmp << std::endl;

				if(("\""+std::string(&tmp)+"\"") == std::string(tok.get_value().c_str()) )
					nodeMaps = true;


			}
			break;
		case T_STRINGLIT:
			{
				if(SgProject::get_verbose() >= 1)
					std::cout << "string " <<std::string(tok.get_value().c_str()) << std::endl;

				if( isSgStringVal(node) != NULL ){
					SgStringVal* stringVal = isSgStringVal(node);
					if(SgProject::get_verbose() >= 1){
						std::cout << std::string(tok.get_value().c_str()) << std::endl;
						std::cout << "stringlit: " << stringVal->get_value() << std::endl;
					}
					if(("\""+stringVal->get_value()+"\"") == std::string(tok.get_value().c_str()) )
						nodeMaps = true;


				}
				break;
			}
			/*case V_SgWcharVal:

			  if( isSgWcharVal(node) != NULL )
			  if( std::string(isSgWcharVal(node)->get_value()) == std::string(tok.get_value().c_str()) )
			  nodeMaps = true;

			  break;*/
		default:
			break; 

	}

	if( token_id(tok) == T_IDENTIFIER ){
		if( isSgInitializedName(node) != NULL ){

			std::cout << "Identifier" << std::endl;
			SgInitializedName* initName = isSgInitializedName(node);
			std::cout <<  initName->get_name().getString() << " " << std::string(tok.get_value().c_str()) << std::endl;
			if( initName->get_name().getString() == std::string(tok.get_value().c_str()) ){
				nodeMaps = true;
			}
		}else if( isSgVarRefExp(node) != NULL ){
			SgVariableSymbol* varSymbol = isSgVarRefExp(node)->get_symbol();
			SgInitializedName* initName = varSymbol->get_declaration();
			if( initName->get_name().getString() == std::string(tok.get_value().c_str()) ){
				nodeMaps = true;
                                std::cout << "Maps:" << initName->get_name().getString() << " " << std::string(tok.get_value().c_str()) 
                                          << std::endl; 
                        }else
                          std::cout << "DONT Map:" << initName->get_name().getString() << " " << std::string(tok.get_value().c_str()) 
                                          << std::endl; 

		}else if( isSgFunctionRefExp(node) != NULL){
			SgFunctionRefExp* funcRef = isSgFunctionRefExp(node);
			SgFunctionSymbol* funcSymbol = funcRef->get_symbol_i();
			if( funcSymbol->get_declaration()->get_name().getString() == std::string(tok.get_value().c_str()) )
				nodeMaps = true;

		}else if( isSgMemberFunctionRefExp(node) != NULL){
			SgMemberFunctionRefExp* funcRef = isSgMemberFunctionRefExp(node);
			SgMemberFunctionSymbol* funcSymbol = funcRef->get_symbol();
			if( funcSymbol->get_declaration()->get_name().getString() == std::string(tok.get_value().c_str()) )
				nodeMaps = true;

		}

	}

        //Exceptions to the general rule

		switch(token_id(tok)){
                  case T_CHARLIT:
                    {
                      switch(node->variantT())
                      {
                        case V_SgCharVal:
                        case V_SgComplexVal:
                        case V_SgDoubleVal:
                        case V_SgEnumVal:
                        case V_SgFloatVal:
                        case V_SgIntVal:
                        case V_SgLongDoubleVal:
                        case V_SgLongIntVal:
                        case V_SgLongLongIntVal:
                        case V_SgShortVal:
                        case V_SgStringVal:
                        case V_SgUnsignedCharVal:
                        case V_SgUnsignedIntVal:
                        case V_SgUnsignedLongLongIntVal:
                        case V_SgUnsignedLongVal:
                        case V_SgUnsignedShortVal:
                        case V_SgWcharVal: 
                          {
                            nodeMaps=true;
                            break;
                          }
                        default:
                          break;

                      };

                      break;
                    };
                }
	switch(boost::wave::token_id(tok)){
		case boost::wave::T_CHAR:
                  {
                  if(isSgTypeChar(node) != NULL)
                    nodeMaps=true;
                  break;
                  }
		case boost::wave::T_CONST:
                  break;
		case boost::wave::T_DOUBLE: 		
                  if(isSgTypeDouble(node) != NULL)
                    nodeMaps=true;
                  break;

		case boost::wave::T_INT:
                  if(isSgTypeInt(node) != NULL)
                    nodeMaps=true;
                  break;

		case boost::wave::T_LONG:
                  if(isSgTypeLong(node) != NULL)
                    nodeMaps=true;
                  break;

		case boost::wave::T_SHORT:
                 if(isSgTypeShort(node) != NULL)
                    nodeMaps=true;
                  break;

		case boost::wave::T_SIGNED:
		case boost::wave::T_UNSIGNED:
		case boost::wave::T_VOID:
                  if(isSgTypeVoid(node) != NULL)
                    nodeMaps=true;
                  break;
		default:
			break;

	};

	return nodeMaps;
}
void
FortranProgramDeclarationsAndDefinitions::analyseParallelLoopArguments (
    FortranParallelLoop * parallelLoop, SgExprListExp * actualArguments,
    int numberOfOpArgs)
{
  using boost::iequals;
  using boost::lexical_cast;
  using std::find;
  using std::string;
  using std::vector;
  using std::map;

  Debug::getInstance ()->debugMessage (
      "Analysing OP_PAR_LOOP actual arguments", Debug::FUNCTION_LEVEL,
      __FILE__, __LINE__);

  unsigned int OP_DAT_ArgumentGroup = 1;
  
  /*
   * ======================================================
   * Loops over the arguments to populate the parallel loop object
   * Each object in the actualArguments array is a function call
   * We analyse the arguments of each call
   * \warning: the args start from position 2 (the first two args are
   * the user kernel reference and the iteration set
   * ======================================================
   */
  
  for ( int i = 2; i < numberOfOpArgs + 2; i++ )
  {
   /*
    * ======================================================
    * Distinguish between op_dat and global variable by
    * checking the function name
    * ======================================================
    */
    SgFunctionCallExp * functionExpression = isSgFunctionCallExp (
      actualArguments->get_expressions ()[i]);
    
    ROSE_ASSERT (functionExpression != NULL);
    
    string functionName = functionExpression ->getAssociatedFunctionSymbol ()->
      get_name ().getString ();
    
    SgExprListExp * opArgArguments = functionExpression ->get_args ();
    ROSE_ASSERT (opArgArguments );

    /*
     * ======================================================
     * Assume that this is not an op_mat, possibly amend later
     * ======================================================     
     */
    parallelLoop->setIsOpMatArg (OP_DAT_ArgumentGroup, false);
    
    if ( functionName == "op_arg_dat" )
    {     
     /*
      * ======================================================
      * Obtain the op_dat reference and its name
      * ======================================================
      */
     
      SgVarRefExp * opDatReference = NULL;

      opDatReference = getOpDatReferenceFromOpArg (opArgArguments);

      ROSE_ASSERT (opDatReference != NULL);

      string const opDatName = opDatReference->get_symbol ()->get_name ().getString ();

     /*
      * ======================================================
      * Obtain the map reference and name, and select access
      * type (direct or indirect)
      * ======================================================            
      */
      SgVarRefExp * opMapReference;

      opMapReference = getOpMapReferenceFromOpArg (opArgArguments);

      ROSE_ASSERT (opMapReference != NULL);

      string const mappingValue = opMapReference->get_symbol ()->get_name ().getString ();

      if (iequals (mappingValue, OP2::OP_ID))
      {
        /*
         * ======================================================
         * OP_ID signals identity mapping and therefore direct
         * access to the data
         * ======================================================
         */
        Debug::getInstance ()->debugMessage ("...DIRECT mapping descriptor",
            Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__);

        parallelLoop->setOpMapValue (OP_DAT_ArgumentGroup, DIRECT);
      }
      else
      {
        Debug::getInstance ()->debugMessage ("...INDIRECT mapping descriptor",
            Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__);

        parallelLoop->setOpMapValue (OP_DAT_ArgumentGroup, INDIRECT);
      }

      setOpDatProperties (parallelLoop, opDatName, OP_DAT_ArgumentGroup);

      setParallelLoopAccessDescriptor (parallelLoop, opArgArguments, OP_DAT_ArgumentGroup, DAT_ACC_POSITION);
    }
    else if ( functionName == "op_arg_gbl" )
    {

      Debug::getInstance ()->debugMessage ("...GLOBAL mapping descriptor",
        Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__);

     /*
      * ======================================================
      * Get the OP_GBL variable reference and name
      * ======================================================
      */
      SgVarRefExp * opDatReference;

      if (isSgDotExp (opArgArguments->get_expressions ()[DAT_POSITION])
          != NULL)
      {
        opDatReference
            = isSgVarRefExp (
                isSgDotExp (
                    opArgArguments->get_expressions ()[DAT_POSITION])->get_rhs_operand ());
      }
      else
      {
        opDatReference = isSgVarRefExp (
            opArgArguments->get_expressions ()[DAT_POSITION]);
      }

      string const globalName =
          opDatReference->get_symbol ()->get_name ().getString ();
      

     /*
      * ======================================================
      * Get the OP_GBL dimension: check the number of args
      * to the op_arg_gbl function (3 = array, 2 = scalar)
      * ======================================================
      */
      if ( opArgArguments->get_expressions ().size () == GBL_SCALAR_ARG_NUM )
      {

        Debug::getInstance ()->debugMessage ("'" + globalName + "' is a scalar",
            Debug::FUNCTION_LEVEL, __FILE__, __LINE__);

       /*
        * ======================================================
        * Since this is a scalar, set the dimension to 1
        * ======================================================
        */

        parallelLoop->setOpDatDimension (OP_DAT_ArgumentGroup, 1);
      }
      else
      {
        SgIntVal * intValExp = isSgIntVal (opArgArguments->get_expressions ()[GBL_DIM_POSITION]);
        ROSE_ASSERT (intValExp != NULL);
        
        int globalDimension = intValExp->get_value ();
        
        parallelLoop->setOpDatDimension (OP_DAT_ArgumentGroup, globalDimension);
        
        Debug::getInstance ()->debugMessage ("'" + globalName
          + "' is NOT a scalar, but has dimension " + lexical_cast<string> (globalDimension),
          Debug::FUNCTION_LEVEL, __FILE__, __LINE__);
      }

     /*
      * ======================================================
      * Set the other fields
      * ======================================================
      */
      parallelLoop->setOpDatType (OP_DAT_ArgumentGroup,
        (opArgArguments->get_expressions ()[DAT_POSITION])->get_type ());
     
      parallelLoop->setUniqueOpDat (globalName);

      parallelLoop->setOpDatVariableName (OP_DAT_ArgumentGroup, globalName);

      parallelLoop->setDuplicateOpDat (OP_DAT_ArgumentGroup, false);

      parallelLoop->setOpMapValue (OP_DAT_ArgumentGroup, GLOBAL);

      if ( opArgArguments->get_expressions ().size () == GBL_SCALAR_ARG_NUM )       
        setParallelLoopAccessDescriptor (parallelLoop, opArgArguments,
          OP_DAT_ArgumentGroup, GBL_SCALAR_ACC_POSITION);
      else        
        setParallelLoopAccessDescriptor (parallelLoop, opArgArguments,
          OP_DAT_ArgumentGroup, GBL_ARRAY_ACC_POSITION);
    }
    else if ( functionName == "op_arg_dat_generic" )
    {
      Debug::getInstance ()->debugMessage ("Found generic op_dat",
        Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__);

      /*
       * ======================================================
       * Obtain the op_dat reference and its name
       * ======================================================
       */
      SgVarRefExp * opDatReference = NULL;

      opDatReference = getOpDatReferenceFromOpArg (opArgArguments);

      ROSE_ASSERT (opDatReference != NULL);

      string const opDatName = opDatReference->get_symbol ()->get_name ().getString ();      

      /*
       * ======================================================
       * Obtain the map reference and name, and select access
       * type (direct or indirect)
       * ======================================================
       */
      SgVarRefExp * opMapReference;

      opMapReference = getOpMapReferenceFromOpArg (opArgArguments);

      ROSE_ASSERT (opMapReference != NULL);

      string const mappingValue = opMapReference->get_symbol ()->get_name ().getString ();

      if (iequals (mappingValue, OP2::OP_ID))
        {
          /*
           * ======================================================
           * OP_ID signals identity mapping and therefore direct
           * access to the data
           * ======================================================
           */
          Debug::getInstance ()->debugMessage ("...DIRECT mapping descriptor",
            Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__);

          parallelLoop->setOpMapValue (OP_DAT_ArgumentGroup, DIRECT);
        }
      else
        {
          Debug::getInstance ()->debugMessage ("...INDIRECT mapping descriptor",
            Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__);

          parallelLoop->setOpMapValue (OP_DAT_ArgumentGroup, INDIRECT);
        }

      /*
       * ======================================================
       * In case of generic loop, I have to read also the
       * dimension and type to be able to set the op_dat info
       * in the parallel loop class
       * ======================================================
       */
      SgIntVal * opDatDimension = isSgIntVal (
        opArgArguments->get_expressions ()[GENERIC_DIM_POSITION]);

        
      SgStringVal * opDataBaseTypeString = isSgStringVal (
        opArgArguments->get_expressions ()[GENERIC_TYPE_POSITION]);

      ROSE_ASSERT (opDataBaseTypeString != NULL);
        
      SgType * opDataBaseType = getTypeFromString (opDataBaseTypeString->get_value (),
        opDatName);

      ROSE_ASSERT (opDataBaseType != NULL);

      setOpDatPropertiesGeneric (parallelLoop, opDatName, opDatDimension->get_value (),
         opDataBaseType, OP_DAT_ArgumentGroup);

      Debug::getInstance ()->debugMessage ("Getting access",
        Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__);

      setParallelLoopAccessDescriptor (parallelLoop, opArgArguments,
        OP_DAT_ArgumentGroup, GENERIC_ACC_POSITION);
    } 
    else if ( functionName == "op_arg_mat" )
    {
      Debug::getInstance ()->debugMessage ("Unsupported argument type",
        Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__);
    }
    else
    {
      Debug::getInstance ()->debugMessage ("Argument type not recognised",
        Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__);      
    }

   /*
    * ======================================================
    * This identifies the argument position in the data
    * structures, while the iteration variable 'i' identifies
    * the corresponding op_arg position in the op_par_loop
    * arguments (i == OP_DAT_ArgumentGroup + 2)
    * ======================================================
    */

    OP_DAT_ArgumentGroup++;
  }
  
  parallelLoop->setNumberOfOpDatArgumentGroups (numberOfOpArgs);
  parallelLoop->setNumberOfOpMatArgumentGroups (0);
  
  if ( parallelLoop->isDirectLoop () )
    Debug::getInstance ()->debugMessage ("This is a DIRECT parallel loop",
      Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__);
  else
    Debug::getInstance ()->debugMessage ("This is an INDIRECT parallel loop",
      Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__);  
}
Example #6
0
File: options.C Project: 8l/rose
void
TransformationSupport::getTransformationOptionsFromVariableDeclarationConstructorArguments (
   SgVariableDeclaration* variableDeclaration,
   list<OptionDeclaration> & returnEnumValueList )
   {
     ROSE_ASSERT (variableDeclaration != NULL);
     SgInitializedNamePtrList & variableList = variableDeclaration->get_variables();

     printf ("Inside of getTransformationOptionsFromVariableDeclarationConstructorArguments() \n");

     for (SgInitializedNamePtrList::iterator i = variableList.begin(); i != variableList.end(); i++)
        {
       // We don't care about the name
       // SgName & name = (*i).get_name();

          ROSE_ASSERT ((*i) != NULL);
       // QY 11/10/04 removed named_item in SgInitializedName
       // printf ("Processing a variable in the list \n");
       //   if ((*i)->get_named_item() != NULL)
       //      {
       //        ROSE_ASSERT ((*i)->get_named_item() != NULL);
       //        SgInitializer *initializerOther = (*i)->get_named_item()->get_initializer();
               SgInitializer *initializerOther = (*i)->get_initializer();

            // This is not always a valid pointer
            // ROSE_ASSERT (initializerOther != NULL);

            // printf ("Test for initialized in variable \n");
               if (initializerOther != NULL)
                  {
                 // There are other things we could ask of the initializer
                    ROSE_ASSERT (initializerOther != NULL);

                 // printf ("Found a valid initialized in variable \n");

                    SgConstructorInitializer* constructorInitializer = isSgConstructorInitializer(initializerOther);
                    ROSE_ASSERT (constructorInitializer != NULL);

                    SgExprListExp* argumentList = constructorInitializer->get_args();
                    ROSE_ASSERT (argumentList != NULL);

                    SgExpressionPtrList & expressionPtrList = argumentList->get_expressions();
                    ROSE_ASSERT(expressionPtrList.empty() == false);
                    SgExpressionPtrList::iterator i = expressionPtrList.begin();

                    ROSE_ASSERT (*i != NULL);

                 // First value is a char* identifying the option
                    SgStringVal* charString = isSgStringVal(*i);
                    ROSE_ASSERT (charString != NULL);

#if 1
                    string optionString;
                    string valueString;

                    int counter = 0;
                    while (i != expressionPtrList.end())
                       {
                      // printf ("Expression List Element #%d of %d (total) (*i)->sage_class_name() = %s \n",
                      //      counter,expressionPtrList.size(),(*i)->sage_class_name());

                         switch ( (*i)->variant() )
                            {
                              case ENUM_VAL:
                                 {
                                   SgEnumVal* enumVal = isSgEnumVal(*i);
                                   ROSE_ASSERT (enumVal != NULL);

                                // int enumValue = enumVal->get_value();
                                   SgName enumName = enumVal->get_name();
                                // printf ("Name = %s value = %d \n",enumName.str(),enumValue);
                                // printf ("Name = %s \n",enumName.str());

                                   string name = enumName.str();
                                // char* name = rose::stringDuplicate( enumName.str() );

                                // Put the value at the start of the list so that the list can be processed in
                                // consecutive order to establish options for consecutive scopes (root to
                                // child scope). Order is not important if we are only ORing the operands!
                                // returnEnumValueList.push_front (name);
                                // returnEnumValueList.push_front (enumValue);

                                   ROSE_ASSERT (counter == 0);
                                   break;
                                 }

                              case STRING_VAL:
                                 {
                                // ROSE_ASSERT (counter == 1);

                                // printf ("Found a SgStringVal expression! \n");

                                   SgStringVal* stringVal = isSgStringVal(*i);
                                   ROSE_ASSERT (stringVal != NULL);

                                   if (counter == 0)
                                      {
                                        optionString = stringVal->get_value();
                                        valueString  = "";
                                     // printf ("optionString = %s \n",optionString);
                                      }

                                   if (counter == 1)
                                      {
                                        valueString = stringVal->get_value();
                                     // printf ("valueString = %s \n",valueString);
                                      }
                                   break;
                                 }

                              case DOUBLE_VAL:
                                 {
                                   ROSE_ASSERT (counter == 1);

                                // printf ("Found a SgStringVal expression! \n");

                                   SgDoubleVal* doubleVal = isSgDoubleVal(*i);
                                   ROSE_ASSERT (doubleVal != NULL);

                                   valueString = StringUtility::numberToString(doubleVal->get_value()).c_str();
                                // printf ("valueString = %s \n",valueString);
                                   break;
                                 }

                              default:
                                 {
                                   printf ("Default reached in switch in getTransformationOptionsFromVariableDeclarationConstructorArguments() (*i)->sage_class_name() = %s \n",(*i)->sage_class_name());
                                   ROSE_ABORT();
                                   break;
                                 }
                            }

                         i++;
                         counter++;
                       }

                 // printf ("optionString = %s valueString = %s \n",optionString,valueString);

                    OptionDeclaration optionSpecification(optionString.c_str(),valueString.c_str());
                    returnEnumValueList.push_front (optionSpecification);
#endif
                  }
                 else
                  {
                 // printf ("Valid initializer not found in variable \n");
                  }
/*
             }
            else
             {
            // printf ("Warning: In getTransformationOptionsFromVariableDeclarationConstructorArguments(): (*i).get_named_item() = NULL \n");
             }
*/
        }
   }