hlicode.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * File: hlIcode.c
  3. * Purpose: High-level icode routines
  4. * Date: September-October 1993
  5. * (C) Cristina Cifuentes
  6. */
  7. #include <cassert>
  8. #include <string.h>
  9. #include <string>
  10. #include <sstream>
  11. #include "dcc.h"
  12. using namespace std;
  13. #define ICODE_DELTA 25
  14. /* Masks off bits set by duReg[] */
  15. dword maskDuReg[] = { 0x00,
  16. 0xFEEFFE, 0xFDDFFD, 0xFBB00B, 0xF77007, /* word regs */
  17. 0xFFFFEF, 0xFFFFDF, 0xFFFFBF, 0xFFFF7F,
  18. 0xFFFEFF, 0xFFFDFF, 0xFFFBFF, 0xFFF7FF, /* seg regs */
  19. 0xFFEFFF, 0xFFDFFF, 0xFFBFFF, 0xFF7FFF, /* byte regs */
  20. 0xFEFFFF, 0xFDFFFF, 0xFBFFFF, 0xF7FFFF,
  21. 0xEFFFFF, /* tmp reg */
  22. 0xFFFFB7, 0xFFFF77, 0xFFFF9F, 0xFFFF5F, /* index regs */
  23. 0xFFFFBF, 0xFFFF7F, 0xFFFFDF, 0xFFFFF7 };
  24. static char buf[lineSize]; /* Line buffer for hl icode output */
  25. /* Places the new HLI_ASSIGN high-level operand in the high-level icode array */
  26. void ICODE::setAsgn(COND_EXPR *lhs, COND_EXPR *rhs)
  27. {
  28. type = HIGH_LEVEL;
  29. ic.hl.opcode = HLI_ASSIGN;
  30. assert(ic.hl.oper.asgn.lhs==0); //prevent memory leaks
  31. assert(ic.hl.oper.asgn.rhs==0); //prevent memory leaks
  32. ic.hl.oper.asgn.lhs = lhs;
  33. ic.hl.oper.asgn.rhs = rhs;
  34. }
  35. void ICODE::checkHlCall()
  36. {
  37. //assert((ic.ll.immed.proc.cb != 0)||ic.ll.immed.proc.proc!=0);
  38. }
  39. /* Places the new HLI_CALL high-level operand in the high-level icode array */
  40. void ICODE::newCallHl()
  41. {
  42. type = HIGH_LEVEL;
  43. ic.hl.opcode = HLI_CALL;
  44. ic.hl.oper.call.proc = ic.ll.src.proc.proc;
  45. ic.hl.oper.call.args = new STKFRAME;
  46. if (ic.ll.src.proc.cb != 0)
  47. ic.hl.oper.call.args->cb = ic.ll.src.proc.cb;
  48. else if(ic.hl.oper.call.proc)
  49. ic.hl.oper.call.args->cb =ic.hl.oper.call.proc->cbParam;
  50. else
  51. {
  52. printf("Function with no cb set, and no valid oper.call.proc , probaby indirect call\n");
  53. ic.hl.oper.call.args->cb = 0;
  54. }
  55. }
  56. /* Places the new HLI_POP/HLI_PUSH/HLI_RET high-level operand in the high-level icode
  57. * array */
  58. void ICODE::setUnary(hlIcode op, COND_EXPR *exp)
  59. {
  60. assert(ic.hl.oper.exp==0);
  61. type = HIGH_LEVEL;
  62. ic.hl.opcode = op;
  63. ic.hl.oper.exp = exp;
  64. }
  65. /* Places the new HLI_JCOND high-level operand in the high-level icode array */
  66. void ICODE::setJCond(COND_EXPR *cexp)
  67. {
  68. assert(ic.hl.oper.exp==0);
  69. type = HIGH_LEVEL;
  70. ic.hl.opcode = HLI_JCOND;
  71. ic.hl.oper.exp = cexp;
  72. }
  73. /* Sets the invalid field to TRUE as this low-level icode is no longer valid,
  74. * it has been replaced by a high-level icode. */
  75. void ICODE ::invalidate()
  76. {
  77. invalid = TRUE;
  78. }
  79. /* Removes the defined register regi from the lhs subtree. If all registers
  80. * of this instruction are unused, the instruction is invalidated (ie.
  81. * removed) */
  82. boolT ICODE::removeDefRegi (byte regi, Int thisDefIdx, LOCAL_ID *locId)
  83. {
  84. Int numDefs;
  85. numDefs = du1.numRegsDef;
  86. if (numDefs == thisDefIdx)
  87. for ( ; numDefs > 0; numDefs--)
  88. {
  89. if ((du1.idx[numDefs-1][0] != 0)||(du.lastDefRegi))
  90. break;
  91. }
  92. if (numDefs == 0)
  93. {
  94. invalidate();
  95. return true;
  96. }
  97. else
  98. {
  99. switch (ic.hl.opcode) {
  100. case HLI_ASSIGN:
  101. removeRegFromLong (regi, locId,ic.hl.oper.asgn.lhs);
  102. du1.numRegsDef--;
  103. du.def &= maskDuReg[regi];
  104. break;
  105. case HLI_POP:
  106. case HLI_PUSH:
  107. removeRegFromLong (regi, locId, ic.hl.oper.exp);
  108. du1.numRegsDef--;
  109. du.def &= maskDuReg[regi];
  110. break;
  111. }
  112. return false;
  113. }
  114. }
  115. /* Translates LOW_LEVEL icodes to HIGH_LEVEL icodes - 1st stage.
  116. * Note: this process should be done before data flow analysis, which
  117. * refines the HIGH_LEVEL icodes. */
  118. void Function::highLevelGen()
  119. { Int i, /* idx into icode array */
  120. numIcode; /* number of icode instructions */
  121. ICODE * pIcode; /* ptr to current icode node */
  122. COND_EXPR *lhs, *rhs; /* left- and right-hand side of expression */
  123. flags32 flg; /* icode flags */
  124. numIcode = Icode.size();
  125. for (i = 0; i < numIcode; i++)
  126. {
  127. pIcode = Icode.GetIcode(i);
  128. if ((pIcode->ic.ll.flg & NOT_HLL) == NOT_HLL)
  129. pIcode->invalidate();
  130. if ((pIcode->type == LOW_LEVEL) && (pIcode->invalid == FALSE))
  131. {
  132. flg = pIcode->ic.ll.flg;
  133. if ((flg & IM_OPS) != IM_OPS) /* not processing IM_OPS yet */
  134. if ((flg & NO_OPS) != NO_OPS) /* if there are opers */
  135. {
  136. if ((flg & NO_SRC) != NO_SRC) /* if there is src op */
  137. rhs = COND_EXPR::id (*pIcode, SRC, this, i, *pIcode, NONE);
  138. lhs = COND_EXPR::id (*pIcode, DST, this, i, *pIcode, NONE);
  139. }
  140. switch (pIcode->ic.ll.opcode)
  141. {
  142. case iADD:
  143. rhs = COND_EXPR::boolOp (lhs, rhs, ADD);
  144. pIcode->setAsgn(lhs, rhs);
  145. break;
  146. case iAND:
  147. rhs = COND_EXPR::boolOp (lhs, rhs, AND);
  148. pIcode->setAsgn(lhs, rhs);
  149. break;
  150. case iCALL:
  151. case iCALLF:
  152. pIcode->checkHlCall();
  153. pIcode->newCallHl();
  154. break;
  155. case iDEC:
  156. rhs = COND_EXPR::idKte (1, 2);
  157. rhs = COND_EXPR::boolOp (lhs, rhs, SUB);
  158. pIcode->setAsgn(lhs, rhs);
  159. break;
  160. case iDIV:
  161. case iIDIV:/* should be signed div */
  162. rhs = COND_EXPR::boolOp (lhs, rhs, DIV);
  163. if (pIcode->ic.ll.flg & B)
  164. {
  165. lhs = COND_EXPR::idReg (rAL, 0, &localId);
  166. pIcode->setRegDU( rAL, eDEF);
  167. }
  168. else
  169. {
  170. lhs = COND_EXPR::idReg (rAX, 0, &localId);
  171. pIcode->setRegDU( rAX, eDEF);
  172. }
  173. pIcode->setAsgn(lhs, rhs);
  174. break;
  175. case iIMUL:
  176. rhs = COND_EXPR::boolOp (lhs, rhs, MUL);
  177. lhs = COND_EXPR::id (*pIcode, LHS_OP, this, i, *pIcode, NONE);
  178. pIcode->setAsgn(lhs, rhs);
  179. break;
  180. case iINC:
  181. rhs = COND_EXPR::idKte (1, 2);
  182. rhs = COND_EXPR::boolOp (lhs, rhs, ADD);
  183. pIcode->setAsgn(lhs, rhs);
  184. break;
  185. case iLEA: rhs = COND_EXPR::unary (ADDRESSOF, rhs);
  186. pIcode->setAsgn(lhs, rhs);
  187. break;
  188. case iMOD: rhs = COND_EXPR::boolOp (lhs, rhs, MOD);
  189. if (pIcode->ic.ll.flg & B)
  190. {
  191. lhs = COND_EXPR::idReg (rAH, 0, &localId);
  192. pIcode->setRegDU( rAH, eDEF);
  193. }
  194. else
  195. {
  196. lhs = COND_EXPR::idReg (rDX, 0, &localId);
  197. pIcode->setRegDU( rDX, eDEF);
  198. }
  199. pIcode->setAsgn(lhs, rhs);
  200. break;
  201. case iMOV: pIcode->setAsgn(lhs, rhs);
  202. break;
  203. case iMUL:
  204. rhs = COND_EXPR::boolOp (lhs, rhs, MUL);
  205. lhs = COND_EXPR::id (*pIcode, LHS_OP, this, i, *pIcode, NONE);
  206. pIcode->setAsgn(lhs, rhs);
  207. break;
  208. case iNEG: rhs = COND_EXPR::unary (NEGATION, lhs);
  209. pIcode->setAsgn(lhs, rhs);
  210. break;
  211. case iNOT:
  212. rhs = COND_EXPR::boolOp (NULL, rhs, NOT);
  213. pIcode->setAsgn(lhs, rhs);
  214. break;
  215. case iOR:
  216. rhs = COND_EXPR::boolOp (lhs, rhs, OR);
  217. pIcode->setAsgn(lhs, rhs);
  218. break;
  219. case iPOP: pIcode->setUnary(HLI_POP, lhs);
  220. break;
  221. case iPUSH: pIcode->setUnary(HLI_PUSH, lhs);
  222. break;
  223. case iRET:
  224. case iRETF: pIcode->setUnary(HLI_RET, NULL);
  225. break;
  226. case iSHL: rhs = COND_EXPR::boolOp (lhs, rhs, SHL);
  227. pIcode->setAsgn(lhs, rhs);
  228. break;
  229. case iSAR: /* signed */
  230. case iSHR:
  231. rhs = COND_EXPR::boolOp (lhs, rhs, SHR); /* unsigned*/
  232. pIcode->setAsgn(lhs, rhs);
  233. break;
  234. case iSIGNEX: pIcode->setAsgn(lhs, rhs);
  235. break;
  236. case iSUB: rhs = COND_EXPR::boolOp (lhs, rhs, SUB);
  237. pIcode->setAsgn(lhs, rhs);
  238. break;
  239. case iXCHG:
  240. break;
  241. case iXOR:
  242. rhs = COND_EXPR::boolOp (lhs, rhs, XOR);
  243. pIcode->setAsgn(lhs, rhs);
  244. break;
  245. }
  246. }
  247. }
  248. }
  249. /* Modifies the given conditional operator to its inverse. This is used
  250. * in if..then[..else] statements, to reflect the condition that takes the
  251. * then part. */
  252. void inverseCondOp (COND_EXPR **exp)
  253. {
  254. static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL,
  255. LESS_EQUAL, LESS, DUMMY,DUMMY,DUMMY,DUMMY,
  256. DUMMY, DUMMY, DUMMY, DUMMY, DUMMY, DUMMY,
  257. DUMMY, DBL_OR, DBL_AND};
  258. if (*exp == NULL)
  259. return;
  260. if ((*exp)->type == BOOLEAN_OP)
  261. {
  262. switch ((*exp)->expr.boolExpr.op)
  263. {
  264. case LESS_EQUAL: case LESS: case EQUAL:
  265. case NOT_EQUAL: case GREATER: case GREATER_EQUAL:
  266. (*exp)->expr.boolExpr.op = invCondOp[(*exp)->expr.boolExpr.op];
  267. break;
  268. case AND: case OR: case XOR: case NOT: case ADD:
  269. case SUB: case MUL: case DIV: case SHR: case SHL: case MOD:
  270. *exp = COND_EXPR::unary (NEGATION, *exp);
  271. break;
  272. case DBL_AND: case DBL_OR:
  273. (*exp)->expr.boolExpr.op = invCondOp[(*exp)->expr.boolExpr.op];
  274. inverseCondOp (&(*exp)->expr.boolExpr.lhs);
  275. inverseCondOp (&(*exp)->expr.boolExpr.rhs);
  276. break;
  277. } /* eos */
  278. }
  279. else if ((*exp)->type == NEGATION) //TODO: memleak here
  280. *exp = (*exp)->expr.unaryExp;
  281. /* other types are left unmodified */
  282. }
  283. /* Returns the string that represents the procedure call of tproc (ie. with
  284. * actual parameters) */
  285. std::string writeCall (Function * tproc, STKFRAME * args, Function * pproc, Int *numLoc)
  286. {
  287. Int i; /* counter of # arguments */
  288. string condExp;
  289. ostringstream s;
  290. s<<tproc->name<<" (";
  291. for (i = 0; i < args->sym.size(); i++)
  292. {
  293. s << walkCondExpr (args->sym[i].actual, pproc, numLoc);
  294. if (i < (args->sym.size() - 1))
  295. s << ", ";
  296. }
  297. s << ")";
  298. return s.str();
  299. }
  300. /* Displays the output of a HLI_JCOND icode. */
  301. char *writeJcond (HLTYPE h, Function * pProc, Int *numLoc)
  302. {
  303. memset (buf, ' ', sizeof(buf));
  304. buf[0] = '\0';
  305. strcat (buf, "if ");
  306. inverseCondOp (&h.oper.exp);
  307. std::string e = walkCondExpr (h.oper.exp, pProc, numLoc);
  308. strcat (buf, e.c_str());
  309. strcat (buf, " {\n");
  310. return (buf);
  311. }
  312. /* Displays the inverse output of a HLI_JCOND icode. This is used in the case
  313. * when the THEN clause of an if..then..else is empty. The clause is
  314. * negated and the ELSE clause is used instead. */
  315. char *writeJcondInv (HLTYPE h, Function * pProc, Int *numLoc)
  316. {
  317. memset (buf, ' ', sizeof(buf));
  318. buf[0] = '\0';
  319. strcat (buf, "if ");
  320. std::string e = walkCondExpr (h.oper.exp, pProc, numLoc);
  321. strcat (buf, e.c_str());
  322. strcat (buf, " {\n");
  323. return (buf);
  324. }
  325. /* Returns a string with the contents of the current high-level icode.
  326. * Note: this routine does not output the contens of HLI_JCOND icodes. This is
  327. * done in a separate routine to be able to support the removal of
  328. * empty THEN clauses on an if..then..else. */
  329. char *write1HlIcode (HLTYPE h, Function * pProc, Int *numLoc)
  330. {
  331. std::string e;
  332. memset (buf, ' ', sizeof(buf));
  333. buf[0] = '\0';
  334. switch (h.opcode) {
  335. case HLI_ASSIGN:
  336. e = walkCondExpr (h.oper.asgn.lhs, pProc, numLoc);
  337. strcat (buf, e.c_str());
  338. strcat (buf, " = ");
  339. e = walkCondExpr (h.oper.asgn.rhs, pProc, numLoc);
  340. strcat (buf, e.c_str());
  341. strcat (buf, ";\n");
  342. break;
  343. case HLI_CALL:
  344. e = writeCall (h.oper.call.proc, h.oper.call.args, pProc,
  345. numLoc);
  346. strcat (buf, e.c_str());
  347. strcat (buf, ";\n");
  348. break;
  349. case HLI_RET:
  350. e = walkCondExpr (h.oper.exp, pProc, numLoc);
  351. if (! e.empty())
  352. {
  353. strcat (buf, "return (");
  354. strcat (buf, e.c_str());
  355. strcat (buf, ");\n");
  356. }
  357. break;
  358. case HLI_POP:
  359. strcat (buf, "HLI_POP ");
  360. e = walkCondExpr (h.oper.exp, pProc, numLoc);
  361. strcat (buf, e.c_str());
  362. strcat (buf, "\n");
  363. break;
  364. case HLI_PUSH: strcat (buf, "HLI_PUSH ");
  365. e = walkCondExpr (h.oper.exp, pProc, numLoc);
  366. strcat (buf, e.c_str());
  367. strcat (buf, "\n");
  368. break;
  369. }
  370. return (buf);
  371. }
  372. Int power2 (Int i)
  373. /* Returns the value of 2 to the power of i */
  374. {
  375. if (i == 0)
  376. return (1);
  377. return (2 << (i-1));
  378. }
  379. /* Writes the registers/stack variables that are used and defined by this
  380. * instruction. */
  381. void ICODE::writeDU(Int idx)
  382. {
  383. static char buf[100];
  384. Int i, j;
  385. memset (buf, ' ', sizeof(buf));
  386. buf[0] = '\0';
  387. for (i = 0; i < (INDEXBASE-1); i++)
  388. {
  389. if ((du.def & power2(i)) != 0)
  390. {
  391. strcat (buf, allRegs[i]);
  392. strcat (buf, " ");
  393. }
  394. }
  395. if (buf[0] != '\0')
  396. printf ("Def (reg) = %s\n", buf);
  397. memset (buf, ' ', sizeof(buf));
  398. buf[0] = '\0';
  399. for (i = 0; i < INDEXBASE; i++)
  400. {
  401. if ((du.use & power2(i)) != 0)
  402. {
  403. strcat (buf, allRegs[i]);
  404. strcat (buf, " ");
  405. }
  406. }
  407. if (buf[0] != '\0')
  408. printf ("Use (reg) = %s\n", buf);
  409. /* Print du1 chain */
  410. printf ("# regs defined = %d\n", du1.numRegsDef);
  411. for (i = 0; i < MAX_REGS_DEF; i++)
  412. {
  413. if (du1.idx[i][0] != 0)
  414. {
  415. printf ("%d: du1[%d][] = ", idx, i);
  416. for (j = 0; j < MAX_USES; j++)
  417. {
  418. if (du1.idx[i][j] == 0)
  419. break;
  420. printf ("%d ", du1.idx[i][j]);
  421. }
  422. printf ("\n");
  423. }
  424. }
  425. /* For HLI_CALL, print # parameter bytes */
  426. if (ic.hl.opcode == HLI_CALL)
  427. printf ("# param bytes = %d\n", ic.hl.oper.call.args->cb);
  428. printf ("\n");
  429. }
  430. /* Frees the storage allocated to h->hlIcode */
  431. void freeHlIcode (ICODE * icode, Int numIcodes)
  432. {
  433. Int i;
  434. HLTYPE h;
  435. for (i = 0; i < numIcodes; i++)
  436. {
  437. h = icode[i].ic.hl;
  438. switch (h.opcode)
  439. {
  440. case HLI_ASSIGN:
  441. h.oper.asgn.lhs->release();
  442. h.oper.asgn.rhs->release();
  443. break;
  444. case HLI_POP:
  445. case HLI_PUSH:
  446. case HLI_JCOND:
  447. h.oper.exp->release();
  448. break;
  449. }
  450. }
  451. }