ast.cpp 27 KB

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