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"
42 struct sl_pp_purify_options options
;
43 struct sl_pp_context
*context
;
45 struct sl_pp_token_info
*outtokens
;
53 in
= fopen(argv
[1], "rb");
58 fseek(in
, 0, SEEK_END
);
60 fseek(in
, 0, SEEK_SET
);
62 out
= fopen(argv
[2], "wb");
68 inbuf
= malloc(size
+ 1);
70 fprintf(out
, "$OOMERROR\n");
77 if (fread(inbuf
, 1, size
, in
) != size
) {
78 fprintf(out
, "$READERROR\n");
89 memset(&options
, 0, sizeof(options
));
91 context
= sl_pp_context_create(inbuf
, &options
);
93 fprintf(out
, "$CONTEXERROR\n");
100 if (sl_pp_version(context
, &version
)) {
101 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
103 sl_pp_context_destroy(context
);
109 if (sl_pp_context_add_extension(context
, "ARB_draw_buffers", "GL_ARB_draw_buffers") ||
110 sl_pp_context_add_extension(context
, "ARB_texture_rectangle", "GL_ARB_texture_rectangle")) {
111 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
113 printf("Error: %s\n", sl_pp_context_error_message(context
));
114 sl_pp_context_destroy(context
);
120 if (sl_pp_context_add_predefined(context
, "__GLSL_PP_PREDEFINED_MACRO_TEST", "1")) {
121 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
123 printf("Error: %s\n", sl_pp_context_error_message(context
));
124 sl_pp_context_destroy(context
);
130 if (sl_pp_process(context
, &outtokens
)) {
131 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
133 sl_pp_context_destroy(context
);
141 for (i
= 0; outtokens
[i
].token
!= SL_PP_EOF
; i
++) {
142 switch (outtokens
[i
].token
) {
151 case SL_PP_SEMICOLON
:
183 case SL_PP_INCREMENT
:
187 case SL_PP_ADDASSIGN
:
195 case SL_PP_DECREMENT
:
199 case SL_PP_SUBASSIGN
:
219 case SL_PP_MULASSIGN
:
227 case SL_PP_DIVASSIGN
:
235 case SL_PP_MODASSIGN
:
236 fprintf(out
, "%%= ");
243 case SL_PP_LSHIFTASSIGN
:
244 fprintf(out
, "<<= ");
251 case SL_PP_LESSEQUAL
:
259 case SL_PP_RSHIFTASSIGN
:
260 fprintf(out
, ">>= ");
267 case SL_PP_GREATEREQUAL
:
287 case SL_PP_BITANDASSIGN
:
299 case SL_PP_BITXORASSIGN
:
311 case SL_PP_BITORASSIGN
:
327 case SL_PP_IDENTIFIER
:
328 fprintf(out
, "%s ", sl_pp_context_cstr(context
, outtokens
[i
].data
.identifier
));
332 fprintf(out
, "%s ", sl_pp_context_cstr(context
, outtokens
[i
].data
._uint
));
336 fprintf(out
, "%s ", sl_pp_context_cstr(context
, outtokens
[i
].data
._float
));
340 fprintf(out
, "%c", outtokens
[i
].data
.other
);
343 case SL_PP_PRAGMA_OPTIMIZE
:
344 fprintf(out
, "#pragma optimize(%s)", outtokens
[i
].data
.pragma
? "on" : "off");
347 case SL_PP_PRAGMA_DEBUG
:
348 fprintf(out
, "#pragma debug(%s)", outtokens
[i
].data
.pragma
? "on" : "off");
351 case SL_PP_EXTENSION_REQUIRE
:
352 fprintf(out
, "#extension %s : require", sl_pp_context_cstr(context
, outtokens
[i
].data
.extension
));
355 case SL_PP_EXTENSION_ENABLE
:
356 fprintf(out
, "#extension %s : enable", sl_pp_context_cstr(context
, outtokens
[i
].data
.extension
));
359 case SL_PP_EXTENSION_WARN
:
360 fprintf(out
, "#extension %s : warn", sl_pp_context_cstr(context
, outtokens
[i
].data
.extension
));
363 case SL_PP_EXTENSION_DISABLE
:
364 fprintf(out
, "#extension %s : disable", sl_pp_context_cstr(context
, outtokens
[i
].data
.extension
));
368 fprintf(out
, "#line %u %u", outtokens
[i
].data
.line
.lineno
, outtokens
[i
].data
.line
.fileno
);
376 sl_pp_context_destroy(context
);