utils/assemble.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 16 Feb 2009 20:29:28 -0500
changeset 634 b7fd8aa02bf4
parent 588 ef3d2efa822d
permissions -rw-r--r--
Fix state when rewinding stream.
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
578
6c8f73c845e7 Allow app to specify a base filename for error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 577
diff changeset
    14
static int assemble(const char *fname, const char *buf, int len,
6c8f73c845e7 Allow app to specify a base filename for error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 577
diff changeset
    15
                    const char *outfile)
473
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
{
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
    FILE *io = fopen(outfile, "wb");
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
    if (io == NULL)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
    {
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
        printf(" ... fopen('%s') failed.\n", outfile);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
        return 0;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
    } // if
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
    const MOJOSHADER_parseData *pd;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
    int retval = 0;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
578
6c8f73c845e7 Allow app to specify a base filename for error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 577
diff changeset
    27
    pd = MOJOSHADER_assemble(fname, buf, len, NULL, 0, NULL, 0,
6c8f73c845e7 Allow app to specify a base filename for error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 577
diff changeset
    28
                             NULL, 0, NULL, NULL, NULL, NULL, NULL);
536
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    29
    if (pd->error_count > 0)
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    30
    {
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    31
        int i;
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    32
        for (i = 0; i < pd->error_count; i++)
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    33
        {
577
bec531dd448e Report filenames in error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 562
diff changeset
    34
            printf("%s:%d: ERROR: %s\n",
bec531dd448e Report filenames in error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 562
diff changeset
    35
                    pd->errors[i].filename ? pd->errors[i].filename : "???",
536
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    36
                    pd->errors[i].error_position,
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    37
                    pd->errors[i].error);
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    38
        } // for
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 524
diff changeset
    39
    } // if
473
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
    {
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
        if (pd->output != NULL)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
        {
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
            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
    45
                printf(" ... fwrite('%s') failed.\n", outfile);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
            else if (fclose(io) == EOF)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
                printf(" ... fclose('%s') failed.\n", outfile);
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
                retval = 1;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
        } // if
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    } // else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    MOJOSHADER_freeParseData(pd);
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
    return retval;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
} // assemble
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
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
int main(int argc, char **argv)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
{
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
    int retval = 1;
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
    if (argc != 3)
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 536
diff changeset
    63
        printf("\n\nUSAGE: %s <inputfile> <outputfile>\n\n", argv[0]);
473
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    {
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
        const char *infile = argv[1];
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
        const char *outfile = argv[2];
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
        FILE *io = fopen(infile, "rb");
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
        if (io == NULL)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
            printf(" ... fopen('%s') failed.\n", infile);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
        else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
        {
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
            char *buf = (char *) malloc(1000000);
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 536
diff changeset
    74
            int rc = fread(buf, 1, 1000000, io);
473
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
            fclose(io);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
            if (rc == EOF)
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
                printf(" ... fread('%s') failed.\n", infile);
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
            else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
            {
578
6c8f73c845e7 Allow app to specify a base filename for error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 577
diff changeset
    80
                if (assemble(infile, buf, rc, outfile))
473
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
                    retval = 0;
474
c2d4e63efdd1 Delete output file if assembly fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 473
diff changeset
    82
                else
c2d4e63efdd1 Delete output file if assembly fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 473
diff changeset
    83
                    remove(outfile);
473
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
                free(buf);
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
        } // for
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    } // else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
    return retval;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
} // main
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
// end of assemble.c ...
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93