ast.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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. {
  121. printf ("Error, glob var not found in symtab\n");
  122. return 0;
  123. }
  124. newExp->expr.ident.idNode.globIdx = i;
  125. return (newExp);
  126. }
  127. /* Returns an identifier conditional expression node of type REGISTER */
  128. COND_EXPR *COND_EXPR::idReg(uint8_t regi, uint32_t icodeFlg, LOCAL_ID *locsym)
  129. {
  130. COND_EXPR *newExp;
  131. newExp = new COND_EXPR(IDENTIFIER);
  132. newExp->expr.ident.idType = REGISTER;
  133. if ((icodeFlg & B) || (icodeFlg & SRC_B))
  134. {
  135. newExp->expr.ident.idNode.regiIdx = locsym->newByteWordReg(TYPE_BYTE_SIGN, regi);
  136. newExp->expr.ident.regiType = BYTE_REG;
  137. }
  138. else /* uint16_t */
  139. {
  140. newExp->expr.ident.idNode.regiIdx = locsym->newByteWordReg( TYPE_WORD_SIGN, regi);
  141. newExp->expr.ident.regiType = WORD_REG;
  142. }
  143. return (newExp);
  144. }
  145. /* Returns an identifier conditional expression node of type REGISTER */
  146. COND_EXPR *COND_EXPR::idRegIdx(int idx, regType reg_type)
  147. {
  148. COND_EXPR *newExp;
  149. newExp = new COND_EXPR(IDENTIFIER);
  150. newExp->expr.ident.idType = REGISTER;
  151. newExp->expr.ident.regiType = reg_type;
  152. newExp->expr.ident.idNode.regiIdx = idx;
  153. return (newExp);
  154. }
  155. /* Returns an identifier conditional expression node of type LOCAL_VAR */
  156. COND_EXPR *COND_EXPR::idLoc(int off, LOCAL_ID *localId)
  157. {
  158. COND_EXPR *newExp;
  159. size_t i;
  160. newExp = new COND_EXPR(IDENTIFIER);
  161. newExp->expr.ident.idType = LOCAL_VAR;
  162. for (i = 0; i < localId->csym(); i++)
  163. if ((localId->id_arr[i].id.bwId.off == off) &&
  164. (localId->id_arr[i].id.bwId.regOff == 0))
  165. break;
  166. if (i == localId->csym())
  167. printf ("Error, cannot find local var\n");
  168. newExp->expr.ident.idNode.localIdx = i;
  169. sprintf (localId->id_arr[i].name, "loc%ld", i);
  170. return (newExp);
  171. }
  172. /* Returns an identifier conditional expression node of type PARAM */
  173. COND_EXPR *COND_EXPR::idParam(int off, const STKFRAME * argSymtab)
  174. {
  175. COND_EXPR *newExp;
  176. size_t i;
  177. newExp = new COND_EXPR(IDENTIFIER);
  178. newExp->expr.ident.idType = PARAM;
  179. for (i = 0; i < argSymtab->sym.size(); i++)
  180. if (argSymtab->sym[i].off == off)
  181. break;
  182. if (i == argSymtab->sym.size()) printf ("Error, cannot find argument var\n");
  183. newExp->expr.ident.idNode.localIdx = i;
  184. return (newExp);
  185. }
  186. /* Returns an identifier conditional expression node of type GLOB_VAR_IDX.
  187. * This global variable is indexed by regi. */
  188. COND_EXPR *idCondExpIdxGlob (int16_t segValue, int16_t off, uint8_t regi, const LOCAL_ID *locSym)
  189. {
  190. COND_EXPR *newExp;
  191. size_t i;
  192. newExp = new COND_EXPR(IDENTIFIER);
  193. newExp->expr.ident.idType = GLOB_VAR_IDX;
  194. for (i = 0; i < locSym->csym(); i++)
  195. if ((locSym->id_arr[i].id.bwGlb.seg == segValue) &&
  196. (locSym->id_arr[i].id.bwGlb.off == off) &&
  197. (locSym->id_arr[i].id.bwGlb.regi == regi))
  198. break;
  199. if (i == locSym->csym())
  200. printf ("Error, indexed-glob var not found in local id table\n");
  201. newExp->expr.ident.idNode.idxGlbIdx = i;
  202. return (newExp);
  203. }
  204. /* Returns an identifier conditional expression node of type CONSTANT */
  205. COND_EXPR *COND_EXPR::idKte(uint32_t kte, uint8_t size)
  206. {
  207. COND_EXPR *newExp = new COND_EXPR(IDENTIFIER);
  208. newExp->expr.ident.idType = CONSTANT;
  209. newExp->expr.ident.idNode.kte.kte = kte;
  210. newExp->expr.ident.idNode.kte.size = size;
  211. return (newExp);
  212. }
  213. /* Returns an identifier conditional expression node of type LONG_VAR,
  214. * that points to the given index idx. */
  215. COND_EXPR *COND_EXPR::idLongIdx (int idx)
  216. {
  217. COND_EXPR *newExp = new COND_EXPR(IDENTIFIER);
  218. newExp->expr.ident.idType = LONG_VAR;
  219. newExp->expr.ident.idNode.longIdx = idx;
  220. return (newExp);
  221. }
  222. /* Returns an identifier conditional expression node of type LONG_VAR */
  223. COND_EXPR *COND_EXPR::idLong(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, iICODE atOffset)
  224. {
  225. int idx;
  226. COND_EXPR *newExp = new COND_EXPR(IDENTIFIER);
  227. /* Check for long constant and save it as a constant expression */
  228. if ((sd == SRC) && pIcode->ll()->testFlags(I)) /* constant */
  229. {
  230. newExp->expr.ident.idType = CONSTANT;
  231. if (f == HIGH_FIRST)
  232. newExp->expr.ident.idNode.kte.kte = (pIcode->ll()->src.op() << 16) +
  233. atOffset->ll()->src.op();
  234. else /* LOW_FIRST */
  235. newExp->expr.ident.idNode.kte.kte =
  236. (atOffset->ll()->src.op() << 16)+ pIcode->ll()->src.op();
  237. newExp->expr.ident.idNode.kte.size = 4;
  238. }
  239. /* Save it as a long expression (reg, stack or glob) */
  240. else
  241. {
  242. idx = localId->newLong(sd, pIcode, f, ix, du, atOffset);
  243. newExp->expr.ident.idType = LONG_VAR;
  244. newExp->expr.ident.idNode.longIdx = idx;
  245. }
  246. return (newExp);
  247. }
  248. /* Returns an identifier conditional expression node of type FUNCTION */
  249. COND_EXPR *COND_EXPR::idFunc(Function * pproc, STKFRAME * args)
  250. {
  251. COND_EXPR *newExp;
  252. newExp = new COND_EXPR(IDENTIFIER);
  253. newExp->expr.ident.idType = FUNCTION;
  254. newExp->expr.ident.idNode.call.proc = pproc;
  255. newExp->expr.ident.idNode.call.args = args;
  256. return (newExp);
  257. }
  258. /* Returns an identifier conditional expression node of type OTHER.
  259. * Temporary solution, should really be encoded as an indexed type (eg.
  260. * arrays). */
  261. COND_EXPR *COND_EXPR::idOther(uint8_t seg, uint8_t regi, int16_t off)
  262. {
  263. COND_EXPR *newExp;
  264. newExp = new COND_EXPR(IDENTIFIER);
  265. newExp->expr.ident.idType = OTHER;
  266. newExp->expr.ident.idNode.other.seg = seg;
  267. newExp->expr.ident.idNode.other.regi = regi;
  268. newExp->expr.ident.idNode.other.off = off;
  269. return (newExp);
  270. }
  271. /* Returns an identifier conditional expression node of type TYPE_LONG or
  272. * TYPE_WORD_SIGN */
  273. COND_EXPR *COND_EXPR::idID (const ID *retVal, LOCAL_ID *locsym, iICODE ix_)
  274. {
  275. COND_EXPR *newExp;
  276. int idx;
  277. newExp = new COND_EXPR(IDENTIFIER);
  278. if (retVal->type == TYPE_LONG_SIGN)
  279. {
  280. idx = locsym->newLongReg (TYPE_LONG_SIGN, retVal->id.longId.h,retVal->id.longId.l, ix_);
  281. newExp->expr.ident.idType = LONG_VAR;
  282. newExp->expr.ident.idNode.longIdx = idx;
  283. }
  284. else if (retVal->type == TYPE_WORD_SIGN)
  285. {
  286. newExp->expr.ident.idType = REGISTER;
  287. newExp->expr.ident.idNode.regiIdx = locsym->newByteWordReg(TYPE_WORD_SIGN, retVal->id.regi);
  288. newExp->expr.ident.regiType = WORD_REG;
  289. }
  290. return (newExp);
  291. }
  292. /* Returns an identifier conditional expression node, according to the given
  293. * type.
  294. * Arguments:
  295. * duIcode: icode instruction that needs the du set.
  296. * du: operand is defined or used in current instruction. */
  297. COND_EXPR *COND_EXPR::id(const ICODE &pIcode, opLoc sd, Function * pProc, iICODE ix_,ICODE &duIcode, operDu du)
  298. {
  299. COND_EXPR *newExp;
  300. int idx; /* idx into pIcode->localId table */
  301. const LLOperand &pm((sd == SRC) ? pIcode.ll()->src : pIcode.ll()->dst);
  302. if ( ((sd == DST) && pIcode.ll()->testFlags(IM_DST)) or
  303. ((sd == SRC) && pIcode.ll()->testFlags(IM_SRC)) or
  304. (sd == LHS_OP)) /* for MUL lhs */
  305. { /* implicit dx:ax */
  306. idx = pProc->localId.newLongReg (TYPE_LONG_SIGN, rDX, rAX, ix_);
  307. newExp = COND_EXPR::idLongIdx (idx);
  308. duIcode.setRegDU (rDX, du);
  309. duIcode.setRegDU (rAX, du);
  310. }
  311. else if ((sd == DST) && pIcode.ll()->testFlags(IM_TMP_DST))
  312. { /* implicit tmp */
  313. newExp = COND_EXPR::idReg (rTMP, 0, &pProc->localId);
  314. duIcode.setRegDU(rTMP, (operDu)eUSE);
  315. }
  316. else if ((sd == SRC) && pIcode.ll()->testFlags(I)) /* constant */
  317. newExp = COND_EXPR::idKte (pIcode.ll()->src.op(), 2);
  318. else if (pm.regi == 0) /* global variable */
  319. newExp = COND_EXPR::idGlob(pm.segValue, pm.off);
  320. else if (pm.regi < INDEXBASE) /* register */
  321. {
  322. newExp = COND_EXPR::idReg (pm.regi, (sd == SRC) ? pIcode.ll()->getFlag() :
  323. pIcode.ll()->getFlag() & NO_SRC_B,
  324. &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) ? ll()->src : ll()->dst);
  379. if ((sd == SRC) && ll()->testFlags(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 HlTypeSupport::performLongRemoval (uint8_t regi, LOCAL_ID *locId, COND_EXPR *tree)
  510. {
  511. IDENTTYPE* ident; /* ptr to an identifier */
  512. uint8_t 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. string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc)
  547. {
  548. int16_t off; /* temporal - for OTHER */
  549. ID* id; /* Pointer to local identifier table */
  550. //char* o; /* Operand string pointer */
  551. bool needBracket; /* Determine whether parenthesis is needed */
  552. BWGLB_TYPE* bwGlb; /* Ptr to BWGLB_TYPE (global indexed var) */
  553. STKSYM * psym; /* Pointer to argument in the stack */
  554. std::ostringstream outStr;
  555. if (expr == NULL)
  556. return "";
  557. needBracket = true;
  558. switch (expr->type)
  559. {
  560. case BOOLEAN_OP:
  561. outStr << "(";
  562. outStr << walkCondExpr(expr->expr.boolExpr.lhs, pProc, numLoc);
  563. outStr << condOpSym[expr->expr.boolExpr.op];
  564. outStr << walkCondExpr(expr->expr.boolExpr.rhs, pProc, numLoc);
  565. outStr << ")";
  566. break;
  567. case NEGATION:
  568. if (expr->expr.unaryExp->type == IDENTIFIER)
  569. {
  570. needBracket = FALSE;
  571. outStr << "!";
  572. }
  573. else
  574. outStr << "! (";
  575. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  576. if (needBracket == TRUE)
  577. outStr << ")";
  578. break;
  579. case ADDRESSOF:
  580. if (expr->expr.unaryExp->type == IDENTIFIER)
  581. {
  582. needBracket = FALSE;
  583. outStr << "&";
  584. }
  585. else
  586. outStr << "&(";
  587. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  588. if (needBracket == TRUE)
  589. outStr << ")";
  590. break;
  591. case DEREFERENCE:
  592. outStr << "*";
  593. if (expr->expr.unaryExp->type == IDENTIFIER)
  594. needBracket = FALSE;
  595. else
  596. outStr << "(";
  597. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  598. if (needBracket == TRUE)
  599. outStr << ")";
  600. break;
  601. case POST_INC:
  602. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc) << "++";
  603. break;
  604. case POST_DEC:
  605. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc) << "--";
  606. break;
  607. case PRE_INC:
  608. outStr << "++"<< walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  609. break;
  610. case PRE_DEC:
  611. outStr << "--"<< walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  612. break;
  613. case IDENTIFIER:
  614. std::ostringstream o;
  615. switch (expr->expr.ident.idType)
  616. {
  617. case GLOB_VAR:
  618. o << symtab[expr->expr.ident.idNode.globIdx].name;
  619. break;
  620. case REGISTER:
  621. id = &pProc->localId.id_arr[expr->expr.ident.idNode.regiIdx];
  622. if (id->name[0] == '\0') /* no name */
  623. {
  624. sprintf (id->name, "loc%ld", ++(*numLoc));
  625. if (id->id.regi < rAL)
  626. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,wordReg[id->id.regi - rAX]);
  627. else
  628. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,byteReg[id->id.regi - rAL]);
  629. }
  630. if (id->hasMacro)
  631. o << id->macro << "("<<id->name<<")";
  632. else
  633. o << id->name;
  634. break;
  635. case LOCAL_VAR:
  636. o << pProc->localId.id_arr[expr->expr.ident.idNode.localIdx].name;
  637. break;
  638. case PARAM:
  639. psym = &pProc->args.sym[expr->expr.ident.idNode.paramIdx];
  640. if (psym->hasMacro)
  641. o << psym->macro<<"("<<psym->name<< ")";
  642. else
  643. o << psym->name;
  644. break;
  645. case GLOB_VAR_IDX:
  646. bwGlb = &pProc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].id.bwGlb;
  647. o << (bwGlb->seg << 4) + bwGlb->off << "["<<wordReg[bwGlb->regi - rAX]<<"]";
  648. break;
  649. case CONSTANT:
  650. if (expr->expr.ident.idNode.kte.kte < 1000)
  651. o << expr->expr.ident.idNode.kte.kte;
  652. else
  653. o << "0x"<<std::hex << expr->expr.ident.idNode.kte.kte;
  654. break;
  655. case STRING:
  656. o << getString (expr->expr.ident.idNode.strIdx);
  657. break;
  658. case LONG_VAR:
  659. id = &pProc->localId.id_arr[expr->expr.ident.idNode.longIdx];
  660. if (id->name[0] != '\0') /* STK_FRAME & REG w/name*/
  661. o << id->name;
  662. else if (id->loc == REG_FRAME)
  663. {
  664. sprintf (id->name, "loc%ld", ++(*numLoc));
  665. cCode.appendDecl("%s %s; /* %s:%s */\n",hlTypes[id->type], id->name,wordReg[id->id.longId.h - rAX],wordReg[id->id.longId.l - rAX]);
  666. o << id->name;
  667. pProc->localId.propLongId (id->id.longId.l,id->id.longId.h, id->name);
  668. }
  669. else /* GLB_FRAME */
  670. {
  671. if (id->id.longGlb.regi == 0) /* not indexed */
  672. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"]";
  673. else if (id->id.longGlb.regi == rBX)
  674. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"][bx]";
  675. }
  676. break;
  677. case FUNCTION:
  678. o << writeCall (expr->expr.ident.idNode.call.proc,expr->expr.ident.idNode.call.args, pProc, numLoc);
  679. break;
  680. case OTHER:
  681. off = expr->expr.ident.idNode.other.off;
  682. o << wordReg[expr->expr.ident.idNode.other.seg - rAX]<< "[";
  683. o << idxReg[expr->expr.ident.idNode.other.regi - INDEXBASE];
  684. if (off < 0)
  685. o << "-"<< hexStr (-off);
  686. else if (off>0)
  687. o << "+"<< hexStr (off);
  688. o << "]";
  689. } /* eos */
  690. outStr << o.str();
  691. break;
  692. }
  693. return outStr.str();
  694. }
  695. /* Makes a copy of the given expression. Allocates newExp storage for each
  696. * node. Returns the copy. */
  697. COND_EXPR *COND_EXPR::clone()
  698. {
  699. COND_EXPR* newExp=0; /* Expression node copy */
  700. switch (type)
  701. {
  702. case BOOLEAN_OP:
  703. newExp = new COND_EXPR(*this);
  704. newExp->expr.boolExpr.lhs = expr.boolExpr.lhs->clone();
  705. newExp->expr.boolExpr.rhs = expr.boolExpr.rhs->clone();
  706. break;
  707. case NEGATION:
  708. case ADDRESSOF:
  709. case DEREFERENCE:
  710. newExp = new COND_EXPR(*this);
  711. newExp->expr.unaryExp = expr.unaryExp->clone();
  712. break;
  713. case IDENTIFIER:
  714. return new COND_EXPR(*this);
  715. }
  716. return (newExp);
  717. }
  718. /* Changes the boolean conditional operator at the root of this expression */
  719. void COND_EXPR::changeBoolOp (condOp newOp)
  720. {
  721. expr.boolExpr.op = newOp;
  722. }
  723. /* Inserts the expression exp into the tree at the location specified by the
  724. * register regi */
  725. bool insertSubTreeReg (COND_EXPR *&tree, COND_EXPR *expr, uint8_t regi,LOCAL_ID *locsym)
  726. {
  727. HlTypeSupport *set_val;
  728. uint8_t 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) /* uint16_t reg */
  737. {
  738. tree = expr;
  739. return true;
  740. }
  741. else if ((regi >= rAX) && (regi <= rBX)) /* uint16_t/uint8_t 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 (tree->expr.boolExpr.lhs, expr, regi, locsym))
  753. return true;
  754. if (insertSubTreeReg (tree->expr.boolExpr.rhs, expr, regi, locsym))
  755. return true;
  756. return false;
  757. case NEGATION:
  758. case ADDRESSOF:
  759. case DEREFERENCE:
  760. if (insertSubTreeReg(tree->expr.unaryExp, expr, 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. }