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.csym; i++)
  134. if (symtab.sym[i].label == adr)
  135. break;
  136. if (i == symtab.csym)
  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, ICODE *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(ICODE *pIcode, opLoc sd, Function * pProc, Int i,ICODE *duIcode, operDu du)
  315. {
  316. COND_EXPR *newExp;
  317. ICODEMEM * pm;
  318. Int idx; /* idx into pIcode->localId table */
  319. pm = (sd == SRC) ? &pIcode->ic.ll.src : &pIcode->ic.ll.dst;
  320. if (((sd == DST) && (pIcode->ic.ll.flg & IM_DST) == IM_DST) ||
  321. ((sd == SRC) && (pIcode->ic.ll.flg & IM_SRC)) ||
  322. (sd == LHS_OP)) /* for MUL lhs */
  323. { /* implicit dx:ax */
  324. idx = pProc->localId.newLongReg (TYPE_LONG_SIGN, rDX, rAX, i);
  325. newExp = COND_EXPR::idLongIdx (idx);
  326. duIcode->setRegDU (rDX, du);
  327. duIcode->setRegDU (rAX, du);
  328. }
  329. else if ((sd == DST) && (pIcode->ic.ll.flg & IM_TMP_DST) == IM_TMP_DST)
  330. { /* implicit tmp */
  331. newExp = COND_EXPR::idReg (rTMP, 0, &pProc->localId);
  332. duIcode->setRegDU(rTMP, (operDu)eUSE);
  333. }
  334. else if ((sd == SRC) && ((pIcode->ic.ll.flg & I) == I)) /* constant */
  335. newExp = COND_EXPR::idKte (pIcode->ic.ll.immed.op, 2);
  336. else if (pm->regi == 0) /* global variable */
  337. newExp = COND_EXPR::idGlob(pm->segValue, pm->off);
  338. else if (pm->regi < INDEXBASE) /* register */
  339. {
  340. newExp = COND_EXPR::idReg (pm->regi, (sd == SRC) ? pIcode->ic.ll.flg :
  341. pIcode->ic.ll.flg & NO_SRC_B, &pProc->localId);
  342. duIcode->setRegDU( pm->regi, du);
  343. }
  344. else if (pm->off) /* offset */
  345. {
  346. if ((pm->seg == rSS) && (pm->regi == INDEXBASE + 6)) /* idx on bp */
  347. {
  348. if (pm->off >= 0) /* argument */
  349. newExp = COND_EXPR::idParam (pm->off, &pProc->args);
  350. else /* local variable */
  351. newExp = COND_EXPR::idLoc (pm->off, &pProc->localId);
  352. }
  353. else if ((pm->seg == rDS) && (pm->regi == INDEXBASE + 7)) /* bx */
  354. {
  355. if (pm->off > 0) /* global variable */
  356. newExp = idCondExpIdxGlob (pm->segValue, pm->off, rBX,&pProc->localId);
  357. else
  358. newExp = COND_EXPR::idOther (pm->seg, pm->regi, pm->off);
  359. duIcode->setRegDU( rBX, eUSE);
  360. }
  361. else /* idx <> bp, bx */
  362. newExp = COND_EXPR::idOther (pm->seg, pm->regi, pm->off);
  363. /**** check long ops, indexed global var *****/
  364. }
  365. else /* (pm->regi >= INDEXBASE && pm->off = 0) => indexed && no off */
  366. {
  367. if ((pm->seg == rDS) && (pm->regi > INDEXBASE + 3)) /* dereference */
  368. {
  369. switch (pm->regi) {
  370. case INDEXBASE + 4: newExp = COND_EXPR::idReg(rSI, 0, &pProc->localId);
  371. duIcode->setRegDU( rSI, du);
  372. break;
  373. case INDEXBASE + 5: newExp = COND_EXPR::idReg(rDI, 0, &pProc->localId);
  374. duIcode->setRegDU( rDI, du);
  375. break;
  376. case INDEXBASE + 6: newExp = COND_EXPR::idReg(rBP, 0, &pProc->localId);
  377. break;
  378. case INDEXBASE + 7: newExp = COND_EXPR::idReg(rBX, 0, &pProc->localId);
  379. duIcode->setRegDU( rBX, du);
  380. break;
  381. default:
  382. newExp = 0;
  383. assert(false);
  384. }
  385. newExp = COND_EXPR::unary (DEREFERENCE, newExp);
  386. }
  387. else
  388. newExp = COND_EXPR::idOther (pm->seg, pm->regi, 0);
  389. }
  390. return (newExp);
  391. }
  392. /* Returns the identifier type */
  393. condId ICODE::idType(opLoc sd)
  394. {
  395. ICODEMEM *pm;
  396. pm = (sd == SRC) ? &ic.ll.src : &ic.ll.dst;
  397. if ((sd == SRC) && ((ic.ll.flg & I) == I))
  398. return (CONSTANT);
  399. else if (pm->regi == 0)
  400. return (GLOB_VAR);
  401. else if (pm->regi < INDEXBASE)
  402. return (REGISTER);
  403. else if ((pm->seg == rSS) && (pm->regi == INDEXBASE))
  404. {
  405. if (pm->off >= 0)
  406. return (PARAM);
  407. else
  408. return (LOCAL_VAR);
  409. }
  410. else
  411. return (OTHER);
  412. }
  413. /* Size of hl types */
  414. Int hlSize[] = {2, 1, 1, 2, 2, 4, 4, 4, 2, 2, 1, 4, 4};
  415. /* Returns the type of the expression */
  416. Int hlTypeSize (const COND_EXPR *expr, Function * pproc)
  417. {
  418. Int first, second;
  419. if (expr == NULL)
  420. return (2); /* for TYPE_UNKNOWN */
  421. switch (expr->type) {
  422. case BOOLEAN_OP:
  423. first = hlTypeSize (expr->expr.boolExpr.lhs, pproc);
  424. second = hlTypeSize (expr->expr.boolExpr.rhs, pproc);
  425. if (first > second)
  426. return (first);
  427. else
  428. return (second);
  429. case NEGATION: case ADDRESSOF:
  430. case POST_INC: case POST_DEC:
  431. case PRE_INC: case PRE_DEC:
  432. case DEREFERENCE: return (hlTypeSize (expr->expr.unaryExp, pproc));
  433. case IDENTIFIER:
  434. switch (expr->expr.ident.idType)
  435. {
  436. case GLOB_VAR:
  437. return (symtab.sym[expr->expr.ident.idNode.globIdx].size);
  438. case REGISTER:
  439. if (expr->expr.ident.regiType == BYTE_REG)
  440. return (1);
  441. else
  442. return (2);
  443. case LOCAL_VAR:
  444. return (hlSize[pproc->localId.id_arr[expr->expr.ident.idNode.localIdx].type]);
  445. case PARAM:
  446. return (hlSize[pproc->args.sym[expr->expr.ident.idNode.paramIdx].type]);
  447. case GLOB_VAR_IDX:
  448. return (hlSize[pproc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].type]);
  449. case CONSTANT:
  450. return (expr->expr.ident.idNode.kte.size);
  451. case STRING:
  452. return (2);
  453. case LONG_VAR:
  454. return (4);
  455. case FUNCTION:
  456. return (hlSize[expr->expr.ident.idNode.call.proc->retVal.type]);
  457. case OTHER:
  458. return (2);
  459. } /* eos */
  460. break;
  461. }
  462. return 2; // CC: is this correct?
  463. }
  464. /* Returns the type of the expression */
  465. hlType expType (const COND_EXPR *expr, Function * pproc)
  466. {
  467. hlType first, second;
  468. if (expr == NULL)
  469. return (TYPE_UNKNOWN);
  470. switch (expr->type)
  471. {
  472. case BOOLEAN_OP:
  473. first = expType (expr->expr.boolExpr.lhs, pproc);
  474. second = expType (expr->expr.boolExpr.rhs, pproc);
  475. if (first != second)
  476. {
  477. if (hlTypeSize (expr->expr.boolExpr.lhs, pproc) >
  478. hlTypeSize (expr->expr.boolExpr.rhs, pproc))
  479. return (first);
  480. else
  481. return (second);
  482. }
  483. else
  484. return (first);
  485. case POST_INC: case POST_DEC:
  486. case PRE_INC: case PRE_DEC:
  487. case NEGATION: return (expType (expr->expr.unaryExp, pproc));
  488. case ADDRESSOF: return (TYPE_PTR); /***????****/
  489. case DEREFERENCE: return (TYPE_PTR);
  490. case IDENTIFIER:
  491. switch (expr->expr.ident.idType)
  492. {
  493. case GLOB_VAR:
  494. return (symtab.sym[expr->expr.ident.idNode.globIdx].type);
  495. case REGISTER:
  496. if (expr->expr.ident.regiType == BYTE_REG)
  497. return (TYPE_BYTE_SIGN);
  498. else
  499. return (TYPE_WORD_SIGN);
  500. case LOCAL_VAR:
  501. return (pproc->localId.id_arr[expr->expr.ident.idNode.localIdx].type);
  502. case PARAM:
  503. return (pproc->args.sym[expr->expr.ident.idNode.paramIdx].type);
  504. case GLOB_VAR_IDX:
  505. return (pproc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].type);
  506. case CONSTANT:
  507. return (TYPE_CONST);
  508. case STRING:
  509. return (TYPE_STR);
  510. case LONG_VAR:
  511. return (pproc->localId.id_arr[expr->expr.ident.idNode.longIdx].type);
  512. case FUNCTION:
  513. return (expr->expr.ident.idNode.call.proc->retVal.type);
  514. case OTHER:
  515. return (TYPE_UNKNOWN);
  516. } /* eos */
  517. case UNKNOWN_OP:
  518. assert(false);
  519. return (TYPE_UNKNOWN);
  520. }
  521. return TYPE_UNKNOWN; // CC: Correct?
  522. }
  523. /* Removes the register from the tree. If the register was part of a long
  524. * register (eg. dx:ax), the node gets transformed into an integer register
  525. * node. */
  526. void removeRegFromLong (byte regi, LOCAL_ID *locId, COND_EXPR *tree)
  527. {
  528. IDENTTYPE* ident; /* ptr to an identifier */
  529. byte otherRegi; /* high or low part of long register */
  530. switch (tree->type) {
  531. case BOOLEAN_OP:
  532. break;
  533. case POST_INC: case POST_DEC:
  534. case PRE_INC: case PRE_DEC:
  535. case NEGATION: case ADDRESSOF:
  536. case DEREFERENCE:
  537. break;
  538. case IDENTIFIER:
  539. ident = &tree->expr.ident;
  540. if (ident->idType == LONG_VAR)
  541. {
  542. otherRegi = otherLongRegi (regi, ident->idNode.longIdx, locId);
  543. ident->idType = REGISTER;
  544. ident->regiType = WORD_REG;
  545. ident->idNode.regiIdx = locId->newByteWordReg(TYPE_WORD_SIGN,otherRegi);
  546. }
  547. break;
  548. }
  549. }
  550. /* Returns the string located in image, formatted in C format. */
  551. static std::string getString (Int offset)
  552. {
  553. ostringstream o;
  554. Int strLen, i;
  555. strLen = strSize (&prog.Image[offset], '\0');
  556. o << '"';
  557. for (i = 0; i < strLen; i++)
  558. o<<cChar(prog.Image[offset+i]);
  559. o << "\"\0";
  560. return (o.str());
  561. }
  562. /* Walks the conditional expression tree and returns the result on a string */
  563. // TODO: use string stream here
  564. string walkCondExpr (const COND_EXPR* expr, Function * pProc, Int* numLoc)
  565. {
  566. int16 off; /* temporal - for OTHER */
  567. ID* id; /* Pointer to local identifier table */
  568. char* o; /* Operand string pointer */
  569. boolT needBracket; /* Determine whether parenthesis is needed */
  570. BWGLB_TYPE* bwGlb; /* Ptr to BWGLB_TYPE (global indexed var) */
  571. STKSYM * psym; /* Pointer to argument in the stack */
  572. std::ostringstream outStr;
  573. if (expr == NULL)
  574. return "";
  575. needBracket = TRUE;
  576. switch (expr->type)
  577. {
  578. case BOOLEAN_OP:
  579. outStr << "(";
  580. outStr << walkCondExpr(expr->expr.boolExpr.lhs, pProc, numLoc);
  581. outStr << condOpSym[expr->expr.boolExpr.op];
  582. outStr << walkCondExpr(expr->expr.boolExpr.rhs, pProc, numLoc);
  583. outStr << ")";
  584. break;
  585. case NEGATION:
  586. if (expr->expr.unaryExp->type == IDENTIFIER)
  587. {
  588. needBracket = FALSE;
  589. outStr << "!";
  590. }
  591. else
  592. outStr << "! (";
  593. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  594. if (needBracket == TRUE)
  595. outStr << ")";
  596. break;
  597. case ADDRESSOF:
  598. if (expr->expr.unaryExp->type == IDENTIFIER)
  599. {
  600. needBracket = FALSE;
  601. outStr << "&";
  602. }
  603. else
  604. outStr << "&(";
  605. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  606. if (needBracket == TRUE)
  607. outStr << ")";
  608. break;
  609. case DEREFERENCE:
  610. outStr << "*";
  611. if (expr->expr.unaryExp->type == IDENTIFIER)
  612. needBracket = FALSE;
  613. else
  614. outStr << "(";
  615. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  616. if (needBracket == TRUE)
  617. outStr << ")";
  618. break;
  619. case POST_INC:
  620. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc) << "++";
  621. break;
  622. case POST_DEC:
  623. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc) << "--";
  624. break;
  625. case PRE_INC:
  626. outStr << "++"<< walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  627. break;
  628. case PRE_DEC:
  629. outStr << "--"<< walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  630. break;
  631. case IDENTIFIER:
  632. std::ostringstream o;
  633. switch (expr->expr.ident.idType)
  634. {
  635. case GLOB_VAR:
  636. o << symtab.sym[expr->expr.ident.idNode.globIdx].name;
  637. break;
  638. case REGISTER:
  639. id = &pProc->localId.id_arr[expr->expr.ident.idNode.regiIdx];
  640. if (id->name[0] == '\0') /* no name */
  641. {
  642. sprintf (id->name, "loc%ld", ++(*numLoc));
  643. if (id->id.regi < rAL)
  644. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,wordReg[id->id.regi - rAX]);
  645. else
  646. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,byteReg[id->id.regi - rAL]);
  647. }
  648. if (id->hasMacro)
  649. o << id->macro << "("<<id->name<<")";
  650. else
  651. o << id->name;
  652. break;
  653. case LOCAL_VAR:
  654. o << pProc->localId.id_arr[expr->expr.ident.idNode.localIdx].name;
  655. break;
  656. case PARAM:
  657. psym = &pProc->args.sym[expr->expr.ident.idNode.paramIdx];
  658. if (psym->hasMacro)
  659. o << psym->macro<<"("<<psym->name<< ")";
  660. else
  661. o << psym->name;
  662. break;
  663. case GLOB_VAR_IDX:
  664. bwGlb = &pProc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].id.bwGlb;
  665. o << (bwGlb->seg << 4) + bwGlb->off << "["<<wordReg[bwGlb->regi - rAX]<<"]";
  666. break;
  667. case CONSTANT:
  668. if (expr->expr.ident.idNode.kte.kte < 1000)
  669. o << expr->expr.ident.idNode.kte.kte;
  670. else
  671. o << "0x"<<std::hex << expr->expr.ident.idNode.kte.kte;
  672. break;
  673. case STRING:
  674. o << getString (expr->expr.ident.idNode.strIdx);
  675. break;
  676. case LONG_VAR:
  677. id = &pProc->localId.id_arr[expr->expr.ident.idNode.longIdx];
  678. if (id->name[0] != '\0') /* STK_FRAME & REG w/name*/
  679. o << id->name;
  680. else if (id->loc == REG_FRAME)
  681. {
  682. sprintf (id->name, "loc%ld", ++(*numLoc));
  683. cCode.appendDecl("%s %s; /* %s:%s */\n",hlTypes[id->type], id->name,wordReg[id->id.longId.h - rAX],wordReg[id->id.longId.l - rAX]);
  684. o << id->name;
  685. pProc->localId.propLongId (id->id.longId.l,id->id.longId.h, id->name);
  686. }
  687. else /* GLB_FRAME */
  688. {
  689. if (id->id.longGlb.regi == 0) /* not indexed */
  690. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"]";
  691. else if (id->id.longGlb.regi == rBX)
  692. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"][bx]";
  693. }
  694. break;
  695. case FUNCTION:
  696. o << writeCall (expr->expr.ident.idNode.call.proc,expr->expr.ident.idNode.call.args, pProc, numLoc);
  697. break;
  698. case OTHER:
  699. off = expr->expr.ident.idNode.other.off;
  700. o << wordReg[expr->expr.ident.idNode.other.seg - rAX]<< "[";
  701. o << idxReg[expr->expr.ident.idNode.other.regi - INDEXBASE];
  702. if (off < 0)
  703. o << "-"<< hexStr (-off);
  704. else if (off>0)
  705. o << "+"<< hexStr (off);
  706. o << "]";
  707. } /* eos */
  708. outStr << o.str();
  709. break;
  710. }
  711. return outStr.str();
  712. }
  713. /* Makes a copy of the given expression. Allocates newExp storage for each
  714. * node. Returns the copy. */
  715. COND_EXPR *COND_EXPR::clone()
  716. {
  717. COND_EXPR* newExp=0; /* Expression node copy */
  718. switch (type)
  719. {
  720. case BOOLEAN_OP:
  721. newExp = new COND_EXPR(*this);
  722. newExp->expr.boolExpr.lhs = expr.boolExpr.lhs->clone();
  723. newExp->expr.boolExpr.rhs = expr.boolExpr.rhs->clone();
  724. break;
  725. case NEGATION:
  726. case ADDRESSOF:
  727. case DEREFERENCE:
  728. newExp = new COND_EXPR(*this);
  729. newExp->expr.unaryExp = expr.unaryExp->clone();
  730. break;
  731. case IDENTIFIER:
  732. newExp = new COND_EXPR(*this);
  733. }
  734. return (newExp);
  735. }
  736. /* Changes the boolean conditional operator at the root of this expression */
  737. void COND_EXPR::changeBoolOp (condOp newOp)
  738. {
  739. expr.boolExpr.op = newOp;
  740. }
  741. /* Inserts the expression exp into the tree at the location specified by the
  742. * register regi */
  743. boolT insertSubTreeReg (COND_EXPR *expr, COND_EXPR **tree, byte regi,LOCAL_ID *locsym)
  744. {
  745. byte treeReg;
  746. if (*tree == NULL)
  747. return FALSE;
  748. switch ((*tree)->type) {
  749. case IDENTIFIER:
  750. if ((*tree)->expr.ident.idType == REGISTER)
  751. {
  752. treeReg = locsym->id_arr[(*tree)->expr.ident.idNode.regiIdx].id.regi;
  753. if (treeReg == regi) /* word reg */
  754. {
  755. *tree = expr;
  756. return TRUE;
  757. }
  758. else if ((regi >= rAX) && (regi <= rBX)) /* word/byte reg */
  759. {
  760. if ((treeReg == (regi + rAL-1)) || (treeReg == (regi + rAH-1)))
  761. {
  762. *tree = expr;
  763. return TRUE;
  764. }
  765. }
  766. }
  767. return FALSE;
  768. case BOOLEAN_OP:
  769. if (insertSubTreeReg (expr, &(*tree)->expr.boolExpr.lhs, regi, locsym))
  770. return TRUE;
  771. if (insertSubTreeReg (expr, &(*tree)->expr.boolExpr.rhs, regi, locsym))
  772. return TRUE;
  773. return FALSE;
  774. case NEGATION:
  775. case ADDRESSOF:
  776. case DEREFERENCE:
  777. if (insertSubTreeReg(expr, &(*tree)->expr.unaryExp,regi, locsym))
  778. return TRUE;
  779. return FALSE;
  780. }
  781. return FALSE;
  782. }
  783. /* Inserts the expression exp into the tree at the location specified by the
  784. * long register index longIdx*/
  785. boolT insertSubTreeLongReg (COND_EXPR *exp, COND_EXPR **tree, Int longIdx)
  786. {
  787. switch ((*tree)->type) {
  788. case IDENTIFIER: if ((*tree)->expr.ident.idNode.longIdx == longIdx)
  789. {
  790. *tree = exp;
  791. return TRUE;
  792. }
  793. return FALSE;
  794. case BOOLEAN_OP: if (insertSubTreeLongReg (exp, &(*tree)->expr.boolExpr.lhs, longIdx))
  795. return TRUE;
  796. if (insertSubTreeLongReg (exp, &(*tree)->expr.boolExpr.rhs, longIdx))
  797. return TRUE;
  798. return FALSE;
  799. case NEGATION:
  800. case ADDRESSOF:
  801. case DEREFERENCE: if (insertSubTreeLongReg (exp, &(*tree)->expr.unaryExp, longIdx))
  802. return TRUE;
  803. return FALSE;
  804. }
  805. return FALSE;
  806. }
  807. /* Recursively deallocates the abstract syntax tree rooted at *exp */
  808. void COND_EXPR::release()
  809. {
  810. switch (type)
  811. {
  812. case BOOLEAN_OP:
  813. expr.boolExpr.lhs->release();
  814. expr.boolExpr.rhs->release();
  815. break;
  816. case NEGATION:
  817. case ADDRESSOF:
  818. case DEREFERENCE:
  819. expr.unaryExp->release();
  820. break;
  821. }
  822. delete (this);
  823. }
  824. /***************************************************************************
  825. * Expression stack functions
  826. **************************************************************************/
  827. /* Reinitalizes the expression stack (expStk) to NULL, by freeing all the
  828. * space allocated (if any). */
  829. void initExpStk()
  830. {
  831. expStk.clear();
  832. }
  833. /* Pushes the given expression onto the local stack (expStk). */
  834. void pushExpStk (COND_EXPR *expr)
  835. {
  836. expStk.push_back(expr);
  837. }
  838. /* Returns the element on the top of the local expression stack (expStk),
  839. * and deallocates the space allocated by this node.
  840. * If there are no elements on the stack, returns NULL. */
  841. COND_EXPR *popExpStk()
  842. {
  843. if(expStk.empty())
  844. return 0;
  845. COND_EXPR *topExp = expStk.back();
  846. expStk.pop_back();
  847. return topExp;
  848. }
  849. /* Returns the number of elements available in the expression stack */
  850. Int numElemExpStk()
  851. {
  852. return expStk.size();
  853. }
  854. /* Returns whether the expression stack is empty or not */
  855. boolT emptyExpStk()
  856. {
  857. return expStk.empty();
  858. }