eval.c 22 KB

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