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 fixupReferencesToGlobalVariables ( Rose_STL_Container<SgVarRefExp*> & variableReferenceList, SgVariableSymbol* globalClassVariableSymbol) { // Now fixup the SgVarRefExp to reference the global variables through a struct for (Rose_STL_Container<SgVarRefExp*>::iterator var = variableReferenceList.begin(); var != variableReferenceList.end(); var++) { assert(*var != NULL); // printf ("Variable reference for %s \n",(*var)->get_symbol()->get_declaration()->get_name().str()); SgNode* parent = (*var)->get_parent(); assert(parent != NULL); // If this is not an expression then is likely a meaningless statement such as ("x;") SgExpression* parentExpression = isSgExpression(parent); assert(parentExpression != NULL); // Build the reference through the global class variable ("x" --> "AMPI_globals.x") // Build source position informaiton (marked as transformation) Sg_File_Info* fileInfo = Sg_File_Info::generateDefaultFileInfoForTransformationNode(); assert(fileInfo != NULL); // Build "AMPI_globals" SgExpression* lhs = new SgVarRefExp(fileInfo,globalClassVariableSymbol); assert(lhs != NULL); // Build "AMPI_globals.x" from "x" SgDotExp* globalVariableReference = new SgDotExp(fileInfo,lhs,*var); assert(globalVariableReference != NULL); if (parentExpression != NULL) { // Introduce reference to *var through the data structure // case of binary operator SgUnaryOp* unaryOperator = isSgUnaryOp(parentExpression); if (unaryOperator != NULL) { unaryOperator->set_operand(globalVariableReference); } else { // case of binary operator SgBinaryOp* binaryOperator = isSgBinaryOp(parentExpression); if (binaryOperator != NULL) { // figure out if the *var is on the lhs or the rhs if (binaryOperator->get_lhs_operand() == *var) { binaryOperator->set_lhs_operand(globalVariableReference); } else { assert(binaryOperator->get_rhs_operand() == *var); binaryOperator->set_rhs_operand(globalVariableReference); } } else { // ignore these cases for now! switch(parentExpression->variantT()) { // Where the variable appers in the function argument list the parent is a SgExprListExp case V_SgExprListExp: { printf ("Sorry not implemented, case of global variable in function argument list ... \n"); assert(false); break; } case V_SgInitializer: case V_SgRefExp: case V_SgVarArgOp: default: { printf ("Error: default reached in switch parentExpression = %p = %s \n",parentExpression,parentExpression->class_name().c_str()); assert(false); } } } } } } }