ast.cpp 29 KB

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