Hash :
3b5643cf
Author :
Date :
2011-02-28T15:06:55
Avoid division by zero in the preprocessor Trac #15792 Issue=115 Signed-off-by: Daniel Koch Author: Nicolas Capens git-svn-id: https://angleproject.googlecode.com/svn/trunk@566 736b8ea6-26fd-11df-bfd4-992fa37f6226
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
/****************************************************************************\
Copyright (c) 2002, NVIDIA Corporation.
NVIDIA Corporation("NVIDIA") supplies this software to you in
consideration of your agreement to the following terms, and your use,
installation, modification or redistribution of this NVIDIA software
constitutes acceptance of these terms. If you do not agree with these
terms, please do not use, install, modify or redistribute this NVIDIA
software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, NVIDIA grants you a personal, non-exclusive
license, under NVIDIA's copyrights in this original NVIDIA software (the
"NVIDIA Software"), to use, reproduce, modify and redistribute the
NVIDIA Software, with or without modifications, in source and/or binary
forms; provided that if you redistribute the NVIDIA Software, you must
retain the copyright notice of NVIDIA, this notice and the following
text and disclaimers in all such redistributions of the NVIDIA Software.
Neither the name, trademarks, service marks nor logos of NVIDIA
Corporation may be used to endorse or promote products derived from the
NVIDIA Software without specific prior written permission from NVIDIA.
Except as expressly stated in this notice, no other rights or licenses
express or implied, are granted by NVIDIA herein, including but not
limited to any patent rights that may be infringed by your derivative
works or by other works in which the NVIDIA Software may be
incorporated. No hardware is licensed hereunder.
THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE,
NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER
PRODUCTS.
IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\****************************************************************************/
//
// cpp.c
//
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "compiler/preprocessor/slglobals.h"
static int CPPif(yystypepp * yylvalpp);
/* Don't use memory.c's replacements, as we clean up properly here */
#undef malloc
#undef free
static int bindAtom = 0;
static int constAtom = 0;
static int defaultAtom = 0;
static int defineAtom = 0;
static int definedAtom = 0;
static int elseAtom = 0;
static int elifAtom = 0;
static int endifAtom = 0;
static int ifAtom = 0;
static int ifdefAtom = 0;
static int ifndefAtom = 0;
static int includeAtom = 0;
static int lineAtom = 0;
static int pragmaAtom = 0;
static int texunitAtom = 0;
static int undefAtom = 0;
static int errorAtom = 0;
static int __LINE__Atom = 0;
static int __FILE__Atom = 0;
static int __VERSION__Atom = 0;
static int versionAtom = 0;
static int extensionAtom = 0;
static Scope *macros = 0;
#define MAX_MACRO_ARGS 64
static SourceLoc ifloc; /* outermost #if */
int InitCPP(void)
{
char buffer[64], *t;
const char *f;
// Add various atoms needed by the CPP line scanner:
bindAtom = LookUpAddString(atable, "bind");
constAtom = LookUpAddString(atable, "const");
defaultAtom = LookUpAddString(atable, "default");
defineAtom = LookUpAddString(atable, "define");
definedAtom = LookUpAddString(atable, "defined");
elifAtom = LookUpAddString(atable, "elif");
elseAtom = LookUpAddString(atable, "else");
endifAtom = LookUpAddString(atable, "endif");
ifAtom = LookUpAddString(atable, "if");
ifdefAtom = LookUpAddString(atable, "ifdef");
ifndefAtom = LookUpAddString(atable, "ifndef");
includeAtom = LookUpAddString(atable, "include");
lineAtom = LookUpAddString(atable, "line");
pragmaAtom = LookUpAddString(atable, "pragma");
texunitAtom = LookUpAddString(atable, "texunit");
undefAtom = LookUpAddString(atable, "undef");
errorAtom = LookUpAddString(atable, "error");
__LINE__Atom = LookUpAddString(atable, "__LINE__");
__FILE__Atom = LookUpAddString(atable, "__FILE__");
__VERSION__Atom = LookUpAddString(atable, "__VERSION__");
versionAtom = LookUpAddString(atable, "version");
extensionAtom = LookUpAddString(atable, "extension");
macros = NewScopeInPool(mem_CreatePool(0, 0));
strcpy(buffer, "PROFILE_");
t = buffer + strlen(buffer);
f = cpp->options.profileString;
while ((isalnum(*f) || *f == '_') && t < buffer + sizeof(buffer) - 1)
*t++ = toupper(*f++);
*t = 0;
PredefineIntMacro("GL_ES", 1);
PredefineIntMacro("GL_FRAGMENT_PRECISION_HIGH", 1);
return 1;
} // InitCPP
int FreeCPP(void)
{
if (macros)
{
mem_FreePool(macros->pool);
macros = 0;
}
return 1;
}
int FinalCPP(void)
{
if (cpp->ifdepth)
CPPErrorToInfoLog("#if mismatch");
return 1;
}
static int CPPdefine(yystypepp * yylvalpp)
{
int token, name, args[MAX_MACRO_ARGS], argc;
const char *message;
MacroSymbol mac;
Symbol *symb;
SourceLoc dummyLoc;
memset(&mac, 0, sizeof(mac));
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token != CPP_IDENTIFIER) {
CPPErrorToInfoLog("#define");
return token;
}
name = yylvalpp->sc_ident;
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token == '(' && !yylvalpp->sc_int) {
// gather arguments
argc = 0;
do {
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (argc == 0 && token == ')') break;
if (token != CPP_IDENTIFIER) {
CPPErrorToInfoLog("#define");
return token;
}
if (argc < MAX_MACRO_ARGS)
args[argc++] = yylvalpp->sc_ident;
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
} while (token == ',');
if (token != ')') {
CPPErrorToInfoLog("#define");
return token;
}
mac.argc = argc;
mac.args = mem_Alloc(macros->pool, argc * sizeof(int));
memcpy(mac.args, args, argc * sizeof(int));
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
}
mac.body = NewTokenStream(GetAtomString(atable, name), macros->pool);
while (token != '\n') {
if (token == '\\') {
CPPErrorToInfoLog("The line continuation character (\\) is not part of the OpenGL ES Shading Language");
return token;
} else if (token <= 0) { // EOF or error
CPPErrorToInfoLog("unexpected end of input in #define preprocessor directive - expected a newline");
return 0;
}
RecordToken(mac.body, token, yylvalpp);
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
};
symb = LookUpSymbol(macros, name);
if (symb) {
if (!symb->details.mac.undef) {
// already defined -- need to make sure they are identical
if (symb->details.mac.argc != mac.argc) goto error;
for (argc=0; argc < mac.argc; argc++)
if (symb->details.mac.args[argc] != mac.args[argc])
goto error;
RewindTokenStream(symb->details.mac.body);
RewindTokenStream(mac.body);
do {
int old_lval, old_token;
old_token = ReadToken(symb->details.mac.body, yylvalpp);
old_lval = yylvalpp->sc_int;
token = ReadToken(mac.body, yylvalpp);
if (token != old_token || yylvalpp->sc_int != old_lval) {
error:
StoreStr("Macro Redefined");
StoreStr(GetStringOfAtom(atable,name));
message=GetStrfromTStr();
DecLineNumber();
CPPShInfoLogMsg(message);
IncLineNumber();
ResetTString();
break; }
} while (token > 0);
}
//FreeMacro(&symb->details.mac);
} else {
dummyLoc.file = 0;
dummyLoc.line = 0;
symb = AddSymbol(&dummyLoc, macros, name, MACRO_S);
}
symb->details.mac = mac;
return '\n';
} // CPPdefine
static int CPPundef(yystypepp * yylvalpp)
{
int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
Symbol *symb;
if(token == '\n'){
CPPErrorToInfoLog("#undef");
return token;
}
if (token != CPP_IDENTIFIER)
goto error;
symb = LookUpSymbol(macros, yylvalpp->sc_ident);
if (symb) {
symb->details.mac.undef = 1;
}
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token != '\n') {
error:
CPPErrorToInfoLog("#undef");
}
return token;
} // CPPundef
/* CPPelse -- skip forward to appropriate spot. This is actually used
** to skip to and #endif after seeing an #else, AND to skip to a #else,
** #elif, or #endif after a #if/#ifdef/#ifndef/#elif test was false
*/
static int CPPelse(int matchelse, yystypepp * yylvalpp)
{
int atom,depth=0;
int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
while (token > 0) {
if (token != '#') {
while (token != '\n') {
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token <= 0) { // EOF or error
CPPErrorToInfoLog("unexpected end of input in #else preprocessor directive - expected a newline");
return 0;
}
}
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
continue;
}
if ((token = cpp->currentInput->scan(cpp->currentInput, yylvalpp)) != CPP_IDENTIFIER)
continue;
atom = yylvalpp->sc_ident;
if (atom == ifAtom || atom == ifdefAtom || atom == ifndefAtom){
depth++; cpp->ifdepth++; cpp->elsetracker++;
if (cpp->ifdepth > MAX_IF_NESTING) {
CPPErrorToInfoLog("max #if nesting depth exceeded");
cpp->CompileError = 1;
return 0;
}
// sanity check elsetracker
if (cpp->elsetracker < 0 || cpp->elsetracker >= MAX_IF_NESTING) {
CPPErrorToInfoLog("mismatched #if/#endif statements");
cpp->CompileError = 1;
return 0;
}
cpp->elsedepth[cpp->elsetracker] = 0;
}
else if (atom == endifAtom) {
if(--depth<0){
if (cpp->elsetracker)
--cpp->elsetracker;
if (cpp->ifdepth)
--cpp->ifdepth;
break;
}
--cpp->elsetracker;
--cpp->ifdepth;
}
else if (((int)(matchelse) != 0)&& depth==0) {
if (atom == elseAtom ) {
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token != '\n') {
CPPWarningToInfoLog("unexpected tokens following #else preprocessor directive - expected a newline");
while (token != '\n') {
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token <= 0) { // EOF or error
CPPErrorToInfoLog("unexpected end of input following #else preprocessor directive - expected a newline");
return 0;
}
}
}
break;
}
else if (atom == elifAtom) {
/* we decrement cpp->ifdepth here, because CPPif will increment
* it and we really want to leave it alone */
if (cpp->ifdepth){
--cpp->ifdepth;
--cpp->elsetracker;
}
return CPPif(yylvalpp);
}
}
else if((atom==elseAtom) && (!ChkCorrectElseNesting())){
CPPErrorToInfoLog("#else after a #else");
cpp->CompileError=1;
return 0;
}
};
return token;
}
enum eval_prec {
MIN_PREC,
COND, LOGOR, LOGAND, OR, XOR, AND, EQUAL, RELATION, SHIFT, ADD, MUL, UNARY,
MAX_PREC
};
static int op_logor(int a, int b) { return a || b; }
static int op_logand(int a, int b) { return a && b; }
static int op_or(int a, int b) { return a | b; }
static int op_xor(int a, int b) { return a ^ b; }
static int op_and(int a, int b) { return a & b; }
static int op_eq(int a, int b) { return a == b; }
static int op_ne(int a, int b) { return a != b; }
static int op_ge(int a, int b) { return a >= b; }
static int op_le(int a, int b) { return a <= b; }
static int op_gt(int a, int b) { return a > b; }
static int op_lt(int a, int b) { return a < b; }
static int op_shl(int a, int b) { return a << b; }
static int op_shr(int a, int b) { return a >> b; }
static int op_add(int a, int b) { return a + b; }
static int op_sub(int a, int b) { return a - b; }
static int op_mul(int a, int b) { return a * b; }
static int op_div(int a, int b) { return a / b; }
static int op_mod(int a, int b) { return a % b; }
static int op_pos(int a) { return a; }
static int op_neg(int a) { return -a; }
static int op_cmpl(int a) { return ~a; }
static int op_not(int a) { return !a; }
struct {
int token, prec, (*op)(int, int);
} binop[] = {
{ CPP_OR_OP, LOGOR, op_logor },
{ CPP_AND_OP, LOGAND, op_logand },
{ '|', OR, op_or },
{ '^', XOR, op_xor },
{ '&', AND, op_and },
{ CPP_EQ_OP, EQUAL, op_eq },
{ CPP_NE_OP, EQUAL, op_ne },
{ '>', RELATION, op_gt },
{ CPP_GE_OP, RELATION, op_ge },
{ '<', RELATION, op_lt },
{ CPP_LE_OP, RELATION, op_le },
{ CPP_LEFT_OP, SHIFT, op_shl },
{ CPP_RIGHT_OP, SHIFT, op_shr },
{ '+', ADD, op_add },
{ '-', ADD, op_sub },
{ '*', MUL, op_mul },
{ '/', MUL, op_div },
{ '%', MUL, op_mod },
};
struct {
int token, (*op)(int);
} unop[] = {
{ '+', op_pos },
{ '-', op_neg },
{ '~', op_cmpl },
{ '!', op_not },
};
#define ALEN(A) (sizeof(A)/sizeof(A[0]))
static int eval(int token, int prec, int *res, int *err, yystypepp * yylvalpp)
{
int i, val;
Symbol *s;
if (token == CPP_IDENTIFIER) {
if (yylvalpp->sc_ident == definedAtom) {
int needclose = 0;
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token == '(') {
needclose = 1;
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
}
if (token != CPP_IDENTIFIER)
goto error;
*res = (s = LookUpSymbol(macros, yylvalpp->sc_ident))
? !s->details.mac.undef : 0;
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (needclose) {
if (token != ')')
goto error;
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
}
} else if (MacroExpand(yylvalpp->sc_ident, yylvalpp)) {
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
return eval(token, prec, res, err, yylvalpp);
} else {
goto error;
}
} else if (token == CPP_INTCONSTANT) {
*res = yylvalpp->sc_int;
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
} else if (token == '(') {
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
token = eval(token, MIN_PREC, res, err, yylvalpp);
if (!*err) {
if (token != ')')
goto error;
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
}
} else {
for (i = ALEN(unop) - 1; i >= 0; i--) {
if (unop[i].token == token)
break;
}
if (i >= 0) {
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
token = eval(token, UNARY, res, err, yylvalpp);
*res = unop[i].op(*res);
} else {
goto error;
}
}
while (!*err) {
if (token == ')' || token == '\n') break;
for (i = ALEN(binop) - 1; i >= 0; i--) {
if (binop[i].token == token)
break;
}
if (i < 0 || binop[i].prec <= prec)
break;
val = *res;
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
token = eval(token, binop[i].prec, res, err, yylvalpp);
if (binop[i].op == op_div || binop[i].op == op_mod)
{
if (*res == 0)
{
CPPErrorToInfoLog("preprocessor divide or modulo by zero");
*err = 1;
return token;
}
}
*res = binop[i].op(val, *res);
}
return token;
error:
CPPErrorToInfoLog("incorrect preprocessor directive");
*err = 1;
*res = 0;
return token;
} // eval
static int CPPif(yystypepp * yylvalpp) {
int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
int res = 0, err = 0;
if (!cpp->ifdepth++)
ifloc = *cpp->tokenLoc;
if(cpp->ifdepth > MAX_IF_NESTING){
CPPErrorToInfoLog("max #if nesting depth exceeded");
cpp->CompileError = 1;
return 0;
}
cpp->elsetracker++;
// sanity check elsetracker
if (cpp->elsetracker < 0 || cpp->elsetracker >= MAX_IF_NESTING) {
CPPErrorToInfoLog("mismatched #if/#endif statements");
cpp->CompileError = 1;
return 0;
}
cpp->elsedepth[cpp->elsetracker] = 0;
token = eval(token, MIN_PREC, &res, &err, yylvalpp);
if (token != '\n') {
CPPWarningToInfoLog("unexpected tokens following #if preprocessor directive - expected a newline");
while (token != '\n') {
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token <= 0) { // EOF or error
CPPErrorToInfoLog("unexpected end of input in #if preprocessor directive - expected a newline");
return 0;
}
}
}
if (!res && !err) {
token = CPPelse(1, yylvalpp);
}
return token;
} // CPPif
static int CPPifdef(int defined, yystypepp * yylvalpp)
{
int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
int name = yylvalpp->sc_ident;
if(++cpp->ifdepth > MAX_IF_NESTING){
CPPErrorToInfoLog("max #if nesting depth exceeded");
cpp->CompileError = 1;
return 0;
}
cpp->elsetracker++;
// sanity check elsetracker
if (cpp->elsetracker < 0 || cpp->elsetracker >= MAX_IF_NESTING) {
CPPErrorToInfoLog("mismatched #if/#endif statements");
cpp->CompileError = 1;
return 0;
}
cpp->elsedepth[cpp->elsetracker] = 0;
if (token != CPP_IDENTIFIER) {
defined ? CPPErrorToInfoLog("ifdef"):CPPErrorToInfoLog("ifndef");
} else {
Symbol *s = LookUpSymbol(macros, name);
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token != '\n') {
CPPWarningToInfoLog("unexpected tokens following #ifdef preprocessor directive - expected a newline");
while (token != '\n') {
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token <= 0) { // EOF or error
CPPErrorToInfoLog("unexpected end of input in #ifdef preprocessor directive - expected a newline");
return 0;
}
}
}
if (((s && !s->details.mac.undef) ? 1 : 0) != defined)
token = CPPelse(1, yylvalpp);
}
return token;
} // CPPifdef
static int CPPline(yystypepp * yylvalpp)
{
int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if(token=='\n'){
DecLineNumber();
CPPErrorToInfoLog("#line");
IncLineNumber();
return token;
}
else if (token == CPP_INTCONSTANT) {
yylvalpp->sc_int=atoi(yylvalpp->symbol_name);
SetLineNumber(yylvalpp->sc_int);
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token == CPP_INTCONSTANT) {
yylvalpp->sc_int=atoi(yylvalpp->symbol_name);
SetStringNumber(yylvalpp->sc_int);
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if(token!='\n')
CPPErrorToInfoLog("#line");
}
else if (token == '\n'){
return token;
}
else{
CPPErrorToInfoLog("#line");
}
}
else{
CPPErrorToInfoLog("#line");
}
return token;
}
static int CPPerror(yystypepp * yylvalpp) {
int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
const char *message;
while (token != '\n') {
if (token <= 0){
CPPErrorToInfoLog("unexpected end of input in #error preprocessor directive - expected a newline");
return 0;
}else if (token == CPP_FLOATCONSTANT || token == CPP_INTCONSTANT){
StoreStr(yylvalpp->symbol_name);
}else if(token == CPP_IDENTIFIER || token == CPP_STRCONSTANT){
StoreStr(GetStringOfAtom(atable,yylvalpp->sc_ident));
}else {
StoreStr(GetStringOfAtom(atable,token));
}
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
}
DecLineNumber();
//store this msg into the shader's information log..set the Compile Error flag!!!!
message=GetStrfromTStr();
CPPShInfoLogMsg(message);
ResetTString();
cpp->CompileError=1;
IncLineNumber();
return '\n';
}//CPPerror
static int CPPpragma(yystypepp * yylvalpp)
{
char SrcStrName[2];
char** allTokens;
int tokenCount = 0;
int maxTokenCount = 10;
const char* SrcStr;
int i;
int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token=='\n') {
DecLineNumber();
CPPErrorToInfoLog("#pragma");
IncLineNumber();
return token;
}
allTokens = (char**)malloc(sizeof(char*) * maxTokenCount);
while (token != '\n') {
if (tokenCount >= maxTokenCount) {
maxTokenCount *= 2;
allTokens = (char**)realloc((char**)allTokens, sizeof(char*) * maxTokenCount);
}
switch (token) {
case CPP_IDENTIFIER:
SrcStr = GetAtomString(atable, yylvalpp->sc_ident);
allTokens[tokenCount] = (char*)malloc(strlen(SrcStr) + 1);
strcpy(allTokens[tokenCount++], SrcStr);
break;
case CPP_INTCONSTANT:
SrcStr = yylvalpp->symbol_name;
allTokens[tokenCount] = (char*)malloc(strlen(SrcStr) + 1);
strcpy(allTokens[tokenCount++], SrcStr);
break;
case CPP_FLOATCONSTANT:
SrcStr = yylvalpp->symbol_name;
allTokens[tokenCount] = (char*)malloc(strlen(SrcStr) + 1);
strcpy(allTokens[tokenCount++], SrcStr);
break;
case -1:
// EOF
CPPShInfoLogMsg("#pragma directive must end with a newline");
return token;
default:
SrcStrName[0] = token;
SrcStrName[1] = '\0';
allTokens[tokenCount] = (char*)malloc(2);
strcpy(allTokens[tokenCount++], SrcStrName);
}
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
}
cpp->currentInput->ungetch(cpp->currentInput, token, yylvalpp);
HandlePragma((const char**)allTokens, tokenCount);
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
for (i = 0; i < tokenCount; ++i) {
free (allTokens[i]);
}
free (allTokens);
return token;
} // CPPpragma
#define ESSL_VERSION_NUMBER 100
#define ESSL_VERSION_STRING "100"
static int CPPversion(yystypepp * yylvalpp)
{
int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (cpp->pastFirstStatement == 1)
CPPShInfoLogMsg("#version must occur before any other statement in the program");
if(token=='\n'){
DecLineNumber();
CPPErrorToInfoLog("#version");
IncLineNumber();
return token;
}
if (token != CPP_INTCONSTANT)
CPPErrorToInfoLog("#version");
yylvalpp->sc_int=atoi(yylvalpp->symbol_name);
//SetVersionNumber(yylvalpp->sc_int);
if (yylvalpp->sc_int != ESSL_VERSION_NUMBER)
CPPShInfoLogMsg("Version number not supported by ESSL");
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token == '\n'){
return token;
}
else{
CPPErrorToInfoLog("#version");
}
return token;
} // CPPversion
static int CPPextension(yystypepp * yylvalpp)
{
int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
char extensionName[MAX_SYMBOL_NAME_LEN + 1];
if(token=='\n'){
DecLineNumber();
CPPShInfoLogMsg("extension name not specified");
IncLineNumber();
return token;
}
if (token != CPP_IDENTIFIER)
CPPErrorToInfoLog("#extension");
strncpy(extensionName, GetAtomString(atable, yylvalpp->sc_ident), MAX_SYMBOL_NAME_LEN);
extensionName[MAX_SYMBOL_NAME_LEN] = '\0';
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token != ':') {
CPPShInfoLogMsg("':' missing after extension name");
return token;
}
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token != CPP_IDENTIFIER) {
CPPShInfoLogMsg("behavior for extension not specified");
return token;
}
updateExtensionBehavior(extensionName, GetAtomString(atable, yylvalpp->sc_ident));
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token == '\n'){
return token;
}
else{
CPPErrorToInfoLog("#extension");
}
return token;
} // CPPextension
int readCPPline(yystypepp * yylvalpp)
{
int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
const char *message;
int isVersion = 0;
if (token == CPP_IDENTIFIER) {
if (yylvalpp->sc_ident == defineAtom) {
token = CPPdefine(yylvalpp);
} else if (yylvalpp->sc_ident == elseAtom) {
if(ChkCorrectElseNesting()){
if (!cpp->ifdepth ){
CPPErrorToInfoLog("#else mismatch");
cpp->CompileError=1;
return 0;
}
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token != '\n') {
CPPWarningToInfoLog("unexpected tokens following #else preprocessor directive - expected a newline");
while (token != '\n') {
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token <= 0) { // EOF or error
CPPErrorToInfoLog("unexpected end of input in #ifdef preprocessor directive - expected a newline");
return 0;
}
}
}
token = CPPelse(0, yylvalpp);
}else{
CPPErrorToInfoLog("#else after a #else");
cpp->ifdepth = 0;
cpp->elsetracker = 0;
cpp->pastFirstStatement = 1;
cpp->CompileError = 1;
return 0;
}
} else if (yylvalpp->sc_ident == elifAtom) {
if (!cpp->ifdepth){
CPPErrorToInfoLog("#elif mismatch");
cpp->CompileError=1;
return 0;
}
// this token is really a dont care, but we still need to eat the tokens
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
while (token != '\n') {
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token <= 0) { // EOF or error
CPPErrorToInfoLog("unexpect tokens following #elif preprocessor directive - expected a newline");
cpp->CompileError = 1;
return 0;
}
}
token = CPPelse(0, yylvalpp);
} else if (yylvalpp->sc_ident == endifAtom) {
if (!cpp->ifdepth){
CPPErrorToInfoLog("#endif mismatch");
cpp->CompileError=1;
return 0;
}
else
--cpp->ifdepth;
if (cpp->elsetracker)
--cpp->elsetracker;
} else if (yylvalpp->sc_ident == ifAtom) {
token = CPPif(yylvalpp);
} else if (yylvalpp->sc_ident == ifdefAtom) {
token = CPPifdef(1, yylvalpp);
} else if (yylvalpp->sc_ident == ifndefAtom) {
token = CPPifdef(0, yylvalpp);
} else if (yylvalpp->sc_ident == lineAtom) {
token = CPPline(yylvalpp);
} else if (yylvalpp->sc_ident == pragmaAtom) {
token = CPPpragma(yylvalpp);
} else if (yylvalpp->sc_ident == undefAtom) {
token = CPPundef(yylvalpp);
} else if (yylvalpp->sc_ident == errorAtom) {
token = CPPerror(yylvalpp);
} else if (yylvalpp->sc_ident == versionAtom) {
token = CPPversion(yylvalpp);
isVersion = 1;
} else if (yylvalpp->sc_ident == extensionAtom) {
token = CPPextension(yylvalpp);
} else {
StoreStr("Invalid Directive");
StoreStr(GetStringOfAtom(atable,yylvalpp->sc_ident));
message=GetStrfromTStr();
CPPShInfoLogMsg(message);
ResetTString();
}
}
while (token != '\n' && token != 0 && token != EOF) {
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
}
cpp->pastFirstStatement = 1;
return token;
} // readCPPline
void FreeMacro(MacroSymbol *s) {
DeleteTokenStream(s->body);
}
void PredefineIntMacro(const char *name, int value) {
SourceLoc location = {0};
Symbol *symbol = NULL;
MacroSymbol macro = {0};
yystypepp val = {0};
int atom = 0;
macro.body = NewTokenStream(name, macros->pool);
val.sc_int = value;
sprintf(val.symbol_name, "%d", value);
RecordToken(macro.body, CPP_INTCONSTANT, &val);
atom = LookUpAddString(atable, name);
symbol = AddSymbol(&location, macros, atom, MACRO_S);
symbol->details.mac = macro;
}
static int eof_scan(InputSrc *in, yystypepp * yylvalpp) { return -1; }
static void noop(InputSrc *in, int ch, yystypepp * yylvalpp) { }
static void PushEofSrc() {
InputSrc *in = malloc(sizeof(InputSrc));
memset(in, 0, sizeof(InputSrc));
in->scan = eof_scan;
in->getch = eof_scan;
in->ungetch = noop;
in->prev = cpp->currentInput;
cpp->currentInput = in;
}
static void PopEofSrc() {
if (cpp->currentInput->scan == eof_scan) {
InputSrc *in = cpp->currentInput;
cpp->currentInput = in->prev;
free(in);
}
}
static TokenStream *PrescanMacroArg(TokenStream *a, yystypepp * yylvalpp) {
int token;
TokenStream *n;
RewindTokenStream(a);
do {
token = ReadToken(a, yylvalpp);
if (token == CPP_IDENTIFIER && LookUpSymbol(macros, yylvalpp->sc_ident))
break;
} while (token > 0);
if (token <= 0) return a;
n = NewTokenStream("macro arg", 0);
PushEofSrc();
ReadFromTokenStream(a, 0, 0);
while ((token = cpp->currentInput->scan(cpp->currentInput, yylvalpp)) > 0) {
if (token == CPP_IDENTIFIER && MacroExpand(yylvalpp->sc_ident, yylvalpp))
continue;
RecordToken(n, token, yylvalpp);
}
PopEofSrc();
DeleteTokenStream(a);
return n;
} // PrescanMacroArg
typedef struct MacroInputSrc {
InputSrc base;
MacroSymbol *mac;
TokenStream **args;
} MacroInputSrc;
/* macro_scan ---
** return the next token for a macro expanion, handling macro args
*/
static int macro_scan(MacroInputSrc *in, yystypepp * yylvalpp) {
int i;
int token = ReadToken(in->mac->body, yylvalpp);
if (token == CPP_IDENTIFIER) {
for (i = in->mac->argc-1; i>=0; i--)
if (in->mac->args[i] == yylvalpp->sc_ident) break;
if (i >= 0) {
ReadFromTokenStream(in->args[i], yylvalpp->sc_ident, 0);
return cpp->currentInput->scan(cpp->currentInput, yylvalpp);
}
}
if (token > 0) return token;
in->mac->busy = 0;
cpp->currentInput = in->base.prev;
if (in->args) {
for (i=in->mac->argc-1; i>=0; i--)
DeleteTokenStream(in->args[i]);
free(in->args);
}
free(in);
return cpp->currentInput->scan(cpp->currentInput, yylvalpp);
} // macro_scan
/* MacroExpand
** check an identifier (atom) to see if it a macro that should be expanded.
** If it is, push an InputSrc that will produce the appropriate expansion
** and return TRUE. If not, return FALSE.
*/
int MacroExpand(int atom, yystypepp * yylvalpp)
{
Symbol *sym = LookUpSymbol(macros, atom);
MacroInputSrc *in;
int i,j, token, depth=0;
const char *message;
if (atom == __LINE__Atom) {
yylvalpp->sc_int = GetLineNumber();
sprintf(yylvalpp->symbol_name,"%d",yylvalpp->sc_int);
UngetToken(CPP_INTCONSTANT, yylvalpp);
return 1;
}
if (atom == __FILE__Atom) {
yylvalpp->sc_int = GetStringNumber();
sprintf(yylvalpp->symbol_name,"%d",yylvalpp->sc_int);
UngetToken(CPP_INTCONSTANT, yylvalpp);
return 1;
}
if (atom == __VERSION__Atom) {
strcpy(yylvalpp->symbol_name,ESSL_VERSION_STRING);
yylvalpp->sc_int = atoi(yylvalpp->symbol_name);
UngetToken(CPP_INTCONSTANT, yylvalpp);
return 1;
}
if (!sym || sym->details.mac.undef) return 0;
if (sym->details.mac.busy) return 0; // no recursive expansions
in = malloc(sizeof(*in));
memset(in, 0, sizeof(*in));
in->base.scan = (void *)macro_scan;
in->base.line = cpp->currentInput->line;
in->base.name = cpp->currentInput->name;
in->mac = &sym->details.mac;
if (sym->details.mac.args) {
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token != '(') {
UngetToken(token, yylvalpp);
yylvalpp->sc_ident = atom;
return 0;
}
in->args = malloc(in->mac->argc * sizeof(TokenStream *));
for (i=0; i<in->mac->argc; i++)
in->args[i] = NewTokenStream("macro arg", 0);
i=0;j=0;
do{
depth = 0;
while(1) {
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token <= 0) {
StoreStr("EOF in Macro ");
StoreStr(GetStringOfAtom(atable,atom));
message=GetStrfromTStr();
CPPShInfoLogMsg(message);
ResetTString();
return 1;
}
if((in->mac->argc==0) && (token!=')')) break;
if (depth == 0 && (token == ',' || token == ')')) break;
if (token == '(') depth++;
if (token == ')') depth--;
RecordToken(in->args[i], token, yylvalpp);
j=1;
}
if (token == ')') {
if((in->mac->argc==1) &&j==0)
break;
i++;
break;
}
i++;
}while(i < in->mac->argc);
if (i < in->mac->argc) {
StoreStr("Too few args in Macro ");
StoreStr(GetStringOfAtom(atable,atom));
message=GetStrfromTStr();
CPPShInfoLogMsg(message);
ResetTString();
} else if (token != ')') {
depth=0;
while (token >= 0 && (depth > 0 || token != ')')) {
if (token == ')') depth--;
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token == '(') depth++;
}
if (token <= 0) {
StoreStr("EOF in Macro ");
StoreStr(GetStringOfAtom(atable,atom));
message=GetStrfromTStr();
CPPShInfoLogMsg(message);
ResetTString();
return 1;
}
StoreStr("Too many args in Macro ");
StoreStr(GetStringOfAtom(atable,atom));
message=GetStrfromTStr();
CPPShInfoLogMsg(message);
ResetTString();
}
for (i=0; i<in->mac->argc; i++) {
in->args[i] = PrescanMacroArg(in->args[i], yylvalpp);
}
}
#if 0
printf(" <%s:%d>found macro %s\n", GetAtomString(atable, loc.file),
loc.line, GetAtomString(atable, atom));
for (i=0; i<in->mac->argc; i++) {
printf("\targ %s = '", GetAtomString(atable, in->mac->args[i]));
DumpTokenStream(stdout, in->args[i]);
printf("'\n");
}
#endif
/*retain the input source*/
in->base.prev = cpp->currentInput;
sym->details.mac.busy = 1;
RewindTokenStream(sym->details.mac.body);
cpp->currentInput = &in->base;
return 1;
} // MacroExpand
int ChkCorrectElseNesting(void)
{
// sanity check to make sure elsetracker is in a valid range
if (cpp->elsetracker < 0 || cpp->elsetracker >= MAX_IF_NESTING) {
return 0;
}
if (cpp->elsedepth[cpp->elsetracker] == 0) {
cpp->elsedepth[cpp->elsetracker] = 1;
return 1;
}
return 0;
}