hlicode.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. {
  88. for ( ; numDefs > 0; numDefs--)
  89. {
  90. if ((du1.idx[numDefs-1][0] != 0)||(du.lastDefRegi.any()))
  91. break;
  92. }
  93. }
  94. if (numDefs == 0)
  95. {
  96. invalidate();
  97. return true;
  98. }
  99. switch (ic.hl.opcode)
  100. {
  101. case HLI_ASSIGN:
  102. removeRegFromLong (regi, locId,ic.hl.oper.asgn.lhs);
  103. du1.numRegsDef--;
  104. du.def &= maskDuReg[regi];
  105. break;
  106. case HLI_POP:
  107. case HLI_PUSH:
  108. removeRegFromLong (regi, locId, ic.hl.oper.exp);
  109. du1.numRegsDef--;
  110. du.def &= maskDuReg[regi];
  111. break;
  112. }
  113. return false;
  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. iICODE 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 (iICODE i = Icode.begin(); i!=Icode.end() ; ++i)
  126. {
  127. assert(numIcode==Icode.size());
  128. pIcode = i; //Icode.GetIcode(i)
  129. if ((pIcode->ic.ll.flg & NOT_HLL) == NOT_HLL)
  130. pIcode->invalidate();
  131. if ((pIcode->type == LOW_LEVEL) && (pIcode->invalid == FALSE))
  132. {
  133. flg = pIcode->ic.ll.flg;
  134. if ((flg & IM_OPS) != IM_OPS) /* not processing IM_OPS yet */
  135. if ((flg & NO_OPS) != NO_OPS) /* if there are opers */
  136. {
  137. if ((flg & NO_SRC) != NO_SRC) /* if there is src op */
  138. rhs = COND_EXPR::id (*pIcode, SRC, this, i, *pIcode, NONE);
  139. lhs = COND_EXPR::id (*pIcode, DST, this, i, *pIcode, NONE);
  140. }
  141. switch (pIcode->ic.ll.opcode)
  142. {
  143. case iADD:
  144. rhs = COND_EXPR::boolOp (lhs, rhs, ADD);
  145. pIcode->setAsgn(lhs, rhs);
  146. break;
  147. case iAND:
  148. rhs = COND_EXPR::boolOp (lhs, rhs, AND);
  149. pIcode->setAsgn(lhs, rhs);
  150. break;
  151. case iCALL:
  152. case iCALLF:
  153. pIcode->checkHlCall();
  154. pIcode->newCallHl();
  155. break;
  156. case iDEC:
  157. rhs = COND_EXPR::idKte (1, 2);
  158. rhs = COND_EXPR::boolOp (lhs, rhs, SUB);
  159. pIcode->setAsgn(lhs, rhs);
  160. break;
  161. case iDIV:
  162. case iIDIV:/* should be signed div */
  163. rhs = COND_EXPR::boolOp (lhs, rhs, DIV);
  164. if (pIcode->ic.ll.flg & B)
  165. {
  166. lhs = COND_EXPR::idReg (rAL, 0, &localId);
  167. pIcode->setRegDU( rAL, eDEF);
  168. }
  169. else
  170. {
  171. lhs = COND_EXPR::idReg (rAX, 0, &localId);
  172. pIcode->setRegDU( rAX, eDEF);
  173. }
  174. pIcode->setAsgn(lhs, rhs);
  175. break;
  176. case iIMUL:
  177. rhs = COND_EXPR::boolOp (lhs, rhs, MUL);
  178. lhs = COND_EXPR::id (*pIcode, LHS_OP, this, i, *pIcode, NONE);
  179. pIcode->setAsgn(lhs, rhs);
  180. break;
  181. case iINC:
  182. rhs = COND_EXPR::idKte (1, 2);
  183. rhs = COND_EXPR::boolOp (lhs, rhs, ADD);
  184. pIcode->setAsgn(lhs, rhs);
  185. break;
  186. case iLEA: rhs = COND_EXPR::unary (ADDRESSOF, rhs);
  187. pIcode->setAsgn(lhs, rhs);
  188. break;
  189. case iMOD: rhs = COND_EXPR::boolOp (lhs, rhs, MOD);
  190. if (pIcode->ic.ll.flg & B)
  191. {
  192. lhs = COND_EXPR::idReg (rAH, 0, &localId);
  193. pIcode->setRegDU( rAH, eDEF);
  194. }
  195. else
  196. {
  197. lhs = COND_EXPR::idReg (rDX, 0, &localId);
  198. pIcode->setRegDU( rDX, eDEF);
  199. }
  200. pIcode->setAsgn(lhs, rhs);
  201. break;
  202. case iMOV: pIcode->setAsgn(lhs, rhs);
  203. break;
  204. case iMUL:
  205. rhs = COND_EXPR::boolOp (lhs, rhs, MUL);
  206. lhs = COND_EXPR::id (*pIcode, LHS_OP, this, i, *pIcode, NONE);
  207. pIcode->setAsgn(lhs, rhs);
  208. break;
  209. case iNEG: rhs = COND_EXPR::unary (NEGATION, lhs);
  210. pIcode->setAsgn(lhs, rhs);
  211. break;
  212. case iNOT:
  213. rhs = COND_EXPR::boolOp (NULL, rhs, NOT);
  214. pIcode->setAsgn(lhs, rhs);
  215. break;
  216. case iOR:
  217. rhs = COND_EXPR::boolOp (lhs, rhs, OR);
  218. pIcode->setAsgn(lhs, rhs);
  219. break;
  220. case iPOP: pIcode->setUnary(HLI_POP, lhs);
  221. break;
  222. case iPUSH: pIcode->setUnary(HLI_PUSH, lhs);
  223. break;
  224. case iRET:
  225. case iRETF: pIcode->setUnary(HLI_RET, NULL);
  226. break;
  227. case iSHL: rhs = COND_EXPR::boolOp (lhs, rhs, SHL);
  228. pIcode->setAsgn(lhs, rhs);
  229. break;
  230. case iSAR: /* signed */
  231. case iSHR:
  232. rhs = COND_EXPR::boolOp (lhs, rhs, SHR); /* unsigned*/
  233. pIcode->setAsgn(lhs, rhs);
  234. break;
  235. case iSIGNEX: pIcode->setAsgn(lhs, rhs);
  236. break;
  237. case iSUB: rhs = COND_EXPR::boolOp (lhs, rhs, SUB);
  238. pIcode->setAsgn(lhs, rhs);
  239. break;
  240. case iXCHG:
  241. break;
  242. case iXOR:
  243. rhs = COND_EXPR::boolOp (lhs, rhs, XOR);
  244. pIcode->setAsgn(lhs, rhs);
  245. break;
  246. }
  247. }
  248. }
  249. }
  250. /* Modifies the given conditional operator to its inverse. This is used
  251. * in if..then[..else] statements, to reflect the condition that takes the
  252. * then part. */
  253. void inverseCondOp (COND_EXPR **exp)
  254. {
  255. static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL,
  256. LESS_EQUAL, LESS, DUMMY,DUMMY,DUMMY,DUMMY,
  257. DUMMY, DUMMY, DUMMY, DUMMY, DUMMY, DUMMY,
  258. DUMMY, DBL_OR, DBL_AND};
  259. if (*exp == NULL)
  260. return;
  261. if ((*exp)->type == BOOLEAN_OP)
  262. {
  263. switch ((*exp)->expr.boolExpr.op)
  264. {
  265. case LESS_EQUAL: case LESS: case EQUAL:
  266. case NOT_EQUAL: case GREATER: case GREATER_EQUAL:
  267. (*exp)->expr.boolExpr.op = invCondOp[(*exp)->expr.boolExpr.op];
  268. break;
  269. case AND: case OR: case XOR: case NOT: case ADD:
  270. case SUB: case MUL: case DIV: case SHR: case SHL: case MOD:
  271. *exp = COND_EXPR::unary (NEGATION, *exp);
  272. break;
  273. case DBL_AND: case DBL_OR:
  274. (*exp)->expr.boolExpr.op = invCondOp[(*exp)->expr.boolExpr.op];
  275. inverseCondOp (&(*exp)->expr.boolExpr.lhs);
  276. inverseCondOp (&(*exp)->expr.boolExpr.rhs);
  277. break;
  278. } /* eos */
  279. }
  280. else if ((*exp)->type == NEGATION) //TODO: memleak here
  281. *exp = (*exp)->expr.unaryExp;
  282. /* other types are left unmodified */
  283. }
  284. /* Returns the string that represents the procedure call of tproc (ie. with
  285. * actual parameters) */
  286. std::string writeCall (Function * tproc, STKFRAME * args, Function * pproc, Int *numLoc)
  287. {
  288. Int i; /* counter of # arguments */
  289. string condExp;
  290. ostringstream s;
  291. s<<tproc->name<<" (";
  292. for (i = 0; i < args->sym.size(); i++)
  293. {
  294. s << walkCondExpr (args->sym[i].actual, pproc, numLoc);
  295. if (i < (args->sym.size() - 1))
  296. s << ", ";
  297. }
  298. s << ")";
  299. return s.str();
  300. }
  301. /* Displays the output of a HLI_JCOND icode. */
  302. char *writeJcond (HLTYPE h, Function * pProc, Int *numLoc)
  303. {
  304. memset (buf, ' ', sizeof(buf));
  305. buf[0] = '\0';
  306. strcat (buf, "if ");
  307. inverseCondOp (&h.oper.exp);
  308. std::string e = walkCondExpr (h.oper.exp, pProc, numLoc);
  309. strcat (buf, e.c_str());
  310. strcat (buf, " {\n");
  311. return (buf);
  312. }
  313. /* Displays the inverse output of a HLI_JCOND icode. This is used in the case
  314. * when the THEN clause of an if..then..else is empty. The clause is
  315. * negated and the ELSE clause is used instead. */
  316. char *writeJcondInv (HLTYPE h, Function * pProc, Int *numLoc)
  317. {
  318. memset (buf, ' ', sizeof(buf));
  319. buf[0] = '\0';
  320. strcat (buf, "if ");
  321. std::string e = walkCondExpr (h.oper.exp, pProc, numLoc);
  322. strcat (buf, e.c_str());
  323. strcat (buf, " {\n");
  324. return (buf);
  325. }
  326. /* Returns a string with the contents of the current high-level icode.
  327. * Note: this routine does not output the contens of HLI_JCOND icodes. This is
  328. * done in a separate routine to be able to support the removal of
  329. * empty THEN clauses on an if..then..else. */
  330. char *write1HlIcode (HLTYPE h, Function * pProc, Int *numLoc)
  331. {
  332. std::string e;
  333. memset (buf, ' ', sizeof(buf));
  334. buf[0] = '\0';
  335. switch (h.opcode) {
  336. case HLI_ASSIGN:
  337. e = walkCondExpr (h.oper.asgn.lhs, pProc, numLoc);
  338. strcat (buf, e.c_str());
  339. strcat (buf, " = ");
  340. e = walkCondExpr (h.oper.asgn.rhs, pProc, numLoc);
  341. strcat (buf, e.c_str());
  342. strcat (buf, ";\n");
  343. break;
  344. case HLI_CALL:
  345. e = writeCall (h.oper.call.proc, h.oper.call.args, pProc,
  346. numLoc);
  347. strcat (buf, e.c_str());
  348. strcat (buf, ";\n");
  349. break;
  350. case HLI_RET:
  351. e = walkCondExpr (h.oper.exp, pProc, numLoc);
  352. if (! e.empty())
  353. {
  354. strcat (buf, "return (");
  355. strcat (buf, e.c_str());
  356. strcat (buf, ");\n");
  357. }
  358. break;
  359. case HLI_POP:
  360. strcat (buf, "HLI_POP ");
  361. e = walkCondExpr (h.oper.exp, pProc, numLoc);
  362. strcat (buf, e.c_str());
  363. strcat (buf, "\n");
  364. break;
  365. case HLI_PUSH: strcat (buf, "HLI_PUSH ");
  366. e = walkCondExpr (h.oper.exp, pProc, numLoc);
  367. strcat (buf, e.c_str());
  368. strcat (buf, "\n");
  369. break;
  370. }
  371. return (buf);
  372. }
  373. Int power2 (Int i)
  374. /* Returns the value of 2 to the power of i */
  375. {
  376. if (i == 0)
  377. return (1);
  378. return (2 << (i-1));
  379. }
  380. /* Writes the registers/stack variables that are used and defined by this
  381. * instruction. */
  382. void ICODE::writeDU(Int idx)
  383. {
  384. static char buf[100];
  385. Int i, j;
  386. memset (buf, ' ', sizeof(buf));
  387. buf[0] = '\0';
  388. for (i = 0; i < (INDEXBASE-1); i++)
  389. {
  390. if (du.def[i])
  391. {
  392. strcat (buf, allRegs[i]);
  393. strcat (buf, " ");
  394. }
  395. }
  396. if (buf[0] != '\0')
  397. printf ("Def (reg) = %s\n", buf);
  398. memset (buf, ' ', sizeof(buf));
  399. buf[0] = '\0';
  400. for (i = 0; i < INDEXBASE; i++)
  401. {
  402. if (du.use[i])
  403. {
  404. strcat (buf, allRegs[i]);
  405. strcat (buf, " ");
  406. }
  407. }
  408. if (buf[0] != '\0')
  409. printf ("Use (reg) = %s\n", buf);
  410. /* Print du1 chain */
  411. printf ("# regs defined = %d\n", du1.numRegsDef);
  412. for (i = 0; i < MAX_REGS_DEF; i++)
  413. {
  414. if (du1.idx[i][0] != 0)
  415. {
  416. printf ("%d: du1[%d][] = ", idx, i);
  417. for (j = 0; j < MAX_USES; j++)
  418. {
  419. if (du1.idx[i][j] == 0)
  420. break;
  421. printf ("%d ", du1.idx[i][j]);
  422. }
  423. printf ("\n");
  424. }
  425. }
  426. /* For HLI_CALL, print # parameter bytes */
  427. if (ic.hl.opcode == HLI_CALL)
  428. printf ("# param bytes = %d\n", ic.hl.oper.call.args->cb);
  429. printf ("\n");
  430. }
  431. /* Frees the storage allocated to h->hlIcode */
  432. void freeHlIcode (ICODE * icode, Int numIcodes)
  433. {
  434. Int i;
  435. HLTYPE h;
  436. for (i = 0; i < numIcodes; i++)
  437. {
  438. h = icode[i].ic.hl;
  439. switch (h.opcode)
  440. {
  441. case HLI_ASSIGN:
  442. h.oper.asgn.lhs->release();
  443. h.oper.asgn.rhs->release();
  444. break;
  445. case HLI_POP:
  446. case HLI_PUSH:
  447. case HLI_JCOND:
  448. h.oper.exp->release();
  449. break;
  450. }
  451. }
  452. }