ast.cpp 32 KB

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