ast.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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 * const 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 * const 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 const 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. }
  85. /* Creates a conditional boolean expression and returns it */
  86. COND_EXPR *COND_EXPR::boolOp(COND_EXPR *_lhs, COND_EXPR *_rhs, condOp _op)
  87. {
  88. COND_EXPR *newExp;
  89. newExp = new COND_EXPR(BOOLEAN_OP);
  90. newExp->boolExpr.op = _op;
  91. newExp->boolExpr.lhs = _lhs;
  92. newExp->boolExpr.rhs = _rhs;
  93. return (newExp);
  94. }
  95. /* Returns a unary conditional expression node. This procedure should
  96. * only be used with the following conditional node types: NEGATION,
  97. * ADDRESSOF, DEREFERENCE, POST_INC, POST_DEC, PRE_INC, PRE_DEC */
  98. COND_EXPR *COND_EXPR::unary(condNodeType t, COND_EXPR *sub_expr)
  99. {
  100. COND_EXPR *newExp;
  101. newExp = new COND_EXPR(t);
  102. newExp->expr.unaryExp = sub_expr;
  103. return (newExp);
  104. }
  105. /* Returns an identifier conditional expression node of type GLOB_VAR */
  106. COND_EXPR *GlobalVariable::Create(int16_t segValue, int16_t off)
  107. {
  108. COND_EXPR *newExp;
  109. uint32_t adr;
  110. size_t i;
  111. newExp = new COND_EXPR(IDENTIFIER);
  112. newExp->expr.ident.idType = GLOB_VAR;
  113. adr = opAdr(segValue, off);
  114. for (i = 0; i < symtab.size(); i++)
  115. if (symtab[i].label == adr)
  116. break;
  117. if (i == symtab.size())
  118. {
  119. printf ("Error, glob var not found in symtab\n");
  120. delete newExp;
  121. return 0;
  122. }
  123. newExp->expr.ident.idNode.globIdx = i;
  124. return (newExp);
  125. }
  126. /* Returns an identifier conditional expression node of type REGISTER */
  127. COND_EXPR *COND_EXPR::idReg(eReg regi, uint32_t icodeFlg, LOCAL_ID *locsym)
  128. {
  129. COND_EXPR *newExp;
  130. newExp = new COND_EXPR(IDENTIFIER);
  131. newExp->expr.ident.idType = REGISTER;
  132. if ((icodeFlg & B) || (icodeFlg & SRC_B))
  133. {
  134. newExp->expr.ident.idNode.regiIdx = locsym->newByteWordReg(TYPE_BYTE_SIGN, regi);
  135. newExp->expr.ident.regiType = BYTE_REG;
  136. }
  137. else /* uint16_t */
  138. {
  139. newExp->expr.ident.idNode.regiIdx = locsym->newByteWordReg( TYPE_WORD_SIGN, regi);
  140. newExp->expr.ident.regiType = WORD_REG;
  141. }
  142. return (newExp);
  143. }
  144. /* Returns an identifier conditional expression node of type REGISTER */
  145. COND_EXPR *COND_EXPR::idRegIdx(int idx, regType reg_type)
  146. {
  147. COND_EXPR *newExp;
  148. newExp = new COND_EXPR(IDENTIFIER);
  149. newExp->expr.ident.idType = REGISTER;
  150. newExp->expr.ident.regiType = reg_type;
  151. newExp->expr.ident.idNode.regiIdx = idx;
  152. return (newExp);
  153. }
  154. /* Returns an identifier conditional expression node of type LOCAL_VAR */
  155. COND_EXPR *COND_EXPR::idLoc(int off, LOCAL_ID *localId)
  156. {
  157. COND_EXPR *newExp;
  158. size_t i;
  159. newExp = new COND_EXPR(IDENTIFIER);
  160. newExp->expr.ident.idType = LOCAL_VAR;
  161. for (i = 0; i < localId->csym(); i++)
  162. if ((localId->id_arr[i].id.bwId.off == off) &&
  163. (localId->id_arr[i].id.bwId.regOff == 0))
  164. break;
  165. if (i == localId->csym())
  166. printf ("Error, cannot find local var\n");
  167. newExp->expr.ident.idNode.localIdx = i;
  168. sprintf (localId->id_arr[i].name, "loc%ld", i);
  169. return (newExp);
  170. }
  171. /* Returns an identifier conditional expression node of type PARAM */
  172. COND_EXPR *COND_EXPR::idParam(int off, const STKFRAME * argSymtab)
  173. {
  174. COND_EXPR *newExp;
  175. size_t i;
  176. newExp = new COND_EXPR(IDENTIFIER);
  177. newExp->expr.ident.idType = PARAM;
  178. for (i = 0; i < argSymtab->sym.size(); i++)
  179. if (argSymtab->sym[i].off == off)
  180. break;
  181. if (i == argSymtab->sym.size()) printf ("Error, cannot find argument var\n");
  182. newExp->expr.ident.idNode.localIdx = i;
  183. return (newExp);
  184. }
  185. /* Returns an identifier conditional expression node of type GLOB_VAR_IDX.
  186. * This global variable is indexed by regi. */
  187. COND_EXPR *idCondExpIdxGlob (int16_t segValue, int16_t off, uint8_t regi, const LOCAL_ID *locSym)
  188. {
  189. COND_EXPR *newExp;
  190. size_t i;
  191. newExp = new COND_EXPR(IDENTIFIER);
  192. newExp->expr.ident.idType = GLOB_VAR_IDX;
  193. for (i = 0; i < locSym->csym(); i++)
  194. if ((locSym->id_arr[i].id.bwGlb.seg == segValue) &&
  195. (locSym->id_arr[i].id.bwGlb.off == off) &&
  196. (locSym->id_arr[i].id.bwGlb.regi == regi))
  197. break;
  198. if (i == locSym->csym())
  199. printf ("Error, indexed-glob var not found in local id table\n");
  200. newExp->expr.ident.idNode.idxGlbIdx = i;
  201. return (newExp);
  202. }
  203. /* Returns an identifier conditional expression node of type CONSTANT */
  204. COND_EXPR *COND_EXPR::idKte(uint32_t kte, uint8_t size)
  205. {
  206. COND_EXPR *newExp = new COND_EXPR(IDENTIFIER);
  207. newExp->expr.ident.idType = CONSTANT;
  208. newExp->expr.ident.idNode.kte.kte = kte;
  209. newExp->expr.ident.idNode.kte.size = size;
  210. return (newExp);
  211. }
  212. /* Returns an identifier conditional expression node of type LONG_VAR,
  213. * that points to the given index idx. */
  214. COND_EXPR *COND_EXPR::idLongIdx (int idx)
  215. {
  216. COND_EXPR *newExp = new COND_EXPR(IDENTIFIER);
  217. newExp->expr.ident.idType = LONG_VAR;
  218. newExp->expr.ident.idNode.longIdx = idx;
  219. return (newExp);
  220. }
  221. /* Returns an identifier conditional expression node of type LONG_VAR */
  222. COND_EXPR *COND_EXPR::idLong(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, iICODE atOffset)
  223. {
  224. int idx;
  225. COND_EXPR *newExp = new COND_EXPR(IDENTIFIER);
  226. /* Check for long constant and save it as a constant expression */
  227. if ((sd == SRC) && pIcode->ll()->testFlags(I)) /* constant */
  228. {
  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, atOffset);
  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 LLInst &ll_insn, 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) ? ll_insn.src : ll_insn.dst);
  301. if ( ((sd == DST) && ll_insn.testFlags(IM_DST)) or
  302. ((sd == SRC) && ll_insn.testFlags(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) && ll_insn.testFlags(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) && ll_insn.testFlags(I)) /* constant */
  316. newExp = COND_EXPR::idKte (ll_insn.src.op(), 2);
  317. else if (pm.regi == 0) /* global variable */
  318. newExp = GlobalVariable::Create(pm.segValue, pm.off);
  319. else if (pm.regi < INDEXBASE) /* register */
  320. {
  321. newExp = COND_EXPR::idReg (pm.regi, (sd == SRC) ? ll_insn.getFlag() :
  322. ll_insn.getFlag() & 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()->testFlags(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->lhs(), pproc);
  405. second = hlTypeSize (expr->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->lhs(), pproc);
  455. second = expType (expr->rhs(), pproc);
  456. if (first != second)
  457. {
  458. if (hlTypeSize (expr->lhs(), pproc) > hlTypeSize (expr->rhs(), pproc))
  459. return (first);
  460. else
  461. return (second);
  462. }
  463. else
  464. return (first);
  465. case POST_INC: case POST_DEC:
  466. case PRE_INC: case PRE_DEC:
  467. case NEGATION:
  468. return (expType (expr->expr.unaryExp, pproc));
  469. case ADDRESSOF: return (TYPE_PTR); /***????****/
  470. case DEREFERENCE: return (TYPE_PTR);
  471. case IDENTIFIER:
  472. switch (expr->expr.ident.idType)
  473. {
  474. case GLOB_VAR:
  475. return (symtab[expr->expr.ident.idNode.globIdx].type);
  476. case REGISTER:
  477. if (expr->expr.ident.regiType == BYTE_REG)
  478. return (TYPE_BYTE_SIGN);
  479. else
  480. return (TYPE_WORD_SIGN);
  481. case LOCAL_VAR:
  482. return (pproc->localId.id_arr[expr->expr.ident.idNode.localIdx].type);
  483. case PARAM:
  484. return (pproc->args.sym[expr->expr.ident.idNode.paramIdx].type);
  485. case GLOB_VAR_IDX:
  486. return (pproc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].type);
  487. case CONSTANT:
  488. return (TYPE_CONST);
  489. case STRING:
  490. return (TYPE_STR);
  491. case LONG_VAR:
  492. return (pproc->localId.id_arr[expr->expr.ident.idNode.longIdx].type);
  493. case FUNCTION:
  494. return (expr->expr.ident.idNode.call.proc->retVal.type);
  495. case OTHER:
  496. return (TYPE_UNKNOWN);
  497. } /* eos */
  498. case UNKNOWN_OP:
  499. assert(false);
  500. return (TYPE_UNKNOWN);
  501. }
  502. return TYPE_UNKNOWN; // CC: Correct?
  503. }
  504. /* Removes the register from the tree. If the register was part of a long
  505. * register (eg. dx:ax), the node gets transformed into an integer register
  506. * node. */
  507. void HlTypeSupport::performLongRemoval (eReg regi, LOCAL_ID *locId, COND_EXPR *tree)
  508. {
  509. IDENTTYPE* ident; /* ptr to an identifier */
  510. eReg otherRegi; /* high or low part of long register */
  511. switch (tree->type) {
  512. case BOOLEAN_OP:
  513. break;
  514. case POST_INC: case POST_DEC:
  515. case PRE_INC: case PRE_DEC:
  516. case NEGATION: case ADDRESSOF:
  517. case DEREFERENCE:
  518. break;
  519. case IDENTIFIER:
  520. ident = &tree->expr.ident;
  521. if (ident->idType == LONG_VAR)
  522. {
  523. otherRegi = otherLongRegi (regi, ident->idNode.longIdx, locId);
  524. ident->idType = REGISTER;
  525. ident->regiType = WORD_REG;
  526. ident->idNode.regiIdx = locId->newByteWordReg(TYPE_WORD_SIGN,otherRegi);
  527. }
  528. break;
  529. }
  530. }
  531. /* Returns the string located in image, formatted in C format. */
  532. static std::string getString (int offset)
  533. {
  534. ostringstream o;
  535. int strLen, i;
  536. strLen = strSize (&prog.Image[offset], '\0');
  537. o << '"';
  538. for (i = 0; i < strLen; i++)
  539. o<<cChar(prog.Image[offset+i]);
  540. o << "\"\0";
  541. return (o.str());
  542. }
  543. /* Walks the conditional expression tree and returns the result on a string */
  544. string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc)
  545. {
  546. int16_t off; /* temporal - for OTHER */
  547. ID* id; /* Pointer to local identifier table */
  548. //char* o; /* Operand string pointer */
  549. bool needBracket; /* Determine whether parenthesis is needed */
  550. BWGLB_TYPE* bwGlb; /* Ptr to BWGLB_TYPE (global indexed var) */
  551. STKSYM * psym; /* Pointer to argument in the stack */
  552. std::ostringstream outStr;
  553. if (expr == NULL)
  554. return "";
  555. needBracket = true;
  556. switch (expr->type)
  557. {
  558. case BOOLEAN_OP:
  559. outStr << "(";
  560. outStr << walkCondExpr(expr->lhs(), pProc, numLoc);
  561. outStr << condOpSym[expr->op()];
  562. outStr << walkCondExpr(expr->rhs(), pProc, numLoc);
  563. outStr << ")";
  564. break;
  565. case NEGATION:
  566. if (expr->expr.unaryExp->type == IDENTIFIER)
  567. {
  568. needBracket = FALSE;
  569. outStr << "!";
  570. }
  571. else
  572. outStr << "! (";
  573. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  574. if (needBracket == TRUE)
  575. outStr << ")";
  576. break;
  577. case ADDRESSOF:
  578. if (expr->expr.unaryExp->type == IDENTIFIER)
  579. {
  580. needBracket = FALSE;
  581. outStr << "&";
  582. }
  583. else
  584. outStr << "&(";
  585. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  586. if (needBracket == TRUE)
  587. outStr << ")";
  588. break;
  589. case DEREFERENCE:
  590. outStr << "*";
  591. if (expr->expr.unaryExp->type == IDENTIFIER)
  592. needBracket = FALSE;
  593. else
  594. outStr << "(";
  595. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  596. if (needBracket == TRUE)
  597. outStr << ")";
  598. break;
  599. case POST_INC:
  600. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc) << "++";
  601. break;
  602. case POST_DEC:
  603. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc) << "--";
  604. break;
  605. case PRE_INC:
  606. outStr << "++"<< walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  607. break;
  608. case PRE_DEC:
  609. outStr << "--"<< walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  610. break;
  611. case IDENTIFIER:
  612. std::ostringstream o;
  613. switch (expr->expr.ident.idType)
  614. {
  615. case GLOB_VAR:
  616. o << symtab[expr->expr.ident.idNode.globIdx].name;
  617. break;
  618. case REGISTER:
  619. id = &pProc->localId.id_arr[expr->expr.ident.idNode.regiIdx];
  620. if (id->name[0] == '\0') /* no name */
  621. {
  622. sprintf (id->name, "loc%ld", ++(*numLoc));
  623. if (id->id.regi < rAL)
  624. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,wordReg[id->id.regi - rAX]);
  625. else
  626. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,byteReg[id->id.regi - rAL]);
  627. }
  628. if (id->hasMacro)
  629. o << id->macro << "("<<id->name<<")";
  630. else
  631. o << id->name;
  632. break;
  633. case LOCAL_VAR:
  634. o << pProc->localId.id_arr[expr->expr.ident.idNode.localIdx].name;
  635. break;
  636. case PARAM:
  637. psym = &pProc->args.sym[expr->expr.ident.idNode.paramIdx];
  638. if (psym->hasMacro)
  639. o << psym->macro<<"("<<psym->name<< ")";
  640. else
  641. o << psym->name;
  642. break;
  643. case GLOB_VAR_IDX:
  644. bwGlb = &pProc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].id.bwGlb;
  645. o << (bwGlb->seg << 4) + bwGlb->off << "["<<wordReg[bwGlb->regi - rAX]<<"]";
  646. break;
  647. case CONSTANT:
  648. if (expr->expr.ident.idNode.kte.kte < 1000)
  649. o << expr->expr.ident.idNode.kte.kte;
  650. else
  651. o << "0x"<<std::hex << expr->expr.ident.idNode.kte.kte;
  652. break;
  653. case STRING:
  654. o << getString (expr->expr.ident.idNode.strIdx);
  655. break;
  656. case LONG_VAR:
  657. id = &pProc->localId.id_arr[expr->expr.ident.idNode.longIdx];
  658. if (id->name[0] != '\0') /* STK_FRAME & REG w/name*/
  659. o << id->name;
  660. else if (id->loc == REG_FRAME)
  661. {
  662. sprintf (id->name, "loc%ld", ++(*numLoc));
  663. cCode.appendDecl("%s %s; /* %s:%s */\n",hlTypes[id->type], id->name,wordReg[id->id.longId.h - rAX],wordReg[id->id.longId.l - rAX]);
  664. o << id->name;
  665. pProc->localId.propLongId (id->id.longId.l,id->id.longId.h, id->name);
  666. }
  667. else /* GLB_FRAME */
  668. {
  669. if (id->id.longGlb.regi == 0) /* not indexed */
  670. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"]";
  671. else if (id->id.longGlb.regi == rBX)
  672. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"][bx]";
  673. }
  674. break;
  675. case FUNCTION:
  676. o << writeCall (expr->expr.ident.idNode.call.proc,expr->expr.ident.idNode.call.args, pProc, numLoc);
  677. break;
  678. case OTHER:
  679. off = expr->expr.ident.idNode.other.off;
  680. o << wordReg[expr->expr.ident.idNode.other.seg - rAX]<< "[";
  681. o << idxReg[expr->expr.ident.idNode.other.regi - INDEXBASE];
  682. if (off < 0)
  683. o << "-"<< hexStr (-off);
  684. else if (off>0)
  685. o << "+"<< hexStr (off);
  686. o << "]";
  687. } /* eos */
  688. outStr << o.str();
  689. break;
  690. }
  691. return outStr.str();
  692. }
  693. /* Makes a copy of the given expression. Allocates newExp storage for each
  694. * node. Returns the copy. */
  695. //lint -sem(COND_EXPR::clone, @p!=0)
  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->boolExpr.lhs = lhs()->clone();
  704. newExp->boolExpr.rhs = 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. boolExpr.op = newOp;
  721. }
  722. /* Inserts the expression exp into the tree at the location specified by the
  723. * register regi */
  724. bool COND_EXPR::insertSubTreeReg (COND_EXPR *&tree, COND_EXPR *_expr, eReg regi,LOCAL_ID *locsym)
  725. {
  726. if (tree == NULL)
  727. return false;
  728. COND_EXPR *temp=tree->insertSubTreeReg(_expr,regi,locsym);
  729. if(nullptr!=temp)
  730. {
  731. tree=temp;
  732. return true;
  733. }
  734. return false;
  735. }
  736. bool isSubRegisterOf(eReg reg,eReg parent)
  737. {
  738. if ((parent < rAX) || (parent > rBX))
  739. return false; // only AX -> BX are coverede by subregisters
  740. return ((reg==subRegH(parent)) || (reg == subRegL(parent)));
  741. }
  742. COND_EXPR *COND_EXPR::insertSubTreeReg (COND_EXPR *_expr, eReg regi,LOCAL_ID *locsym)
  743. {
  744. //HlTypeSupport *set_val;
  745. eReg treeReg;
  746. COND_EXPR *temp;
  747. switch (type) {
  748. case IDENTIFIER:
  749. if (expr.ident.idType == REGISTER)
  750. {
  751. treeReg = locsym->id_arr[expr.ident.idNode.regiIdx].id.regi;
  752. if (treeReg == regi) /* uint16_t reg */
  753. {
  754. return _expr;
  755. }
  756. else if(isSubRegisterOf(treeReg,regi)) /* uint16_t/uint8_t reg */
  757. {
  758. return _expr;
  759. }
  760. }
  761. return FALSE;
  762. case BOOLEAN_OP:
  763. temp = lhs()->insertSubTreeReg( _expr, regi, locsym);
  764. if (nullptr!=temp)
  765. {
  766. boolExpr.lhs = temp;
  767. return this;
  768. }
  769. temp = rhs()->insertSubTreeReg( _expr, regi, locsym);
  770. if (nullptr!=temp)
  771. {
  772. boolExpr.rhs = temp;
  773. return this;
  774. }
  775. return nullptr;
  776. case NEGATION:
  777. case ADDRESSOF:
  778. case DEREFERENCE:
  779. temp = expr.unaryExp->insertSubTreeReg( _expr, regi, locsym);
  780. if (nullptr!=temp)
  781. {
  782. expr.unaryExp = temp;
  783. return this;
  784. }
  785. return nullptr;
  786. }
  787. return nullptr;
  788. }
  789. COND_EXPR *BinaryOperator::insertSubTreeReg(COND_EXPR *_expr, eReg regi, LOCAL_ID *locsym)
  790. {
  791. COND_EXPR *r;
  792. r=m_lhs->insertSubTreeReg(_expr,regi,locsym);
  793. if(r)
  794. {
  795. m_lhs = r;
  796. return this;
  797. }
  798. r=m_rhs->insertSubTreeReg(_expr,regi,locsym);
  799. if(r)
  800. {
  801. m_rhs = r;
  802. return this;
  803. }
  804. return nullptr;
  805. }
  806. /* Inserts the expression exp into the tree at the location specified by the
  807. * long register index longIdx*/
  808. bool COND_EXPR::insertSubTreeLongReg(COND_EXPR *_expr, COND_EXPR **tree, int longIdx)
  809. {
  810. if (tree == NULL)
  811. return false;
  812. COND_EXPR *temp=(*tree)->insertSubTreeLongReg(_expr,longIdx);
  813. if(nullptr!=temp)
  814. {
  815. *tree=temp;
  816. return true;
  817. }
  818. return false;
  819. }
  820. COND_EXPR *COND_EXPR::insertSubTreeLongReg(COND_EXPR *_expr, int longIdx)
  821. {
  822. COND_EXPR *temp;
  823. switch (type)
  824. {
  825. case IDENTIFIER:
  826. if (expr.ident.idNode.longIdx == longIdx)
  827. {
  828. return _expr;
  829. }
  830. return nullptr;
  831. case BOOLEAN_OP:
  832. temp = lhs()->insertSubTreeLongReg( _expr,longIdx);
  833. if (nullptr!=temp)
  834. {
  835. boolExpr.lhs = temp;
  836. return this;
  837. }
  838. temp = rhs()->insertSubTreeLongReg( _expr,longIdx);
  839. if (nullptr!=temp)
  840. {
  841. boolExpr.rhs = temp;
  842. return this;
  843. }
  844. return nullptr;
  845. case NEGATION:
  846. case ADDRESSOF:
  847. case DEREFERENCE:
  848. temp = expr.unaryExp->insertSubTreeLongReg(_expr,longIdx);
  849. if (nullptr!=temp)
  850. {
  851. expr.unaryExp = temp;
  852. return this;
  853. }
  854. return nullptr;
  855. }
  856. return nullptr;
  857. }
  858. COND_EXPR *BinaryOperator::insertSubTreeLongReg(COND_EXPR *_expr, int longIdx)
  859. {
  860. COND_EXPR *r;
  861. r=m_lhs->insertSubTreeLongReg(_expr,longIdx);
  862. if(r)
  863. {
  864. m_lhs = r;
  865. return this;
  866. }
  867. r=m_rhs->insertSubTreeLongReg(_expr,longIdx);
  868. if(r)
  869. {
  870. m_rhs = r;
  871. return this;
  872. }
  873. return nullptr;
  874. }
  875. /* Recursively deallocates the abstract syntax tree rooted at *exp */
  876. void COND_EXPR::release()
  877. {
  878. switch (type)
  879. {
  880. case BOOLEAN_OP:
  881. lhs()->release();
  882. rhs()->release();
  883. break;
  884. case NEGATION:
  885. case ADDRESSOF:
  886. case DEREFERENCE:
  887. expr.unaryExp->release();
  888. break;
  889. }
  890. delete (this);
  891. }
  892. /* Makes a copy of the given expression. Allocates newExp storage for each
  893. * node. Returns the copy. */
  894. //lint -sem(BinaryOperator::clone, @p!=0)
  895. COND_EXPR *BinaryOperator::clone()
  896. {
  897. BinaryOperator* newExp=new BinaryOperator(m_op); /* Expression node copy */
  898. newExp->m_lhs = m_lhs->clone();
  899. newExp->m_rhs = m_rhs->clone();
  900. return newExp;
  901. }
  902. COND_EXPR *BinaryOperator::inverse()
  903. {
  904. static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL,
  905. LESS_EQUAL, LESS, DUMMY,DUMMY,DUMMY,DUMMY,
  906. DUMMY, DUMMY, DUMMY, DUMMY, DUMMY, DUMMY,
  907. DUMMY, DBL_OR, DBL_AND};
  908. BinaryOperator *res=reinterpret_cast<BinaryOperator *>(this->clone());
  909. switch (m_op)
  910. {
  911. case LESS_EQUAL: case LESS: case EQUAL:
  912. case NOT_EQUAL: case GREATER: case GREATER_EQUAL:
  913. res->m_op = invCondOp[m_op];
  914. return res;
  915. case AND: case OR: case XOR: case NOT: case ADD:
  916. case SUB: case MUL: case DIV: case SHR: case SHL: case MOD:
  917. return COND_EXPR::unary (NEGATION, res);
  918. case DBL_AND: case DBL_OR:
  919. res->m_op = invCondOp[m_op];
  920. res->m_lhs=m_lhs->inverse ();
  921. res->m_rhs=m_rhs->inverse ();
  922. return res;
  923. } /* eos */
  924. assert(false);
  925. return res;
  926. }