cstoper.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /* C O N S T A N T E X P R E S S I O N H A N D L I N G */
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "debug.h"
  5. #include "target_sizes.h"
  6. #include <alloc.h>
  7. #include <assert.h>
  8. #include <em_arith.h>
  9. #include <em_label.h>
  10. #include "LLlex.h"
  11. #include "Lpars.h"
  12. #include "const.h"
  13. #include "node.h"
  14. #include "required.h"
  15. #include "type.h"
  16. long mach_long_sign; /* sign bit of the machine long */
  17. long full_mask[MAXSIZE+1];/* full_mask[1] == 0xFF, full_mask[2] == 0xFFFF, .. */
  18. arith max_int; /* maximum integer on the target machine */
  19. arith min_int; /* mimimum integer on the target machin */
  20. char *maxint_str; /* string representation of maximum integer */
  21. arith wrd_bits; /* number of bits in a word */
  22. arith max_intset; /* largest value of set of integer */
  23. overflow(expp)
  24. struct node *expp;
  25. {
  26. node_warning(expp, "overflow in constant expression");
  27. }
  28. cstunary(expp)
  29. register struct node *expp;
  30. {
  31. /* The unary operation in "expp" is performed on the constant
  32. expression below it, and the result restored in expp.
  33. */
  34. register arith o1 = expp->nd_right->nd_INT;
  35. switch( expp->nd_symb ) {
  36. /* Should not get here
  37. case '+':
  38. case '(':
  39. break;
  40. */
  41. case '-':
  42. o1 = -o1;
  43. break;
  44. case NOT:
  45. o1 = !o1;
  46. break;
  47. default:
  48. crash("(cstunary)");
  49. }
  50. expp->nd_class = Value;
  51. expp->nd_token = expp->nd_right->nd_token;
  52. expp->nd_INT = o1;
  53. CutSize(expp);
  54. FreeNode(expp->nd_right);
  55. expp->nd_right = NULLNODE;
  56. }
  57. cstbin(expp)
  58. register struct node *expp;
  59. {
  60. /* The binary operation in "expp" is performed on the constant
  61. expressions below it, and the result restored in expp.
  62. */
  63. register arith o1, o2;
  64. register char *s1, *s2;
  65. int str = expp->nd_left->nd_type->tp_fund & T_STRINGCONST;
  66. if( str ) {
  67. o1 = o2 = 0; /* so LINT won't complain */
  68. s1 = expp->nd_left->nd_STR;
  69. s2 = expp->nd_right->nd_STR;
  70. }
  71. else {
  72. s1 = s2 = (char *) 0; /* so LINT won't complain */
  73. o1 = expp->nd_left->nd_INT;
  74. o2 = expp->nd_right->nd_INT;
  75. }
  76. assert(expp->nd_class == Boper);
  77. assert(expp->nd_left->nd_class == Value);
  78. assert(expp->nd_right->nd_class == Value);
  79. switch( expp->nd_symb ) {
  80. case '+':
  81. if (o1 > 0 && o2 > 0) {
  82. if (max_int - o1 < o2) overflow(expp);
  83. }
  84. else if (o1 < 0 && o2 < 0) {
  85. if (min_int - o1 > o2) overflow(expp);
  86. }
  87. o1 += o2;
  88. break;
  89. case '-':
  90. if ( o1 >= 0 && o2 < 0) {
  91. if (max_int + o2 < o1) overflow(expp);
  92. }
  93. else if (o1 < 0 && o2 >= 0) {
  94. if (min_int + o2 > o1) overflow(expp);
  95. }
  96. o1 -= o2;
  97. break;
  98. case '*':
  99. if (o1 > 0 && o2 > 0) {
  100. if (max_int / o1 < o2) overflow(expp);
  101. }
  102. else if (o1 < 0 && o2 < 0) {
  103. if (o1 == min_int || o2 == min_int ||
  104. max_int / (-o1) < (-o2)) overflow(expp);
  105. }
  106. else if (o1 > 0) {
  107. if (min_int / o1 > o2) overflow(expp);
  108. }
  109. else if (o2 > 0) {
  110. if (min_int / o2 > o1) overflow(expp);
  111. }
  112. o1 *= o2;
  113. break;
  114. case DIV:
  115. if( o2 == 0 ) {
  116. node_error(expp, "division by 0");
  117. return;
  118. }
  119. else o1 /= o2;
  120. break;
  121. case MOD:
  122. if( o2 == 0 ) {
  123. node_error(expp, "modulo by 0");
  124. return;
  125. }
  126. else
  127. o1 %= o2;
  128. break;
  129. case OR:
  130. o1 = (o1 || o2);
  131. break;
  132. case AND:
  133. o1 = (o1 && o2);
  134. break;
  135. case '=':
  136. o1 = str ? !strcmp(s1, s2) : (o1 == o2);
  137. break;
  138. case NOTEQUAL:
  139. o1 = str ? (strcmp(s1, s2) != 0) : (o1 != o2);
  140. break;
  141. case LESSEQUAL:
  142. o1 = str ? (strcmp(s1, s2) <= 0) : (o1 <= o2);
  143. break;
  144. case GREATEREQUAL:
  145. o1 = str ? (strcmp(s1, s2) >= 0) : (o1 >= o2);
  146. break;
  147. case '<':
  148. o1 = str ? (strcmp(s1, s2) < 0) : (o1 < o2);
  149. break;
  150. case '>':
  151. o1 = str ? (strcmp(s1, s2) > 0) : (o1 > o2);
  152. break;
  153. /* case '/': */
  154. default:
  155. crash("(cstbin)");
  156. }
  157. expp->nd_class = Value;
  158. expp->nd_token = expp->nd_right->nd_token;
  159. /* STRING compare has a bool_type as result */
  160. if( expp->nd_type == bool_type ) expp->nd_symb = INTEGER;
  161. expp->nd_INT = o1;
  162. CutSize(expp);
  163. FreeNode(expp->nd_left);
  164. FreeNode(expp->nd_right);
  165. expp->nd_left = expp->nd_right = NULLNODE;
  166. }
  167. cstset(expp)
  168. register struct node *expp;
  169. {
  170. register arith *set1, *set2;
  171. arith *resultset = (arith *) 0;
  172. int empty_result = 0;
  173. register int setsize, j;
  174. assert(expp->nd_right->nd_class == Set);
  175. assert(expp->nd_symb == IN || expp->nd_left->nd_class == Set);
  176. set2 = expp->nd_right->nd_set;
  177. setsize = (unsigned) (expp->nd_right->nd_type->tp_size) / (unsigned) word_size;
  178. if( expp->nd_symb == IN ) {
  179. arith i;
  180. assert(expp->nd_left->nd_class == Value);
  181. i = expp->nd_left->nd_INT;
  182. expp->nd_class = Value;
  183. expp->nd_symb = INTEGER;
  184. expp->nd_INT = (i >= 0 && set2 && i < (setsize * wrd_bits) &&
  185. (set2[i/wrd_bits] & (1 << (i%wrd_bits))));
  186. if( set2 ) free((char *) set2);
  187. }
  188. else {
  189. set1 = expp->nd_left->nd_set;
  190. resultset = set1;
  191. expp->nd_left->nd_set = (arith *) 0;
  192. switch( expp->nd_symb ) {
  193. case '+':
  194. /* Set union
  195. */
  196. if( !set1 ) {
  197. resultset = set2;
  198. expp->nd_right->nd_set = (arith *) 0;
  199. break;
  200. }
  201. if( set2 )
  202. for( j = 0; j < setsize; j++ )
  203. *set1++ |= *set2++;
  204. break;
  205. case '-':
  206. /* Set difference
  207. */
  208. if( !set1 || !set2 ) {
  209. /* The set from which something is substracted
  210. is already empty, or the set that is
  211. substracted is empty. In either case, the
  212. result set is set1.
  213. */
  214. break;
  215. }
  216. empty_result = 1;
  217. for( j = 0; j < setsize; j++ )
  218. if( *set1++ &= ~*set2++ ) empty_result = 0;
  219. break;
  220. case '*':
  221. /* Set intersection
  222. */
  223. if( !set1 ) {
  224. /* set1 is empty, and so is the result set
  225. */
  226. break;
  227. }
  228. if( !set2 ) {
  229. /* set 2 is empty, so the result set must be
  230. empty too.
  231. */
  232. resultset = set2;
  233. expp->nd_right->nd_set = (arith *) 0;
  234. break;
  235. }
  236. empty_result = 1;
  237. for( j = 0; j < setsize; j++ )
  238. if( *set1++ &= *set2++ ) empty_result = 0;
  239. break;
  240. case '=':
  241. case NOTEQUAL:
  242. case LESSEQUAL:
  243. case GREATEREQUAL:
  244. /* Constant set comparisons
  245. */
  246. if( !setsize ) setsize++; /* force comparison */
  247. expp->nd_left->nd_set = set1; /* may be disposed of */
  248. for( j = 0; j < setsize; j++ ) {
  249. switch( expp->nd_symb ) {
  250. case '=':
  251. case NOTEQUAL:
  252. if( !set1 && !set2 ) {
  253. j = setsize;
  254. break;
  255. }
  256. if( !set1 || !set2 ) break;
  257. if( *set1++ != *set2++ ) break;
  258. continue;
  259. case LESSEQUAL:
  260. if( !set1 ) {
  261. j = setsize;
  262. break;
  263. }
  264. if( !set2 ) break;
  265. if( (*set2 | *set1++) != *set2 ) break;
  266. set2++;
  267. continue;
  268. case GREATEREQUAL:
  269. if( !set2 ) {
  270. j = setsize;
  271. break;
  272. }
  273. if( !set1 ) break;
  274. if( (*set1 | *set2++) != *set1 ) break;
  275. set1++;
  276. continue;
  277. }
  278. break;
  279. }
  280. if( j < setsize )
  281. expp->nd_INT = expp->nd_symb == NOTEQUAL;
  282. else
  283. expp->nd_INT = expp->nd_symb != NOTEQUAL;
  284. expp->nd_class = Value;
  285. expp->nd_symb = INTEGER;
  286. if( expp->nd_left->nd_set )
  287. free((char *) expp->nd_left->nd_set);
  288. if( expp->nd_right->nd_set )
  289. free((char *) expp->nd_right->nd_set);
  290. FreeNode(expp->nd_left);
  291. FreeNode(expp->nd_right);
  292. expp->nd_left = expp->nd_right = NULLNODE;
  293. return;
  294. default:
  295. crash("(cstset)");
  296. }
  297. if( expp->nd_right->nd_set )
  298. free((char *) expp->nd_right->nd_set);
  299. if( expp->nd_left->nd_set )
  300. free((char *) expp->nd_left->nd_set);
  301. if( empty_result ) {
  302. free((char *) resultset);
  303. resultset = (arith *) 0;
  304. }
  305. expp->nd_class = Set;
  306. expp->nd_set = resultset;
  307. }
  308. FreeNode(expp->nd_left);
  309. FreeNode(expp->nd_right);
  310. expp->nd_left = expp->nd_right = NULLNODE;
  311. }
  312. cstcall(expp, req)
  313. register struct node *expp;
  314. {
  315. /* a standard procedure call is found that can be evaluated
  316. compile time, so do so.
  317. */
  318. register struct node *expr = NULLNODE;
  319. assert(expp->nd_class == Call);
  320. expr = expp->nd_right->nd_left;
  321. expp->nd_class = Value;
  322. expp->nd_symb = INTEGER;
  323. switch( req ) {
  324. case R_ABS:
  325. if( expr->nd_INT < 0 ) {
  326. if (expr->nd_INT <= min_int) {
  327. overflow(expr);
  328. }
  329. expp->nd_INT = - expr->nd_INT;
  330. }
  331. else expp->nd_INT = expr->nd_INT;
  332. CutSize(expp);
  333. break;
  334. case R_SQR:
  335. if (expr->nd_INT < 0) {
  336. if ( expr->nd_INT == min_int ||
  337. max_int / expr->nd_INT > expr->nd_INT) {
  338. overflow(expr);
  339. }
  340. }
  341. else if (max_int / expr->nd_INT < expr->nd_INT) {
  342. overflow(expr);
  343. }
  344. expp->nd_INT = expr->nd_INT * expr->nd_INT;
  345. CutSize(expp);
  346. break;
  347. case R_ORD:
  348. case R_CHR:
  349. expp->nd_INT = expr->nd_INT;
  350. CutSize(expp);
  351. break;
  352. case R_ODD:
  353. expp->nd_INT = (expr->nd_INT & 1);
  354. break;
  355. case R_SUCC:
  356. expp->nd_INT = expr->nd_INT + 1;
  357. if( /* Check overflow of subranges or enumerations */
  358. (expp->nd_type->tp_fund & T_SUBRANGE &&
  359. expp->nd_INT > expp->nd_type->sub_ub
  360. )
  361. ||
  362. ( expp->nd_type->tp_fund & T_ENUMERATION &&
  363. expp->nd_INT >= expp->nd_type->enm_ncst
  364. )
  365. )
  366. node_warning(expp, "\"succ\": no successor");
  367. else CutSize(expp);
  368. break;
  369. case R_PRED:
  370. expp->nd_INT = expr->nd_INT - 1;
  371. if( /* Check with lowerbound of subranges or enumerations */
  372. (expp->nd_type->tp_fund & T_SUBRANGE &&
  373. expp->nd_INT < expp->nd_type->sub_lb
  374. )
  375. ||
  376. ( expp->nd_type->tp_fund & T_ENUMERATION &&
  377. expp->nd_INT < 0
  378. )
  379. )
  380. node_warning(expp, "\"pred\": no predecessor");
  381. else CutSize(expp);
  382. break;
  383. default:
  384. crash("(cstcall)");
  385. }
  386. FreeNode(expp->nd_left);
  387. FreeNode(expp->nd_right);
  388. expp->nd_right = expp->nd_left = NULLNODE;
  389. }
  390. CutSize(expr)
  391. register struct node *expr;
  392. {
  393. /* The constant value of the expression expr is made to conform
  394. * to the size of the type of the expression
  395. */
  396. register arith o1 = expr->nd_INT;
  397. register struct type *tp = BaseType(expr->nd_type);
  398. int size = tp->tp_size;
  399. long remainder = o1 & ~full_mask[size];
  400. assert(expr->nd_class == Value);
  401. if( tp->tp_fund & T_CHAR ) {
  402. if( o1 & (~full_mask[size] >> 1) ) {
  403. node_warning(expr, "overflow in character value");
  404. o1 &= 0177;
  405. }
  406. }
  407. else if( remainder != 0 && remainder != ~full_mask[size] ||
  408. (o1 & full_mask[size]) == 1 << (size * 8 - 1) ) {
  409. /* integers in [-maxint .. maxint] */
  410. int nbits = (int) (sizeof(long) - size) * 8;
  411. /* overflow(expr); */
  412. /* sign bit of o1 in sign bit of mach_long */
  413. o1 <<= nbits;
  414. /* shift back to get sign extension */
  415. o1 >>= nbits;
  416. }
  417. expr->nd_INT = o1;
  418. }
  419. InitCst()
  420. {
  421. extern char *long2str(), *Salloc();
  422. register int i = 0;
  423. register arith bt = (arith)0;
  424. while( !(bt < 0) ) {
  425. bt = (bt << 8) + 0377;
  426. i++;
  427. if( i == MAXSIZE + 1 )
  428. fatal("array full_mask too small for this machine");
  429. full_mask[i] = bt;
  430. }
  431. mach_long_sign = 1L << (sizeof(long) * 8 - 1);
  432. if( int_size > sizeof(long) )
  433. fatal("sizeof (long) insufficient on this machine");
  434. max_int = full_mask[int_size] & ~(1L << (int_size * 8 - 1));
  435. min_int = - max_int;
  436. maxint_str = long2str(max_int, 10);
  437. maxint_str = Salloc(maxint_str, (unsigned int) strlen(maxint_str));
  438. wrd_bits = 8 * (int) word_size;
  439. if( !max_intset ) max_intset = wrd_bits - 1;
  440. }