hlicode.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * File: hlIcode.c
  3. * Purpose: High-level icode routines
  4. * Date: September-October 1993
  5. * (C) Cristina Cifuentes
  6. */
  7. #include <string.h>
  8. #include "dcc.h"
  9. #define ICODE_DELTA 25
  10. /* Masks off bits set by duReg[] */
  11. dword maskDuReg[] = { 0x00,
  12. 0xFEEFFE, 0xFDDFFD, 0xFBB00B, 0xF77007, /* word regs */
  13. 0xFFFFEF, 0xFFFFDF, 0xFFFFBF, 0xFFFF7F,
  14. 0xFFFEFF, 0xFFFDFF, 0xFFFBFF, 0xFFF7FF, /* seg regs */
  15. 0xFFEFFF, 0xFFDFFF, 0xFFBFFF, 0xFF7FFF, /* byte regs */
  16. 0xFEFFFF, 0xFDFFFF, 0xFBFFFF, 0xF7FFFF,
  17. 0xEFFFFF, /* tmp reg */
  18. 0xFFFFB7, 0xFFFF77, 0xFFFF9F, 0xFFFF5F, /* index regs */
  19. 0xFFFFBF, 0xFFFF7F, 0xFFFFDF, 0xFFFFF7 };
  20. static char buf[lineSize]; /* Line buffer for hl icode output */
  21. void newAsgnHlIcode (PICODE pIcode, COND_EXPR *lhs, COND_EXPR *rhs)
  22. /* Places the new HLI_ASSIGN high-level operand in the high-level icode array */
  23. {
  24. pIcode->type = HIGH_LEVEL;
  25. pIcode->ic.hl.opcode = HLI_ASSIGN;
  26. pIcode->ic.hl.oper.asgn.lhs = lhs;
  27. pIcode->ic.hl.oper.asgn.rhs = rhs;
  28. }
  29. void newCallHlIcode (PICODE pIcode)
  30. /* Places the new HLI_CALL high-level operand in the high-level icode array */
  31. {
  32. pIcode->type = HIGH_LEVEL;
  33. pIcode->ic.hl.opcode = HLI_CALL;
  34. pIcode->ic.hl.oper.call.proc = pIcode->ic.ll.immed.proc.proc;
  35. pIcode->ic.hl.oper.call.args = (STKFRAME*)allocMem (sizeof(STKFRAME));
  36. memset (pIcode->ic.hl.oper.call.args, 0, sizeof(STKFRAME));
  37. if (pIcode->ic.ll.immed.proc.cb != 0)
  38. pIcode->ic.hl.oper.call.args->cb = pIcode->ic.ll.immed.proc.cb;
  39. else
  40. pIcode->ic.hl.oper.call.args->cb =pIcode->ic.hl.oper.call.proc->cbParam;
  41. }
  42. void newUnaryHlIcode (PICODE pIcode, hlIcode op, COND_EXPR *exp)
  43. /* Places the new HLI_POP/HLI_PUSH/HLI_RET high-level operand in the high-level icode
  44. * array */
  45. {
  46. pIcode->type = HIGH_LEVEL;
  47. pIcode->ic.hl.opcode = op;
  48. pIcode->ic.hl.oper.exp = exp;
  49. }
  50. void newJCondHlIcode (PICODE pIcode, COND_EXPR *cexp)
  51. /* Places the new HLI_JCOND high-level operand in the high-level icode array */
  52. {
  53. pIcode->type = HIGH_LEVEL;
  54. pIcode->ic.hl.opcode = HLI_JCOND;
  55. pIcode->ic.hl.oper.exp = cexp;
  56. }
  57. void invalidateIcode (PICODE pIcode)
  58. /* Sets the invalid field to TRUE as this low-level icode is no longer valid,
  59. * it has been replaced by a high-level icode. */
  60. {
  61. pIcode->invalid = TRUE;
  62. }
  63. boolT removeDefRegi (byte regi, PICODE picode, Int thisDefIdx, LOCAL_ID *locId)
  64. /* Removes the defined register regi from the lhs subtree. If all registers
  65. * of this instruction are unused, the instruction is invalidated (ie.
  66. * removed) */
  67. { Int numDefs;
  68. numDefs = picode->du1.numRegsDef;
  69. if (numDefs == thisDefIdx)
  70. for ( ; numDefs > 0; numDefs--)
  71. {
  72. if ((picode->du1.idx[numDefs-1][0] != 0)||(picode->du.lastDefRegi))
  73. break;
  74. }
  75. if (numDefs == 0)
  76. {
  77. invalidateIcode (picode);
  78. return (TRUE);
  79. }
  80. else
  81. {
  82. switch (picode->ic.hl.opcode) {
  83. case HLI_ASSIGN: removeRegFromLong (regi, locId,
  84. picode->ic.hl.oper.asgn.lhs);
  85. picode->du1.numRegsDef--;
  86. picode->du.def &= maskDuReg[regi];
  87. break;
  88. case HLI_POP:
  89. case HLI_PUSH: removeRegFromLong (regi, locId, picode->ic.hl.oper.exp);
  90. picode->du1.numRegsDef--;
  91. picode->du.def &= maskDuReg[regi];
  92. break;
  93. }
  94. return (FALSE);
  95. }
  96. }
  97. void highLevelGen (PPROC pProc)
  98. /* Translates LOW_LEVEL icodes to HIGH_LEVEL icodes - 1st stage.
  99. * Note: this process should be done before data flow analysis, which
  100. * refines the HIGH_LEVEL icodes. */
  101. { Int i, /* idx into icode array */
  102. numIcode; /* number of icode instructions */
  103. PICODE pIcode; /* ptr to current icode node */
  104. COND_EXPR *lhs, *rhs; /* left- and right-hand side of expression */
  105. flags32 flg; /* icode flags */
  106. numIcode = pProc->Icode.GetNumIcodes();
  107. for (i = 0; i < numIcode; i++)
  108. {
  109. pIcode = pProc->Icode.GetIcode(i);
  110. if ((pIcode->ic.ll.flg & NOT_HLL) == NOT_HLL)
  111. invalidateIcode (pIcode);
  112. if ((pIcode->type == LOW_LEVEL) && (pIcode->invalid == FALSE))
  113. {
  114. flg = pIcode->ic.ll.flg;
  115. if ((flg & IM_OPS) != IM_OPS) /* not processing IM_OPS yet */
  116. if ((flg & NO_OPS) != NO_OPS) /* if there are opers */
  117. {
  118. if ((flg & NO_SRC) != NO_SRC) /* if there is src op */
  119. rhs = idCondExp (pIcode, SRC, pProc, i, pIcode, NONE);
  120. lhs = idCondExp (pIcode, DST, pProc, i, pIcode, NONE);
  121. }
  122. switch (pIcode->ic.ll.opcode) {
  123. case iADD: rhs = boolCondExp (lhs, rhs, ADD);
  124. newAsgnHlIcode (pIcode, lhs, rhs);
  125. break;
  126. case iAND: rhs = boolCondExp (lhs, rhs, AND);
  127. newAsgnHlIcode (pIcode, lhs, rhs);
  128. break;
  129. case iCALL:
  130. case iCALLF: newCallHlIcode (pIcode);
  131. break;
  132. case iDEC: rhs = idCondExpKte (1, 2);
  133. rhs = boolCondExp (lhs, rhs, SUB);
  134. newAsgnHlIcode (pIcode, lhs, rhs);
  135. break;
  136. case iDIV:
  137. case iIDIV:/* should be signed div */
  138. rhs = boolCondExp (lhs, rhs, DIV);
  139. if (pIcode->ic.ll.flg & B)
  140. {
  141. lhs = idCondExpReg (rAL, 0, &pProc->localId);
  142. setRegDU (pIcode, rAL, E_DEF);
  143. }
  144. else
  145. {
  146. lhs = idCondExpReg (rAX, 0, &pProc->localId);
  147. setRegDU (pIcode, rAX, E_DEF);
  148. }
  149. newAsgnHlIcode (pIcode, lhs, rhs);
  150. break;
  151. case iIMUL: rhs = boolCondExp (lhs, rhs, MUL);
  152. lhs = idCondExp (pIcode, LHS_OP, pProc, i, pIcode,
  153. NONE);
  154. newAsgnHlIcode (pIcode, lhs, rhs);
  155. break;
  156. case iINC: rhs = idCondExpKte (1, 2);
  157. rhs = boolCondExp (lhs, rhs, ADD);
  158. newAsgnHlIcode (pIcode, lhs, rhs);
  159. break;
  160. case iLEA: rhs = unaryCondExp (ADDRESSOF, rhs);
  161. newAsgnHlIcode (pIcode, lhs, rhs);
  162. break;
  163. case iMOD: rhs = boolCondExp (lhs, rhs, MOD);
  164. if (pIcode->ic.ll.flg & B)
  165. {
  166. lhs = idCondExpReg (rAH, 0, &pProc->localId);
  167. setRegDU (pIcode, rAH, E_DEF);
  168. }
  169. else
  170. {
  171. lhs = idCondExpReg (rDX, 0, &pProc->localId);
  172. setRegDU (pIcode, rDX, E_DEF);
  173. }
  174. newAsgnHlIcode (pIcode, lhs, rhs);
  175. break;
  176. case iMOV: newAsgnHlIcode (pIcode, lhs, rhs);
  177. break;
  178. case iMUL: rhs = boolCondExp (lhs, rhs, MUL);
  179. lhs = idCondExp (pIcode, LHS_OP, pProc, i, pIcode,
  180. NONE);
  181. newAsgnHlIcode (pIcode, lhs, rhs);
  182. break;
  183. case iNEG: rhs = unaryCondExp (NEGATION, lhs);
  184. newAsgnHlIcode (pIcode, lhs, rhs);
  185. break;
  186. case iNOT: rhs = boolCondExp (NULL, rhs, NOT);
  187. newAsgnHlIcode (pIcode, lhs, rhs);
  188. break;
  189. case iOR: rhs = boolCondExp (lhs, rhs, OR);
  190. newAsgnHlIcode (pIcode, lhs, rhs);
  191. break;
  192. case iPOP: newUnaryHlIcode (pIcode, HLI_POP, lhs);
  193. break;
  194. case iPUSH: newUnaryHlIcode (pIcode, HLI_PUSH, lhs);
  195. break;
  196. case iRET:
  197. case iRETF: newUnaryHlIcode (pIcode, HLI_RET, NULL);
  198. break;
  199. case iSHL: rhs = boolCondExp (lhs, rhs, SHL);
  200. newAsgnHlIcode (pIcode, lhs, rhs);
  201. break;
  202. case iSAR: /* signed */
  203. case iSHR: rhs = boolCondExp (lhs, rhs, SHR); /* unsigned*/
  204. newAsgnHlIcode (pIcode, lhs, rhs);
  205. break;
  206. case iSIGNEX: newAsgnHlIcode (pIcode, lhs, rhs);
  207. break;
  208. case iSUB: rhs = boolCondExp (lhs, rhs, SUB);
  209. newAsgnHlIcode (pIcode, lhs, rhs);
  210. break;
  211. case iXCHG:
  212. break;
  213. case iXOR: rhs = boolCondExp (lhs, rhs, XOR);
  214. newAsgnHlIcode (pIcode, lhs, rhs);
  215. break;
  216. }
  217. }
  218. }
  219. }
  220. void inverseCondOp (COND_EXPR **exp)
  221. /* Modifies the given conditional operator to its inverse. This is used
  222. * in if..then[..else] statements, to reflect the condition that takes the
  223. * then part. */
  224. {
  225. static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL,
  226. LESS_EQUAL, LESS, DUMMY,DUMMY,DUMMY,DUMMY,
  227. DUMMY, DUMMY, DUMMY, DUMMY, DUMMY, DUMMY,
  228. DUMMY, DBL_OR, DBL_AND};
  229. if (*exp == NULL) return;
  230. if ((*exp)->type == BOOLEAN_OP)
  231. {
  232. switch ((*exp)->expr.boolExpr.op) {
  233. case LESS_EQUAL: case LESS: case EQUAL:
  234. case NOT_EQUAL: case GREATER: case GREATER_EQUAL:
  235. (*exp)->expr.boolExpr.op = invCondOp[(*exp)->expr.boolExpr.op];
  236. break;
  237. case AND: case OR: case XOR: case NOT: case ADD:
  238. case SUB: case MUL: case DIV: case SHR: case SHL: case MOD:
  239. *exp = unaryCondExp (NEGATION, *exp);
  240. break;
  241. case DBL_AND: case DBL_OR:
  242. (*exp)->expr.boolExpr.op = invCondOp[(*exp)->expr.boolExpr.op];
  243. inverseCondOp (&(*exp)->expr.boolExpr.lhs);
  244. inverseCondOp (&(*exp)->expr.boolExpr.rhs);
  245. break;
  246. } /* eos */
  247. }
  248. else if ((*exp)->type == NEGATION)
  249. *exp = (*exp)->expr.unaryExp;
  250. /* other types are left unmodified */
  251. }
  252. char *writeCall (PPROC tproc, PSTKFRAME args, PPROC pproc, Int *numLoc)
  253. /* Returns the string that represents the procedure call of tproc (ie. with
  254. * actual parameters) */
  255. { Int i; /* counter of # arguments */
  256. char *s, *condExp;
  257. s = (char*)allocMem (100 * sizeof(char));
  258. s[0] = '\0';
  259. sprintf (s, "%s (", tproc->name);
  260. for (i = 0; i < args->csym; i++)
  261. {
  262. condExp = walkCondExpr (args->sym[i].actual, pproc, numLoc);
  263. strcat (s, condExp);
  264. if (i < (args->csym - 1))
  265. strcat (s, ", ");
  266. }
  267. strcat (s, ")");
  268. return (s);
  269. }
  270. char *writeJcond (HLTYPE h, PPROC pProc, Int *numLoc)
  271. /* Displays the output of a HLI_JCOND icode. */
  272. { char *e;
  273. memset (buf, ' ', sizeof(buf));
  274. buf[0] = '\0';
  275. strcat (buf, "if ");
  276. inverseCondOp (&h.oper.exp);
  277. e = walkCondExpr (h.oper.exp, pProc, numLoc);
  278. strcat (buf, e);
  279. strcat (buf, " {\n");
  280. return (buf);
  281. }
  282. char *writeJcondInv (HLTYPE h, PPROC pProc, Int *numLoc)
  283. /* Displays the inverse output of a HLI_JCOND icode. This is used in the case
  284. * when the THEN clause of an if..then..else is empty. The clause is
  285. * negated and the ELSE clause is used instead. */
  286. { char *e;
  287. memset (buf, ' ', sizeof(buf));
  288. buf[0] = '\0';
  289. strcat (buf, "if ");
  290. e = walkCondExpr (h.oper.exp, pProc, numLoc);
  291. strcat (buf, e);
  292. strcat (buf, " {\n");
  293. return (buf);
  294. }
  295. char *write1HlIcode (HLTYPE h, PPROC pProc, Int *numLoc)
  296. /* Returns a string with the contents of the current high-level icode.
  297. * Note: this routine does not output the contens of HLI_JCOND icodes. This is
  298. * done in a separate routine to be able to support the removal of
  299. * empty THEN clauses on an if..then..else. */
  300. { char *e;
  301. memset (buf, ' ', sizeof(buf));
  302. buf[0] = '\0';
  303. switch (h.opcode) {
  304. case HLI_ASSIGN: e = walkCondExpr (h.oper.asgn.lhs, pProc, numLoc);
  305. strcat (buf, e);
  306. strcat (buf, " = ");
  307. e = walkCondExpr (h.oper.asgn.rhs, pProc, numLoc);
  308. strcat (buf, e);
  309. strcat (buf, ";\n");
  310. break;
  311. case HLI_CALL: e = writeCall (h.oper.call.proc, h.oper.call.args, pProc,
  312. numLoc);
  313. strcat (buf, e);
  314. strcat (buf, ";\n");
  315. break;
  316. case HLI_RET: e = walkCondExpr (h.oper.exp, pProc, numLoc);
  317. if (e[0] != '\0')
  318. {
  319. strcat (buf, "return (");
  320. strcat (buf, e);
  321. strcat (buf, ");\n");
  322. }
  323. break;
  324. case HLI_POP: strcat (buf, "HLI_POP ");
  325. e = walkCondExpr (h.oper.exp, pProc, numLoc);
  326. strcat (buf, e);
  327. strcat (buf, "\n");
  328. break;
  329. case HLI_PUSH: strcat (buf, "HLI_PUSH ");
  330. e = walkCondExpr (h.oper.exp, pProc, numLoc);
  331. strcat (buf, e);
  332. strcat (buf, "\n");
  333. break;
  334. }
  335. return (buf);
  336. }
  337. Int power2 (Int i)
  338. /* Returns the value of 2 to the power of i */
  339. {
  340. if (i == 0)
  341. return (1);
  342. return (2 << (i-1));
  343. }
  344. void writeDU (PICODE pIcode, Int idx)
  345. /* Writes the registers/stack variables that are used and defined by this
  346. * instruction. */
  347. { static char buf[100];
  348. Int i, j;
  349. memset (buf, ' ', sizeof(buf));
  350. buf[0] = '\0';
  351. for (i = 0; i < (INDEXBASE-1); i++)
  352. {
  353. if ((pIcode->du.def & power2(i)) != 0)
  354. {
  355. strcat (buf, allRegs[i]);
  356. strcat (buf, " ");
  357. }
  358. }
  359. if (buf[0] != '\0')
  360. printf ("Def (reg) = %s\n", buf);
  361. memset (buf, ' ', sizeof(buf));
  362. buf[0] = '\0';
  363. for (i = 0; i < INDEXBASE; i++)
  364. {
  365. if ((pIcode->du.use & power2(i)) != 0)
  366. {
  367. strcat (buf, allRegs[i]);
  368. strcat (buf, " ");
  369. }
  370. }
  371. if (buf[0] != '\0')
  372. printf ("Use (reg) = %s\n", buf);
  373. /* Print du1 chain */
  374. printf ("# regs defined = %d\n", pIcode->du1.numRegsDef);
  375. for (i = 0; i < MAX_REGS_DEF; i++)
  376. if (pIcode->du1.idx[i][0] != 0)
  377. {
  378. printf ("%d: du1[%d][] = ", idx, i);
  379. for (j = 0; j < MAX_USES; j++)
  380. {
  381. if (pIcode->du1.idx[i][j] == 0)
  382. break;
  383. printf ("%d ", pIcode->du1.idx[i][j]);
  384. }
  385. printf ("\n");
  386. }
  387. /* For HLI_CALL, print # parameter bytes */
  388. if (pIcode->ic.hl.opcode == HLI_CALL)
  389. printf ("# param bytes = %d\n", pIcode->ic.hl.oper.call.args->cb);
  390. printf ("\n");
  391. }
  392. void freeHlIcode (PICODE icode, Int numIcodes)
  393. /* Frees the storage allocated to h->hlIcode */
  394. { Int i;
  395. HLTYPE h;
  396. for (i = 0; i < numIcodes; i++)
  397. {
  398. h = icode[i].ic.hl;
  399. switch (h.opcode) {
  400. case HLI_ASSIGN: freeCondExpr (h.oper.asgn.lhs);
  401. freeCondExpr (h.oper.asgn.rhs);
  402. break;
  403. case HLI_POP: case HLI_PUSH:
  404. case HLI_JCOND: freeCondExpr (h.oper.exp);
  405. break;
  406. }
  407. }
  408. }