ast.cpp 27 KB

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