mojoshader_assembler.c
changeset 587 202354e004fc
parent 586 321a19a62989
child 598 decc32dc03a7
equal deleted inserted replaced
586:321a19a62989 587:202354e004fc
     8  */
     8  */
     9 
     9 
    10 #define __MOJOSHADER_INTERNAL__ 1
    10 #define __MOJOSHADER_INTERNAL__ 1
    11 #include "mojoshader_internal.h"
    11 #include "mojoshader_internal.h"
    12 
    12 
    13 #define DEBUG_ASSEMBLY_PARSER 0
    13 #if DEBUG_ASSEMBLY_PARSER
       
    14     #define print_debug_token(token, len, val) \
       
    15         MOJOSHADER_print_debug_token("ASSEMBLER", token, len, val)
       
    16 #else
       
    17     #define print_debug_token(token, len, val)
       
    18 #endif
       
    19 
    14 
    20 
    15 // Simple linked list to cache source filenames, so we don't have to copy
    21 // Simple linked list to cache source filenames, so we don't have to copy
    16 //  the same string over and over for each opcode.
    22 //  the same string over and over for each opcode.
    17 typedef struct FilenameCache
    23 typedef struct FilenameCache
    18 {
    24 {
   222 
   228 
   223     return ctx->tokenval;
   229     return ctx->tokenval;
   224 } // _nexttoken
   230 } // _nexttoken
   225 
   231 
   226 
   232 
   227 // !!! FIXME: cut-and-paste from preprocessor.
       
   228 #if !DEBUG_ASSEMBLY_PARSER
       
   229 #define print_debug_token(ctx)
       
   230 #else
       
   231 static void print_debug_token(Context *ctx)
       
   232 {
       
   233     printf("ASSEMBLER TOKEN: \"");
       
   234     unsigned int i;
       
   235     for (i = 0; i < ctx->tokenlen; i++)
       
   236     {
       
   237         if (ctx->token[i] == '\n')
       
   238             printf("\\n");
       
   239         else
       
   240             printf("%c", ctx->token[i]);
       
   241     } // for
       
   242     printf("\" (");
       
   243     switch (ctx->tokenval)
       
   244     {
       
   245         #define TOKENCASE(x) case x: printf("%s", #x); break
       
   246         TOKENCASE(TOKEN_UNKNOWN);
       
   247         TOKENCASE(TOKEN_IDENTIFIER);
       
   248         TOKENCASE(TOKEN_INT_LITERAL);
       
   249         TOKENCASE(TOKEN_FLOAT_LITERAL);
       
   250         TOKENCASE(TOKEN_STRING_LITERAL);
       
   251         TOKENCASE(TOKEN_ADDASSIGN);
       
   252         TOKENCASE(TOKEN_SUBASSIGN);
       
   253         TOKENCASE(TOKEN_MULTASSIGN);
       
   254         TOKENCASE(TOKEN_DIVASSIGN);
       
   255         TOKENCASE(TOKEN_MODASSIGN);
       
   256         TOKENCASE(TOKEN_XORASSIGN);
       
   257         TOKENCASE(TOKEN_ANDASSIGN);
       
   258         TOKENCASE(TOKEN_ORASSIGN);
       
   259         TOKENCASE(TOKEN_INCREMENT);
       
   260         TOKENCASE(TOKEN_DECREMENT);
       
   261         TOKENCASE(TOKEN_RSHIFT);
       
   262         TOKENCASE(TOKEN_LSHIFT);
       
   263         TOKENCASE(TOKEN_ANDAND);
       
   264         TOKENCASE(TOKEN_OROR);
       
   265         TOKENCASE(TOKEN_LEQ);
       
   266         TOKENCASE(TOKEN_GEQ);
       
   267         TOKENCASE(TOKEN_EQL);
       
   268         TOKENCASE(TOKEN_NEQ);
       
   269         TOKENCASE(TOKEN_HASHHASH);
       
   270         TOKENCASE(TOKEN_PP_INCLUDE);
       
   271         TOKENCASE(TOKEN_PP_LINE);
       
   272         TOKENCASE(TOKEN_PP_DEFINE);
       
   273         TOKENCASE(TOKEN_PP_UNDEF);
       
   274         TOKENCASE(TOKEN_PP_IF);
       
   275         TOKENCASE(TOKEN_PP_IFDEF);
       
   276         TOKENCASE(TOKEN_PP_IFNDEF);
       
   277         TOKENCASE(TOKEN_PP_ELSE);
       
   278         TOKENCASE(TOKEN_PP_ELIF);
       
   279         TOKENCASE(TOKEN_PP_ENDIF);
       
   280         TOKENCASE(TOKEN_PP_ERROR);
       
   281         TOKENCASE(TOKEN_PP_INCOMPLETE_COMMENT);
       
   282         TOKENCASE(TOKEN_EOI);
       
   283         #undef TOKENCASE
       
   284 
       
   285         case ((Token) '\n'):
       
   286             printf("'\\n'");
       
   287             break;
       
   288 
       
   289         default:
       
   290             assert(((int)ctx->tokenval) < 256);
       
   291             printf("'%c'", (char) ctx->tokenval);
       
   292             break;
       
   293     } // switch
       
   294     printf(")\n");
       
   295 }
       
   296 #endif
       
   297 
       
   298 static Token nexttoken(Context *ctx)
   233 static Token nexttoken(Context *ctx)
   299 {
   234 {
   300     if (ctx->pushedback)
   235     if (ctx->pushedback)
   301     {
   236     {
   302         print_debug_token(ctx);
   237         print_debug_token(ctx->token, ctx->tokenlen, ctx->tokenval);
   303         ctx->pushedback = 0;
   238         ctx->pushedback = 0;
   304         return ctx->tokenval;
   239         return ctx->tokenval;
   305     } // if
   240     } // if
   306 
   241 
   307     Token token = _nexttoken(ctx);
   242     Token token = _nexttoken(ctx);
   318 
   253 
   319         while (token == ((Token) '\n'))
   254         while (token == ((Token) '\n'))
   320             token = _nexttoken(ctx);  // skip endlines.
   255             token = _nexttoken(ctx);  // skip endlines.
   321     } // if
   256     } // if
   322 
   257 
   323     print_debug_token(ctx);
   258     print_debug_token(ctx->token, ctx->tokenlen, ctx->tokenval);
   324     return token;
   259     return token;
   325 } // nexttoken
   260 } // nexttoken
   326 
   261 
   327 
   262 
   328 static const char *cache_filename(Context *ctx, const char *fname)
   263 static const char *cache_filename(Context *ctx, const char *fname)