eval.c 22 KB

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