assemble.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 03 Feb 2009 17:16:56 -0500
changeset 549 13714d3b3fa2
parent 536 5af65fe6e917
child 562 c9a2bc5129c9
permissions -rw-r--r--
Handle out of memory error better.
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
524
03eea2f0762c First (incomplete!) shot at more robust CTAB support.
Ryan C. Gordon <icculus@icculus.org>
parents: 486
diff changeset
    26
    pd = MOJOSHADER_assemble(buf, NULL, 0, NULL, 0, NULL, NULL, NULL);
536
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    27
    if (pd->error_count > 0)
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    28
    {
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    29
        int i;
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    30
        for (i = 0; i < pd->error_count; i++)
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    31
        {
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    32
            printf("ERROR: (line %d) %s\n",
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    33
                    pd->errors[i].error_position,
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    34
                    pd->errors[i].error);
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    35
        } // for
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    36
    } // if
473
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
    {
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
        if (pd->output != NULL)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
        {
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
            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
    42
                printf(" ... fwrite('%s') failed.\n", outfile);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
            else if (fclose(io) == EOF)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
                printf(" ... fclose('%s') failed.\n", outfile);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
            else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
                retval = 1;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
        } // if
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
    } // else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    MOJOSHADER_freeParseData(pd);
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
    return retval;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
} // assemble
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
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
int main(int argc, char **argv)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
{
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    int retval = 1;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    if (argc != 3)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
        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
    61
    else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    {
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
        const char *infile = argv[1];
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
        const char *outfile = argv[2];
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
        FILE *io = fopen(infile, "rb");
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
        if (io == NULL)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
            printf(" ... fopen('%s') failed.\n", infile);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
        else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
        {
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
            char *buf = (char *) malloc(1000000);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
            int rc = fread(buf, 1, 1000000-1, io);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
            fclose(io);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
            if (rc == EOF)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
                printf(" ... fread('%s') failed.\n", infile);
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
                buf[rc] = '\0';
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
                if (assemble(buf, outfile))
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
                    retval = 0;
474
c2d4e63efdd1 Delete output file if assembly fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 473
diff changeset
    80
                else
c2d4e63efdd1 Delete output file if assembly fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 473
diff changeset
    81
                    remove(outfile);
473
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
                free(buf);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
            } // else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
        } // for
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
    } // else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    return retval;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
} // main
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
// end of assemble.c ...
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91