ast.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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.src.op() << 16) +
  235. (pIcode+off)->ic.ll.src.op();
  236. else /* LOW_FIRST */
  237. newExp->expr.ident.idNode.kte.kte =
  238. ((pIcode+off)->ic.ll.src.op() << 16)+ pIcode->ic.ll.src.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 LLOpcode &pm((sd == SRC) ? pIcode.ic.ll.src : pIcode.ic.ll.dst);
  304. if ( ((sd == DST) && pIcode.ic.ll.anyFlagSet(IM_DST)) or
  305. ((sd == SRC) && pIcode.ic.ll.anyFlagSet(IM_SRC)) or
  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.anyFlagSet(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.anyFlagSet(I)) /* constant */
  319. newExp = COND_EXPR::idKte (pIcode.ic.ll.src.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. LLOpcode &pm((sd == SRC) ? ic.ll.src : ic.ll.dst);
  380. if ((sd == SRC) && ((ic.ll.flg & I) == I))
  381. return (CONSTANT);
  382. else if (pm.regi == 0)
  383. return (GLOB_VAR);
  384. else if (pm.regi < INDEXBASE)
  385. return (REGISTER);
  386. else if ((pm.seg == rSS) && (pm.regi == INDEXBASE))
  387. {
  388. if (pm.off >= 0)
  389. return (PARAM);
  390. else
  391. return (LOCAL_VAR);
  392. }
  393. else
  394. return (OTHER);
  395. }
  396. /* Size of hl types */
  397. Int hlSize[] = {2, 1, 1, 2, 2, 4, 4, 4, 2, 2, 1, 4, 4};
  398. /* Returns the type of the expression */
  399. Int hlTypeSize (const COND_EXPR *expr, Function * pproc)
  400. {
  401. Int first, second;
  402. if (expr == NULL)
  403. return (2); /* for TYPE_UNKNOWN */
  404. switch (expr->type) {
  405. case BOOLEAN_OP:
  406. first = hlTypeSize (expr->expr.boolExpr.lhs, pproc);
  407. second = hlTypeSize (expr->expr.boolExpr.rhs, pproc);
  408. if (first > second)
  409. return (first);
  410. else
  411. return (second);
  412. case NEGATION: case ADDRESSOF:
  413. case POST_INC: case POST_DEC:
  414. case PRE_INC: case PRE_DEC:
  415. case DEREFERENCE: return (hlTypeSize (expr->expr.unaryExp, pproc));
  416. case IDENTIFIER:
  417. switch (expr->expr.ident.idType)
  418. {
  419. case GLOB_VAR:
  420. return (symtab[expr->expr.ident.idNode.globIdx].size);
  421. case REGISTER:
  422. if (expr->expr.ident.regiType == BYTE_REG)
  423. return (1);
  424. else
  425. return (2);
  426. case LOCAL_VAR:
  427. return (hlSize[pproc->localId.id_arr[expr->expr.ident.idNode.localIdx].type]);
  428. case PARAM:
  429. return (hlSize[pproc->args.sym[expr->expr.ident.idNode.paramIdx].type]);
  430. case GLOB_VAR_IDX:
  431. return (hlSize[pproc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].type]);
  432. case CONSTANT:
  433. return (expr->expr.ident.idNode.kte.size);
  434. case STRING:
  435. return (2);
  436. case LONG_VAR:
  437. return (4);
  438. case FUNCTION:
  439. return (hlSize[expr->expr.ident.idNode.call.proc->retVal.type]);
  440. case OTHER:
  441. return (2);
  442. } /* eos */
  443. break;
  444. }
  445. return 2; // CC: is this correct?
  446. }
  447. /* Returns the type of the expression */
  448. hlType expType (const COND_EXPR *expr, Function * pproc)
  449. {
  450. hlType first, second;
  451. if (expr == NULL)
  452. return (TYPE_UNKNOWN);
  453. switch (expr->type)
  454. {
  455. case BOOLEAN_OP:
  456. first = expType (expr->expr.boolExpr.lhs, pproc);
  457. second = expType (expr->expr.boolExpr.rhs, pproc);
  458. if (first != second)
  459. {
  460. if (hlTypeSize (expr->expr.boolExpr.lhs, pproc) >
  461. hlTypeSize (expr->expr.boolExpr.rhs, pproc))
  462. return (first);
  463. else
  464. return (second);
  465. }
  466. else
  467. return (first);
  468. case POST_INC: case POST_DEC:
  469. case PRE_INC: case PRE_DEC:
  470. case NEGATION:
  471. return (expType (expr->expr.unaryExp, pproc));
  472. case ADDRESSOF: return (TYPE_PTR); /***????****/
  473. case DEREFERENCE: return (TYPE_PTR);
  474. case IDENTIFIER:
  475. switch (expr->expr.ident.idType)
  476. {
  477. case GLOB_VAR:
  478. return (symtab[expr->expr.ident.idNode.globIdx].type);
  479. case REGISTER:
  480. if (expr->expr.ident.regiType == BYTE_REG)
  481. return (TYPE_BYTE_SIGN);
  482. else
  483. return (TYPE_WORD_SIGN);
  484. case LOCAL_VAR:
  485. return (pproc->localId.id_arr[expr->expr.ident.idNode.localIdx].type);
  486. case PARAM:
  487. return (pproc->args.sym[expr->expr.ident.idNode.paramIdx].type);
  488. case GLOB_VAR_IDX:
  489. return (pproc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].type);
  490. case CONSTANT:
  491. return (TYPE_CONST);
  492. case STRING:
  493. return (TYPE_STR);
  494. case LONG_VAR:
  495. return (pproc->localId.id_arr[expr->expr.ident.idNode.longIdx].type);
  496. case FUNCTION:
  497. return (expr->expr.ident.idNode.call.proc->retVal.type);
  498. case OTHER:
  499. return (TYPE_UNKNOWN);
  500. } /* eos */
  501. case UNKNOWN_OP:
  502. assert(false);
  503. return (TYPE_UNKNOWN);
  504. }
  505. return TYPE_UNKNOWN; // CC: Correct?
  506. }
  507. /* Removes the register from the tree. If the register was part of a long
  508. * register (eg. dx:ax), the node gets transformed into an integer register
  509. * node. */
  510. void removeRegFromLong (byte regi, LOCAL_ID *locId, COND_EXPR *tree)
  511. {
  512. IDENTTYPE* ident; /* ptr to an identifier */
  513. byte otherRegi; /* high or low part of long register */
  514. switch (tree->type) {
  515. case BOOLEAN_OP:
  516. break;
  517. case POST_INC: case POST_DEC:
  518. case PRE_INC: case PRE_DEC:
  519. case NEGATION: case ADDRESSOF:
  520. case DEREFERENCE:
  521. break;
  522. case IDENTIFIER:
  523. ident = &tree->expr.ident;
  524. if (ident->idType == LONG_VAR)
  525. {
  526. otherRegi = otherLongRegi (regi, ident->idNode.longIdx, locId);
  527. ident->idType = REGISTER;
  528. ident->regiType = WORD_REG;
  529. ident->idNode.regiIdx = locId->newByteWordReg(TYPE_WORD_SIGN,otherRegi);
  530. }
  531. break;
  532. }
  533. }
  534. /* Returns the string located in image, formatted in C format. */
  535. static std::string getString (Int offset)
  536. {
  537. ostringstream o;
  538. Int strLen, i;
  539. strLen = strSize (&prog.Image[offset], '\0');
  540. o << '"';
  541. for (i = 0; i < strLen; i++)
  542. o<<cChar(prog.Image[offset+i]);
  543. o << "\"\0";
  544. return (o.str());
  545. }
  546. /* Walks the conditional expression tree and returns the result on a string */
  547. // TODO: use string stream here
  548. string walkCondExpr (const COND_EXPR* expr, Function * pProc, Int* numLoc)
  549. {
  550. int16 off; /* temporal - for OTHER */
  551. ID* id; /* Pointer to local identifier table */
  552. //char* o; /* Operand string pointer */
  553. bool needBracket; /* Determine whether parenthesis is needed */
  554. BWGLB_TYPE* bwGlb; /* Ptr to BWGLB_TYPE (global indexed var) */
  555. STKSYM * psym; /* Pointer to argument in the stack */
  556. std::ostringstream outStr;
  557. if (expr == NULL)
  558. return "";
  559. needBracket = true;
  560. switch (expr->type)
  561. {
  562. case BOOLEAN_OP:
  563. outStr << "(";
  564. outStr << walkCondExpr(expr->expr.boolExpr.lhs, pProc, numLoc);
  565. outStr << condOpSym[expr->expr.boolExpr.op];
  566. outStr << walkCondExpr(expr->expr.boolExpr.rhs, pProc, numLoc);
  567. outStr << ")";
  568. break;
  569. case NEGATION:
  570. if (expr->expr.unaryExp->type == IDENTIFIER)
  571. {
  572. needBracket = FALSE;
  573. outStr << "!";
  574. }
  575. else
  576. outStr << "! (";
  577. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  578. if (needBracket == TRUE)
  579. outStr << ")";
  580. break;
  581. case ADDRESSOF:
  582. if (expr->expr.unaryExp->type == IDENTIFIER)
  583. {
  584. needBracket = FALSE;
  585. outStr << "&";
  586. }
  587. else
  588. outStr << "&(";
  589. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  590. if (needBracket == TRUE)
  591. outStr << ")";
  592. break;
  593. case DEREFERENCE:
  594. outStr << "*";
  595. if (expr->expr.unaryExp->type == IDENTIFIER)
  596. needBracket = FALSE;
  597. else
  598. outStr << "(";
  599. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  600. if (needBracket == TRUE)
  601. outStr << ")";
  602. break;
  603. case POST_INC:
  604. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc) << "++";
  605. break;
  606. case POST_DEC:
  607. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc) << "--";
  608. break;
  609. case PRE_INC:
  610. outStr << "++"<< walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  611. break;
  612. case PRE_DEC:
  613. outStr << "--"<< walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  614. break;
  615. case IDENTIFIER:
  616. std::ostringstream o;
  617. switch (expr->expr.ident.idType)
  618. {
  619. case GLOB_VAR:
  620. o << symtab[expr->expr.ident.idNode.globIdx].name;
  621. break;
  622. case REGISTER:
  623. id = &pProc->localId.id_arr[expr->expr.ident.idNode.regiIdx];
  624. if (id->name[0] == '\0') /* no name */
  625. {
  626. sprintf (id->name, "loc%ld", ++(*numLoc));
  627. if (id->id.regi < rAL)
  628. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,wordReg[id->id.regi - rAX]);
  629. else
  630. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,byteReg[id->id.regi - rAL]);
  631. }
  632. if (id->hasMacro)
  633. o << id->macro << "("<<id->name<<")";
  634. else
  635. o << id->name;
  636. break;
  637. case LOCAL_VAR:
  638. o << pProc->localId.id_arr[expr->expr.ident.idNode.localIdx].name;
  639. break;
  640. case PARAM:
  641. psym = &pProc->args.sym[expr->expr.ident.idNode.paramIdx];
  642. if (psym->hasMacro)
  643. o << psym->macro<<"("<<psym->name<< ")";
  644. else
  645. o << psym->name;
  646. break;
  647. case GLOB_VAR_IDX:
  648. bwGlb = &pProc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].id.bwGlb;
  649. o << (bwGlb->seg << 4) + bwGlb->off << "["<<wordReg[bwGlb->regi - rAX]<<"]";
  650. break;
  651. case CONSTANT:
  652. if (expr->expr.ident.idNode.kte.kte < 1000)
  653. o << expr->expr.ident.idNode.kte.kte;
  654. else
  655. o << "0x"<<std::hex << expr->expr.ident.idNode.kte.kte;
  656. break;
  657. case STRING:
  658. o << getString (expr->expr.ident.idNode.strIdx);
  659. break;
  660. case LONG_VAR:
  661. id = &pProc->localId.id_arr[expr->expr.ident.idNode.longIdx];
  662. if (id->name[0] != '\0') /* STK_FRAME & REG w/name*/
  663. o << id->name;
  664. else if (id->loc == REG_FRAME)
  665. {
  666. sprintf (id->name, "loc%ld", ++(*numLoc));
  667. cCode.appendDecl("%s %s; /* %s:%s */\n",hlTypes[id->type], id->name,wordReg[id->id.longId.h - rAX],wordReg[id->id.longId.l - rAX]);
  668. o << id->name;
  669. pProc->localId.propLongId (id->id.longId.l,id->id.longId.h, id->name);
  670. }
  671. else /* GLB_FRAME */
  672. {
  673. if (id->id.longGlb.regi == 0) /* not indexed */
  674. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"]";
  675. else if (id->id.longGlb.regi == rBX)
  676. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"][bx]";
  677. }
  678. break;
  679. case FUNCTION:
  680. o << writeCall (expr->expr.ident.idNode.call.proc,expr->expr.ident.idNode.call.args, pProc, numLoc);
  681. break;
  682. case OTHER:
  683. off = expr->expr.ident.idNode.other.off;
  684. o << wordReg[expr->expr.ident.idNode.other.seg - rAX]<< "[";
  685. o << idxReg[expr->expr.ident.idNode.other.regi - INDEXBASE];
  686. if (off < 0)
  687. o << "-"<< hexStr (-off);
  688. else if (off>0)
  689. o << "+"<< hexStr (off);
  690. o << "]";
  691. } /* eos */
  692. outStr << o.str();
  693. break;
  694. }
  695. return outStr.str();
  696. }
  697. /* Makes a copy of the given expression. Allocates newExp storage for each
  698. * node. Returns the copy. */
  699. COND_EXPR *COND_EXPR::clone()
  700. {
  701. COND_EXPR* newExp=0; /* Expression node copy */
  702. switch (type)
  703. {
  704. case BOOLEAN_OP:
  705. newExp = new COND_EXPR(*this);
  706. newExp->expr.boolExpr.lhs = expr.boolExpr.lhs->clone();
  707. newExp->expr.boolExpr.rhs = expr.boolExpr.rhs->clone();
  708. break;
  709. case NEGATION:
  710. case ADDRESSOF:
  711. case DEREFERENCE:
  712. newExp = new COND_EXPR(*this);
  713. newExp->expr.unaryExp = expr.unaryExp->clone();
  714. break;
  715. case IDENTIFIER:
  716. return new COND_EXPR(*this);
  717. }
  718. return (newExp);
  719. }
  720. /* Changes the boolean conditional operator at the root of this expression */
  721. void COND_EXPR::changeBoolOp (condOp newOp)
  722. {
  723. expr.boolExpr.op = newOp;
  724. }
  725. /* Inserts the expression exp into the tree at the location specified by the
  726. * register regi */
  727. bool insertSubTreeReg (COND_EXPR *expr, COND_EXPR **tree, byte regi,LOCAL_ID *locsym)
  728. {
  729. byte treeReg;
  730. if (*tree == NULL)
  731. return FALSE;
  732. switch ((*tree)->type) {
  733. case IDENTIFIER:
  734. if ((*tree)->expr.ident.idType == REGISTER)
  735. {
  736. treeReg = locsym->id_arr[(*tree)->expr.ident.idNode.regiIdx].id.regi;
  737. if (treeReg == regi) /* word reg */
  738. {
  739. *tree = expr;
  740. return TRUE;
  741. }
  742. else if ((regi >= rAX) && (regi <= rBX)) /* word/byte reg */
  743. {
  744. if ((treeReg == (regi + rAL-1)) || (treeReg == (regi + rAH-1)))
  745. {
  746. *tree = expr;
  747. return TRUE;
  748. }
  749. }
  750. }
  751. return FALSE;
  752. case BOOLEAN_OP:
  753. if (insertSubTreeReg (expr, &(*tree)->expr.boolExpr.lhs, regi, locsym))
  754. return TRUE;
  755. if (insertSubTreeReg (expr, &(*tree)->expr.boolExpr.rhs, regi, locsym))
  756. return TRUE;
  757. return FALSE;
  758. case NEGATION:
  759. case ADDRESSOF:
  760. case DEREFERENCE:
  761. if (insertSubTreeReg(expr, &(*tree)->expr.unaryExp,regi, locsym))
  762. return TRUE;
  763. return FALSE;
  764. }
  765. return FALSE;
  766. }
  767. /* Inserts the expression exp into the tree at the location specified by the
  768. * long register index longIdx*/
  769. bool insertSubTreeLongReg(COND_EXPR *exp, COND_EXPR **tree, Int longIdx)
  770. {
  771. switch ((*tree)->type)
  772. {
  773. case IDENTIFIER:
  774. if ((*tree)->expr.ident.idNode.longIdx == longIdx)
  775. {
  776. *tree = exp;
  777. return true;
  778. }
  779. return false;
  780. case BOOLEAN_OP:
  781. if (insertSubTreeLongReg (exp, &(*tree)->expr.boolExpr.lhs, longIdx))
  782. return true;
  783. if (insertSubTreeLongReg (exp, &(*tree)->expr.boolExpr.rhs, longIdx))
  784. return true;
  785. return false;
  786. case NEGATION:
  787. case ADDRESSOF:
  788. case DEREFERENCE:
  789. if (insertSubTreeLongReg (exp, &(*tree)->expr.unaryExp, longIdx))
  790. return true;
  791. return false;
  792. }
  793. return false;
  794. }
  795. /* Recursively deallocates the abstract syntax tree rooted at *exp */
  796. void COND_EXPR::release()
  797. {
  798. switch (type)
  799. {
  800. case BOOLEAN_OP:
  801. expr.boolExpr.lhs->release();
  802. expr.boolExpr.rhs->release();
  803. break;
  804. case NEGATION:
  805. case ADDRESSOF:
  806. case DEREFERENCE:
  807. expr.unaryExp->release();
  808. break;
  809. }
  810. delete (this);
  811. }