assemble.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 10 Dec 2008 01:54:08 -0500
changeset 473 78c418bde2d2
child 474 c2d4e63efdd1
permissions -rw-r--r--
Added assemble.c test program.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
473
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/**
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * MojoShader; generate shader programs from bytecode of compiled
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *  Direct3D shaders.
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 *
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 * Please see the file LICENSE.txt in the source's root directory.
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 *  This file written by Ryan C. Gordon.
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 */
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
#include <stdio.h>
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#include <stdlib.h>
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
#include "mojoshader.h"
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
static int assemble(const char *buf, const char *outfile)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
{
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
    FILE *io = fopen(outfile, "wb");
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
    if (io == NULL)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
    {
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
        printf(" ... fopen('%s') failed.\n", outfile);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
        return 0;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
    } // if
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
    const MOJOSHADER_parseData *pd;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
    int retval = 0;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
    pd = MOJOSHADER_assemble(buf, NULL, NULL, NULL);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
    if (pd->error != NULL)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
        printf("ERROR: %s\n", pd->error);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
    else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
    {
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
        if (pd->output != NULL)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
        {
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
            if (fwrite(pd->output, pd->output_len, 1, io) != 1)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
                printf(" ... fwrite('%s') failed.\n", outfile);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
            else if (fclose(io) == EOF)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
                printf(" ... fclose('%s') failed.\n", outfile);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
            else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
                retval = 1;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
        } // if
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    } // else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    MOJOSHADER_freeParseData(pd);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    return retval;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
} // assemble
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
int main(int argc, char **argv)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
{
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    int retval = 1;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    if (argc != 3)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
        printf("\n\nUSAGE: %s <d3dasmfile> <outputfile>\n\n", argv[0]);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    {
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
        const char *infile = argv[1];
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
        const char *outfile = argv[2];
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
        FILE *io = fopen(infile, "rb");
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
        if (io == NULL)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
            printf(" ... fopen('%s') failed.\n", infile);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
        else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
        {
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
            char *buf = (char *) malloc(1000000);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
            int rc = fread(buf, 1, 1000000-1, io);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
            fclose(io);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
            if (rc == EOF)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
                printf(" ... fread('%s') failed.\n", infile);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
            else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
            {
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
                buf[rc] = '\0';
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
                if (assemble(buf, outfile))
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
                    retval = 0;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
                free(buf);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
            } // else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
        } // for
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    } // else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    return retval;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
} // main
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
// end of assemble.c ...
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81