Unified some cut-and-paste code.
authorRyan C. Gordon <icculus@icculus.org>
Thu, 12 Feb 2009 17:25:49 -0500
changeset 587 202354e004fc
parent 586 321a19a62989
child 588 ef3d2efa822d
Unified some cut-and-paste code.
mojoshader_assembler.c
mojoshader_internal.h
mojoshader_preprocessor.c
--- a/mojoshader_assembler.c	Thu Feb 12 17:12:46 2009 -0500
+++ b/mojoshader_assembler.c	Thu Feb 12 17:25:49 2009 -0500
@@ -10,7 +10,13 @@
 #define __MOJOSHADER_INTERNAL__ 1
 #include "mojoshader_internal.h"
 
-#define DEBUG_ASSEMBLY_PARSER 0
+#if DEBUG_ASSEMBLY_PARSER
+    #define print_debug_token(token, len, val) \
+        MOJOSHADER_print_debug_token("ASSEMBLER", token, len, val)
+#else
+    #define print_debug_token(token, len, val)
+#endif
+
 
 // Simple linked list to cache source filenames, so we don't have to copy
 //  the same string over and over for each opcode.
@@ -224,82 +230,11 @@
 } // _nexttoken
 
 
-// !!! FIXME: cut-and-paste from preprocessor.
-#if !DEBUG_ASSEMBLY_PARSER
-#define print_debug_token(ctx)
-#else
-static void print_debug_token(Context *ctx)
-{
-    printf("ASSEMBLER TOKEN: \"");
-    unsigned int i;
-    for (i = 0; i < ctx->tokenlen; i++)
-    {
-        if (ctx->token[i] == '\n')
-            printf("\\n");
-        else
-            printf("%c", ctx->token[i]);
-    } // for
-    printf("\" (");
-    switch (ctx->tokenval)
-    {
-        #define TOKENCASE(x) case x: printf("%s", #x); break
-        TOKENCASE(TOKEN_UNKNOWN);
-        TOKENCASE(TOKEN_IDENTIFIER);
-        TOKENCASE(TOKEN_INT_LITERAL);
-        TOKENCASE(TOKEN_FLOAT_LITERAL);
-        TOKENCASE(TOKEN_STRING_LITERAL);
-        TOKENCASE(TOKEN_ADDASSIGN);
-        TOKENCASE(TOKEN_SUBASSIGN);
-        TOKENCASE(TOKEN_MULTASSIGN);
-        TOKENCASE(TOKEN_DIVASSIGN);
-        TOKENCASE(TOKEN_MODASSIGN);
-        TOKENCASE(TOKEN_XORASSIGN);
-        TOKENCASE(TOKEN_ANDASSIGN);
-        TOKENCASE(TOKEN_ORASSIGN);
-        TOKENCASE(TOKEN_INCREMENT);
-        TOKENCASE(TOKEN_DECREMENT);
-        TOKENCASE(TOKEN_RSHIFT);
-        TOKENCASE(TOKEN_LSHIFT);
-        TOKENCASE(TOKEN_ANDAND);
-        TOKENCASE(TOKEN_OROR);
-        TOKENCASE(TOKEN_LEQ);
-        TOKENCASE(TOKEN_GEQ);
-        TOKENCASE(TOKEN_EQL);
-        TOKENCASE(TOKEN_NEQ);
-        TOKENCASE(TOKEN_HASHHASH);
-        TOKENCASE(TOKEN_PP_INCLUDE);
-        TOKENCASE(TOKEN_PP_LINE);
-        TOKENCASE(TOKEN_PP_DEFINE);
-        TOKENCASE(TOKEN_PP_UNDEF);
-        TOKENCASE(TOKEN_PP_IF);
-        TOKENCASE(TOKEN_PP_IFDEF);
-        TOKENCASE(TOKEN_PP_IFNDEF);
-        TOKENCASE(TOKEN_PP_ELSE);
-        TOKENCASE(TOKEN_PP_ELIF);
-        TOKENCASE(TOKEN_PP_ENDIF);
-        TOKENCASE(TOKEN_PP_ERROR);
-        TOKENCASE(TOKEN_PP_INCOMPLETE_COMMENT);
-        TOKENCASE(TOKEN_EOI);
-        #undef TOKENCASE
-
-        case ((Token) '\n'):
-            printf("'\\n'");
-            break;
-
-        default:
-            assert(((int)ctx->tokenval) < 256);
-            printf("'%c'", (char) ctx->tokenval);
-            break;
-    } // switch
-    printf(")\n");
-}
-#endif
-
 static Token nexttoken(Context *ctx)
 {
     if (ctx->pushedback)
     {
-        print_debug_token(ctx);
+        print_debug_token(ctx->token, ctx->tokenlen, ctx->tokenval);
         ctx->pushedback = 0;
         return ctx->tokenval;
     } // if
@@ -320,7 +255,7 @@
             token = _nexttoken(ctx);  // skip endlines.
     } // if
 
-    print_debug_token(ctx);
+    print_debug_token(ctx->token, ctx->tokenlen, ctx->tokenval);
     return token;
 } // nexttoken
 
--- a/mojoshader_internal.h	Thu Feb 12 17:12:46 2009 -0500
+++ b/mojoshader_internal.h	Thu Feb 12 17:25:49 2009 -0500
@@ -16,6 +16,10 @@
 
 #include "mojoshader.h"
 
+#define DEBUG_PREPROCESSOR 0
+#define DEBUG_ASSEMBLY_PARSER 0
+#define DEBUG_TOKENIZER ((DEBUG_PREPROCESSOR) || (DEBUG_ASSEMBLY_PARSER))
+
 #if (defined(__APPLE__) && defined(__MACH__))
 #define PLATFORM_MACOSX 1
 #endif
@@ -360,6 +364,12 @@
 const char *preprocessor_sourcepos(Preprocessor *pp, unsigned int *pos);
 
 
+#if DEBUG_TOKENIZER
+void MOJOSHADER_print_debug_token(const char *subsystem, const char *token,
+                                  const unsigned int tokenlen,
+                                  const Token tokenval);
+#endif
+
 #endif  // _INCLUDE_MOJOSHADER_INTERNAL_H_
 
 
--- a/mojoshader_preprocessor.c	Thu Feb 12 17:12:46 2009 -0500
+++ b/mojoshader_preprocessor.c	Thu Feb 12 17:25:49 2009 -0500
@@ -10,7 +10,12 @@
 #define __MOJOSHADER_INTERNAL__ 1
 #include "mojoshader_internal.h"
 
-#define DEBUG_TOKENIZER 0
+#if DEBUG_PREPROCESSOR
+    #define print_debug_token(token, len, val) \
+        MOJOSHADER_print_debug_token("PREPROCESSOR", token, len, val)
+#else
+    #define print_debug_token(token, len, val)
+#endif
 
 typedef struct DefineHash
 {
@@ -81,6 +86,77 @@
 } // fail
 
 
+#if DEBUG_TOKENIZER
+void MOJOSHADER_print_debug_token(const char *subsystem, const char *token,
+                                  const unsigned int tokenlen,
+                                  const Token tokenval)
+{
+    printf("%s TOKEN: \"", subsystem);
+    unsigned int i;
+    for (i = 0; i < tokenlen; i++)
+    {
+        if (token[i] == '\n')
+            printf("\\n");
+        else
+            printf("%c", token[i]);
+    } // for
+    printf("\" (");
+    switch (tokenval)
+    {
+        #define TOKENCASE(x) case x: printf("%s", #x); break
+        TOKENCASE(TOKEN_UNKNOWN);
+        TOKENCASE(TOKEN_IDENTIFIER);
+        TOKENCASE(TOKEN_INT_LITERAL);
+        TOKENCASE(TOKEN_FLOAT_LITERAL);
+        TOKENCASE(TOKEN_STRING_LITERAL);
+        TOKENCASE(TOKEN_ADDASSIGN);
+        TOKENCASE(TOKEN_SUBASSIGN);
+        TOKENCASE(TOKEN_MULTASSIGN);
+        TOKENCASE(TOKEN_DIVASSIGN);
+        TOKENCASE(TOKEN_MODASSIGN);
+        TOKENCASE(TOKEN_XORASSIGN);
+        TOKENCASE(TOKEN_ANDASSIGN);
+        TOKENCASE(TOKEN_ORASSIGN);
+        TOKENCASE(TOKEN_INCREMENT);
+        TOKENCASE(TOKEN_DECREMENT);
+        TOKENCASE(TOKEN_RSHIFT);
+        TOKENCASE(TOKEN_LSHIFT);
+        TOKENCASE(TOKEN_ANDAND);
+        TOKENCASE(TOKEN_OROR);
+        TOKENCASE(TOKEN_LEQ);
+        TOKENCASE(TOKEN_GEQ);
+        TOKENCASE(TOKEN_EQL);
+        TOKENCASE(TOKEN_NEQ);
+        TOKENCASE(TOKEN_HASHHASH);
+        TOKENCASE(TOKEN_PP_INCLUDE);
+        TOKENCASE(TOKEN_PP_LINE);
+        TOKENCASE(TOKEN_PP_DEFINE);
+        TOKENCASE(TOKEN_PP_UNDEF);
+        TOKENCASE(TOKEN_PP_IF);
+        TOKENCASE(TOKEN_PP_IFDEF);
+        TOKENCASE(TOKEN_PP_IFNDEF);
+        TOKENCASE(TOKEN_PP_ELSE);
+        TOKENCASE(TOKEN_PP_ELIF);
+        TOKENCASE(TOKEN_PP_ENDIF);
+        TOKENCASE(TOKEN_PP_ERROR);
+        TOKENCASE(TOKEN_PP_INCOMPLETE_COMMENT);
+        TOKENCASE(TOKEN_EOI);
+        #undef TOKENCASE
+
+        case ((Token) '\n'):
+            printf("'\\n'");
+            break;
+
+        default:
+            assert(((int)tokenval) < 256);
+            printf("'%c'", (char) tokenval);
+            break;
+    } // switch
+    printf(")\n");
+} // MOJOSHADER_print_debug_token
+#endif
+
+
 // Preprocessor define hashtable stuff...
 
 static unsigned char hash_define(const char *sym)
@@ -372,74 +448,7 @@
                                    Token *token)
 {
     const char *retval = _preprocessor_nexttoken(ctx, len, token);
-
-    #if DEBUG_TOKENIZER
-    printf("PREPROCESSOR TOKEN: \"");
-    unsigned int i;
-    for (i = 0; i < *len; i++)
-    {
-        if (retval[i] == '\n')
-            printf("\\n");
-        else
-            printf("%c", retval[i]);
-    } // for
-    printf("\" (");
-    switch (*token)
-    {
-        #define TOKENCASE(x) case x: printf("%s", #x); break
-        TOKENCASE(TOKEN_UNKNOWN);
-        TOKENCASE(TOKEN_IDENTIFIER);
-        TOKENCASE(TOKEN_INT_LITERAL);
-        TOKENCASE(TOKEN_FLOAT_LITERAL);
-        TOKENCASE(TOKEN_STRING_LITERAL);
-        TOKENCASE(TOKEN_RSHIFTASSIGN);
-        TOKENCASE(TOKEN_LSHIFTASSIGN);
-        TOKENCASE(TOKEN_ADDASSIGN);
-        TOKENCASE(TOKEN_SUBASSIGN);
-        TOKENCASE(TOKEN_MULTASSIGN);
-        TOKENCASE(TOKEN_DIVASSIGN);
-        TOKENCASE(TOKEN_MODASSIGN);
-        TOKENCASE(TOKEN_XORASSIGN);
-        TOKENCASE(TOKEN_ANDASSIGN);
-        TOKENCASE(TOKEN_ORASSIGN);
-        TOKENCASE(TOKEN_INCREMENT);
-        TOKENCASE(TOKEN_DECREMENT);
-        TOKENCASE(TOKEN_RSHIFT);
-        TOKENCASE(TOKEN_LSHIFT);
-        TOKENCASE(TOKEN_ANDAND);
-        TOKENCASE(TOKEN_OROR);
-        TOKENCASE(TOKEN_LEQ);
-        TOKENCASE(TOKEN_GEQ);
-        TOKENCASE(TOKEN_EQL);
-        TOKENCASE(TOKEN_NEQ);
-        TOKENCASE(TOKEN_HASHHASH);
-        TOKENCASE(TOKEN_PP_INCLUDE);
-        TOKENCASE(TOKEN_PP_LINE);
-        TOKENCASE(TOKEN_PP_DEFINE);
-        TOKENCASE(TOKEN_PP_UNDEF);
-        TOKENCASE(TOKEN_PP_IF);
-        TOKENCASE(TOKEN_PP_IFDEF);
-        TOKENCASE(TOKEN_PP_IFNDEF);
-        TOKENCASE(TOKEN_PP_ELSE);
-        TOKENCASE(TOKEN_PP_ELIF);
-        TOKENCASE(TOKEN_PP_ENDIF);
-        TOKENCASE(TOKEN_PP_ERROR);
-        TOKENCASE(TOKEN_PP_INCOMPLETE_COMMENT);
-        TOKENCASE(TOKEN_EOI);
-        #undef TOKENCASE
-
-        case ((Token) '\n'):
-            printf("'\\n'");
-            break;
-
-        default:
-            assert(((int)*token) < 256);
-            printf("'%c'", (char) *token);
-            break;
-    } // switch
-    printf(")\n");
-    #endif
-
+    print_debug_token(retval, *len, *token);
     return retval;
 } // preprocessor_nexttoken