eval.c 22 KB

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