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 **************************************************************************/
31 #include "../pp/sl_pp_context.h"
32 #include "../pp/sl_pp_purify.h"
33 #include "../pp/sl_pp_version.h"
34 #include "../pp/sl_pp_process.h"
44 struct sl_pp_purify_options options
;
46 struct sl_pp_context context
;
47 struct sl_pp_token_info
*tokens
;
49 unsigned int tokens_eaten
;
50 struct sl_pp_token_info
*outtokens
;
58 in
= fopen(argv
[1], "rb");
63 fseek(in
, 0, SEEK_END
);
65 fseek(in
, 0, SEEK_SET
);
67 inbuf
= malloc(size
+ 1);
73 if (fread(inbuf
, 1, size
, in
) != size
) {
82 memset(&options
, 0, sizeof(options
));
84 if (sl_pp_purify(inbuf
, &options
, &outbuf
)) {
91 sl_pp_context_init(&context
);
93 if (sl_pp_tokenise(&context
, outbuf
, &tokens
)) {
94 sl_pp_context_destroy(&context
);
101 if (sl_pp_version(&context
, tokens
, &version
, &tokens_eaten
)) {
102 sl_pp_context_destroy(&context
);
107 out
= fopen(argv
[2], "wb");
109 sl_pp_context_destroy(&context
);
114 if (sl_pp_process(&context
, &tokens
[tokens_eaten
], &outtokens
)) {
115 fprintf(out
, "$ERROR: `%s'\n", context
.error_msg
);
117 sl_pp_context_destroy(&context
);
125 for (i
= 0; outtokens
[i
].token
!= SL_PP_EOF
; i
++) {
126 switch (outtokens
[i
].token
) {
135 case SL_PP_SEMICOLON
:
167 case SL_PP_INCREMENT
:
171 case SL_PP_ADDASSIGN
:
179 case SL_PP_DECREMENT
:
183 case SL_PP_SUBASSIGN
:
203 case SL_PP_MULASSIGN
:
211 case SL_PP_DIVASSIGN
:
219 case SL_PP_MODASSIGN
:
227 case SL_PP_LSHIFTASSIGN
:
228 fprintf(out
, "<<= ");
235 case SL_PP_LESSEQUAL
:
243 case SL_PP_RSHIFTASSIGN
:
244 fprintf(out
, ">>= ");
251 case SL_PP_GREATEREQUAL
:
271 case SL_PP_BITANDASSIGN
:
283 case SL_PP_BITXORASSIGN
:
295 case SL_PP_BITORASSIGN
:
311 case SL_PP_IDENTIFIER
:
312 fprintf(out
, "%s ", sl_pp_context_cstr(&context
, outtokens
[i
].data
.identifier
));
316 fprintf(out
, "%s ", sl_pp_context_cstr(&context
, outtokens
[i
].data
.number
));
320 fprintf(out
, "%c", outtokens
[i
].data
.other
);
323 case SL_PP_PRAGMA_OPTIMIZE
:
324 fprintf(out
, "#pragma optimize(%s)", outtokens
[i
].data
.pragma
? "on" : "off");
327 case SL_PP_PRAGMA_DEBUG
:
328 fprintf(out
, "#pragma debug(%s)", outtokens
[i
].data
.pragma
? "on" : "off");
331 case SL_PP_EXTENSION_REQUIRE
:
332 fprintf(out
, "#extension %s : require", sl_pp_context_cstr(&context
, outtokens
[i
].data
.extension
));
335 case SL_PP_EXTENSION_ENABLE
:
336 fprintf(out
, "#extension %s : enable", sl_pp_context_cstr(&context
, outtokens
[i
].data
.extension
));
339 case SL_PP_EXTENSION_WARN
:
340 fprintf(out
, "#extension %s : warn", sl_pp_context_cstr(&context
, outtokens
[i
].data
.extension
));
343 case SL_PP_EXTENSION_DISABLE
:
344 fprintf(out
, "#extension %s : disable", sl_pp_context_cstr(&context
, outtokens
[i
].data
.extension
));
348 fprintf(out
, "#line %u", outtokens
[i
].data
.line
);
352 fprintf(out
, " #file %u", outtokens
[i
].data
.file
);
360 sl_pp_context_destroy(&context
);