assemble.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 10 Dec 2008 04:14:50 -0500
changeset 486 45efac751027
parent 474 c2d4e63efdd1
child 524 03eea2f0762c
permissions -rw-r--r--
Added error_position to assembly results.
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)
486
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 474
diff changeset
    28
        printf("ERROR: (line %d) %s\n", pd->error_position, pd->error);
473
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;
474
c2d4e63efdd1 Delete output file if assembly fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 473
diff changeset
    72
                else
c2d4e63efdd1 Delete output file if assembly fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 473
diff changeset
    73
                    remove(outfile);
473
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
                free(buf);
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
        } // for
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    } // else
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    return retval;
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
} // main
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
// end of assemble.c ...
78c418bde2d2 Added assemble.c test program.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83