eval.c 10 KB

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