eval.c 20 KB

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