author | Ryan C. Gordon <icculus@icculus.org> |
Thu, 19 Feb 2009 03:38:10 -0500 | |
changeset 669 | fd14a7278a95 |
parent 668 | 983f396a3d71 |
child 670 | 8df3588cf14a |
permissions | -rw-r--r-- |
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 |
|
665
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
18 |
#if MOJOSHADER_DEBUG_MALLOC |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
19 |
static void *Malloc(int len) |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
20 |
{ |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
21 |
void *ptr = malloc(len + sizeof (int)); |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
22 |
int *store = (int *) ptr; |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
23 |
printf("malloc() %d bytes (%p)\n", len, ptr); |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
24 |
if (ptr == NULL) return NULL; |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
25 |
*store = len; |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
26 |
return (void *) (store + 1); |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
27 |
} // Malloc |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
28 |
|
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
29 |
|
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
30 |
static void Free(void *_ptr) |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
31 |
{ |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
32 |
int *ptr = (((int *) _ptr) - 1); |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
33 |
int len = *ptr; |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
34 |
printf("free() %d bytes (%p)\n", len, ptr); |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
35 |
free(ptr); |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
36 |
} // Free |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
37 |
#else |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
38 |
#define Malloc NULL |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
39 |
#define Free NULL |
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
40 |
#endif |
660
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
41 |
|
668 | 42 |
|
43 |
static void fail(const char *err) |
|
44 |
{ |
|
45 |
printf("%s.\n", err); |
|
46 |
exit(1); |
|
47 |
} // fail |
|
48 |
||
49 |
||
660
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
50 |
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
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
55 |
int i; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
56 |
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
|
57 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
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
|
62 |
return 0; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
63 |
|
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
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
|
68 |
continue; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
69 |
|
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
70 |
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
|
71 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
72 |
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
|
73 |
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
|
74 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
75 |
fclose(io); |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
76 |
return 0; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
77 |
} // if |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
78 |
|
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
79 |
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
|
80 |
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
|
81 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
82 |
fclose(io); |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
83 |
return 0; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
84 |
} // if |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
85 |
|
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
86 |
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
|
87 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
88 |
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
|
89 |
fclose(io); |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
90 |
return 0; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
91 |
} // if |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
92 |
|
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
93 |
fclose(io); |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
94 |
*outdata = data; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
95 |
*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
|
96 |
return 1; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
97 |
} // if |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
98 |
} // for |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
99 |
|
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
100 |
return 0; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
101 |
} // open_include |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
102 |
|
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
103 |
|
663 | 104 |
static void close_include(const char *data, MOJOSHADER_malloc m, |
105 |
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
|
106 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
107 |
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
|
108 |
} // close_include |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
109 |
|
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
110 |
|
578
6c8f73c845e7
Allow app to specify a base filename for error messages.
Ryan C. Gordon <icculus@icculus.org>
parents:
577
diff
changeset
|
111 |
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
|
112 |
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
|
113 |
const MOJOSHADER_preprocessorDefine *defs, |
669
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
114 |
unsigned int defcount, FILE *io) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
115 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
116 |
const MOJOSHADER_preprocessData *pd; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
117 |
int retval = 0; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
118 |
|
665
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
119 |
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
|
120 |
close_include, Malloc, Free, NULL); |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
121 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
122 |
if (pd->error_count > 0) |
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 |
int i; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
125 |
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
|
126 |
{ |
577
bec531dd448e
Report filenames in error messages.
Ryan C. Gordon <icculus@icculus.org>
parents:
556
diff
changeset
|
127 |
printf("%s:%d: ERROR: %s\n", |
bec531dd448e
Report filenames in error messages.
Ryan C. Gordon <icculus@icculus.org>
parents:
556
diff
changeset
|
128 |
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
|
129 |
pd->errors[i].error_position, |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
130 |
pd->errors[i].error); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
131 |
} // for |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
132 |
} // if |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
133 |
else |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
134 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
135 |
if (pd->output != NULL) |
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 (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
|
138 |
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
|
139 |
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
|
140 |
printf(" ... fclose('%s') failed.\n", outfile); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
141 |
else |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
142 |
retval = 1; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
143 |
} // if |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
144 |
} // else |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
145 |
MOJOSHADER_freePreprocessData(pd); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
146 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
147 |
return retval; |
556
04282775cc2c
Fixed cut-and-pasted comments.
Ryan C. Gordon <icculus@icculus.org>
parents:
555
diff
changeset
|
148 |
} // preprocess |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
149 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
150 |
|
669
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
151 |
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
|
152 |
const char *outfile, |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
153 |
const MOJOSHADER_preprocessorDefine *defs, |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
154 |
unsigned int defcount, FILE *io) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
155 |
{ |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
156 |
const MOJOSHADER_parseData *pd; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
157 |
int retval = 0; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
158 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
159 |
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
|
160 |
defs, defcount, open_include, close_include, |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
161 |
Malloc, Free, NULL); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
162 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
163 |
if (pd->error_count > 0) |
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 |
int i; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
166 |
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
|
167 |
{ |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
168 |
printf("%s:%d: ERROR: %s\n", |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
169 |
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
|
170 |
pd->errors[i].error_position, |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
171 |
pd->errors[i].error); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
172 |
} // for |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
173 |
} // if |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
174 |
else |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
175 |
{ |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
176 |
if (pd->output != NULL) |
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 (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
|
179 |
printf(" ... fwrite('%s') failed.\n", outfile); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
180 |
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
|
181 |
printf(" ... fclose('%s') failed.\n", outfile); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
182 |
else |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
183 |
retval = 1; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
184 |
} // if |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
185 |
} // else |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
186 |
MOJOSHADER_freeParseData(pd); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
187 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
188 |
return retval; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
189 |
} // assemble |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
190 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
191 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
192 |
typedef enum |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
193 |
{ |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
194 |
ACTION_UNKNOWN, |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
195 |
ACTION_PREPROCESS, |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
196 |
ACTION_ASSEMBLE, |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
197 |
} Action; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
198 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
199 |
|
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
200 |
int main(int argc, char **argv) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
201 |
{ |
669
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
202 |
Action action = ACTION_UNKNOWN; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
203 |
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
|
204 |
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
|
205 |
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
|
206 |
int i; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
207 |
|
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
208 |
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
|
209 |
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
|
210 |
|
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
211 |
for (i = 1; i < argc; i++) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
212 |
{ |
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
213 |
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
|
214 |
|
669
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
215 |
if (strcmp(arg, "-P") == 0) |
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 |
if (action != ACTION_UNKNOWN) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
218 |
fail("Multiple actions specified"); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
219 |
action = ACTION_PREPROCESS; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
220 |
} // if |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
221 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
222 |
else if (strcmp(arg, "-A") == 0) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
223 |
{ |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
224 |
if (action != ACTION_UNKNOWN) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
225 |
fail("Multiple actions specified"); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
226 |
action = ACTION_ASSEMBLE; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
227 |
} // else if |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
228 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
229 |
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
|
230 |
{ |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
231 |
if (outfile != NULL) |
668 | 232 |
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
|
233 |
|
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
234 |
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
|
235 |
if (arg == NULL) |
668 | 236 |
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
|
237 |
outfile = arg; |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
238 |
} // if |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
239 |
|
667
8efc63f4ab9b
That should be an "else if", not an "if" ...
Ryan C. Gordon <icculus@icculus.org>
parents:
666
diff
changeset
|
240 |
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
|
241 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
242 |
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
|
243 |
if (arg == NULL) |
668 | 244 |
fail("no path after '-I'"); |
245 |
||
660
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
246 |
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
|
247 |
(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
|
248 |
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
|
249 |
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
|
250 |
} // if |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
251 |
|
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
252 |
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
|
253 |
{ |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
254 |
arg += 2; |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
255 |
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
|
256 |
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
|
257 |
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
|
258 |
if (ptr) |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
259 |
{ |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
260 |
*ptr = '\0'; |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
261 |
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
|
262 |
} // if |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
263 |
|
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
264 |
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
|
265 |
(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
|
266 |
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
|
267 |
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
|
268 |
defcount++; |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
269 |
} // else if |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
270 |
|
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
271 |
else |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
272 |
{ |
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
273 |
if (infile != NULL) |
668 | 274 |
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
|
275 |
infile = arg; |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
276 |
} // else |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
277 |
} // for |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
278 |
|
669
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
279 |
if (action == ACTION_UNKNOWN) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
280 |
action = ACTION_ASSEMBLE; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
281 |
|
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
282 |
if (infile == NULL) |
668 | 283 |
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
|
284 |
|
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
285 |
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
|
286 |
if (io == NULL) |
669
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
287 |
fail("failed to open input file"); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
288 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
289 |
fseek(io, 0, SEEK_END); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
290 |
long fsize = ftell(io); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
291 |
fseek(io, 0, SEEK_SET); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
292 |
if (fsize == -1) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
293 |
fsize = 1000000; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
294 |
char *buf = (char *) malloc(fsize); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
295 |
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
|
296 |
fclose(io); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
297 |
if (rc == EOF) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
298 |
fail("failed to read input file"); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
299 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
300 |
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
|
301 |
if (outio == NULL) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
302 |
fail("failed to open output file"); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
303 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
304 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
305 |
if (action == ACTION_PREPROCESS) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
306 |
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
|
307 |
else if (action == ACTION_ASSEMBLE) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
308 |
retval = (!assemble(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
|
309 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
310 |
if ((retval != 0) && (outfile != NULL)) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
311 |
remove(outfile); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
312 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
313 |
free(buf); |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
314 |
|
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
315 |
for (i = 0; i < defcount; i++) |
661
a502c4999238
Fixed logic bug: used wrong variable.
Ryan C. Gordon <icculus@icculus.org>
parents:
660
diff
changeset
|
316 |
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
|
317 |
free(defs); |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
318 |
|
660
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
319 |
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
|
320 |
|
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
321 |
return retval; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
322 |
} // main |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
323 |
|
666 | 324 |
// end of mojoshader-compiler.c ... |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
325 |