ast.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*
  2. * File: ast.c
  3. * Purpose: Support module for abstract syntax trees.
  4. * Date: September 1993
  5. * (C) Cristina Cifuentes
  6. */
  7. #include <stdint.h>
  8. #include <malloc.h> /* For free() */
  9. #include <string>
  10. #include <sstream>
  11. #include <iostream>
  12. #include <cassert>
  13. #include "types.h"
  14. #include "dcc.h"
  15. using namespace std;
  16. /* Index registers **** temp solution */
  17. static const char *idxReg[8] = {"bx+si", "bx+di", "bp+si", "bp+di",
  18. "si", "di", "bp", "bx" };
  19. /* Conditional operator symbols in C. Index by condOp enumeration type */
  20. static const char *condOpSym[] = { " <= ", " < ", " == ", " != ", " > ", " >= ",
  21. " & ", " | ", " ^ ", " ~ ",
  22. " + ", " - ", " * ", " / ",
  23. " >> ", " << ", " % ", " && ", " || " };
  24. #define EXP_SIZE 200 /* Size of the expression buffer */
  25. /* Local expression stack */
  26. //typedef struct _EXP_STK {
  27. // COND_EXPR *exp;
  28. // struct _EXP_STK *next;
  29. //} EXP_STK;
  30. /* Returns the integer i in C hexadecimal format */
  31. static char *hexStr (uint16_t i)
  32. {
  33. static char buf[10];
  34. // i &= 0xFFFF;
  35. sprintf (buf, "%s%x", (i > 9) ? "0x" : "", i);
  36. return (buf);
  37. }
  38. /* Sets the du record for registers according to the du flag */
  39. void ICODE::setRegDU (byte regi, operDu du_in)
  40. {
  41. // printf("%s %d %x\n",__FUNCTION__,regi,int(du_in));
  42. switch (du_in)
  43. {
  44. case eDEF:
  45. du.def |= duReg[regi];
  46. du1.numRegsDef++;
  47. printf("%s du.def |= %x\n",__FUNCTION__,duReg[regi]);
  48. break;
  49. case eUSE:
  50. du.use |= duReg[regi];
  51. printf("%s du.use |= %x\n",__FUNCTION__,duReg[regi]);
  52. break;
  53. case USE_DEF:
  54. du.def |= duReg[regi];
  55. du1.numRegsDef++;
  56. printf("%s du.def |= %x\n",__FUNCTION__,duReg[regi]);
  57. printf("%s du.use |= %x\n",__FUNCTION__,duReg[regi]);
  58. du.use |= duReg[regi];
  59. break;
  60. case NONE: /* do nothing */
  61. break;
  62. }
  63. }
  64. /* Copies the def, use, or def and use fields of duIcode into pIcode */
  65. void ICODE::copyDU(const ICODE &duIcode, operDu _du, operDu duDu)
  66. {
  67. // printf("%s %d,%d from %d to %d\n",__FUNCTION__,int(du),int(duDu),duIcode->ic.ll.opcode,pIcode->ic.ll.opcode);
  68. switch (_du)
  69. {
  70. case eDEF:
  71. if (duDu == eDEF)
  72. du.def=duIcode.du.def;
  73. else
  74. du.def=duIcode.du.use;
  75. break;
  76. case eUSE:
  77. if (duDu == eDEF)
  78. du.use=duIcode.du.def;
  79. else
  80. du.use =duIcode.du.use;
  81. break;
  82. case USE_DEF:
  83. du = duIcode.du;
  84. break;
  85. case NONE:
  86. assert(false);
  87. break;
  88. }
  89. printf("%s end: %x,%x\n",__FUNCTION__,du.def,du.use);
  90. }
  91. /* Creates a conditional boolean expression and returns it */
  92. COND_EXPR *COND_EXPR::boolOp(COND_EXPR *lhs, COND_EXPR *rhs, condOp op)
  93. {
  94. //printf("%s:%d\n",__FUNCTION__,int(op));
  95. COND_EXPR *newExp;
  96. newExp = new COND_EXPR(BOOLEAN_OP);
  97. newExp->expr.boolExpr.op = op;
  98. newExp->expr.boolExpr.lhs = lhs;
  99. newExp->expr.boolExpr.rhs = rhs;
  100. return (newExp);
  101. }
  102. /* Returns a unary conditional expression node. This procedure should
  103. * only be used with the following conditional node types: NEGATION,
  104. * ADDRESSOF, DEREFERENCE, POST_INC, POST_DEC, PRE_INC, PRE_DEC */
  105. COND_EXPR *COND_EXPR::unary(condNodeType t, COND_EXPR *sub_expr)
  106. {
  107. COND_EXPR *newExp;
  108. newExp = new COND_EXPR(t);
  109. newExp->expr.unaryExp = sub_expr;
  110. return (newExp);
  111. }
  112. /* Returns an identifier conditional expression node of type GLOB_VAR */
  113. COND_EXPR *COND_EXPR::idGlob (int16 segValue, int16 off)
  114. {
  115. COND_EXPR *newExp;
  116. dword adr;
  117. size_t i;
  118. newExp = new COND_EXPR(IDENTIFIER);
  119. newExp->expr.ident.idType = GLOB_VAR;
  120. adr = opAdr(segValue, off);
  121. for (i = 0; i < symtab.size(); i++)
  122. if (symtab[i].label == adr)
  123. break;
  124. if (i == symtab.size())
  125. printf ("Error, glob var not found in symtab\n");
  126. newExp->expr.ident.idNode.globIdx = i;
  127. return (newExp);
  128. }
  129. /* Returns an identifier conditional expression node of type REGISTER */
  130. COND_EXPR *COND_EXPR::idReg(byte regi, flags32 icodeFlg, LOCAL_ID *locsym)
  131. {
  132. COND_EXPR *newExp;
  133. newExp = new COND_EXPR(IDENTIFIER);
  134. newExp->expr.ident.idType = REGISTER;
  135. if ((icodeFlg & B) || (icodeFlg & SRC_B))
  136. {
  137. newExp->expr.ident.idNode.regiIdx = locsym->newByteWordReg(TYPE_BYTE_SIGN, regi);
  138. newExp->expr.ident.regiType = BYTE_REG;
  139. }
  140. else /* word */
  141. {
  142. newExp->expr.ident.idNode.regiIdx = locsym->newByteWordReg( TYPE_WORD_SIGN, regi);
  143. newExp->expr.ident.regiType = WORD_REG;
  144. }
  145. return (newExp);
  146. }
  147. /* Returns an identifier conditional expression node of type REGISTER */
  148. COND_EXPR *COND_EXPR::idRegIdx(Int idx, regType reg_type)
  149. {
  150. COND_EXPR *newExp;
  151. newExp = new COND_EXPR(IDENTIFIER);
  152. newExp->expr.ident.idType = REGISTER;
  153. newExp->expr.ident.regiType = reg_type;
  154. newExp->expr.ident.idNode.regiIdx = idx;
  155. return (newExp);
  156. }
  157. /* Returns an identifier conditional expression node of type LOCAL_VAR */
  158. COND_EXPR *COND_EXPR::idLoc(Int off, LOCAL_ID *localId)
  159. {
  160. COND_EXPR *newExp;
  161. size_t i;
  162. newExp = new COND_EXPR(IDENTIFIER);
  163. newExp->expr.ident.idType = LOCAL_VAR;
  164. for (i = 0; i < localId->csym(); i++)
  165. if ((localId->id_arr[i].id.bwId.off == off) &&
  166. (localId->id_arr[i].id.bwId.regOff == 0))
  167. break;
  168. if (i == localId->csym())
  169. printf ("Error, cannot find local var\n");
  170. newExp->expr.ident.idNode.localIdx = i;
  171. sprintf (localId->id_arr[i].name, "loc%ld", i);
  172. return (newExp);
  173. }
  174. /* Returns an identifier conditional expression node of type PARAM */
  175. COND_EXPR *COND_EXPR::idParam(Int off, const STKFRAME * argSymtab)
  176. {
  177. COND_EXPR *newExp;
  178. size_t i;
  179. newExp = new COND_EXPR(IDENTIFIER);
  180. newExp->expr.ident.idType = PARAM;
  181. for (i = 0; i < argSymtab->sym.size(); i++)
  182. if (argSymtab->sym[i].off == off)
  183. break;
  184. if (i == argSymtab->sym.size()) printf ("Error, cannot find argument var\n");
  185. newExp->expr.ident.idNode.localIdx = i;
  186. return (newExp);
  187. }
  188. /* Returns an identifier conditional expression node of type GLOB_VAR_IDX.
  189. * This global variable is indexed by regi. */
  190. COND_EXPR *idCondExpIdxGlob (int16 segValue, int16 off, byte regi, const LOCAL_ID *locSym)
  191. {
  192. COND_EXPR *newExp;
  193. size_t i;
  194. newExp = new COND_EXPR(IDENTIFIER);
  195. newExp->expr.ident.idType = GLOB_VAR_IDX;
  196. for (i = 0; i < locSym->csym(); i++)
  197. if ((locSym->id_arr[i].id.bwGlb.seg == segValue) &&
  198. (locSym->id_arr[i].id.bwGlb.off == off) &&
  199. (locSym->id_arr[i].id.bwGlb.regi == regi))
  200. break;
  201. if (i == locSym->csym())
  202. printf ("Error, indexed-glob var not found in local id table\n");
  203. newExp->expr.ident.idNode.idxGlbIdx = i;
  204. return (newExp);
  205. }
  206. /* Returns an identifier conditional expression node of type CONSTANT */
  207. COND_EXPR *COND_EXPR::idKte(dword kte, byte size)
  208. {
  209. COND_EXPR *newExp = new COND_EXPR(IDENTIFIER);
  210. newExp->expr.ident.idType = CONSTANT;
  211. newExp->expr.ident.idNode.kte.kte = kte;
  212. newExp->expr.ident.idNode.kte.size = size;
  213. return (newExp);
  214. }
  215. /* Returns an identifier conditional expression node of type LONG_VAR,
  216. * that points to the given index idx. */
  217. COND_EXPR *COND_EXPR::idLongIdx (Int idx)
  218. {
  219. COND_EXPR *newExp = new COND_EXPR(IDENTIFIER);
  220. newExp->expr.ident.idType = LONG_VAR;
  221. newExp->expr.ident.idNode.longIdx = idx;
  222. return (newExp);
  223. }
  224. /* Returns an identifier conditional expression node of type LONG_VAR */
  225. COND_EXPR *COND_EXPR::idLong(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f, Int ix, operDu du, Int off)
  226. {
  227. Int idx;
  228. COND_EXPR *newExp = new COND_EXPR(IDENTIFIER);
  229. /* Check for long constant and save it as a constant expression */
  230. if ((sd == SRC) && ((pIcode->ic.ll.flg & I) == I)) /* constant */
  231. {
  232. newExp->expr.ident.idType = CONSTANT;
  233. if (f == HIGH_FIRST)
  234. newExp->expr.ident.idNode.kte.kte = (pIcode->ic.ll.immed.op << 16) +
  235. (pIcode+off)->ic.ll.immed.op;
  236. else /* LOW_FIRST */
  237. newExp->expr.ident.idNode.kte.kte =
  238. ((pIcode+off)->ic.ll.immed.op << 16)+ pIcode->ic.ll.immed.op;
  239. newExp->expr.ident.idNode.kte.size = 4;
  240. }
  241. /* Save it as a long expression (reg, stack or glob) */
  242. else
  243. {
  244. idx = localId->newLong(sd, &(*pIcode), f, ix, du, off);
  245. newExp->expr.ident.idType = LONG_VAR;
  246. newExp->expr.ident.idNode.longIdx = idx;
  247. }
  248. return (newExp);
  249. }
  250. /* Returns an identifier conditional expression node of type FUNCTION */
  251. COND_EXPR *COND_EXPR::idFunc(Function * pproc, STKFRAME * args)
  252. {
  253. COND_EXPR *newExp;
  254. newExp = new COND_EXPR(IDENTIFIER);
  255. newExp->expr.ident.idType = FUNCTION;
  256. newExp->expr.ident.idNode.call.proc = pproc;
  257. newExp->expr.ident.idNode.call.args = args;
  258. return (newExp);
  259. }
  260. /* Returns an identifier conditional expression node of type OTHER.
  261. * Temporary solution, should really be encoded as an indexed type (eg.
  262. * arrays). */
  263. COND_EXPR *COND_EXPR::idOther(byte seg, byte regi, int16 off)
  264. {
  265. COND_EXPR *newExp;
  266. newExp = new COND_EXPR(IDENTIFIER);
  267. newExp->expr.ident.idType = OTHER;
  268. newExp->expr.ident.idNode.other.seg = seg;
  269. newExp->expr.ident.idNode.other.regi = regi;
  270. newExp->expr.ident.idNode.other.off = off;
  271. return (newExp);
  272. }
  273. /* Returns an identifier conditional expression node of type TYPE_LONG or
  274. * TYPE_WORD_SIGN */
  275. COND_EXPR *COND_EXPR::idID (const ID *retVal, LOCAL_ID *locsym, Int ix)
  276. {
  277. COND_EXPR *newExp;
  278. Int idx;
  279. newExp = new COND_EXPR(IDENTIFIER);
  280. if (retVal->type == TYPE_LONG_SIGN)
  281. {
  282. idx = locsym->newLongReg (TYPE_LONG_SIGN, retVal->id.longId.h,retVal->id.longId.l, ix);
  283. newExp->expr.ident.idType = LONG_VAR;
  284. newExp->expr.ident.idNode.longIdx = idx;
  285. }
  286. else if (retVal->type == TYPE_WORD_SIGN)
  287. {
  288. newExp->expr.ident.idType = REGISTER;
  289. newExp->expr.ident.idNode.regiIdx = locsym->newByteWordReg(TYPE_WORD_SIGN, retVal->id.regi);
  290. newExp->expr.ident.regiType = WORD_REG;
  291. }
  292. return (newExp);
  293. }
  294. /* Returns an identifier conditional expression node, according to the given
  295. * type.
  296. * Arguments: i : index into the icode array, used for newLongRegId only.
  297. * duIcode: icode instruction that needs the du set.
  298. * du: operand is defined or used in current instruction. */
  299. COND_EXPR *COND_EXPR::id(const ICODE &pIcode, opLoc sd, Function * pProc, Int i,ICODE &duIcode, operDu du)
  300. {
  301. COND_EXPR *newExp;
  302. Int idx; /* idx into pIcode->localId table */
  303. const ICODEMEM * pm = (sd == SRC) ? &pIcode.ic.ll.src : &pIcode.ic.ll.dst;
  304. if (((sd == DST) && (pIcode.ic.ll.flg & IM_DST) == IM_DST) ||
  305. ((sd == SRC) && (pIcode.ic.ll.flg & IM_SRC)) ||
  306. (sd == LHS_OP)) /* for MUL lhs */
  307. { /* implicit dx:ax */
  308. idx = pProc->localId.newLongReg (TYPE_LONG_SIGN, rDX, rAX, i);
  309. newExp = COND_EXPR::idLongIdx (idx);
  310. duIcode.setRegDU (rDX, du);
  311. duIcode.setRegDU (rAX, du);
  312. }
  313. else if ((sd == DST) && (pIcode.ic.ll.flg & IM_TMP_DST) == IM_TMP_DST)
  314. { /* implicit tmp */
  315. newExp = COND_EXPR::idReg (rTMP, 0, &pProc->localId);
  316. duIcode.setRegDU(rTMP, (operDu)eUSE);
  317. }
  318. else if ((sd == SRC) && ((pIcode.ic.ll.flg & I) == I)) /* constant */
  319. newExp = COND_EXPR::idKte (pIcode.ic.ll.immed.op, 2);
  320. else if (pm->regi == 0) /* global variable */
  321. newExp = COND_EXPR::idGlob(pm->segValue, pm->off);
  322. else if (pm->regi < INDEXBASE) /* register */
  323. {
  324. newExp = COND_EXPR::idReg (pm->regi, (sd == SRC) ? pIcode.ic.ll.flg :
  325. pIcode.ic.ll.flg & NO_SRC_B, &pProc->localId);
  326. duIcode.setRegDU( pm->regi, du);
  327. }
  328. else if (pm->off) /* offset */
  329. {
  330. if ((pm->seg == rSS) && (pm->regi == INDEXBASE + 6)) /* idx on bp */
  331. {
  332. if (pm->off >= 0) /* argument */
  333. newExp = COND_EXPR::idParam (pm->off, &pProc->args);
  334. else /* local variable */
  335. newExp = COND_EXPR::idLoc (pm->off, &pProc->localId);
  336. }
  337. else if ((pm->seg == rDS) && (pm->regi == INDEXBASE + 7)) /* bx */
  338. {
  339. if (pm->off > 0) /* global variable */
  340. newExp = idCondExpIdxGlob (pm->segValue, pm->off, rBX,&pProc->localId);
  341. else
  342. newExp = COND_EXPR::idOther (pm->seg, pm->regi, pm->off);
  343. duIcode.setRegDU( rBX, eUSE);
  344. }
  345. else /* idx <> bp, bx */
  346. newExp = COND_EXPR::idOther (pm->seg, pm->regi, pm->off);
  347. /**** check long ops, indexed global var *****/
  348. }
  349. else /* (pm->regi >= INDEXBASE && pm->off = 0) => indexed && no off */
  350. {
  351. if ((pm->seg == rDS) && (pm->regi > INDEXBASE + 3)) /* dereference */
  352. {
  353. switch (pm->regi) {
  354. case INDEXBASE + 4: newExp = COND_EXPR::idReg(rSI, 0, &pProc->localId);
  355. duIcode.setRegDU( rSI, du);
  356. break;
  357. case INDEXBASE + 5: newExp = COND_EXPR::idReg(rDI, 0, &pProc->localId);
  358. duIcode.setRegDU( rDI, du);
  359. break;
  360. case INDEXBASE + 6: newExp = COND_EXPR::idReg(rBP, 0, &pProc->localId);
  361. break;
  362. case INDEXBASE + 7: newExp = COND_EXPR::idReg(rBX, 0, &pProc->localId);
  363. duIcode.setRegDU( rBX, du);
  364. break;
  365. default:
  366. newExp = 0;
  367. assert(false);
  368. }
  369. newExp = COND_EXPR::unary (DEREFERENCE, newExp);
  370. }
  371. else
  372. newExp = COND_EXPR::idOther (pm->seg, pm->regi, 0);
  373. }
  374. return (newExp);
  375. }
  376. /* Returns the identifier type */
  377. condId ICODE::idType(opLoc sd)
  378. {
  379. ICODEMEM *pm;
  380. pm = (sd == SRC) ? &ic.ll.src : &ic.ll.dst;
  381. if ((sd == SRC) && ((ic.ll.flg & I) == I))
  382. return (CONSTANT);
  383. else if (pm->regi == 0)
  384. return (GLOB_VAR);
  385. else if (pm->regi < INDEXBASE)
  386. return (REGISTER);
  387. else if ((pm->seg == rSS) && (pm->regi == INDEXBASE))
  388. {
  389. if (pm->off >= 0)
  390. return (PARAM);
  391. else
  392. return (LOCAL_VAR);
  393. }
  394. else
  395. return (OTHER);
  396. }
  397. /* Size of hl types */
  398. Int hlSize[] = {2, 1, 1, 2, 2, 4, 4, 4, 2, 2, 1, 4, 4};
  399. /* Returns the type of the expression */
  400. Int hlTypeSize (const COND_EXPR *expr, Function * pproc)
  401. {
  402. Int first, second;
  403. if (expr == NULL)
  404. return (2); /* for TYPE_UNKNOWN */
  405. switch (expr->type) {
  406. case BOOLEAN_OP:
  407. first = hlTypeSize (expr->expr.boolExpr.lhs, pproc);
  408. second = hlTypeSize (expr->expr.boolExpr.rhs, pproc);
  409. if (first > second)
  410. return (first);
  411. else
  412. return (second);
  413. case NEGATION: case ADDRESSOF:
  414. case POST_INC: case POST_DEC:
  415. case PRE_INC: case PRE_DEC:
  416. case DEREFERENCE: return (hlTypeSize (expr->expr.unaryExp, pproc));
  417. case IDENTIFIER:
  418. switch (expr->expr.ident.idType)
  419. {
  420. case GLOB_VAR:
  421. return (symtab[expr->expr.ident.idNode.globIdx].size);
  422. case REGISTER:
  423. if (expr->expr.ident.regiType == BYTE_REG)
  424. return (1);
  425. else
  426. return (2);
  427. case LOCAL_VAR:
  428. return (hlSize[pproc->localId.id_arr[expr->expr.ident.idNode.localIdx].type]);
  429. case PARAM:
  430. return (hlSize[pproc->args.sym[expr->expr.ident.idNode.paramIdx].type]);
  431. case GLOB_VAR_IDX:
  432. return (hlSize[pproc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].type]);
  433. case CONSTANT:
  434. return (expr->expr.ident.idNode.kte.size);
  435. case STRING:
  436. return (2);
  437. case LONG_VAR:
  438. return (4);
  439. case FUNCTION:
  440. return (hlSize[expr->expr.ident.idNode.call.proc->retVal.type]);
  441. case OTHER:
  442. return (2);
  443. } /* eos */
  444. break;
  445. }
  446. return 2; // CC: is this correct?
  447. }
  448. /* Returns the type of the expression */
  449. hlType expType (const COND_EXPR *expr, Function * pproc)
  450. {
  451. hlType first, second;
  452. if (expr == NULL)
  453. return (TYPE_UNKNOWN);
  454. switch (expr->type)
  455. {
  456. case BOOLEAN_OP:
  457. first = expType (expr->expr.boolExpr.lhs, pproc);
  458. second = expType (expr->expr.boolExpr.rhs, pproc);
  459. if (first != second)
  460. {
  461. if (hlTypeSize (expr->expr.boolExpr.lhs, pproc) >
  462. hlTypeSize (expr->expr.boolExpr.rhs, pproc))
  463. return (first);
  464. else
  465. return (second);
  466. }
  467. else
  468. return (first);
  469. case POST_INC: case POST_DEC:
  470. case PRE_INC: case PRE_DEC:
  471. case NEGATION:
  472. return (expType (expr->expr.unaryExp, pproc));
  473. case ADDRESSOF: return (TYPE_PTR); /***????****/
  474. case DEREFERENCE: return (TYPE_PTR);
  475. case IDENTIFIER:
  476. switch (expr->expr.ident.idType)
  477. {
  478. case GLOB_VAR:
  479. return (symtab[expr->expr.ident.idNode.globIdx].type);
  480. case REGISTER:
  481. if (expr->expr.ident.regiType == BYTE_REG)
  482. return (TYPE_BYTE_SIGN);
  483. else
  484. return (TYPE_WORD_SIGN);
  485. case LOCAL_VAR:
  486. return (pproc->localId.id_arr[expr->expr.ident.idNode.localIdx].type);
  487. case PARAM:
  488. return (pproc->args.sym[expr->expr.ident.idNode.paramIdx].type);
  489. case GLOB_VAR_IDX:
  490. return (pproc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].type);
  491. case CONSTANT:
  492. return (TYPE_CONST);
  493. case STRING:
  494. return (TYPE_STR);
  495. case LONG_VAR:
  496. return (pproc->localId.id_arr[expr->expr.ident.idNode.longIdx].type);
  497. case FUNCTION:
  498. return (expr->expr.ident.idNode.call.proc->retVal.type);
  499. case OTHER:
  500. return (TYPE_UNKNOWN);
  501. } /* eos */
  502. case UNKNOWN_OP:
  503. assert(false);
  504. return (TYPE_UNKNOWN);
  505. }
  506. return TYPE_UNKNOWN; // CC: Correct?
  507. }
  508. /* Removes the register from the tree. If the register was part of a long
  509. * register (eg. dx:ax), the node gets transformed into an integer register
  510. * node. */
  511. void removeRegFromLong (byte regi, LOCAL_ID *locId, COND_EXPR *tree)
  512. {
  513. IDENTTYPE* ident; /* ptr to an identifier */
  514. byte otherRegi; /* high or low part of long register */
  515. switch (tree->type) {
  516. case BOOLEAN_OP:
  517. break;
  518. case POST_INC: case POST_DEC:
  519. case PRE_INC: case PRE_DEC:
  520. case NEGATION: case ADDRESSOF:
  521. case DEREFERENCE:
  522. break;
  523. case IDENTIFIER:
  524. ident = &tree->expr.ident;
  525. if (ident->idType == LONG_VAR)
  526. {
  527. otherRegi = otherLongRegi (regi, ident->idNode.longIdx, locId);
  528. ident->idType = REGISTER;
  529. ident->regiType = WORD_REG;
  530. ident->idNode.regiIdx = locId->newByteWordReg(TYPE_WORD_SIGN,otherRegi);
  531. }
  532. break;
  533. }
  534. }
  535. /* Returns the string located in image, formatted in C format. */
  536. static std::string getString (Int offset)
  537. {
  538. ostringstream o;
  539. Int strLen, i;
  540. strLen = strSize (&prog.Image[offset], '\0');
  541. o << '"';
  542. for (i = 0; i < strLen; i++)
  543. o<<cChar(prog.Image[offset+i]);
  544. o << "\"\0";
  545. return (o.str());
  546. }
  547. /* Walks the conditional expression tree and returns the result on a string */
  548. // TODO: use string stream here
  549. string walkCondExpr (const COND_EXPR* expr, Function * pProc, Int* numLoc)
  550. {
  551. int16 off; /* temporal - for OTHER */
  552. ID* id; /* Pointer to local identifier table */
  553. //char* o; /* Operand string pointer */
  554. bool needBracket; /* Determine whether parenthesis is needed */
  555. BWGLB_TYPE* bwGlb; /* Ptr to BWGLB_TYPE (global indexed var) */
  556. STKSYM * psym; /* Pointer to argument in the stack */
  557. std::ostringstream outStr;
  558. if (expr == NULL)
  559. return "";
  560. needBracket = true;
  561. switch (expr->type)
  562. {
  563. case BOOLEAN_OP:
  564. outStr << "(";
  565. outStr << walkCondExpr(expr->expr.boolExpr.lhs, pProc, numLoc);
  566. outStr << condOpSym[expr->expr.boolExpr.op];
  567. outStr << walkCondExpr(expr->expr.boolExpr.rhs, pProc, numLoc);
  568. outStr << ")";
  569. break;
  570. case NEGATION:
  571. if (expr->expr.unaryExp->type == IDENTIFIER)
  572. {
  573. needBracket = FALSE;
  574. outStr << "!";
  575. }
  576. else
  577. outStr << "! (";
  578. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  579. if (needBracket == TRUE)
  580. outStr << ")";
  581. break;
  582. case ADDRESSOF:
  583. if (expr->expr.unaryExp->type == IDENTIFIER)
  584. {
  585. needBracket = FALSE;
  586. outStr << "&";
  587. }
  588. else
  589. outStr << "&(";
  590. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  591. if (needBracket == TRUE)
  592. outStr << ")";
  593. break;
  594. case DEREFERENCE:
  595. outStr << "*";
  596. if (expr->expr.unaryExp->type == IDENTIFIER)
  597. needBracket = FALSE;
  598. else
  599. outStr << "(";
  600. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  601. if (needBracket == TRUE)
  602. outStr << ")";
  603. break;
  604. case POST_INC:
  605. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc) << "++";
  606. break;
  607. case POST_DEC:
  608. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc) << "--";
  609. break;
  610. case PRE_INC:
  611. outStr << "++"<< walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  612. break;
  613. case PRE_DEC:
  614. outStr << "--"<< walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  615. break;
  616. case IDENTIFIER:
  617. std::ostringstream o;
  618. switch (expr->expr.ident.idType)
  619. {
  620. case GLOB_VAR:
  621. o << symtab[expr->expr.ident.idNode.globIdx].name;
  622. break;
  623. case REGISTER:
  624. id = &pProc->localId.id_arr[expr->expr.ident.idNode.regiIdx];
  625. if (id->name[0] == '\0') /* no name */
  626. {
  627. sprintf (id->name, "loc%ld", ++(*numLoc));
  628. if (id->id.regi < rAL)
  629. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,wordReg[id->id.regi - rAX]);
  630. else
  631. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,byteReg[id->id.regi - rAL]);
  632. }
  633. if (id->hasMacro)
  634. o << id->macro << "("<<id->name<<")";
  635. else
  636. o << id->name;
  637. break;
  638. case LOCAL_VAR:
  639. o << pProc->localId.id_arr[expr->expr.ident.idNode.localIdx].name;
  640. break;
  641. case PARAM:
  642. psym = &pProc->args.sym[expr->expr.ident.idNode.paramIdx];
  643. if (psym->hasMacro)
  644. o << psym->macro<<"("<<psym->name<< ")";
  645. else
  646. o << psym->name;
  647. break;
  648. case GLOB_VAR_IDX:
  649. bwGlb = &pProc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].id.bwGlb;
  650. o << (bwGlb->seg << 4) + bwGlb->off << "["<<wordReg[bwGlb->regi - rAX]<<"]";
  651. break;
  652. case CONSTANT:
  653. if (expr->expr.ident.idNode.kte.kte < 1000)
  654. o << expr->expr.ident.idNode.kte.kte;
  655. else
  656. o << "0x"<<std::hex << expr->expr.ident.idNode.kte.kte;
  657. break;
  658. case STRING:
  659. o << getString (expr->expr.ident.idNode.strIdx);
  660. break;
  661. case LONG_VAR:
  662. id = &pProc->localId.id_arr[expr->expr.ident.idNode.longIdx];
  663. if (id->name[0] != '\0') /* STK_FRAME & REG w/name*/
  664. o << id->name;
  665. else if (id->loc == REG_FRAME)
  666. {
  667. sprintf (id->name, "loc%ld", ++(*numLoc));
  668. cCode.appendDecl("%s %s; /* %s:%s */\n",hlTypes[id->type], id->name,wordReg[id->id.longId.h - rAX],wordReg[id->id.longId.l - rAX]);
  669. o << id->name;
  670. pProc->localId.propLongId (id->id.longId.l,id->id.longId.h, id->name);
  671. }
  672. else /* GLB_FRAME */
  673. {
  674. if (id->id.longGlb.regi == 0) /* not indexed */
  675. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"]";
  676. else if (id->id.longGlb.regi == rBX)
  677. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"][bx]";
  678. }
  679. break;
  680. case FUNCTION:
  681. o << writeCall (expr->expr.ident.idNode.call.proc,expr->expr.ident.idNode.call.args, pProc, numLoc);
  682. break;
  683. case OTHER:
  684. off = expr->expr.ident.idNode.other.off;
  685. o << wordReg[expr->expr.ident.idNode.other.seg - rAX]<< "[";
  686. o << idxReg[expr->expr.ident.idNode.other.regi - INDEXBASE];
  687. if (off < 0)
  688. o << "-"<< hexStr (-off);
  689. else if (off>0)
  690. o << "+"<< hexStr (off);
  691. o << "]";
  692. } /* eos */
  693. outStr << o.str();
  694. break;
  695. }
  696. return outStr.str();
  697. }
  698. /* Makes a copy of the given expression. Allocates newExp storage for each
  699. * node. Returns the copy. */
  700. COND_EXPR *COND_EXPR::clone()
  701. {
  702. COND_EXPR* newExp=0; /* Expression node copy */
  703. switch (type)
  704. {
  705. case BOOLEAN_OP:
  706. newExp = new COND_EXPR(*this);
  707. newExp->expr.boolExpr.lhs = expr.boolExpr.lhs->clone();
  708. newExp->expr.boolExpr.rhs = expr.boolExpr.rhs->clone();
  709. break;
  710. case NEGATION:
  711. case ADDRESSOF:
  712. case DEREFERENCE:
  713. newExp = new COND_EXPR(*this);
  714. newExp->expr.unaryExp = expr.unaryExp->clone();
  715. break;
  716. case IDENTIFIER:
  717. return new COND_EXPR(*this);
  718. }
  719. return (newExp);
  720. }
  721. /* Changes the boolean conditional operator at the root of this expression */
  722. void COND_EXPR::changeBoolOp (condOp newOp)
  723. {
  724. expr.boolExpr.op = newOp;
  725. }
  726. /* Inserts the expression exp into the tree at the location specified by the
  727. * register regi */
  728. bool insertSubTreeReg (COND_EXPR *expr, COND_EXPR **tree, byte regi,LOCAL_ID *locsym)
  729. {
  730. byte treeReg;
  731. if (*tree == NULL)
  732. return FALSE;
  733. switch ((*tree)->type) {
  734. case IDENTIFIER:
  735. if ((*tree)->expr.ident.idType == REGISTER)
  736. {
  737. treeReg = locsym->id_arr[(*tree)->expr.ident.idNode.regiIdx].id.regi;
  738. if (treeReg == regi) /* word reg */
  739. {
  740. *tree = expr;
  741. return TRUE;
  742. }
  743. else if ((regi >= rAX) && (regi <= rBX)) /* word/byte reg */
  744. {
  745. if ((treeReg == (regi + rAL-1)) || (treeReg == (regi + rAH-1)))
  746. {
  747. *tree = expr;
  748. return TRUE;
  749. }
  750. }
  751. }
  752. return FALSE;
  753. case BOOLEAN_OP:
  754. if (insertSubTreeReg (expr, &(*tree)->expr.boolExpr.lhs, regi, locsym))
  755. return TRUE;
  756. if (insertSubTreeReg (expr, &(*tree)->expr.boolExpr.rhs, regi, locsym))
  757. return TRUE;
  758. return FALSE;
  759. case NEGATION:
  760. case ADDRESSOF:
  761. case DEREFERENCE:
  762. if (insertSubTreeReg(expr, &(*tree)->expr.unaryExp,regi, locsym))
  763. return TRUE;
  764. return FALSE;
  765. }
  766. return FALSE;
  767. }
  768. /* Inserts the expression exp into the tree at the location specified by the
  769. * long register index longIdx*/
  770. bool insertSubTreeLongReg(COND_EXPR *exp, COND_EXPR **tree, Int longIdx)
  771. {
  772. switch ((*tree)->type)
  773. {
  774. case IDENTIFIER:
  775. if ((*tree)->expr.ident.idNode.longIdx == longIdx)
  776. {
  777. *tree = exp;
  778. return true;
  779. }
  780. return false;
  781. case BOOLEAN_OP:
  782. if (insertSubTreeLongReg (exp, &(*tree)->expr.boolExpr.lhs, longIdx))
  783. return true;
  784. if (insertSubTreeLongReg (exp, &(*tree)->expr.boolExpr.rhs, longIdx))
  785. return true;
  786. return false;
  787. case NEGATION:
  788. case ADDRESSOF:
  789. case DEREFERENCE:
  790. if (insertSubTreeLongReg (exp, &(*tree)->expr.unaryExp, longIdx))
  791. return true;
  792. return false;
  793. }
  794. return false;
  795. }
  796. /* Recursively deallocates the abstract syntax tree rooted at *exp */
  797. void COND_EXPR::release()
  798. {
  799. switch (type)
  800. {
  801. case BOOLEAN_OP:
  802. expr.boolExpr.lhs->release();
  803. expr.boolExpr.rhs->release();
  804. break;
  805. case NEGATION:
  806. case ADDRESSOF:
  807. case DEREFERENCE:
  808. expr.unaryExp->release();
  809. break;
  810. }
  811. delete (this);
  812. }