hlicode.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. uint32_t maskDuReg[] = { 0x00,
  16. 0xFEEFFE, 0xFDDFFD, 0xFBB00B, 0xF77007, /* uint16_t regs */
  17. 0xFFFFEF, 0xFFFFDF, 0xFFFFBF, 0xFFFF7F,
  18. 0xFFFEFF, 0xFFFDFF, 0xFFFBFF, 0xFFF7FF, /* seg regs */
  19. 0xFFEFFF, 0xFFDFFF, 0xFFBFFF, 0xFF7FFF, /* uint8_t 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. hl()->set(lhs,rhs);
  30. }
  31. void ICODE::checkHlCall()
  32. {
  33. //assert((ll()->immed.proc.cb != 0)||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. hl()->opcode = HLI_CALL;
  40. hl()->call.proc = ll()->src.proc.proc;
  41. hl()->call.args = new STKFRAME;
  42. if (ll()->src.proc.cb != 0)
  43. hl()->call.args->cb = ll()->src.proc.cb;
  44. else if(hl()->call.proc)
  45. hl()->call.args->cb =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. 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. 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. 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 (uint8_t 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=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. uint32_t 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. LLInst *ll = pIcode->ll();
  123. if ( ll->isLlFlag(NOT_HLL) )
  124. pIcode->invalidate();
  125. if ((pIcode->type == LOW_LEVEL) && pIcode->valid() )
  126. {
  127. flg = ll->GetLlFlag();
  128. if ((flg & IM_OPS) != IM_OPS) /* not processing IM_OPS yet */
  129. if ((flg & NO_OPS) != NO_OPS) /* if there are opers */
  130. {
  131. if ((flg & NO_SRC) != NO_SRC) /* if there is src op */
  132. rhs = COND_EXPR::id (*pIcode, SRC, this, i, *pIcode, NONE);
  133. lhs = COND_EXPR::id (*pIcode, DST, this, i, *pIcode, NONE);
  134. }
  135. switch (ll->opcode)
  136. {
  137. case iADD:
  138. rhs = COND_EXPR::boolOp (lhs, rhs, ADD);
  139. pIcode->setAsgn(lhs, rhs);
  140. break;
  141. case iAND:
  142. rhs = COND_EXPR::boolOp (lhs, rhs, AND);
  143. pIcode->setAsgn(lhs, rhs);
  144. break;
  145. case iCALL:
  146. case iCALLF:
  147. pIcode->checkHlCall();
  148. pIcode->newCallHl();
  149. break;
  150. case iDEC:
  151. rhs = COND_EXPR::idKte (1, 2);
  152. rhs = COND_EXPR::boolOp (lhs, rhs, SUB);
  153. pIcode->setAsgn(lhs, rhs);
  154. break;
  155. case iDIV:
  156. case iIDIV:/* should be signed div */
  157. rhs = COND_EXPR::boolOp (lhs, rhs, DIV);
  158. if ( ll->isLlFlag(B) )
  159. {
  160. lhs = COND_EXPR::idReg (rAL, 0, &localId);
  161. pIcode->setRegDU( rAL, eDEF);
  162. }
  163. else
  164. {
  165. lhs = COND_EXPR::idReg (rAX, 0, &localId);
  166. pIcode->setRegDU( rAX, eDEF);
  167. }
  168. pIcode->setAsgn(lhs, rhs);
  169. break;
  170. case iIMUL:
  171. rhs = COND_EXPR::boolOp (lhs, rhs, MUL);
  172. lhs = COND_EXPR::id (*pIcode, LHS_OP, this, i, *pIcode, NONE);
  173. pIcode->setAsgn(lhs, rhs);
  174. break;
  175. case iINC:
  176. rhs = COND_EXPR::idKte (1, 2);
  177. rhs = COND_EXPR::boolOp (lhs, rhs, ADD);
  178. pIcode->setAsgn(lhs, rhs);
  179. break;
  180. case iLEA:
  181. rhs = COND_EXPR::unary (ADDRESSOF, rhs);
  182. pIcode->setAsgn(lhs, rhs);
  183. break;
  184. case iMOD:
  185. rhs = COND_EXPR::boolOp (lhs, rhs, MOD);
  186. if ( ll->isLlFlag(B) )
  187. {
  188. lhs = COND_EXPR::idReg (rAH, 0, &localId);
  189. pIcode->setRegDU( rAH, eDEF);
  190. }
  191. else
  192. {
  193. lhs = COND_EXPR::idReg (rDX, 0, &localId);
  194. pIcode->setRegDU( rDX, eDEF);
  195. }
  196. pIcode->setAsgn(lhs, rhs);
  197. break;
  198. case iMOV: pIcode->setAsgn(lhs, rhs);
  199. break;
  200. case iMUL:
  201. rhs = COND_EXPR::boolOp (lhs, rhs, MUL);
  202. lhs = COND_EXPR::id (*pIcode, LHS_OP, this, i, *pIcode, NONE);
  203. pIcode->setAsgn(lhs, rhs);
  204. break;
  205. case iNEG: rhs = COND_EXPR::unary (NEGATION, lhs);
  206. pIcode->setAsgn(lhs, rhs);
  207. break;
  208. case iNOT:
  209. rhs = COND_EXPR::boolOp (NULL, rhs, NOT);
  210. pIcode->setAsgn(lhs, rhs);
  211. break;
  212. case iOR:
  213. rhs = COND_EXPR::boolOp (lhs, rhs, OR);
  214. pIcode->setAsgn(lhs, rhs);
  215. break;
  216. case iPOP: pIcode->setUnary(HLI_POP, lhs);
  217. break;
  218. case iPUSH: pIcode->setUnary(HLI_PUSH, lhs);
  219. break;
  220. case iRET:
  221. case iRETF: pIcode->setUnary(HLI_RET, NULL);
  222. break;
  223. case iSHL:
  224. rhs = COND_EXPR::boolOp (lhs, rhs, SHL);
  225. pIcode->setAsgn(lhs, rhs);
  226. break;
  227. case iSAR: /* signed */
  228. case iSHR:
  229. rhs = COND_EXPR::boolOp (lhs, rhs, SHR); /* unsigned*/
  230. pIcode->setAsgn(lhs, rhs);
  231. break;
  232. case iSIGNEX: pIcode->setAsgn(lhs, rhs);
  233. break;
  234. case iSUB: rhs = COND_EXPR::boolOp (lhs, rhs, SUB);
  235. pIcode->setAsgn(lhs, rhs);
  236. break;
  237. case iXCHG:
  238. break;
  239. case iXOR:
  240. rhs = COND_EXPR::boolOp (lhs, rhs, XOR);
  241. pIcode->setAsgn(lhs, rhs);
  242. break;
  243. }
  244. }
  245. }
  246. }
  247. /* Modifies the given conditional operator to its inverse. This is used
  248. * in if..then[..else] statements, to reflect the condition that takes the
  249. * then part. */
  250. COND_EXPR *COND_EXPR::inverse ()
  251. {
  252. static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL,
  253. LESS_EQUAL, LESS, DUMMY,DUMMY,DUMMY,DUMMY,
  254. DUMMY, DUMMY, DUMMY, DUMMY, DUMMY, DUMMY,
  255. DUMMY, DBL_OR, DBL_AND};
  256. COND_EXPR *res=0;
  257. if (type == BOOLEAN_OP)
  258. {
  259. switch (expr.boolExpr.op)
  260. {
  261. case LESS_EQUAL: case LESS: case EQUAL:
  262. case NOT_EQUAL: case GREATER: case GREATER_EQUAL:
  263. res = this->clone();
  264. res->expr.boolExpr.op = invCondOp[expr.boolExpr.op];
  265. return res;
  266. case AND: case OR: case XOR: case NOT: case ADD:
  267. case SUB: case MUL: case DIV: case SHR: case SHL: case MOD:
  268. return COND_EXPR::unary (NEGATION, this->clone());
  269. case DBL_AND: case DBL_OR:
  270. res = this->clone();
  271. res->expr.boolExpr.op = invCondOp[expr.boolExpr.op];
  272. res->expr.boolExpr.lhs=expr.boolExpr.lhs->inverse ();
  273. res->expr.boolExpr.rhs=expr.boolExpr.rhs->inverse ();
  274. return res;
  275. } /* eos */
  276. }
  277. else if (type == NEGATION) //TODO: memleak here
  278. {
  279. return expr.unaryExp->clone();
  280. }
  281. return this->clone();
  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. COND_EXPR *inverted=h.expr()->inverse();
  308. //inverseCondOp (&h.exp);
  309. std::string e = walkCondExpr (inverted, pProc, numLoc);
  310. delete inverted;
  311. strcat (buf, e.c_str());
  312. strcat (buf, " {\n");
  313. return (buf);
  314. }
  315. /* Displays the inverse output of a HLI_JCOND icode. This is used in the case
  316. * when the THEN clause of an if..then..else is empty. The clause is
  317. * negated and the ELSE clause is used instead. */
  318. char *writeJcondInv (HLTYPE h, Function * pProc, int *numLoc)
  319. {
  320. memset (buf, ' ', sizeof(buf));
  321. buf[0] = '\0';
  322. strcat (buf, "if ");
  323. std::string e = walkCondExpr (h.expr(), pProc, numLoc);
  324. strcat (buf, e.c_str());
  325. strcat (buf, " {\n");
  326. return (buf);
  327. }
  328. string AssignType::writeOut(Function *pProc, int *numLoc)
  329. {
  330. ostringstream ostr;
  331. ostr << walkCondExpr (lhs, pProc, numLoc);
  332. ostr << " = ";
  333. ostr << walkCondExpr (rhs, pProc, numLoc);
  334. ostr << ";\n";
  335. return ostr.str();
  336. }
  337. string CallType::writeOut(Function *pProc, int *numLoc)
  338. {
  339. ostringstream ostr;
  340. ostr << writeCall (proc, args, pProc,numLoc);
  341. ostr << ";\n";
  342. return ostr.str();
  343. }
  344. string ExpType::writeOut(Function *pProc, int *numLoc)
  345. {
  346. return walkCondExpr (v, pProc, numLoc);
  347. }
  348. /* Returns a string with the contents of the current high-level icode.
  349. * Note: this routine does not output the contens of HLI_JCOND icodes. This is
  350. * done in a separate routine to be able to support the removal of
  351. * empty THEN clauses on an if..then..else. */
  352. string HLTYPE::write1HlIcode (Function * pProc, int *numLoc)
  353. {
  354. string e;
  355. ostringstream ostr;
  356. HlTypeSupport *p = get();
  357. switch (opcode)
  358. {
  359. case HLI_ASSIGN:
  360. return p->writeOut(pProc,numLoc);
  361. case HLI_CALL:
  362. return p->writeOut(pProc,numLoc);
  363. case HLI_RET:
  364. e = p->writeOut(pProc,numLoc);
  365. if (! e.empty())
  366. ostr << "return (" << e << ");\n";
  367. break;
  368. case HLI_POP:
  369. ostr << "HLI_POP ";
  370. ostr << p->writeOut(pProc,numLoc);
  371. ostr << "\n";
  372. break;
  373. case HLI_PUSH:
  374. ostr << "HLI_PUSH ";
  375. ostr << p->writeOut(pProc,numLoc);
  376. ostr << "\n";
  377. break;
  378. }
  379. return ostr.str();
  380. }
  381. int power2 (int i)
  382. /* Returns the value of 2 to the power of i */
  383. {
  384. if (i == 0)
  385. return (1);
  386. return (2 << (i-1));
  387. }
  388. /* Writes the registers/stack variables that are used and defined by this
  389. * instruction. */
  390. void ICODE::writeDU(int idx)
  391. {
  392. static char buf[100];
  393. int i, j;
  394. memset (buf, ' ', sizeof(buf));
  395. buf[0] = '\0';
  396. for (i = 0; i < (INDEXBASE-1); i++)
  397. {
  398. if (du.def[i])
  399. {
  400. strcat (buf, allRegs[i]);
  401. strcat (buf, " ");
  402. }
  403. }
  404. if (buf[0] != '\0')
  405. printf ("Def (reg) = %s\n", buf);
  406. memset (buf, ' ', sizeof(buf));
  407. buf[0] = '\0';
  408. for (i = 0; i < INDEXBASE; i++)
  409. {
  410. if (du.use[i])
  411. {
  412. strcat (buf, allRegs[i]);
  413. strcat (buf, " ");
  414. }
  415. }
  416. if (buf[0] != '\0')
  417. printf ("Use (reg) = %s\n", buf);
  418. /* Print du1 chain */
  419. printf ("# regs defined = %d\n", du1.numRegsDef);
  420. for (i = 0; i < MAX_REGS_DEF; i++)
  421. {
  422. if (du1.used(i))
  423. {
  424. printf ("%d: du1[%d][] = ", idx, i);
  425. for(std::list<ICODE>::iterator j : du1.idx[i].uses)
  426. {
  427. printf ("%d ", j->loc_ip);
  428. }
  429. printf ("\n");
  430. }
  431. }
  432. /* For HLI_CALL, print # parameter bytes */
  433. if (hl()->opcode == HLI_CALL)
  434. printf ("# param bytes = %d\n", hl()->call.args->cb);
  435. printf ("\n");
  436. }