eval.c 22 KB

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