mojoshader_preprocessor.c
changeset 676 29447167fc68
parent 675 e85587e4650d
child 677 75e28bb57b2d
equal deleted inserted replaced
675:e85587e4650d 676:29447167fc68
    25     return retval;
    25     return retval;
    26 } // debug_preprocessor_lexer
    26 } // debug_preprocessor_lexer
    27 #define preprocessor_lexer(s) debug_preprocessor_lexer(s)
    27 #define preprocessor_lexer(s) debug_preprocessor_lexer(s)
    28 #endif
    28 #endif
    29 
    29 
    30 typedef struct DefineHash
    30 typedef struct Define
    31 {
    31 {
    32     const char *identifier;
    32     const char *identifier;
    33     const char *definition;
    33     const char *definition;
    34     struct DefineHash *next;
    34     struct Define *next;
    35 } DefineHash;
    35 } Define;
    36 
    36 
    37 
    37 
    38 // Simple linked list to cache source filenames, so we don't have to copy
    38 // Simple linked list to cache source filenames, so we don't have to copy
    39 //  the same string over and over for each opcode.
    39 //  the same string over and over for each opcode.
    40 typedef struct FilenameCache
    40 typedef struct FilenameCache
    49     int out_of_memory;
    49     int out_of_memory;
    50     char failstr[256];
    50     char failstr[256];
    51     Conditional *conditional_pool;
    51     Conditional *conditional_pool;
    52     IncludeState *include_stack;
    52     IncludeState *include_stack;
    53     IncludeState *include_pool;
    53     IncludeState *include_pool;
    54     DefineHash *define_hashtable[256];
    54     Define *define_hashtable[256];
    55     DefineHash *define_pool;
    55     Define *define_pool;
    56     FilenameCache *filename_cache;
    56     FilenameCache *filename_cache;
    57     MOJOSHADER_includeOpen open_callback;
    57     MOJOSHADER_includeOpen open_callback;
    58     MOJOSHADER_includeClose close_callback;
    58     MOJOSHADER_includeClose close_callback;
    59     MOJOSHADER_malloc malloc;
    59     MOJOSHADER_malloc malloc;
    60     MOJOSHADER_free free;
    60     MOJOSHADER_free free;
   370     GET_POOL(type, poolname) \
   370     GET_POOL(type, poolname) \
   371     PUT_POOL(type, poolname)
   371     PUT_POOL(type, poolname)
   372 
   372 
   373 IMPLEMENT_POOL(Conditional, conditional)
   373 IMPLEMENT_POOL(Conditional, conditional)
   374 IMPLEMENT_POOL(IncludeState, include)
   374 IMPLEMENT_POOL(IncludeState, include)
   375 IMPLEMENT_POOL(DefineHash, define)
   375 IMPLEMENT_POOL(Define, define)
   376 
   376 
   377 
   377 
   378 // Preprocessor define hashtable stuff...
   378 // Preprocessor define hashtable stuff...
   379 
   379 
   380 static unsigned char hash_define(const char *sym)
   380 static unsigned char hash_define(const char *sym)
   389 static int add_define(Context *ctx, const char *sym, const char *val, int copy)
   389 static int add_define(Context *ctx, const char *sym, const char *val, int copy)
   390 {
   390 {
   391     char *identifier = NULL;
   391     char *identifier = NULL;
   392     char *definition = NULL;
   392     char *definition = NULL;
   393     const unsigned char hash = hash_define(sym);
   393     const unsigned char hash = hash_define(sym);
   394     DefineHash *bucket = ctx->define_hashtable[hash];
   394     Define *bucket = ctx->define_hashtable[hash];
   395     while (bucket)
   395     while (bucket)
   396     {
   396     {
   397         if (strcmp(bucket->identifier, sym) == 0)
   397         if (strcmp(bucket->identifier, sym) == 0)
   398         {
   398         {
   399             failf(ctx, "'%s' already defined", sym);
   399             failf(ctx, "'%s' already defined", sym);
   435 
   435 
   436 
   436 
   437 static int remove_define(Context *ctx, const char *sym)
   437 static int remove_define(Context *ctx, const char *sym)
   438 {
   438 {
   439     const unsigned char hash = hash_define(sym);
   439     const unsigned char hash = hash_define(sym);
   440     DefineHash *bucket = ctx->define_hashtable[hash];
   440     Define *bucket = ctx->define_hashtable[hash];
   441     DefineHash *prev = NULL;
   441     Define *prev = NULL;
   442     while (bucket)
   442     while (bucket)
   443     {
   443     {
   444         if (strcmp(bucket->identifier, sym) == 0)
   444         if (strcmp(bucket->identifier, sym) == 0)
   445         {
   445         {
   446             if (prev == NULL)
   446             if (prev == NULL)
   461 
   461 
   462 
   462 
   463 static const char *find_define(Context *ctx, const char *sym)
   463 static const char *find_define(Context *ctx, const char *sym)
   464 {
   464 {
   465     const unsigned char hash = hash_define(sym);
   465     const unsigned char hash = hash_define(sym);
   466     DefineHash *bucket = ctx->define_hashtable[hash];
   466     Define *bucket = ctx->define_hashtable[hash];
   467     while (bucket)
   467     while (bucket)
   468     {
   468     {
   469         if (strcmp(bucket->identifier, sym) == 0)
   469         if (strcmp(bucket->identifier, sym) == 0)
   470             return bucket->definition;
   470             return bucket->definition;
   471         bucket = bucket->next;
   471         bucket = bucket->next;
   477 static void put_all_defines(Context *ctx)
   477 static void put_all_defines(Context *ctx)
   478 {
   478 {
   479     int i;
   479     int i;
   480     for (i = 0; i < STATICARRAYLEN(ctx->define_hashtable); i++)
   480     for (i = 0; i < STATICARRAYLEN(ctx->define_hashtable); i++)
   481     {
   481     {
   482         DefineHash *bucket = ctx->define_hashtable[i];
   482         Define *bucket = ctx->define_hashtable[i];
   483         ctx->define_hashtable[i] = NULL;
   483         ctx->define_hashtable[i] = NULL;
   484         while (bucket)
   484         while (bucket)
   485         {
   485         {
   486             DefineHash *next = bucket->next;
   486             Define *next = bucket->next;
   487             Free(ctx, (void *) bucket->identifier);
   487             Free(ctx, (void *) bucket->identifier);
   488             Free(ctx, (void *) bucket->definition);
   488             Free(ctx, (void *) bucket->definition);
   489             put_define(ctx, bucket);
   489             put_define(ctx, bucket);
   490             bucket = next;
   490             bucket = next;
   491         } // while
   491         } // while