ast.cpp 26 KB

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