mojoshader_compiler.c
changeset 1007 7bd3a6b86fd7
parent 1005 7ca34382b740
child 1009 ba0ed22a4bac
equal deleted inserted replaced
1006:0f65599179f9 1007:7bd3a6b86fd7
  2275         case MOJOSHADER_AST_DATATYPE_FLOAT_UNORM: return 1;
  2275         case MOJOSHADER_AST_DATATYPE_FLOAT_UNORM: return 1;
  2276         default: return 0;
  2276         default: return 0;
  2277     } // switch
  2277     } // switch
  2278 } // is_float_datatype
  2278 } // is_float_datatype
  2279 
  2279 
       
  2280 static int datatype_elems(Context *ctx, const MOJOSHADER_astDataType *dt)
       
  2281 {
       
  2282     dt = reduce_datatype(ctx, dt);
       
  2283     switch (dt->type)
       
  2284     {
       
  2285         case MOJOSHADER_AST_DATATYPE_VECTOR:
       
  2286             return dt->vector.elements;
       
  2287         case MOJOSHADER_AST_DATATYPE_MATRIX:
       
  2288             return dt->matrix.rows * dt->matrix.columns;
       
  2289         default:
       
  2290             return 1;
       
  2291     } // switch
       
  2292 } // datatype_elems
       
  2293 
  2280 static const MOJOSHADER_astDataType *datatype_base(Context *ctx, const MOJOSHADER_astDataType *dt)
  2294 static const MOJOSHADER_astDataType *datatype_base(Context *ctx, const MOJOSHADER_astDataType *dt)
  2281 {
  2295 {
  2282     dt = reduce_datatype(ctx, dt);
  2296     dt = reduce_datatype(ctx, dt);
  2283     switch (dt->type)
  2297     switch (dt->type)
  2284     {
  2298     {
  2569         case MOJOSHADER_AST_OP_POSTDECREMENT:
  2583         case MOJOSHADER_AST_OP_POSTDECREMENT:
  2570         case MOJOSHADER_AST_OP_PREINCREMENT:
  2584         case MOJOSHADER_AST_OP_PREINCREMENT:
  2571         case MOJOSHADER_AST_OP_PREDECREMENT:
  2585         case MOJOSHADER_AST_OP_PREDECREMENT:
  2572         case MOJOSHADER_AST_OP_COMPLEMENT:
  2586         case MOJOSHADER_AST_OP_COMPLEMENT:
  2573         case MOJOSHADER_AST_OP_NEGATE:
  2587         case MOJOSHADER_AST_OP_NEGATE:
  2574         case MOJOSHADER_AST_OP_NOT:
  2588             // !!! FIXME: must be lvalue.
       
  2589             // !!! FIXME: bools must type-promote to ...int?
       
  2590             // !!! FIXME: complement must not be float (...right?)
  2575             datatype = type_check_ast(ctx, ast->unary.operand);
  2591             datatype = type_check_ast(ctx, ast->unary.operand);
  2576             require_numeric_datatype(ctx, datatype);
  2592             require_numeric_datatype(ctx, datatype);
  2577             ast->unary.datatype = datatype;
  2593             ast->unary.datatype = datatype;
       
  2594             return datatype;
       
  2595 
       
  2596         case MOJOSHADER_AST_OP_NOT:
       
  2597             datatype = type_check_ast(ctx, ast->unary.operand);
       
  2598             require_boolean_datatype(ctx, datatype);
       
  2599             // !!! FIXME: coerce to bool here.
       
  2600             ast->unary.datatype = &ctx->dt_bool;
  2578             return datatype;
  2601             return datatype;
  2579 
  2602 
  2580         case MOJOSHADER_AST_OP_DEREF_ARRAY:
  2603         case MOJOSHADER_AST_OP_DEREF_ARRAY:
  2581             datatype = type_check_ast(ctx, ast->binary.left);
  2604             datatype = type_check_ast(ctx, ast->binary.left);
  2582             datatype2 = type_check_ast(ctx, ast->binary.right);
  2605             datatype2 = type_check_ast(ctx, ast->binary.right);
  2706         case MOJOSHADER_AST_OP_LOGICALOR:
  2729         case MOJOSHADER_AST_OP_LOGICALOR:
  2707             datatype = type_check_ast(ctx, ast->binary.left);
  2730             datatype = type_check_ast(ctx, ast->binary.left);
  2708             datatype2 = type_check_ast(ctx, ast->binary.right);
  2731             datatype2 = type_check_ast(ctx, ast->binary.right);
  2709             require_boolean_datatype(ctx, datatype);
  2732             require_boolean_datatype(ctx, datatype);
  2710             require_boolean_datatype(ctx, datatype2);
  2733             require_boolean_datatype(ctx, datatype2);
       
  2734             // !!! FIXME: coerce each to bool here, separately.
  2711             add_type_coercion(ctx, &ast->binary.left, datatype,
  2735             add_type_coercion(ctx, &ast->binary.left, datatype,
  2712                               &ast->binary.right, datatype2);
  2736                               &ast->binary.right, datatype2);
  2713             ast->binary.datatype = &ctx->dt_bool;
  2737             ast->binary.datatype = &ctx->dt_bool;
  2714 
  2738 
  2715         case MOJOSHADER_AST_OP_ASSIGN:
  2739         case MOJOSHADER_AST_OP_ASSIGN:
  2721         case MOJOSHADER_AST_OP_LSHIFTASSIGN:
  2745         case MOJOSHADER_AST_OP_LSHIFTASSIGN:
  2722         case MOJOSHADER_AST_OP_RSHIFTASSIGN:
  2746         case MOJOSHADER_AST_OP_RSHIFTASSIGN:
  2723         case MOJOSHADER_AST_OP_ANDASSIGN:
  2747         case MOJOSHADER_AST_OP_ANDASSIGN:
  2724         case MOJOSHADER_AST_OP_XORASSIGN:
  2748         case MOJOSHADER_AST_OP_XORASSIGN:
  2725         case MOJOSHADER_AST_OP_ORASSIGN:
  2749         case MOJOSHADER_AST_OP_ORASSIGN:
       
  2750             // !!! FIXME: verify binary.left is an lvalue, or fail()!
  2726             datatype = type_check_ast(ctx, ast->binary.left);
  2751             datatype = type_check_ast(ctx, ast->binary.left);
  2727             datatype2 = type_check_ast(ctx, ast->binary.right);
  2752             datatype2 = type_check_ast(ctx, ast->binary.right);
  2728             ast->binary.datatype = add_type_coercion(ctx, NULL, datatype,
  2753             ast->binary.datatype = add_type_coercion(ctx, NULL, datatype,
  2729                                                 &ast->binary.right, datatype2);
  2754                                                 &ast->binary.right, datatype2);
  2730             return ast->binary.datatype;
  2755             return ast->binary.datatype;
  2939 
  2964 
  2940         case MOJOSHADER_AST_STATEMENT_SWITCH:
  2965         case MOJOSHADER_AST_STATEMENT_SWITCH:
  2941         {
  2966         {
  2942             ctx->switch_count++;
  2967             ctx->switch_count++;
  2943             MOJOSHADER_astSwitchCases *cases = ast->switchstmt.cases;
  2968             MOJOSHADER_astSwitchCases *cases = ast->switchstmt.cases;
       
  2969             // !!! FIXME: expr must be POD (no structs, arrays, etc!).
  2944             datatype = type_check_ast(ctx, ast->switchstmt.expr);
  2970             datatype = type_check_ast(ctx, ast->switchstmt.expr);
  2945             while (cases)
  2971             while (cases)
  2946             {
  2972             {
       
  2973                 // !!! FIXME: case must be POD (no structs, arrays, etc!).
  2947                 datatype2 = type_check_ast(ctx, cases->expr);
  2974                 datatype2 = type_check_ast(ctx, cases->expr);
  2948                 add_type_coercion(ctx, NULL, datatype,
  2975                 add_type_coercion(ctx, NULL, datatype,
  2949                                   &cases->expr, datatype2);
  2976                                   &cases->expr, datatype2);
  2950                 type_check_ast(ctx, cases->statement);
  2977                 type_check_ast(ctx, cases->statement);
  2951                 cases = cases->next;
  2978                 cases = cases->next;
  3021             assert(!ctx->is_func_scope);
  3048             assert(!ctx->is_func_scope);
  3022 
  3049 
  3023             // We have to tapdance here to make sure the function is in
  3050             // We have to tapdance here to make sure the function is in
  3024             //  the global scope, but it's parameters are pushed as variables
  3051             //  the global scope, but it's parameters are pushed as variables
  3025             //  in the function's scope.
  3052             //  in the function's scope.
  3026 
       
  3027             datatype = type_check_ast(ctx, ast->funcunit.declaration);
  3053             datatype = type_check_ast(ctx, ast->funcunit.declaration);
  3028             push_function(ctx, ast->funcunit.declaration->identifier,
  3054             push_function(ctx, ast->funcunit.declaration->identifier,
  3029                           datatype, ast->funcunit.definition == NULL);
  3055                           datatype, ast->funcunit.definition == NULL);
  3030 
  3056 
  3031             // not just a declaration, but a full function definition?
  3057             // not just a declaration, but a full function definition?