int main(int argc, char *argv[]) { // Command line processing specific to SME-C: bool isSmecy = smecy::processCommandLine(argc, argv); //std::string test = CommandlineProcessing::generateStringFromArgList( CommandlineProcessing::generateArgListFromArgcArgv(argc, argv) ); //std::cout << "command line :" << test << std::endl; // Call the ROSE front end, mainly the parser: SgProject* project = frontend(argc,argv); // Display the list of files to deal with: std::vector<SgFile*> files = project->get_fileList(); for (SgFile * f : files) std::cerr << "File " << f->getFileName() << std::endl; //if (isSgSourceFile(files[i])) //OmpSupport::lower_omp(isSgSourceFile(files[i])); // Translating smecy #pragma if the -smecy option has been given std::cerr << "Translating smecy" << std::endl; if (isSmecy) smecy::translateSmecy(project); // There is a limitation of ROSE on the AST beautifier that cannot // handle more than 1 file: if (files.size() > 1) std::cerr << "PDF and DOT generation of the AST" << std::endl; else { generatePDF(*project); std::cerr << "Generating DOT" << std::endl; generateDOT(*project); } // If we want to run ROSE unit tests: //AstTests::runAllTests(project); // Finish by calling the backend: compiler, linker... return backend(project); }
int main( int argc, char * argv[] ) { // If we want this translator to take specific options (beyond those defined // by ROSE) then insert command line processing for new options here. // To better support the stencil specification that might benifit from constant // folding, I have turned this ON is hte frontend. By default it is OFF so that // we can preserve source code as much as possible (original expression trees). // The Stencil DSL can be made to work in eithr setting, but this make sure that // dimension dependent processing of the stencil coeficients will be evaluated // to constants. I will turn this off (and thus use a less blunt axe) when the new // constant expression evaluation in ROSE is fixed to support more general types // than integer expresion (should be done by JP later today). // bool frontendConstantFolding = true; bool frontendConstantFolding = false; // Generate the ROSE AST. SgProject* project = frontend(argc,argv,frontendConstantFolding); ROSE_ASSERT(project != NULL); #if DEBUG_USING_DOT_GRAPHS // generateDOTforMultipleFile(*project); generateDOT(*project,"_before_transformation"); #endif #if DEBUG_USING_DOT_GRAPHS && 1 const int MAX_NUMBER_OF_IR_NODES_TO_GRAPH_FOR_WHOLE_GRAPH = 12000; #endif #if DEBUG_USING_DOT_GRAPHS && 1 // Output an optional graph of the AST (the whole graph, of bounded complexity, when active) generateAstGraph(project,MAX_NUMBER_OF_IR_NODES_TO_GRAPH_FOR_WHOLE_GRAPH,"_before"); #endif // Build the inherited attribute Detection_InheritedAttribute inheritedAttribute; // Define the traversal DetectionTraversal shiftCalculus_DetectionTraversal; #if 0 printf ("Call the Detection traversal starting at the project (root) node of the AST \n"); #endif // Call the traversal starting at the project (root) node of the AST Detection_SynthesizedAttribute result = shiftCalculus_DetectionTraversal.traverse(project,inheritedAttribute); #if 0 printf ("Stencil Operator was transformed: %s \n",result.get_stencilOperatorTransformed() ? "true" : "false"); #endif ROSE_ASSERT(result.get_stencilOperatorTransformed() == false); #if 0 printf ("DONE: Call the Detection traversal starting at the project (root) node of the AST \n"); #endif // Build the inherited attribute StencilEvaluation_InheritedAttribute inheritedAttribute_stencilEval; // Define the traversal StencilEvaluationTraversal shiftCalculus_StencilEvaluationTraversal(shiftCalculus_DetectionTraversal); #if 0 printf ("Call the StencilEvaluation traversal starting at the project (root) node of the AST \n"); #endif // Call the traversal starting at the project (root) node of the AST StencilEvaluation_SynthesizedAttribute result_stencilEval = shiftCalculus_StencilEvaluationTraversal.traverse(project,inheritedAttribute_stencilEval); #if 0 printf ("Stencil Evaluation was transformed: %s \n",result_stencilEval.get_stencilOperatorTransformed() ? "true" : "false"); #endif ROSE_ASSERT(result_stencilEval.get_stencilOperatorTransformed() == false); #if 0 printf ("DONE: Call the StencilEvaluation traversal starting at the project (root) node of the AST \n"); #endif shiftCalculus_StencilEvaluationTraversal.displayStencil("After evaluation of stencil"); #if 1 printf ("Call generateStencilCode to generate example code \n"); #endif // Generate code from stencil data structure. bool generateLowlevelCode = true; generateStencilCode(shiftCalculus_StencilEvaluationTraversal,generateLowlevelCode); #if 1 printf ("DONE: Call generateStencilCode to generate example code \n"); #endif // AST consistency tests (optional for users, but this enforces more of our tests) AstTests::runAllTests(project); #if DEBUG_USING_DOT_GRAPHS printf ("Write out the DOT file after the transformation \n"); // generateDOTforMultipleFile(*project,"after_transformation"); generateDOT(*project,"_after_transformation"); printf ("DONE: Write out the DOT file after the transformation \n"); #endif #if DEBUG_USING_DOT_GRAPHS && 0 // Output an optional graph of the AST (the whole graph, of bounded complexity, when active) // const int MAX_NUMBER_OF_IR_NODES_TO_GRAPH_FOR_WHOLE_GRAPH = 10000; generateAstGraph(project,MAX_NUMBER_OF_IR_NODES_TO_GRAPH_FOR_WHOLE_GRAPH,"_after"); #endif // Regenerate the source code but skip the call the to the vendor compiler. return backend(project); }
int main ( int argc, char* argv[] ) { // Main Function for default example ROSE Preprocessor // This is an example of a preprocessor that can be built with ROSE // This example can be used to test the ROSE infrastructure #if 1 list<string> l = CommandlineProcessing::generateArgListFromArgcArgv (argc,argv); printf ("Preprocessor (before): argv = \n%s \n",StringUtility::listToString(l).c_str()); // testing removeArgs CommandlineProcessing::removeArgs (argc,argv,"-edg:"); CommandlineProcessing::removeArgs (argc,argv,"--edg:"); CommandlineProcessing::removeArgsWithParameters (argc,argv,"-edg_parameter:"); CommandlineProcessing::removeArgsWithParameters (argc,argv,"--edg_parameter:"); printf ("argc = %d \n",argc); l = CommandlineProcessing::generateArgListFromArgcArgv (argc,argv); printf ("l.size() = %d \n",l.size()); printf ("Preprocessor (after): argv = \n%s \n",StringUtility::listToString(l).c_str()); // printf ("Exiting in main! \n"); // ROSE_ASSERT(1 == 2); #endif #if 0 int modifiedArgc = 0; char** modifiedArgv = NULL; // resets modifiedArgc and allocates memory to modifiedArgv // list<string> edgOptionWithNameParameterList = // CommandlineProcessing::generateOptionWithNameParameterList (argc, argv,"-edg_parameter:",modifiedArgc,modifiedArgv); // resets modifiedArgc and allocates memory to modifiedArgv list<string> edgOptionWithNumberParameterList = CommandlineProcessing::generateOptionWithNameParameterList (argc, argv,"-edg_parameter:",modifiedArgc,modifiedArgv); l = CommandlineProcessing::generateArgListFromArgcArgv (modifiedArgc,modifiedArgv); printf ("Preprocessor (after): argv = \n%s \n",StringUtility::listToString(l).c_str()); // resets modifiedArgc to zero and releases memory from modifiedArgv (resets modifiedArgv to NULL) CommandlineProcessing::releaseArgListMemory(modifiedArgc,modifiedArgv); #endif #if 0 if ( CommandlineProcessing::isOption(argc,argv,"-rose:","(h|help)",true) || CommandlineProcessing::isOption(argc,argv,"-", "(h|help)",true) || CommandlineProcessing::isOption(argc,argv,"--","(h|help)",true) ) { printf ("\nROSE (pre-release alpha version: %s) \n",VERSION); ROSE::usage(0); exit(0); } l = CommandlineProcessing::generateArgListFromArgcArgv (argc,argv); printf ("Preprocessor (after): argv = \n%s \n",StringUtility::listToString(l).c_str()); printf ("Exiting after initial command line processing \n"); ROSE_ABORT(); #endif #if 0 string stringParameter; if ( CommandlineProcessing::isOptionWithParameter(argc,argv,"-rose:","(o|output)",stringParameter,true) ) { printf ("-rose:output %s \n",stringParameter.c_str()); // Make our own copy of the filename string int length = stringParameter.length(); char* p_unparse_output_filename = (char*) new char[length+1]; ROSE_ASSERT (p_unparse_output_filename != NULL); stringParameter.copy(p_unparse_output_filename,length,0); p_unparse_output_filename[length] = '\0'; printf ("p_unparse_output_filename = %s \n",p_unparse_output_filename); } else { printf ("-rose:output not set! \n"); } #endif #if 0 int modifiedArgc = 0; char** modifiedArgv = NULL; // resets modifiedArgc and allocates memory to modifiedArgv list<string> edgOptionList = CommandlineProcessing::generateOptionList (argc, argv,"-edg:",modifiedArgc,modifiedArgv); // resets modifiedArgc to zero and releases memory from modifiedArgv (resets modifiedArgv to NULL) CommandlineProcessing::releaseArgListMemory(modifiedArgc,modifiedArgv); if ( CommandlineProcessing::isOption(argc,argv,"-","help",true) ) { printf ("Option -help found! \n"); } if ( CommandlineProcessing::isOption(argc,argv,"--","help",true) ) { printf ("Option --help found! \n"); } int integerParameter; if ( CommandlineProcessing::isOptionWithParameter(argc,argv,"-edg:","test",integerParameter,true) ) { printf ("Option (integer parameter) -test %d found! \n",integerParameter); } string stringParameter; if ( CommandlineProcessing::isOptionWithParameter(argc,argv,"-edg:","test",stringParameter,true) ) { printf ("Option (string parameter) -test %s found! \n",stringParameter.c_str()); } #endif SgProject* project = frontend(argc,argv); ROSE_ASSERT (project != NULL); #if 0 // See if we can access the EDG AST directly ROSE_ASSERT (il_header.primary_source_file != NULL); ROSE_ASSERT (il_header.primary_source_file->file_name != NULL); printf ("##### il_header.primary_source_file->file_name = %s \n",il_header.primary_source_file->file_name); #endif // DQ (2/6/2004): These tests fail in Coco for test2004_14.C // AstTests::runAllTests(const_cast<SgProject*>(project)); // printf ("Generate the pdf output of the SAGE III AST \n"); // generatePDF ( project ); printf ("Generate the DOT output of the SAGE III AST \n"); generateDOT ( *project ); return backend(project); // alternative form // return backend(frontend(argc,argv)); }
int main (int argc, char* argv[]) { // Main Function for default example ROSE Preprocessor // This is an example of a preprocessor that can be built with ROSE // Build the project object (AST) which we will fill up with multiple files and use as a // handle for all processing of the AST(s) associated with one or more source files. SgProject* sageProject = frontend(argc,argv); // DQ (7/20/2004): Added internal consistancy tests on AST AstTests::runAllTests(sageProject); // This is not needed here // FixSgProject(sageProject); bool changed = true; int count = 0; /* Inline one call at a time until all have been inlined. Loops on recursive code. */ while (changed) { changed = false; calls_to_inline.clear(); FindCallsVisitor().traverseInputFiles(sageProject, preorder); for (std::vector<SgFunctionCallExp*>::iterator i = calls_to_inline.begin(); i != calls_to_inline.end(); ++i) { // cout << (*i)->unparseToString() << endl; // generateAstGraph(sageProject, 400000); if (doInline(*i)) { changed = true; // AstTests::runAllTests(sageProject); break; } } ++count; #if 0 sageProject.unparse(); #endif // To prevent infinite loops if (count == 10) { break; } } #if 1 // Rename each variable declaration renameVariables(sageProject); #if 1 // Fold up blocks flattenBlocks(sageProject); // Clean up inliner-generated code cleanupInlinedCode(sageProject); #endif // Change members to public changeAllMembersToPublic(sageProject); // AstPDFGeneration().generateInputFiles(sageProject); // AstDOTGeneration().generateInputFiles(sageProject); #endif AstTests::runAllTests(sageProject); #if 0 // Output an optional graph of the AST (just the tree, when active) printf ("Generating a dot file... (ROSE Release Note: turn off output of dot files before committing code) \n"); generateDOT (*sageProject ); // generateAstGraph(project, 2000); #endif #if 1 // Output an optional graph of the AST (the whole graph, of bounded complexity, when active) const int MAX_NUMBER_OF_IR_NODES_TO_GRAPH_FOR_WHOLE_GRAPH = 8000; generateAstGraph(sageProject,MAX_NUMBER_OF_IR_NODES_TO_GRAPH_FOR_WHOLE_GRAPH); #endif return backend(sageProject); }
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); }