//! Convert a variable list to x,y,z ,without parenthesis. std::string FailSafe::Attribute::toFailSafeString(std::vector<std::pair<std::string,SgNode* > >var_list) { string result; std::vector<std::pair<std::string,SgNode* > >::iterator iter; for (iter=var_list.begin();iter!=var_list.end();iter++) { if (iter != var_list.begin()) result +=","; result+=(*iter).first; // For map (a[0:n], b[0:m],c), a variable may have optional list of dimension information SgInitializedName* initname = isSgInitializedName((*iter).second); if (initname != NULL) { SgVariableSymbol * sym = isSgVariableSymbol(initname->get_symbol_from_symbol_table()); ROSE_ASSERT (sym != NULL); #if 0 //TODO: we should have similar syntax for array variables also for ( std::vector < std::pair <SgExpression*, SgExpression*> >::const_iterator citer = dims.begin(); citer!= dims.end(); citer ++) { result+="["; std::pair <SgExpression*, SgExpression*> c_pair = (*citer); result += c_pair.first->unparseToString(); result += ":"; result += c_pair.second->unparseToString(); result+="]"; } #endif } } return result; }
SgSymbol* my_search_for_symbol_from_symbol_table(SgInitializedName* initname) const { SgSymbol *symbol; SgInitializedName* aPrevDeclItem = p_prev_decl_item; while(aPrevDeclItem != NULL && aPrevDeclItem->p_prev_decl_item != NULL) { // DQ (11/19/2011): This loop will not terminate if (aPrevDeclItem->p_prev_decl_item == aPrevDeclItem). // This should not happen but this mistake has happened and this assertion helps it be caught instead // of be an infinite loop. ROSE_ASSERT(aPrevDeclItem->p_prev_decl_item != aPrevDeclItem); aPrevDeclItem = aPrevDeclItem->p_prev_decl_item; } if (aPrevDeclItem != NULL) symbol = aPrevDeclItem->get_symbol_from_symbol_table(); else symbol = get_symbol_from_symbol_table(); assert(symbol != NULL); return symbol; }
void mlmFrontend::attachAttribute(SgPragmaDeclaration* pragma, AstAttribute* attr) { // attribute to specify memory level if(isSgVariableDeclaration(getNextStatement(pragma))) { SgVariableDeclaration* decl = isSgVariableDeclaration(getNextStatement(pragma)); ROSE_ASSERT(decl); SgInitializedNamePtrList nameList = decl->get_variables(); if(nameList.size() == 1) { SgInitializedName* initName = nameList[0]; SgSymbol* symbol = initName->get_symbol_from_symbol_table(); symbol->setAttribute("mlmAttribute", attr); } } // attribute to specify tiling level else if(isSgForStatement(getNextStatement(pragma))) { SgForStatement* forStmt = isSgForStatement(getNextStatement(pragma)); ROSE_ASSERT(forStmt); forStmt->setAttribute("mlmAttribute", attr); } }
ExprSynAttr *examineVariableDeclaration(SgVariableDeclaration* decl, ostream &out) { SgInitializedNamePtrList& name_list = decl->get_variables(); SgInitializedNamePtrList::const_iterator name_iter; ExprSynAttr *ret = NULL; ExprSynAttr *gc = NULL; ret = new ExprSynAttr(); for (name_iter = name_list.begin(); name_iter != name_list.end(); name_iter++) { SgInitializedName* name = *name_iter; SgSymbol* symbol = name->get_symbol_from_symbol_table(); SgType *type = symbol->get_type(); int nr_stars = 0; stringstream ss1; while (isSgArrayType(type) || isSgPointerType(type)) { if (isSgArrayType(type)) { SgArrayType *atype = isSgArrayType(type); SgExpression *expr = atype->get_index(); type = atype->get_base_type(); ss1 << "["; if (expr) examineExpr(expr, ss1); ss1 << "]"; } else { SgPointerType *ttype = isSgPointerType(type); type = ttype->get_base_type(); nr_stars++; } } examinePrimTypeName(type, ret->code); ret->code << " "; for (int i = 0; i < nr_stars; ++i) ret->code << "*"; ret->code << symbol->get_name().getString(); ret->code << ss1.str(); ss1.str(""); SgInitializer *initer = name->get_initializer(); if (initer) { switch (initer->variantT()) { case V_SgAssignInitializer: SgAssignInitializer *ai = isSgAssignInitializer(initer); SgExpression *expr = ai->get_operand(); if (expr) { ret->code << "="; gc = examineExpr(expr, ret->code); if (gc != NULL) delete gc; } break; default: break; } } /* end of this decl */ ret->code << ";"; out << ret->code.str(); return ret; /* cout << "[Decl] Variable (name:"<<symbol->get_name().getString(); cout << ",type:"<<symbol->get_type()->class_name(); cout << ",init:"; SgInitializer* init_expr = name->get_initializer(); if (init_expr) cout << init_expr->class_name(); else cout << "none"; cout << ")" << endl; */ } }
// Convert something like "int a = foo();" into "int a; a = foo();" SgAssignOp* convertInitializerIntoAssignment(SgAssignInitializer* init) { #ifndef CXX_IS_ROSE_CODE_GENERATION using namespace SageBuilder; assert (SageInterface::isDefaultConstructible(init->get_operand_i()->get_type())); SgStatement* stmt = getStatementOfExpression(init); assert (stmt); SgScopeStatement* parent = isSgScopeStatement(stmt->get_parent()); if (!parent && isSgForInitStatement(stmt->get_parent())) parent = isSgScopeStatement(stmt->get_parent()->get_parent()->get_parent()); assert (parent); SgNode* initparent = init->get_parent(); assert (initparent); SgInitializedName* initname = NULL; if (isSgInitializedName(initparent)) initname = isSgInitializedName(initparent); else if (isSgVariableDefinition(initparent)) initname = isSgVariableDefinition(initparent)->get_vardefn(); else if (isSgVariableDeclaration(initparent)) { SgInitializedNamePtrList& vars = isSgVariableDeclaration(initparent)->get_variables(); for (SgInitializedNamePtrList::iterator i = vars.begin(); i != vars.end(); ++i) { if ((*i)->get_initializer() == init) { initname = *i; break; } } } else { std::cout << "initparent is a " << initparent->sage_class_name() << std::endl; assert (!"Should not happen"); } assert (initname); assert (initname->get_initializer() == init); assert (parent); SgSymbol* sym = initname->get_symbol_from_symbol_table(); ROSE_ASSERT (isSgVariableSymbol(sym)); SgVarRefExp* vr = buildVarRefExp(isSgVariableSymbol(sym)); vr->set_lvalue(true); SgExprStatement* assign_stmt = buildAssignStatement(vr, init->get_operand()); initname->set_initializer(NULL); // assignment->set_parent(assign_stmt); // cout << "stmt is " << stmt->unparseToString() << endl; // cout << "stmt->get_parent() is a " << stmt->get_parent()->sage_class_name() << endl; myStatementInsert(stmt, assign_stmt, false); assign_stmt->set_parent(parent); // FixSgTree(assign_stmt); // FixSgTree(parent); // AstPostProcessing(assign_stmt); return isSgAssignOp(assign_stmt->get_expression()); #else return NULL; #endif }
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); } } }