eval.c 22 KB

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