eval.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /* $Header$ */
  2. /* EXPRESSION-CODE GENERATOR */
  3. /* main functions :
  4. EVAL() -- expression tree evaluator
  5. tmp_pointer_var() -- deliver temporary pointer variable
  6. free_tmp_var() -- return the pointer var
  7. store_val() -- store primary expression
  8. load_val() -- load primary expression
  9. auxiliary functions:
  10. assop()
  11. compare()
  12. */
  13. #include <em.h>
  14. #include "debug.h"
  15. #include "nobitfield.h"
  16. #include "dataflow.h"
  17. #include "arith.h"
  18. #include "type.h"
  19. #include "idf.h"
  20. #include "label.h"
  21. #include "code.h"
  22. #include "assert.h"
  23. #include "def.h"
  24. #include "expr.h"
  25. #include "sizes.h"
  26. #include "Lpars.h"
  27. #include "level.h"
  28. #include "stack.h"
  29. #include "align.h"
  30. #include "mes.h"
  31. #include "atw.h"
  32. #define CRASH() crash("EVAL: CRASH at line %u", __LINE__)
  33. #define toword(n) ((n) < word_size ? word_size : (n))
  34. char *symbol2str();
  35. char *long2str();
  36. arith tmp_pointer_var();
  37. /* EVAL() serves as the main expression tree evaluator, which turns
  38. any legal expression tree into legal EM code.
  39. The parameters describe how EVAL should treat the expression tree:
  40. struct expr *expr: pointer to root of the expression tree to
  41. be evaluated
  42. int val: indicates whether the resulting expression
  43. is to be dereferenced (if val == RVAL and
  44. expr->ex_lvalue == 1) or not (val == LVAL).
  45. The latter case indicates that the resulting
  46. expression is an lvalue expression which should
  47. not be dereferenced by EVAL
  48. int code: indicates whether the expression tree must be
  49. turned into EM code or not. E.g. the expression
  50. statement "12;" delivers the expression "12" to
  51. EVAL while this should not result in any EM
  52. code
  53. label false_label:
  54. label true_label: if the expression is a logical or relational
  55. expression and if the loop of the program
  56. depends on the resulting value then EVAL
  57. generates jumps to the specified program
  58. labels, in case they are specified
  59. (i.e. are non-zero)
  60. */
  61. EVAL(expr, val, code, true_label, false_label)
  62. struct expr *expr; /* the expression tree itself */
  63. int val; /* either RVAL or LVAL */
  64. int code; /* generate explicit code or not */
  65. label true_label;
  66. label false_label; /* labels to jump to in logical expr's */
  67. {
  68. register gencode = (code == TRUE);
  69. switch (expr->ex_class) {
  70. case Value: /* just a simple value */
  71. if (gencode)
  72. load_val(expr, val);
  73. break;
  74. case String: /* a string constant */
  75. expr_warning(expr, "(DEBUG) value-class 'String' seen");
  76. if (gencode) {
  77. string2pointer(&expr);
  78. C_lae_dlb(expr->VL_LBL, expr->VL_VALUE);
  79. }
  80. break;
  81. #ifndef NOFLOAT
  82. case Float: /* a floating constant */
  83. if (gencode) {
  84. label datlab = data_label();
  85. C_df_dlb(datlab);
  86. C_rom_fcon(expr->FL_VALUE, expr->ex_type->tp_size);
  87. C_lae_dlb(datlab, (arith)0);
  88. C_loi(expr->ex_type->tp_size);
  89. }
  90. break;
  91. #endif NOFLOAT
  92. case Oper: /* compound expression */
  93. {
  94. register int oper = expr->OP_OPER;
  95. register struct expr *leftop = expr->OP_LEFT;
  96. register struct expr *rightop = expr->OP_RIGHT;
  97. register struct type *tp = expr->OP_TYPE;
  98. if (tp->tp_fund == ERRONEOUS) /* stop immediately */
  99. break;
  100. switch (oper) {
  101. case '+':
  102. /* We have the following possibilities :
  103. int + int, pointer + int, pointer + long,
  104. long + long, double + double
  105. */
  106. EVAL(leftop, RVAL, code, NO_LABEL, NO_LABEL);
  107. EVAL(rightop, RVAL, code, NO_LABEL, NO_LABEL);
  108. if (gencode) {
  109. switch (tp->tp_fund) {
  110. case INT:
  111. case LONG:
  112. if (tp->tp_unsigned)
  113. C_adu(tp->tp_size);
  114. else
  115. C_adi(tp->tp_size);
  116. break;
  117. case POINTER:
  118. C_ads(rightop->ex_type->tp_size);
  119. break;
  120. #ifndef NOFLOAT
  121. case DOUBLE:
  122. C_adf(tp->tp_size);
  123. break;
  124. #endif NOFLOAT
  125. default:
  126. crash("bad type +");
  127. }
  128. }
  129. break;
  130. case '-':
  131. if (leftop == 0) { /* unary */
  132. EVAL(rightop, RVAL, code, NO_LABEL, NO_LABEL);
  133. if (gencode) {
  134. switch (tp->tp_fund) {
  135. case INT:
  136. case LONG:
  137. case POINTER:
  138. C_ngi(tp->tp_size);
  139. break;
  140. #ifndef NOFLOAT
  141. case DOUBLE:
  142. C_ngf(tp->tp_size);
  143. break;
  144. #endif NOFLOAT
  145. default:
  146. CRASH();
  147. }
  148. }
  149. break;
  150. }
  151. /* Binary: we have the following flavours:
  152. int - int, pointer - int, pointer - long,
  153. pointer - pointer, long - long, double - double
  154. */
  155. EVAL(leftop, RVAL, code, NO_LABEL, NO_LABEL);
  156. EVAL(rightop, RVAL, code, NO_LABEL, NO_LABEL);
  157. if (!gencode)
  158. break;
  159. switch (tp->tp_fund) {
  160. case INT:
  161. case LONG:
  162. if (tp->tp_unsigned)
  163. C_sbu(tp->tp_size);
  164. else
  165. C_sbi(tp->tp_size);
  166. break;
  167. case POINTER:
  168. if (rightop->ex_type->tp_fund == POINTER)
  169. C_sbs(pointer_size);
  170. else {
  171. C_ngi(rightop->ex_type->tp_size);
  172. C_ads(rightop->ex_type->tp_size);
  173. }
  174. break;
  175. #ifndef NOFLOAT
  176. case DOUBLE:
  177. C_sbf(tp->tp_size);
  178. break;
  179. #endif NOFLOAT
  180. default:
  181. crash("bad type -");
  182. }
  183. break;
  184. case '*':
  185. if (leftop == 0) /* unary */
  186. EVAL(rightop, RVAL, code, NO_LABEL, NO_LABEL);
  187. else { /* binary */
  188. EVAL(leftop, RVAL, code, NO_LABEL, NO_LABEL);
  189. EVAL(rightop, RVAL, code, NO_LABEL, NO_LABEL);
  190. if (gencode)
  191. switch (tp->tp_fund) {
  192. case INT:
  193. case LONG:
  194. case POINTER:
  195. if (tp->tp_unsigned)
  196. C_mlu(tp->tp_size);
  197. else
  198. C_mli(tp->tp_size);
  199. break;
  200. #ifndef NOFLOAT
  201. case DOUBLE:
  202. C_mlf(double_size);
  203. break;
  204. #endif NOFLOAT
  205. default:
  206. crash("bad type *");
  207. }
  208. }
  209. break;
  210. case '/':
  211. EVAL(leftop, RVAL, code, NO_LABEL, NO_LABEL);
  212. EVAL(rightop, RVAL, code, NO_LABEL, NO_LABEL);
  213. if (gencode)
  214. switch (tp->tp_fund) {
  215. case INT:
  216. case LONG:
  217. case POINTER:
  218. if (tp->tp_unsigned)
  219. C_dvu(tp->tp_size);
  220. else
  221. C_dvi(tp->tp_size);
  222. break;
  223. #ifndef NOFLOAT
  224. case DOUBLE:
  225. C_dvf(double_size);
  226. break;
  227. #endif NOFLOAT
  228. default:
  229. crash("bad type /");
  230. }
  231. break;
  232. case '%':
  233. EVAL(leftop, RVAL, code, NO_LABEL, NO_LABEL);
  234. EVAL(rightop, RVAL, code, NO_LABEL, NO_LABEL);
  235. ASSERT(tp->tp_fund==INT || tp->tp_fund==LONG);
  236. if (gencode) {
  237. if (tp->tp_unsigned)
  238. C_rmu(tp->tp_size);
  239. else
  240. C_rmi(tp->tp_size);
  241. }
  242. break;
  243. case LEFT:
  244. EVAL(leftop, RVAL, code, NO_LABEL, NO_LABEL);
  245. EVAL(rightop, RVAL, code, NO_LABEL, NO_LABEL);
  246. if (gencode)
  247. if (tp->tp_unsigned)
  248. C_slu(tp->tp_size);
  249. else
  250. C_sli(tp->tp_size);
  251. break;
  252. case RIGHT:
  253. EVAL(leftop, RVAL, code, NO_LABEL, NO_LABEL);
  254. EVAL(rightop, RVAL, code, NO_LABEL, NO_LABEL);
  255. if (gencode)
  256. if (tp->tp_unsigned)
  257. C_sru(tp->tp_size);
  258. else
  259. C_sri(tp->tp_size);
  260. break;
  261. case '<':
  262. case LESSEQ:
  263. case '>':
  264. case GREATEREQ:
  265. case EQUAL:
  266. case NOTEQUAL:
  267. EVAL(leftop, RVAL, code, NO_LABEL, NO_LABEL);
  268. EVAL(rightop, RVAL, code, NO_LABEL, NO_LABEL);
  269. if (gencode) {
  270. /* The operands have the same type */
  271. arith size = leftop->ex_type->tp_size;
  272. switch (tp->tp_fund) {
  273. case INT:
  274. case LONG:
  275. if (leftop->ex_type->tp_unsigned)
  276. C_cmu(size);
  277. else
  278. C_cmi(size);
  279. break;
  280. #ifndef NOFLOAT
  281. case FLOAT: /* thought they were converted??? */
  282. case DOUBLE:
  283. C_cmf(size);
  284. break;
  285. #endif NOFLOAT
  286. case POINTER:
  287. C_cmp();
  288. break;
  289. case ENUM:
  290. C_cmi(size);
  291. break;
  292. default:
  293. CRASH();
  294. }
  295. if (true_label != 0) {
  296. compare(oper, true_label);
  297. C_bra(false_label);
  298. }
  299. else {
  300. label l_true = text_label();
  301. label l_end = text_label();
  302. compare(oper, l_true);
  303. C_loc((arith)0);
  304. C_bra(l_end);
  305. C_df_ilb(l_true);
  306. C_loc((arith)1);
  307. C_df_ilb(l_end);
  308. }
  309. }
  310. break;
  311. case '&':
  312. case '|':
  313. case '^':
  314. /* both operands should have type int */
  315. EVAL(leftop, RVAL, code, NO_LABEL, NO_LABEL);
  316. EVAL(rightop, RVAL, code, NO_LABEL, NO_LABEL);
  317. if (gencode) {
  318. arith size = tp->tp_size;
  319. if (size < word_size)
  320. size = word_size;
  321. switch (oper) {
  322. case '&':
  323. C_and(size);
  324. break;
  325. case '|':
  326. C_ior(size);
  327. break;
  328. case '^':
  329. C_xor(size);
  330. break;
  331. }
  332. }
  333. break;
  334. case '=':
  335. #ifndef NOBITFIELD
  336. if (leftop->ex_type->tp_fund == FIELD) {
  337. /* assignment to bitfield variable
  338. */
  339. eval_field(expr, code);
  340. break;
  341. }
  342. #endif NOBITFIELD
  343. EVAL(rightop, RVAL, TRUE, NO_LABEL, NO_LABEL);
  344. if (gencode)
  345. C_dup(ATW(tp->tp_size));
  346. if (leftop->ex_class != Value) {
  347. EVAL(leftop, LVAL, TRUE, NO_LABEL, NO_LABEL);
  348. store_block(tp->tp_size, tp->tp_align);
  349. }
  350. else
  351. store_val(
  352. &(leftop->ex_object.ex_value),
  353. leftop->ex_type
  354. );
  355. break;
  356. case PLUSAB:
  357. case MINAB:
  358. case TIMESAB:
  359. case DIVAB:
  360. case MODAB:
  361. case LEFTAB:
  362. case RIGHTAB:
  363. case ANDAB:
  364. case XORAB:
  365. case ORAB:
  366. {
  367. arith old_offset;
  368. arith tmpvar = tmp_pointer_var(&old_offset);
  369. #ifndef NOBITFIELD
  370. if (leftop->ex_type->tp_fund == FIELD) {
  371. eval_field(expr, code);
  372. break;
  373. }
  374. #endif NOBITFIELD
  375. if (leftop->ex_class != Value) {
  376. EVAL(leftop, LVAL, TRUE, NO_LABEL, NO_LABEL);
  377. C_lal(tmpvar);
  378. C_sti(pointer_size);
  379. C_lal(tmpvar);
  380. C_loi(pointer_size);
  381. C_loi(leftop->ex_type->tp_size);
  382. }
  383. else {
  384. load_val(leftop, RVAL);
  385. }
  386. conversion(leftop->ex_type, tp);
  387. EVAL(rightop, RVAL, TRUE, NO_LABEL, NO_LABEL);
  388. assop(tp, oper);
  389. conversion(tp, leftop->ex_type);
  390. if (gencode)
  391. C_dup(toword(leftop->ex_type->tp_size));
  392. if (leftop->ex_class != Value) {
  393. C_lal(tmpvar);
  394. C_loi(pointer_size);
  395. C_sti(leftop->ex_type->tp_size);
  396. free_tmp_var(old_offset);
  397. }
  398. else {
  399. store_val(
  400. &(leftop->ex_object.ex_value),
  401. leftop->ex_type
  402. );
  403. }
  404. if (gencode)
  405. conversion(leftop->ex_type, expr->ex_type);
  406. break;
  407. }
  408. case '(':
  409. {
  410. register struct expr *expr;
  411. arith ParSize = (arith)0;
  412. if (expr = rightop) {
  413. /* function call with parameters*/
  414. while ( expr->ex_class == Oper &&
  415. expr->OP_OPER == PARCOMMA
  416. ) {
  417. EVAL(expr->OP_RIGHT, RVAL, TRUE,
  418. NO_LABEL, NO_LABEL);
  419. ParSize +=
  420. ATW(expr->ex_type->tp_size);
  421. expr = expr->OP_LEFT;
  422. }
  423. EVAL(expr, RVAL, TRUE, NO_LABEL, NO_LABEL);
  424. ParSize += ATW(expr->ex_type->tp_size);
  425. }
  426. if ( leftop->ex_class == Value
  427. && leftop->VL_CLASS == Name
  428. ) {
  429. /* just an example:
  430. main() { (*((int (*)())0))(); }
  431. */
  432. C_cal(leftop->VL_IDF->id_text);
  433. #ifdef DATAFLOW
  434. { extern char options[];
  435. if (options['d'])
  436. DfaCallFunction(
  437. leftop->VL_IDF->id_text
  438. );
  439. }
  440. #endif DATAFLOW
  441. }
  442. else {
  443. EVAL(leftop, LVAL, TRUE, NO_LABEL, NO_LABEL);
  444. C_cai();
  445. }
  446. /* remove parameters from stack */
  447. if (ParSize > (arith)0)
  448. C_asp(ParSize);
  449. if (!gencode)
  450. break;
  451. if (is_struct_or_union(tp->tp_fund)) {
  452. C_lfr(pointer_size);
  453. load_block(tp->tp_size, tp->tp_align);
  454. }
  455. else
  456. C_lfr(ATW(tp->tp_size));
  457. break;
  458. }
  459. case '.':
  460. EVAL(leftop, LVAL, code, NO_LABEL, NO_LABEL);
  461. ASSERT(is_cp_cst(rightop));
  462. if (gencode)
  463. C_adp(rightop->VL_VALUE);
  464. break;
  465. case ARROW:
  466. EVAL(leftop, RVAL, code, NO_LABEL, NO_LABEL);
  467. ASSERT(is_cp_cst(rightop));
  468. if (gencode)
  469. C_adp(rightop->VL_VALUE);
  470. break;
  471. case ',':
  472. EVAL(leftop, RVAL, FALSE, NO_LABEL, NO_LABEL);
  473. EVAL(rightop, RVAL, code, NO_LABEL, NO_LABEL);
  474. break;
  475. case '~':
  476. EVAL(rightop, RVAL, code, NO_LABEL, NO_LABEL);
  477. if (gencode)
  478. C_com(tp->tp_size);
  479. break;
  480. case POSTINCR:
  481. case POSTDECR:
  482. case PLUSPLUS:
  483. case MINMIN:
  484. {
  485. arith old_offset, tmp;
  486. arith esize = tp->tp_size;
  487. #ifndef NOBITFIELD
  488. if (leftop->ex_type->tp_fund == FIELD) {
  489. eval_field(expr, code);
  490. break;
  491. }
  492. #endif NOBITFIELD
  493. if (leftop->ex_class != Value) {
  494. tmp = tmp_pointer_var(&old_offset);
  495. EVAL(leftop, LVAL, TRUE, NO_LABEL, NO_LABEL);
  496. C_dup(pointer_size);
  497. C_lal(tmp);
  498. C_sti(pointer_size);
  499. C_loi(tp->tp_size);
  500. }
  501. else
  502. load_val(leftop, RVAL);
  503. /* We made the choice to put this stuff here
  504. and not to put the conversion in the expression
  505. tree because this conversion is EM dependent
  506. and not described in C
  507. */
  508. if (esize < word_size) {
  509. conversion(tp, word_type);
  510. esize = word_size;
  511. }
  512. if (gencode && (oper == POSTINCR || oper == POSTDECR))
  513. C_dup(esize);
  514. EVAL(rightop, RVAL, TRUE, NO_LABEL, NO_LABEL);
  515. assop(tp, oper);
  516. if (gencode && (oper == PLUSPLUS || oper == MINMIN))
  517. C_dup(esize);
  518. if (tp->tp_size < word_size)
  519. conversion(word_type, tp);
  520. if (leftop->ex_class != Value) {
  521. C_lal(tmp); /* always init'd */
  522. C_loi(pointer_size);
  523. C_sti(tp->tp_size);
  524. free_tmp_var(old_offset);
  525. }
  526. else
  527. store_val(
  528. &(leftop->ex_object.ex_value),
  529. leftop->ex_type
  530. );
  531. break;
  532. }
  533. case '?': /* must be followed by ':' */
  534. {
  535. label l_true = text_label();
  536. label l_false = text_label();
  537. label l_end = text_label();
  538. EVAL(leftop, RVAL, TRUE, l_true, l_false);
  539. C_df_ilb(l_true);
  540. EVAL(rightop->OP_LEFT, RVAL, code, NO_LABEL, NO_LABEL);
  541. C_bra(l_end);
  542. C_df_ilb(l_false);
  543. EVAL(rightop->OP_RIGHT, RVAL, code, NO_LABEL, NO_LABEL);
  544. C_df_ilb(l_end);
  545. break;
  546. }
  547. case AND:
  548. if (true_label == 0) {
  549. label l_true = text_label();
  550. label l_false = text_label();
  551. label l_maybe = text_label();
  552. label l_end = text_label();
  553. EVAL(leftop, RVAL, TRUE, l_maybe, l_false);
  554. C_df_ilb(l_maybe);
  555. if (gencode) {
  556. EVAL(rightop, RVAL, TRUE,
  557. l_true, l_false);
  558. C_df_ilb(l_true);
  559. C_loc((arith)1);
  560. C_bra(l_end);
  561. C_df_ilb(l_false);
  562. C_loc((arith)0);
  563. C_df_ilb(l_end);
  564. }
  565. else {
  566. EVAL(rightop, RVAL, FALSE, l_false,
  567. l_false);
  568. C_df_ilb(l_false);
  569. }
  570. }
  571. else {
  572. label l_maybe = text_label();
  573. EVAL(leftop, RVAL, TRUE, l_maybe, false_label);
  574. C_df_ilb(l_maybe);
  575. EVAL(rightop, RVAL, code, true_label,
  576. false_label);
  577. }
  578. break;
  579. case OR:
  580. if (true_label == 0) {
  581. label l_true = text_label();
  582. label l_false = text_label();
  583. label l_maybe = text_label();
  584. label l_end = text_label();
  585. EVAL(leftop, RVAL, TRUE, l_true, l_maybe);
  586. C_df_ilb(l_maybe);
  587. if (gencode) {
  588. EVAL(rightop, RVAL, TRUE,
  589. l_true, l_false);
  590. C_df_ilb(l_false);
  591. C_loc((arith)0);
  592. C_bra(l_end);
  593. C_df_ilb(l_true);
  594. C_loc((arith)1);
  595. C_df_ilb(l_end);
  596. }
  597. else {
  598. EVAL(rightop, RVAL, FALSE, l_true,
  599. l_true);
  600. C_df_ilb(l_true);
  601. }
  602. }
  603. else {
  604. label l_maybe = text_label();
  605. EVAL(leftop, RVAL, TRUE, true_label, l_maybe);
  606. C_df_ilb(l_maybe);
  607. EVAL(rightop, RVAL, code, true_label,
  608. false_label);
  609. }
  610. break;
  611. case '!':
  612. if (true_label == 0) {
  613. if (gencode) {
  614. label l_true = text_label();
  615. label l_false = text_label();
  616. label l_end = text_label();
  617. EVAL(rightop, RVAL, TRUE,
  618. l_false, l_true);
  619. C_df_ilb(l_false);
  620. C_loc((arith)0);
  621. C_bra(l_end);
  622. C_df_ilb(l_true);
  623. C_loc((arith)1);
  624. C_df_ilb(l_end);
  625. }
  626. else
  627. EVAL(rightop, RVAL, FALSE,
  628. NO_LABEL, NO_LABEL);
  629. }
  630. else
  631. EVAL(rightop, RVAL, code, false_label,
  632. true_label);
  633. break;
  634. case INT2INT:
  635. #ifndef NOFLOAT
  636. case INT2FLOAT:
  637. case FLOAT2INT:
  638. case FLOAT2FLOAT:
  639. #endif NOFLOAT
  640. EVAL(rightop, RVAL, code, NO_LABEL, NO_LABEL);
  641. if (gencode)
  642. conversion(rightop->ex_type, leftop->ex_type);
  643. break;
  644. default:
  645. crash("(EVAL) bad operator %s\n", symbol2str(oper));
  646. }
  647. /* If the rvalue of the expression is required but
  648. only its lvalue is evaluated, its rvalue is
  649. loaded by the following statements:
  650. */
  651. if (gencode && val == RVAL && expr->ex_lvalue == 1)
  652. load_block(expr->ex_type->tp_size,
  653. expr->ex_type->tp_align);
  654. break;
  655. }
  656. case Type:
  657. default:
  658. crash("(EVAL) bad expression class");
  659. }
  660. }
  661. /* compare() serves as an auxiliary function of EVAL */
  662. compare(relop, lbl)
  663. int relop;
  664. label lbl;
  665. {
  666. switch (relop) {
  667. case '<':
  668. C_zlt(lbl);
  669. break;
  670. case LESSEQ:
  671. C_zle(lbl);
  672. break;
  673. case '>':
  674. C_zgt(lbl);
  675. break;
  676. case GREATEREQ:
  677. C_zge(lbl);
  678. break;
  679. case EQUAL:
  680. C_zeq(lbl);
  681. break;
  682. case NOTEQUAL:
  683. C_zne(lbl);
  684. break;
  685. default:
  686. CRASH();
  687. }
  688. }
  689. /* assop() generates the opcode of an assignment operators op= */
  690. assop(type, oper)
  691. struct type *type;
  692. int oper;
  693. {
  694. register arith size = type->tp_size;
  695. register uns = type->tp_unsigned;
  696. if (size < word_size)
  697. size = word_size;
  698. switch (type->tp_fund) {
  699. case CHAR:
  700. case SHORT:
  701. case INT:
  702. case LONG:
  703. case ENUM:
  704. switch (oper) {
  705. case PLUSAB:
  706. case PLUSPLUS:
  707. case POSTINCR:
  708. if (uns)
  709. C_adu(size);
  710. else
  711. C_adi(size);
  712. break;
  713. case MINAB:
  714. case MINMIN:
  715. case POSTDECR:
  716. if (uns)
  717. C_sbu(size);
  718. else
  719. C_sbi(size);
  720. break;
  721. case TIMESAB:
  722. if (uns)
  723. C_mlu(size);
  724. else
  725. C_mli(size);
  726. break;
  727. case DIVAB:
  728. if (uns)
  729. C_dvu(size);
  730. else
  731. C_dvi(size);
  732. break;
  733. case MODAB:
  734. if (uns)
  735. C_rmu(size);
  736. else
  737. C_rmi(size);
  738. break;
  739. case LEFTAB:
  740. if (uns)
  741. C_slu(size);
  742. else
  743. C_sli(size);
  744. break;
  745. case RIGHTAB:
  746. if (uns)
  747. C_sru(size);
  748. else
  749. C_sri(size);
  750. break;
  751. case ANDAB:
  752. C_and(size);
  753. break;
  754. case XORAB:
  755. C_xor(size);
  756. break;
  757. case ORAB:
  758. C_ior(size);
  759. break;
  760. }
  761. break;
  762. #ifndef NOFLOAT
  763. case FLOAT:
  764. case DOUBLE:
  765. switch (oper) {
  766. case PLUSAB:
  767. case PLUSPLUS:
  768. case POSTINCR:
  769. C_adf(size);
  770. break;
  771. case MINAB:
  772. case MINMIN:
  773. case POSTDECR:
  774. C_sbf(size);
  775. break;
  776. case TIMESAB:
  777. C_mlf(size);
  778. break;
  779. case DIVAB:
  780. C_dvf(size);
  781. break;
  782. }
  783. break;
  784. #endif NOFLOAT
  785. case POINTER:
  786. if (oper == MINAB || oper == MINMIN || oper == POSTDECR)
  787. C_ngi(size);
  788. C_ads(size);
  789. break;
  790. case ERRONEOUS:
  791. break;
  792. default:
  793. crash("(assop) bad type %s\n", symbol2str(type->tp_fund));
  794. }
  795. }
  796. /* tmp_pointer_var() returns the EM address of a new temporary
  797. pointer variable needed at increment, decrement and assignment
  798. operations to store the address of some variable or lvalue-expression.
  799. */
  800. arith
  801. tmp_pointer_var(oldoffset)
  802. arith *oldoffset; /* previous allocated address */
  803. {
  804. struct stack_level *stl = local_level;
  805. *oldoffset = stl->sl_local_offset;
  806. stl->sl_local_offset =
  807. - align(-stl->sl_local_offset + pointer_size, pointer_align);
  808. if (stl->sl_local_offset < stl->sl_max_block)
  809. stl->sl_max_block = stl->sl_local_offset;
  810. return stl->sl_local_offset;
  811. }
  812. /* free_tmp_var() returns the address allocated by tmp_pointer_var()
  813. and resets the last allocated address.
  814. */
  815. free_tmp_var(oldoffset)
  816. arith oldoffset;
  817. {
  818. local_level->sl_local_offset = oldoffset;
  819. }
  820. /* store_val() generates code for a store operation.
  821. There are four ways of storing data:
  822. - into a global variable
  823. - into an automatic local variable
  824. - into a local static variable
  825. - absolute addressing
  826. */
  827. store_val(vl, tp)
  828. register struct value *vl;
  829. struct type *tp;
  830. {
  831. arith size = tp->tp_size;
  832. int tpalign = tp->tp_align;
  833. int al_on_word;
  834. register int inword;
  835. register int indword;
  836. arith val = vl->vl_value;
  837. if (vl->vl_class == Const) { /* absolute addressing */
  838. load_cst(val, pointer_size);
  839. store_block(size, tpalign);
  840. return;
  841. }
  842. al_on_word = (tpalign % word_align == 0);
  843. if (!(inword = (size == word_size && al_on_word)))
  844. indword = (size == dword_size && al_on_word);
  845. if (vl->vl_class == Name) {
  846. register struct idf *id = vl->vl_data.vl_idf;
  847. register struct def *df = id->id_def;
  848. if (df->df_level == L_GLOBAL) {
  849. if (inword)
  850. C_ste_dnam(id->id_text, val);
  851. else
  852. if (indword)
  853. C_sde_dnam(id->id_text, val);
  854. else {
  855. C_lae_dnam(id->id_text, val);
  856. store_block(size, tpalign);
  857. }
  858. }
  859. else {
  860. ASSERT(df->df_sc != STATIC);
  861. if (inword)
  862. C_stl(df->df_address + val);
  863. else
  864. if (indword)
  865. C_sdl(df->df_address + val);
  866. else {
  867. C_lal(df->df_address + val);
  868. store_block(size, tpalign);
  869. df->df_register = REG_NONE;
  870. }
  871. }
  872. }
  873. else {
  874. label dlb = vl->vl_data.vl_lbl;
  875. ASSERT(vl->vl_class == Label);
  876. if (inword)
  877. C_ste_dlb(dlb, val);
  878. else
  879. if (indword)
  880. C_sde_dlb(dlb, val);
  881. else {
  882. C_lae_dlb(dlb, val);
  883. store_block(size, tpalign);
  884. }
  885. }
  886. }
  887. /* load_val() generates code for stacking a certain value (from ex),
  888. which can be obtained in one of the following ways:
  889. - value from absolute addressed memory
  890. - constant value
  891. - function result
  892. - global variable
  893. - static variable
  894. - local variable
  895. */
  896. load_val(expr, val)
  897. struct expr *expr; /* expression containing the value */
  898. int val; /* generate either LVAL or RVAL */
  899. {
  900. register struct type *tp = expr->ex_type;
  901. register int rvalue = (val == RVAL && expr->ex_lvalue != 0);
  902. register arith size = tp->tp_size;
  903. register int tpalign = tp->tp_align;
  904. register int al_on_word;
  905. register int inword, indword;
  906. register arith val = expr->VL_VALUE;
  907. if (expr->VL_CLASS == Const) {
  908. if (rvalue) { /* absolute addressing */
  909. load_cst(val, pointer_size);
  910. load_block(size, tpalign);
  911. }
  912. else /* integer, unsigned, long, enum etc */
  913. load_cst(val, size);
  914. return;
  915. }
  916. if (rvalue) {
  917. al_on_word = (tpalign % word_align == 0);
  918. if (!(inword = (size == word_size && al_on_word)))
  919. indword = (size == dword_size && al_on_word);
  920. }
  921. if (expr->VL_CLASS == Label) {
  922. if (rvalue) {
  923. if (inword)
  924. C_loe_dlb(expr->VL_LBL, val);
  925. else
  926. if (indword)
  927. C_lde_dlb(expr->VL_LBL, val);
  928. else {
  929. C_lae_dlb(expr->VL_LBL, val);
  930. load_block(size, tpalign);
  931. }
  932. }
  933. else {
  934. C_lae_dlb(expr->VL_LBL, (arith)0);
  935. C_adp(val);
  936. }
  937. }
  938. else {
  939. register struct idf *id = expr->VL_IDF;
  940. register struct def *df;
  941. ASSERT(expr->VL_CLASS == Name);
  942. if ((df = id->id_def)->df_type->tp_fund == FUNCTION)
  943. /* the previous statement tried to catch a function
  944. identifier, which may be cast to a pointer to a
  945. function.
  946. ASSERT(!(rvalue)); ???
  947. */
  948. C_lpi(id->id_text);
  949. else
  950. if (df->df_level == L_GLOBAL) {
  951. if (rvalue) {
  952. if (inword)
  953. C_loe_dnam(id->id_text, val);
  954. else
  955. if (indword)
  956. C_lde_dnam(id->id_text, val);
  957. else {
  958. C_lae_dnam(id->id_text, val);
  959. load_block(size, tpalign);
  960. }
  961. }
  962. else {
  963. C_lae_dnam(id->id_text, (arith)0);
  964. C_adp(val);
  965. }
  966. }
  967. else {
  968. ASSERT(df->df_sc != STATIC);
  969. if (rvalue) {
  970. if (inword)
  971. C_lol(df->df_address + val);
  972. else
  973. if (indword)
  974. C_ldl(df->df_address + val);
  975. else {
  976. C_lal(df->df_address + val);
  977. load_block(size, tpalign);
  978. df->df_register = REG_NONE;
  979. }
  980. }
  981. else {
  982. C_lal(df->df_address);
  983. C_adp(val);
  984. df->df_register = REG_NONE;
  985. }
  986. }
  987. }
  988. }
  989. load_cst(val, siz)
  990. arith val, siz;
  991. {
  992. if (siz <= word_size)
  993. C_loc(val);
  994. else
  995. if (siz == dword_size)
  996. C_ldc(val);
  997. else {
  998. label datlab;
  999. C_df_dlb(datlab = data_label());
  1000. C_rom_icon(long2str((long)val, 10), siz);
  1001. C_lae_dlb(datlab, (arith)0);
  1002. C_loi(siz);
  1003. }
  1004. }