mojoshader_compiler.c
changeset 1015 87c39e720f36
parent 1014 7db1c2dfead6
child 1016 9c082b107efe
equal deleted inserted replaced
1014:7db1c2dfead6 1015:87c39e720f36
   110     int var_index;  // next variable index for current function.
   110     int var_index;  // next variable index for current function.
   111     int global_var_index;  // next variable index for global scope.
   111     int global_var_index;  // next variable index for global scope.
   112     int user_func_index;  // next function index for user-defined functions.
   112     int user_func_index;  // next function index for user-defined functions.
   113     int intrinsic_func_index;  // next function index for intrinsic functions.
   113     int intrinsic_func_index;  // next function index for intrinsic functions.
   114 
   114 
   115     MOJOSHADER_irNode *ir;  // intermediate representation.
   115     MOJOSHADER_irStatement **ir;  // intermediate representation.
   116     int ir_label_count;  // next unused IR label index.
   116     int ir_label_count;  // next unused IR label index.
   117     int ir_temp_count;  // next unused IR temporary value index.
   117     int ir_temp_count;  // next unused IR temporary value index.
   118     int ir_end; // current function's end label during IR build.
   118     int ir_end; // current function's end label during IR build.
   119     int ir_ret; // temp that holds current function's retval during IR build.
   119     int ir_ret; // temp that holds current function's retval during IR build.
   120     LoopLabels *ir_loop;  // nested loop boundary labels during IR build.
   120     LoopLabels *ir_loop;  // nested loop boundary labels during IR build.
  3548 {
  3548 {
  3549     if (ctx != NULL)
  3549     if (ctx != NULL)
  3550     {
  3550     {
  3551         MOJOSHADER_free f = ((ctx->free != NULL) ? ctx->free : MOJOSHADER_internal_free);
  3551         MOJOSHADER_free f = ((ctx->free != NULL) ? ctx->free : MOJOSHADER_internal_free);
  3552         void *d = ctx->malloc_data;
  3552         void *d = ctx->malloc_data;
       
  3553         size_t i = 0;
  3553 
  3554 
  3554         // !!! FIXME: this is kinda hacky.
  3555         // !!! FIXME: this is kinda hacky.
  3555         const size_t count = buffer_size(ctx->garbage) / sizeof (void *);
  3556         const size_t count = buffer_size(ctx->garbage) / sizeof (void *);
  3556         if (count > 0)
  3557         if (count > 0)
  3557         {
  3558         {
  3558             void **garbage = (void **) buffer_flatten(ctx->garbage);
  3559             void **garbage = (void **) buffer_flatten(ctx->garbage);
  3559             if (garbage != NULL)
  3560             if (garbage != NULL)
  3560             {
  3561             {
  3561                 size_t i;
       
  3562                 for (i = 0; i < count; i++)
  3562                 for (i = 0; i < count; i++)
  3563                     f(garbage[i], d);
  3563                     f(garbage[i], d);
  3564                 f(garbage, d);
  3564                 f(garbage, d);
  3565             } // if
  3565             } // if
  3566         } // if
  3566         } // if
  3571         destroy_symbolmap(ctx, &ctx->variables);
  3571         destroy_symbolmap(ctx, &ctx->variables);
  3572         stringcache_destroy(ctx->strcache);
  3572         stringcache_destroy(ctx->strcache);
  3573         errorlist_destroy(ctx->errors);
  3573         errorlist_destroy(ctx->errors);
  3574         errorlist_destroy(ctx->warnings);
  3574         errorlist_destroy(ctx->warnings);
  3575 
  3575 
  3576         delete_ir(ctx, ctx->ir);
  3576         if (ctx->ir != NULL)
       
  3577         {
       
  3578             for (i = 0; i <= ctx->user_func_index; i++)
       
  3579                 delete_ir(ctx, ctx->ir[i]);
       
  3580             f(ctx->ir, d);
       
  3581         } // if
  3577 
  3582 
  3578         // !!! FIXME: more to clean up here, now.
  3583         // !!! FIXME: more to clean up here, now.
  3579 
  3584 
  3580         f(ctx, d);
  3585         f(ctx, d);
  3581     } // if
  3586     } // if
  5889     } // switch
  5894     } // switch
  5890 
  5895 
  5891     Free(ctx, ir);
  5896     Free(ctx, ir);
  5892 } // delete_ir
  5897 } // delete_ir
  5893 
  5898 
       
  5899 
       
  5900 static void print_whole_ir(Context *ctx, FILE *io)
       
  5901 {
       
  5902     if (ctx->ir != NULL)
       
  5903     {
       
  5904         int i;
       
  5905         for (i = 0; i <= ctx->user_func_index; i++)
       
  5906         {
       
  5907             printf("[FUNCTION %d ]\n", i);
       
  5908             print_ir(io, 1, ctx->ir[i]);
       
  5909         } // for
       
  5910     } // if
       
  5911 } // print_whole_ir
       
  5912 
       
  5913 
  5894 static void intermediate_representation(Context *ctx)
  5914 static void intermediate_representation(Context *ctx)
  5895 {
  5915 {
  5896     const MOJOSHADER_astCompilationUnit *ast = NULL;
  5916     const MOJOSHADER_astCompilationUnit *ast = NULL;
  5897     const MOJOSHADER_astCompilationUnitFunction *astfn = NULL;
  5917     const MOJOSHADER_astCompilationUnitFunction *astfn = NULL;
       
  5918     const size_t arraylen = (ctx->user_func_index+1) * sizeof (MOJOSHADER_irStatement *);
       
  5919 
       
  5920     ctx->ir = Malloc(ctx, arraylen);
       
  5921     if (ctx->ir == NULL)
       
  5922         return;
       
  5923     memset(ctx->ir, '\0', arraylen);
  5898 
  5924 
  5899     ctx->ir_end = -1;
  5925     ctx->ir_end = -1;
  5900     ctx->ir_ret = -1;
  5926     ctx->ir_ret = -1;
  5901 
  5927 
  5902     for (ast = &ctx->ast->compunit; ast != NULL; ast = ast->next)
  5928     for (ast = &ctx->ast->compunit; ast != NULL; ast = ast->next)
  5924         funcseq = new_ir_seq(ctx, funcseq, new_ir_label(ctx, end));
  5950         funcseq = new_ir_seq(ctx, funcseq, new_ir_label(ctx, end));
  5925         assert(ctx->ir_loop == NULL);  // parser should have caught this!
  5951         assert(ctx->ir_loop == NULL);  // parser should have caught this!
  5926         ctx->ir_end = -1;
  5952         ctx->ir_end = -1;
  5927         ctx->ir_ret = -1;
  5953         ctx->ir_ret = -1;
  5928 
  5954 
  5929 printf("[FUNCTION %d ]\n", astfn->index); print_ir(stdout, 1, funcseq);
  5955         assert(astfn->index <= ctx->user_func_index);
       
  5956         assert(ctx->ir[astfn->index] == NULL);
       
  5957         ctx->ir[astfn->index] = funcseq;
  5930     } // for
  5958     } // for
  5931 
  5959 
       
  5960     print_whole_ir(ctx, stdout);
       
  5961 
  5932     // done with the AST, nuke it.
  5962     // done with the AST, nuke it.
       
  5963     // !!! FIXME: we're going to need CTAB data from this at some point.
  5933     delete_compilation_unit(ctx, (MOJOSHADER_astCompilationUnit *) ctx->ast);
  5964     delete_compilation_unit(ctx, (MOJOSHADER_astCompilationUnit *) ctx->ast);
  5934     ctx->ast = NULL;
  5965     ctx->ast = NULL;
  5935 } // intermediate_representation
  5966 } // intermediate_representation
  5936 
  5967 
  5937 
  5968