sagexx_ug (1158317), страница 11
Текст из файла (страница 11)
It traverses the program file in lexicalorder and examines each statement and prints out theinformation stored in the file about the statement.// THIS PROGRAM WORKS, BUT IT IS NOT FINISHED!!!Dump Info - variants// each object in the .dep file has an integer "variant" to// describe its type. There is a standard defined name for// each and that may be retrieved from the array "tag" below.#define MAXTAGS 1000static char *tag[MAXTAGS];void initVariantNames(){for(int i = 0; i < MAXTAGS; i++) tag[i] = NULL;#include "../h/tag.h"}void printVariantName(int i){if((i >= 0 && i < MAXTAGS) && tag[i]) printf("%s", tag[i]);else printf("not a known node variant");}Dump Info - ProjectUnparse//////////the unparse operation regenerates source code.The function below prints the entire project tostdout. note: the current unparser will only printto stdout.
in the near future we will have a versionthat will print to a string buffer.voidProjectUnparse(SgProject *project){for (int i = 0; i < project->numberOfFiles(); i++) {SgFile *f;169Chapter 9: Example Programs}}170f = &(project->file(i));f->unparsestdout();Dump Info - classifyStatements// some forward declarations.voidvoidvoidvoidvoidvoidvoidvoidvoidvoidvoiddoExpr(SgExpression *e);doSymb(SgSymbol *s);doType(SgType *t);doRoutineHeader(SgStatement *s);doIfStmts(SgStatement *s);doLoopStmts(SgStatement *s);doIOStmts(SgStatement *s);doGoToStmt(SgStatement *s);doVarDecl(SgStatement *s);doFullType(SgType *t);doSymbAttribs(SgSymbol *s);// this function traverses the list of statements in a file.// for each statement it prints out all the information available// in the .dep data base.void classifyStatements(SgFile *f){SgStatement *line;SgStatement *s;int i,j;// grab the first statement in the file.s = f->firstStatement();////////follow the statements in lexical order.an alternative is to follow them in scope orderusing a recursive traversal, but this is simplerfor what we want to do here.for (line = s; line; line = line->lexNext()){printf("---------------------------------------------------------------------\n");// print the line to the standard out.// note: if the statement controls other blocks// nested within, it will print the entire block// of "control children".line->unparsestdout();Chapter 9: Example Programs// print out the variant tag, i.e.
the type of statement.printf("STMT[%d] at line %d has variant:",line->id(), line->lineNumber());printVariantName(line->variant());printf("\n");// is this a labeled statement? if so print the label.if(line->hasLabel()){SgLabel *lab = line->label();printf("has label[%d]\n", lab->id());}// are there comments that precede this statement?// if so print them out.if(line->comments())printf("comments befor this line:\n%s", line->comments());171Chapter 9: Example Programs172// for each type of statement call the appropriate special case// function.switch(line->variant()){case GLOBAL:printf("The head-of-fole global node.
not really a statement\n");break;case PROG_HEDR:case PROC_HEDR:case FUNC_HEDR:printf(" a new routine.\n");doRoutineHeader(line);break;case BASIC_BLOCK :printf(" a beginning of a control block for C programs \n");break;case CONTROL_END:printf(" end of a control block for stmt %d\n",(line->controlParent())->id());break;case IF_NODE:case ELSEIF_NODE:case ARITHIF_NODE:case LOGIF_NODE:doIfStmts(line);break;case LOOP_NODE:case FOR_NODE:case FORALL_NODE:case WHILE_NODE:doLoopStmts(line);break;case EXIT_NODE:// note the use of the special cast function// we are casting to a subtype but this is o.k.// with this function.{SgExitStmt *exst = isSgExitStmt(line);printf("an exit statement.
exit from:\n");doSymb(exst->constructName());}break;case ASSIGN_STAT:{ SgAssignStmt *ass = isSgAssignStmt(line);printf("an assignment\n");// print out the two expressions on each side of =printf("left hand side:\n");doExpr(ass->lhs());printf("\nright hand side:\n");Chapter 9: Example ProgramsdoExpr(ass->rhs());}break;{ int i;173printf("\n");case PROC_STAT:SgCallStmt *call = isSgCallStmt(line);// a "call f(...) statement. extract the passed// arguement expressions and print them.printf("a subroutine call. passed args are\n");for(i = 0; i < call->numberOfArgs(); i++){printf("argument %d:\n", i);doExpr(call->arg(i));printf("\n");}}break;case WHERE_NODE:case ALLDO_NODE:case IDENTIFY:case FORMAT_STAT:case STOP_STAT:break;case RETURN_STAT:break;case GOTO_NODE:case ASSGOTO_NODE:case COMGOTO_NODE:doGoToStmt(line);break;case VAR_DECL:case INTENT_STMT:case OPTIONAL_STMT:case PUBLIC_STMT:case PRIVATE_STMT:case DIM_STAT:case ALLOCATABLE_STMT:case POINTER_STMT:case TARGET_STMT:case MODULE_PROC_STMT:case EXTERN_STAT:case COMM_STAT:case NAMELIST_STAT:case EQUI_STAT:case PARAM_DECL:case IMPL_DECL:case SAVE_DECL:case DATA_DECL:Chapter 9: Example ProgramsdoVarDecl(line);casecasecasecasecasebreak;break;casecasecasecasecasecase174break;PAUSE_NODE:break;STOP_NODE:break;ASSLAB_STAT:break;COMMENT_STAT:break;CONT_STAT:printf(" continue statement: end of cntl for stmt %d\n",(line->controlParent())->id());ENTRY_STAT:STMTFN_STAT:BLOCK_DATA:INTRIN_STAT:PROC_COM: /* process common */ATTR_DECL: /* attribute declaration */printf("another fortran statement\n");// the statements below are all valid, and will be// treated in example when I have more time.case READ_STAT:case WRITE_STAT:case PRINT_STAT:case BACKSPACE_STAT:case REWIND_STAT :case ENDFILE_STAT:case INQUIRE_STAT:case OPEN_STAT:case CLOSE_STAT:case OTHERIO_STAT:Chapter 9: Example Programs175doIOStmts(line);break;INCLUDE_STAT:ALLOCATE_STMT:NULLIFY_STMT:DEALLOCATE_STMT:SEQUENCE_STMT:CYCLE_STMT:EXIT_STMT:CONTAINS_STMT:WHERE_BLOCK_STMT:MODULE_STMT:USE_STMT:INTERFACE_STMT:OVERLOADED_ASSIGN_STAT:POINTER_ASSIGN_STAT:OVERLOADED_PROC_STAT:printf("yet another fortran 90 statement.\n");break;case DECOMPOSITION_STMT: /* Old Fortran D stuff */case ALIGN_STMT:case DISTRIBUTE_STMT:case REDUCE_STMT:printf("Old Fortran D.
not treated here..needs update\n");break;case CDOALL_NODE: /* added for Cedar Fortran */case SDOALL_NODE:case DOACROSS_NODE:case CDOACROSS_NODE:printf("Cedar Fortran. who cares anymore. \n");break;case PARDO_NODE:/* Following added for PCF Fortran */case PARSECTIONS_NODE:case SECTION_NODE:case GUARDS_NODE:case LOCK_NODE:case UNLOCK_NODE:case CRITSECTION_NODE:case POST_NODE:case WAIT_NODE:case CLEAR_NODE:case POSTSEQ_NODE:case WAITSEQ_NODE:case SETSEQ_NODE:case ASSIGN_NODE:case RELEASE_NODE:case PRIVATE_NODE:case SCOMMON_NODE:case PARREGION_NODE:case PDO_NODE:case PSECTIONS_NODE:case SINGLEPROCESS_NODE:case SKIPPASTEOF_NODE:printf("Old pcf fortran. needs update.
who cares anymore. \n");casecasecasecasecasecasecasecasecasecasecasecasecasecasecasebreak;default:printf("unknown statement. not treated here..I am tired.\n");Chapter 9: Example Programs}Dump Info - doRoutineHeader// print out the name and formal parameters for this subroutine,// function or main program header.void doRoutineHeader(SgStatement *s){SgProgHedrStmt *prog;SgProcHedrStmt *subr;SgFuncHedrStmt *func;int i;SgSymbol *sy;if(subr = isSgProcHedrStmt (s)){printf("symbol:%s", (subr->name()).identifier());printf(" which is a suroutine.\n");printf("parameter list:\n");for(i = 0; i < subr->numberOfParameters(); i++){sy = subr->parameter(i);printf("parameter(%d):", i); doSymb(sy);printf(" of type: "); doFullType(sy->type());doSymbAttribs(sy);printf("\n");}}else if(func = isSgFuncHedrStmt (s)){printf("symbol:%s", (func->name()).identifier());printf("which is a function of type ");doFullType((func->name()).type());printf("\n");printf("parameter list:\n");for(i = 0; i < func->numberOfParameters(); i++){sy = func->parameter(i);printf("parameter(%d):", i); doSymb(sy);printf(" of type: "); doFullType(sy->type());doSymbAttribs(sy);printf("\n");}}}Dump Info - doIfStmts176Chapter 9: Example Programs// this routine handles the different types of "if" statements.// not finished.void doIfStmts(SgStatement *s){SgLogIfStmt *logif;SgIfStmt *ifst;SgArithIfStmt * arithif;if (logif = isSgLogIfStmt (s)){printf("logical if: condition EXPR[%d] ", logif->conditional()->id());(logif->conditional())->unparsestdout();printf(" :if true do STMT[%d]\n", (logif->body())->id());}else if(ifst = isSgIfStmt (s)){}else if(arithif = isSgArithIfStmt (s)){}}Dump Info - doLoopStmts// this routine handles the different types of loop statements.// only does do loops now.
more later.void doLoopStmts(SgStatement *s){SgForStmt * doloop;SgWhileStmt * whileloop;SgExpression *e;if (doloop = isSgForStmt(s)){printf("a DO LOOP: control var:");doSymb(doloop->symbol());printf("\n lower bound = ");if(e = doloop->start()) e->unparsestdout();printf(" , upper bound = ");if(e = doloop->end()) e->unparsestdout();if(e = doloop->step()){printf(" , step = ");e->unparsestdout();}printf(" \n");if(doloop->endOfLoop()){printf("end of loop label[%d]\n",(doloop->endOfLoop())->id());}}else if(whileloop = isSgWhileStmt (s)){}else printf("not finished\n");}177Chapter 9: Example ProgramsDump Info - doIOStmts// print out info about I/O statements.// not done.void doIOStmts(SgStatement *s){SgIOStmt *iost;SgInputOutputStmt * inoust;SgIOControlStmt * iocntl;if (iost = isSgIOStmt(s)){}else if(inoust = isSgInputOutputStmt (s)){}else if(iocntl = isSgIOControlStmt(s)){};}// print out the information about the different types of// goto statements.// not done.
only does standard goto's.Dump Info - doGoToStmtvoid doGoToStmt(SgStatement *s){SgGotoStmt *gotostmt;SgAssignedGotoStmt * assgoto;SgComputedGotoStmt * compgoto;if(gotostmt = isSgGotoStmt(s)){printf("garden variety goto: branch to stmt with label [ %d ]\n",gotostmt->branchLabel()->id());}}else if(assgoto = isSgAssignedGotoStmt(s)){}else if(compgoto = isSgComputedGotoStmt(s)){}Dump Info - doVarDecl////////this does variable declarations.there are lots of different forms that adeclaration can take. this just does the standardtypes now. not done yet.void doVarDecl(SgStatement *s){SgVarDeclStmt * var;SgVarListDeclStmt * varlist;SgStructureDeclStmt * strucdecl;178Chapter 9: Example ProgramsSgNestedVarListDeclStmt* declst;SgParameterStmt *parm;SgImplicitStmt *implic;if (var = isSgVarDeclStmt(s)){int i;printf("Variables declared here are:\n");for(i = 0; i < var->numberOfSymbols(); i++){SgSymbol *s;s = var->symbol(i);doSymb(s); printf(" of type: ");if(s->type()) doFullType(s->type());printf("\n");}}else if (varlist = isSgVarListDeclStmt (s)){}/* else if (strucdecl = isSgStructureDeclStmt (s)){} */else if ( declst = isSgNestedVarListDeclStmt (s)){}else if ( parm = isSgParameterStmt (s)){}else if ( implic = isSgImplicitStmt (s)){}else printf("not here!\n");}Dump Info - doSymbvoid doSymb(SgSymbol *s){printf("SYMB[%d]:%s",s->id(),s->identifier());}Dump Info - doSymbAttribsvoid doSymbAttribs(SgSymbol *s){int i;179Chapter 9: Example Programsif(s->attributes()){i = s->attributes();printf(" attributes =( ");if(i & ALLOCATABLE_BIT ){ printf(" allocatable,"); }if(i & DIMENSION_BIT ){ printf(" dim,"); }if(i & EXTERNAL_BIT ){ printf(" extern,"); }if(i & IN_BIT ){ printf(" in,"); }if(i & INOUT_BIT ){ printf(" inout,"); }if(i & INTRINSIC_BIT ){ printf(" intrinsic,"); }if(i & OPTIONAL_BIT ){ printf(" optional,"); }if(i & OUT_BIT ){ printf(" out,"); }if(i & PARAMETER_BIT ){ printf(" parameter,"); }if(i & POINTER_BIT ){ printf(" pointer,"); }if(i & PRIVATE_BIT ){ printf(" private,"); }if(i & PUBLIC_BIT ){ printf(" public,"); }if(i & SAVE_BIT ){ printf(" save,"); }if(i & SEQUENCE_BIT ){ printf(" seq,"); }if(i & RECURSIVE_BIT ){ printf(" recur,"); }if(i & TARGET_BIT ){ printf(" target,"); }printf(")");}}Dump Info - doFullType// given a type, traverse it and generate full infomrationvoid doFullType(SgType *t){SgArrayType *arrayt;SgPointerType *ptr;SgReferenceType *ref; // only for C++SgDerivedType *deriv;SgDescriptType *descr; // only for C and C++SgDerivedCollectionType *col; // only for pC++int i,n;if(arrayt = isSgArrayType(t)){printf("dimension(");n = arrayt->dimension();for(i = 0; i < n; i++){(arrayt->sizeInDim(i))->unparsestdout();if(i < n-1) printf(", ");}printf(") ");}else{180Chapter 9: Example Programsswitch(t->variant()){case T_INT:printf("integer "); break;case T_FLOAT:printf("real "); break;case T_DOUBLE: printf("double precision "); break;case T_CHAR:printf("character "); break;case T_BOOL:printf("boolean "); break;case T_STRING: printf("string "); break;case T_RECORD: printf("record..