nv50: fix build-predicate function
[mesa.git] / src / glsl / apps / process.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 #include "../pp/sl_pp_public.h"
33 #include "../pp/sl_pp_purify.h"
34 #include "../pp/sl_pp_token.h"
35
36
37 int
38 main(int argc,
39 char *argv[])
40 {
41 FILE *in;
42 long size;
43 char *inbuf;
44 struct sl_pp_purify_options options;
45 struct sl_pp_context *context;
46 unsigned int version;
47 struct sl_pp_token_info *outtokens;
48 FILE *out;
49 unsigned int i;
50
51 if (argc != 3) {
52 printf("Usage: process infile outfile\n");
53 return 1;
54 }
55
56 in = fopen(argv[1], "rb");
57 if (!in) {
58 return 1;
59 }
60
61 fseek(in, 0, SEEK_END);
62 size = ftell(in);
63 assert(size != -1);
64 if (size == -1) {
65 return 1;
66 }
67 fseek(in, 0, SEEK_SET);
68
69 out = fopen(argv[2], "wb");
70 if (!out) {
71 fclose(in);
72 return 1;
73 }
74
75 inbuf = malloc(size + 1);
76 if (!inbuf) {
77 fprintf(out, "$OOMERROR\n");
78
79 fclose(out);
80 fclose(in);
81 return 1;
82 }
83
84 if (fread(inbuf, 1, size, in) != size) {
85 fprintf(out, "$READERROR\n");
86
87 free(inbuf);
88 fclose(out);
89 fclose(in);
90 return 1;
91 }
92 inbuf[size] = '\0';
93
94 fclose(in);
95
96 memset(&options, 0, sizeof(options));
97
98 context = sl_pp_context_create(inbuf, &options);
99 if (!context) {
100 fprintf(out, "$CONTEXERROR\n");
101
102 free(inbuf);
103 fclose(out);
104 return 1;
105 }
106
107 if (sl_pp_version(context, &version)) {
108 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
109
110 sl_pp_context_destroy(context);
111 free(inbuf);
112 fclose(out);
113 return -1;
114 }
115
116 if (sl_pp_context_add_extension(context, "GL_ARB_draw_buffers") ||
117 sl_pp_context_add_extension(context, "GL_ARB_texture_rectangle")) {
118 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
119
120 printf("Error: %s\n", sl_pp_context_error_message(context));
121 sl_pp_context_destroy(context);
122 free(inbuf);
123 fclose(out);
124 return 0;
125 }
126
127 if (sl_pp_context_add_predefined(context, "__GLSL_PP_PREDEFINED_MACRO_TEST", "1")) {
128 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
129
130 printf("Error: %s\n", sl_pp_context_error_message(context));
131 sl_pp_context_destroy(context);
132 free(inbuf);
133 fclose(out);
134 return 0;
135 }
136
137 if (sl_pp_process(context, &outtokens)) {
138 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
139
140 sl_pp_context_destroy(context);
141 free(inbuf);
142 fclose(out);
143 return -1;
144 }
145
146 free(inbuf);
147
148 for (i = 0; outtokens[i].token != SL_PP_EOF; i++) {
149 switch (outtokens[i].token) {
150 case SL_PP_NEWLINE:
151 fprintf(out, "\n");
152 break;
153
154 case SL_PP_COMMA:
155 fprintf(out, ", ");
156 break;
157
158 case SL_PP_SEMICOLON:
159 fprintf(out, "; ");
160 break;
161
162 case SL_PP_LBRACE:
163 fprintf(out, "{ ");
164 break;
165
166 case SL_PP_RBRACE:
167 fprintf(out, "} ");
168 break;
169
170 case SL_PP_LPAREN:
171 fprintf(out, "( ");
172 break;
173
174 case SL_PP_RPAREN:
175 fprintf(out, ") ");
176 break;
177
178 case SL_PP_LBRACKET:
179 fprintf(out, "[ ");
180 break;
181
182 case SL_PP_RBRACKET:
183 fprintf(out, "] ");
184 break;
185
186 case SL_PP_DOT:
187 fprintf(out, ". ");
188 break;
189
190 case SL_PP_INCREMENT:
191 fprintf(out, "++ ");
192 break;
193
194 case SL_PP_ADDASSIGN:
195 fprintf(out, "+= ");
196 break;
197
198 case SL_PP_PLUS:
199 fprintf(out, "+ ");
200 break;
201
202 case SL_PP_DECREMENT:
203 fprintf(out, "-- ");
204 break;
205
206 case SL_PP_SUBASSIGN:
207 fprintf(out, "-= ");
208 break;
209
210 case SL_PP_MINUS:
211 fprintf(out, "- ");
212 break;
213
214 case SL_PP_BITNOT:
215 fprintf(out, "~ ");
216 break;
217
218 case SL_PP_NOTEQUAL:
219 fprintf(out, "!= ");
220 break;
221
222 case SL_PP_NOT:
223 fprintf(out, "! ");
224 break;
225
226 case SL_PP_MULASSIGN:
227 fprintf(out, "*= ");
228 break;
229
230 case SL_PP_STAR:
231 fprintf(out, "* ");
232 break;
233
234 case SL_PP_DIVASSIGN:
235 fprintf(out, "/= ");
236 break;
237
238 case SL_PP_SLASH:
239 fprintf(out, "/ ");
240 break;
241
242 case SL_PP_MODASSIGN:
243 fprintf(out, "%%= ");
244 break;
245
246 case SL_PP_MODULO:
247 fprintf(out, "%% ");
248 break;
249
250 case SL_PP_LSHIFTASSIGN:
251 fprintf(out, "<<= ");
252 break;
253
254 case SL_PP_LSHIFT:
255 fprintf(out, "<< ");
256 break;
257
258 case SL_PP_LESSEQUAL:
259 fprintf(out, "<= ");
260 break;
261
262 case SL_PP_LESS:
263 fprintf(out, "< ");
264 break;
265
266 case SL_PP_RSHIFTASSIGN:
267 fprintf(out, ">>= ");
268 break;
269
270 case SL_PP_RSHIFT:
271 fprintf(out, ">> ");
272 break;
273
274 case SL_PP_GREATEREQUAL:
275 fprintf(out, ">= ");
276 break;
277
278 case SL_PP_GREATER:
279 fprintf(out, "> ");
280 break;
281
282 case SL_PP_EQUAL:
283 fprintf(out, "== ");
284 break;
285
286 case SL_PP_ASSIGN:
287 fprintf(out, "= ");
288 break;
289
290 case SL_PP_AND:
291 fprintf(out, "&& ");
292 break;
293
294 case SL_PP_BITANDASSIGN:
295 fprintf(out, "&= ");
296 break;
297
298 case SL_PP_BITAND:
299 fprintf(out, "& ");
300 break;
301
302 case SL_PP_XOR:
303 fprintf(out, "^^ ");
304 break;
305
306 case SL_PP_BITXORASSIGN:
307 fprintf(out, "^= ");
308 break;
309
310 case SL_PP_BITXOR:
311 fprintf(out, "^ ");
312 break;
313
314 case SL_PP_OR:
315 fprintf(out, "|| ");
316 break;
317
318 case SL_PP_BITORASSIGN:
319 fprintf(out, "|= ");
320 break;
321
322 case SL_PP_BITOR:
323 fprintf(out, "| ");
324 break;
325
326 case SL_PP_QUESTION:
327 fprintf(out, "? ");
328 break;
329
330 case SL_PP_COLON:
331 fprintf(out, ": ");
332 break;
333
334 case SL_PP_IDENTIFIER:
335 fprintf(out, "%s ", sl_pp_context_cstr(context, outtokens[i].data.identifier));
336 break;
337
338 case SL_PP_UINT:
339 fprintf(out, "%s ", sl_pp_context_cstr(context, outtokens[i].data._uint));
340 break;
341
342 case SL_PP_FLOAT:
343 fprintf(out, "%s ", sl_pp_context_cstr(context, outtokens[i].data._float));
344 break;
345
346 case SL_PP_OTHER:
347 fprintf(out, "%c", outtokens[i].data.other);
348 break;
349
350 case SL_PP_PRAGMA_OPTIMIZE:
351 fprintf(out, "#pragma optimize(%s)", outtokens[i].data.pragma ? "on" : "off");
352 break;
353
354 case SL_PP_PRAGMA_DEBUG:
355 fprintf(out, "#pragma debug(%s)", outtokens[i].data.pragma ? "on" : "off");
356 break;
357
358 case SL_PP_EXTENSION_REQUIRE:
359 fprintf(out, "#extension %s : require", sl_pp_context_cstr(context, outtokens[i].data.extension));
360 break;
361
362 case SL_PP_EXTENSION_ENABLE:
363 fprintf(out, "#extension %s : enable", sl_pp_context_cstr(context, outtokens[i].data.extension));
364 break;
365
366 case SL_PP_EXTENSION_WARN:
367 fprintf(out, "#extension %s : warn", sl_pp_context_cstr(context, outtokens[i].data.extension));
368 break;
369
370 case SL_PP_EXTENSION_DISABLE:
371 fprintf(out, "#extension %s : disable", sl_pp_context_cstr(context, outtokens[i].data.extension));
372 break;
373
374 case SL_PP_LINE:
375 fprintf(out, "#line %u %u", outtokens[i].data.line.lineno, outtokens[i].data.line.fileno);
376 break;
377
378 default:
379 assert(0);
380 }
381 }
382
383 sl_pp_context_destroy(context);
384 free(outtokens);
385 fclose(out);
386
387 return 0;
388 }