eval.c 21 KB

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