📄 zprof.y
字号:
AMPERSAND_EQ : '&='($t) { OUT($t); } ;AUTO : 'auto'($t) { OUT($t); } ;BANG : '!'($t) { OUT($t); } ;BANG_EQ : '!='($t) { OUT($t); } ;BAR : '|'($t) { OUT($t); } ;BAR_BAR : '||'($t) { OUT($t); } ;BAR_EQ : '|='($t) { OUT($t); } ;BREAK : 'break'($t) { OUT($t); } ;CARAT : '^'($t) { OUT($t); } ;CARAT_EQ : '^='($t) { OUT($t); } ;CASE : 'case'($t) { OUT($t); } ;CHAR : 'char'($t) { OUT($t); } ;COLON : ':'($t) { OUT($t); } ;COMMA : ','($t) { OUT($t); } ;CONST : 'const'($t) { OUT($t); } ;CONTINUE : 'continue'($t) { OUT($t); } ;DEFAULT : 'default'($t) { OUT($t); } ;DO : 'do'($t) { OUT($t); } ;DOT : '.'($t) { OUT($t); } ;DOT_DOT_DOT : '...'($t) { OUT($t); } ;DOUBLE : 'double'($t) { OUT($t); } ;ELSE : 'else'($t) { OUT($t); } ;ENUM : 'enum'($t) { OUT($t); } ;EQ : '='($t) { OUT($t); } ;EQ_EQ : '=='($t) { OUT($t); } ;EXTERN : 'extern'($t) { OUT($t); } ;FLOAT : 'float'($t) { OUT($t); } ;FOR : 'for'($t) { OUT($t); } ;GOTO : 'goto'($t) { OUT($t); } ;GT : '>'($t) { OUT($t); } ;GT_EQ : '>='($t) { OUT($t); } ;GT_GT : '>>'($t) { OUT($t); } ;GT_GT_EQ : '>>='($t) { OUT($t); } ;ID(Index $id) : ID_TOK($t) { $id= $t.id; OUT($t); #if TEST_TYPEDEF printf(" %s", getIDString($id)); #endif } ;IF : 'if'($t) { OUT($t); } ;INT : 'int'($t) { OUT($t); } ;LBRACE : '{'($t) { OUT($t); } ;LBRACKET : '['($t) { OUT($t); } ;LONG : 'long'($t) { OUT($t); } ;LPAREN : '('($t) { OUT($t); } ;LT : '<'($t) { OUT($t); } ;LT_EQ : '<='($t) { OUT($t); } ;LT_LT : '<<'($t) { OUT($t); } ;LT_LT_EQ : '<<='($t) { OUT($t); } ;MINUS : '-'($t) { OUT($t); } ;MINUS_EQ : '-='($t) { OUT($t); } ;MINUS_GT : '->'($t) { OUT($t); } ;MINUS_MINUS : '--'($t) { OUT($t); } ;NUM : NUM_TOK($t) { OUT($t); } ;PERCENT : '%'($t) { OUT($t); } ;PERCENT_EQ : '%='($t) { OUT($t); } ;PLUS : '+'($t) { OUT($t); } ;PLUS_EQ : '+='($t) { OUT($t); } ;PLUS_PLUS : '++'($t) { OUT($t); } ;QUESTION : '?'($t) { OUT($t); } ;RBRACE : '}'($t) { OUT($t); } ;RBRACKET : ']'($t) { OUT($t); } ;REGISTER : 'register'($t) { OUT($t); } ;RETURN : 'return'($t) { OUT($t); } ;RPAREN : ')'($t) { OUT($t); } ;SEMI : ';'($t) { OUT($t); } ;SHORT : 'short'($t) { OUT($t); } ;SIGNED : 'signed'($t) { OUT($t); } ;SIZEOF : 'sizeof'($t) { OUT($t); } ;SLASH : '/'($t) { OUT($t); } ;SLASH_EQ : '/='($t) { OUT($t); } ;STAR : '*'($t) { OUT($t); } ;STAR_EQ : '*='($t) { OUT($t); } ;STATIC : 'static'($t) { OUT($t); } ;STRING /* catenate adjacent strings */ : STRING_TOK($t) { OUT($t); } | STRING STRING_TOK($t) { OUT($t); } ;STRUCT : 'struct'($t) { OUT($t); } ;SWITCH : 'switch'($t) { OUT($t); } ;TILDE : '~'($t) { OUT($t); } ;TYPEDEF : 'typedef'($t) { OUT($t); } ;UNION : 'union'($t) { OUT($t); } ;UNSIGNED : 'unsigned'($t) { OUT($t); } ;VOID : 'void'($t) { OUT($t); } ;VOLATILE : 'volatile'($t) { OUT($t); } ;WHILE : 'while'($t) { OUT($t); } ;counter_begin(%in CounterType $counter0) : /* empty */ { beginCounter($counter0); } ;counter_end(%in CounterType $counter0) : /* empty */ { endCounter($counter0); } ;%%/* IDENTIFIERS */ static HashTab hashTab; typedef enum { TYPEDEF_ID= 1, NON_TYPEDEF_ID= 2} IDType;static VOIDinitIDs(){ enum { INIT_HASH_SIZE= 256, MAX_LOAD_FACTOR= 4 }; HashOpts opts; opts.codeFnP= NULL; opts.equalFnP= NULL; opts.initSize= INIT_HASH_SIZE; opts.maxLoadFactor= MAX_LOAD_FACTOR; opts.isScoped= TRUE; opts.reorderOK= FALSE; hashTab= newHashTab(&opts);}static VOIDterminateIDs(){ delHashTab(hashTab);}static VOID dclTypedef(id, isTypedef) Index id; BooleanX isTypedef;{ Index val= isTypedef ? TYPEDEF_ID : NON_TYPEDEF_ID; putHashTab(hashTab, (HashKey)id, (HashVal)val); VOID_RET();}static Boolean isTypedef(){ CONST Index id= yylval.tok.t.id; HashVal v= getHashTab(hashTab, (HashKey)id); return v != NULL_HASH_VAL && ((Index)v) == TYPEDEF_ID;}static VOIDbeginScope(){ beginScopeHashTab(hashTab);}static VOIDendScope(){ endScopeHashTab(hashTab);}/* MAIN PROGRAM */static Options options;CONST Options *CONST optionsP= &options;OptInfo optTab[]= { /* LongOpt Name, ShortOpt Name, Argument Type, User ID, CheckP, ValP, * OptFn, Doc */ OPT_ENTRY("cond", 'c', NO_OPT_FLAG, COND_OPT, NULL, (VOIDP) &options.isCond, yesNoOptFn, "\--cond | -c Profile subexpressions of a ?: conditional expression\n\ (default: `0').\n\" ), OPT_ENTRY("help", 'h', NO_OPT_FLAG, HELP_OPT, NULL, NULL, helpOptFn, "\--help | -h Print summary of options and exit.\n\" ), OPT_ENTRY("output", 'o', REQUIRED_OPT_FLAG, OUTPUT_OPT, NULL, (VOIDP) &options.output, stringOptFn, "\--output=OUTFILE | Use OUTFILE as the name of the generated output\n\ -o OUTFILE C file (default: append `_BB').\n\" ), OPT_ENTRY("prefix", 'p', REQUIRED_OPT_FLAG, PREFIX_OPT, NULL, (VOIDP) &options.prefix, stringOptFn, "\--prefix=PREFIX | Use PREFIX as prefix of all generated names in\n\ -p PREFIX instrumented file (default: `_BB').\n\" ), OPT_ENTRY("preprocess", 'P', NO_OPT_FLAG, PREPROCESS_OPT, NULL, (VOIDP) &options.isPreprocess, yesNoOptFn, "\--preprocess | -P Run preprocessor (given by environmental var CPP) on\n\ input file (default: `1').\n\" ), OPT_ENTRY("silent", 's', NO_OPT_FLAG, SILENT_OPT, NULL, (VOIDP) &options.isSilent, yesNoOptFn, "\--silent | -s Do not generate error messages (default: `0').\n\" ), OPT_ENTRY(NULL, 'V', SYNONYM_OPT_FLAG, VERSION_OPT, NULL, NULL, NULL, NULL ), OPT_ENTRY("version", 'v', NO_OPT_FLAG, VERSION_OPT, NULL, NULL, versionOptFn, "\--version | -v | -V Print version information and exit.\n\" ), OPT_ENTRY("typedef", 't', REQUIRED_OPT_FLAG, TYPEDEF_OPT, NULL, NULL, typedefOptFn, "\--typedef=ID | Declare identifier ID to be a typedef in the global\n\ -t ID scope.\n\" )};static Int helpOptFn(id, checkP, valP, argP) Int id; VOIDP checkP; VOIDP valP; ConstString argP;{ allOptsHelp(NULL, "Usage: zprof [options] lex-file.", optTab, N_ELEMENTS(optTab)); exit(0); return 0;}static Int typedefOptFn(id, checkP, valP, argP) Int id; VOIDP checkP; VOIDP valP; ConstString argP;{ dclTypedef(getID(argP, strlen(argP)), TRUE); return 0;}#ifndef VERSION#define VERSION "0.0"#endifstatic Int versionOptFn(id, checkP, valP, argP) Int id; VOIDP checkP; VOIDP valP; ConstString argP;{ fprintf(stderr, "zprof version %s\n", VERSION); exit(0); return 0;}#ifndef SIGNATURE#define SIGNATURE "Zerksis"#endif/* Insert signature string into executable to permit distinguishing it * from other executables of the same name. */char uniqueSignature[]= SIGNATURE;/* A little function to ensure that above signature doesn't get optimized * away by optimizing linkers. */int ensureSig(){ int n= sizeof(uniqueSignature); int i; for (i= 0; i < n; i++) uniqueSignature[i]= (char)(2*i); return uniqueSignature[2];}int main(int argc, ConstString argv[]) { int n;#if YY_ZYACC_DEBUG yydebug= 1;#endif ensureSig(); initOut(); initScan(); initIDs(); options.prefix= "_BB"; options.isPreprocess= 1; n= parseOpt(NULL, argc, argv, optTab, N_ELEMENTS(optTab)); if (n != argc - 1) { allOptsHelp(NULL, "Usage: zprof [options] file.", optTab, N_ELEMENTS(optTab)); exit(1); } newFile(argv[n]); beginOutFile(argv[n]); if (yyparse() != 0) { fatal("could not recover from parse errors."); } putOut(&yylval.tok.t); endOutFile(); terminateIDs(); terminateScan(); terminateOut(); return 0;}#if YY_ZYACC_DEBUGstatic YY_VOIDtokSemFn(out, tokNum, p) FILE *out; int tokNum; VOIDP p;{ enum { MAX_TOK= 10 }; YYSTYPE *yylvalP= p; CONST int len= (yylvalP->tok.t.len < MAX_TOK) ? yylvalP->tok.t.len : MAX_TOK; fprintf(out, "%.*s", len, yylvalP->tok.t.text + yylvalP->tok.t.len1);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -