author | Ryan C. Gordon <icculus@icculus.org> |
Thu, 09 Apr 2009 04:02:28 -0400 | |
changeset 747 | 0d1dbadcb387 |
parent 706 | 01a92f30b84f |
child 793 | 616980fb00bb |
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 |
|
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 | 44 |
|
45 |
static void fail(const char *err) |
|
46 |
{ |
|
47 |
printf("%s.\n", err); |
|
48 |
exit(1); |
|
49 |
} // fail |
|
50 |
||
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 | 106 |
static void close_include(const char *data, MOJOSHADER_malloc m, |
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 |
{ |
747
0d1dbadcb387
Don't report false error when writing a zero-byte file.
Ryan C. Gordon <icculus@icculus.org>
parents:
706
diff
changeset
|
139 |
const int len = pd->output_len; |
0d1dbadcb387
Don't report false error when writing a zero-byte file.
Ryan C. Gordon <icculus@icculus.org>
parents:
706
diff
changeset
|
140 |
if ((len) && (fwrite(pd->output, len, 1, io) != 1)) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
141 |
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
|
142 |
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
|
143 |
printf(" ... fclose('%s') failed.\n", outfile); |
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 |
retval = 1; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
146 |
} // if |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
147 |
} // else |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
148 |
MOJOSHADER_freePreprocessData(pd); |
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 |
return retval; |
556
04282775cc2c
Fixed cut-and-pasted comments.
Ryan C. Gordon <icculus@icculus.org>
parents:
555
diff
changeset
|
151 |
} // preprocess |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
152 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
153 |
|
669
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
154 |
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
|
155 |
const char *outfile, |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
156 |
const MOJOSHADER_preprocessorDefine *defs, |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
157 |
unsigned int defcount, FILE *io) |
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 |
const MOJOSHADER_parseData *pd; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
160 |
int retval = 0; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
161 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
162 |
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
|
163 |
defs, defcount, open_include, close_include, |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
164 |
Malloc, Free, NULL); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
165 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
166 |
if (pd->error_count > 0) |
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 |
int i; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
169 |
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
|
170 |
{ |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
171 |
printf("%s:%d: ERROR: %s\n", |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
172 |
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
|
173 |
pd->errors[i].error_position, |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
174 |
pd->errors[i].error); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
175 |
} // for |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
176 |
} // if |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
177 |
else |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
178 |
{ |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
179 |
if (pd->output != NULL) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
180 |
{ |
747
0d1dbadcb387
Don't report false error when writing a zero-byte file.
Ryan C. Gordon <icculus@icculus.org>
parents:
706
diff
changeset
|
181 |
const int len = pd->output_len; |
0d1dbadcb387
Don't report false error when writing a zero-byte file.
Ryan C. Gordon <icculus@icculus.org>
parents:
706
diff
changeset
|
182 |
if ((len) && (fwrite(pd->output, len, 1, io) != 1)) |
669
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
183 |
printf(" ... fwrite('%s') failed.\n", outfile); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
184 |
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
|
185 |
printf(" ... fclose('%s') failed.\n", outfile); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
186 |
else |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
187 |
retval = 1; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
188 |
} // if |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
189 |
} // else |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
190 |
MOJOSHADER_freeParseData(pd); |
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 |
return retval; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
193 |
} // assemble |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
194 |
|
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
|
195 |
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
|
196 |
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
|
197 |
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
|
198 |
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
|
199 |
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
|
200 |
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
|
201 |
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
|
202 |
|
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 |
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
|
204 |
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
|
205 |
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
|
206 |
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
|
207 |
{ |
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 |
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
|
209 |
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
|
210 |
|
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 |
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
|
212 |
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
|
213 |
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
|
214 |
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
|
215 |
} // compile |
669
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 |
typedef enum |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
218 |
{ |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
219 |
ACTION_UNKNOWN, |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
220 |
ACTION_PREPROCESS, |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
221 |
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
|
222 |
ACTION_COMPILE, |
669
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
223 |
} Action; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
224 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
225 |
|
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
226 |
int main(int argc, char **argv) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
227 |
{ |
669
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
228 |
Action action = ACTION_UNKNOWN; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
229 |
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
|
230 |
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
|
231 |
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
|
232 |
int i; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
233 |
|
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
234 |
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
|
235 |
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
|
236 |
|
686
021241f2a237
Current working directory should always be in the include path.
Ryan C. Gordon <icculus@icculus.org>
parents:
673
diff
changeset
|
237 |
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
|
238 |
include_paths[0] = "."; |
021241f2a237
Current working directory should always be in the include path.
Ryan C. Gordon <icculus@icculus.org>
parents:
673
diff
changeset
|
239 |
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
|
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 |
for (i = 1; i < argc; i++) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
242 |
{ |
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
243 |
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
|
244 |
|
669
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
245 |
if (strcmp(arg, "-P") == 0) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
246 |
{ |
688
29ee34e66161
No reason you can't specify the same option twice.
Ryan C. Gordon <icculus@icculus.org>
parents:
686
diff
changeset
|
247 |
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
|
248 |
fail("Multiple actions specified"); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
249 |
action = ACTION_PREPROCESS; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
250 |
} // if |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
251 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
252 |
else if (strcmp(arg, "-A") == 0) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
253 |
{ |
688
29ee34e66161
No reason you can't specify the same option twice.
Ryan C. Gordon <icculus@icculus.org>
parents:
686
diff
changeset
|
254 |
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
|
255 |
fail("Multiple actions specified"); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
256 |
action = ACTION_ASSEMBLE; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
257 |
} // else if |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
258 |
|
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
|
259 |
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
|
260 |
{ |
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 |
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
|
262 |
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
|
263 |
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
|
264 |
} // 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
|
265 |
|
669
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
266 |
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
|
267 |
{ |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
268 |
if (outfile != NULL) |
668 | 269 |
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
|
270 |
|
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
271 |
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
|
272 |
if (arg == NULL) |
668 | 273 |
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
|
274 |
outfile = arg; |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
275 |
} // if |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
276 |
|
667
8efc63f4ab9b
That should be an "else if", not an "if" ...
Ryan C. Gordon <icculus@icculus.org>
parents:
666
diff
changeset
|
277 |
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
|
278 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
279 |
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
|
280 |
if (arg == NULL) |
668 | 281 |
fail("no path after '-I'"); |
282 |
||
660
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 = (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
|
284 |
(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
|
285 |
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
|
286 |
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
|
287 |
} // if |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
288 |
|
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
289 |
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
|
290 |
{ |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
291 |
arg += 2; |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
292 |
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
|
293 |
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
|
294 |
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
|
295 |
if (ptr) |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
296 |
{ |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
297 |
*ptr = '\0'; |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
298 |
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
|
299 |
} // if |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
300 |
|
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
301 |
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
|
302 |
(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
|
303 |
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
|
304 |
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
|
305 |
defcount++; |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
306 |
} // else if |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
307 |
|
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
308 |
else |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
309 |
{ |
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
310 |
if (infile != NULL) |
670 | 311 |
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
|
312 |
infile = arg; |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
313 |
} // else |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
314 |
} // for |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
315 |
|
669
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
316 |
if (action == ACTION_UNKNOWN) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
317 |
action = ACTION_ASSEMBLE; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
318 |
|
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
319 |
if (infile == NULL) |
670 | 320 |
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
|
321 |
|
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
322 |
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
|
323 |
if (io == NULL) |
669
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
324 |
fail("failed to open input file"); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
325 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
326 |
fseek(io, 0, SEEK_END); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
327 |
long fsize = ftell(io); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
328 |
fseek(io, 0, SEEK_SET); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
329 |
if (fsize == -1) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
330 |
fsize = 1000000; |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
331 |
char *buf = (char *) malloc(fsize); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
332 |
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
|
333 |
fclose(io); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
334 |
if (rc == EOF) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
335 |
fail("failed to read input file"); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
336 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
337 |
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
|
338 |
if (outio == NULL) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
339 |
fail("failed to open output file"); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
340 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
341 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
342 |
if (action == ACTION_PREPROCESS) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
343 |
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
|
344 |
else if (action == ACTION_ASSEMBLE) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
345 |
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
|
346 |
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
|
347 |
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
|
348 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
349 |
if ((retval != 0) && (outfile != NULL)) |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
350 |
remove(outfile); |
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
351 |
|
fd14a7278a95
Merged assemble.c into mojoshader-compiler.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
668
diff
changeset
|
352 |
free(buf); |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
353 |
|
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
354 |
for (i = 0; i < defcount; i++) |
661
a502c4999238
Fixed logic bug: used wrong variable.
Ryan C. Gordon <icculus@icculus.org>
parents:
660
diff
changeset
|
355 |
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
|
356 |
free(defs); |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
357 |
|
660
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
358 |
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
|
359 |
|
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
360 |
return retval; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
361 |
} // main |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
362 |
|
666 | 363 |
// end of mojoshader-compiler.c ... |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
364 |