/* * Replace the op_par_loop with respective kernel function */ void OPSource::fixParLoops(SgNode *n) { SgName kernel_name; SgFunctionCallExp *fn = isSgFunctionCallExp(n); if(fn != NULL) { string fn_name = fn->getAssociatedFunctionDeclaration()->get_name().getString(); if(fn_name.compare("op_par_loop_2")==0 || fn_name.compare("op_par_loop_3")==0 || fn_name.compare("op_par_loop_4")==0 || fn_name.compare("op_par_loop_5")==0 || fn_name.compare("op_par_loop_6")==0 || fn_name.compare("op_par_loop_7")==0 || fn_name.compare("op_par_loop_8")==0 || fn_name.compare("op_par_loop_9")==0) { SgExprListExp* exprList = fn->get_args(); SgExpressionPtrList &exprs = exprList->get_expressions(); SgFunctionRefExp* varExp = isSgFunctionRefExp(exprs[0]); if(varExp != NULL) { kernel_name = varExp->get_symbol()->get_name(); } exprs.erase(exprs.begin()); SgExpressionPtrList::iterator it = exprs.begin() + op_par_loop_args::num_params - 1; for(; it != exprs.end(); it += op_argument::num_params) { *it = buildCastExp( *it, buildPointerType(SgClassType::createType( buildStructDeclaration("op_dat<void>"))) ); } // Inject Name exprs.insert(exprs.begin(), buildStringVal(kernel_name)); // Fetch the declaration SgName name = SgName("op_par_loop_") + kernel_name; SgFunctionDeclaration *funcDecl = cudaFunctionDeclarations[kernel_name]; if(funcDecl) { SgFunctionRefExp* ref = isSgFunctionRefExp(fn->get_function()); SgFunctionSymbol *symbol = ref->get_symbol(); symbol->set_declaration(funcDecl); ref->set_symbol(symbol); fn->set_function(ref); } } } }
void instrumentRead(SgVarRefExp *varRef) { SgExpression *parent = isSgExpression(varRef->get_parent()); assert(parent != NULL); Sg_File_Info * file_info = Sg_File_Info::generateDefaultFileInfoForTransformationNode(); SgCommaOpExp *commaOp = new SgCommaOpExp(file_info, beforeRead.getCallExp(), varRef, varRef->get_type()); SgUnaryOp *uOp = isSgUnaryOp(parent); if (uOp != NULL) { uOp->set_operand(commaOp); } else { SgBinaryOp *bOp = isSgBinaryOp(parent); if (bOp != NULL) { if (bOp->get_lhs_operand() == varRef) { bOp->set_lhs_operand(commaOp); } else { assert(bOp->get_rhs_operand() == varRef); bOp->set_rhs_operand(commaOp); } } else { SgExprListExp *expList = isSgExprListExp(parent); if (expList != NULL) { SgExpressionPtrList& expressions = expList->get_expressions(); for (SgExpressionPtrList::iterator iter = expressions.begin(); ; iter++) { assert (iter != expressions.end()); //element must be in the list! if (*iter == varRef) { //insert commaOp instead of varRef expressions.insert(expressions.erase(iter), commaOp); break; } } } else { //SgClassNameRefExp //SgConditionalExp //SgDeleteExp //go on implementing other cases cerr<<"unexpected parent expression: "<<parent->class_name()<<endl; assert (false); } } } }
void Fortran_to_C::translateArrayDeclaration(SgArrayType* originalArrayType) { // Get dim_info SgExprListExp* dimInfo = originalArrayType->get_dim_info(); // Get dim list SgExpressionPtrList dimExpressionPtrList = dimInfo->get_expressions(); // Get array base_type SgType* baseType = originalArrayType->get_base_type(); Rose_STL_Container<SgExpression*>::iterator j = dimExpressionPtrList.begin(); SgExpression* indexExpression = getFortranDimensionSize(*j); //std::cout << "array rank:" << originalArrayType->get_rank() << std::endl; if(originalArrayType->get_rank() == 1) { originalArrayType->set_base_type(baseType); originalArrayType->set_index(indexExpression); indexExpression->set_parent(originalArrayType); constantFolding(indexExpression); } else { SgArrayType* newType = buildArrayType(baseType,indexExpression); baseType->set_parent(newType); j = j + 1; for(; j< (dimExpressionPtrList.end()-1); ++j) { indexExpression = getFortranDimensionSize(*j); baseType = newType; newType = buildArrayType(baseType,indexExpression); baseType->set_parent(newType); } j = dimExpressionPtrList.end()-1; indexExpression = getFortranDimensionSize(*j); originalArrayType->set_base_type(newType); originalArrayType->set_index(indexExpression); indexExpression->set_parent(originalArrayType); } }
StencilEvaluation_InheritedAttribute StencilEvaluationTraversal::evaluateInheritedAttribute (SgNode* astNode, StencilEvaluation_InheritedAttribute inheritedAttribute ) { #if 0 printf ("In evaluateInheritedAttribute(): astNode = %p = %s \n",astNode,astNode->class_name().c_str()); #endif bool foundPairShiftDoubleConstructor = false; // This is for stencil specifications using vectors of points to represent offsets (not finished). // bool foundVariableDeclarationForStencilInput = false; double stencilCoeficientValue = 0.0; // StencilOffsetFSM offset; StencilOffsetFSM* stencilOffsetFSM = NULL; // We want to interogate the SgAssignInitializer, but we need to generality in the refactored function to use any SgInitializer (e.g. SgConstructorInitializer, etc.). SgInitializedName* initializedName = detectVariableDeclarationOfSpecificType (astNode,"Point"); if (initializedName != NULL) { // This is the code that is specific to the DSL (e.g. the semantics of getZeros() and getUnitv() functions). // So this may be the limit of what can be refactored to common DSL support code. // Or I can maybe do a second pass at atempting to refactor more code later. string name = initializedName->get_name(); SgInitializer* initializer = initializedName->get_initptr(); SgAssignInitializer* assignInitializer = isSgAssignInitializer(initializer); if (assignInitializer != NULL) { SgExpression* exp = assignInitializer->get_operand(); ROSE_ASSERT(exp != NULL); SgFunctionCallExp* functionCallExp = isSgFunctionCallExp(exp); if (functionCallExp != NULL) { SgFunctionRefExp* functionRefExp = isSgFunctionRefExp(functionCallExp->get_function()); if (functionRefExp != NULL) { SgFunctionSymbol* functionSymbol = functionRefExp->get_symbol(); ROSE_ASSERT(functionSymbol != NULL); string functionName = functionSymbol->get_name(); #if 0 printf ("functionName = %s \n",functionName.c_str()); #endif if (functionName == "getZeros") { // We leverage the semantics of known functions used to initialize "Point" objects ("getZeros" initialized the Point object to be all zeros). // In a stencil this will be the center point from which all other points will have non-zero offsets. // For a common centered difference discretization this will be the center point of the stencil. #if 0 printf ("Identified and interpreting the semantics of getZeros() function \n"); #endif stencilOffsetFSM = new StencilOffsetFSM(0,0,0); ROSE_ASSERT(stencilOffsetFSM != NULL); } if (functionName == "getUnitv") { // We leverage the semantics of known functions used to initialize "Point" objects // ("getUnitv" initializes the Point object to be a unit vector for a specific input dimention). // In a stencil this will be an ofset from the center point. #if 0 printf ("Identified and interpreting the semantics of getUnitv() function \n"); #endif // Need to get the dimention argument. SgExprListExp* argumentList = functionCallExp->get_args(); ROSE_ASSERT(argumentList != NULL); // This function has a single argument. ROSE_ASSERT(argumentList->get_expressions().size() == 1); SgExpression* functionArg = argumentList->get_expressions()[0]; ROSE_ASSERT(functionArg != NULL); SgIntVal* intVal = isSgIntVal(functionArg); // ROSE_ASSERT(intVal != NULL); if (intVal != NULL) { int value = intVal->get_value(); #if 0 printf ("value = %d \n",value); #endif switch(value) { case 0: stencilOffsetFSM = new StencilOffsetFSM(1,0,0); break; case 1: stencilOffsetFSM = new StencilOffsetFSM(0,1,0); break; case 2: stencilOffsetFSM = new StencilOffsetFSM(0,0,1); break; default: { printf ("Error: default reached in switch: value = %d (for be value of 0, 1, or 2) \n",value); ROSE_ASSERT(false); } } ROSE_ASSERT(stencilOffsetFSM != NULL); // End of test for intVal != NULL } else { #if 0 printf ("functionArg = %p = %s \n",functionArg,functionArg->class_name().c_str()); #endif } } // ROSE_ASSERT(stencilOffsetFSM != NULL); } } } if (stencilOffsetFSM != NULL) { // Put the FSM into the map. #if 0 printf ("Put the stencilOffsetFSM = %p into the StencilOffsetMap using key = %s \n",stencilOffsetFSM,name.c_str()); #endif ROSE_ASSERT(StencilOffsetMap.find(name) == StencilOffsetMap.end()); // We have a choice of syntax to add the element to the map. // StencilOffsetMap.insert(pair<string,StencilOffsetFSM*>(name,stencilOffsetFSM)); StencilOffsetMap[name] = stencilOffsetFSM; } // new StencilOffsetFSM(); #if 0 printf ("Exiting as a test! \n"); ROSE_ASSERT(false); #endif } // Recognize member function calls on "Point" objects so that we can trigger events on those associated finite state machines. bool isTemplateClass = false; bool isTemplateFunctionInstantiation = false; SgInitializedName* initializedNameUsedToCallMemberFunction = NULL; SgFunctionCallExp* functionCallExp = detectMemberFunctionOfSpecificClassType(astNode,initializedNameUsedToCallMemberFunction,"Point",isTemplateClass,"operator*=",isTemplateFunctionInstantiation); if (functionCallExp != NULL) { // This is the DSL specific part (capturing the semantics of operator*= with specific integer values). // The name of the variable off of which the member function is called (variable has type "Point"). ROSE_ASSERT(initializedNameUsedToCallMemberFunction != NULL); string name = initializedNameUsedToCallMemberFunction->get_name(); // Need to get the dimention argument. SgExprListExp* argumentList = functionCallExp->get_args(); ROSE_ASSERT(argumentList != NULL); // This function has a single argument. ROSE_ASSERT(argumentList->get_expressions().size() == 1); SgExpression* functionArg = argumentList->get_expressions()[0]; ROSE_ASSERT(functionArg != NULL); SgIntVal* intVal = isSgIntVal(functionArg); bool usingUnaryMinus = false; if (intVal == NULL) { SgMinusOp* minusOp = isSgMinusOp(functionArg); if (minusOp != NULL) { #if 0 printf ("Using SgMinusOp on stencil constant \n"); #endif usingUnaryMinus = true; intVal = isSgIntVal(minusOp->get_operand()); } } ROSE_ASSERT(intVal != NULL); int value = intVal->get_value(); if (usingUnaryMinus == true) { value *= -1; } #if 0 printf ("value = %d \n",value); #endif // Look up the stencil offset finite state machine ROSE_ASSERT(StencilOffsetMap.find(name) != StencilOffsetMap.end()); StencilOffsetFSM* stencilOffsetFSM = StencilOffsetMap[name]; ROSE_ASSERT(stencilOffsetFSM != NULL); #if 0 printf ("We have found the StencilOffsetFSM associated with the StencilOffset named %s \n",name.c_str()); #endif #if 0 stencilOffsetFSM->display("before multiply event"); #endif if (value == -1) { // Execute the event on the finte state machine to accumulate the state. stencilOffsetFSM->operator*=(-1); } else { printf ("Error: constant value other than -1 are not supported \n"); ROSE_ASSERT(false); } #if 0 stencilOffsetFSM->display("after multiply event"); #endif } // Detection of "pair<Shift,double>(xdir,ident)" defined as an event in the stencil finite machine model. // Actually, it is the Stencil that is create using the "pair<Shift,double>(xdir,ident)" that should be the // event so we first detect the SgConstructorInitializer. There is not other code similar to this which // has to test for the template arguments, so this has not yet refactored into the dslSupport.C file. // I will do this later since this is general support that could be resused in other DSL compilers. SgConstructorInitializer* constructorInitializer = isSgConstructorInitializer(astNode); if (constructorInitializer != NULL) { // DQ (10/20/2014): This can sometimes be NULL. // ROSE_ASSERT(constructorInitializer->get_class_decl() != NULL); SgClassDeclaration* classDeclaration = constructorInitializer->get_class_decl(); // ROSE_ASSERT(classDeclaration != NULL); if (classDeclaration != NULL) { #if 0 printf ("constructorInitializer = %p class name = %s \n",constructorInitializer,classDeclaration->get_name().str()); #endif SgTemplateInstantiationDecl* templateInstantiationDecl = isSgTemplateInstantiationDecl(classDeclaration); // ROSE_ASSERT(templateInstantiationDecl != NULL); #if 0 if (templateInstantiationDecl != NULL) { printf ("constructorInitializer = %p name = %s template name = %s \n",constructorInitializer,templateInstantiationDecl->get_name().str(),templateInstantiationDecl->get_templateName().str()); } #endif // if (classDeclaration->get_name() == "pair") if (templateInstantiationDecl != NULL && templateInstantiationDecl->get_templateName() == "pair") { // Look at the template parameters. #if 0 printf ("Found template instantiation for pair \n"); #endif SgTemplateArgumentPtrList & templateArgs = templateInstantiationDecl->get_templateArguments(); if (templateArgs.size() == 2) { // Now look at the template arguments and check that they represent the pattern that we are looking for in the AST. // It is not clear now flexible we should be, at present shift/coeficent pairs must be specified exactly one way. SgType* type_0 = templateArgs[0]->get_type(); SgType* type_1 = templateArgs[1]->get_type(); if ( type_0 != NULL && type_1 != NULL) { SgClassType* classType_0 = isSgClassType(type_0); // ROSE_ASSERT(classType_0 != NULL); if (classType_0 != NULL) { SgClassDeclaration* classDeclarationType_0 = isSgClassDeclaration(classType_0->get_declaration()); ROSE_ASSERT(classDeclarationType_0 != NULL); #if 0 printf ("templateArgs[0]->get_name() = %s \n",classDeclarationType_0->get_name().str()); printf ("templateArgs[1]->get_type()->class_name() = %s \n",type_1->class_name().c_str()); #endif bool foundShiftExpression = false; bool foundStencilCoeficient = false; // We might want to be more flexiable about the type of the 2nd parameter (allow SgTypeFloat, SgTypeComplex, etc.). if (classDeclarationType_0->get_name() == "Shift" && type_1->variant() == V_SgTypeDouble) { // Found a pair<Shift,double> input for a stencil. #if 0 printf ("##### Found a pair<Shift,double>() input for a stencil input \n"); #endif // ***************************************************************************************************** // Look at the first parameter to the pair<Shift,double>() constructor. // ***************************************************************************************************** SgExpression* stencilOffset = constructorInitializer->get_args()->get_expressions()[0]; ROSE_ASSERT(stencilOffset != NULL); #if 0 printf ("stencilOffset = %p = %s \n",stencilOffset,stencilOffset->class_name().c_str()); #endif SgConstructorInitializer* stencilOffsetConstructorInitializer = isSgConstructorInitializer(stencilOffset); if (stencilOffsetConstructorInitializer != NULL) { // This is the case of a Shift being constructed implicitly from a Point (doing so more directly would be easier to make sense of in the AST). #if 0 printf ("!!!!! Looking for the stencil offset \n"); #endif ROSE_ASSERT(stencilOffsetConstructorInitializer->get_class_decl() != NULL); SgClassDeclaration* stencilOffsetClassDeclaration = stencilOffsetConstructorInitializer->get_class_decl(); ROSE_ASSERT(stencilOffsetClassDeclaration != NULL); #if 0 printf ("stencilOffsetConstructorInitializer = %p class name = %s \n",stencilOffsetConstructorInitializer,stencilOffsetClassDeclaration->get_name().str()); printf ("stencilOffsetConstructorInitializer = %p class = %p = %s \n",stencilOffsetConstructorInitializer,stencilOffsetClassDeclaration,stencilOffsetClassDeclaration->class_name().c_str()); #endif // This should not be a template instantiation (the Shift is defined to be a noo-template class declaration, not a template class declaration). SgTemplateInstantiationDecl* stencilOffsetTemplateInstantiationDecl = isSgTemplateInstantiationDecl(stencilOffsetClassDeclaration); ROSE_ASSERT(stencilOffsetTemplateInstantiationDecl == NULL); if (stencilOffsetClassDeclaration != NULL && stencilOffsetClassDeclaration->get_name() == "Shift") { // Now we know that the type associated with the first template parameter is associated with the class "Shift". // But we need so also now what the first parametr is associate with the constructor initializer, since it will // be the name of the variable used to interprete the stencil offset (and the name of the variable will be the // key into the map of finite machine models used to accumulate the state of the stencil offsets that we accumulate // to build the stencil. // Now we need the value of the input (computed using it's fine state machine). SgExpression* inputToShiftConstructor = stencilOffsetConstructorInitializer->get_args()->get_expressions()[0]; ROSE_ASSERT(inputToShiftConstructor != NULL); SgConstructorInitializer* inputToShiftConstructorInitializer = isSgConstructorInitializer(inputToShiftConstructor); if (stencilOffsetConstructorInitializer != NULL) { SgExpression* inputToPointConstructor = inputToShiftConstructorInitializer->get_args()->get_expressions()[0]; ROSE_ASSERT(inputToPointConstructor != NULL); // This should be a SgVarRefExp (if we strictly follow the stencil specification rules (which are not written down yet). SgVarRefExp* inputToPointVarRefExp = isSgVarRefExp(inputToPointConstructor); if (inputToPointVarRefExp != NULL) { #if 0 printf ("Found varRefExp in bottom of chain of constructors \n"); #endif SgVariableSymbol* variableSymbolForOffset = isSgVariableSymbol(inputToPointVarRefExp->get_symbol()); ROSE_ASSERT(variableSymbolForOffset != NULL); SgInitializedName* initializedNameForOffset = variableSymbolForOffset->get_declaration(); ROSE_ASSERT(initializedNameForOffset != NULL); SgInitializer* initializer = initializedNameForOffset->get_initptr(); ROSE_ASSERT(initializer != NULL); #if 0 printf ("Found initializedName: name = %s in bottom of chain of constructors: initializer = %p = %s \n",initializedNameForOffset->get_name().str(),initializer,initializer->class_name().c_str()); #endif // Record the name to be used as a key into the map of "StencilOffset" finite state machines. SgAssignInitializer* assignInitializer = isSgAssignInitializer(initializer); ROSE_ASSERT(assignInitializer != NULL); string name = initializedNameForOffset->get_name(); // Look up the current state in the finite state machine for the "Point". // Check that this is a previously defined stencil offset. ROSE_ASSERT(StencilOffsetMap.find(name) != StencilOffsetMap.end()); // StencilOffsetFSM* stencilOffsetFSM = StencilOffsetMap[name]; stencilOffsetFSM = StencilOffsetMap[name]; ROSE_ASSERT(stencilOffsetFSM != NULL); #if 0 printf ("We have found the StencilOffsetFSM associated with the StencilOffset named %s \n",name.c_str()); #endif #if 0 printf ("Exiting as a test! \n"); ROSE_ASSERT(false); #endif } else { printf ("What is this expression: inputToPointConstructor = %p = %s \n",inputToPointConstructor,inputToPointConstructor->class_name().c_str()); ROSE_ASSERT(false); } } #if 0 printf ("Found Shift type \n"); #endif foundShiftExpression = true; } #if 0 printf ("Exiting as a test! \n"); ROSE_ASSERT(false); #endif } else { // This case for the specification of a Shift in the first argument is not yet supported (need an example of this). printf ("This case of using a shift is not a part of what is supported \n"); } // ***************************************************************************************************** // Look at the second parameter to the pair<Shift,double>(first_parameter,second_parameter) constructor. // ***************************************************************************************************** SgExpression* stencilCoeficent = constructorInitializer->get_args()->get_expressions()[1]; ROSE_ASSERT(stencilCoeficent != NULL); SgVarRefExp* stencilCoeficentVarRefExp = isSgVarRefExp(stencilCoeficent); if (stencilCoeficentVarRefExp != NULL) { // Handle the case where this is a constant SgVarRefExp and the value is available in the declaration. SgVariableSymbol* variableSymbolForConstant = isSgVariableSymbol(stencilCoeficentVarRefExp->get_symbol()); ROSE_ASSERT(variableSymbolForConstant != NULL); SgInitializedName* initializedNameForConstant = variableSymbolForConstant->get_declaration(); ROSE_ASSERT(initializedNameForConstant != NULL); SgInitializer* initializer = initializedNameForConstant->get_initptr(); ROSE_ASSERT(initializer != NULL); SgAssignInitializer* assignInitializer = isSgAssignInitializer(initializer); ROSE_ASSERT(assignInitializer != NULL); SgValueExp* valueExp = isSgValueExp(assignInitializer->get_operand()); bool usingUnaryMinus = false; // ROSE_ASSERT(valueExp != NULL); if (valueExp == NULL) { SgExpression* operand = assignInitializer->get_operand(); SgMinusOp* minusOp = isSgMinusOp(operand); if (minusOp != NULL) { #if 0 printf ("Using SgMinusOp on stencil constant \n"); #endif usingUnaryMinus = true; valueExp = isSgValueExp(minusOp->get_operand()); } } SgDoubleVal* doubleVal = isSgDoubleVal(valueExp); // ROSE_ASSERT(doubleVal != NULL); double value = 0.0; if (doubleVal == NULL) { // Call JP's function to evaluate the constant expression. ROSE_ASSERT(valueExp == NULL); ROSE_ASSERT(stencilCoeficent != NULL); DSL_Support::const_numeric_expr_t const_expression = DSL_Support::evaluateConstNumericExpression(stencilCoeficent); if (const_expression.hasValue_ == true) { ROSE_ASSERT(const_expression.isIntOnly_ == false); value = const_expression.value_; printf ("const expression evaluated to value = %4.2f \n",value); } else { printf ("constnat value expression could not be evaluated to a constant \n"); ROSE_ASSERT(false); } } else { #if 1 printf ("SgDoubleVal value = %f \n",doubleVal->get_value()); #endif value = (usingUnaryMinus == false) ? doubleVal->get_value() : -(doubleVal->get_value()); } #if 1 printf ("Stencil coeficient = %f \n",value); #endif foundStencilCoeficient = true; stencilCoeficientValue = value; } else { // When we turn on constant folding in the frontend we eveluate directly to a SgDoubleVal. SgDoubleVal* doubleVal = isSgDoubleVal(stencilCoeficent); if (doubleVal != NULL) { ROSE_ASSERT(doubleVal != NULL); #if 0 printf ("SgDoubleVal value = %f \n",doubleVal->get_value()); #endif double value = doubleVal->get_value(); #if 0 printf ("Stencil coeficient = %f \n",value); #endif foundStencilCoeficient = true; stencilCoeficientValue = value; } else { printf ("Error: second parameter in pair for stencil is not a SgVarRefExp (might be explicit value not yet supported) \n"); printf (" --- stencilCoeficent = %p = %s \n",stencilCoeficent,stencilCoeficent->class_name().c_str()); ROSE_ASSERT(false); } } } #if 0 printf ("foundShiftExpression = %s \n",foundShiftExpression ? "true" : "false"); printf ("foundStencilCoeficient = %s \n",foundStencilCoeficient ? "true" : "false"); #endif if (foundShiftExpression == true && foundStencilCoeficient == true) { #if 0 printf ("Found pair<Shift,double>() constructor expression! \n"); #endif foundPairShiftDoubleConstructor = true; } // End of test for classType_0 != NULL } } } } else { #if 0 printf ("This is not a SgConstructorInitializer for the pair templated class \n"); #endif } // End of test for classDeclaration != NULL } } #if 0 printf ("foundPairShiftDoubleConstructor = %s \n",foundPairShiftDoubleConstructor ? "true" : "false"); #endif if (foundPairShiftDoubleConstructor == true) { // This is the recognition of an event for one of the finite state machines we implement to evaluate the stencil at compile time. #if 0 printf ("In evaluateInheritedAttribute(): found pair<Shift,double>() constructor expression! \n"); printf (" --- stencilOffsetFSM = %p \n",stencilOffsetFSM); printf (" --- stencilCoeficientValue = %f \n",stencilCoeficientValue); #endif ROSE_ASSERT(stencilOffsetFSM != NULL); inheritedAttribute.stencilOffsetFSM = stencilOffsetFSM; inheritedAttribute.stencilCoeficientValue = stencilCoeficientValue; #if 0 printf ("Exiting as a test! \n"); ROSE_ASSERT(false); #endif } // Construct the return attribute from the modified input attribute. return StencilEvaluation_InheritedAttribute(inheritedAttribute); }
void FortranProgramDeclarationsAndDefinitions::visit (SgNode * node) { using boost::filesystem::path; using boost::filesystem::system_complete; using boost::iequals; using boost::starts_with; using boost::lexical_cast; using std::string; if (isSgSourceFile (node)) { path p = system_complete (path (isSgSourceFile (node)->getFileName ())); currentSourceFile = p.filename (); Debug::getInstance ()->debugMessage ("Source file '" + currentSourceFile + "' detected", Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__ ); } else if (Globals::getInstance ()->isInputFile (currentSourceFile)) { /* * ====================================================== * Only process this portion of the AST if we recognise * this source file as one passed on the command line. In * Fortran, .rmod files are sometimes generated whose * traversal should be avoided * ====================================================== */ switch (node->variantT ()) { case V_SgModuleStatement: { SgModuleStatement * moduleStatement = isSgModuleStatement (node); currentModuleName = moduleStatement->get_name ().getString (); fileNameToModuleNames[currentSourceFile].push_back (currentModuleName); moduleNameToFileName[currentModuleName] = currentSourceFile; Debug::getInstance ()->debugMessage ("Module '" + currentModuleName + "' in file '" + currentSourceFile + "'", Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__ ); break; } case V_SgProcedureHeaderStatement: { /* * ====================================================== * We need to store all subroutine definitions since we * later have to copy and modify the user kernel subroutine * ====================================================== */ SgProcedureHeaderStatement * procedureHeaderStatement = isSgProcedureHeaderStatement (node); string const subroutineName = procedureHeaderStatement->get_name ().getString (); subroutinesInSourceCode[subroutineName] = procedureHeaderStatement; ROSE_ASSERT (currentModuleName.size() > 0); moduleNameToSubroutines[currentModuleName].push_back (subroutineName); subroutineToFileName[subroutineName] = currentSourceFile; Debug::getInstance ()->debugMessage ( "Found procedure header statement '" + procedureHeaderStatement->get_name ().getString () + "' in file '" + currentSourceFile + "', and module '" + currentModuleName + "'", Debug::FUNCTION_LEVEL, __FILE__, __LINE__); break; } case V_SgFunctionCallExp: { /* * ====================================================== * Function call found in the AST. Get its actual arguments * and the callee name * ====================================================== */ SgFunctionCallExp * functionCallExp = isSgFunctionCallExp (node); SgExprListExp * actualArguments = functionCallExp->get_args (); string const calleeName = functionCallExp->getAssociatedFunctionSymbol ()->get_name ().getString (); Debug::getInstance ()->debugMessage ("Found function call '" + calleeName + "'", Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__); if ( iequals (calleeName, OP2::OP_DECL_SET) || iequals (calleeName, OP2::OP_DECL_SET_HDF5) ) { /* * ====================================================== * An OP_SET variable declared through an OP_DECL_SET call * ====================================================== */ bool isHDF5Format = false; if ( iequals (calleeName, OP2::OP_DECL_SET_HDF5) ) isHDF5Format = true; FortranOpSetDefinition * opSetDeclaration = new FortranOpSetDefinition (actualArguments, isHDF5Format); OpSetDefinitions[opSetDeclaration->getVariableName ()] = opSetDeclaration; } else if ( iequals (calleeName, OP2::OP_DECL_MAP) || iequals (calleeName, OP2::OP_DECL_MAP_HDF5) ) { /* * ====================================================== * An OP_MAP variable declared through an OP_DECL_MAP call * ====================================================== */ bool isHDF5Format = false; if ( iequals (calleeName, OP2::OP_DECL_MAP_HDF5) ) { isHDF5Format = true; Debug::getInstance ()->debugMessage ("This is the HDF5 version: '" + calleeName + "'", Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__); } FortranOpMapDefinition * opMapDeclaration = new FortranOpMapDefinition (actualArguments, isHDF5Format); OpMapDefinitions[opMapDeclaration->getVariableName ()] = opMapDeclaration; } else if ( iequals (calleeName, OP2::OP_DECL_DAT) || iequals (calleeName, OP2::OP_DECL_DAT_HDF5) ) { /* * ====================================================== * An OP_DAT variable declared through an OP_DECL_DAT call * ====================================================== */ bool isHDF5Format = false; if ( iequals (calleeName, OP2::OP_DECL_DAT_HDF5) ) isHDF5Format = true; FortranOpDatDefinition * opDatDeclaration = new FortranOpDatDefinition (actualArguments, isHDF5Format); OpDatDefinitions[opDatDeclaration->getVariableName ()] = opDatDeclaration; } else if (iequals (calleeName, OP2::OP_DECL_CONST)) { /* * ====================================================== * A constant declared through an OP_DECL_CONST call * ====================================================== */ FortranOpConstDefinition * opConstDeclaration = new FortranOpConstDefinition (actualArguments, functionCallExp); OpConstDefinitions[opConstDeclaration->getVariableName ()] = opConstDeclaration; } else if (starts_with (calleeName, OP2::OP_PAR_LOOP)) { /* * ====================================================== * The first argument to an 'OP_PAR_LOOP' call should be * a reference to the kernel function. Cast it and proceed, * otherwise throw an exception * ====================================================== */ SgExprListExp * actualArguments = functionCallExp->get_args (); SgFunctionRefExp * functionRefExpression = isSgFunctionRefExp ( actualArguments->get_expressions ().front ()); ROSE_ASSERT (functionRefExpression != NULL); string const userSubroutineName = functionRefExpression->getAssociatedFunctionDeclaration ()->get_name ().getString (); Debug::getInstance ()->debugMessage ("Found '" + calleeName + "' with (host) user subroutine '" + userSubroutineName + "'", Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__); if (parallelLoops.find (userSubroutineName) == parallelLoops.end ()) { int numberOfOpArgs = getLoopSuffixNumber ( calleeName ); /* * ====================================================== * If this kernel has not been previously encountered then * build a new parallel loop representation * ====================================================== */ FortranParallelLoop * parallelLoop = new FortranParallelLoop ( functionCallExp); parallelLoop->addFileName (currentSourceFile); parallelLoops[userSubroutineName] = parallelLoop; Debug::getInstance ()->debugMessage ("Parallel loop with '" + lexical_cast <string> (numberOfOpArgs) + "' arguments", Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__); analyseParallelLoopArguments (parallelLoop, actualArguments, numberOfOpArgs); parallelLoop->checkArguments (); parallelLoop->setIncrementalID (IDCounter); IDCounter++; } else { Debug::getInstance ()->debugMessage ("Parallel loop for '" + userSubroutineName + "' already created", Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__); ParallelLoop * parallelLoop = parallelLoops[userSubroutineName]; parallelLoop->addFunctionCallExpression (functionCallExp); parallelLoop->addFileName (currentSourceFile); } } break; } default: { break; } } } }
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__); }
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, ¶ms, (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; }
//Generates SSA form numbers for the variables contained in *ex and attaches them as AstValueAttributes to the related SgNodes //Assumption: *ex is located in in the inTrueBranch branch of the if node labeled *condLabel (These two arguments are required to generate the SSA form numbers) void SSAGenerator::processSgExpression(SgExpression* ex, Label* condLabel, bool inTrueBranch) { SgIntVal* intVal = dynamic_cast<SgIntVal*>(ex); SgMinusOp* minusOp = dynamic_cast<SgMinusOp*>(ex); SgVarRefExp* varRef = dynamic_cast<SgVarRefExp*>(ex); SgBinaryOp* binOp = dynamic_cast<SgBinaryOp*>(ex); SgFunctionCallExp* funcCall = dynamic_cast<SgFunctionCallExp*>(ex); if(intVal) //Int value { //Nothing needs to be done; Case is listed here to have a collection of all expected cases } else if(minusOp) { processSgExpression(minusOp->get_operand(), condLabel, inTrueBranch); } else if(varRef) //Reference to variable that is NOT on the left hand side of an assignment { //Assign number to variable string varName = varRef->get_symbol()->get_name().getString(); int varNumber = currentNumber(varName, condLabel, inTrueBranch); logger[Sawyer::Message::DEBUG] << "Current number for variable " << varName << ": " << varNumber << endl; AstValueAttribute<int>* varNumberAtt = new AstValueAttribute<int>(varNumber); varRef->setAttribute("SSA_NUMBER", varNumberAtt); } else if(binOp) //Binary operation { SgExpression* lhs = binOp->get_lhs_operand(); SgExpression* rhs = binOp->get_rhs_operand(); //Process right hand side first processSgExpression(rhs, condLabel, inTrueBranch); //Process left hand side second SgAssignOp* assignOp = dynamic_cast<SgAssignOp*>(binOp); if(assignOp) //Assignment to a variable { //Assign new number to that variable SgVarRefExp* lhsVarRef = dynamic_cast<SgVarRefExp*>(lhs); assert(lhsVarRef != NULL); processAssignmentTo(lhsVarRef, condLabel, inTrueBranch); } else //Arithmetic operation or boolean operation (or something unexpected) { processSgExpression(lhs, condLabel, inTrueBranch); } } else if(funcCall) //Call to a function //RERS specific; Only two function call types are supported: //(1) scanf("%d",&...); //(2) __VERIFIER_error(RERSVerifierErrorNumber); The artificial bool variable RERSErrorOccured has to be updated { string funcName = funcCall->getAssociatedFunctionSymbol()->get_name().getString(); logger[Sawyer::Message::DEBUG] << "Call to function: " << funcName << endl; if(funcName == "scanf") //(1) { SgExprListExp* funcArgs = funcCall->get_args(); SgExpressionPtrList funcArgsPtrs = funcArgs->get_expressions(); SgExpressionPtrList::iterator i = funcArgsPtrs.begin(); while(i != funcArgsPtrs.end()) { SgAddressOfOp* addrOp = dynamic_cast<SgAddressOfOp*>(*i); if(addrOp) { SgVarRefExp* varRef = dynamic_cast<SgVarRefExp*>(addrOp->get_operand()); if(varRef) { processAssignmentTo(varRef, condLabel, inTrueBranch); } else logger[Sawyer::Message::DEBUG] << "FOUND NO REFERENCE TO VARIABLE" << endl; } i++; } } else if(funcName == "__VERIFIER_error" && prepareReachabilityAnalysisZ3) { SgExprListExp* funcArgs = funcCall->get_args(); SgExpressionPtrList funcArgsPtrs = funcArgs->get_expressions(); assert(funcArgsPtrs.size() == 1); SgExpression* argument = *funcArgsPtrs.begin(); SgIntVal* intArgument = dynamic_cast<SgIntVal*>(argument); assert(intArgument != NULL); if(intArgument->get_value() == RERSVerifierErrorNumber) //(2) { int RERSErrorOccuredNumber = nextNumber("RERSErrorOccured", condLabel, inTrueBranch); logger[Sawyer::Message::DEBUG] << "Next number for variable RERSErrorOccured: " << RERSErrorOccuredNumber << endl; AstValueAttribute<int>* numberAtt = new AstValueAttribute<int>(RERSErrorOccuredNumber); funcCall->setAttribute("SSA_NUMBER", numberAtt); } } else logger[Sawyer::Message::DEBUG] << "Ignoring function call" << endl; } else //Unexpected { logger[Sawyer::Message::ERROR] << "ERROR: SgExpression could not be handled: " << ex->class_name() << endl; assert(false); } }
std::string getSgInitializedName(SgInitializedName* initName) { std::string exprStr; SgSymbol* initNameSym = initName->search_for_symbol_from_symbol_table(); std::string varInit = initializeVariable(initName); std::string retString; if (initName->get_initptr() != NULL) { SgInitializer* nameInitializer = initName->get_initializer(); VariantT var = nameInitializer->variantT(); switch (var) { case V_SgAggregateInitializer: { SgAggregateInitializer* aggInit = isSgAggregateInitializer(nameInitializer); if (!isSgArrayType(aggInit->get_type())) { std::cout << "currently only arrays use aggregate initializers, you are using " << aggInit->class_name() << std::endl; ROSE_ASSERT(false); } SgExprListExp* members = aggInit->get_initializers(); SgExpressionPtrList member_expressions = members->get_expressions(); std::string symName = SymbolToZ3[initNameSym]; ROSE_ASSERT(SymbolToInstances[initNameSym] == 0); int arrmem = 0; std::stringstream exprStream; for (SgExpressionPtrList::iterator i = member_expressions.begin(); i != member_expressions.end(); i++) { exprStream << "\n(assert (= (select " << symName << "_0 " << arrmem << ") " << getSgExpressionString((isSgAssignInitializer((*i))->get_operand())) << ")"; arrmem = arrmem+1; } retString = varInit + "\n" + exprStream.str(); #ifdef ARRAY_TEST std::cout << "retString: " << retString << std::endl; #endif break; } case V_SgCompoundInitializer: { std::cout << "SgCompoundInitializer not yet supported" << std::endl; ROSE_ASSERT(false); break; } case V_SgConstructorInitializer: { std::cout << "SgConstructorInitializer is not yet supported" << std::endl; ROSE_ASSERT(false); break; } case V_SgDesignatedInitializer: { std::cout << "SgDesignatedInitializer is not yet supported" << std::endl; ROSE_ASSERT(false); break; } case V_SgAssignInitializer: { SgAssignInitializer* assignInit = isSgAssignInitializer(nameInitializer); std::string symName = SymbolToZ3[initNameSym]; ROSE_ASSERT(SymbolToInstances[initNameSym] == 0); exprStr = "(assert (= " + symName + "_0 " + getSgExpressionString(assignInit->get_operand()) + "))"; retString = varInit + "\n" + exprStr; break; } default: { std::cout << "unknown initializer of type: " << nameInitializer->class_name() << std::endl; ROSE_ASSERT(false); break; } } } else { retString = varInit; } return retString; }
void generateStencilCode(StencilEvaluationTraversal & traversal, bool generateLowlevelCode) { // Read the stencil and generate the inner most loop AST for the stencil. // Note that generateLowlevelCode controls the generation of low level C code using a // base pointer to raw memory and linearized indexing off of that pointer. The // alternative is to use the operator[] member function in the RectMDArray class. // Example of code that we want to generate: // for (j=0; j < source.size(0); j++) // { // int axis_x_size = source.size(0); // for (i=0; i < source.size(0); i++) // { // destination[j*axis_x_size+i] = source[j*axis_x_size+i]; // } // } // This function genertes the loop nest only: // SgForStatement* buildLoopNest(int stencilDimension, SgBasicBlock* & innerLoopBody) // This function generates the statement in the inner most loop body: // SgExprStatement* assembleStencilSubTreeArray(vector<SgExpression*> & stencilSubTreeArray) // This function generates the AST representing the stencil points: // SgExpression* buildStencilPoint (StencilOffsetFSM* stencilOffsetFSM, double stencilCoeficient, int stencilDimension, SgVariableSymbol* destinationVariableSymbol, SgVariableSymbol* sourceVariableSymbol) // The generated code should be in terms of the operator[]() functions on the // RectMDArray objects. Likely we have to support a wider range of generated code later. // const RectMDArray<TDest>& a_LOfPhi, // const RectMDArray<TSrc>& a_phi, // std::vector<SgFunctionCallExp*> stencilOperatorFunctionCallList; std::vector<SgFunctionCallExp*> stencilOperatorFunctionCallList = traversal.get_stencilOperatorFunctionCallList(); for (size_t i = 0; i < stencilOperatorFunctionCallList.size(); i++) { SgFunctionCallExp* functionCallExp = stencilOperatorFunctionCallList[i]; ROSE_ASSERT(functionCallExp != NULL); printf ("processing functionCallExp = %p \n",functionCallExp); SgStatement* associatedStatement = TransformationSupport::getStatement(functionCallExp); ROSE_ASSERT(associatedStatement != NULL); string filename = associatedStatement->get_file_info()->get_filename(); int lineNumber = associatedStatement->get_file_info()->get_line(); printf ("Generating code for stencil operator used at file = %s at line = %d \n",filename.c_str(),lineNumber); SgExprListExp* argumentList = functionCallExp->get_args(); ROSE_ASSERT(argumentList != NULL); // There should be four elements to a stencil operator. ROSE_ASSERT(argumentList->get_expressions().size() == 4); // Stencil SgExpression* stencilExpression = argumentList->get_expressions()[0]; SgVarRefExp* stencilVarRefExp = isSgVarRefExp(stencilExpression); ROSE_ASSERT(stencilVarRefExp != NULL); // RectMDArray (destination) SgExpression* destinationArrayReferenceExpression = argumentList->get_expressions()[1]; SgVarRefExp* destinationArrayVarRefExp = isSgVarRefExp(destinationArrayReferenceExpression); ROSE_ASSERT(destinationArrayVarRefExp != NULL); // RectMDArray (source) SgExpression* sourceArrayReferenceExpression = argumentList->get_expressions()[2]; SgVarRefExp* sourceArrayVarRefExp = isSgVarRefExp(sourceArrayReferenceExpression); ROSE_ASSERT(sourceArrayVarRefExp != NULL); // Box SgExpression* boxReferenceExpression = argumentList->get_expressions()[3]; SgVarRefExp* boxVarRefExp = isSgVarRefExp(boxReferenceExpression); ROSE_ASSERT(boxVarRefExp != NULL); printf ("DONE: processing inputs to stencil operator \n"); ROSE_ASSERT(stencilVarRefExp->get_symbol() != NULL); SgInitializedName* stencilInitializedName = stencilVarRefExp->get_symbol()->get_declaration(); ROSE_ASSERT(stencilInitializedName != NULL); string stencilName = stencilInitializedName->get_name(); printf ("stencilName = %s \n",stencilName.c_str()); std::map<std::string,StencilFSM*> & stencilMap = traversal.get_stencilMap(); ROSE_ASSERT(stencilMap.find(stencilName) != stencilMap.end()); StencilFSM* stencilFSM = stencilMap[stencilName]; ROSE_ASSERT(stencilFSM != NULL); // DQ (2/8/2015): Moved out of loop. int stencilDimension = stencilFSM->stencilDimension(); ROSE_ASSERT(stencilDimension > 0); // These are computed values. printf ("Stencil dimension = %d \n",stencilDimension); printf ("Stencil width = %d \n",stencilFSM->stencilWidth()); std::vector<std::pair<StencilOffsetFSM,double> > & stencilPointList = stencilFSM->stencilPointList; // This is the scope where the stencil operator is evaluated. SgScopeStatement* outerScope = associatedStatement->get_scope(); ROSE_ASSERT(outerScope != NULL); SgVariableSymbol* indexVariableSymbol_X = NULL; SgVariableSymbol* indexVariableSymbol_Y = NULL; SgVariableSymbol* indexVariableSymbol_Z = NULL; SgVariableSymbol* arraySizeVariableSymbol_X = NULL; SgVariableSymbol* arraySizeVariableSymbol_Y = NULL; SgVariableSymbol* destinationVariableSymbol = destinationArrayVarRefExp->get_symbol(); ROSE_ASSERT(destinationVariableSymbol != NULL); SgVariableSymbol* sourceVariableSymbol = sourceArrayVarRefExp->get_symbol(); ROSE_ASSERT(sourceVariableSymbol != NULL); SgVariableSymbol* boxVariableSymbol = boxVarRefExp->get_symbol(); ROSE_ASSERT(boxVariableSymbol != NULL); // This can be important in handling of comments and CPP directives. bool autoMovePreprocessingInfo = true; SgStatement* lastStatement = associatedStatement; if (generateLowlevelCode == true) { #if 1 SgVariableDeclaration* sourceDataPointerVariableDeclaration = buildDataPointer("sourceDataPointer",sourceVariableSymbol,outerScope); #else // Optionally build a pointer variable so that we can optionally support a C style indexing for the DTEC DSL blocks. SgExpression* sourcePointerExp = buildMemberFunctionCall(sourceVariableSymbol,"getPointer",NULL,false); ROSE_ASSERT(sourcePointerExp != NULL); SgAssignInitializer* assignInitializer = SageBuilder::buildAssignInitializer_nfi(sourcePointerExp); ROSE_ASSERT(assignInitializer != NULL); // Build the variable declaration for the pointer to the data. string sourcePointerName = "sourceDataPointer"; SgVariableDeclaration* sourceDataPointerVariableDeclaration = SageBuilder::buildVariableDeclaration_nfi(sourcePointerName,SageBuilder::buildPointerType(SageBuilder::buildDoubleType()),assignInitializer,outerScope); ROSE_ASSERT(sourceDataPointerVariableDeclaration != NULL); #endif // SageInterface::insertStatementAfter(associatedStatement,forStatementScope,autoMovePreprocessingInfo); SageInterface::insertStatementAfter(associatedStatement,sourceDataPointerVariableDeclaration,autoMovePreprocessingInfo); SgVariableDeclaration* destinationDataPointerVariableDeclaration = buildDataPointer("destinationDataPointer",destinationVariableSymbol,outerScope); SageInterface::insertStatementAfter(sourceDataPointerVariableDeclaration,destinationDataPointerVariableDeclaration,autoMovePreprocessingInfo); // Reset the variable symbols we will use in the buildStencilPoint() function. sourceVariableSymbol = SageInterface::getFirstVarSym(sourceDataPointerVariableDeclaration); destinationVariableSymbol = SageInterface::getFirstVarSym(destinationDataPointerVariableDeclaration); lastStatement = destinationDataPointerVariableDeclaration; } SgBasicBlock* innerLoopBody = NULL; // SgForStatement* loopNest = buildLoopNest(stencilFSM->stencilDimension(),innerLoopBody,sourceVariableSymbol,indexVariableSymbol_X,indexVariableSymbol_Y,indexVariableSymbol_Z,arraySizeVariableSymbol_X,arraySizeVariableSymbol_Y); SgForStatement* loopNest = buildLoopNest(stencilFSM->stencilDimension(),innerLoopBody,boxVariableSymbol,indexVariableSymbol_X,indexVariableSymbol_Y,indexVariableSymbol_Z,arraySizeVariableSymbol_X,arraySizeVariableSymbol_Y); ROSE_ASSERT(innerLoopBody != NULL); ROSE_ASSERT(lastStatement != NULL); SageInterface::insertStatementAfter(lastStatement,loopNest,autoMovePreprocessingInfo); // Mark this as compiler generated so that it will not be unparsed. associatedStatement->get_file_info()->setCompilerGenerated(); // Form an array of AST subtrees to represent the different points in the stencil. // vector<SgFunctionCallExp*> stencilSubTreeArray; vector<SgExpression*> stencilSubTreeArray; for (size_t j = 0; j < stencilPointList.size(); j++) { #if 0 printf ("Forming stencil point subtree for offsetValues[0] = %3d [1] = %3d [2] = %3d \n",stencilPointList[j].first.offsetValues[0],stencilPointList[j].first.offsetValues[1],stencilPointList[j].first.offsetValues[2]); #endif StencilOffsetFSM* stencilOffsetFSM = &(stencilPointList[j].first); double stencilCoeficient = stencilPointList[j].second; // SgFunctionCallExp* stencilSubTree = buildStencilPoint(stencilOffsetFSM,stencilCoeficient,stencilFSM->stencilDimension()); SgExpression* stencilSubTree = buildStencilPoint(stencilOffsetFSM,stencilCoeficient,stencilDimension,sourceVariableSymbol, indexVariableSymbol_X,indexVariableSymbol_Y,indexVariableSymbol_Z,arraySizeVariableSymbol_X,arraySizeVariableSymbol_Y,generateLowlevelCode); ROSE_ASSERT(stencilSubTree != NULL); stencilSubTreeArray.push_back(stencilSubTree); } // Construct the lhs value for the stencil inner loop statement. StencilOffsetFSM* stencilOffsetFSM_lhs = new StencilOffsetFSM(0,0,0); double stencilCoeficient_lhs = 1.00; SgExpression* stencil_lhs = buildStencilPoint(stencilOffsetFSM_lhs,stencilCoeficient_lhs,stencilDimension,destinationVariableSymbol, indexVariableSymbol_X,indexVariableSymbol_Y,indexVariableSymbol_Z,arraySizeVariableSymbol_X,arraySizeVariableSymbol_Y,generateLowlevelCode); ROSE_ASSERT(stencil_lhs != NULL); // Assemble the stencilSubTreeArray into a single expression. SgExprStatement* stencilStatement = assembleStencilSubTreeArray(stencil_lhs,stencilSubTreeArray,stencilDimension,destinationVariableSymbol); SageInterface::appendStatement(stencilStatement,innerLoopBody); } }
void Fortran_to_C::linearizeArraySubscript(SgPntrArrRefExp* pntrArrRefExp) { // get lhs operand SgVarRefExp* arrayName = isSgVarRefExp(pntrArrRefExp->get_lhs_operand()); // get array symbol SgVariableSymbol* arraySymbol = arrayName->get_symbol(); // get array type and dim_info SgArrayType* arrayType = isSgArrayType(arraySymbol->get_type()); ROSE_ASSERT(arrayType); SgExprListExp* dimInfo = arrayType->get_dim_info(); // get rhs operand SgExprListExp* arraySubscript = isSgExprListExp(pntrArrRefExp->get_rhs_operand()); /* No matter it is single or multi dimensional array, pntrArrRefExp always has a child, SgExprListExp, to store the subscript information. */ if(arrayType->findBaseType()->variantT() == V_SgTypeString) { arraySubscript->prepend_expression(buildIntVal(1)); } if(arraySubscript != NULL) { // get the list of subscript SgExpressionPtrList subscriptExprList = arraySubscript->get_expressions(); // get the list of dimension inforamtion from array definition. SgExpressionPtrList dimExpressionPtrList = dimInfo->get_expressions(); // Create new SgExpressionPtrList for the linearalized array subscript. SgExpressionPtrList newSubscriptExprList; // rank info has to match between subscripts and dim_info ROSE_ASSERT(arraySubscript->get_expressions().size() == dimInfo->get_expressions().size()); /* The subscript conversion is following this example: case 1: dimension a(d1,d2,d3,d4) ====> dimension a(d1*d2*d3*d4) a(s1,s2,s3,s4) ====> a(s1-1 + d1*(s2-1 + d2*( s3-1 + d3*(s4-1)))) case 2: dimension a(d1L:d1H,d2L:d2H) ====> dimension a((d1H-d1L+1)*(d2H-d2L+1)) a(s1,s2) ====> a(s1-d1L + (d1H-d1L+1)*(s2-d2L)) */ Rose_STL_Container<SgExpression*>::reverse_iterator j1 = subscriptExprList.rbegin(); Rose_STL_Container<SgExpression*>::reverse_iterator j2 = dimExpressionPtrList.rbegin(); // Need to know current size of both current and previous dimension SgExpression* newSubscript; while((j1 != subscriptExprList.rend()) && (j2 != dimExpressionPtrList.rend())) { // get the lowerBound for each dimension SgExpression* newDimIndex; SgExpression* dimSize; /* get the dimension size at each dimension */ SgSubscriptExpression* subscriptExpression = isSgSubscriptExpression(*j2); /* This is for the 1st type of array declaration: a(10,15,20) Fortran is 1-based array. Lowerbound is 1 by default. */ if(subscriptExpression == NULL) { dimSize = deepCopy(*j2); } /* This is for the 2nd type of array declaration: a(1:10,5:15,10:20) Actual dimension size = upperBound - lowerBound + 1 */ else { dimSize = buildAddOp(buildSubtractOp(deepCopy(subscriptExpression->get_upperBound()), deepCopy(subscriptExpression->get_lowerBound())), buildIntVal(1)); } // convert the 1-based subscript to 0-based subscript newDimIndex = get0basedIndex(*j1, *j2); if(j1 != subscriptExprList.rbegin()) { newSubscript = buildAddOp(newDimIndex, buildMultiplyOp(dimSize,newSubscript)); } else { newSubscript = newDimIndex; delete(dimSize); } ++j1; ++j2; } // end of while loop newSubscriptExprList.push_back(newSubscript); SgExprListExp* newSubscriptList = buildExprListExp(newSubscriptExprList); // un-link and remove the rhs operand pntrArrRefExp->get_rhs_operand()->set_parent(NULL); removeList.push_back(pntrArrRefExp->get_rhs_operand()); // add the new subscriptExpression into rhs operand pntrArrRefExp->set_rhs_operand(newSubscriptList); newSubscriptList->set_parent(pntrArrRefExp); } // end of arraySubscript != NULL }
void Fortran_to_C::translateArraySubscript(SgPntrArrRefExp* pntrArrRefExp) { // get lhs operand SgVarRefExp* arrayName = isSgVarRefExp(pntrArrRefExp->get_lhs_operand()); SgExpression* baseExp = isSgExpression(arrayName); // get array symbol SgVariableSymbol* arraySymbol = arrayName->get_symbol(); // get array type and dim_info SgArrayType* arrayType = isSgArrayType(arraySymbol->get_type()); ROSE_ASSERT(arrayType); SgExprListExp* dimInfo = arrayType->get_dim_info(); // get rhs operand SgExprListExp* arraySubscript = isSgExprListExp(pntrArrRefExp->get_rhs_operand()); if(arrayType->findBaseType()->variantT() == V_SgTypeString) { arraySubscript->prepend_expression(buildIntVal(1)); } /* No matter it is single or multi dimensional array, pntrArrRefExp always has a child, SgExprListExp, to store the subscript information. */ if(arraySubscript != NULL) { // get the list of subscript SgExpressionPtrList subscriptExprList = arraySubscript->get_expressions(); // get the list of dimension inforamtion from array definition. SgExpressionPtrList dimExpressionPtrList = dimInfo->get_expressions(); // Create new SgExpressionPtrList for the linearalized array subscript. SgExpressionPtrList newSubscriptExprList; // rank info has to match between subscripts and dim_info ROSE_ASSERT(arraySubscript->get_expressions().size() == dimInfo->get_expressions().size()); if(subscriptExprList.size() == 1) { Rose_STL_Container<SgExpression*>::iterator j1 = subscriptExprList.begin(); Rose_STL_Container<SgExpression*>::iterator j2 = dimExpressionPtrList.begin(); SgExpression* newIndexExp = get0basedIndex(*j1, *j2); pntrArrRefExp->set_rhs_operand(newIndexExp); } else { Rose_STL_Container<SgExpression*>::reverse_iterator j1 = subscriptExprList.rbegin(); Rose_STL_Container<SgExpression*>::reverse_iterator j2 = dimExpressionPtrList.rbegin(); SgExpression* newIndexExp = get0basedIndex(*j1, *j2); SgPntrArrRefExp* newPntrArrRefExp = buildPntrArrRefExp(baseExp, newIndexExp); baseExp->set_parent(newPntrArrRefExp); j1 = j1 + 1; j2 = j2 + 1; for(; j1< (subscriptExprList.rend()-1); ++j1, ++j2) { SgExpression* newIndexExp = get0basedIndex(*j1, *j2); baseExp = isSgExpression(newPntrArrRefExp); newPntrArrRefExp = buildPntrArrRefExp(baseExp, newIndexExp); baseExp->set_parent(newPntrArrRefExp); } newIndexExp = get0basedIndex(*j1, *j2); pntrArrRefExp->set_lhs_operand(newPntrArrRefExp); pntrArrRefExp->set_rhs_operand(newIndexExp); newIndexExp->set_parent(pntrArrRefExp); } } }
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"); } */ } }
void mlmTransform::transformCallExp(SgCallExpression* callExp) { ROSE_ASSERT(callExp); SgFunctionRefExp* funcName = isSgFunctionRefExp(callExp->get_function()); if(!funcName) return; SgExprListExp* funcArgList = callExp->get_args(); SgExpressionPtrList argList = funcArgList->get_expressions(); SgScopeStatement* scope = getScope(callExp); //cout << funcName->get_symbol()->get_name() << endl; /** if it is malloc, search for the mlm attribute and append the memory level **/ if(strncmp("malloc",funcName->get_symbol()->get_name().str(),6) == 0) { if(argList.size() != 1) return; SgExprListExp* funcArgList = callExp->get_args(); // check if LHS of malloc has an attribute assigned SgNode* parentNode = callExp->get_parent(); // parent node can be a casting expression if(isSgCastExp(parentNode)) { parentNode = parentNode->get_parent(); } // the mlm attribute AstAttribute* attr = NULL; // So far we spot two candidates for parentNode that we need to transform if(isSgAssignOp(parentNode)) { SgAssignOp* assignOp = isSgAssignOp(parentNode); SgExpression* lhs = isSgExpression(assignOp->get_lhs_operand()); if(!isSgVarRefExp(lhs)) { //cout << "lhs:" << assignOp->get_lhs_operand()->class_name() << endl; // if pointer is packaged inside a struct, then we need to look down in lhs. if(isSgDotExp(lhs)) { lhs = isSgDotExp(lhs)->get_rhs_operand(); } } SgVarRefExp* lhsVarRef = isSgVarRefExp(lhs); ROSE_ASSERT(lhsVarRef); SgSymbol* symbol = lhsVarRef->get_symbol(); ROSE_ASSERT(symbol); //retrieve the attribute from symbol attr = symbol->getAttribute("mlmAttribute"); //cout << "LHS symbol name: " << symbol->get_name() << endl; } else if(isSgAssignInitializer(parentNode)) { SgInitializedName* initName = isSgInitializedName(parentNode->get_parent()); ROSE_ASSERT(initName); SgSymbol* symbol = initName->get_symbol_from_symbol_table(); if(!symbol) return; ROSE_ASSERT(symbol); //retrieve the attribute from symbol attr = symbol->getAttribute("mlmAttribute"); //cout << "Initialized symbol name: " << symbol->get_name() << endl; } else { // do nothing because no attribute assigned or we always set to default } // if there is a mlm attribute attached to the symbol, then create new malloc if(attr) { mlmAttribute* mlmAttr = dynamic_cast<mlmAttribute*> (attr); SgExprListExp* funcArgList = callExp->get_args(); funcArgList->append_expression(buildIntVal(mlmAttr->getMemType())); replaceExpression(callExp, buildFunctionCallExp("mlm_malloc",deepCopy(callExp->get_type()),deepCopy(funcArgList),getScope(callExp))); } } else if(strncmp("memcpy",funcName->get_symbol()->get_name().str(),6) == 0) { // cout << "replacing memcpy" << endl; if(argList.size() != 3) return; Rose_STL_Container<SgNode*> varList = NodeQuery::querySubTree(funcArgList, V_SgVarRefExp); SgVarRefExp* dst = isSgVarRefExp(varList[0]); SgVarRefExp* src = isSgVarRefExp(varList[1]); AstAttribute* attrDst = dst->get_symbol()->getAttribute("mlmAttribute"); AstAttribute* attrSrc = src->get_symbol()->getAttribute("mlmAttribute"); mlmAttribute* mlmAttrDst = dynamic_cast<mlmAttribute*>(attrDst); mlmAttribute* mlmAttrSrc = dynamic_cast<mlmAttribute*>(attrSrc); // if((mlmAttrDst && !mlmAttrSrc) || (mlmAttrDst && mlmAttrSrc && (mlmAttrDst->getMemType() < mlmAttrDst->getMemType()))) // { // replaceExpression(callExp, buildFunctionCallExp("mlm_memcpy",deepCopy(callExp->get_type()),deepCopy(funcArgList),scope),true); // DeletepragmasList2.push_back(callExp); // } // // else if((!mlmAttrDst && mlmAttrSrc) || (mlmAttrDst && mlmAttrSrc && (mlmAttrDst->getMemType() > mlmAttrDst->getMemType()))) // 09/30/14 Following Simon's suggestion, we always insert wait for the mlm_memcpy // { string tagName = generateUniqueVariableName(scope,"copy_tag"); SgVariableDeclaration* newDecl = buildVariableDeclaration(tagName, buildOpaqueType("mlm_Tag",getGlobalScope(callExp)), buildAssignInitializer(buildFunctionCallExp("mlm_memcpy",deepCopy(callExp->get_type()),deepCopy(funcArgList),scope))); SgExprStatement* waitStmt = buildFunctionCallStmt("mlm_waitComplete", buildVoidType(), buildExprListExp(buildVarRefExp(tagName,scope)), scope); insertStatement(getEnclosingStatement(callExp),newDecl,true); insertStatement(getEnclosingStatement(callExp),waitStmt,true); removeStatement(getEnclosingStatement(callExp)); // } } else if(strncmp("free",funcName->get_symbol()->get_name().str(),4) == 0) { // cout << "replacing free" << endl; if(argList.size() != 1) return; SgExpression* varExp = isSgExpression(argList[0]); //cout << "exp:" << varExp->class_name() << endl; if(!isSgVarRefExp(varExp)) { if(isSgCastExp(varExp)) { varExp = isSgCastExp(varExp)->get_operand_i(); } // if pointer is packaged inside a struct, then we need to look down in lhs. if(isSgDotExp(varExp)) { varExp = isSgDotExp(varExp)->get_rhs_operand(); } } SgVarRefExp* varRef = isSgVarRefExp(varExp); ROSE_ASSERT(varRef); AstAttribute* attr = varRef->get_symbol()->getAttribute("mlmAttribute"); if(attr) { replaceExpression(callExp, buildFunctionCallExp("mlm_free",deepCopy(callExp->get_type()),deepCopy(funcArgList),scope),false); } } }
void SystemDependenceGraph::build() { boost::unordered_map<CFGVertex, Vertex> cfgVerticesToSdgVertices; boost::unordered_map<SgNode*, Vertex> astNodesToSdgVertices; //map<SgFunctionCallExp*, vector<SDGNode*> > funcCallToArgs; vector<CallSiteInfo> functionCalls; map<SgNode*, vector<Vertex> > actualInParameters; map<SgNode*, vector<Vertex> > actualOutParameters; map<SgNode*, Vertex> formalInParameters; map<SgNode*, Vertex> formalOutParameters; vector<SgFunctionDefinition*> funcDefs = SageInterface::querySubTree<SgFunctionDefinition>(project_, V_SgFunctionDefinition); foreach (SgFunctionDefinition* funcDef, funcDefs) { SgFunctionDeclaration* funcDecl = funcDef->get_declaration(); CFG* cfg = new CFG(funcDef, cfgNodefilter_); functionsToCFGs_[funcDecl] = cfg; // For each function, build an entry node for it. SDGNode* entry = new SDGNode(SDGNode::Entry); entry->astNode = funcDef; //entry->funcDef = funcDef; Vertex entryVertex = addVertex(entry); functionsToEntries_[funcDecl] = entryVertex; // Add all out formal parameters to SDG. const SgInitializedNamePtrList& formalArgs = funcDecl->get_args(); foreach (SgInitializedName* initName, formalArgs) { // If the parameter is passed by reference, create a formal-out node. if (isParaPassedByRef(initName->get_type())) { SDGNode* formalOutNode = new SDGNode(SDGNode::FormalOut); formalOutNode->astNode = initName; Vertex formalOutVertex = addVertex(formalOutNode); formalOutParameters[initName] = formalOutVertex; // Add a CD edge from call node to this formal-out node. addTrueCDEdge(entryVertex, formalOutVertex); } } // A vertex representing the returned value. Vertex returnVertex; // If the function returns something, build a formal-out node. if (!isSgTypeVoid(funcDecl->get_type()->get_return_type())) { SDGNode* formalOutNode = new SDGNode(SDGNode::FormalOut); // Assign the function declaration to the AST node of this vertex to make // it possible to classify this node into the subgraph of this function. formalOutNode->astNode = funcDecl; returnVertex = addVertex(formalOutNode); formalOutParameters[funcDecl] = returnVertex; // Add a CD edge from call node to this formal-out node. addTrueCDEdge(entryVertex, returnVertex); } // Add all CFG vertices to SDG. foreach (CFGVertex cfgVertex, boost::vertices(*cfg)) { if (cfgVertex == cfg->getEntry() || cfgVertex == cfg->getExit()) continue; SgNode* astNode = (*cfg)[cfgVertex]->getNode(); // If this node is an initialized name and it is a parameter, make it // as a formal in node. SgInitializedName* initName = isSgInitializedName(astNode); if (initName && isSgFunctionParameterList(initName->get_parent())) { SDGNode* formalInNode = new SDGNode(SDGNode::FormalIn); formalInNode->astNode = initName; Vertex formalInVertex = addVertex(formalInNode); formalInParameters[initName] = formalInVertex; cfgVerticesToSdgVertices[cfgVertex] = formalInVertex; astNodesToSdgVertices[astNode] = formalInVertex; // Add a CD edge from call node to this formal-in node. addTrueCDEdge(entryVertex, formalInVertex); continue; } // Add a new node to SDG. SDGNode* newSdgNode = new SDGNode(SDGNode::ASTNode); //newSdgNode->cfgNode = (*cfg)[cfgVertex]; newSdgNode->astNode = astNode; Vertex sdgVertex = addVertex(newSdgNode); cfgVerticesToSdgVertices[cfgVertex] = sdgVertex; astNodesToSdgVertices[astNode] = sdgVertex; // Connect a vertex containing the return statement to the formal-out return vertex. if (isSgReturnStmt(astNode) || isSgReturnStmt(astNode->get_parent())) { SDGEdge* newEdge = new SDGEdge(SDGEdge::DataDependence); addEdge(sdgVertex, returnVertex, newEdge); } // If this CFG node contains a function call expression, extract its all parameters // and make them as actual-in nodes. if (SgFunctionCallExp* funcCallExpr = isSgFunctionCallExp(astNode)) { CallSiteInfo callInfo; callInfo.funcCall = funcCallExpr; callInfo.vertex = sdgVertex; // Change the node type. newSdgNode->type = SDGNode::FunctionCall; vector<SDGNode*> argsNodes; // Get the associated function declaration. SgFunctionDeclaration* funcDecl = funcCallExpr->getAssociatedFunctionDeclaration(); if (funcDecl == NULL) continue; ROSE_ASSERT(funcDecl); const SgInitializedNamePtrList& formalArgs = funcDecl->get_args(); SgExprListExp* args = funcCallExpr->get_args(); const SgExpressionPtrList& actualArgs = args->get_expressions(); if (formalArgs.size() != actualArgs.size()) { cout << "The following function has variadic arguments:\n"; cout << funcDecl->get_file_info()->get_filename() << endl; cout << funcDecl->get_name() << formalArgs.size() << " " << actualArgs.size() << endl; continue; } for (int i = 0, s = actualArgs.size(); i < s; ++i) { // Make sure that this parameter node is added to SDG then we // change its node type from normal AST node to a ActualIn arg. ROSE_ASSERT(astNodesToSdgVertices.count(actualArgs[i])); Vertex paraInVertex = astNodesToSdgVertices.at(actualArgs[i]); SDGNode* paraInNode = (*this)[paraInVertex]; paraInNode->type = SDGNode::ActualIn; actualInParameters[formalArgs[i]].push_back(paraInVertex); callInfo.inPara.push_back(paraInVertex); // Add a CD edge from call node to this actual-in node. addTrueCDEdge(sdgVertex, paraInVertex); // If the parameter is passed by reference, create a parameter-out node. if (isParaPassedByRef(formalArgs[i]->get_type())) { SDGNode* paraOutNode = new SDGNode(SDGNode::ActualOut); paraOutNode->astNode = actualArgs[i]; //argsNodes.push_back(paraInNode); // Add an actual-out parameter node. Vertex paraOutVertex = addVertex(paraOutNode); actualOutParameters[formalArgs[i]].push_back(paraOutVertex); callInfo.outPara.push_back(paraOutVertex); // Add a CD edge from call node to this actual-out node. addTrueCDEdge(sdgVertex, paraOutVertex); } } if (!isSgTypeVoid(funcDecl->get_type()->get_return_type())) { // If this function returns a value, create a actual-out vertex. SDGNode* paraOutNode = new SDGNode(SDGNode::ActualOut); paraOutNode->astNode = funcCallExpr; // Add an actual-out parameter node. Vertex paraOutVertex = addVertex(paraOutNode); actualOutParameters[funcDecl].push_back(paraOutVertex); callInfo.outPara.push_back(paraOutVertex); callInfo.isVoid = false; callInfo.returned = paraOutVertex; // Add a CD edge from call node to this actual-out node. addTrueCDEdge(sdgVertex, paraOutVertex); } functionCalls.push_back(callInfo); //funcCallToArgs[funcCallExpr] = argsNodes; } } // Add control dependence edges. addControlDependenceEdges(cfgVerticesToSdgVertices, *cfg, entryVertex); }
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.
// first visits the VarRef and then creates entry in operandDatabase which is // useful in expressionStatement transformation. Thus, we replace the VarRef // at the end of the traversal and insert loops during traversal ArrayAssignmentStatementQuerySynthesizedAttributeType ArrayAssignmentStatementTransformation::evaluateSynthesizedAttribute( SgNode* astNode, ArrayAssignmentStatementQueryInheritedAttributeType arrayAssignmentStatementQueryInheritedData, SubTreeSynthesizedAttributes synthesizedAttributeList) { // This function assembles the elements of the input list (a list of char*) to form the output (a single char*) #if DEBUG printf ("\n$$$$$ TOP of evaluateSynthesizedAttribute (astNode = %s) (synthesizedAttributeList.size() = %d) \n", astNode->sage_class_name(),synthesizedAttributeList.size()); //cout << " Ast node string: " << astNode->unparseToString() << endl; #endif // Build the return value for this function ArrayAssignmentStatementQuerySynthesizedAttributeType returnSynthesizedAttribute(astNode); // Iterator used within several error checking loops (not sure we should declare it here!) vector<ArrayAssignmentStatementQuerySynthesizedAttributeType>::iterator i; // Make a reference to the global operand database OperandDataBaseType & operandDataBase = accumulatorValue.operandDataBase; // Make sure the data base has been setup properly ROSE_ASSERT(operandDataBase.transformationOption > ArrayTransformationSupport::UnknownIndexingAccess); ROSE_ASSERT(operandDataBase.dimension > -1); // Build up a return string string returnString = ""; string operatorString; // Need to handle all unary and binary operators and variables (but not much else) switch (astNode->variant()) { case FUNC_CALL: { // Error checking: Verify that we have a SgFunctionCallExp object SgFunctionCallExp* functionCallExpression = isSgFunctionCallExp(astNode); ROSE_ASSERT(functionCallExpression != NULL); string operatorName = TransformationSupport::getFunctionName(functionCallExpression); ROSE_ASSERT(operatorName.c_str() != NULL); string functionTypeName = TransformationSupport::getFunctionTypeName(functionCallExpression); if ((functionTypeName != "doubleArray") && (functionTypeName != "floatArray") && (functionTypeName != "intArray")) { // Use this query to handle only A++ function call expressions // printf ("Break out of overloaded operator processing since type = %s is not to be processed \n",functionTypeName.c_str()); break; } else { // printf ("Processing overloaded operator of type = %s \n",functionTypeName.c_str()); } ROSE_ASSERT((functionTypeName == "doubleArray") || (functionTypeName == "floatArray") || (functionTypeName == "intArray")); // printf ("CASE FUNC_CALL: Overloaded operator = %s \n",operatorName.c_str()); // Get the number of parameters to this function SgExprListExp* exprListExp = functionCallExpression->get_args(); ROSE_ASSERT(exprListExp != NULL); SgExpressionPtrList & expressionPtrList = exprListExp->get_expressions(); int numberOfParameters = expressionPtrList.size(); TransformationSupport::operatorCodeType operatorCodeVariant = TransformationSupport::classifyOverloadedOperator(operatorName.c_str(), numberOfParameters); // printf ("CASE FUNC_CALL: numberOfParameters = %d operatorCodeVariant = %d \n", // numberOfParameters,operatorCodeVariant); ROSE_ASSERT(operatorName.length() > 0); // Separating this case into additional cases makes up to some // extent for using a more specific higher level grammar. switch (operatorCodeVariant) { case TransformationSupport::ASSIGN_OPERATOR_CODE: { vector<ArrayOperandDataBase>::iterator lhs = operandDataBase.arrayOperandList.begin(); vector<ArrayOperandDataBase>::iterator rhs = lhs; rhs++; while (rhs != operandDataBase.arrayOperandList.end()) { // look at the operands on the rhs for a match with the one on the lhs if ((*lhs).arrayVariableName == (*rhs).arrayVariableName) { // A loop dependence has been identified // Mark the synthesized attribute to record // the loop dependence within this statement returnSynthesizedAttribute.setLoopDependence(TRUE); } rhs++; } break; } default: break; } break; } case EXPR_STMT: { printf("Found a EXPR STMT expression %s\n", astNode->unparseToString().c_str()); // The assembly associated with the SgExprStatement is what // triggers the generation of the transformation string SgExprStatement* expressionStatement = isSgExprStatement(astNode); ROSE_ASSERT(expressionStatement != NULL); ArrayAssignmentStatementQuerySynthesizedAttributeType innerLoopTransformation = synthesizedAttributeList[SgExprStatement_expression]; // Call another global support // Create appropriate macros, nested loops, etc expressionStatementTransformation(expressionStatement, arrayAssignmentStatementQueryInheritedData, innerLoopTransformation, operandDataBase); break; } // case TransformationSupport::PARENTHESIS_OPERATOR_CODE: { // ROSE_ASSERT (operatorName == "operator()"); // // printf ("Indexing of InternalIndex objects in not implemented yet! \n"); // // // Now get the operands out and search for the offsets in the index objects // // // We only want to pass on the transformationOptions as inherited attributes // // to the indexOffsetQuery // // list<int> & transformationOptionList = arrayAssignmentStatementQueryInheritedData.getTransformationOptions(); // // // string offsetString; // string indexOffsetString[6]; // = {NULL,NULL,NULL,NULL,NULL,NULL}; // // // retrieve the variable name from the data base (so that we can add the associated index object names) // // printf ("WARNING (WHICH OPERAND TO SELECT): operandDataBase.size() = %d \n",operandDataBase.size()); // // ROSE_ASSERT (operandDataBase.size() == 1); // // string arrayVariableName = returnSynthesizedAttribute.arrayOperandList[0].arrayVariableName; // int lastOperandInDataBase = operandDataBase.size() - 1; // ArrayOperandDataBase & arrayOperandDB = operandDataBase.arrayOperandList[lastOperandInDataBase]; // // string arrayVariableName = // // operandDataBase.arrayOperandList[operandDataBase.size()-1].arrayVariableName; // string arrayVariableName = arrayOperandDB.arrayVariableName; // // string arrayDataPointerNameSubstring = string("_") + arrayVariableName; // // // printf ("***** WARNING: Need to get identifier from the database using the ArrayOperandDataBase::generateIdentifierString() function \n"); // // if (expressionPtrList.size() == 0) { // // Case of A() (index object with no offset integer expression) Nothing to do here (I think???) // printf("Special case of Indexing with no offset! exiting ... \n"); // ROSE_ABORT(); // // returnString = ""; // } else { // // Get the value of the offsets (start the search from the functionCallExp) // SgExprListExp* exprListExp = functionCallExpression->get_args(); // ROSE_ASSERT (exprListExp != NULL); // // SgExpressionPtrList & expressionPtrList = exprListExp->get_expressions(); // SgExpressionPtrList::iterator i = expressionPtrList.begin(); // // // Case of indexing objects used within operator() // int counter = 0; // while (i != expressionPtrList.end()) { // // printf ("Looking for the offset on #%d of %d (total) \n",counter,expressionPtrList.size()); // // // Build up the name of the final index variable (or at least give // // it a unique number by dimension) // string counterString = StringUtility::numberToString(counter + 1); // // // Call another transformation mechanism to generate string for the index // // expression (since we don't have an unparser mechanism in ROSE yet) // indexOffsetString[counter] = IndexOffsetQuery::transformation(*i); // // ROSE_ASSERT (indexOffsetString[counter].c_str() != NULL); // // printf ("indexOffsetString [%d] = %s \n",counter,indexOffsetString[counter].c_str()); // // // Accumulate a list of all the InternalIndex, Index, and Range objects // printf(" Warning - Need to handle indexNameList from the older code \n"); // // i++; // counter++; // } // Added VAR_REF case (moved from the local function) case VAR_REF: { // A VAR_REF has to output a string (the variable name) #if DEBUG printf ("Found a variable reference expression \n"); #endif // Since we are at a leaf in the traversal of the AST this attribute list should a size of 0. ROSE_ASSERT(synthesizedAttributeList.size() == 0); SgVarRefExp* varRefExp = isSgVarRefExp(astNode); ROSE_ASSERT(varRefExp != NULL); SgVariableSymbol* variableSymbol = varRefExp->get_symbol(); ROSE_ASSERT(variableSymbol != NULL); SgInitializedName* initializedName = variableSymbol->get_declaration(); ROSE_ASSERT(initializedName != NULL); SgName variableName = initializedName->get_name(); string buffer; string indexOffsetString; // Now compute the offset to the index objects (form a special query for this???) SgType* type = variableSymbol->get_type(); ROSE_ASSERT(type != NULL); string typeName = TransformationSupport::getTypeName(type); ROSE_ASSERT(typeName.c_str() != NULL); // Recognize only these types at present if (typeName == "intArray" || typeName == "floatArray" || typeName == "doubleArray") { // Only define the variable name if we are using an object of array type // Copy the string from the SgName object to a string object string variableNameString = variableName.str(); #if DEBUG printf("Handle case of A++ array object VariableName: %s \n", variableNameString.c_str()); #endif if (arrayAssignmentStatementQueryInheritedData.arrayStatementDimensionDefined == TRUE) { cout << " Dim: " << arrayAssignmentStatementQueryInheritedData.arrayStatementDimension << endl; // The the globally computed array dimension from the arrayAssignmentStatementQueryInheritedData dimensionList.push_back(arrayAssignmentStatementQueryInheritedData.arrayStatementDimension); } else { dimensionList.push_back(6); // Default dimension for A++/P++ } nodeList.push_back(isSgExpression(varRefExp)); //processArrayRefExp(varRefExp, arrayAssignmentStatementQueryInheritedData); // Setup an intry in the synthesized attribute data base for this variable any // future results from analysis could be place there at this point as well // record the name in the synthesized attribute ROSE_ASSERT(operandDataBase.transformationOption > ArrayTransformationSupport::UnknownIndexingAccess); ArrayOperandDataBase arrayOperandDB = operandDataBase.setVariableName(variableNameString); } break; } default: { break; } } // End of main switch statement #if DEBUG printf ("$$$$$ BOTTOM of arrayAssignmentStatementAssembly::evaluateSynthesizedAttribute (astNode = %s) \n",astNode->sage_class_name()); printf (" BOTTOM: returnString = \n%s \n",returnString.c_str()); #endif return returnSynthesizedAttribute; }
void UnparseFortran_type::unparseArrayType(SgType* type, SgUnparse_Info& info, bool printDim) { // Examples: // real, dimension(10, 10) :: A1, A2 // real, dimension(:) :: B1 // character(len=*) :: s1 #if 0 curprint ("\n! Inside of UnparserFort::unparseArrayType \n"); #endif SgArrayType* array_type = isSgArrayType(type); ROSE_ASSERT(array_type != NULL); // I think that supressStrippedTypeName() and SkipBaseType() are redundant... if (info.supressStrippedTypeName() == false) { // DQ (1/16/2011): We only want to output the name of the stripped type once! SgType* stripType = array_type->stripType(); unparseType(stripType, info); info.set_supressStrippedTypeName(); } // DQ (8/5/2010): It is an error to treat an array of char as a string (see test2010_16.f90). #if 0 // dimension information SgExprListExp* dim = array_type->get_dim_info(); // if (isCharType(array_type->get_base_type())) // if (false && isCharType(array_type->get_base_type())) if (isCharType(array_type->get_base_type())) { // a character type: must be treated specially ROSE_ASSERT(array_type->get_rank() == 1); curprint("(len="); SgExpressionPtrList::iterator it = dim->get_expressions().begin(); if (it != dim->get_expressions().end()) { SgExpression* expr = *it; if (expr->variantT() == V_SgSubscriptExpression) { // this is a subscript expression but all we want to unparse is the length // of the string, which should be the upper bound of the subscript expression SgSubscriptExpression* sub_expr = isSgSubscriptExpression(expr); ROSE_ASSERT(sub_expr != NULL); ROSE_ASSERT(unp != NULL); ROSE_ASSERT(unp->u_fortran_locatedNode != NULL); unp->u_fortran_locatedNode->unparseExpression(sub_expr->get_upperBound(), info); } else { // unparse the entire expression ROSE_ASSERT(unp != NULL); ROSE_ASSERT(unp->u_fortran_locatedNode != NULL); unp->u_fortran_locatedNode->unparseExpression(*it, info); } } else { curprint("*"); } curprint(")"); } else { // a non-character type // explicit-shape (explicit rank and bounds/extents) // assumed-shape (explicit rank; unspecified bounds/extents) // deferred-shape (explicit rank; unspecified bounds/extents) // assumed-size (explicit ranks, explicit bounds/extents except last dim) ROSE_ASSERT(array_type->get_rank() >= 1); curprint(", DIMENSION"); ROSE_ASSERT(unp != NULL); ROSE_ASSERT(unp->u_fortran_locatedNode != NULL); // unp->u_fortran_locatedNode->unparseExprList(dim, info); // adds parens // unp->u_fortran_locatedNode->UnparseLanguageIndependentConstructs::unparseExprList(dim, info); // adds parens // curprint("("); // curprint( StringUtility::numberToString(array_type->get_rank()) ); // curprint(")"); // unp->u_fortran_locatedNode->unparseExpression(array_type->get_dim_info(),info); unp->u_fortran_locatedNode->unparseExprList(array_type->get_dim_info(),info,/* output parens */ true); } #else if (printDim) { ROSE_ASSERT(array_type->get_rank() >= 1); curprint(array_type->get_isCoArray()? ", CODIMENSION": ", DIMENSION"); ROSE_ASSERT(unp != NULL); ROSE_ASSERT(unp->u_fortran_locatedNode != NULL); if (array_type->get_isCoArray()) { // print codimension info curprint("["); unp->u_fortran_locatedNode->unparseExprList(array_type->get_dim_info(),info,/* do not output parens */ false); curprint("]"); } else // print dimension info unp->u_fortran_locatedNode->unparseExprList(array_type->get_dim_info(),info,/* output parens */ true); } // DQ (1/16/2011): Plus unparse the base type...(unless it will just output the stripped types name). if (array_type->get_base_type()->containsInternalTypes() == true) { unparseType(array_type->get_base_type(), info, printDim); } #endif #if 0 curprint ("\n! Leaving UnparserFort::unparseArrayType \n"); #endif }
int main( int argc, char* argv[] ) { // Initialize and check compatibility. See rose::initialize ROSE_INITIALIZE; SgProject* project = frontend(argc,argv); AstTests::runAllTests(project); #if 0 // Output the graph so that we can see the whole AST graph, for debugging. generateAstGraph(project, 4000); #endif #if 1 printf ("Generate the dot output of the SAGE III AST \n"); generateDOT ( *project ); printf ("DONE: Generate the dot output of the SAGE III AST \n"); #endif // There are lots of way to write this, this is one simple approach; get all the function calls. std::vector<SgNode*> functionCalls = NodeQuery::querySubTree (project,V_SgFunctionCallExp); // Find the SgFunctionSymbol for snprintf so that we can reset references to "sprintf" to "snprintf" instead. // SgGlobal* globalScope = (*project)[0]->get_globalScope(); SgSourceFile* sourceFile = isSgSourceFile(project->get_fileList()[0]); ROSE_ASSERT(sourceFile != NULL); SgGlobal* globalScope = sourceFile->get_globalScope(); SgFunctionSymbol* snprintf_functionSymbol = globalScope->lookup_function_symbol("snprintf"); ROSE_ASSERT(snprintf_functionSymbol != NULL); // Iterate over the function calls to find the calls to "sprintf" for (unsigned long i = 0; i < functionCalls.size(); i++) { SgFunctionCallExp* functionCallExp = isSgFunctionCallExp(functionCalls[i]); ROSE_ASSERT(functionCallExp != NULL); SgFunctionRefExp* functionRefExp = isSgFunctionRefExp(functionCallExp->get_function()); if (functionRefExp != NULL) { SgFunctionSymbol* functionSymbol = functionRefExp->get_symbol(); if (functionSymbol != NULL) { SgName functionName = functionSymbol->get_name(); // printf ("Function being called: %s \n",functionName.str()); if (functionName == "sprintf") { // Now we have something to do! functionRefExp->set_symbol(snprintf_functionSymbol); // Now add the "n" argument SgExprListExp* functionArguments = functionCallExp->get_args(); SgExpressionPtrList & functionArgumentList = functionArguments->get_expressions(); // "sprintf" shuld have exactly 2 arguments (I guess the "..." don't count) printf ("functionArgumentList.size() = %zu \n",functionArgumentList.size()); // ROSE_ASSERT(functionArgumentList.size() == 2); SgExpressionPtrList::iterator i = functionArgumentList.begin(); // printf ("(*i) = %p = %s = %s \n",*i,(*i)->class_name().c_str(),SageInterface::get_name(*i).c_str()); SgVarRefExp* variableRefExp = isSgVarRefExp(*i); ROSE_ASSERT(variableRefExp != NULL); // printf ("variableRefExp->get_type() = %p = %s = %s \n",variableRefExp->get_type(),variableRefExp->get_type()->class_name().c_str(),SageInterface::get_name(variableRefExp->get_type()).c_str()); SgType* bufferType = variableRefExp->get_type(); SgExpression* bufferLengthExpression = NULL; switch(bufferType->variantT()) { case V_SgArrayType: { SgArrayType* arrayType = isSgArrayType(bufferType); bufferLengthExpression = arrayType->get_index(); break; } case V_SgPointerType: { // SgPointerType* pointerType = isSgPointerType(bufferType); SgInitializedName* variableDeclaration = variableRefExp->get_symbol()->get_declaration(); ROSE_ASSERT(variableDeclaration != NULL); SgExpression* initializer = variableDeclaration->get_initializer(); if (initializer != NULL) { SgAssignInitializer* assignmentInitializer = isSgAssignInitializer(initializer); ROSE_ASSERT(assignmentInitializer != NULL); // This is the rhs of the initialization of the pointer (likely a malloc through a cast). // This assumes: buffer = (char*) malloc(bufferLengthExpression); SgExpression* initializationExpression = assignmentInitializer->get_operand(); ROSE_ASSERT(initializationExpression != NULL); SgCastExp* castExp = isSgCastExp(initializationExpression); ROSE_ASSERT(castExp != NULL); SgFunctionCallExp* functionCall = isSgFunctionCallExp(castExp->get_operand()); ROSE_ASSERT(functionCall != NULL); SgExprListExp* functionArguments = isSgExprListExp(functionCall->get_args()); bufferLengthExpression = functionArguments->get_expressions()[0]; ROSE_ASSERT(bufferLengthExpression != NULL); } else { printf ("Initializer not found, so no value for n in snprintf can be computed currently \n"); } break; } default: { printf ("Error: default reached in evaluation of buffer type = %p = %s \n",bufferType,bufferType->class_name().c_str()); ROSE_ASSERT(false); } } ROSE_ASSERT(bufferLengthExpression != NULL); // printf ("bufferLengthExpression = %p = %s = %s \n",bufferLengthExpression,bufferLengthExpression->class_name().c_str(),SageInterface::get_name(bufferLengthExpression).c_str()); // Jump over the first argument, the "n" is defined to be the 2nd argument (the rest are shifted one position). i++; // Build a deep copy of the expression used to define the static buffer (could be any complex expression). SgTreeCopy copy_help; SgExpression* bufferLengthExpression_copy = isSgExpression(bufferLengthExpression->copy(copy_help)); // Insert the "n" for the parameter list to work with "snprintf" instead of "sprintf" functionArgumentList.insert(i,bufferLengthExpression_copy); } } } } return backend(project); }