backend.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*****************************************************************************
  2. * Project: dcc
  3. * File: backend.c
  4. * Purpose: Back-end module. Generates C code for each procedure.
  5. * (C) Cristina Cifuentes
  6. ****************************************************************************/
  7. #include <cassert>
  8. #include <string>
  9. #include "dcc.h"
  10. #include <fstream>
  11. #include <string.h>
  12. #include <stdio.h>
  13. bundle cCode; /* Procedure declaration and code */
  14. using namespace std;
  15. /* Indentation buffer */
  16. #define indSize 81 /* size of the indentation buffer. Each indentation
  17. * is of 4 spaces => max. 20 indentation levels */
  18. static char indentBuf[indSize] =
  19. " ";
  20. /* Indentation according to the depth of the statement */
  21. static char *indent (Int indLevel)
  22. {
  23. return (&indentBuf[indSize-(indLevel*4)-1]);
  24. }
  25. static Int getNextLabel()
  26. /* Returns a unique index to the next label */
  27. { static Int labelIdx = 1; /* index of the next label */
  28. return (labelIdx++);
  29. }
  30. /* displays statistics on the subroutine */
  31. void Function::displayStats ()
  32. {
  33. printf("\nStatistics - Subroutine %s\n", name);
  34. printf ("Number of Icode instructions:\n");
  35. printf (" Low-level : %4d\n", stats.numLLIcode);
  36. if (! (flg & PROC_ASM))
  37. {
  38. printf (" High-level: %4d\n", stats.numHLIcode);
  39. printf (" Percentage reduction: %2.2f%%\n", 100.0 - (stats.numHLIcode *
  40. 100.0) / stats.numLLIcode);
  41. }
  42. }
  43. /**** this proc is not required any more?? ****/
  44. #if 0
  45. static void fixupLabels (PPROC pProc)
  46. /* Checks the graph (pProc->cfg) for any nodes that have labels, and gives
  47. * a unique label number for it. This label is placed in the associated
  48. * icode for the node (pProc->Icode). The procedure is done in sequential
  49. * order of dsfLast numbering. */
  50. { Int i; /* index into the dfsLast array */
  51. PBB *dfsLast; /* pointer to the dfsLast array */
  52. dfsLast = pProc->dfsLast;
  53. for (i = 0; i < pProc->numBBs; i++)
  54. if (dfsLast[i]->flg/* & BB_HAS_LABEL*/) {
  55. pProc->Icode.icode[dfsLast[i]->start].ic.ll.flg |= HLL_LABEL;
  56. pProc->Icode.icode[dfsLast[i]->start].ic.ll.hllLabNum = getNextLabel();
  57. }
  58. }
  59. #endif
  60. /* Returns the corresponding C string for the given character c. Character
  61. * constants such as carriage return and line feed, require 2 C characters. */
  62. char *cChar (byte c)
  63. {
  64. static char res[3];
  65. switch (c) {
  66. case 0x8: /* backspace */
  67. sprintf (res, "\\b");
  68. break;
  69. case 0x9: /* horizontal tab */
  70. sprintf (res, "\\t");
  71. break;
  72. case 0x0A: /* new line */
  73. sprintf (res, "\\n");
  74. break;
  75. case 0x0C: /* form feed */
  76. sprintf (res, "\\f");
  77. break;
  78. case 0x0D: /* carriage return */
  79. sprintf (res, "\\r");
  80. break;
  81. default: /* any other character*/
  82. sprintf (res, "%c", c);
  83. }
  84. return (res);
  85. }
  86. /* Prints the variable's name and initial contents on the file.
  87. * Note: to get to the value of the variable:
  88. * com file: prog.Image[operand]
  89. * exe file: prog.Image[operand+0x100] */
  90. static void printGlobVar (SYM * psym)
  91. {
  92. Int j;
  93. dword relocOp = prog.fCOM ? psym->label : psym->label + 0x100;
  94. char *strContents; /* initial contents of variable */
  95. switch (psym->size) {
  96. case 1: cCode.appendDecl( "byte\t%s = %ld;\n",
  97. psym->name, prog.Image[relocOp]);
  98. break;
  99. case 2: cCode.appendDecl( "word\t%s = %ld;\n",
  100. psym->name, LH(prog.Image+relocOp));
  101. break;
  102. case 4: if (psym->type == TYPE_PTR) /* pointer */
  103. cCode.appendDecl( "word\t*%s = %ld;\n",
  104. psym->name, LH(prog.Image+relocOp));
  105. else /* char */
  106. cCode.appendDecl(
  107. "char\t%s[4] = \"%c%c%c%c\";\n",
  108. psym->name, prog.Image[relocOp],
  109. prog.Image[relocOp+1], prog.Image[relocOp+2],
  110. prog.Image[relocOp+3]);
  111. break;
  112. default:strContents = (char *)allocMem((psym->size*2+1) *sizeof(char));
  113. strContents[0] = '\0';
  114. for (j=0; j < psym->size; j++)
  115. strcat (strContents, cChar(prog.Image[relocOp + j]));
  116. cCode.appendDecl( "char\t*%s = \"%s\";\n",
  117. psym->name, strContents);
  118. }
  119. }
  120. // Note: Not called at present.
  121. /* Writes the contents of the symbol table, along with any variable
  122. * initialization. */
  123. static void writeGlobSymTable()
  124. {
  125. Int idx;
  126. char type[10];
  127. SYM * pSym;
  128. if (symtab.csym)
  129. {
  130. cCode.appendDecl( "/* Global variables */\n");
  131. for (idx = 0; idx < symtab.csym; idx++)
  132. {
  133. pSym = &symtab.sym[idx];
  134. if (symtab.sym[idx].duVal.isUSE_VAL()) /* first used */
  135. printGlobVar (&(symtab.sym[idx]));
  136. else { /* first defined */
  137. switch (pSym->size) {
  138. case 1: strcpy (type, "byte\t"); break;
  139. case 2: strcpy (type, "int\t"); break;
  140. case 4: if (pSym->type == TYPE_PTR)
  141. strcpy (type, "int\t*");
  142. else
  143. strcpy (type, "char\t*");
  144. break;
  145. default: strcpy (type, "char\t*");
  146. }
  147. cCode.appendDecl( "%s%s;\t/* size = %ld */\n",
  148. type, pSym->name, pSym->size);
  149. }
  150. }
  151. cCode.appendDecl( "\n");
  152. }
  153. }
  154. /* Writes the header information and global variables to the output C file
  155. * fp. */
  156. static void writeHeader (std::ostream &ios, char *fileName)
  157. {
  158. /* Write header information */
  159. newBundle (&cCode);
  160. cCode.appendDecl( "/*\n");
  161. cCode.appendDecl( " * Input file\t: %s\n", fileName);
  162. cCode.appendDecl( " * File type\t: %s\n", (prog.fCOM)?"COM":"EXE");
  163. cCode.appendDecl( " */\n\n#include \"dcc.h\"\n\n");
  164. /* Write global symbol table */
  165. /** writeGlobSymTable(); *** need to change them into locident fmt ***/
  166. writeBundle (ios, cCode);
  167. freeBundle (&cCode);
  168. }
  169. /* Writes the registers that are set in the bitvector */
  170. static void writeBitVector (dword regi)
  171. { Int j;
  172. for (j = 0; j < INDEXBASE; j++)
  173. {
  174. if ((regi & power2(j)) != 0)
  175. printf ("%s ", allRegs[j]);
  176. }
  177. }
  178. /* Checks the given icode to determine whether it has a label associated
  179. * to it. If so, a goto is emitted to this label; otherwise, a new label
  180. * is created and a goto is also emitted.
  181. * Note: this procedure is to be used when the label is to be backpatched
  182. * onto code in cCode.code */
  183. static void emitGotoLabel (ICODE * pt, Int indLevel)
  184. {
  185. if (! (pt->ic.ll.flg & HLL_LABEL)) /* node hasn't got a lab */
  186. {
  187. /* Generate new label */
  188. pt->ic.ll.hllLabNum = getNextLabel();
  189. pt->ic.ll.flg |= HLL_LABEL;
  190. /* Node has been traversed already, so backpatch this label into
  191. * the code */
  192. addLabelBundle (cCode.code, pt->codeIdx, pt->ic.ll.hllLabNum);
  193. }
  194. cCode.appendCode( "%sgoto L%ld;\n", indent(indLevel),
  195. pt->ic.ll.hllLabNum);
  196. stats.numHLIcode++;
  197. }
  198. // Note: Not currently called!
  199. static void emitFwdGotoLabel (ICODE * pt, Int indLevel)
  200. /* Checks the given icode to determine whether it has a label associated
  201. * to it. If so, a goto is emitted to this label; otherwise, a new label
  202. * is created and a goto is also emitted.
  203. * Note: this procedure is to be used when the label is to be forward on
  204. * the code; that is, the target code has not been traversed yet. */
  205. {
  206. if (! (pt->ic.ll.flg & HLL_LABEL)) /* node hasn't got a lab */
  207. {
  208. /* Generate new label */
  209. pt->ic.ll.hllLabNum = getNextLabel();
  210. pt->ic.ll.flg |= HLL_LABEL;
  211. }
  212. cCode.appendCode( "%sgoto l%ld;\n", indent(indLevel),
  213. pt->ic.ll.hllLabNum);
  214. }
  215. /* Writes the code for the current basic block.
  216. * Args: pBB: pointer to the current basic block.
  217. * Icode: pointer to the array of icodes for current procedure.
  218. * lev: indentation level - used for formatting. */
  219. static void writeBB (const BB * const pBB, ICODE * hli, Int lev, Function * pProc, Int *numLoc)
  220. { Int i, last;
  221. char *line; /* Pointer to the HIGH-LEVEL line */
  222. /* Save the index into the code table in case there is a later goto
  223. * into this instruction (first instruction of the BB) */
  224. hli[pBB->start].codeIdx = nextBundleIdx (&cCode.code);
  225. /* Generate code for each hlicode that is not a HLI_JCOND */
  226. for (i = pBB->start, last = i + pBB->length; i < last; i++)
  227. if ((hli[i].type == HIGH_LEVEL) && (hli[i].invalid == FALSE))
  228. {
  229. line = write1HlIcode (hli[i].ic.hl, pProc, numLoc);
  230. if (line[0] != '\0')
  231. {
  232. cCode.appendCode( "%s%s", indent(lev), line);
  233. stats.numHLIcode++;
  234. }
  235. if (option.verbose)
  236. hli[i].writeDU(i);
  237. }
  238. //if (hli[i].invalid)
  239. //printf("Invalid icode: %d!\n", hli[i].invalid);
  240. }
  241. /* Recursive procedure that writes the code for the given procedure, pointed
  242. * to by pBB.
  243. * Parameters: pBB: pointer to the cfg.
  244. * Icode: pointer to the Icode array for the cfg graph of the
  245. * current procedure.
  246. * indLevel: indentation level - used for formatting.
  247. * numLoc: last # assigned to local variables */
  248. void BB::writeCode (Int indLevel, Function * pProc , Int *numLoc,Int latchNode, Int _ifFollow)
  249. {
  250. Int follow, /* ifFollow */
  251. _loopType, /* Type of loop, if any */
  252. _nodeType; /* Type of node */
  253. BB * succ, *latch; /* Successor and latching node */
  254. ICODE * picode; /* Pointer to HLI_JCOND instruction */
  255. char *l; /* Pointer to HLI_JCOND expression */
  256. boolT emptyThen, /* THEN clause is empty */
  257. repCond; /* Repeat condition for while() */
  258. /* Check if this basic block should be analysed */
  259. if ((_ifFollow != UN_INIT) && (this == pProc->dfsLast[_ifFollow]))
  260. return;
  261. if (traversed == DFS_ALPHA)
  262. return;
  263. traversed = DFS_ALPHA;
  264. /* Check for start of loop */
  265. repCond = FALSE;
  266. latch = NULL;
  267. _loopType = loopType;
  268. if (_loopType)
  269. {
  270. latch = pProc->dfsLast[this->latchNode];
  271. switch (_loopType)
  272. {
  273. case WHILE_TYPE:
  274. picode = pProc->Icode.GetIcode(start + length - 1);
  275. /* Check for error in while condition */
  276. if (picode->ic.hl.opcode != HLI_JCOND)
  277. reportError (WHILE_FAIL);
  278. /* Check if condition is more than 1 HL instruction */
  279. if (numHlIcodes > 1)
  280. {
  281. /* Write the code for this basic block */
  282. writeBB(this, pProc->Icode.GetFirstIcode(), indLevel, pProc, numLoc);
  283. repCond = TRUE;
  284. }
  285. /* Condition needs to be inverted if the loop body is along
  286. * the THEN path of the header node */
  287. if (edges[ELSE].BBptr->dfsLastNum == loopFollow)
  288. inverseCondOp (&picode->ic.hl.oper.exp);
  289. {
  290. std::string e=walkCondExpr (picode->ic.hl.oper.exp, pProc, numLoc);
  291. cCode.appendCode( "\n%swhile (%s) {\n", indent(indLevel),e.c_str());
  292. }
  293. picode->invalidate();
  294. break;
  295. case REPEAT_TYPE:
  296. cCode.appendCode( "\n%sdo {\n", indent(indLevel));
  297. picode = pProc->Icode.GetIcode(latch->start+latch->length-1);
  298. picode->invalidate();
  299. break;
  300. case ENDLESS_TYPE:
  301. cCode.appendCode( "\n%sfor (;;) {\n", indent(indLevel));
  302. }
  303. stats.numHLIcode += 1;
  304. indLevel++;
  305. }
  306. /* Write the code for this basic block */
  307. if (repCond == FALSE)
  308. writeBB (this, pProc->Icode.GetFirstIcode(), indLevel, pProc, numLoc);
  309. /* Check for end of path */
  310. _nodeType = nodeType;
  311. if (_nodeType == RETURN_NODE || _nodeType == TERMINATE_NODE ||
  312. _nodeType == NOWHERE_NODE || (dfsLastNum == latchNode))
  313. return;
  314. /* Check type of loop/node and process code */
  315. if (_loopType) /* there is a loop */
  316. {
  317. assert(latch);
  318. if (this != latch) /* loop is over several bbs */
  319. {
  320. if (_loopType == WHILE_TYPE)
  321. {
  322. succ = edges[THEN].BBptr;
  323. if (succ->dfsLastNum == loopFollow)
  324. succ = edges[ELSE].BBptr;
  325. }
  326. else
  327. succ = edges[0].BBptr;
  328. if (succ->traversed != DFS_ALPHA)
  329. succ->writeCode (indLevel, pProc, numLoc, latch->dfsLastNum,_ifFollow);
  330. else /* has been traversed so we need a goto */
  331. emitGotoLabel (pProc->Icode.GetIcode(succ->start), indLevel);
  332. }
  333. /* Loop epilogue: generate the loop trailer */
  334. indLevel--;
  335. if (_loopType == WHILE_TYPE)
  336. {
  337. /* Check if there is need to repeat other statements involved
  338. * in while condition, then, emit the loop trailer */
  339. if (repCond)
  340. writeBB (this, pProc->Icode.GetFirstIcode(), indLevel+1, pProc, numLoc);
  341. cCode.appendCode( "%s} /* end of while */\n",indent(indLevel));
  342. }
  343. else if (_loopType == ENDLESS_TYPE)
  344. cCode.appendCode( "%s} /* end of loop */\n",indent(indLevel));
  345. else if (_loopType == REPEAT_TYPE)
  346. {
  347. if (picode->ic.hl.opcode != HLI_JCOND)
  348. reportError (REPEAT_FAIL);
  349. {
  350. string e=walkCondExpr (picode->ic.hl.oper.exp, pProc, numLoc);
  351. cCode.appendCode( "%s} while (%s);\n", indent(indLevel),e.c_str());
  352. }
  353. }
  354. /* Recurse on the loop follow */
  355. if (loopFollow != MAX)
  356. {
  357. succ = pProc->dfsLast[loopFollow];
  358. if (succ->traversed != DFS_ALPHA)
  359. succ->writeCode (indLevel, pProc, numLoc, latchNode, _ifFollow);
  360. else /* has been traversed so we need a goto */
  361. emitGotoLabel (pProc->Icode.GetIcode(succ->start), indLevel);
  362. }
  363. }
  364. else /* no loop, process nodeType of the graph */
  365. {
  366. if (_nodeType == TWO_BRANCH) /* if-then[-else] */
  367. {
  368. stats.numHLIcode++;
  369. indLevel++;
  370. emptyThen = FALSE;
  371. if (ifFollow != MAX) /* there is a follow */
  372. {
  373. /* process the THEN part */
  374. follow = ifFollow;
  375. succ = edges[THEN].BBptr;
  376. if (succ->traversed != DFS_ALPHA) /* not visited */
  377. {
  378. if (succ->dfsLastNum != follow) /* THEN part */
  379. {
  380. l = writeJcond ( pProc->Icode.GetIcode(start + length -1)->ic.hl,
  381. pProc, numLoc);
  382. cCode.appendCode( "\n%s%s", indent(indLevel-1), l);
  383. succ->writeCode (indLevel, pProc, numLoc, latchNode,follow);
  384. }
  385. else /* empty THEN part => negate ELSE part */
  386. {
  387. l = writeJcondInv ( pProc->Icode.GetIcode(start + length -1)->ic.hl,
  388. pProc, numLoc);
  389. cCode.appendCode( "\n%s%s", indent(indLevel-1), l);
  390. edges[ELSE].BBptr->writeCode (indLevel, pProc, numLoc, latchNode, follow);
  391. emptyThen = TRUE;
  392. }
  393. }
  394. else /* already visited => emit label */
  395. emitGotoLabel (pProc->Icode.GetIcode(succ->start), indLevel);
  396. /* process the ELSE part */
  397. succ = edges[ELSE].BBptr;
  398. if (succ->traversed != DFS_ALPHA) /* not visited */
  399. {
  400. if (succ->dfsLastNum != follow) /* ELSE part */
  401. {
  402. cCode.appendCode( "%s}\n%selse {\n",
  403. indent(indLevel-1), indent(indLevel - 1));
  404. succ->writeCode (indLevel, pProc, numLoc, latchNode, follow);
  405. }
  406. /* else (empty ELSE part) */
  407. }
  408. else if (! emptyThen) /* already visited => emit label */
  409. {
  410. cCode.appendCode( "%s}\n%selse {\n",
  411. indent(indLevel-1), indent(indLevel - 1));
  412. emitGotoLabel (pProc->Icode.GetIcode(succ->start), indLevel);
  413. }
  414. cCode.appendCode( "%s}\n", indent(--indLevel));
  415. /* Continue with the follow */
  416. succ = pProc->dfsLast[follow];
  417. if (succ->traversed != DFS_ALPHA)
  418. succ->writeCode (indLevel, pProc, numLoc, latchNode,_ifFollow);
  419. }
  420. else /* no follow => if..then..else */
  421. {
  422. l = writeJcond (
  423. pProc->Icode.GetIcode(start + length -1)->ic.hl, pProc, numLoc);
  424. cCode.appendCode( "%s%s", indent(indLevel-1), l);
  425. edges[THEN].BBptr->writeCode (indLevel, pProc, numLoc, latchNode, _ifFollow);
  426. cCode.appendCode( "%s}\n%selse {\n", indent(indLevel-1), indent(indLevel - 1));
  427. edges[ELSE].BBptr->writeCode (indLevel, pProc, numLoc, latchNode, _ifFollow);
  428. cCode.appendCode( "%s}\n", indent(--indLevel));
  429. }
  430. }
  431. else /* fall, call, 1w */
  432. {
  433. succ = edges[0].BBptr; /* fall-through edge */
  434. if (succ->traversed != DFS_ALPHA)
  435. succ->writeCode (indLevel, pProc,numLoc, latchNode,_ifFollow);
  436. }
  437. }
  438. }
  439. /* Writes the procedure's declaration (including arguments), local variables,
  440. * and invokes the procedure that writes the code of the given record *hli */
  441. void Function::codeGen (std::ostream &fs)
  442. {
  443. Int i, numLoc;
  444. //STKFRAME * args; /* Procedure arguments */
  445. char buf[200], /* Procedure's definition */
  446. arg[30]; /* One argument */
  447. ID *locid; /* Pointer to one local identifier */
  448. BB *pBB; /* Pointer to basic block */
  449. /* Write procedure/function header */
  450. newBundle (&cCode);
  451. if (flg & PROC_IS_FUNC) /* Function */
  452. cCode.appendDecl( "\n%s %s (", hlTypes[retVal.type],name);
  453. else /* Procedure */
  454. cCode.appendDecl( "\nvoid %s (", name);
  455. /* Write arguments */
  456. memset (buf, 0, sizeof(buf));
  457. for (i = 0; i < args.sym.size(); i++)
  458. {
  459. if (args.sym[i].invalid == FALSE)
  460. {
  461. sprintf (arg,"%s %s",hlTypes[args.sym[i].type], args.sym[i].name);
  462. strcat (buf, arg);
  463. if (i < (args.numArgs - 1))
  464. strcat (buf, ", ");
  465. }
  466. }
  467. strcat (buf, ")\n");
  468. cCode.appendDecl( "%s", buf);
  469. /* Write comments */
  470. writeProcComments();
  471. /* Write local variables */
  472. if (! (flg & PROC_ASM))
  473. {
  474. numLoc = 0;
  475. for (i = 0; i < localId.csym(); i++)
  476. {
  477. locid = &localId.id_arr[i];
  478. /* Output only non-invalidated entries */
  479. if (locid->illegal == FALSE)
  480. {
  481. if (locid->loc == REG_FRAME)
  482. {
  483. /* Register variables are assigned to a local variable */
  484. if (((flg & SI_REGVAR) && (locid->id.regi == rSI)) ||
  485. ((flg & DI_REGVAR) && (locid->id.regi == rDI)))
  486. {
  487. sprintf (locid->name, "loc%ld", ++numLoc);
  488. cCode.appendDecl( "int %s;\n", locid->name);
  489. }
  490. /* Other registers are named when they are first used in
  491. * the output C code, and appended to the proc decl. */
  492. }
  493. else if (locid->loc == STK_FRAME)
  494. {
  495. /* Name local variables and output appropriate type */
  496. sprintf (locid->name, "loc%ld", ++numLoc);
  497. cCode.appendDecl( "%s %s;\n",hlTypes[locid->type], locid->name);
  498. }
  499. }
  500. }
  501. }
  502. /* Write procedure's code */
  503. if (flg & PROC_ASM) /* generate assembler */
  504. disassem (3, this);
  505. else /* generate C */
  506. cfg.front()->writeCode (1, this, &numLoc, MAX, UN_INIT);
  507. cCode.appendCode( "}\n\n");
  508. writeBundle (fs, cCode);
  509. freeBundle (&cCode);
  510. /* Write Live register analysis information */
  511. if (option.verbose)
  512. for (i = 0; i < numBBs; i++)
  513. {
  514. pBB = dfsLast[i];
  515. if (pBB->flg & INVALID_BB) continue; /* skip invalid BBs */
  516. printf ("BB %d\n", i);
  517. printf (" Start = %d, end = %d\n", pBB->start, pBB->start +
  518. pBB->length - 1);
  519. printf (" LiveUse = ");
  520. writeBitVector (pBB->liveUse);
  521. printf ("\n Def = ");
  522. writeBitVector (pBB->def);
  523. printf ("\n LiveOut = ");
  524. writeBitVector (pBB->liveOut);
  525. printf ("\n LiveIn = ");
  526. writeBitVector (pBB->liveIn);
  527. printf ("\n\n");
  528. }
  529. }
  530. /* Recursive procedure. Displays the procedure's code in depth-first order
  531. * of the call graph. */
  532. static void backBackEnd (char *filename, CALL_GRAPH * pcallGraph, std::ostream &ios)
  533. {
  534. Int i;
  535. // IFace.Yield(); /* This is a good place to yield to other apps */
  536. /* Check if this procedure has been processed already */
  537. if ((pcallGraph->proc->flg & PROC_OUTPUT) ||
  538. (pcallGraph->proc->flg & PROC_ISLIB))
  539. return;
  540. pcallGraph->proc->flg |= PROC_OUTPUT;
  541. /* Dfs if this procedure has any successors */
  542. for (i = 0; i < pcallGraph->outEdges.size(); i++)
  543. {
  544. backBackEnd (filename, pcallGraph->outEdges[i], ios);
  545. }
  546. /* Generate code for this procedure */
  547. stats.numLLIcode = pcallGraph->proc->Icode.GetNumIcodes();
  548. stats.numHLIcode = 0;
  549. pcallGraph->proc->codeGen (ios);
  550. /* Generate statistics */
  551. if (option.Stats)
  552. pcallGraph->proc->displayStats ();
  553. if (! (pcallGraph->proc->flg & PROC_ASM))
  554. {
  555. stats.totalLL += stats.numLLIcode;
  556. stats.totalHL += stats.numHLIcode;
  557. }
  558. }
  559. /* Invokes the necessary routines to produce code one procedure at a time. */
  560. void BackEnd (char *fileName, CALL_GRAPH * pcallGraph)
  561. {
  562. char* outName, *ext;
  563. std::ofstream fs; /* Output C file */
  564. /* Get output file name */
  565. outName = strcpy ((char*)allocMem(strlen(fileName)+1), fileName);
  566. if ((ext = strrchr (outName, '.')) != NULL)
  567. *ext = '\0';
  568. strcat (outName, ".b"); /* b for beta */
  569. /* Open output file */
  570. fs.open(outName);
  571. if(!fs.is_open())
  572. fatalError (CANNOT_OPEN, outName);
  573. printf ("dcc: Writing C beta file %s\n", outName);
  574. /* Header information */
  575. writeHeader (fs, fileName);
  576. /* Initialize total Icode instructions statistics */
  577. stats.totalLL = 0;
  578. stats.totalHL = 0;
  579. /* Process each procedure at a time */
  580. backBackEnd (fileName, pcallGraph, fs);
  581. /* Close output file */
  582. fs.close();
  583. printf ("dcc: Finished writing C beta file\n");
  584. }