eval.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "cpp.h"
  4. #define NSTAK 32
  5. #define SGN 0
  6. #define UNS 1
  7. #define UND 2
  8. #define UNSMARK 0x1000
  9. struct value {
  10. long val;
  11. int type;
  12. };
  13. /* conversion types */
  14. #define RELAT 1
  15. #define ARITH 2
  16. #define LOGIC 3
  17. #define SPCL 4
  18. #define SHIFT 5
  19. #define UNARY 6
  20. /* operator priority, arity, and conversion type, indexed by tokentype */
  21. struct pri {
  22. char pri;
  23. char arity;
  24. char ctype;
  25. } priority[] = {
  26. { 0, 0, 0 }, /* END */
  27. { 0, 0, 0 }, /* UNCLASS */
  28. { 0, 0, 0 }, /* NAME */
  29. { 0, 0, 0 }, /* NUMBER */
  30. { 0, 0, 0 }, /* STRING */
  31. { 0, 0, 0 }, /* CCON */
  32. { 0, 0, 0 }, /* NL */
  33. { 0, 0, 0 }, /* WS */
  34. { 0, 0, 0 }, /* DSHARP */
  35. { 11, 2, RELAT }, /* EQ */
  36. { 11, 2, RELAT }, /* NEQ */
  37. { 12, 2, RELAT }, /* LEQ */
  38. { 12, 2, RELAT }, /* GEQ */
  39. { 13, 2, SHIFT }, /* LSH */
  40. { 13, 2, SHIFT }, /* RSH */
  41. { 7, 2, LOGIC }, /* LAND */
  42. { 6, 2, LOGIC }, /* LOR */
  43. { 0, 0, 0 }, /* PPLUS */
  44. { 0, 0, 0 }, /* MMINUS */
  45. { 0, 0, 0 }, /* ARROW */
  46. { 0, 0, 0 }, /* SBRA */
  47. { 0, 0, 0 }, /* SKET */
  48. { 3, 0, 0 }, /* LP */
  49. { 3, 0, 0 }, /* RP */
  50. { 0, 0, 0 }, /* DOT */
  51. { 10, 2, ARITH }, /* AND */
  52. { 15, 2, ARITH }, /* STAR */
  53. { 14, 2, ARITH }, /* PLUS */
  54. { 14, 2, ARITH }, /* MINUS */
  55. { 16, 1, UNARY }, /* TILDE */
  56. { 16, 1, UNARY }, /* NOT */
  57. { 15, 2, ARITH }, /* SLASH */
  58. { 15, 2, ARITH }, /* PCT */
  59. { 12, 2, RELAT }, /* LT */
  60. { 12, 2, RELAT }, /* GT */
  61. { 9, 2, ARITH }, /* CIRC */
  62. { 8, 2, ARITH }, /* OR */
  63. { 5, 2, SPCL }, /* QUEST */
  64. { 5, 2, SPCL }, /* COLON */
  65. { 0, 0, 0 }, /* ASGN */
  66. { 4, 2, 0 }, /* COMMA */
  67. { 0, 0, 0 }, /* SHARP */
  68. { 0, 0, 0 }, /* SEMIC */
  69. { 0, 0, 0 }, /* CBRA */
  70. { 0, 0, 0 }, /* CKET */
  71. { 0, 0, 0 }, /* ASPLUS */
  72. { 0, 0, 0 }, /* ASMINUS */
  73. { 0, 0, 0 }, /* ASSTAR */
  74. { 0, 0, 0 }, /* ASSLASH */
  75. { 0, 0, 0 }, /* ASPCT */
  76. { 0, 0, 0 }, /* ASCIRC */
  77. { 0, 0, 0 }, /* ASLSH */
  78. { 0, 0, 0 }, /* ASRSH */
  79. { 0, 0, 0 }, /* ASOR */
  80. { 0, 0, 0 }, /* ASAND */
  81. { 0, 0, 0 }, /* ELLIPS */
  82. { 0, 0, 0 }, /* DSHARP1 */
  83. { 0, 0, 0 }, /* NAME1 */
  84. { 16, 1, UNARY }, /* DEFINED */
  85. { 16, 0, UNARY }, /* UMINUS */
  86. };
  87. int evalop(struct pri);
  88. struct value tokval(Token *);
  89. struct value vals[NSTAK], *vp;
  90. enum toktype ops[NSTAK], *op;
  91. /*
  92. * Evaluate an #if #elif #ifdef #ifndef line. trp->tp points to the keyword.
  93. */
  94. long
  95. eval(Tokenrow *trp, int kw)
  96. {
  97. Token *tp;
  98. Nlist *np;
  99. int ntok, rand;
  100. trp->tp++;
  101. if (kw==KIFDEF || kw==KIFNDEF) {
  102. if (trp->lp - trp->bp != 4 || trp->tp->type!=NAME) {
  103. error(ERROR, "Syntax error in #ifdef/#ifndef");
  104. return 0;
  105. }
  106. np = lookup(trp->tp, 0);
  107. return (kw==KIFDEF) == (np && np->flag&(ISDEFINED|ISMAC));
  108. }
  109. ntok = trp->tp - trp->bp;
  110. kwdefined->val = KDEFINED; /* activate special meaning of defined */
  111. expandrow(trp, "<if>");
  112. kwdefined->val = NAME;
  113. vp = vals;
  114. op = ops;
  115. *op++ = END;
  116. for (rand=0, tp = trp->bp+ntok; tp < trp->lp; tp++) {
  117. switch(tp->type) {
  118. case WS:
  119. case NL:
  120. continue;
  121. /* nilary */
  122. case NAME:
  123. case NAME1:
  124. case NUMBER:
  125. case CCON:
  126. case STRING:
  127. if (rand)
  128. goto syntax;
  129. *vp++ = tokval(tp);
  130. rand = 1;
  131. continue;
  132. /* unary */
  133. case DEFINED:
  134. case TILDE:
  135. case NOT:
  136. if (rand)
  137. goto syntax;
  138. *op++ = tp->type;
  139. continue;
  140. /* unary-binary */
  141. case PLUS: case MINUS: case STAR: case AND:
  142. if (rand==0) {
  143. if (tp->type==MINUS)
  144. *op++ = UMINUS;
  145. if (tp->type==STAR || tp->type==AND) {
  146. error(ERROR, "Illegal operator * or & in #if/#elsif");
  147. return 0;
  148. }
  149. continue;
  150. }
  151. /* flow through */
  152. /* plain binary */
  153. case EQ: case NEQ: case LEQ: case GEQ: case LSH: case RSH:
  154. case LAND: case LOR: case SLASH: case PCT:
  155. case LT: case GT: case CIRC: case OR: case QUEST:
  156. case COLON: case COMMA:
  157. if (rand==0)
  158. goto syntax;
  159. if (evalop(priority[tp->type])!=0)
  160. return 0;
  161. *op++ = tp->type;
  162. rand = 0;
  163. continue;
  164. case LP:
  165. if (rand)
  166. goto syntax;
  167. *op++ = LP;
  168. continue;
  169. case RP:
  170. if (!rand)
  171. goto syntax;
  172. if (evalop(priority[RP])!=0)
  173. return 0;
  174. if (op<=ops || op[-1]!=LP) {
  175. goto syntax;
  176. }
  177. op--;
  178. continue;
  179. default:
  180. error(ERROR,"Bad operator (%t) in #if/#elsif", tp);
  181. return 0;
  182. }
  183. }
  184. if (rand==0)
  185. goto syntax;
  186. if (evalop(priority[END])!=0)
  187. return 0;
  188. if (op!=&ops[1] || vp!=&vals[1]) {
  189. error(ERROR, "Botch in #if/#elsif");
  190. return 0;
  191. }
  192. if (vals[0].type==UND)
  193. error(ERROR, "Undefined expression value");
  194. return vals[0].val;
  195. syntax:
  196. error(ERROR, "Syntax error in #if/#elsif");
  197. return 0;
  198. }
  199. int
  200. evalop(struct pri pri)
  201. {
  202. struct value v1, v2;
  203. long rv1, rv2;
  204. int rtype, oper;
  205. rv2=0;
  206. rtype=0;
  207. while (pri.pri < priority[op[-1]].pri) {
  208. oper = *--op;
  209. if (priority[oper].arity==2) {
  210. v2 = *--vp;
  211. rv2 = v2.val;
  212. }
  213. v1 = *--vp;
  214. rv1 = v1.val;
  215. /*lint -e574 -e644 */
  216. switch (priority[oper].ctype) {
  217. case 0:
  218. default:
  219. error(WARNING, "Syntax error in #if/#endif");
  220. return 1;
  221. case ARITH:
  222. case RELAT:
  223. if (v1.type==UNS || v2.type==UNS)
  224. rtype = UNS;
  225. else
  226. rtype = SGN;
  227. if (v1.type==UND || v2.type==UND)
  228. rtype = UND;
  229. if (priority[oper].ctype==RELAT && rtype==UNS) {
  230. oper |= UNSMARK;
  231. rtype = SGN;
  232. }
  233. break;
  234. case SHIFT:
  235. if (v1.type==UND || v2.type==UND)
  236. rtype = UND;
  237. else
  238. rtype = v1.type;
  239. if (rtype==UNS)
  240. oper |= UNSMARK;
  241. break;
  242. case UNARY:
  243. rtype = v1.type;
  244. break;
  245. case LOGIC:
  246. case SPCL:
  247. break;
  248. }
  249. switch (oper) {
  250. case EQ: case EQ|UNSMARK:
  251. rv1 = rv1==rv2; break;
  252. case NEQ: case NEQ|UNSMARK:
  253. rv1 = rv1!=rv2; break;
  254. case LEQ:
  255. rv1 = rv1<=rv2; break;
  256. case GEQ:
  257. rv1 = rv1>=rv2; break;
  258. case LT:
  259. rv1 = rv1<rv2; break;
  260. case GT:
  261. rv1 = rv1>rv2; break;
  262. case LEQ|UNSMARK:
  263. rv1 = (unsigned long)rv1<=rv2; break;
  264. case GEQ|UNSMARK:
  265. rv1 = (unsigned long)rv1>=rv2; break;
  266. case LT|UNSMARK:
  267. rv1 = (unsigned long)rv1<rv2; break;
  268. case GT|UNSMARK:
  269. rv1 = (unsigned long)rv1>rv2; break;
  270. case LSH:
  271. rv1 <<= rv2; break;
  272. case LSH|UNSMARK:
  273. rv1 = (unsigned long)rv1<<rv2; break;
  274. case RSH:
  275. rv1 >>= rv2; break;
  276. case RSH|UNSMARK:
  277. rv1 = (unsigned long)rv1>>rv2; break;
  278. case LAND:
  279. rtype = UND;
  280. if (v1.type==UND)
  281. break;
  282. if (rv1!=0) {
  283. if (v2.type==UND)
  284. break;
  285. rv1 = rv2!=0;
  286. } else
  287. rv1 = 0;
  288. rtype = SGN;
  289. break;
  290. case LOR:
  291. rtype = UND;
  292. if (v1.type==UND)
  293. break;
  294. if (rv1==0) {
  295. if (v2.type==UND)
  296. break;
  297. rv1 = rv2!=0;
  298. } else
  299. rv1 = 1;
  300. rtype = SGN;
  301. break;
  302. case AND:
  303. rv1 &= rv2; break;
  304. case STAR:
  305. rv1 *= rv2; break;
  306. case PLUS:
  307. rv1 += rv2; break;
  308. case MINUS:
  309. rv1 -= rv2; break;
  310. case UMINUS:
  311. if (v1.type==UND)
  312. rtype = UND;
  313. rv1 = -rv1; break;
  314. case OR:
  315. rv1 |= rv2; break;
  316. case CIRC:
  317. rv1 ^= rv2; break;
  318. case TILDE:
  319. rv1 = ~rv1; break;
  320. case NOT:
  321. rv1 = !rv1; if (rtype!=UND) rtype = SGN; break;
  322. case SLASH:
  323. if (rv2==0) {
  324. rtype = UND;
  325. break;
  326. }
  327. if (rtype==UNS)
  328. rv1 /= (unsigned long)rv2;
  329. else
  330. rv1 /= rv2;
  331. break;
  332. case PCT:
  333. if (rv2==0) {
  334. rtype = UND;
  335. break;
  336. }
  337. if (rtype==UNS)
  338. rv1 %= (unsigned long)rv2;
  339. else
  340. rv1 %= rv2;
  341. break;
  342. case COLON:
  343. if (op[-1] != QUEST)
  344. error(ERROR, "Bad ?: in #if/endif");
  345. else {
  346. op--;
  347. if ((--vp)->val==0)
  348. v1 = v2;
  349. rtype = v1.type;
  350. rv1 = v1.val;
  351. }
  352. break;
  353. case DEFINED:
  354. break;
  355. default:
  356. error(ERROR, "Eval botch (unknown operator)");
  357. return 1;
  358. }
  359. /*lint +e574 +e644 */
  360. v1.val = rv1;
  361. v1.type = rtype;
  362. *vp++ = v1;
  363. }
  364. return 0;
  365. }
  366. struct value
  367. tokval(Token *tp)
  368. {
  369. struct value v;
  370. Nlist *np;
  371. int i, base, c;
  372. unsigned long n;
  373. uchar *p;
  374. v.type = SGN;
  375. v.val = 0;
  376. switch (tp->type) {
  377. case NAME:
  378. v.val = 0;
  379. break;
  380. case NAME1:
  381. if ((np = lookup(tp, 0)) != NULL && np->flag&(ISDEFINED|ISMAC))
  382. v.val = 1;
  383. break;
  384. case NUMBER:
  385. n = 0;
  386. base = 10;
  387. p = tp->t;
  388. c = p[tp->len];
  389. p[tp->len] = '\0';
  390. if (*p=='0') {
  391. base = 8;
  392. if (p[1]=='x' || p[1]=='X') {
  393. base = 16;
  394. p++;
  395. }
  396. p++;
  397. }
  398. for (;; p++) {
  399. if ((i = digit(*p)) < 0)
  400. break;
  401. if (i>=base)
  402. error(WARNING,
  403. "Bad digit in number %t", tp);
  404. n *= base;
  405. n += i;
  406. }
  407. if (n>=0x80000000 && base!=10)
  408. v.type = UNS;
  409. for (; *p; p++) {
  410. if (*p=='u' || *p=='U')
  411. v.type = UNS;
  412. else if (*p=='l' || *p=='L')
  413. ;
  414. else {
  415. error(ERROR,
  416. "Bad number %t in #if/#elsif", tp);
  417. break;
  418. }
  419. }
  420. v.val = n;
  421. tp->t[tp->len] = c;
  422. break;
  423. case CCON:
  424. n = 0;
  425. p = tp->t;
  426. if (*p=='L') {
  427. p += 1;
  428. error(WARNING, "Wide char constant value undefined");
  429. }
  430. p += 1;
  431. if (*p=='\\') {
  432. p += 1;
  433. if ((i = digit(*p))>=0 && i<=7) {
  434. n = i;
  435. p += 1;
  436. if ((i = digit(*p))>=0 && i<=7) {
  437. p += 1;
  438. n <<= 3;
  439. n += i;
  440. if ((i = digit(*p))>=0 && i<=7) {
  441. p += 1;
  442. n <<= 3;
  443. n += i;
  444. }
  445. }
  446. } else if (*p=='x') {
  447. p += 1;
  448. while ((i = digit(*p))>=0 && i<=15) {
  449. p += 1;
  450. n <<= 4;
  451. n += i;
  452. }
  453. } else {
  454. static char cvcon[]
  455. = "b\bf\fn\nr\rt\tv\v''\"\"??\\\\";
  456. for (i=0; i<sizeof(cvcon); i+=2) {
  457. if (*p == cvcon[i]) {
  458. n = cvcon[i+1];
  459. break;
  460. }
  461. }
  462. p += 1;
  463. if (i>=sizeof(cvcon))
  464. error(WARNING,
  465. "Undefined escape in character constant");
  466. }
  467. } else if (*p=='\'')
  468. error(ERROR, "Empty character constant");
  469. else
  470. n = *p++;
  471. if (*p!='\'')
  472. error(WARNING, "Multibyte character constant undefined");
  473. else if (n>127)
  474. error(WARNING, "Character constant taken as not signed");
  475. v.val = n;
  476. break;
  477. case STRING:
  478. error(ERROR, "String in #if/#elsif");
  479. break;
  480. }
  481. return v;
  482. }
  483. int
  484. digit(int i)
  485. {
  486. if ('0'<=i && i<='9')
  487. i -= '0';
  488. else if ('a'<=i && i<='f')
  489. i -= 'a'-10;
  490. else if ('A'<=i && i<='F')
  491. i -= 'A'-10;
  492. else
  493. i = -1;
  494. return i;
  495. }