eval.c 21 KB

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