ast.cpp 30 KB

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