1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
32 #include "../pp/sl_pp_public.h"
33 #include "../pp/sl_pp_purify.h"
34 #include "../pp/sl_pp_token.h"
44 struct sl_pp_purify_options options
;
45 struct sl_pp_context
*context
;
47 struct sl_pp_token_info
*outtokens
;
52 printf("Usage: process infile outfile\n");
56 in
= fopen(argv
[1], "rb");
61 fseek(in
, 0, SEEK_END
);
67 fseek(in
, 0, SEEK_SET
);
69 out
= fopen(argv
[2], "wb");
75 inbuf
= malloc(size
+ 1);
77 fprintf(out
, "$OOMERROR\n");
84 if (fread(inbuf
, 1, size
, in
) != size
) {
85 fprintf(out
, "$READERROR\n");
96 memset(&options
, 0, sizeof(options
));
98 context
= sl_pp_context_create(inbuf
, &options
);
100 fprintf(out
, "$CONTEXERROR\n");
107 if (sl_pp_version(context
, &version
)) {
108 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
110 sl_pp_context_destroy(context
);
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
));
120 printf("Error: %s\n", sl_pp_context_error_message(context
));
121 sl_pp_context_destroy(context
);
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
));
130 printf("Error: %s\n", sl_pp_context_error_message(context
));
131 sl_pp_context_destroy(context
);
137 if (sl_pp_process(context
, &outtokens
)) {
138 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
140 sl_pp_context_destroy(context
);
148 for (i
= 0; outtokens
[i
].token
!= SL_PP_EOF
; i
++) {
149 switch (outtokens
[i
].token
) {
158 case SL_PP_SEMICOLON
:
190 case SL_PP_INCREMENT
:
194 case SL_PP_ADDASSIGN
:
202 case SL_PP_DECREMENT
:
206 case SL_PP_SUBASSIGN
:
226 case SL_PP_MULASSIGN
:
234 case SL_PP_DIVASSIGN
:
242 case SL_PP_MODASSIGN
:
243 fprintf(out
, "%%= ");
250 case SL_PP_LSHIFTASSIGN
:
251 fprintf(out
, "<<= ");
258 case SL_PP_LESSEQUAL
:
266 case SL_PP_RSHIFTASSIGN
:
267 fprintf(out
, ">>= ");
274 case SL_PP_GREATEREQUAL
:
294 case SL_PP_BITANDASSIGN
:
306 case SL_PP_BITXORASSIGN
:
318 case SL_PP_BITORASSIGN
:
334 case SL_PP_IDENTIFIER
:
335 fprintf(out
, "%s ", sl_pp_context_cstr(context
, outtokens
[i
].data
.identifier
));
339 fprintf(out
, "%s ", sl_pp_context_cstr(context
, outtokens
[i
].data
._uint
));
343 fprintf(out
, "%s ", sl_pp_context_cstr(context
, outtokens
[i
].data
._float
));
347 fprintf(out
, "%c", outtokens
[i
].data
.other
);
350 case SL_PP_PRAGMA_OPTIMIZE
:
351 fprintf(out
, "#pragma optimize(%s)", outtokens
[i
].data
.pragma
? "on" : "off");
354 case SL_PP_PRAGMA_DEBUG
:
355 fprintf(out
, "#pragma debug(%s)", outtokens
[i
].data
.pragma
? "on" : "off");
358 case SL_PP_EXTENSION_REQUIRE
:
359 fprintf(out
, "#extension %s : require", sl_pp_context_cstr(context
, outtokens
[i
].data
.extension
));
362 case SL_PP_EXTENSION_ENABLE
:
363 fprintf(out
, "#extension %s : enable", sl_pp_context_cstr(context
, outtokens
[i
].data
.extension
));
366 case SL_PP_EXTENSION_WARN
:
367 fprintf(out
, "#extension %s : warn", sl_pp_context_cstr(context
, outtokens
[i
].data
.extension
));
370 case SL_PP_EXTENSION_DISABLE
:
371 fprintf(out
, "#extension %s : disable", sl_pp_context_cstr(context
, outtokens
[i
].data
.extension
));
375 fprintf(out
, "#line %u %u", outtokens
[i
].data
.line
.lineno
, outtokens
[i
].data
.line
.fileno
);
383 sl_pp_context_destroy(context
);