ast.cpp 28 KB

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