ast.cpp 32 KB

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