mojoshader.c
branchtrunk
changeset 150 eafaa587dbde
parent 149 07c73dfc1475
child 151 1667680fe402
--- a/mojoshader.c	Sat Apr 19 04:59:27 2008 -0400
+++ b/mojoshader.c	Sat Apr 19 13:32:05 2008 -0400
@@ -3727,7 +3727,6 @@
 {
     const char *opcode_string;
     MOJOSHADER_shaderType shader_types;  // mask of types that can use opcode.
-    int arg_tokens;
     args_function parse_args;
     state_function state;
     emit_function emitter[STATICARRAYLEN(profiles)];
@@ -3740,113 +3739,113 @@
     // INSTRUCTION_STATE means this opcode has to update the state machine
     //  (we're entering an ELSE block, etc). INSTRUCTION means there's no
     //  state, just go straight to the emitters.
-    #define INSTRUCTION_STATE(op, args, argsseq, t) { \
-        #op, t, args, parse_args_##argsseq, state_##op, PROFILE_EMITTERS(op) \
+    #define INSTRUCTION_STATE(op, argsseq, t) { \
+        #op, t, parse_args_##argsseq, state_##op, PROFILE_EMITTERS(op) \
     }
-    #define INSTRUCTION(op, args, argsseq, t) { \
-        #op, t, args, parse_args_##argsseq, 0, PROFILE_EMITTERS(op) \
+    #define INSTRUCTION(op, argsseq, t) { \
+        #op, t, parse_args_##argsseq, 0, PROFILE_EMITTERS(op) \
     }
 
     // !!! FIXME: Some of these MOJOSHADER_TYPE_ANYs need to have their scope
     // !!! FIXME:  reduced to just PIXEL or VERTEX.
 
-    INSTRUCTION(NOP, 0, NULL, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(MOV, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(ADD, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(SUB, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(MAD, 4, DSSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(MUL, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(RCP, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(RSQ, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(DP3, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(DP4, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(MIN, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(MAX, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(SLT, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(SGE, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(EXP, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(LOG, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(LIT, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(DST, 3, DSS, MOJOSHADER_TYPE_VERTEX),
-    INSTRUCTION(LRP, 4, DSSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(FRC, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(M4X4, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(M4X3, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(M3X4, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(M3X3, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(M3X2, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(CALL, 1, S, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(CALLNZ, 2, SS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(LOOP, 2, SS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(RET, 0, NULL, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(ENDLOOP, 0, NULL, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(LABEL, 1, S, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(DCL, 2, DCL, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(POW, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(CRS, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(SGN, 4, DSSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(ABS, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(NRM, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(SINCOS, -1, SINCOS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(REP, 1, S, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(ENDREP, 0, NULL, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(IF, 1, S, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(IFC, 2, SS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(ELSE, 0, NULL, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(ENDIF, 0, NULL, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(BREAK, 0, NULL, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(BREAKC, 2, SS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(MOVA, 2, DS, MOJOSHADER_TYPE_VERTEX),
-    INSTRUCTION_STATE(DEFB, 2, DEFB, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(DEFI, 5, DEF, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(RESERVED, 0, NULL, MOJOSHADER_TYPE_UNKNOWN),
-    INSTRUCTION(RESERVED, 0, NULL, MOJOSHADER_TYPE_UNKNOWN),
-    INSTRUCTION(RESERVED, 0, NULL, MOJOSHADER_TYPE_UNKNOWN),
-    INSTRUCTION(RESERVED, 0, NULL, MOJOSHADER_TYPE_UNKNOWN),
-    INSTRUCTION(RESERVED, 0, NULL, MOJOSHADER_TYPE_UNKNOWN),
-    INSTRUCTION(RESERVED, 0, NULL, MOJOSHADER_TYPE_UNKNOWN),
-    INSTRUCTION(RESERVED, 0, NULL, MOJOSHADER_TYPE_UNKNOWN),
-    INSTRUCTION(RESERVED, 0, NULL, MOJOSHADER_TYPE_UNKNOWN),
-    INSTRUCTION(RESERVED, 0, NULL, MOJOSHADER_TYPE_UNKNOWN),
-    INSTRUCTION(RESERVED, 0, NULL, MOJOSHADER_TYPE_UNKNOWN),
-    INSTRUCTION(RESERVED, 0, NULL, MOJOSHADER_TYPE_UNKNOWN),
-    INSTRUCTION(RESERVED, 0, NULL, MOJOSHADER_TYPE_UNKNOWN),
-    INSTRUCTION(RESERVED, 0, NULL, MOJOSHADER_TYPE_UNKNOWN),
-    INSTRUCTION(RESERVED, 0, NULL, MOJOSHADER_TYPE_UNKNOWN),
-    INSTRUCTION(RESERVED, 0, NULL, MOJOSHADER_TYPE_UNKNOWN),
-    INSTRUCTION(TEXCOORD, -1, TEXCOORD, MOJOSHADER_TYPE_PIXEL),
-    INSTRUCTION(TEXKILL, 1, D, MOJOSHADER_TYPE_PIXEL),
-    INSTRUCTION(TEX, -1, TEX, MOJOSHADER_TYPE_PIXEL),
-    INSTRUCTION(TEXBEM, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(TEXBEML, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(TEXREG2AR, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(TEXREG2GB, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(TEXM3X2PAD, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(TEXM3X2TEX, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(TEXM3X3PAD, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(TEXM3X3TEX, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(RESERVED, 0, NULL, MOJOSHADER_TYPE_UNKNOWN),
-    INSTRUCTION(TEXM3X3SPEC, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(TEXM3X3VSPEC, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(EXPP, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(LOGP, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(CND, 4, DSSS, MOJOSHADER_TYPE_PIXEL),
-    INSTRUCTION_STATE(DEF, 5, DEF, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(TEXREG2RGB, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(TEXDP3TEX, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(TEXM3X2DEPTH, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(TEXDP3, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(TEXM3X3, 2, DS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(TEXDEPTH, 1, D, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(CMP, 4, DSSS, MOJOSHADER_TYPE_PIXEL),
-    INSTRUCTION(BEM, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(DP2ADD, 4, DSSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(DSX, 2, DS, MOJOSHADER_TYPE_PIXEL),
-    INSTRUCTION(DSY, 2, DS, MOJOSHADER_TYPE_PIXEL),
-    INSTRUCTION(TEXLDD, 5, DSSSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(SETP, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION(TEXLDL, 3, DSS, MOJOSHADER_TYPE_ANY),
-    INSTRUCTION_STATE(BREAKP, 1, S, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(NOP, NULL, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(MOV, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(ADD, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(SUB, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(MAD, DSSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(MUL, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(RCP, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(RSQ, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(DP3, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(DP4, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(MIN, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(MAX, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(SLT, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(SGE, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(EXP, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(LOG, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(LIT, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(DST, DSS, MOJOSHADER_TYPE_VERTEX),
+    INSTRUCTION(LRP, DSSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(FRC, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(M4X4, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(M4X3, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(M3X4, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(M3X3, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(M3X2, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(CALL, S, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(CALLNZ, SS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(LOOP, SS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(RET, NULL, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(ENDLOOP, NULL, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(LABEL, S, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(DCL, DCL, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(POW, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(CRS, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(SGN, DSSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(ABS, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(NRM, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(SINCOS, SINCOS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(REP, S, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(ENDREP, NULL, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(IF, S, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(IFC, SS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(ELSE, NULL, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(ENDIF, NULL, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(BREAK, NULL, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(BREAKC, SS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(MOVA, DS, MOJOSHADER_TYPE_VERTEX),
+    INSTRUCTION_STATE(DEFB, DEFB, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(DEFI, DEF, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(RESERVED, NULL, MOJOSHADER_TYPE_UNKNOWN),
+    INSTRUCTION(RESERVED, NULL, MOJOSHADER_TYPE_UNKNOWN),
+    INSTRUCTION(RESERVED, NULL, MOJOSHADER_TYPE_UNKNOWN),
+    INSTRUCTION(RESERVED, NULL, MOJOSHADER_TYPE_UNKNOWN),
+    INSTRUCTION(RESERVED, NULL, MOJOSHADER_TYPE_UNKNOWN),
+    INSTRUCTION(RESERVED, NULL, MOJOSHADER_TYPE_UNKNOWN),
+    INSTRUCTION(RESERVED, NULL, MOJOSHADER_TYPE_UNKNOWN),
+    INSTRUCTION(RESERVED, NULL, MOJOSHADER_TYPE_UNKNOWN),
+    INSTRUCTION(RESERVED, NULL, MOJOSHADER_TYPE_UNKNOWN),
+    INSTRUCTION(RESERVED, NULL, MOJOSHADER_TYPE_UNKNOWN),
+    INSTRUCTION(RESERVED, NULL, MOJOSHADER_TYPE_UNKNOWN),
+    INSTRUCTION(RESERVED, NULL, MOJOSHADER_TYPE_UNKNOWN),
+    INSTRUCTION(RESERVED, NULL, MOJOSHADER_TYPE_UNKNOWN),
+    INSTRUCTION(RESERVED, NULL, MOJOSHADER_TYPE_UNKNOWN),
+    INSTRUCTION(RESERVED, NULL, MOJOSHADER_TYPE_UNKNOWN),
+    INSTRUCTION(TEXCOORD, TEXCOORD, MOJOSHADER_TYPE_PIXEL),
+    INSTRUCTION(TEXKILL, D, MOJOSHADER_TYPE_PIXEL),
+    INSTRUCTION(TEX, TEX, MOJOSHADER_TYPE_PIXEL),
+    INSTRUCTION(TEXBEM, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(TEXBEML, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(TEXREG2AR, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(TEXREG2GB, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(TEXM3X2PAD, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(TEXM3X2TEX, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(TEXM3X3PAD, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(TEXM3X3TEX, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(RESERVED, NULL, MOJOSHADER_TYPE_UNKNOWN),
+    INSTRUCTION(TEXM3X3SPEC, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(TEXM3X3VSPEC, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(EXPP, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(LOGP, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(CND, DSSS, MOJOSHADER_TYPE_PIXEL),
+    INSTRUCTION_STATE(DEF, DEF, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(TEXREG2RGB, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(TEXDP3TEX, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(TEXM3X2DEPTH, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(TEXDP3, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(TEXM3X3, DS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(TEXDEPTH, D, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(CMP, DSSS, MOJOSHADER_TYPE_PIXEL),
+    INSTRUCTION(BEM, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(DP2ADD, DSSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(DSX, DS, MOJOSHADER_TYPE_PIXEL),
+    INSTRUCTION(DSY, DS, MOJOSHADER_TYPE_PIXEL),
+    INSTRUCTION(TEXLDD, DSSSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(SETP, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION(TEXLDL, DSS, MOJOSHADER_TYPE_ANY),
+    INSTRUCTION_STATE(BREAKP, S, MOJOSHADER_TYPE_ANY),
 
     #undef INSTRUCTION
     #undef INSTRUCTION_STATE
@@ -3880,28 +3879,6 @@
     if (coissue)  // !!! FIXME: I'm not sure what this means, yet.
         return fail(ctx, "coissue instructions unsupported");
 
-    if (ctx->major_ver < 2)
-    {
-        if (insttoks != 0)  // this is a reserved field in shaders < 2.0 ...
-            return fail(ctx, "instruction token count must be zero");
-    } // if
-
-    else if (instruction->arg_tokens >= 0)
-    {
-        if (instruction->arg_tokens != insttoks)
-        {
-            return failf(ctx, "unexpected tokens count (%u) for opcode '%s'.",
-                        (uint) insttoks, instruction->opcode_string);
-        } // if
-        else if (ctx->tokencount <= instruction->arg_tokens)
-        {
-            return failf(ctx,
-                        "need more tokens (need %u, got %u) for opcode '%s'.",
-                        (uint) instruction->arg_tokens, (uint) ctx->tokencount,
-                        instruction->opcode_string);
-        } // else if
-    } // else if
-
     if ((ctx->shader_type & instruction->shader_types) == 0)
     {
         return failf(ctx, "opcode '%s' not available in this shader type.",
@@ -3938,6 +3915,25 @@
 
     ctx->previous_opcode = opcode;
 
+    if (!isfail(ctx))
+    {
+        if (!shader_version_atleast(ctx, 2, 0))
+        {
+            if (insttoks != 0)  // reserved field in shaders < 2.0 ...
+                return fail(ctx, "instruction token count must be zero");
+        } // if
+        else
+        {
+            if (retval != (insttoks+1))
+            {
+                return failf(ctx,
+                        "wrong token count (%u, not %u) for opcode '%s'.",
+                        (uint) insttoks, (uint) retval,
+                        instruction->opcode_string);
+            } // if
+        } // else
+    } // if
+
     return retval;
 } // parse_instruction_token