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
;
44 struct sl_pp_context
*context
;
45 struct sl_pp_token_info
*tokens
;
47 unsigned int tokens_eaten
;
48 struct sl_pp_token_info
*outtokens
;
56 in
= fopen(argv
[1], "rb");
61 fseek(in
, 0, SEEK_END
);
63 fseek(in
, 0, SEEK_SET
);
65 out
= fopen(argv
[2], "wb");
71 inbuf
= malloc(size
+ 1);
73 fprintf(out
, "$OOMERROR\n");
80 if (fread(inbuf
, 1, size
, in
) != size
) {
81 fprintf(out
, "$READERROR\n");
92 memset(&options
, 0, sizeof(options
));
94 if (sl_pp_purify(inbuf
, &options
, &outbuf
)) {
95 fprintf(out
, "$PURIFYERROR\n");
104 context
= sl_pp_context_create();
106 fprintf(out
, "$CONTEXERROR\n");
113 if (sl_pp_tokenise(context
, outbuf
, &tokens
)) {
114 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
116 sl_pp_context_destroy(context
);
124 if (sl_pp_version(context
, tokens
, &version
, &tokens_eaten
)) {
125 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
127 sl_pp_context_destroy(context
);
133 if (sl_pp_process(context
, &tokens
[tokens_eaten
], &outtokens
)) {
134 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
136 sl_pp_context_destroy(context
);
144 for (i
= 0; outtokens
[i
].token
!= SL_PP_EOF
; i
++) {
145 switch (outtokens
[i
].token
) {
154 case SL_PP_SEMICOLON
:
186 case SL_PP_INCREMENT
:
190 case SL_PP_ADDASSIGN
:
198 case SL_PP_DECREMENT
:
202 case SL_PP_SUBASSIGN
:
222 case SL_PP_MULASSIGN
:
230 case SL_PP_DIVASSIGN
:
238 case SL_PP_MODASSIGN
:
239 fprintf(out
, "%%= ");
246 case SL_PP_LSHIFTASSIGN
:
247 fprintf(out
, "<<= ");
254 case SL_PP_LESSEQUAL
:
262 case SL_PP_RSHIFTASSIGN
:
263 fprintf(out
, ">>= ");
270 case SL_PP_GREATEREQUAL
:
290 case SL_PP_BITANDASSIGN
:
302 case SL_PP_BITXORASSIGN
:
314 case SL_PP_BITORASSIGN
:
330 case SL_PP_IDENTIFIER
:
331 fprintf(out
, "%s ", sl_pp_context_cstr(context
, outtokens
[i
].data
.identifier
));
335 fprintf(out
, "%s ", sl_pp_context_cstr(context
, outtokens
[i
].data
._uint
));
339 fprintf(out
, "%s ", sl_pp_context_cstr(context
, outtokens
[i
].data
._float
));
343 fprintf(out
, "%c", outtokens
[i
].data
.other
);
346 case SL_PP_PRAGMA_OPTIMIZE
:
347 fprintf(out
, "#pragma optimize(%s)", outtokens
[i
].data
.pragma
? "on" : "off");
350 case SL_PP_PRAGMA_DEBUG
:
351 fprintf(out
, "#pragma debug(%s)", outtokens
[i
].data
.pragma
? "on" : "off");
354 case SL_PP_EXTENSION_REQUIRE
:
355 fprintf(out
, "#extension %s : require", sl_pp_context_cstr(context
, outtokens
[i
].data
.extension
));
358 case SL_PP_EXTENSION_ENABLE
:
359 fprintf(out
, "#extension %s : enable", sl_pp_context_cstr(context
, outtokens
[i
].data
.extension
));
362 case SL_PP_EXTENSION_WARN
:
363 fprintf(out
, "#extension %s : warn", sl_pp_context_cstr(context
, outtokens
[i
].data
.extension
));
366 case SL_PP_EXTENSION_DISABLE
:
367 fprintf(out
, "#extension %s : disable", sl_pp_context_cstr(context
, outtokens
[i
].data
.extension
));
371 fprintf(out
, "#line %u %u", outtokens
[i
].data
.line
.lineno
, outtokens
[i
].data
.line
.fileno
);
379 sl_pp_context_destroy(context
);