expr.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  1. /* $Header$ */
  2. /* This file contains the expression evaluator. It exports the following
  3. routines:
  4. - int eval_cond(p_tree p)
  5. This routine evaluates the conditional expression indicated by p
  6. and returns 1 if it evaluates to TRUE, or 0 if it could not be
  7. evaluated for some reason or if it evalutes to FALSE.
  8. If the expression cannot be evaluated, an error message is given.
  9. - int eval_desig(p_tree p, t_addr *pbuf, long **psize, p_type *ptp)
  10. This routine evaluates the expression indicated by p, which should
  11. result in a designator. The result of the expression is an address
  12. which is to be found in *pbuf. *psize will contain the size of the
  13. designated object, and *ptp its type.
  14. If the expression cannot be evaluated or does not result in a
  15. designator, 0 is returned and an error message is given.
  16. Otherwise, 1 is returned.
  17. - int eval_expr(p_tree p, char **pbuf, long **psize, p_type *ptp)
  18. This routine evaluates the expression indicated by p.
  19. The result of the expression is left in *pbuf.
  20. *psize will contain the size of the value, and *ptp its type.
  21. If the expression cannot be evaluated, 0 is returned and an error
  22. message is given. Otherwise, 1 is returned.
  23. - int convert(char **pbuf, long *psize, p_type *ptp, p_type tp, long size)
  24. This routine tries to convert the value in pbuf of size psize
  25. and type ptp to type tp with size size. It returns 0 if this fails,
  26. while producing an error message. Otherwise, it returns 1 and
  27. the resulting value, type and size are left in pbuf, ptp, and
  28. psize, respectively.
  29. - long get_int(char *buf, long size, int class)
  30. Returns the value of size 'size', residing in 'buf', of 'class'
  31. T_INTEGER, T_UNSIGNED, or T_ENUM.
  32. - int put_int(char *buf, long size, long value)
  33. Stores the value 'value' of size 'size' in 'buf'.
  34. - double get_real(char *buf, long size)
  35. Returns the real value of size 'size', residing in 'buf'.
  36. T_INTEGER, T_UNSIGNED, or T_ENUM.
  37. - int put_real(char *buf, long size, double value)
  38. Stores the value 'value' of size 'size' in 'buf'.
  39. */
  40. #include <stdio.h>
  41. #include <alloc.h>
  42. #include <assert.h>
  43. #include "position.h"
  44. #include "operator.h"
  45. #include "tree.h"
  46. #include "expr.h"
  47. #include "symbol.h"
  48. #include "type.h"
  49. #include "langdep.h"
  50. extern FILE *db_out;
  51. extern char *strcpy();
  52. #define malloc_succeeded(p) if (! (p)) {\
  53. error("could not allocate enough memory");\
  54. return 0;\
  55. }
  56. /* buffer to integer and vice versa routines */
  57. long
  58. get_int(buf, size, class)
  59. char *buf;
  60. long size;
  61. int class;
  62. {
  63. register long l;
  64. switch((int)size) {
  65. case sizeof(char):
  66. l = *buf;
  67. if (class == T_INTEGER && l >= 0x7F) l -= 256;
  68. else if (class != T_INTEGER && l < 0) l += 256;
  69. break;
  70. case sizeof(short):
  71. l = *((short *) buf);
  72. if (class == T_INTEGER && l >= 0x7FFF) l -= 65536;
  73. else if (class != T_INTEGER && l < 0) l += 65536;
  74. break;
  75. default:
  76. l = *((long *) buf);
  77. }
  78. return l;
  79. }
  80. put_int(buf, size, value)
  81. char *buf;
  82. long size;
  83. long value;
  84. {
  85. switch((int)size) {
  86. case sizeof(char):
  87. *buf = value;
  88. break;
  89. case sizeof(short):
  90. *((short *) buf) = value;
  91. break;
  92. default:
  93. *((long *) buf) = value;
  94. break;
  95. }
  96. /*NOTREACHED*/
  97. }
  98. /* buffer to real and vice versa routines */
  99. double
  100. get_real(buf, size)
  101. char *buf;
  102. long size;
  103. {
  104. switch((int) size) {
  105. case sizeof(float):
  106. return *((float *) buf);
  107. default:
  108. return *((double *) buf);
  109. }
  110. /*NOTREACHED*/
  111. }
  112. put_real(buf, size, value)
  113. char *buf;
  114. long size;
  115. double value;
  116. {
  117. switch((int)size) {
  118. case sizeof(float):
  119. *((float *) buf) = value;
  120. break;
  121. default:
  122. *((double *) buf) = value;
  123. break;
  124. }
  125. /* NOTREACHED */
  126. }
  127. int
  128. convert(pbuf, psize, ptp, tp, size)
  129. char **pbuf;
  130. long *psize;
  131. register p_type *ptp;
  132. register p_type tp;
  133. long size;
  134. {
  135. /* Convert the value in pbuf, of size psize and type ptp, to type
  136. tp and leave the resulting value in pbuf, the resulting size
  137. in psize, and the resulting type in ptp.
  138. */
  139. long l;
  140. double d;
  141. if (*ptp == tp) return 1;
  142. if (size > *psize) {
  143. *pbuf = realloc(*pbuf, (unsigned int) size);
  144. malloc_succeeded(*pbuf);
  145. }
  146. if ((*ptp)->ty_class == T_SUBRANGE) *ptp = (*ptp)->ty_base;
  147. if (tp && *ptp) switch((*ptp)->ty_class) {
  148. case T_INTEGER:
  149. case T_UNSIGNED:
  150. case T_POINTER:
  151. case T_ENUM:
  152. l = get_int(*pbuf, *psize, (*ptp)->ty_class);
  153. if (tp == bool_type) l = l != 0;
  154. switch(tp->ty_class) {
  155. case T_SUBRANGE:
  156. case T_INTEGER:
  157. case T_UNSIGNED:
  158. case T_POINTER:
  159. case T_ENUM:
  160. put_int(*pbuf, size, l);
  161. *psize = size;
  162. *ptp = tp;
  163. return 1;
  164. case T_REAL:
  165. put_real(*pbuf,
  166. size,
  167. (*ptp)->ty_class == T_INTEGER
  168. ? (double) l
  169. : (double) (unsigned long) l);
  170. *psize = size;
  171. *ptp = tp;
  172. return 1;
  173. default:
  174. break;
  175. }
  176. break;
  177. case T_REAL:
  178. d = get_real(*pbuf, *psize);
  179. switch(tp->ty_class) {
  180. case T_ENUM:
  181. case T_SUBRANGE:
  182. case T_INTEGER:
  183. case T_UNSIGNED:
  184. case T_POINTER:
  185. if (tp == bool_type) put_int(*pbuf, size, (long) (d != 0));
  186. else put_int(*pbuf, size, (long) d);
  187. *psize = size;
  188. *ptp = tp;
  189. return 1;
  190. case T_REAL:
  191. put_real(*pbuf, size, d);
  192. *psize = size;
  193. *ptp = tp;
  194. return 1;
  195. default:
  196. break;
  197. }
  198. break;
  199. default:
  200. break;
  201. }
  202. error("illegal conversion");
  203. return 0;
  204. }
  205. int
  206. eval_cond(p)
  207. p_tree p;
  208. {
  209. char *buf;
  210. long size;
  211. p_type tp;
  212. long val;
  213. p_type target_tp = currlang->has_bool_type ? bool_type : int_type;
  214. if (eval_expr(p, &buf, &size, &tp)) {
  215. if (convert(&buf, &size, &tp, target_tp, target_tp->ty_size)) {
  216. val = get_int(buf, size, T_UNSIGNED);
  217. free(buf);
  218. return (int) (val != 0);
  219. }
  220. free(buf);
  221. }
  222. return 0;
  223. }
  224. /* one routine for each unary operator */
  225. static int
  226. not_op(p, pbuf, psize, ptp)
  227. p_tree p;
  228. char **pbuf;
  229. long *psize;
  230. p_type *ptp;
  231. {
  232. p_type target_tp = currlang->has_bool_type ? bool_type : int_type;
  233. if (eval_expr(p->t_args[0], pbuf, psize, ptp) &&
  234. convert(pbuf, psize, ptp, target_tp, target_tp->ty_size)) {
  235. put_int(*pbuf, *psize, (long) !get_int(*pbuf, *psize, T_UNSIGNED));
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. static int
  241. bnot_op(p, pbuf, psize, ptp)
  242. p_tree p;
  243. char **pbuf;
  244. long *psize;
  245. p_type *ptp;
  246. {
  247. if (eval_expr(p->t_args[0], pbuf, psize, ptp)) {
  248. switch((*ptp)->ty_class) {
  249. case T_INTEGER:
  250. case T_ENUM:
  251. case T_UNSIGNED:
  252. case T_SUBRANGE:
  253. put_int(*pbuf, *psize, ~get_int(*pbuf, *psize, T_UNSIGNED));
  254. return 1;
  255. default:
  256. error("illegal operand type(s)");
  257. break;
  258. }
  259. }
  260. return 0;
  261. }
  262. static int
  263. ptr_addr(p, paddr, psize, ptp)
  264. p_tree p;
  265. t_addr *paddr;
  266. long *psize;
  267. p_type *ptp;
  268. {
  269. char *buf;
  270. if (eval_expr(p->t_args[0], &buf, psize, ptp)) {
  271. switch((*ptp)->ty_class) {
  272. case T_POINTER:
  273. *ptp = (*ptp)->ty_ptrto;
  274. *psize = (*ptp)->ty_size;
  275. *paddr = get_int(buf, pointer_size, T_UNSIGNED);
  276. free(buf);
  277. return 1;
  278. default:
  279. error("illegal operand of DEREF");
  280. free(buf);
  281. break;
  282. }
  283. }
  284. return 0;
  285. }
  286. static int
  287. deref_op(p, pbuf, psize, ptp)
  288. p_tree p;
  289. char **pbuf;
  290. long *psize;
  291. p_type *ptp;
  292. {
  293. t_addr addr;
  294. if (ptr_addr(p, &addr, psize, ptp)) {
  295. *pbuf = malloc((unsigned) *psize);
  296. malloc_succeeded(*pbuf);
  297. if (! get_bytes(*psize, addr, *pbuf)) {
  298. free(*pbuf);
  299. *pbuf = 0;
  300. return 0;
  301. }
  302. return 1;
  303. }
  304. return 0;
  305. }
  306. static int
  307. addr_op(p, pbuf, psize, ptp)
  308. p_tree p;
  309. char **pbuf;
  310. long *psize;
  311. p_type *ptp;
  312. {
  313. t_addr addr;
  314. if (eval_desig(p->t_args[0], &addr, psize, ptp)) {
  315. *pbuf = malloc((unsigned) pointer_size);
  316. malloc_succeeded(*pbuf);
  317. put_int(*pbuf, pointer_size, (long) addr);
  318. address_type->ty_ptrto = *ptp;
  319. *ptp = address_type;
  320. return 1;
  321. }
  322. return 0;
  323. }
  324. static int
  325. unmin_op(p, pbuf, psize, ptp)
  326. p_tree p;
  327. char **pbuf;
  328. long *psize;
  329. p_type *ptp;
  330. {
  331. if (eval_expr(p->t_args[0], pbuf, psize, ptp)) {
  332. switch((*ptp)->ty_class) {
  333. case T_SUBRANGE:
  334. case T_INTEGER:
  335. case T_ENUM:
  336. case T_UNSIGNED:
  337. put_int(*pbuf, *psize, -get_int(*pbuf, *psize, (*ptp)->ty_class));
  338. return 1;
  339. case T_REAL:
  340. put_real(*pbuf, *psize, -get_real(*pbuf, *psize));
  341. return 1;
  342. default:
  343. error("illegal operand of unary -");
  344. break;
  345. }
  346. }
  347. return 0;
  348. }
  349. static int
  350. unplus_op(p, pbuf, psize, ptp)
  351. p_tree p;
  352. char **pbuf;
  353. long *psize;
  354. p_type *ptp;
  355. {
  356. if (eval_expr(p->t_args[0], pbuf, psize, ptp)) {
  357. switch((*ptp)->ty_class) {
  358. case T_SUBRANGE:
  359. case T_INTEGER:
  360. case T_ENUM:
  361. case T_UNSIGNED:
  362. case T_REAL:
  363. return 1;
  364. default:
  365. error("illegal operand of unary +");
  366. break;
  367. }
  368. }
  369. return 0;
  370. }
  371. static int (*un_op[])() = {
  372. 0,
  373. not_op,
  374. deref_op,
  375. 0,
  376. 0,
  377. 0,
  378. 0,
  379. 0,
  380. 0,
  381. 0,
  382. 0,
  383. unplus_op,
  384. unmin_op,
  385. 0,
  386. 0,
  387. 0,
  388. 0,
  389. 0,
  390. 0,
  391. 0,
  392. 0,
  393. 0,
  394. 0,
  395. 0,
  396. bnot_op,
  397. 0,
  398. 0,
  399. 0,
  400. addr_op
  401. };
  402. static p_type
  403. balance(tp1, tp2)
  404. p_type tp1, tp2;
  405. {
  406. if (tp1->ty_class == T_SUBRANGE) tp1 = tp1->ty_base;
  407. if (tp2->ty_class == T_SUBRANGE) tp2 = tp2->ty_base;
  408. if (tp1 == tp2) return tp2;
  409. if (tp2->ty_class == T_REAL) {
  410. p_type tmp = tp1; tp1 = tp2; tp2 = tmp;
  411. }
  412. if (tp1->ty_class == T_REAL) {
  413. switch(tp2->ty_class) {
  414. case T_INTEGER:
  415. case T_UNSIGNED:
  416. case T_ENUM:
  417. return tp1;
  418. case T_REAL:
  419. return tp1->ty_size > tp2->ty_size ? tp1 : tp2;
  420. default:
  421. error("illegal type combination");
  422. return 0;
  423. }
  424. }
  425. if (tp2->ty_class == T_POINTER) {
  426. p_type tmp = tp1; tp1 = tp2; tp2 = tmp;
  427. }
  428. if (tp1->ty_class == T_POINTER) {
  429. switch(tp2->ty_class) {
  430. case T_INTEGER:
  431. case T_UNSIGNED:
  432. case T_POINTER:
  433. case T_ENUM:
  434. return tp1;
  435. default:
  436. error("illegal type combination");
  437. return 0;
  438. }
  439. }
  440. if (tp2->ty_class == T_UNSIGNED) {
  441. p_type tmp = tp1; tp1 = tp2; tp2 = tmp;
  442. }
  443. if (tp1->ty_class == T_UNSIGNED) {
  444. switch(tp2->ty_class) {
  445. case T_INTEGER:
  446. case T_UNSIGNED:
  447. if (tp1->ty_size >= tp2->ty_size) return tp1;
  448. return tp2;
  449. case T_ENUM:
  450. return tp1;
  451. default:
  452. error("illegal type combination");
  453. return 0;
  454. }
  455. }
  456. if (tp2->ty_class == T_INTEGER) {
  457. p_type tmp = tp1; tp1 = tp2; tp2 = tmp;
  458. }
  459. if (tp1->ty_class == T_INTEGER) {
  460. switch(tp2->ty_class) {
  461. case T_INTEGER:
  462. if (tp1->ty_size >= tp2->ty_size) return tp1;
  463. return tp2;
  464. case T_ENUM:
  465. return tp1;
  466. default:
  467. error("illegal type combination");
  468. return 0;
  469. }
  470. }
  471. error("illegal type combination");
  472. return 0;
  473. }
  474. static int
  475. andor_op(p, pbuf, psize, ptp)
  476. p_tree p;
  477. char **pbuf;
  478. long *psize;
  479. p_type *ptp;
  480. {
  481. long l1, l2;
  482. char *buf = 0;
  483. long size;
  484. p_type tp;
  485. p_type target_tp = currlang->has_bool_type ? bool_type : int_type;
  486. if (eval_expr(p->t_args[0], pbuf, psize, ptp) &&
  487. convert(pbuf, psize, ptp, target_tp, target_tp->ty_size) &&
  488. eval_expr(p->t_args[1], &buf, &size, &tp) &&
  489. convert(&buf, &size, &tp, target_tp, target_tp->ty_size)) {
  490. l1 = get_int(*pbuf, *psize, T_UNSIGNED);
  491. l2 = get_int(buf, size, T_UNSIGNED);
  492. put_int(*pbuf,
  493. *psize,
  494. p->t_whichoper == E_AND
  495. ? (long)(l1 && l2)
  496. : (long)(l1 || l2));
  497. free(buf);
  498. return 1;
  499. }
  500. if (buf) free(buf);
  501. return 0;
  502. }
  503. static int
  504. arith_op(p, pbuf, psize, ptp)
  505. p_tree p;
  506. char **pbuf;
  507. long *psize;
  508. p_type *ptp;
  509. {
  510. long l1, l2;
  511. double d1, d2;
  512. char *buf = 0;
  513. long size;
  514. p_type tp, balance_tp;
  515. if (!(eval_expr(p->t_args[0], pbuf, psize, ptp) &&
  516. eval_expr(p->t_args[1], &buf, &size, &tp))) {
  517. return 0;
  518. }
  519. if ((*ptp)->ty_class == T_POINTER) {
  520. if (currlang != c_dep ||
  521. (p->t_whichoper != E_PLUS && p->t_whichoper != E_MIN)) {
  522. error("illegal operand type(s)");
  523. free(buf);
  524. return 0;
  525. }
  526. l1 = get_int(*pbuf, *psize, T_UNSIGNED);
  527. if (tp->ty_class == T_POINTER) {
  528. if (p->t_whichoper != E_MIN) {
  529. error("illegal operand type(s)");
  530. free(buf);
  531. return 0;
  532. }
  533. l2 = get_int(buf, size, T_UNSIGNED);
  534. free(buf);
  535. *pbuf = Realloc(*pbuf, (unsigned) long_size);
  536. put_int(*pbuf, long_size, (l1 - l2)/(*ptp)->ty_ptrto->ty_size);
  537. *ptp = long_type;
  538. return 1;
  539. }
  540. if (! convert(&buf, &size, &tp, long_type, long_size)) {
  541. free(buf);
  542. return 0;
  543. }
  544. l2 = get_int(buf, size, T_INTEGER) * (*ptp)->ty_ptrto->ty_size;
  545. free(buf);
  546. buf = 0;
  547. if (p->t_whichoper == E_PLUS) l1 += l2;
  548. else l1 -= l2;
  549. put_int(*pbuf, *psize, l1);
  550. return 1;
  551. }
  552. if ((balance_tp = balance(*ptp, tp)) &&
  553. convert(pbuf, psize, ptp, balance_tp, balance_tp->ty_size) &&
  554. convert(&buf, &size, &tp, balance_tp, balance_tp->ty_size)) {
  555. switch(balance_tp->ty_class) {
  556. case T_INTEGER:
  557. case T_ENUM:
  558. case T_UNSIGNED:
  559. l1 = get_int(*pbuf, *psize, balance_tp->ty_class);
  560. l2 = get_int(buf, size, balance_tp->ty_class);
  561. free(buf);
  562. buf = 0;
  563. switch(p->t_whichoper) {
  564. case E_BAND:
  565. l1 &= l2;
  566. break;
  567. case E_BOR:
  568. l1 |= l2;
  569. break;
  570. case E_BXOR:
  571. l1 ^= l2;
  572. break;
  573. case E_PLUS:
  574. l1 += l2;
  575. break;
  576. case E_MIN:
  577. l1 -= l2;
  578. break;
  579. case E_MUL:
  580. l1 *= l2;
  581. break;
  582. case E_DIV:
  583. case E_ZDIV:
  584. if (! l2) {
  585. error("division by 0");
  586. return 0;
  587. }
  588. if (balance_tp->ty_class == T_INTEGER) {
  589. if ((l1 < 0) != (l2 < 0)) {
  590. if (l1 < 0) l1 = - l1;
  591. else l2 = -l2;
  592. if (p->t_whichoper == E_DIV) {
  593. l1 = -((l1+l2-1)/l2);
  594. }
  595. else {
  596. l1 = -(l1/l2);
  597. }
  598. }
  599. else l1 /= l2;
  600. }
  601. else l1 = (unsigned long) l1 /
  602. (unsigned long) l2;
  603. break;
  604. case E_MOD:
  605. case E_ZMOD:
  606. if (! l2) {
  607. error("modulo by 0");
  608. return 0;
  609. }
  610. if (balance_tp->ty_class == T_INTEGER) {
  611. if ((l1 < 0) != (l2 < 0)) {
  612. if (l1 < 0) l1 = - l1;
  613. else l2 = -l2;
  614. if (p->t_whichoper == E_MOD) {
  615. l1 = ((l1+l2-1)/l2)*l2 - l1;
  616. }
  617. else {
  618. l1 = (l1/l2)*l2 - l1;
  619. }
  620. }
  621. else l1 %= l2;
  622. }
  623. else l1 = (unsigned long) l1 %
  624. (unsigned long) l2;
  625. break;
  626. }
  627. put_int(*pbuf, *psize, l1);
  628. break;
  629. case T_REAL:
  630. d1 = get_real(*pbuf, *psize);
  631. d2 = get_real(buf, size);
  632. free(buf);
  633. buf = 0;
  634. switch(p->t_whichoper) {
  635. case E_DIV:
  636. case E_ZDIV:
  637. if (d2 == 0.0) {
  638. error("division by 0.0");
  639. return 0;
  640. }
  641. d1 /= d2;
  642. break;
  643. case E_PLUS:
  644. d1 += d2;
  645. break;
  646. case E_MIN:
  647. d1 -= d2;
  648. break;
  649. case E_MUL:
  650. d1 *= d2;
  651. break;
  652. }
  653. put_real(*pbuf, *psize, d1);
  654. break;
  655. default:
  656. error("illegal operand type(s)");
  657. free(buf);
  658. return 0;
  659. }
  660. return 1;
  661. }
  662. if (buf) free(buf);
  663. return 0;
  664. }
  665. static int
  666. sft_op(p, pbuf, psize, ptp)
  667. p_tree p;
  668. char **pbuf;
  669. long *psize;
  670. p_type *ptp;
  671. {
  672. long l1, l2;
  673. char *buf = 0;
  674. long size;
  675. p_type tp;
  676. if (eval_expr(p->t_args[0], pbuf, psize, ptp) &&
  677. eval_expr(p->t_args[1], &buf, &size, &tp) &&
  678. convert(&buf, &size, &tp, int_type, int_size)) {
  679. tp = *ptp;
  680. if (tp->ty_class == T_SUBRANGE) {
  681. tp = tp->ty_base;
  682. }
  683. switch(tp->ty_class) {
  684. case T_INTEGER:
  685. case T_ENUM:
  686. case T_UNSIGNED:
  687. l1 = get_int(*pbuf, *psize, tp->ty_class);
  688. l2 = get_int(buf, size, T_INTEGER);
  689. free(buf);
  690. buf = 0;
  691. switch(p->t_whichoper) {
  692. case E_LSFT:
  693. l1 <<= (int) l2;
  694. break;
  695. case E_RSFT:
  696. if (tp->ty_class == T_INTEGER) l1 >>= (int) l2;
  697. else l1 = (unsigned long) l1 >> (int) l2;
  698. break;
  699. }
  700. break;
  701. default:
  702. error("illegal operand type(s)");
  703. free(buf);
  704. return 0;
  705. }
  706. return 1;
  707. }
  708. if (buf) free(buf);
  709. return 0;
  710. }
  711. static int
  712. cmp_op(p, pbuf, psize, ptp)
  713. p_tree p;
  714. char **pbuf;
  715. long *psize;
  716. p_type *ptp;
  717. {
  718. long l1, l2;
  719. double d1, d2;
  720. char *buf = 0;
  721. long size;
  722. p_type tp, balance_tp;
  723. if (eval_expr(p->t_args[0], pbuf, psize, ptp) &&
  724. eval_expr(p->t_args[1], &buf, &size, &tp) &&
  725. (balance_tp = balance(*ptp, tp)) &&
  726. convert(pbuf, psize, ptp, balance_tp, balance_tp->ty_size) &&
  727. convert(&buf, &size, &tp, balance_tp, balance_tp->ty_size)) {
  728. switch(balance_tp->ty_class) {
  729. case T_INTEGER:
  730. case T_ENUM:
  731. case T_UNSIGNED:
  732. case T_POINTER:
  733. l1 = get_int(*pbuf, *psize, balance_tp->ty_class);
  734. l2 = get_int(buf, size, balance_tp->ty_class);
  735. free(buf);
  736. buf = 0;
  737. switch(p->t_whichoper) {
  738. case E_EQUAL:
  739. l1 = l1 == l2;
  740. break;
  741. case E_NOTEQUAL:
  742. l1 = l1 != l2;
  743. break;
  744. case E_LTEQUAL:
  745. if (balance_tp->ty_class == T_INTEGER) {
  746. l1 = l1 <= l2;
  747. }
  748. else l1 = (unsigned long) l1 <=
  749. (unsigned long) l2;
  750. break;
  751. case E_LT:
  752. if (balance_tp->ty_class == T_INTEGER) {
  753. l1 = l1 < l2;
  754. }
  755. else l1 = (unsigned long) l1 <
  756. (unsigned long) l2;
  757. break;
  758. case E_GTEQUAL:
  759. if (balance_tp->ty_class == T_INTEGER) {
  760. l1 = l1 >= l2;
  761. }
  762. else l1 = (unsigned long) l1 >=
  763. (unsigned long) l2;
  764. break;
  765. case E_GT:
  766. if (balance_tp->ty_class == T_INTEGER) {
  767. l1 = l1 > l2;
  768. }
  769. else l1 = (unsigned long) l1 >
  770. (unsigned long) l2;
  771. break;
  772. }
  773. break;
  774. case T_REAL:
  775. d1 = get_real(*pbuf, *psize);
  776. d2 = get_real(buf, size);
  777. free(buf);
  778. buf = 0;
  779. switch(p->t_whichoper) {
  780. case E_EQUAL:
  781. l1 = d1 == d2;
  782. break;
  783. case E_NOTEQUAL:
  784. l1 = d1 != d2;
  785. break;
  786. case E_LTEQUAL:
  787. l1 = d1 <= d2;
  788. break;
  789. case E_LT:
  790. l1 = d1 < d2;
  791. break;
  792. case E_GTEQUAL:
  793. l1 = d1 >= d2;
  794. break;
  795. case E_GT:
  796. l1 = d1 > d2;
  797. break;
  798. }
  799. break;
  800. default:
  801. error("illegal operand type(s)");
  802. free(buf);
  803. return 0;
  804. }
  805. if (*psize < int_size) {
  806. *psize = int_size;
  807. *pbuf = realloc(*pbuf, (unsigned int) int_size);
  808. malloc_succeeded(*pbuf);
  809. }
  810. else *psize = int_size;
  811. if (currlang->has_bool_type) {
  812. *ptp = bool_type;
  813. }
  814. else *ptp = int_type;
  815. put_int(*pbuf, *psize, l1);
  816. return 1;
  817. }
  818. if (buf) free(buf);
  819. return 0;
  820. }
  821. static int
  822. in_op(p, pbuf, psize, ptp)
  823. p_tree p;
  824. char **pbuf;
  825. long *psize;
  826. p_type *ptp;
  827. {
  828. long l;
  829. char *buf = 0;
  830. long size;
  831. p_type tp;
  832. int sft = int_size == 2 ? 4 : 5;
  833. if (eval_expr(p->t_args[0], pbuf, psize, ptp) &&
  834. eval_expr(p->t_args[1], &buf, &size, &tp)) {
  835. if (tp->ty_class != T_SET) {
  836. error("right-hand side of IN not a set");
  837. free(buf);
  838. return 0;
  839. }
  840. if (! convert(pbuf, psize, ptp, tp->ty_setbase, int_size)) {
  841. free(buf);
  842. return 0;
  843. }
  844. l = get_int(*pbuf, *psize, (*ptp)->ty_class) - tp->ty_setlow;
  845. l = l >= 0
  846. && l <= (size << 3)
  847. && (((int *) buf)[(int)(l>>sft)] & (1 << (l & ((1 << sft)-1))));
  848. free(buf);
  849. *pbuf = realloc(*pbuf, (unsigned) int_size);
  850. malloc_succeeded(*pbuf);
  851. *psize = int_size;
  852. *ptp = currlang->has_bool_type ? bool_type : int_type;
  853. put_int(*pbuf, *psize, l);
  854. return 1;
  855. }
  856. return 0;
  857. }
  858. static int
  859. array_addr(p, paddr, psize, ptp)
  860. p_tree p;
  861. t_addr *paddr;
  862. long *psize;
  863. p_type *ptp;
  864. {
  865. long l;
  866. char *buf = 0;
  867. long size;
  868. p_type tp;
  869. if (eval_desig(p->t_args[0], paddr, psize, ptp) &&
  870. eval_expr(p->t_args[1], &buf, &size, &tp)) {
  871. if ((*ptp)->ty_class != T_ARRAY && (*ptp)->ty_class != T_POINTER) {
  872. error("illegal left-hand side of [");
  873. free(buf);
  874. return 0;
  875. }
  876. if ((*ptp)->ty_class == T_POINTER) {
  877. if (! get_bytes(pointer_size, *paddr, (char *) paddr)) {
  878. free(buf);
  879. return 0;
  880. }
  881. *paddr = get_int((char *) paddr, pointer_size, T_UNSIGNED);
  882. }
  883. if (! convert(&buf, &size, &tp, int_type, int_size)) {
  884. free(buf);
  885. return 0;
  886. }
  887. l = get_int(buf, size, T_INTEGER);
  888. free(buf);
  889. buf = 0;
  890. if ((*ptp)->ty_class == T_ARRAY) {
  891. if (l < (*ptp)->ty_lb || l > (*ptp)->ty_hb) {
  892. error("array bound error");
  893. return 0;
  894. }
  895. l -= (*ptp)->ty_lb;
  896. *ptp = (*ptp)->ty_elements;
  897. l *= (*currlang->arrayelsize)((*ptp)->ty_size);
  898. }
  899. else {
  900. *ptp = (*ptp)->ty_ptrto;
  901. l *= (*ptp)->ty_size;
  902. }
  903. *psize = (*ptp)->ty_size;
  904. *paddr += l;
  905. return 1;
  906. }
  907. return 0;
  908. }
  909. static int
  910. array_op(p, pbuf, psize, ptp)
  911. p_tree p;
  912. char **pbuf;
  913. long *psize;
  914. p_type *ptp;
  915. {
  916. t_addr a;
  917. if (array_addr(p, &a, psize, ptp)) {
  918. *pbuf = malloc((unsigned int) *psize);
  919. malloc_succeeded(*pbuf);
  920. if (! get_bytes(*psize, a, *pbuf)) {
  921. return 0;
  922. }
  923. return 1;
  924. }
  925. return 0;
  926. }
  927. static int
  928. select_addr(p, paddr, psize, ptp)
  929. p_tree p;
  930. t_addr *paddr;
  931. long *psize;
  932. p_type *ptp;
  933. {
  934. register p_type tp;
  935. register struct fields *f;
  936. register int nf;
  937. if (eval_desig(p->t_args[0], paddr, psize, ptp)) {
  938. tp = *ptp;
  939. if (tp->ty_class != T_STRUCT && tp->ty_class != T_UNION) {
  940. error("SELECT on non-struct");
  941. return 0;
  942. }
  943. if (p->t_args[1]->t_oper != OP_NAME) {
  944. error("right-hand side of SELECT not a name");
  945. return 0;
  946. }
  947. for (nf = tp->ty_nfields, f = tp->ty_fields; nf; nf--, f++) {
  948. if (! strcmp(f->fld_name, p->t_args[1]->t_str)) break;
  949. }
  950. if (! nf) {
  951. error("'%s' not found", p->t_args[1]->t_str);
  952. return 0;
  953. }
  954. /* ??? this needs some work for bitfields ??? */
  955. *paddr += f->fld_pos>>3;
  956. *psize = f->fld_bitsize >> 3;
  957. *ptp = f->fld_type;
  958. return 1;
  959. }
  960. return 0;
  961. }
  962. static int
  963. select_op(p, pbuf, psize, ptp)
  964. p_tree p;
  965. char **pbuf;
  966. long *psize;
  967. p_type *ptp;
  968. {
  969. t_addr a;
  970. if (select_addr(p, &a, psize, ptp)) {
  971. *pbuf = malloc((unsigned int) *psize);
  972. malloc_succeeded(*pbuf);
  973. if (! get_bytes(*psize, a, *pbuf)) {
  974. free(*pbuf);
  975. *pbuf = 0;
  976. return 0;
  977. }
  978. return 1;
  979. }
  980. return 0;
  981. }
  982. static int
  983. derselect_op(p, pbuf, psize, ptp)
  984. p_tree p;
  985. char **pbuf;
  986. long *psize;
  987. p_type *ptp;
  988. {
  989. int retval;
  990. t_tree t;
  991. t.t_oper = OP_UNOP;
  992. t.t_whichoper = E_DEREF;
  993. t.t_args[0] = p->t_args[0];
  994. p->t_args[0] = &t;
  995. p->t_whichoper = E_SELECT;
  996. retval = eval_expr(p, pbuf, psize, ptp);
  997. p->t_args[0] = t.t_args[0];
  998. p->t_whichoper = E_DERSELECT;
  999. return retval;
  1000. }
  1001. static int (*bin_op[])() = {
  1002. 0,
  1003. 0,
  1004. 0,
  1005. andor_op,
  1006. andor_op,
  1007. arith_op,
  1008. arith_op,
  1009. arith_op,
  1010. arith_op,
  1011. in_op,
  1012. array_op,
  1013. arith_op,
  1014. arith_op,
  1015. arith_op,
  1016. cmp_op,
  1017. cmp_op,
  1018. cmp_op,
  1019. cmp_op,
  1020. cmp_op,
  1021. cmp_op,
  1022. select_op,
  1023. arith_op,
  1024. arith_op,
  1025. arith_op,
  1026. 0,
  1027. derselect_op,
  1028. sft_op,
  1029. sft_op,
  1030. 0
  1031. };
  1032. int
  1033. eval_expr(p, pbuf, psize, ptp)
  1034. p_tree p;
  1035. char **pbuf;
  1036. long *psize;
  1037. p_type *ptp;
  1038. {
  1039. register p_symbol sym;
  1040. int retval = 0;
  1041. *pbuf = 0;
  1042. switch(p->t_oper) {
  1043. case OP_FORMAT:
  1044. if (eval_expr(p->t_args[0], pbuf, psize, ptp)) retval = 1;
  1045. break;
  1046. case OP_NAME:
  1047. case OP_SELECT:
  1048. sym = identify(p, VAR|REGVAR|LOCVAR|VARPAR|CONST);
  1049. if (! sym) return 0;
  1050. if (! get_value(sym, pbuf, psize)) {
  1051. break;
  1052. }
  1053. *ptp = sym->sy_type;
  1054. retval = 1;
  1055. break;
  1056. case OP_INTEGER:
  1057. *pbuf = malloc((unsigned int) long_size);
  1058. malloc_succeeded(*pbuf);
  1059. *psize = long_size;
  1060. *ptp = long_type;
  1061. put_int(*pbuf, long_size, p->t_ival);
  1062. retval = 1;
  1063. break;
  1064. case OP_REAL:
  1065. *pbuf = malloc((unsigned int) double_size);
  1066. malloc_succeeded(*pbuf);
  1067. *psize = double_size;
  1068. *ptp = double_type;
  1069. put_real(*pbuf, double_size, p->t_fval);
  1070. retval = 1;
  1071. break;
  1072. case OP_STRING:
  1073. *psize = strlen(p->t_sval)+1;
  1074. *pbuf = malloc((unsigned int)*psize);
  1075. malloc_succeeded(*pbuf);
  1076. *ptp = string_type;
  1077. strcpy(*pbuf, p->t_sval);
  1078. retval = 1;
  1079. break;
  1080. case OP_UNOP:
  1081. retval = (*un_op[p->t_whichoper])(p, pbuf, psize, ptp);
  1082. break;
  1083. case OP_BINOP:
  1084. retval = (*bin_op[p->t_whichoper])(p, pbuf, psize, ptp);
  1085. break;
  1086. default:
  1087. assert(0);
  1088. break;
  1089. }
  1090. if (! retval) {
  1091. if (*pbuf) {
  1092. free(*pbuf);
  1093. *pbuf = 0;
  1094. }
  1095. *psize = 0;
  1096. }
  1097. else {
  1098. if ((*ptp)->ty_class == T_CROSS) {
  1099. *ptp = (*ptp)->ty_cross;
  1100. if (! *ptp) *ptp = void_type;
  1101. }
  1102. }
  1103. return retval;
  1104. }
  1105. extern t_addr get_addr();
  1106. int
  1107. eval_desig(p, paddr, psize, ptp)
  1108. p_tree p;
  1109. t_addr *paddr;
  1110. long *psize;
  1111. p_type *ptp;
  1112. {
  1113. register p_symbol sym;
  1114. int retval = 0;
  1115. t_addr a;
  1116. switch(p->t_oper) {
  1117. case OP_NAME:
  1118. case OP_SELECT:
  1119. sym = identify(p, VAR|REGVAR|LOCVAR|VARPAR);
  1120. if (! sym) return 0;
  1121. if (! (a = get_addr(sym, psize))) {
  1122. break;
  1123. }
  1124. *paddr = a;
  1125. *ptp = sym->sy_type;
  1126. retval = 1;
  1127. break;
  1128. case OP_UNOP:
  1129. switch(p->t_whichoper) {
  1130. case E_DEREF:
  1131. if (ptr_addr(p, paddr, psize, ptp)) {
  1132. retval = 1;
  1133. }
  1134. break;
  1135. default:
  1136. print_node(db_out, p, 0);
  1137. fputs(" not a designator\n", db_out);
  1138. break;
  1139. }
  1140. break;
  1141. case OP_BINOP:
  1142. switch(p->t_whichoper) {
  1143. case E_ARRAY:
  1144. if (array_addr(p, paddr, psize, ptp)) {
  1145. retval = 1;
  1146. }
  1147. break;
  1148. case E_SELECT:
  1149. if (select_addr(p, paddr, psize, ptp)) {
  1150. retval = 1;
  1151. }
  1152. break;
  1153. default:
  1154. print_node(db_out, p, 0);
  1155. fputs(" not a designator\n", db_out);
  1156. break;
  1157. }
  1158. break;
  1159. default:
  1160. error("illegal designator");
  1161. break;
  1162. }
  1163. if (! retval) {
  1164. *psize = 0;
  1165. }
  1166. else {
  1167. if ((*ptp)->ty_class == T_CROSS) {
  1168. *ptp = (*ptp)->ty_cross;
  1169. if (! *ptp) {
  1170. *ptp = void_type;
  1171. print_node(db_out, p, 0);
  1172. fputs(" designator has unknown type\n", db_out);
  1173. retval = 0;
  1174. *psize = 0;
  1175. }
  1176. }
  1177. }
  1178. return retval;
  1179. }