eval.c 22 KB

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