hlicode.cpp 15 KB

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