utils/mojoshader-compiler.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 28 Feb 2009 04:30:56 -0500
changeset 706 01a92f30b84f
parent 688 29ee34e66161
child 747 0d1dbadcb387
permissions -rw-r--r--
Added some basic compiler stub stuff, just to get this building at all.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/**
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * MojoShader; generate shader programs from bytecode of compiled
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *  Direct3D shaders.
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 *
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 * Please see the file LICENSE.txt in the source's root directory.
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 *  This file written by Ryan C. Gordon.
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 */
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
#include <stdio.h>
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#include <stdlib.h>
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
    12
#include <string.h>
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
#include "mojoshader.h"
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
660
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    15
static const char **include_paths = NULL;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    16
static unsigned int include_path_count = 0;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    17
673
4b14154df11b Turn off allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 671
diff changeset
    18
#define MOJOSHADER_DEBUG_MALLOC 0
671
af7b7bfdfc2a Fixed allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 670
diff changeset
    19
665
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    20
#if MOJOSHADER_DEBUG_MALLOC
671
af7b7bfdfc2a Fixed allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 670
diff changeset
    21
static void *Malloc(int len, void *d)
665
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    22
{
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    23
    void *ptr = malloc(len + sizeof (int));
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    24
    int *store = (int *) ptr;
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    25
    printf("malloc() %d bytes (%p)\n", len, ptr);
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    26
    if (ptr == NULL) return NULL;
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    27
    *store = len;
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    28
    return (void *) (store + 1);
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    29
} // Malloc
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    30
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    31
671
af7b7bfdfc2a Fixed allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 670
diff changeset
    32
static void Free(void *_ptr, void *d)
665
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    33
{
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    34
    int *ptr = (((int *) _ptr) - 1);
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    35
    int len = *ptr;
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    36
    printf("free() %d bytes (%p)\n", len, ptr);
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    37
    free(ptr);
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    38
} // Free
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    39
#else
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    40
#define Malloc NULL
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    41
#define Free NULL
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    42
#endif
660
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    43
668
983f396a3d71 Added fail().
Ryan C. Gordon <icculus@icculus.org>
parents: 667
diff changeset
    44
983f396a3d71 Added fail().
Ryan C. Gordon <icculus@icculus.org>
parents: 667
diff changeset
    45
static void fail(const char *err)
983f396a3d71 Added fail().
Ryan C. Gordon <icculus@icculus.org>
parents: 667
diff changeset
    46
{
983f396a3d71 Added fail().
Ryan C. Gordon <icculus@icculus.org>
parents: 667
diff changeset
    47
    printf("%s.\n", err);
983f396a3d71 Added fail().
Ryan C. Gordon <icculus@icculus.org>
parents: 667
diff changeset
    48
    exit(1);
983f396a3d71 Added fail().
Ryan C. Gordon <icculus@icculus.org>
parents: 667
diff changeset
    49
} // fail
983f396a3d71 Added fail().
Ryan C. Gordon <icculus@icculus.org>
parents: 667
diff changeset
    50
983f396a3d71 Added fail().
Ryan C. Gordon <icculus@icculus.org>
parents: 667
diff changeset
    51
660
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    52
static int open_include(MOJOSHADER_includeType inctype, const char *fname,
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    53
                        const char *parent, const char **outdata,
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    54
                        unsigned int *outbytes, MOJOSHADER_malloc m,
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    55
                        MOJOSHADER_free f, void *d)
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    56
{
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    57
    int i;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    58
    for (i = 0; i < include_path_count; i++)
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    59
    {
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    60
        const char *path = include_paths[i];
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    61
        const size_t len = strlen(path) + strlen(fname) + 2;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    62
        char *buf = (char *) m(len, d);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    63
        if (buf == NULL)
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    64
            return 0;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    65
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    66
        snprintf(buf, len, "%s/%s", path, fname);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    67
        FILE *io = fopen(buf, "rb");
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    68
        f(buf, d);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    69
        if (io == NULL)
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    70
            continue;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    71
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    72
        if (fseek(io, 0, SEEK_END) != -1)
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    73
        {
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    74
            const long fsize = ftell(io);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    75
            if ((fsize == -1) || (fseek(io, 0, SEEK_SET) == -1))
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    76
            {
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    77
                fclose(io);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    78
                return 0;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    79
            } // if
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    80
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    81
            char *data = (char *) m(fsize, d);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    82
            if (data == NULL)
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    83
            {
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    84
                fclose(io);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    85
                return 0;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    86
            } // if
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    87
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    88
            if (fread(data, fsize, 1, io) != 1)
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    89
            {
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    90
                f(data, d);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    91
                fclose(io);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    92
                return 0;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    93
            } // if
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    94
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    95
            fclose(io);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    96
            *outdata = data;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    97
            *outbytes = (unsigned int) fsize;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    98
            return 1;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    99
        } // if
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   100
    } // for
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   101
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   102
    return 0;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   103
} // open_include
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   104
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   105
663
e7b3b60bd3b1 Made function static.
Ryan C. Gordon <icculus@icculus.org>
parents: 662
diff changeset
   106
static void close_include(const char *data, MOJOSHADER_malloc m,
e7b3b60bd3b1 Made function static.
Ryan C. Gordon <icculus@icculus.org>
parents: 662
diff changeset
   107
                          MOJOSHADER_free f, void *d)
660
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   108
{
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   109
    f((void *) data, d);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   110
} // close_include
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   111
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   112
578
6c8f73c845e7 Allow app to specify a base filename for error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 577
diff changeset
   113
static int preprocess(const char *fname, const char *buf, int len,
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   114
                      const char *outfile,
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   115
                      const MOJOSHADER_preprocessorDefine *defs,
669
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   116
                      unsigned int defcount, FILE *io)
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    const MOJOSHADER_preprocessData *pd;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
    int retval = 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
665
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
   121
    pd = MOJOSHADER_preprocess(fname, buf, len, defs, defcount, open_include,
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
   122
                               close_include, Malloc, Free, NULL);
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
    if (pd->error_count > 0)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
        int i;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
        for (i = 0; i < pd->error_count; i++)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
        {
577
bec531dd448e Report filenames in error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 556
diff changeset
   129
            printf("%s:%d: ERROR: %s\n",
bec531dd448e Report filenames in error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 556
diff changeset
   130
                    pd->errors[i].filename ? pd->errors[i].filename : "???",
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
                    pd->errors[i].error_position,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
                    pd->errors[i].error);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
        } // for
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
    } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
    else
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
        if (pd->output != NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
        {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
            if (fwrite(pd->output, pd->output_len, 1, io) != 1)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
                printf(" ... fwrite('%s') failed.\n", outfile);
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   141
            else if ((outfile != NULL) && (fclose(io) == EOF))
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
                printf(" ... fclose('%s') failed.\n", outfile);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
            else
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
                retval = 1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
        } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
    } // else
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
    MOJOSHADER_freePreprocessData(pd);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    return retval;
556
04282775cc2c Fixed cut-and-pasted comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 555
diff changeset
   150
} // preprocess
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
669
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   153
static int assemble(const char *fname, const char *buf, int len,
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   154
                    const char *outfile,
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   155
                    const MOJOSHADER_preprocessorDefine *defs,
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   156
                    unsigned int defcount, FILE *io)
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   157
{
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   158
    const MOJOSHADER_parseData *pd;
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   159
    int retval = 0;
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   160
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   161
    pd = MOJOSHADER_assemble(fname, buf, len, NULL, 0, NULL, 0,
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   162
                             defs, defcount, open_include, close_include,
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   163
                             Malloc, Free, NULL);
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   164
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   165
    if (pd->error_count > 0)
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   166
    {
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   167
        int i;
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   168
        for (i = 0; i < pd->error_count; i++)
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   169
        {
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   170
            printf("%s:%d: ERROR: %s\n",
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   171
                    pd->errors[i].filename ? pd->errors[i].filename : "???",
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   172
                    pd->errors[i].error_position,
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   173
                    pd->errors[i].error);
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   174
        } // for
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   175
    } // if
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   176
    else
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   177
    {
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   178
        if (pd->output != NULL)
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   179
        {
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   180
            if (fwrite(pd->output, pd->output_len, 1, io) != 1)
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   181
                printf(" ... fwrite('%s') failed.\n", outfile);
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   182
            else if ((outfile != NULL) && (fclose(io) == EOF))
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   183
                printf(" ... fclose('%s') failed.\n", outfile);
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   184
            else
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   185
                retval = 1;
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   186
        } // if
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   187
    } // else
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   188
    MOJOSHADER_freeParseData(pd);
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   189
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   190
    return retval;
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   191
} // assemble
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   192
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   193
void MOJOSHADER_compile(const char *filename,
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   194
                             const char *source, unsigned int sourcelen,
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   195
                             const MOJOSHADER_preprocessorDefine *defines,
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   196
                             unsigned int define_count,
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   197
                             MOJOSHADER_includeOpen include_open,
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   198
                             MOJOSHADER_includeClose include_close,
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   199
                             MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   200
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   201
static int compile(const char *fname, const char *buf, int len,
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   202
                    const char *outfile,
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   203
                    const MOJOSHADER_preprocessorDefine *defs,
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   204
                    unsigned int defcount, FILE *io)
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   205
{
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   206
    const MOJOSHADER_parseData *pd;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   207
    int retval = 0;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   208
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   209
    MOJOSHADER_compile(fname, buf, len, defs, defcount,
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   210
                             open_include, close_include,
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   211
                             Malloc, Free, NULL);
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   212
    return 1;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   213
} // compile
669
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   214
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   215
typedef enum
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   216
{
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   217
    ACTION_UNKNOWN,
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   218
    ACTION_PREPROCESS,
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   219
    ACTION_ASSEMBLE,
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   220
    ACTION_COMPILE,
669
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   221
} Action;
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   222
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   223
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
int main(int argc, char **argv)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
{
669
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   226
    Action action = ACTION_UNKNOWN;
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
    int retval = 1;
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   228
    const char *infile = NULL;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   229
    const char *outfile = NULL;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   230
    int i;
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   232
    MOJOSHADER_preprocessorDefine *defs = NULL;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   233
    unsigned int defcount = 0;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   234
686
021241f2a237 Current working directory should always be in the include path.
Ryan C. Gordon <icculus@icculus.org>
parents: 673
diff changeset
   235
    include_paths = (const char **) malloc(sizeof (char *));
021241f2a237 Current working directory should always be in the include path.
Ryan C. Gordon <icculus@icculus.org>
parents: 673
diff changeset
   236
    include_paths[0] = ".";
021241f2a237 Current working directory should always be in the include path.
Ryan C. Gordon <icculus@icculus.org>
parents: 673
diff changeset
   237
    include_path_count = 1;
021241f2a237 Current working directory should always be in the include path.
Ryan C. Gordon <icculus@icculus.org>
parents: 673
diff changeset
   238
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   239
    for (i = 1; i < argc; i++)
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
    {
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   241
        const char *arg = argv[i];
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   242
669
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   243
        if (strcmp(arg, "-P") == 0)
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   244
        {
688
29ee34e66161 No reason you can't specify the same option twice.
Ryan C. Gordon <icculus@icculus.org>
parents: 686
diff changeset
   245
            if ((action != ACTION_UNKNOWN) && (action != ACTION_PREPROCESS))
669
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   246
                fail("Multiple actions specified");
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   247
            action = ACTION_PREPROCESS;
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   248
        } // if
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   249
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   250
        else if (strcmp(arg, "-A") == 0)
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   251
        {
688
29ee34e66161 No reason you can't specify the same option twice.
Ryan C. Gordon <icculus@icculus.org>
parents: 686
diff changeset
   252
            if ((action != ACTION_UNKNOWN) && (action != ACTION_ASSEMBLE))
669
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   253
                fail("Multiple actions specified");
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   254
            action = ACTION_ASSEMBLE;
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   255
        } // else if
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   256
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   257
        else if (strcmp(arg, "-C") == 0)
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   258
        {
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   259
            if ((action != ACTION_UNKNOWN) && (action != ACTION_COMPILE))
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   260
                fail("Multiple actions specified");
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   261
            action = ACTION_COMPILE;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   262
        } // else if
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   263
669
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   264
        else if (strcmp(arg, "-o") == 0)
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   265
        {
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   266
            if (outfile != NULL)
668
983f396a3d71 Added fail().
Ryan C. Gordon <icculus@icculus.org>
parents: 667
diff changeset
   267
                fail("multiple output files specified");
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   268
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   269
            arg = argv[++i];
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   270
            if (arg == NULL)
668
983f396a3d71 Added fail().
Ryan C. Gordon <icculus@icculus.org>
parents: 667
diff changeset
   271
                fail("no filename after '-o'");
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   272
            outfile = arg;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   273
        } // if
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   274
667
8efc63f4ab9b That should be an "else if", not an "if" ...
Ryan C. Gordon <icculus@icculus.org>
parents: 666
diff changeset
   275
        else if (strcmp(arg, "-I") == 0)
660
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   276
        {
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   277
            arg = argv[++i];
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   278
            if (arg == NULL)
668
983f396a3d71 Added fail().
Ryan C. Gordon <icculus@icculus.org>
parents: 667
diff changeset
   279
                fail("no path after '-I'");
983f396a3d71 Added fail().
Ryan C. Gordon <icculus@icculus.org>
parents: 667
diff changeset
   280
660
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   281
            include_paths = (const char **) realloc(include_paths,
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   282
                       (include_path_count+1) * sizeof (char *));
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   283
            include_paths[include_path_count] = arg;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   284
            include_path_count++;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   285
        } // if
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   286
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   287
        else if (strncmp(arg, "-D", 2) == 0)
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   288
        {
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   289
            arg += 2;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   290
            char *ident = strdup(arg);
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   291
            char *ptr = strchr(ident, '=');
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   292
            const char *val = "";
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   293
            if (ptr)
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   294
            {
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   295
                *ptr = '\0';
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   296
                val = ptr+1;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   297
            } // if
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   298
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   299
            defs = (MOJOSHADER_preprocessorDefine *) realloc(defs,
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   300
                       (defcount+1) * sizeof (MOJOSHADER_preprocessorDefine));
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   301
            defs[defcount].identifier = ident;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   302
            defs[defcount].definition = val;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   303
            defcount++;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   304
        } // else if
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   305
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
        else
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
        {
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   308
            if (infile != NULL)
670
8df3588cf14a Fixed some strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 669
diff changeset
   309
                fail("multiple input files specified");
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   310
            infile = arg;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   311
        } // else
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   312
    } // for
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   313
669
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   314
    if (action == ACTION_UNKNOWN)
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   315
        action = ACTION_ASSEMBLE;
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   316
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   317
    if (infile == NULL)
670
8df3588cf14a Fixed some strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 669
diff changeset
   318
        fail("no input file specified");
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   319
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   320
    FILE *io = fopen(infile, "rb");
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   321
    if (io == NULL)
669
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   322
        fail("failed to open input file");
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   323
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   324
    fseek(io, 0, SEEK_END);
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   325
    long fsize = ftell(io);
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   326
    fseek(io, 0, SEEK_SET);
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   327
    if (fsize == -1)
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   328
        fsize = 1000000;
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   329
    char *buf = (char *) malloc(fsize);
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   330
    const int rc = fread(buf, 1, fsize, io);
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   331
    fclose(io);
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   332
    if (rc == EOF)
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   333
        fail("failed to read input file");
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   334
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   335
    FILE *outio = outfile ? fopen(outfile, "wb") : stdout;
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   336
    if (outio == NULL)
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   337
        fail("failed to open output file");
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   338
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   339
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   340
    if (action == ACTION_PREPROCESS)
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   341
        retval = (!preprocess(infile, buf, rc, outfile, defs, defcount, outio));
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   342
    else if (action == ACTION_ASSEMBLE)
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   343
        retval = (!assemble(infile, buf, rc, outfile, defs, defcount, outio));
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   344
    else if (action == ACTION_COMPILE)
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents: 688
diff changeset
   345
        retval = (!compile(infile, buf, rc, outfile, defs, defcount, outio));
669
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   346
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   347
    if ((retval != 0) && (outfile != NULL))
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   348
        remove(outfile);
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   349
fd14a7278a95 Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 668
diff changeset
   350
    free(buf);
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   352
    for (i = 0; i < defcount; i++)
661
a502c4999238 Fixed logic bug: used wrong variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 660
diff changeset
   353
        free((void *) defs[i].identifier);
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   354
    free(defs);
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   355
660
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   356
    free(include_paths);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   357
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
    return retval;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
} // main
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
666
aec40e1c5e99 Fixed comment.
Ryan C. Gordon <icculus@icculus.org>
parents: 665
diff changeset
   361
// end of mojoshader-compiler.c ...
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362