ast.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /*
  2. * File: ast.c
  3. * Purpose: Support module for abstract syntax trees.
  4. * Date: September 1993
  5. * (C) Cristina Cifuentes
  6. */
  7. #include "ast.h"
  8. #include "msvc_fixes.h"
  9. #include "types.h"
  10. #include "bundle.h"
  11. #include "machine_x86.h"
  12. #include "project.h"
  13. #include <boost/range.hpp>
  14. #include <boost/range/adaptors.hpp>
  15. #include <boost/range/algorithm.hpp>
  16. #include <boost/assign.hpp>
  17. #include <stdint.h>
  18. #include <string>
  19. #include <sstream>
  20. #include <iostream>
  21. #include <cassert>
  22. using namespace std;
  23. using namespace boost;
  24. using namespace boost::adaptors;
  25. extern int strSize (const uint8_t *, char);
  26. extern char *cChar(uint8_t c);
  27. // Conditional operator symbols in C. Index by condOp enumeration type
  28. static const char * const condOpSym[] = { " <= ", " < ", " == ", " != ", " > ", " >= ",
  29. " & ", " | ", " ^ ", " ~ ",
  30. " + ", " - ", " * ", " / ",
  31. " >> ", " << ", " % ", " && ", " || " };
  32. /* Local expression stack */
  33. //typedef struct _EXP_STK {
  34. // COND_EXPR *exp;
  35. // struct _EXP_STK *next;
  36. //} EXP_STK; - for local expression stack
  37. /* Returns the integer i in C hexadecimal format */
  38. static const char *hexStr (uint16_t i)
  39. {
  40. static char buf[10];
  41. sprintf (buf, "%s%x", (i > 9) ? "0x" : "", i);
  42. return (buf);
  43. }
  44. /* Sets the du record for registers according to the du flag */
  45. void ICODE::setRegDU (eReg regi, operDu du_in)
  46. {
  47. // printf("%s %d %x\n",__FUNCTION__,regi,int(du_in));
  48. if(regi==rSP)
  49. {
  50. printf("Discarding SP def&use info for now\n");
  51. return;
  52. }
  53. switch (du_in)
  54. {
  55. case eDEF:
  56. du.def.addReg(regi);
  57. du1.addDef(regi);
  58. break;
  59. case eUSE:
  60. du.use.addReg(regi);
  61. break;
  62. case USE_DEF:
  63. du.addDefinedAndUsed(regi);
  64. du1.addDef(regi);
  65. break;
  66. case NONE: /* do nothing */
  67. break;
  68. }
  69. }
  70. /* Copies the def, use, or def and use fields of duIcode into pIcode */
  71. void ICODE::copyDU(const ICODE &duIcode, operDu _du, operDu duDu)
  72. {
  73. switch (_du)
  74. {
  75. case eDEF:
  76. if (duDu == eDEF)
  77. du.def=duIcode.du.def;
  78. else
  79. du.def=duIcode.du.use;
  80. break;
  81. case eUSE:
  82. if (duDu == eDEF)
  83. du.use = duIcode.du.def;
  84. else
  85. du.use = duIcode.du.use;
  86. break;
  87. case USE_DEF:
  88. du = duIcode.du;
  89. break;
  90. case NONE:
  91. assert(false);
  92. break;
  93. }
  94. }
  95. /* Creates a conditional boolean expression and returns it */
  96. //COND_EXPR *COND_EXPR::boolOp(COND_EXPR *_lhs, COND_EXPR *_rhs, condOp _op)
  97. //{
  98. // return BinaryOperator::Create(_op,_lhs,_rhs);
  99. //}
  100. /* Returns a unary conditional expression node. This procedure should
  101. * only be used with the following conditional node types: NEGATION,
  102. * ADDRESSOF, DEREFERENCE, POST_INC, POST_DEC, PRE_INC, PRE_DEC */
  103. /* Returns an identifier conditional expression node of type GLOB_VAR */
  104. GlobalVariable::GlobalVariable(int16_t segValue, int16_t off)
  105. {
  106. uint32_t adr;
  107. valid = true;
  108. ident.idType = GLOB_VAR;
  109. adr = opAdr(segValue, off);
  110. auto i=Project::get()->getSymIdxByAdd(adr);
  111. if ( not Project::get()->validSymIdx(i) )
  112. {
  113. printf ("Error, glob var not found in symtab\n");
  114. valid = false;
  115. }
  116. globIdx = i;
  117. }
  118. string GlobalVariable::walkCondExpr(Function *, int *) const
  119. {
  120. if(valid)
  121. return Project::get()->symtab[globIdx].name;
  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 = new AstIdent();
  129. newExp->ident.idType = LOCAL_VAR;
  130. for (i = 0; i < localId->csym(); i++)
  131. {
  132. const ID &lID(localId->id_arr[i]);
  133. if ((lID.id.bwId.off == off) and (lID.id.bwId.regOff == 0))
  134. break;
  135. }
  136. if (i == localId->csym())
  137. printf ("Error, cannot find local var\n");
  138. newExp->ident.idNode.localIdx = i;
  139. localId->id_arr[i].setLocalName(i);
  140. return (newExp);
  141. }
  142. /* Returns an identifier conditional expression node of type PARAM */
  143. AstIdent *AstIdent::Param(int off, const STKFRAME * argSymtab)
  144. {
  145. AstIdent *newExp;
  146. newExp = new AstIdent();
  147. newExp->ident.idType = PARAM;
  148. auto iter=argSymtab->findByLabel(off);
  149. if (iter == argSymtab->end())
  150. printf ("Error, cannot find argument var\n");
  151. newExp->ident.idNode.localIdx = distance(argSymtab->begin(),iter);
  152. return (newExp);
  153. }
  154. /* Returns an identifier conditional expression node of type GLOB_VAR_IDX.
  155. * This global variable is indexed by regi. */
  156. GlobalVariableIdx::GlobalVariableIdx (int16_t segValue, int16_t off, uint8_t regi, const LOCAL_ID *locSym)
  157. {
  158. size_t i;
  159. ident.type(GLOB_VAR_IDX);
  160. for (i = 0; i < locSym->csym(); i++)
  161. {
  162. const BWGLB_TYPE &lID(locSym->id_arr[i].id.bwGlb);
  163. if ((lID.seg == segValue) and (lID.off == off) and (lID.regi == regi))
  164. break;
  165. }
  166. if (i == locSym->csym())
  167. printf ("Error, indexed-glob var not found in local id table\n");
  168. idxGlbIdx = i;
  169. }
  170. string GlobalVariableIdx::walkCondExpr(Function *pProc, int *) const
  171. {
  172. ostringstream o;
  173. auto bwGlb = &pProc->localId.id_arr[idxGlbIdx].id.bwGlb;
  174. o << (bwGlb->seg << 4) + bwGlb->off << "["<<Machine_X86::regName(bwGlb->regi)<<"]";
  175. return o.str();
  176. }
  177. /* Returns an identifier conditional expression node of type LONG_VAR,
  178. * that points to the given index idx. */
  179. AstIdent *AstIdent::LongIdx (int idx)
  180. {
  181. AstIdent *newExp = new AstIdent;
  182. newExp->ident.idType = LONG_VAR;
  183. newExp->ident.idNode.longIdx = idx;
  184. return (newExp);
  185. }
  186. AstIdent *AstIdent::String(uint32_t idx)
  187. {
  188. AstIdent *newExp = new AstIdent;
  189. newExp->ident.idNode.strIdx = idx;
  190. newExp->ident.type(STRING);
  191. return newExp;
  192. }
  193. /* Returns an identifier conditional expression node of type LONG_VAR */
  194. AstIdent *AstIdent::Long(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, LLInst &atOffset)
  195. {
  196. AstIdent *newExp;
  197. /* Check for long constant and save it as a constant expression */
  198. if ((sd == SRC) and pIcode->ll()->testFlags(I)) /* constant */
  199. {
  200. int value;
  201. if (f == HIGH_FIRST)
  202. value = (pIcode->ll()->src().getImm2() << 16) + atOffset.src().getImm2();
  203. else/* LOW_FIRST */
  204. value = (atOffset.src().getImm2() << 16)+ pIcode->ll()->src().getImm2();
  205. newExp = new Constant(value,4);
  206. }
  207. /* Save it as a long expression (reg, stack or glob) */
  208. else
  209. {
  210. newExp = new AstIdent();
  211. newExp->ident.idType = LONG_VAR;
  212. newExp->ident.idNode.longIdx = localId->newLong(sd, pIcode, f, ix, du, atOffset);
  213. }
  214. return (newExp);
  215. }
  216. /* Returns an identifier conditional expression node of type OTHER.
  217. * Temporary solution, should really be encoded as an indexed type (eg.
  218. * arrays). */
  219. AstIdent *AstIdent::Other(eReg seg, eReg regi, int16_t off)
  220. {
  221. AstIdent *newExp = new AstIdent();
  222. newExp->ident.idType = OTHER;
  223. newExp->ident.idNode.other.seg = seg;
  224. newExp->ident.idNode.other.regi = regi;
  225. newExp->ident.idNode.other.off = off;
  226. return (newExp);
  227. }
  228. /* Returns an identifier conditional expression node of type TYPE_LONG or
  229. * TYPE_WORD_SIGN */
  230. AstIdent *AstIdent::idID (const ID *retVal, LOCAL_ID *locsym, iICODE ix_)
  231. {
  232. int idx;
  233. AstIdent *newExp=nullptr;
  234. switch(retVal->type)
  235. {
  236. case TYPE_LONG_SIGN:
  237. {
  238. newExp = new AstIdent();
  239. idx = locsym->newLongReg (TYPE_LONG_SIGN, retVal->longId(), ix_);
  240. newExp->ident.idType = LONG_VAR;
  241. newExp->ident.idNode.longIdx = idx;
  242. break;
  243. }
  244. case TYPE_WORD_SIGN:
  245. newExp = new RegisterNode(locsym->newByteWordReg(retVal->type, retVal->id.regi),WORD_REG,locsym);
  246. break;
  247. case TYPE_BYTE_SIGN:
  248. newExp = new RegisterNode(locsym->newByteWordReg(retVal->type, retVal->id.regi),BYTE_REG,locsym);
  249. break;
  250. default:
  251. fprintf(stderr,"AstIdent::idID unhandled type %d\n",retVal->type);
  252. }
  253. return (newExp);
  254. }
  255. /* Returns an identifier conditional expression node, according to the given
  256. * type.
  257. * Arguments:
  258. * duIcode: icode instruction that needs the du set.
  259. * du: operand is defined or used in current instruction. */
  260. Expr *AstIdent::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICODE ix_,ICODE &duIcode, operDu du)
  261. {
  262. Expr *newExp;
  263. int idx; /* idx into pIcode->localId table */
  264. const LLOperand &pm(*ll_insn.get(sd));
  265. if ( ((sd == DST) and ll_insn.testFlags(IM_DST)) or
  266. ((sd == SRC) and ll_insn.testFlags(IM_SRC)) or
  267. (sd == LHS_OP)) /* for MUL lhs */
  268. { /* implicit dx:ax */
  269. idx = pProc->localId.newLongReg (TYPE_LONG_SIGN, LONGID_TYPE(rDX, rAX), ix_);
  270. newExp = AstIdent::LongIdx (idx);
  271. duIcode.setRegDU (rDX, du);
  272. duIcode.setRegDU (rAX, du);
  273. }
  274. else if ((sd == DST) and ll_insn.testFlags(IM_TMP_DST))
  275. { /* implicit tmp */
  276. newExp = new RegisterNode(LLOperand(rTMP,2), &pProc->localId);
  277. duIcode.setRegDU(rTMP, (operDu)eUSE);
  278. }
  279. else if ((sd == SRC) and ll_insn.testFlags(I)) /* constant */
  280. newExp = new Constant(ll_insn.src().getImm2(), 2);
  281. else if (pm.regi == rUNDEF) /* global variable */
  282. newExp = new GlobalVariable(pm.segValue, pm.off);
  283. else if ( pm.isReg() ) /* register */
  284. {
  285. //(sd == SRC) ? ll_insn.getFlag() : ll_insn.getFlag() & NO_SRC_B
  286. newExp = new RegisterNode(pm, &pProc->localId);
  287. duIcode.setRegDU( pm.regi, du);
  288. }
  289. else if (pm.off) /* offset */
  290. { // TODO: this is ABI specific, should be actually based on Function calling conv
  291. if ((pm.seg == rSS) and (pm.regi == INDEX_BP)) /* idx on bp */
  292. {
  293. if (pm.off >= 0) /* argument */
  294. newExp = AstIdent::Param (pm.off, &pProc->args);
  295. else /* local variable */
  296. newExp = AstIdent::Loc (pm.off, &pProc->localId);
  297. }
  298. else if ((pm.seg == rDS) and (pm.regi == INDEX_BX)) /* bx */
  299. {
  300. if (pm.off > 0) /* global variable */
  301. newExp = new GlobalVariableIdx(pm.segValue, pm.off, rBX,&pProc->localId);
  302. else
  303. newExp = AstIdent::Other (pm.seg, pm.regi, pm.off);
  304. duIcode.setRegDU( rBX, eUSE);
  305. }
  306. else /* idx <> bp, bx */
  307. newExp = AstIdent::Other (pm.seg, pm.regi, pm.off);
  308. /**** check long ops, indexed global var *****/
  309. }
  310. else /* (pm->regi >= INDEXBASE and pm->off = 0) => indexed and no off */
  311. {
  312. if ((pm.seg == rDS) and (pm.regi > INDEX_BP_DI)) /* dereference */
  313. {
  314. eReg selected;
  315. switch (pm.regi) {
  316. case INDEX_SI: selected = rSI; break;
  317. case INDEX_DI: selected = rDI; break;
  318. case INDEX_BP: selected = rBP; break;
  319. case INDEX_BX: selected = rBX; break;
  320. default:
  321. newExp = nullptr;
  322. assert(false);
  323. }
  324. //NOTICE: was selected, 0
  325. newExp = new RegisterNode(LLOperand(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) and 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) and (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 == nullptr)
  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 == nullptr)
  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 == nullptr)
  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. /* Makes a copy of the given expression. Allocates newExp storage for each
  753. * node. Returns the copy. */
  754. Expr *BinaryOperator::clone() const
  755. {
  756. /* Expression node copy */
  757. return new BinaryOperator(m_op,m_lhs->clone(),m_rhs->clone());
  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,locId);
  794. }
  795. return this;
  796. }
  797. eReg AstIdent::otherLongRegi (eReg regi, int idx, LOCAL_ID *locTbl)
  798. {
  799. ID *id = &locTbl->id_arr[idx];
  800. if ((id->loc == REG_FRAME) and ((id->type == TYPE_LONG_SIGN) or
  801. (id->type == TYPE_LONG_UNSIGN)))
  802. {
  803. if (id->longId().h() == regi)
  804. return (id->longId().l());
  805. else if (id->longId().l() == regi)
  806. return (id->longId().h());
  807. }
  808. return rUNDEF; // Cristina: please check this!
  809. }
  810. string Constant::walkCondExpr(Function *, int *) const
  811. {
  812. ostringstream o;
  813. if (kte.kte < 1000)
  814. o << kte.kte;
  815. else
  816. o << "0x"<<std::hex << kte.kte;
  817. return o.str();
  818. }
  819. int Constant::hlTypeSize(Function *) const
  820. {
  821. return kte.size;
  822. }
  823. hlType Constant::expType(Function *pproc) const
  824. {
  825. return TYPE_CONST;
  826. }
  827. string FuncNode::walkCondExpr(Function *pProc, int *numLoc) const
  828. {
  829. return pProc->writeCall(call.proc,*call.args, numLoc);
  830. }
  831. int FuncNode::hlTypeSize(Function *) const
  832. {
  833. return hlSize[call.proc->retVal.type];
  834. }
  835. hlType FuncNode::expType(Function *) const
  836. {
  837. return call.proc->retVal.type;
  838. }