mojoshader_compiler.c
changeset 1002 f47e8025c2a1
parent 1000 0f4a8ec6bd9b
child 1003 05f4a65482f9
equal deleted inserted replaced
1001:61991d334619 1002:f47e8025c2a1
  2382     } // if
  2382     } // if
  2383 
  2383 
  2384     return DT_MATCH_INCOMPATIBLE;
  2384     return DT_MATCH_INCOMPATIBLE;
  2385 } // compatible_arg_datatype
  2385 } // compatible_arg_datatype
  2386 
  2386 
  2387 static void print_ast_datatype(FILE *io, const MOJOSHADER_astDataType *dt)
       
  2388 {
       
  2389     int i;
       
  2390 
       
  2391     if (dt == NULL)
       
  2392         return;
       
  2393 
       
  2394     switch (dt->type)
       
  2395     {
       
  2396         case MOJOSHADER_AST_DATATYPE_BOOL:
       
  2397             fprintf(io, "bool");
       
  2398             return;
       
  2399         case MOJOSHADER_AST_DATATYPE_INT:
       
  2400             fprintf(io, "int");
       
  2401             return;
       
  2402         case MOJOSHADER_AST_DATATYPE_UINT:
       
  2403             fprintf(io, "uint");
       
  2404             return;
       
  2405         case MOJOSHADER_AST_DATATYPE_FLOAT:
       
  2406             fprintf(io, "float");
       
  2407             return;
       
  2408         case MOJOSHADER_AST_DATATYPE_FLOAT_SNORM:
       
  2409             fprintf(io, "snorm float");
       
  2410             return;
       
  2411         case MOJOSHADER_AST_DATATYPE_FLOAT_UNORM:
       
  2412             fprintf(io, "unorm float");
       
  2413             return;
       
  2414         case MOJOSHADER_AST_DATATYPE_HALF:
       
  2415             fprintf(io, "half");
       
  2416             return;
       
  2417         case MOJOSHADER_AST_DATATYPE_DOUBLE:
       
  2418             fprintf(io, "double");
       
  2419             return;
       
  2420         case MOJOSHADER_AST_DATATYPE_STRING:
       
  2421             fprintf(io, "string");
       
  2422             return;
       
  2423         case MOJOSHADER_AST_DATATYPE_SAMPLER_1D:
       
  2424             fprintf(io, "sampler1D");
       
  2425             return;
       
  2426         case MOJOSHADER_AST_DATATYPE_SAMPLER_2D:
       
  2427             fprintf(io, "sampler2D");
       
  2428             return;
       
  2429         case MOJOSHADER_AST_DATATYPE_SAMPLER_3D:
       
  2430             fprintf(io, "sampler3D");
       
  2431             return;
       
  2432         case MOJOSHADER_AST_DATATYPE_SAMPLER_CUBE:
       
  2433             fprintf(io, "samplerCUBE");
       
  2434             return;
       
  2435         case MOJOSHADER_AST_DATATYPE_SAMPLER_STATE:
       
  2436             fprintf(io, "sampler_state");
       
  2437             return;
       
  2438         case MOJOSHADER_AST_DATATYPE_SAMPLER_COMPARISON_STATE:
       
  2439             fprintf(io, "SamplerComparisonState");
       
  2440             return;
       
  2441 
       
  2442         case MOJOSHADER_AST_DATATYPE_STRUCT:
       
  2443             fprintf(io, "struct { ");
       
  2444             for (i = 0; i < dt->structure.member_count; i++)
       
  2445             {
       
  2446                 print_ast_datatype(io, dt->structure.members[i].datatype);
       
  2447                 fprintf(io, " %s; ", dt->structure.members[i].identifier);
       
  2448             } // for
       
  2449             fprintf(io, "}");
       
  2450             return;
       
  2451 
       
  2452         case MOJOSHADER_AST_DATATYPE_ARRAY:
       
  2453             print_ast_datatype(io, dt->array.base);
       
  2454             if (dt->array.elements < 0)
       
  2455                 fprintf(io, "[]");
       
  2456             else
       
  2457                 fprintf(io, "[%d]", dt->array.elements);
       
  2458             return;
       
  2459 
       
  2460         case MOJOSHADER_AST_DATATYPE_VECTOR:
       
  2461             fprintf(io, "vector<");
       
  2462             print_ast_datatype(io, dt->vector.base);
       
  2463             fprintf(io, ",%d>", dt->vector.elements);
       
  2464             return;
       
  2465 
       
  2466         case MOJOSHADER_AST_DATATYPE_MATRIX:
       
  2467             fprintf(io, "matrix<");
       
  2468             print_ast_datatype(io, dt->matrix.base);
       
  2469             fprintf(io, ",%d,%d>", dt->matrix.rows, dt->matrix.columns);
       
  2470             return;
       
  2471 
       
  2472         case MOJOSHADER_AST_DATATYPE_BUFFER:
       
  2473             fprintf(io, "buffer<");
       
  2474             print_ast_datatype(io, dt->buffer.base);
       
  2475             fprintf(io, ">");
       
  2476             return;
       
  2477 
       
  2478         case MOJOSHADER_AST_DATATYPE_USER:
       
  2479             fprintf(io, "%s", dt->user.name);
       
  2480             return;
       
  2481 
       
  2482         //case MOJOSHADER_AST_DATATYPE_NONE:
       
  2483         //case MOJOSHADER_AST_DATATYPE_FUNCTION:
       
  2484 
       
  2485         default:
       
  2486             assert(0 && "Unexpected datatype.");
       
  2487             return;
       
  2488     } // switch
       
  2489 } // print_ast_datatype
       
  2490 
       
  2491 
  2387 
  2492 static const MOJOSHADER_astDataType *type_check_ast(Context *ctx, void *_ast);
  2388 static const MOJOSHADER_astDataType *type_check_ast(Context *ctx, void *_ast);
  2493 
  2389 
  2494 // !!! FIXME: this function sucks.
  2390 // !!! FIXME: this function sucks.
  2495 static const MOJOSHADER_astDataType *match_func_to_call(Context *ctx,
  2391 static const MOJOSHADER_astDataType *match_func_to_call(Context *ctx,
  2508     {
  2404     {
  2509         argcount++;
  2405         argcount++;
  2510         type_check_ast(ctx, args->argument);
  2406         type_check_ast(ctx, args->argument);
  2511         args = args->next;
  2407         args = args->next;
  2512     } // while;
  2408     } // while;
  2513 
       
  2514 // !!! FIXME: remove this debug code later.
       
  2515 #define DEBUG_OVERLOADS 0
       
  2516 #if DEBUG_OVERLOADS
       
  2517 printf("Attempt to call function %s(", sym);
       
  2518 args = ast->args;
       
  2519 int qq = 0;
       
  2520 for (qq = 0; qq < argcount; qq++)
       
  2521 {
       
  2522 assert(args != NULL);
       
  2523 const MOJOSHADER_astDataType *x = args->argument->datatype;
       
  2524 args = args->next;
       
  2525 print_ast_datatype(stdout, x);
       
  2526 if (args) printf(", ");
       
  2527 }
       
  2528 printf("); ...\n");
       
  2529 #endif
       
  2530 
  2409 
  2531     // we do some tapdancing to handle function overloading here.
  2410     // we do some tapdancing to handle function overloading here.
  2532     int match = 0;
  2411     int match = 0;
  2533     while (hash_iter(ctx->variables.hash, sym, &value, &iter))
  2412     while (hash_iter(ctx->variables.hash, sym, &value, &iter))
  2534     {
  2413     {
  2570         if (score == 0)  // incompatible.
  2449         if (score == 0)  // incompatible.
  2571             continue;
  2450             continue;
  2572 
  2451 
  2573         else if (score == perfect)  // perfection! stop looking!
  2452         else if (score == perfect)  // perfection! stop looking!
  2574         {
  2453         {
  2575 #if DEBUG_OVERLOADS
       
  2576 FILE *io = stdout;
       
  2577 printf("%d PERFECT MATCH (%d/%d): ", ctx->sourceline, score, perfect);
       
  2578 if (dtfn->intrinsic)
       
  2579     printf("/* intrinsic */ ");
       
  2580 if (dtfn->retval)
       
  2581     print_ast_datatype(io, dtfn->retval);
       
  2582 else
       
  2583     printf("void");
       
  2584 printf(" %s(", sym);
       
  2585 int i;
       
  2586 for (i = 0; i < dtfn->num_params; i++) {
       
  2587     print_ast_datatype(io, dtfn->params[i]);
       
  2588     if (i < dtfn->num_params-1)
       
  2589         printf(", ");
       
  2590 }
       
  2591 printf(");\n");
       
  2592 #endif
       
  2593             match = 1;  // ignore all other compatible matches.
  2454             match = 1;  // ignore all other compatible matches.
  2594             best = item;
  2455             best = item;
  2595             break;
  2456             break;
  2596         } // if
  2457         } // if
  2597 
  2458 
  2598         else if (score >= best_score)  // compatible, but not perfect, match.
  2459         else if (score >= best_score)  // compatible, but not perfect, match.
  2599         {
  2460         {
  2600 #if DEBUG_OVERLOADS
       
  2601 FILE *io = stdout;
       
  2602 printf("%d COMPATIBLE MATCH (%d/%d): ", ctx->sourceline, score, perfect);
       
  2603 if (dtfn->intrinsic)
       
  2604     printf("/* intrinsic */ ");
       
  2605 if (dtfn->retval)
       
  2606     print_ast_datatype(io, dtfn->retval);
       
  2607 else
       
  2608     printf("void");
       
  2609 printf(" %s(", sym);
       
  2610 int i;
       
  2611 for (i = 0; i < dtfn->num_params; i++) {
       
  2612     print_ast_datatype(io, dtfn->params[i]);
       
  2613     if (i < dtfn->num_params-1)
       
  2614         printf(", ");
       
  2615 }
       
  2616 printf(");\n");
       
  2617 #endif
       
  2618 
       
  2619             if (score == best_score)
  2461             if (score == best_score)
  2620             {
  2462             {
  2621                 match++;
  2463                 match++;
  2622                 // !!! FIXME: list each possible function in a fail(),
  2464                 // !!! FIXME: list each possible function in a fail(),
  2623                 // !!! FIXME:  but you can't actually fail() here, since
  2465                 // !!! FIXME:  but you can't actually fail() here, since