From b2b145ae8bc2a504c2e0f0d4ab31655cd6f5c220 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sat, 28 Feb 2009 15:44:43 -0500 Subject: [PATCH] Cleaned up HLSL parser tracing. --- misc/lempar.c | 42 +++++++++++++++++++++++------------------- mojoshader_assembler.c | 4 ++-- mojoshader_compiler.c | 9 ++++++++- mojoshader_internal.h | 5 +++-- 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/misc/lempar.c b/misc/lempar.c index 0005802a..7b6be35c 100644 --- a/misc/lempar.c +++ b/misc/lempar.c @@ -6,6 +6,10 @@ #define __MOJOSHADER__ 1 #endif +#if !__MOJOSHADER__ +#define LEMON_SUPPORT_TRACING (!defined(NDEBUG)) +#endif + /* Driver template for the LEMON parser generator. ** The author disclaims copyright to this source code. */ @@ -178,14 +182,14 @@ struct yyParser { }; typedef struct yyParser yyParser; -#ifndef NDEBUG +#if LEMON_SUPPORT_TRACING /* __MOJOSHADER__ */ #include static FILE *yyTraceFILE = 0; static char *yyTracePrompt = 0; #endif /* NDEBUG */ -#ifndef NDEBUG -/* +#if LEMON_SUPPORT_TRACING /* __MOJOSHADER__ */ +/* ** Turn parser tracing on by giving a stream to which to write the trace ** and a prompt to preface each trace message. Tracing is turned off ** by making either argument NULL @@ -213,7 +217,7 @@ void ParseTrace(FILE *TraceFILE, char *zTracePrompt){ } #endif /* NDEBUG */ -#ifndef NDEBUG +#if LEMON_SUPPORT_TRACING /* __MOJOSHADER__ */ /* For tracing shifts, the names of all terminals and nonterminals ** are required. The following table supplies these names */ static const char *const yyTokenName[] = { @@ -221,7 +225,7 @@ static const char *const yyTokenName[] = { }; #endif /* NDEBUG */ -#ifndef NDEBUG +#if LEMON_SUPPORT_TRACING /* __MOJOSHADER__ */ /* For tracing reduce actions, the names of all rules are required. */ static const char *const yyRuleName[] = { @@ -243,7 +247,7 @@ static void yyGrowStack(yyParser *p){ if( pNew ){ p->yystack = pNew; p->yystksz = newSize; -#ifndef NDEBUG +#if LEMON_SUPPORT_TRACING /* __MOJOSHADER__ */ if( yyTraceFILE ){ fprintf(yyTraceFILE,"%sStack grows to %d entries!\n", yyTracePrompt, p->yystksz); @@ -328,7 +332,7 @@ static int yy_pop_parser_stack(yyParser *pParser){ yyStackEntry *yytos = &pParser->yystack[pParser->yyidx]; if( pParser->yyidx<0 ) return 0; -#ifndef NDEBUG +#if LEMON_SUPPORT_TRACING /* __MOJOSHADER__ */ if( yyTraceFILE && pParser->yyidx>=0 ){ fprintf(yyTraceFILE,"%sPopping %s\n", yyTracePrompt, @@ -414,7 +418,7 @@ static int yy_find_shift_action( YYCODETYPE iFallback; /* Fallback token */ if( iLookAhead %s\n", yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]); @@ -427,7 +431,7 @@ static int yy_find_shift_action( { int j = i - iLookAhead + YYWILDCARD; if( j>=0 && j %s\n", yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]); @@ -485,7 +489,7 @@ static int yy_find_reduce_action( static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){ ParseARG_FETCH; yypParser->yyidx--; -#ifndef NDEBUG +#if LEMON_SUPPORT_TRACING /* __MOJOSHADER__ */ if( yyTraceFILE ){ fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt); } @@ -531,7 +535,7 @@ static void yy_shift( yytos->stateno = (YYACTIONTYPE)yyNewState; yytos->major = (YYCODETYPE)yyMajor; yytos->minor = *yypMinor; -#ifndef NDEBUG +#if LEMON_SUPPORT_TRACING /* __MOJOSHADER__ */ if( yyTraceFILE && yypParser->yyidx>0 ){ int i; fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState); @@ -570,8 +574,8 @@ static void yy_reduce( int yysize; /* Amount to pop the stack */ ParseARG_FETCH; yymsp = &yypParser->yystack[yypParser->yyidx]; -#ifndef NDEBUG - if( yyTraceFILE && yyruleno>=0 +#if LEMON_SUPPORT_TRACING /* __MOJOSHADER__ */ + if( yyTraceFILE && yyruleno>=0 && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){ fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt, yyRuleName[yyruleno]); @@ -612,7 +616,7 @@ static void yy_reduce( yypParser->yyidx -= yysize; yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto); if( yyact < YYNSTATE ){ -#ifdef NDEBUG +#if LEMON_SUPPORT_TRACING /* __MOJOSHADER__ */ /* If we are not debugging and the reduce action popped at least ** one element off the stack, then we can push the new element back ** onto the stack here, and skip the stack overflow test in yy_shift(). @@ -641,7 +645,7 @@ static void yy_parse_failed( yyParser *yypParser /* The parser */ ){ ParseARG_FETCH; -#ifndef NDEBUG +#if LEMON_SUPPORT_TRACING /* __MOJOSHADER__ */ if( yyTraceFILE ){ fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt); } @@ -674,7 +678,7 @@ static void yy_accept( yyParser *yypParser /* The parser */ ){ ParseARG_FETCH; -#ifndef NDEBUG +#if LEMON_SUPPORT_TRACING /* __MOJOSHADER__ */ if( yyTraceFILE ){ fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt); } @@ -742,7 +746,7 @@ void Parse( yyendofinput = (yymajor==0); ParseARG_STORE; -#ifndef NDEBUG +#if LEMON_SUPPORT_TRACING /* __MOJOSHADER__ */ if( yyTraceFILE ){ fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]); } @@ -762,7 +766,7 @@ void Parse( #ifdef YYERRORSYMBOL int yymx; #endif -#ifndef NDEBUG +#if LEMON_SUPPORT_TRACING /* __MOJOSHADER__ */ if( yyTraceFILE ){ fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt); } @@ -792,7 +796,7 @@ void Parse( } yymx = yypParser->yystack[yypParser->yyidx].major; if( yymx==YYERRORSYMBOL || yyerrorhit ){ -#ifndef NDEBUG +#if LEMON_SUPPORT_TRACING /* __MOJOSHADER__ */ if( yyTraceFILE ){ fprintf(yyTraceFILE,"%sDiscard input token %s\n", yyTracePrompt,yyTokenName[yymajor]); diff --git a/mojoshader_assembler.c b/mojoshader_assembler.c index 6e044730..36c66428 100644 --- a/mojoshader_assembler.c +++ b/mojoshader_assembler.c @@ -13,7 +13,7 @@ #define __MOJOSHADER_INTERNAL__ 1 #include "mojoshader_internal.h" -#if DEBUG_ASSEMBLY_PARSER +#if DEBUG_ASSEMBLER_PARSER #define print_debug_token(token, len, val) \ MOJOSHADER_print_debug_token("ASSEMBLER", token, len, val) #else @@ -193,7 +193,7 @@ static inline int shader_is_vertex(const Context *ctx) static inline void pushback(Context *ctx) { - #if DEBUG_ASSEMBLY_PARSER + #if DEBUG_ASSEMBLER_PARSER printf("ASSEMBLER PUSHBACK\n"); #endif assert(!ctx->pushedback); diff --git a/mojoshader_compiler.c b/mojoshader_compiler.c index 9d728cd0..1caca61f 100644 --- a/mojoshader_compiler.c +++ b/mojoshader_compiler.c @@ -9,6 +9,10 @@ typedef struct Context Token tokenval; // assembler token! } Context; +#if DEBUG_COMPILER_PARSER +#define LEMON_SUPPORT_TRACING 1 +#endif + #define __MOJOSHADER_HLSL_COMPILER__ 1 #include "mojoshader_parser_hlsl.h" @@ -130,7 +134,10 @@ void MOJOSHADER_compile(const char *filename, defines, define_count, 0, m, f, d); void *pParser = ParseHLSLAlloc(m, d); - ParseHLSLTrace(stdout, "TRACE: "); + + #if DEBUG_COMPILER_PARSER + ParseHLSLTrace(stdout, "COMPILER: "); + #endif do { ctx.token = preprocessor_nexttoken(ctx.preprocessor, diff --git a/mojoshader_internal.h b/mojoshader_internal.h index 0ecdad22..008d439f 100644 --- a/mojoshader_internal.h +++ b/mojoshader_internal.h @@ -18,9 +18,10 @@ #define DEBUG_LEXER 0 #define DEBUG_PREPROCESSOR 0 -#define DEBUG_ASSEMBLY_PARSER 0 +#define DEBUG_ASSEMBLER_PARSER 0 +#define DEBUG_COMPILER_PARSER 0 #define DEBUG_TOKENIZER \ - (DEBUG_PREPROCESSOR || DEBUG_ASSEMBLY_PARSER || DEBUG_LEXER) + (DEBUG_PREPROCESSOR || DEBUG_ASSEMBLER_PARSER || DEBUG_LEXER) #if (defined(__APPLE__) && defined(__MACH__)) #define PLATFORM_MACOSX 1