eval.c 21 KB

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