hlicode.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. 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 (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=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. 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. 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:
  221. rhs = COND_EXPR::boolOp (lhs, rhs, SHL);
  222. pIcode->setAsgn(lhs, rhs);
  223. break;
  224. case iSAR: /* signed */
  225. case iSHR:
  226. rhs = COND_EXPR::boolOp (lhs, rhs, SHR); /* unsigned*/
  227. pIcode->setAsgn(lhs, rhs);
  228. break;
  229. case iSIGNEX: pIcode->setAsgn(lhs, rhs);
  230. break;
  231. case iSUB: rhs = COND_EXPR::boolOp (lhs, rhs, SUB);
  232. pIcode->setAsgn(lhs, rhs);
  233. break;
  234. case iXCHG:
  235. break;
  236. case iXOR:
  237. rhs = COND_EXPR::boolOp (lhs, rhs, XOR);
  238. pIcode->setAsgn(lhs, rhs);
  239. break;
  240. }
  241. }
  242. }
  243. }
  244. /* Modifies the given conditional operator to its inverse. This is used
  245. * in if..then[..else] statements, to reflect the condition that takes the
  246. * then part. */
  247. COND_EXPR *COND_EXPR::inverse ()
  248. {
  249. static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL,
  250. LESS_EQUAL, LESS, DUMMY,DUMMY,DUMMY,DUMMY,
  251. DUMMY, DUMMY, DUMMY, DUMMY, DUMMY, DUMMY,
  252. DUMMY, DBL_OR, DBL_AND};
  253. COND_EXPR *res=0;
  254. if (type == BOOLEAN_OP)
  255. {
  256. switch (expr.boolExpr.op)
  257. {
  258. case LESS_EQUAL: case LESS: case EQUAL:
  259. case NOT_EQUAL: case GREATER: case GREATER_EQUAL:
  260. res = this->clone();
  261. res->expr.boolExpr.op = invCondOp[expr.boolExpr.op];
  262. return res;
  263. case AND: case OR: case XOR: case NOT: case ADD:
  264. case SUB: case MUL: case DIV: case SHR: case SHL: case MOD:
  265. return COND_EXPR::unary (NEGATION, this->clone());
  266. case DBL_AND: case DBL_OR:
  267. res = this->clone();
  268. res->expr.boolExpr.op = invCondOp[expr.boolExpr.op];
  269. res->expr.boolExpr.lhs=expr.boolExpr.lhs->inverse ();
  270. res->expr.boolExpr.rhs=expr.boolExpr.rhs->inverse ();
  271. return res;
  272. } /* eos */
  273. }
  274. else if (type == NEGATION) //TODO: memleak here
  275. {
  276. return expr.unaryExp->clone();
  277. }
  278. return this->clone();
  279. /* other types are left unmodified */
  280. }
  281. /* Returns the string that represents the procedure call of tproc (ie. with
  282. * actual parameters) */
  283. std::string writeCall (Function * tproc, STKFRAME * args, Function * pproc, int *numLoc)
  284. {
  285. int i; /* counter of # arguments */
  286. string condExp;
  287. ostringstream s;
  288. s<<tproc->name<<" (";
  289. for (i = 0; i < args->sym.size(); i++)
  290. {
  291. s << walkCondExpr (args->sym[i].actual, pproc, numLoc);
  292. if (i < (args->sym.size() - 1))
  293. s << ", ";
  294. }
  295. s << ")";
  296. return s.str();
  297. }
  298. /* Displays the output of a HLI_JCOND icode. */
  299. char *writeJcond (HLTYPE h, Function * pProc, int *numLoc)
  300. {
  301. memset (buf, ' ', sizeof(buf));
  302. buf[0] = '\0';
  303. strcat (buf, "if ");
  304. COND_EXPR *inverted=h.expr()->inverse();
  305. //inverseCondOp (&h.exp);
  306. std::string e = walkCondExpr (inverted, pProc, numLoc);
  307. delete inverted;
  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.expr(), pProc, numLoc);
  321. strcat (buf, e.c_str());
  322. strcat (buf, " {\n");
  323. return (buf);
  324. }
  325. string AssignType::writeOut(Function *pProc, int *numLoc)
  326. {
  327. ostringstream ostr;
  328. ostr << walkCondExpr (lhs, pProc, numLoc);
  329. ostr << " = ";
  330. ostr << walkCondExpr (rhs, pProc, numLoc);
  331. ostr << ";\n";
  332. return ostr.str();
  333. }
  334. string CallType::writeOut(Function *pProc, int *numLoc)
  335. {
  336. ostringstream ostr;
  337. ostr << writeCall (proc, args, pProc,numLoc);
  338. ostr << ";\n";
  339. return ostr.str();
  340. }
  341. string ExpType::writeOut(Function *pProc, int *numLoc)
  342. {
  343. return walkCondExpr (v, pProc, numLoc);
  344. }
  345. /* Returns a string with the contents of the current high-level icode.
  346. * Note: this routine does not output the contens of HLI_JCOND icodes. This is
  347. * done in a separate routine to be able to support the removal of
  348. * empty THEN clauses on an if..then..else. */
  349. string HLTYPE::write1HlIcode (Function * pProc, int *numLoc)
  350. {
  351. string e;
  352. ostringstream ostr;
  353. HlTypeSupport *p = get();
  354. switch (opcode)
  355. {
  356. case HLI_ASSIGN:
  357. return p->writeOut(pProc,numLoc);
  358. case HLI_CALL:
  359. return p->writeOut(pProc,numLoc);
  360. case HLI_RET:
  361. e = p->writeOut(pProc,numLoc);
  362. if (! e.empty())
  363. ostr << "return (" << e << ");\n";
  364. break;
  365. case HLI_POP:
  366. ostr << "HLI_POP ";
  367. ostr << p->writeOut(pProc,numLoc);
  368. ostr << "\n";
  369. break;
  370. case HLI_PUSH:
  371. ostr << "HLI_PUSH ";
  372. ostr << p->writeOut(pProc,numLoc);
  373. ostr << "\n";
  374. break;
  375. }
  376. return ostr.str();
  377. }
  378. int power2 (int i)
  379. /* Returns the value of 2 to the power of i */
  380. {
  381. if (i == 0)
  382. return (1);
  383. return (2 << (i-1));
  384. }
  385. /* Writes the registers/stack variables that are used and defined by this
  386. * instruction. */
  387. void ICODE::writeDU(int idx)
  388. {
  389. static char buf[100];
  390. int i, j;
  391. memset (buf, ' ', sizeof(buf));
  392. buf[0] = '\0';
  393. for (i = 0; i < (INDEXBASE-1); i++)
  394. {
  395. if (du.def[i])
  396. {
  397. strcat (buf, allRegs[i]);
  398. strcat (buf, " ");
  399. }
  400. }
  401. if (buf[0] != '\0')
  402. printf ("Def (reg) = %s\n", buf);
  403. memset (buf, ' ', sizeof(buf));
  404. buf[0] = '\0';
  405. for (i = 0; i < INDEXBASE; i++)
  406. {
  407. if (du.use[i])
  408. {
  409. strcat (buf, allRegs[i]);
  410. strcat (buf, " ");
  411. }
  412. }
  413. if (buf[0] != '\0')
  414. printf ("Use (reg) = %s\n", buf);
  415. /* Print du1 chain */
  416. printf ("# regs defined = %d\n", du1.numRegsDef);
  417. for (i = 0; i < MAX_REGS_DEF; i++)
  418. {
  419. if (du1.used(i))
  420. {
  421. printf ("%d: du1[%d][] = ", idx, i);
  422. for(std::list<ICODE>::iterator j : du1.idx[i].uses)
  423. {
  424. printf ("%d ", j->loc_ip);
  425. }
  426. printf ("\n");
  427. }
  428. }
  429. /* For HLI_CALL, print # parameter bytes */
  430. if (ic.hl.opcode == HLI_CALL)
  431. printf ("# param bytes = %d\n", ic.hl.call.args->cb);
  432. printf ("\n");
  433. }