eval.c 21 KB

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