ast.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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 <QtCore/QTextStream>
  14. #include <boost/range.hpp>
  15. #include <boost/range/adaptor/filtered.hpp>
  16. #include <boost/range/algorithm.hpp>
  17. #include <boost/assign.hpp>
  18. #include <stdint.h>
  19. #include <string>
  20. #include <sstream>
  21. #include <iostream>
  22. #include <cassert>
  23. using namespace std;
  24. using namespace boost;
  25. using namespace boost::adaptors;
  26. extern int strSize (const uint8_t *, char);
  27. extern char *cChar(uint8_t c);
  28. // Conditional operator symbols in C. Index by condOp enumeration type
  29. static const char * const condOpSym[] = { " <= ", " < ", " == ", " != ", " > ", " >= ",
  30. " & ", " | ", " ^ ", " ~ ",
  31. " + ", " - ", " * ", " / ",
  32. " >> ", " << ", " % ", " && ", " || " };
  33. /* Local expression stack */
  34. //typedef struct _EXP_STK {
  35. // COND_EXPR *exp;
  36. // struct _EXP_STK *next;
  37. //} EXP_STK; - for local expression stack
  38. /* Returns the integer i in C hexadecimal format */
  39. static const char *hexStr (uint16_t i)
  40. {
  41. static char buf[10];
  42. sprintf (buf, "%s%x", (i > 9) ? "0x" : "", i);
  43. return (buf);
  44. }
  45. /* Sets the du record for registers according to the du flag */
  46. void ICODE::setRegDU (eReg regi, operDu du_in)
  47. {
  48. // printf("%s %d %x\n",__FUNCTION__,regi,int(du_in));
  49. if(regi==rSP)
  50. {
  51. printf("Discarding SP def&use info for now\n");
  52. return;
  53. }
  54. switch (du_in)
  55. {
  56. case eDEF:
  57. du.def.addReg(regi);
  58. du1.addDef(regi);
  59. break;
  60. case eUSE:
  61. du.use.addReg(regi);
  62. break;
  63. case USE_DEF:
  64. du.addDefinedAndUsed(regi);
  65. du1.addDef(regi);
  66. break;
  67. case NONE: /* do nothing */
  68. break;
  69. }
  70. }
  71. /* Copies the def, use, or def and use fields of duIcode into pIcode */
  72. void ICODE::copyDU(const ICODE &duIcode, operDu _du, operDu duDu)
  73. {
  74. switch (_du)
  75. {
  76. case eDEF:
  77. if (duDu == eDEF)
  78. du.def=duIcode.du.def;
  79. else
  80. du.def=duIcode.du.use;
  81. break;
  82. case eUSE:
  83. if (duDu == eDEF)
  84. du.use = duIcode.du.def;
  85. else
  86. du.use = duIcode.du.use;
  87. break;
  88. case USE_DEF:
  89. du = duIcode.du;
  90. break;
  91. case NONE:
  92. assert(false);
  93. break;
  94. }
  95. }
  96. /* Creates a conditional boolean expression and returns it */
  97. //COND_EXPR *COND_EXPR::boolOp(COND_EXPR *_lhs, COND_EXPR *_rhs, condOp _op)
  98. //{
  99. // return BinaryOperator::Create(_op,_lhs,_rhs);
  100. //}
  101. /* Returns a unary conditional expression node. This procedure should
  102. * only be used with the following conditional node types: NEGATION,
  103. * ADDRESSOF, DEREFERENCE, POST_INC, POST_DEC, PRE_INC, PRE_DEC */
  104. /* Returns an identifier conditional expression node of type GLOB_VAR */
  105. GlobalVariable::GlobalVariable(int16_t segValue, int16_t off)
  106. {
  107. uint32_t adr;
  108. valid = true;
  109. ident.idType = GLOB_VAR;
  110. adr = opAdr(segValue, off);
  111. auto i=Project::get()->getSymIdxByAdd(adr);
  112. if ( not Project::get()->validSymIdx(i) )
  113. {
  114. printf ("Error, glob var not found in symtab\n");
  115. valid = false;
  116. }
  117. globIdx = i;
  118. }
  119. QString GlobalVariable::walkCondExpr(Function *, int *) const
  120. {
  121. if(valid)
  122. return Project::get()->symtab[globIdx].name;
  123. return "INVALID GlobalVariable";
  124. }
  125. /* Returns an identifier conditional expression node of type LOCAL_VAR */
  126. AstIdent *AstIdent::Loc(int off, LOCAL_ID *localId)
  127. {
  128. size_t i;
  129. AstIdent *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) and (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) and (lID.off == off) and (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. QString GlobalVariableIdx::walkCondExpr(Function *pProc, int *) const
  172. {
  173. auto bwGlb = &pProc->localId.id_arr[idxGlbIdx].id.bwGlb;
  174. return QString("%1[%2]").arg((bwGlb->seg << 4) + bwGlb->off).arg(Machine_X86::regName(bwGlb->regi));
  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) and 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) and ll_insn.testFlags(IM_DST)) or
  265. ((sd == SRC) and 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) and 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) and 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) and (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) and (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 and pm->off = 0) => indexed and no off */
  310. {
  311. if ((pm.seg == rDS) and (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) and 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) and (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 QString getString (int offset)
  476. {
  477. PROG &prog(Project::get()->prog);
  478. QString 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;
  486. }
  487. QString BinaryOperator::walkCondExpr(Function * pProc, int* numLoc) const
  488. {
  489. assert(rhs());
  490. return QString("(%1%2%3)")
  491. .arg((m_op!=NOT) ? lhs()->walkCondExpr(pProc, numLoc) : "")
  492. .arg(condOpSym[m_op])
  493. .arg(rhs()->walkCondExpr(pProc, numLoc));
  494. }
  495. QString AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
  496. {
  497. int16_t off; /* temporal - for OTHER */
  498. ID* id; /* Pointer to local identifier table */
  499. BWGLB_TYPE* bwGlb; /* Ptr to BWGLB_TYPE (global indexed var) */
  500. STKSYM * psym; /* Pointer to argument in the stack */
  501. QString codeContents;
  502. QString collectedContents;
  503. QTextStream codeOut(&codeContents);
  504. QTextStream o(&collectedContents);
  505. switch (ident.idType)
  506. {
  507. case LOCAL_VAR:
  508. o << pProc->localId.id_arr[ident.idNode.localIdx].name;
  509. break;
  510. case PARAM:
  511. psym = &pProc->args[ident.idNode.paramIdx];
  512. if (psym->hasMacro)
  513. o << psym->macro<<"("<<psym->name<< ")";
  514. else
  515. o << psym->name;
  516. break;
  517. case STRING:
  518. o << getString (ident.idNode.strIdx);
  519. break;
  520. case LONG_VAR:
  521. id = &pProc->localId.id_arr[ident.idNode.longIdx];
  522. if (id->name[0] != '\0') /* STK_FRAME & REG w/name*/
  523. o << id->name;
  524. else if (id->loc == REG_FRAME)
  525. {
  526. id->setLocalName(++(*numLoc));
  527. codeOut <<TypeContainer::typeName(id->type)<< " "<<id->name<<"; ";
  528. codeOut <<"/* "<<Machine_X86::regName(id->longId().h()) << ":" <<
  529. Machine_X86::regName(id->longId().l()) << " */\n";
  530. o << id->name;
  531. pProc->localId.propLongId (id->longId().l(),id->longId().h(), id->name);
  532. }
  533. else /* GLB_FRAME */
  534. {
  535. if (id->id.longGlb.regi == 0) /* not indexed */
  536. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"]";
  537. else if (id->id.longGlb.regi == rBX)
  538. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"][bx]";
  539. }
  540. break;
  541. case OTHER:
  542. off = ident.idNode.other.off;
  543. o << Machine_X86::regName(ident.idNode.other.seg)<< "[";
  544. o << Machine_X86::regName(ident.idNode.other.regi);
  545. if (off < 0)
  546. o << "-"<< hexStr (-off);
  547. else if (off>0)
  548. o << "+"<< hexStr (off);
  549. o << "]";
  550. break;
  551. default:
  552. assert(false);
  553. return "";
  554. } /* eos */
  555. cCode.appendDecl(codeContents);
  556. return collectedContents;
  557. }
  558. QString UnaryOperator::wrapUnary(Function *pProc, int *numLoc,QChar op) const
  559. {
  560. QString outStr = op;
  561. QString inner = unaryExp->walkCondExpr (pProc, numLoc);
  562. if (unaryExp->m_type == IDENTIFIER)
  563. outStr += inner;
  564. else
  565. outStr += "("+inner+')';
  566. return outStr;
  567. }
  568. QString UnaryOperator::walkCondExpr(Function *pProc, int *numLoc) const
  569. {
  570. QString outStr;
  571. switch(m_type)
  572. {
  573. case NEGATION:
  574. outStr+=wrapUnary(pProc,numLoc,'!');
  575. break;
  576. case ADDRESSOF:
  577. outStr+=wrapUnary(pProc,numLoc,'&');
  578. break;
  579. case DEREFERENCE:
  580. outStr+=wrapUnary(pProc,numLoc,'*');
  581. break;
  582. case POST_INC:
  583. outStr += unaryExp->walkCondExpr (pProc, numLoc) + "++";
  584. break;
  585. case POST_DEC:
  586. outStr += unaryExp->walkCondExpr (pProc, numLoc) + "--";
  587. break;
  588. case PRE_INC:
  589. outStr += "++" + unaryExp->walkCondExpr (pProc, numLoc);
  590. break;
  591. case PRE_DEC:
  592. outStr += "--" + unaryExp->walkCondExpr (pProc, numLoc);
  593. break;
  594. }
  595. return outStr;
  596. }
  597. /* Walks the conditional expression tree and returns the result on a string */
  598. /* Changes the boolean conditional operator at the root of this expression */
  599. void BinaryOperator::changeBoolOp (condOp newOp)
  600. {
  601. m_op = newOp;
  602. }
  603. bool Expr::insertSubTreeReg (AstIdent *&tree, Expr *_expr, eReg regi,const LOCAL_ID *locsym)
  604. {
  605. Expr *nd = tree;
  606. bool res=insertSubTreeReg (nd, _expr, regi,locsym);
  607. if(res)
  608. {
  609. tree= dynamic_cast<AstIdent *>(nd);
  610. return true;
  611. assert(tree);
  612. }
  613. return false;
  614. }
  615. /* Inserts the expression exp into the tree at the location specified by the
  616. * register regi */
  617. bool Expr::insertSubTreeReg (Expr *&tree, Expr *_expr, eReg regi,const LOCAL_ID *locsym)
  618. {
  619. if (tree == nullptr)
  620. return false;
  621. Expr *temp=tree->insertSubTreeReg(_expr,regi,locsym);
  622. if(nullptr!=temp)
  623. {
  624. tree=temp;
  625. return true;
  626. }
  627. return false;
  628. }
  629. Expr *UnaryOperator::insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym)
  630. {
  631. Expr *temp;
  632. switch (m_type) {
  633. case NEGATION:
  634. case ADDRESSOF:
  635. case DEREFERENCE:
  636. temp = unaryExp->insertSubTreeReg( _expr, regi, locsym);
  637. if (nullptr!=temp)
  638. {
  639. unaryExp = temp;
  640. return this;
  641. }
  642. return nullptr;
  643. default:
  644. fprintf(stderr,"insertSubTreeReg attempt on unhandled type %d\n",m_type);
  645. }
  646. return nullptr;
  647. }
  648. Expr *BinaryOperator::insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym)
  649. {
  650. Expr *r;
  651. if(this->op()!=NOT)
  652. {
  653. assert(m_lhs);
  654. r=m_lhs->insertSubTreeReg(_expr,regi,locsym);
  655. if(r)
  656. {
  657. m_lhs = r;
  658. return this;
  659. }
  660. }
  661. assert(m_rhs);
  662. r=m_rhs->insertSubTreeReg(_expr,regi,locsym);
  663. if(r)
  664. {
  665. m_rhs = r;
  666. return this;
  667. }
  668. return nullptr;
  669. }
  670. Expr *AstIdent::insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym)
  671. {
  672. if (ident.idType == REGISTER)
  673. {
  674. assert(false);
  675. }
  676. return nullptr;
  677. }
  678. /* Inserts the expression exp into the tree at the location specified by the
  679. * long register index longIdx*/
  680. bool Expr::insertSubTreeLongReg(Expr *_expr, Expr *&tree, int longIdx)
  681. {
  682. if (tree == nullptr)
  683. return false;
  684. Expr *temp=tree->insertSubTreeLongReg(_expr,longIdx);
  685. if(nullptr!=temp)
  686. {
  687. tree=temp;
  688. return true;
  689. }
  690. return false;
  691. }
  692. Expr *UnaryOperator::insertSubTreeLongReg(Expr *_expr, int longIdx)
  693. {
  694. Expr *temp = unaryExp->insertSubTreeLongReg(_expr,longIdx);
  695. if (nullptr!=temp)
  696. {
  697. unaryExp = temp;
  698. return this;
  699. }
  700. return nullptr;
  701. }
  702. Expr *BinaryOperator::insertSubTreeLongReg(Expr *_expr, int longIdx)
  703. {
  704. Expr *r;
  705. if(m_op!=NOT)
  706. {
  707. r=m_lhs->insertSubTreeLongReg(_expr,longIdx);
  708. if(r)
  709. {
  710. m_lhs = r;
  711. return this;
  712. }
  713. }
  714. r=m_rhs->insertSubTreeLongReg(_expr,longIdx);
  715. if(r)
  716. {
  717. m_rhs = r;
  718. return this;
  719. }
  720. return nullptr;
  721. }
  722. Expr *AstIdent::insertSubTreeLongReg(Expr *_expr, int longIdx)
  723. {
  724. if (ident.idNode.longIdx == longIdx)
  725. {
  726. return _expr;
  727. }
  728. return nullptr;
  729. }
  730. /* Makes a copy of the given expression. Allocates newExp storage for each
  731. * node. Returns the copy. */
  732. Expr *BinaryOperator::clone() const
  733. {
  734. /* Expression node copy */
  735. return new BinaryOperator(m_op,m_lhs->clone(),m_rhs->clone());
  736. }
  737. Expr *BinaryOperator::inverse() const
  738. {
  739. static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL,
  740. LESS_EQUAL, LESS, DUMMY,DUMMY,DUMMY,DUMMY,
  741. DUMMY, DUMMY, DUMMY, DUMMY, DUMMY, DUMMY,
  742. DUMMY, DBL_OR, DBL_AND};
  743. BinaryOperator *res=reinterpret_cast<BinaryOperator *>(this->clone());
  744. switch (m_op)
  745. {
  746. case LESS_EQUAL: case LESS: case EQUAL:
  747. case NOT_EQUAL: case GREATER: case GREATER_EQUAL:
  748. res->m_op = invCondOp[m_op];
  749. return res;
  750. case AND: case OR: case XOR: case NOT: case ADD:
  751. case SUB: case MUL: case DIV: case SHR: case SHL: case MOD:
  752. return UnaryOperator::Create(NEGATION, res);
  753. case DBL_AND: case DBL_OR:
  754. res->m_op = invCondOp[m_op];
  755. res->m_lhs=m_lhs->inverse ();
  756. res->m_rhs=m_rhs->inverse ();
  757. return res;
  758. default:
  759. fprintf(stderr,"BinaryOperator::inverse attempt on unhandled op %d\n",m_op);
  760. } /* eos */
  761. assert(false);
  762. return res;
  763. }
  764. Expr *AstIdent::performLongRemoval(eReg regi, LOCAL_ID *locId)
  765. {
  766. eReg otherRegi; /* high or low part of long register */
  767. if (ident.idType == LONG_VAR)
  768. {
  769. otherRegi = otherLongRegi (regi, ident.idNode.longIdx, locId);
  770. delete this;
  771. return new RegisterNode(locId->newByteWordReg(TYPE_WORD_SIGN,otherRegi),WORD_REG,locId);
  772. }
  773. return this;
  774. }
  775. eReg AstIdent::otherLongRegi (eReg regi, int idx, LOCAL_ID *locTbl)
  776. {
  777. ID *id = &locTbl->id_arr[idx];
  778. if ((id->loc == REG_FRAME) and ((id->type == TYPE_LONG_SIGN) or
  779. (id->type == TYPE_LONG_UNSIGN)))
  780. {
  781. if (id->longId().h() == regi)
  782. return (id->longId().l());
  783. else if (id->longId().l() == regi)
  784. return (id->longId().h());
  785. }
  786. return rUNDEF; // Cristina: please check this!
  787. }
  788. QString Constant::walkCondExpr(Function *, int *) const
  789. {
  790. if (kte.kte < 1000)
  791. return QString::number(kte.kte);
  792. else
  793. return "0x" + QString::number(kte.kte,16);
  794. }
  795. int Constant::hlTypeSize(Function *) const
  796. {
  797. return kte.size;
  798. }
  799. hlType Constant::expType(Function *pproc) const
  800. {
  801. return TYPE_CONST;
  802. }
  803. QString FuncNode::walkCondExpr(Function *pProc, int *numLoc) const
  804. {
  805. return pProc->writeCall(call.proc,*call.args, numLoc);
  806. }
  807. int FuncNode::hlTypeSize(Function *) const
  808. {
  809. return hlSize[call.proc->retVal.type];
  810. }
  811. hlType FuncNode::expType(Function *) const
  812. {
  813. return call.proc->retVal.type;
  814. }