hlicode.cpp 15 KB

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