ast.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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()->getOpcode(),pIcode->ll()->getOpcode());
  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, iICODE atOffset)
  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()->testFlags(I)) /* constant */
  226. {
  227. newExp->expr.ident.idType = CONSTANT;
  228. if (f == HIGH_FIRST)
  229. newExp->expr.ident.idNode.kte.kte = (pIcode->ll()->src.op() << 16) +
  230. atOffset->ll()->src.op();
  231. else /* LOW_FIRST */
  232. newExp->expr.ident.idNode.kte.kte =
  233. (atOffset->ll()->src.op() << 16)+ pIcode->ll()->src.op();
  234. newExp->expr.ident.idNode.kte.size = 4;
  235. }
  236. /* Save it as a long expression (reg, stack or glob) */
  237. else
  238. {
  239. idx = localId->newLong(sd, pIcode, f, ix, du, atOffset);
  240. newExp->expr.ident.idType = LONG_VAR;
  241. newExp->expr.ident.idNode.longIdx = idx;
  242. }
  243. return (newExp);
  244. }
  245. /* Returns an identifier conditional expression node of type FUNCTION */
  246. COND_EXPR *COND_EXPR::idFunc(Function * pproc, STKFRAME * args)
  247. {
  248. COND_EXPR *newExp;
  249. newExp = new COND_EXPR(IDENTIFIER);
  250. newExp->expr.ident.idType = FUNCTION;
  251. newExp->expr.ident.idNode.call.proc = pproc;
  252. newExp->expr.ident.idNode.call.args = args;
  253. return (newExp);
  254. }
  255. /* Returns an identifier conditional expression node of type OTHER.
  256. * Temporary solution, should really be encoded as an indexed type (eg.
  257. * arrays). */
  258. COND_EXPR *COND_EXPR::idOther(uint8_t seg, uint8_t regi, int16_t off)
  259. {
  260. COND_EXPR *newExp;
  261. newExp = new COND_EXPR(IDENTIFIER);
  262. newExp->expr.ident.idType = OTHER;
  263. newExp->expr.ident.idNode.other.seg = seg;
  264. newExp->expr.ident.idNode.other.regi = regi;
  265. newExp->expr.ident.idNode.other.off = off;
  266. return (newExp);
  267. }
  268. /* Returns an identifier conditional expression node of type TYPE_LONG or
  269. * TYPE_WORD_SIGN */
  270. COND_EXPR *COND_EXPR::idID (const ID *retVal, LOCAL_ID *locsym, iICODE ix_)
  271. {
  272. COND_EXPR *newExp;
  273. int idx;
  274. newExp = new COND_EXPR(IDENTIFIER);
  275. if (retVal->type == TYPE_LONG_SIGN)
  276. {
  277. idx = locsym->newLongReg (TYPE_LONG_SIGN, retVal->id.longId.h,retVal->id.longId.l, ix_);
  278. newExp->expr.ident.idType = LONG_VAR;
  279. newExp->expr.ident.idNode.longIdx = idx;
  280. }
  281. else if (retVal->type == TYPE_WORD_SIGN)
  282. {
  283. newExp->expr.ident.idType = REGISTER;
  284. newExp->expr.ident.idNode.regiIdx = locsym->newByteWordReg(TYPE_WORD_SIGN, retVal->id.regi);
  285. newExp->expr.ident.regiType = WORD_REG;
  286. }
  287. return (newExp);
  288. }
  289. /* Returns an identifier conditional expression node, according to the given
  290. * type.
  291. * Arguments:
  292. * duIcode: icode instruction that needs the du set.
  293. * du: operand is defined or used in current instruction. */
  294. COND_EXPR *COND_EXPR::id(const ICODE &pIcode, opLoc sd, Function * pProc, iICODE ix_,ICODE &duIcode, operDu du)
  295. {
  296. COND_EXPR *newExp;
  297. int idx; /* idx into pIcode->localId table */
  298. const LLOperand &pm((sd == SRC) ? pIcode.ll()->src : pIcode.ll()->dst);
  299. if ( ((sd == DST) && pIcode.ll()->testFlags(IM_DST)) or
  300. ((sd == SRC) && pIcode.ll()->testFlags(IM_SRC)) or
  301. (sd == LHS_OP)) /* for MUL lhs */
  302. { /* implicit dx:ax */
  303. idx = pProc->localId.newLongReg (TYPE_LONG_SIGN, rDX, rAX, ix_);
  304. newExp = COND_EXPR::idLongIdx (idx);
  305. duIcode.setRegDU (rDX, du);
  306. duIcode.setRegDU (rAX, du);
  307. }
  308. else if ((sd == DST) && pIcode.ll()->testFlags(IM_TMP_DST))
  309. { /* implicit tmp */
  310. newExp = COND_EXPR::idReg (rTMP, 0, &pProc->localId);
  311. duIcode.setRegDU(rTMP, (operDu)eUSE);
  312. }
  313. else if ((sd == SRC) && pIcode.ll()->testFlags(I)) /* constant */
  314. newExp = COND_EXPR::idKte (pIcode.ll()->src.op(), 2);
  315. else if (pm.regi == 0) /* global variable */
  316. newExp = COND_EXPR::idGlob(pm.segValue, pm.off);
  317. else if (pm.regi < INDEXBASE) /* register */
  318. {
  319. newExp = COND_EXPR::idReg (pm.regi, (sd == SRC) ? pIcode.ll()->getFlag() :
  320. pIcode.ll()->getFlag() & NO_SRC_B,
  321. &pProc->localId);
  322. duIcode.setRegDU( pm.regi, du);
  323. }
  324. else if (pm.off) /* offset */
  325. {
  326. if ((pm.seg == rSS) && (pm.regi == INDEXBASE + 6)) /* idx on bp */
  327. {
  328. if (pm.off >= 0) /* argument */
  329. newExp = COND_EXPR::idParam (pm.off, &pProc->args);
  330. else /* local variable */
  331. newExp = COND_EXPR::idLoc (pm.off, &pProc->localId);
  332. }
  333. else if ((pm.seg == rDS) && (pm.regi == INDEXBASE + 7)) /* bx */
  334. {
  335. if (pm.off > 0) /* global variable */
  336. newExp = idCondExpIdxGlob (pm.segValue, pm.off, rBX,&pProc->localId);
  337. else
  338. newExp = COND_EXPR::idOther (pm.seg, pm.regi, pm.off);
  339. duIcode.setRegDU( rBX, eUSE);
  340. }
  341. else /* idx <> bp, bx */
  342. newExp = COND_EXPR::idOther (pm.seg, pm.regi, pm.off);
  343. /**** check long ops, indexed global var *****/
  344. }
  345. else /* (pm->regi >= INDEXBASE && pm->off = 0) => indexed && no off */
  346. {
  347. if ((pm.seg == rDS) && (pm.regi > INDEXBASE + 3)) /* dereference */
  348. {
  349. switch (pm.regi) {
  350. case INDEXBASE + 4: newExp = COND_EXPR::idReg(rSI, 0, &pProc->localId);
  351. duIcode.setRegDU( rSI, du);
  352. break;
  353. case INDEXBASE + 5: newExp = COND_EXPR::idReg(rDI, 0, &pProc->localId);
  354. duIcode.setRegDU( rDI, du);
  355. break;
  356. case INDEXBASE + 6: newExp = COND_EXPR::idReg(rBP, 0, &pProc->localId);
  357. break;
  358. case INDEXBASE + 7: newExp = COND_EXPR::idReg(rBX, 0, &pProc->localId);
  359. duIcode.setRegDU( rBX, du);
  360. break;
  361. default:
  362. newExp = 0;
  363. assert(false);
  364. }
  365. newExp = COND_EXPR::unary (DEREFERENCE, newExp);
  366. }
  367. else
  368. newExp = COND_EXPR::idOther (pm.seg, pm.regi, 0);
  369. }
  370. return (newExp);
  371. }
  372. /* Returns the identifier type */
  373. condId ICODE::idType(opLoc sd)
  374. {
  375. LLOperand &pm((sd == SRC) ? ll()->src : ll()->dst);
  376. if ((sd == SRC) && ll()->testFlags(I))
  377. return (CONSTANT);
  378. else if (pm.regi == 0)
  379. return (GLOB_VAR);
  380. else if (pm.regi < INDEXBASE)
  381. return (REGISTER);
  382. else if ((pm.seg == rSS) && (pm.regi == INDEXBASE))
  383. {
  384. if (pm.off >= 0)
  385. return (PARAM);
  386. else
  387. return (LOCAL_VAR);
  388. }
  389. else
  390. return (OTHER);
  391. }
  392. /* Size of hl types */
  393. int hlSize[] = {2, 1, 1, 2, 2, 4, 4, 4, 2, 2, 1, 4, 4};
  394. /* Returns the type of the expression */
  395. int hlTypeSize (const COND_EXPR *expr, Function * pproc)
  396. {
  397. int first, second;
  398. if (expr == NULL)
  399. return (2); /* for TYPE_UNKNOWN */
  400. switch (expr->type) {
  401. case BOOLEAN_OP:
  402. first = hlTypeSize (expr->expr.boolExpr.lhs, pproc);
  403. second = hlTypeSize (expr->expr.boolExpr.rhs, pproc);
  404. if (first > second)
  405. return (first);
  406. else
  407. return (second);
  408. case NEGATION: case ADDRESSOF:
  409. case POST_INC: case POST_DEC:
  410. case PRE_INC: case PRE_DEC:
  411. case DEREFERENCE: return (hlTypeSize (expr->expr.unaryExp, pproc));
  412. case IDENTIFIER:
  413. switch (expr->expr.ident.idType)
  414. {
  415. case GLOB_VAR:
  416. return (symtab[expr->expr.ident.idNode.globIdx].size);
  417. case REGISTER:
  418. if (expr->expr.ident.regiType == BYTE_REG)
  419. return (1);
  420. else
  421. return (2);
  422. case LOCAL_VAR:
  423. return (hlSize[pproc->localId.id_arr[expr->expr.ident.idNode.localIdx].type]);
  424. case PARAM:
  425. return (hlSize[pproc->args.sym[expr->expr.ident.idNode.paramIdx].type]);
  426. case GLOB_VAR_IDX:
  427. return (hlSize[pproc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].type]);
  428. case CONSTANT:
  429. return (expr->expr.ident.idNode.kte.size);
  430. case STRING:
  431. return (2);
  432. case LONG_VAR:
  433. return (4);
  434. case FUNCTION:
  435. return (hlSize[expr->expr.ident.idNode.call.proc->retVal.type]);
  436. case OTHER:
  437. return (2);
  438. } /* eos */
  439. break;
  440. }
  441. return 2; // CC: is this correct?
  442. }
  443. /* Returns the type of the expression */
  444. hlType expType (const COND_EXPR *expr, Function * pproc)
  445. {
  446. hlType first, second;
  447. if (expr == NULL)
  448. return (TYPE_UNKNOWN);
  449. switch (expr->type)
  450. {
  451. case BOOLEAN_OP:
  452. first = expType (expr->expr.boolExpr.lhs, pproc);
  453. second = expType (expr->expr.boolExpr.rhs, pproc);
  454. if (first != second)
  455. {
  456. if (hlTypeSize (expr->expr.boolExpr.lhs, pproc) >
  457. hlTypeSize (expr->expr.boolExpr.rhs, pproc))
  458. return (first);
  459. else
  460. return (second);
  461. }
  462. else
  463. return (first);
  464. case POST_INC: case POST_DEC:
  465. case PRE_INC: case PRE_DEC:
  466. case NEGATION:
  467. return (expType (expr->expr.unaryExp, pproc));
  468. case ADDRESSOF: return (TYPE_PTR); /***????****/
  469. case DEREFERENCE: return (TYPE_PTR);
  470. case IDENTIFIER:
  471. switch (expr->expr.ident.idType)
  472. {
  473. case GLOB_VAR:
  474. return (symtab[expr->expr.ident.idNode.globIdx].type);
  475. case REGISTER:
  476. if (expr->expr.ident.regiType == BYTE_REG)
  477. return (TYPE_BYTE_SIGN);
  478. else
  479. return (TYPE_WORD_SIGN);
  480. case LOCAL_VAR:
  481. return (pproc->localId.id_arr[expr->expr.ident.idNode.localIdx].type);
  482. case PARAM:
  483. return (pproc->args.sym[expr->expr.ident.idNode.paramIdx].type);
  484. case GLOB_VAR_IDX:
  485. return (pproc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].type);
  486. case CONSTANT:
  487. return (TYPE_CONST);
  488. case STRING:
  489. return (TYPE_STR);
  490. case LONG_VAR:
  491. return (pproc->localId.id_arr[expr->expr.ident.idNode.longIdx].type);
  492. case FUNCTION:
  493. return (expr->expr.ident.idNode.call.proc->retVal.type);
  494. case OTHER:
  495. return (TYPE_UNKNOWN);
  496. } /* eos */
  497. case UNKNOWN_OP:
  498. assert(false);
  499. return (TYPE_UNKNOWN);
  500. }
  501. return TYPE_UNKNOWN; // CC: Correct?
  502. }
  503. /* Removes the register from the tree. If the register was part of a long
  504. * register (eg. dx:ax), the node gets transformed into an integer register
  505. * node. */
  506. void HlTypeSupport::performLongRemoval (uint8_t regi, LOCAL_ID *locId, COND_EXPR *tree)
  507. {
  508. IDENTTYPE* ident; /* ptr to an identifier */
  509. uint8_t otherRegi; /* high or low part of long register */
  510. switch (tree->type) {
  511. case BOOLEAN_OP:
  512. break;
  513. case POST_INC: case POST_DEC:
  514. case PRE_INC: case PRE_DEC:
  515. case NEGATION: case ADDRESSOF:
  516. case DEREFERENCE:
  517. break;
  518. case IDENTIFIER:
  519. ident = &tree->expr.ident;
  520. if (ident->idType == LONG_VAR)
  521. {
  522. otherRegi = otherLongRegi (regi, ident->idNode.longIdx, locId);
  523. ident->idType = REGISTER;
  524. ident->regiType = WORD_REG;
  525. ident->idNode.regiIdx = locId->newByteWordReg(TYPE_WORD_SIGN,otherRegi);
  526. }
  527. break;
  528. }
  529. }
  530. /* Returns the string located in image, formatted in C format. */
  531. static std::string getString (int offset)
  532. {
  533. ostringstream o;
  534. int strLen, i;
  535. strLen = strSize (&prog.Image[offset], '\0');
  536. o << '"';
  537. for (i = 0; i < strLen; i++)
  538. o<<cChar(prog.Image[offset+i]);
  539. o << "\"\0";
  540. return (o.str());
  541. }
  542. /* Walks the conditional expression tree and returns the result on a string */
  543. string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc)
  544. {
  545. int16_t off; /* temporal - for OTHER */
  546. ID* id; /* Pointer to local identifier table */
  547. //char* o; /* Operand string pointer */
  548. bool needBracket; /* Determine whether parenthesis is needed */
  549. BWGLB_TYPE* bwGlb; /* Ptr to BWGLB_TYPE (global indexed var) */
  550. STKSYM * psym; /* Pointer to argument in the stack */
  551. std::ostringstream outStr;
  552. if (expr == NULL)
  553. return "";
  554. needBracket = true;
  555. switch (expr->type)
  556. {
  557. case BOOLEAN_OP:
  558. outStr << "(";
  559. outStr << walkCondExpr(expr->expr.boolExpr.lhs, pProc, numLoc);
  560. outStr << condOpSym[expr->expr.boolExpr.op];
  561. outStr << walkCondExpr(expr->expr.boolExpr.rhs, pProc, numLoc);
  562. outStr << ")";
  563. break;
  564. case NEGATION:
  565. if (expr->expr.unaryExp->type == IDENTIFIER)
  566. {
  567. needBracket = FALSE;
  568. outStr << "!";
  569. }
  570. else
  571. outStr << "! (";
  572. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  573. if (needBracket == TRUE)
  574. outStr << ")";
  575. break;
  576. case ADDRESSOF:
  577. if (expr->expr.unaryExp->type == IDENTIFIER)
  578. {
  579. needBracket = FALSE;
  580. outStr << "&";
  581. }
  582. else
  583. outStr << "&(";
  584. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  585. if (needBracket == TRUE)
  586. outStr << ")";
  587. break;
  588. case DEREFERENCE:
  589. outStr << "*";
  590. if (expr->expr.unaryExp->type == IDENTIFIER)
  591. needBracket = FALSE;
  592. else
  593. outStr << "(";
  594. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  595. if (needBracket == TRUE)
  596. outStr << ")";
  597. break;
  598. case POST_INC:
  599. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc) << "++";
  600. break;
  601. case POST_DEC:
  602. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc) << "--";
  603. break;
  604. case PRE_INC:
  605. outStr << "++"<< walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  606. break;
  607. case PRE_DEC:
  608. outStr << "--"<< walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  609. break;
  610. case IDENTIFIER:
  611. std::ostringstream o;
  612. switch (expr->expr.ident.idType)
  613. {
  614. case GLOB_VAR:
  615. o << symtab[expr->expr.ident.idNode.globIdx].name;
  616. break;
  617. case REGISTER:
  618. id = &pProc->localId.id_arr[expr->expr.ident.idNode.regiIdx];
  619. if (id->name[0] == '\0') /* no name */
  620. {
  621. sprintf (id->name, "loc%ld", ++(*numLoc));
  622. if (id->id.regi < rAL)
  623. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,wordReg[id->id.regi - rAX]);
  624. else
  625. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,byteReg[id->id.regi - rAL]);
  626. }
  627. if (id->hasMacro)
  628. o << id->macro << "("<<id->name<<")";
  629. else
  630. o << id->name;
  631. break;
  632. case LOCAL_VAR:
  633. o << pProc->localId.id_arr[expr->expr.ident.idNode.localIdx].name;
  634. break;
  635. case PARAM:
  636. psym = &pProc->args.sym[expr->expr.ident.idNode.paramIdx];
  637. if (psym->hasMacro)
  638. o << psym->macro<<"("<<psym->name<< ")";
  639. else
  640. o << psym->name;
  641. break;
  642. case GLOB_VAR_IDX:
  643. bwGlb = &pProc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].id.bwGlb;
  644. o << (bwGlb->seg << 4) + bwGlb->off << "["<<wordReg[bwGlb->regi - rAX]<<"]";
  645. break;
  646. case CONSTANT:
  647. if (expr->expr.ident.idNode.kte.kte < 1000)
  648. o << expr->expr.ident.idNode.kte.kte;
  649. else
  650. o << "0x"<<std::hex << expr->expr.ident.idNode.kte.kte;
  651. break;
  652. case STRING:
  653. o << getString (expr->expr.ident.idNode.strIdx);
  654. break;
  655. case LONG_VAR:
  656. id = &pProc->localId.id_arr[expr->expr.ident.idNode.longIdx];
  657. if (id->name[0] != '\0') /* STK_FRAME & REG w/name*/
  658. o << id->name;
  659. else if (id->loc == REG_FRAME)
  660. {
  661. sprintf (id->name, "loc%ld", ++(*numLoc));
  662. cCode.appendDecl("%s %s; /* %s:%s */\n",hlTypes[id->type], id->name,wordReg[id->id.longId.h - rAX],wordReg[id->id.longId.l - rAX]);
  663. o << id->name;
  664. pProc->localId.propLongId (id->id.longId.l,id->id.longId.h, id->name);
  665. }
  666. else /* GLB_FRAME */
  667. {
  668. if (id->id.longGlb.regi == 0) /* not indexed */
  669. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"]";
  670. else if (id->id.longGlb.regi == rBX)
  671. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"][bx]";
  672. }
  673. break;
  674. case FUNCTION:
  675. o << writeCall (expr->expr.ident.idNode.call.proc,expr->expr.ident.idNode.call.args, pProc, numLoc);
  676. break;
  677. case OTHER:
  678. off = expr->expr.ident.idNode.other.off;
  679. o << wordReg[expr->expr.ident.idNode.other.seg - rAX]<< "[";
  680. o << idxReg[expr->expr.ident.idNode.other.regi - INDEXBASE];
  681. if (off < 0)
  682. o << "-"<< hexStr (-off);
  683. else if (off>0)
  684. o << "+"<< hexStr (off);
  685. o << "]";
  686. } /* eos */
  687. outStr << o.str();
  688. break;
  689. }
  690. return outStr.str();
  691. }
  692. /* Makes a copy of the given expression. Allocates newExp storage for each
  693. * node. Returns the copy. */
  694. COND_EXPR *COND_EXPR::clone()
  695. {
  696. COND_EXPR* newExp=0; /* Expression node copy */
  697. switch (type)
  698. {
  699. case BOOLEAN_OP:
  700. newExp = new COND_EXPR(*this);
  701. newExp->expr.boolExpr.lhs = expr.boolExpr.lhs->clone();
  702. newExp->expr.boolExpr.rhs = expr.boolExpr.rhs->clone();
  703. break;
  704. case NEGATION:
  705. case ADDRESSOF:
  706. case DEREFERENCE:
  707. newExp = new COND_EXPR(*this);
  708. newExp->expr.unaryExp = expr.unaryExp->clone();
  709. break;
  710. case IDENTIFIER:
  711. return new COND_EXPR(*this);
  712. }
  713. return (newExp);
  714. }
  715. /* Changes the boolean conditional operator at the root of this expression */
  716. void COND_EXPR::changeBoolOp (condOp newOp)
  717. {
  718. expr.boolExpr.op = newOp;
  719. }
  720. /* Inserts the expression exp into the tree at the location specified by the
  721. * register regi */
  722. bool insertSubTreeReg (COND_EXPR *expr, COND_EXPR **tree, uint8_t regi,LOCAL_ID *locsym)
  723. {
  724. uint8_t treeReg;
  725. if (*tree == NULL)
  726. return FALSE;
  727. switch ((*tree)->type) {
  728. case IDENTIFIER:
  729. if ((*tree)->expr.ident.idType == REGISTER)
  730. {
  731. treeReg = locsym->id_arr[(*tree)->expr.ident.idNode.regiIdx].id.regi;
  732. if (treeReg == regi) /* uint16_t reg */
  733. {
  734. *tree = expr;
  735. return TRUE;
  736. }
  737. else if ((regi >= rAX) && (regi <= rBX)) /* uint16_t/uint8_t reg */
  738. {
  739. if ((treeReg == (regi + rAL-1)) || (treeReg == (regi + rAH-1)))
  740. {
  741. *tree = expr;
  742. return TRUE;
  743. }
  744. }
  745. }
  746. return FALSE;
  747. case BOOLEAN_OP:
  748. if (insertSubTreeReg (expr, &(*tree)->expr.boolExpr.lhs, regi, locsym))
  749. return TRUE;
  750. if (insertSubTreeReg (expr, &(*tree)->expr.boolExpr.rhs, regi, locsym))
  751. return TRUE;
  752. return FALSE;
  753. case NEGATION:
  754. case ADDRESSOF:
  755. case DEREFERENCE:
  756. if (insertSubTreeReg(expr, &(*tree)->expr.unaryExp,regi, locsym))
  757. return TRUE;
  758. return FALSE;
  759. }
  760. return FALSE;
  761. }
  762. /* Inserts the expression exp into the tree at the location specified by the
  763. * long register index longIdx*/
  764. bool insertSubTreeLongReg(COND_EXPR *exp, COND_EXPR **tree, int longIdx)
  765. {
  766. switch ((*tree)->type)
  767. {
  768. case IDENTIFIER:
  769. if ((*tree)->expr.ident.idNode.longIdx == longIdx)
  770. {
  771. *tree = exp;
  772. return true;
  773. }
  774. return false;
  775. case BOOLEAN_OP:
  776. if (insertSubTreeLongReg (exp, &(*tree)->expr.boolExpr.lhs, longIdx))
  777. return true;
  778. if (insertSubTreeLongReg (exp, &(*tree)->expr.boolExpr.rhs, longIdx))
  779. return true;
  780. return false;
  781. case NEGATION:
  782. case ADDRESSOF:
  783. case DEREFERENCE:
  784. if (insertSubTreeLongReg (exp, &(*tree)->expr.unaryExp, longIdx))
  785. return true;
  786. return false;
  787. }
  788. return false;
  789. }
  790. /* Recursively deallocates the abstract syntax tree rooted at *exp */
  791. void COND_EXPR::release()
  792. {
  793. switch (type)
  794. {
  795. case BOOLEAN_OP:
  796. expr.boolExpr.lhs->release();
  797. expr.boolExpr.rhs->release();
  798. break;
  799. case NEGATION:
  800. case ADDRESSOF:
  801. case DEREFERENCE:
  802. expr.unaryExp->release();
  803. break;
  804. }
  805. delete (this);
  806. }