parse.c
author icculus
Sat, 09 Feb 2008 01:15:39 -0500
branchtrunk
changeset 1 99ac16070d07
child 2 c37210f5e87b
permissions -rw-r--r--
[svn] Initial add.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
     1
#include <stdio.h>
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
     2
#include <stdlib.h>
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
     3
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
     4
#define SWAP32(x) (x)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
     5
typedef unsigned int uint;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
     6
typedef unsigned char uint8;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
     7
typedef unsigned int uint32;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
     8
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
     9
// This enum complements of Filip Navara's public domain dev-cpp d3d9 header.
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    10
typedef enum
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    11
{
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    12
    OPCODE_NOP = 0,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    13
    OPCODE_MOV = 1,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    14
    OPCODE_ADD = 2,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    15
    OPCODE_SUB = 3,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    16
    OPCODE_MAD = 4,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    17
    OPCODE_MUL = 5,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    18
    OPCODE_RCP = 6,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    19
    OPCODE_RSQ = 7,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    20
    OPCODE_DP3 = 8,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    21
    OPCODE_DP4 = 9,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    22
    OPCODE_MIN = 10,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    23
    OPCODE_MAX = 11,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    24
    OPCODE_SLT = 12,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    25
    OPCODE_SGE = 13,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    26
    OPCODE_EXP = 14,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    27
    OPCODE_LOG = 15,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    28
    OPCODE_LIT = 16,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    29
    OPCODE_DST = 17,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    30
    OPCODE_LRP = 18,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    31
    OPCODE_FRC = 19,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    32
    OPCODE_M4x4 = 20,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    33
    OPCODE_M4x3 = 21,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    34
    OPCODE_M3x4 = 22,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    35
    OPCODE_M3x3 = 23,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    36
    OPCODE_M3x2 = 24,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    37
    OPCODE_CALL = 25,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    38
    OPCODE_CALLNZ = 26,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    39
    OPCODE_LOOP = 27,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    40
    OPCODE_RET = 28,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    41
    OPCODE_ENDLOOP = 29,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    42
    OPCODE_LABEL = 30,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    43
    OPCODE_DCL = 31,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    44
    OPCODE_POW = 32,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    45
    OPCODE_CRS = 33,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    46
    OPCODE_SGN = 34,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    47
    OPCODE_ABS = 35,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    48
    OPCODE_NRM = 36,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    49
    OPCODE_SINCOS = 37,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    50
    OPCODE_REP = 38,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    51
    OPCODE_ENDREP = 39,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    52
    OPCODE_IF = 40,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    53
    OPCODE_IFC = 41,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    54
    OPCODE_ELSE = 42,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    55
    OPCODE_ENDIF = 43,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    56
    OPCODE_BREAK = 44,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    57
    OPCODE_BREAKC = 45,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    58
    OPCODE_MOVA = 46,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    59
    OPCODE_DEFB = 47,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    60
    OPCODE_DEFI = 48,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    61
    OPCODE_TEXCOORD = 64,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    62
    OPCODE_TEXKILL = 65,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    63
    OPCODE_TEX = 66,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    64
    OPCODE_TEXBEM = 67,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    65
    OPCODE_TEXBEML = 68,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    66
    OPCODE_TEXREG2AR = 69,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    67
    OPCODE_TEXREG2GB = 70,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    68
    OPCODE_TEXM3x2PAD = 71,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    69
    OPCODE_TEXM3x2TEX = 72,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    70
    OPCODE_TEXM3x3PAD = 73,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    71
    OPCODE_TEXM3x3TEX = 74,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    72
    OPCODE_RESERVED0 = 75,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    73
    OPCODE_TEXM3x3SPEC = 76,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    74
    OPCODE_TEXM3x3VSPEC = 77,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    75
    OPCODE_EXPP = 78,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    76
    OPCODE_LOGP = 79,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    77
    OPCODE_CND = 80,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    78
    OPCODE_DEF = 81,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    79
    OPCODE_TEXREG2RGB = 82,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    80
    OPCODE_TEXDP3TEX = 83,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    81
    OPCODE_TEXM3x2DEPTH = 84,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    82
    OPCODE_TEXDP3 = 85,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    83
    OPCODE_TEXM3x3 = 86,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    84
    OPCODE_TEXDEPTH = 87,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    85
    OPCODE_CMP = 88,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    86
    OPCODE_BEM = 89,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    87
    OPCODE_DP2ADD = 90,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    88
    OPCODE_DSX = 91,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    89
    OPCODE_DSY = 92,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    90
    OPCODE_TEXLDD = 93,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    91
    OPCODE_SETP = 94,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    92
    OPCODE_TEXLDL = 95,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    93
    OPCODE_BREAKP = 96,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    94
    OPCODE_PHASE = 0xfffd,
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    95
} OpcodeVal;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    96
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    97
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    98
static int parse_version_token(const uint32 *tokens, const uint32 tokencount)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    99
{
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   100
    if (tokencount == 0)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   101
        return -1;  // no tokens at all?
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   102
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   103
    const uint32 token = SWAP32(*tokens);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   104
    const uint32 shadertype = ((token >> 16) & 0xFFFF);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   105
    const uint32 major = ((token >> 8) & 0xFF);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   106
    const uint32 minor = (token & 0xFF);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   107
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   108
    if (shadertype == 0xFFFF)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   109
        printf("Pixel shader\n");
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   110
    else if (shadertype == 0xFFFE)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   111
        printf("Vertex shader\n");
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   112
    else
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   113
        return -1;  // geometry shader? Unsupported at the moment. FAIL.
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   114
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   115
    printf("Version %u.%u\n", (uint) major, (uint) minor);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   116
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   117
    return 1;  // ate one token.
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   118
} // parse_version_token
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   119
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   120
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   121
static int parse_comment_token(const uint32 *tokens, const uint32 tokencount)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   122
{
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   123
    const uint32 token = SWAP32(*tokens);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   124
    if ((token & 0xFFFF) != 0xFFFE)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   125
        return 0;  // not a comment token.
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   126
    else if ((token & 0x80000000) != 0)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   127
        return -1;  // msdn docs say high bit must be zero. FAIL.
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   128
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   129
    const uint32 commenttoks = ((token >> 16) & 0xFFFF);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   130
    const uint32 commentlen = commenttoks * sizeof (uint32);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   131
    printf("Comment (%u tokens, %u bytes): ",
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   132
            (uint) commenttoks, (uint) commentlen);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   133
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   134
    uint32 i = 0;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   135
    const char *comment = (const char *) (tokens+1);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   136
    while (i < commentlen)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   137
        fputc(comment[i++], stdout);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   138
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   139
    printf("\n");
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   140
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   141
    return commenttoks + 1;  // comment data plus the initial token.
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   142
} // parse_comment_token
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   143
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   144
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   145
static int parse_end_token(const uint32 *tokens, const uint32 tokencount)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   146
{
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   147
    if (SWAP32(*tokens) != 0x0000FFFF)      // end token is always 0x0000FFFF.
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   148
        return 0;  // not us, eat no tokens.
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   149
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   150
    printf("END\n");
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   151
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   152
    // we _must_ be last. If so, eat the token. Otherwise: FAIL.
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   153
    return (tokencount == 1) ? 1 : -1;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   154
} // parse_end_token
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   155
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   156
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   157
static int parse_instruction_token(const uint32 *tokens, const uint32 tokencount)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   158
{
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   159
    const uint32 token = SWAP32(*tokens);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   160
    const uint32 opcode = (token & 0xFFFF);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   161
    const uint32 controls = ((token >> 16) & 0xFF);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   162
    const uint32 insttoks = ((token >> 24) & 0x0F);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   163
    const int coissue = (token & 0x40000000) ? 1 : 0;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   164
    const int predicated = (token & 0x10000000) ? 1 : 0;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   165
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   166
    if ((token & 0x80000000) != 0)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   167
        return -1;  // msdn docs say high bit must be zero. FAIL.
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   168
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   169
    #define PARSE_OP(op) printf("OPCODE %s\n", #op);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   170
    //case OPCODE_##op:
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   171
    //    parse_op_##op(tokens+1, opcode, controls, insttoks, coissue, predicated);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   172
    //    break;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   173
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   174
    PARSE_OP(NOP);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   175
    PARSE_OP(MOV);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   176
    PARSE_OP(ADD);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   177
    PARSE_OP(SUB);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   178
    PARSE_OP(MAD);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   179
    PARSE_OP(MUL);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   180
    PARSE_OP(RCP);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   181
    PARSE_OP(RSQ);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   182
    PARSE_OP(DP3);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   183
    PARSE_OP(DP4);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   184
    PARSE_OP(MIN);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   185
    PARSE_OP(MAX);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   186
    PARSE_OP(SLT);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   187
    PARSE_OP(SGE);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   188
    PARSE_OP(EXP);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   189
    PARSE_OP(LOG);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   190
    PARSE_OP(LIT);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   191
    PARSE_OP(DST);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   192
    PARSE_OP(LRP);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   193
    PARSE_OP(FRC);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   194
    PARSE_OP(M4x4);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   195
    PARSE_OP(M4x3);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   196
    PARSE_OP(M3x4);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   197
    PARSE_OP(M3x3);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   198
    PARSE_OP(M3x2);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   199
    PARSE_OP(CALL);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   200
    PARSE_OP(CALLNZ);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   201
    PARSE_OP(LOOP);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   202
    PARSE_OP(RET);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   203
    PARSE_OP(ENDLOOP);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   204
    PARSE_OP(LABEL);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   205
    PARSE_OP(DCL);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   206
    PARSE_OP(POW);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   207
    PARSE_OP(CRS);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   208
    PARSE_OP(SGN);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   209
    PARSE_OP(ABS);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   210
    PARSE_OP(NRM);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   211
    PARSE_OP(SINCOS);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   212
    PARSE_OP(REP);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   213
    PARSE_OP(ENDREP);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   214
    PARSE_OP(IF);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   215
    PARSE_OP(IFC);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   216
    PARSE_OP(ELSE);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   217
    PARSE_OP(ENDIF);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   218
    PARSE_OP(BREAK);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   219
    PARSE_OP(BREAKC);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   220
    PARSE_OP(MOVA);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   221
    PARSE_OP(DEFB);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   222
    PARSE_OP(DEFI);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   223
    PARSE_OP(TEXCOORD);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   224
    PARSE_OP(TEXKILL);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   225
    PARSE_OP(TEX);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   226
    PARSE_OP(TEXBEM);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   227
    PARSE_OP(TEXBEML);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   228
    PARSE_OP(TEXREG2AR);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   229
    PARSE_OP(TEXREG2GB);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   230
    PARSE_OP(TEXM3x2PAD);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   231
    PARSE_OP(TEXM3x2TEX);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   232
    PARSE_OP(TEXM3x3PAD);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   233
    PARSE_OP(TEXM3x3TEX);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   234
    PARSE_OP(RESERVED0);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   235
    PARSE_OP(TEXM3x3SPEC);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   236
    PARSE_OP(TEXM3x3VSPEC);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   237
    PARSE_OP(EXPP);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   238
    PARSE_OP(LOGP);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   239
    PARSE_OP(CND);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   240
    PARSE_OP(DEF);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   241
    PARSE_OP(TEXREG2RGB);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   242
    PARSE_OP(TEXDP3TEX);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   243
    PARSE_OP(TEXM3x2DEPTH);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   244
    PARSE_OP(TEXDP3);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   245
    PARSE_OP(TEXM3x3);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   246
    PARSE_OP(TEXDEPTH);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   247
    PARSE_OP(CMP);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   248
    PARSE_OP(BEM);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   249
    PARSE_OP(DP2ADD);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   250
    PARSE_OP(DSX);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   251
    PARSE_OP(DSY);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   252
    PARSE_OP(TEXLDD);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   253
    PARSE_OP(SETP);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   254
    PARSE_OP(TEXLDL);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   255
    PARSE_OP(BREAKP);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   256
    PARSE_OP(PHASE);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   257
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   258
    #undef PARSE_OP
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   259
} // parse_instruction_token
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   260
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   261
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   262
static int parse_token(const uint32 *tokens, const uint32 tokencount)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   263
{
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   264
    int retval = -1;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   265
    int rc = 0;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   266
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   267
    if (tokencount == 0)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   268
        return -1;  // shouldn't happen, but just in case...
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   269
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   270
    if ((rc = parse_comment_token(tokens, tokencount)) != 0)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   271
        return rc;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   272
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   273
    if ((rc = parse_end_token(tokens, tokencount)) != 0)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   274
        return rc;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   275
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   276
    if ((rc = parse_instruction_token(tokens, tokencount)) != 0)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   277
        return rc;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   278
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   279
    return -1;  // nothing handled this? FAIL.
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   280
} // parse_token
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   281
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   282
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   283
int D3D2GLSL_parse(const uint8 *tokenbuf, const uint32 bufsize)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   284
{
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   285
    const uint32 *tokens = (const uint32 *) tokenbuf;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   286
    uint32 tokencount = bufsize / sizeof (uint32);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   287
    int rc = parse_version_token(tokens, tokencount);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   288
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   289
    // parse out the rest of the tokens after the version token...
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   290
    while ((rc > 0) && (tokencount > 0))
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   291
    {
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   292
        tokens += rc;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   293
        tokencount -= rc;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   294
        rc = parse_token(tokens, tokencount);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   295
    } // while
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   296
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   297
    return ((rc <= 0) || (tokencount > 0)) ? 0 : 1;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   298
} // D3D2GLSL_parse
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   299
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   300
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   301
int main(int argc, char **argv)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   302
{
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   303
    if (argv[1] != NULL)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   304
    {
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   305
        FILE *io = fopen(argv[1], "rb");
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   306
        if (io != NULL)
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   307
        {
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   308
            uint8 *buf = (uint8 *) malloc(1000000);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   309
            int rc = fread(buf, 1, 1000000, io);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   310
            fclose(io);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   311
            D3D2GLSL_parse(buf, rc);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   312
            free(buf);
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   313
        } // if
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   314
    } // if
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   315
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   316
    return 0;
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   317
} // main
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   318
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   319
// end of parse.c ...
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
   320