ast.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. AstIdent *newExp=nullptr;
  232. switch(retVal->type)
  233. {
  234. case TYPE_LONG_SIGN:
  235. {
  236. newExp = new AstIdent();
  237. int idx = locsym->newLongReg (TYPE_LONG_SIGN, retVal->longId(), 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,locsym);
  244. break;
  245. case TYPE_BYTE_SIGN:
  246. newExp = new RegisterNode(locsym->newByteWordReg(retVal->type, retVal->id.regi),BYTE_REG,locsym);
  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(*ll_insn.get(sd));
  263. if ( ((sd == DST) and ll_insn.testFlags(IM_DST)) or
  264. ((sd == SRC) and 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, LONGID_TYPE(rDX, rAX), ix_);
  268. newExp = AstIdent::LongIdx (idx);
  269. duIcode.setRegDU (rDX, du);
  270. duIcode.setRegDU (rAX, du);
  271. }
  272. else if ((sd == DST) and ll_insn.testFlags(IM_TMP_DST))
  273. { /* implicit tmp */
  274. newExp = new RegisterNode(LLOperand(rTMP,2), &pProc->localId);
  275. duIcode.setRegDU(rTMP, (operDu)eUSE);
  276. }
  277. else if ((sd == SRC) and 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. //(sd == SRC) ? ll_insn.getFlag() : ll_insn.getFlag() & NO_SRC_B
  284. newExp = new RegisterNode(pm, &pProc->localId);
  285. duIcode.setRegDU( pm.regi, du);
  286. }
  287. else if (pm.off) /* offset */
  288. { // TODO: this is ABI specific, should be actually based on Function calling conv
  289. if ((pm.seg == rSS) and (pm.regi == INDEX_BP)) /* idx on bp */
  290. {
  291. if (pm.off >= 0) /* argument */
  292. newExp = AstIdent::Param (pm.off, &pProc->args);
  293. else /* local variable */
  294. newExp = AstIdent::Loc (pm.off, &pProc->localId);
  295. }
  296. else if ((pm.seg == rDS) and (pm.regi == INDEX_BX)) /* bx */
  297. {
  298. if (pm.off > 0) /* global variable */
  299. newExp = new GlobalVariableIdx(pm.segValue, pm.off, rBX,&pProc->localId);
  300. else
  301. newExp = AstIdent::Other (pm.seg, pm.regi, pm.off);
  302. duIcode.setRegDU( rBX, eUSE);
  303. }
  304. else /* idx <> bp, bx */
  305. newExp = AstIdent::Other (pm.seg, pm.regi, pm.off);
  306. /**** check long ops, indexed global var *****/
  307. }
  308. else /* (pm->regi >= INDEXBASE and pm->off = 0) => indexed and no off */
  309. {
  310. if ((pm.seg == rDS) and (pm.regi > INDEX_BP_DI)) /* dereference */
  311. {
  312. eReg selected;
  313. switch (pm.regi) {
  314. case INDEX_SI: selected = rSI; break;
  315. case INDEX_DI: selected = rDI; break;
  316. case INDEX_BP: selected = rBP; break;
  317. case INDEX_BX: selected = rBX; break;
  318. default:
  319. newExp = nullptr;
  320. assert(false);
  321. }
  322. //NOTICE: was selected, 0
  323. newExp = new RegisterNode(LLOperand(selected, 0), &pProc->localId);
  324. duIcode.setRegDU( selected, du);
  325. newExp = UnaryOperator::Create(DEREFERENCE, newExp);
  326. }
  327. else
  328. newExp = AstIdent::Other (pm.seg, pm.regi, 0);
  329. }
  330. return (newExp);
  331. }
  332. /* Returns the identifier type */
  333. condId LLInst::idType(opLoc sd) const
  334. {
  335. const LLOperand &pm((sd == SRC) ? src() : m_dst);
  336. if ((sd == SRC) and testFlags(I))
  337. return (CONSTANT);
  338. else if (pm.regi == 0)
  339. return (GLOB_VAR);
  340. else if ( pm.isReg() )
  341. return (REGISTER);
  342. else if ((pm.seg == rSS) and (pm.regi == INDEX_BP))
  343. {
  344. //TODO: which pm.seg/pm.regi pairs should produce PARAM/LOCAL_VAR ?
  345. if (pm.off >= 0)
  346. return (PARAM);
  347. else
  348. return (LOCAL_VAR);
  349. }
  350. else
  351. return (OTHER);
  352. }
  353. /* Size of hl types */
  354. int hlSize[] = {2, 1, 1, 2, 2, 4, 4, 4, 2, 2, 1, 4, 4};
  355. int Expr::hlTypeSize(Function * pproc) const
  356. {
  357. if (this == nullptr)
  358. return (2); /* for TYPE_UNKNOWN */
  359. fprintf(stderr,"hlTypeSize queried for Unkown type %d \n",m_type);
  360. return 2; // CC: is this correct?
  361. }
  362. /* Returns the type of the expression */
  363. int BinaryOperator::hlTypeSize(Function * pproc) const
  364. {
  365. return std::max(lhs()->hlTypeSize (pproc),rhs()->hlTypeSize (pproc));
  366. }
  367. int UnaryOperator::hlTypeSize(Function *pproc) const
  368. {
  369. return (unaryExp->hlTypeSize (pproc));
  370. }
  371. int GlobalVariable::hlTypeSize(Function *pproc) const
  372. {
  373. return (Project::get()->symbolSize(globIdx));
  374. }
  375. int GlobalVariableIdx::hlTypeSize(Function *pproc) const
  376. {
  377. return (hlSize[pproc->localId.id_arr[idxGlbIdx].type]);
  378. }
  379. int AstIdent::hlTypeSize(Function *pproc) const
  380. {
  381. switch (ident.idType)
  382. {
  383. case GLOB_VAR:
  384. assert(false);
  385. return 1;
  386. case LOCAL_VAR:
  387. return (hlSize[pproc->localId.id_arr[ident.idNode.localIdx].type]);
  388. case PARAM:
  389. return (hlSize[pproc->args[ident.idNode.paramIdx].type]);
  390. case STRING:
  391. return (2);
  392. case LONG_VAR:
  393. return (4);
  394. case OTHER:
  395. return (2);
  396. default:
  397. assert(false);
  398. return -1;
  399. } /* eos */
  400. }
  401. hlType BinaryOperator::expType(Function *pproc) const
  402. {
  403. hlType first = lhs()->expType ( pproc );
  404. hlType second = rhs()->expType ( pproc );
  405. if (first != second)
  406. {
  407. if (lhs()->hlTypeSize(pproc) > rhs()->hlTypeSize (pproc))
  408. return (first);
  409. else
  410. return (second);
  411. }
  412. else
  413. return (first);
  414. }
  415. hlType UnaryOperator::expType(Function *pproc) const
  416. {
  417. return unaryExp->expType (pproc);
  418. }
  419. hlType GlobalVariable::expType(Function *pproc) const
  420. {
  421. return Project::get()->symbolType(globIdx);
  422. }
  423. hlType GlobalVariableIdx::expType(Function *pproc) const
  424. {
  425. return (pproc->localId.id_arr[idxGlbIdx].type);
  426. }
  427. hlType AstIdent::expType(Function *pproc) const
  428. {
  429. switch (ident.idType)
  430. {
  431. case UNDEF:
  432. case CONSTANT:
  433. case FUNCTION:
  434. case REGISTER:
  435. case GLOB_VAR:
  436. case GLOB_VAR_IDX:
  437. assert(false);
  438. return TYPE_UNKNOWN;
  439. case LOCAL_VAR:
  440. return (pproc->localId.id_arr[ident.idNode.localIdx].type);
  441. case PARAM:
  442. return (pproc->args[ident.idNode.paramIdx].type);
  443. case STRING:
  444. return (TYPE_STR);
  445. case LONG_VAR:
  446. return (pproc->localId.id_arr[ident.idNode.longIdx].type);
  447. default:
  448. return (TYPE_UNKNOWN);
  449. } /* eos */
  450. return (TYPE_UNKNOWN);
  451. }
  452. /* Returns the type of the expression */
  453. /* Removes the register from the tree. If the register was part of a long
  454. * register (eg. dx:ax), the node gets transformed into an integer register
  455. * node. */
  456. Expr * HlTypeSupport::performLongRemoval (eReg regi, LOCAL_ID *locId, Expr *tree)
  457. {
  458. switch (tree->m_type) {
  459. case BOOLEAN_OP:
  460. case POST_INC: case POST_DEC:
  461. case PRE_INC: case PRE_DEC:
  462. case NEGATION: case ADDRESSOF:
  463. case DEREFERENCE:
  464. case IDENTIFIER:
  465. return tree->performLongRemoval(regi,locId);
  466. break;
  467. default:
  468. fprintf(stderr,"performLongRemoval attemped on %d\n",tree->m_type);
  469. break;
  470. }
  471. return tree;
  472. }
  473. /* Returns the string located in image, formatted in C format. */
  474. static QString getString (int offset)
  475. {
  476. PROG &prog(Project::get()->prog);
  477. QString o;
  478. int strLen, i;
  479. strLen = strSize (&prog.image()[offset], '\0');
  480. o += '"';
  481. for (i = 0; i < strLen; i++)
  482. o += cChar(prog.image()[offset+i]);
  483. o += "\"\0";
  484. return o;
  485. }
  486. QString BinaryOperator::walkCondExpr(Function * pProc, int* numLoc) const
  487. {
  488. assert(rhs());
  489. return QString("(%1%2%3)")
  490. .arg((m_op!=NOT) ? lhs()->walkCondExpr(pProc, numLoc) : "")
  491. .arg(condOpSym[m_op])
  492. .arg(rhs()->walkCondExpr(pProc, numLoc));
  493. }
  494. QString AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
  495. {
  496. int16_t off; /* temporal - for OTHER */
  497. ID* id; /* Pointer to local identifier table */
  498. BWGLB_TYPE* bwGlb; /* Ptr to BWGLB_TYPE (global indexed var) */
  499. STKSYM * psym; /* Pointer to argument in the stack */
  500. QString codeContents;
  501. QString collectedContents;
  502. QTextStream codeOut(&codeContents);
  503. QTextStream o(&collectedContents);
  504. switch (ident.idType)
  505. {
  506. case LOCAL_VAR:
  507. o << pProc->localId.id_arr[ident.idNode.localIdx].name;
  508. break;
  509. case PARAM:
  510. psym = &pProc->args[ident.idNode.paramIdx];
  511. if (psym->hasMacro)
  512. o << psym->macro<<"("<<psym->name<< ")";
  513. else
  514. o << psym->name;
  515. break;
  516. case STRING:
  517. o << getString (ident.idNode.strIdx);
  518. break;
  519. case LONG_VAR:
  520. id = &pProc->localId.id_arr[ident.idNode.longIdx];
  521. if (id->name[0] != '\0') /* STK_FRAME & REG w/name*/
  522. o << id->name;
  523. else if (id->loc == REG_FRAME)
  524. {
  525. id->setLocalName(++(*numLoc));
  526. codeOut <<TypeContainer::typeName(id->type)<< " "<<id->name<<"; ";
  527. codeOut <<"/* "<<Machine_X86::regName(id->longId().h()) << ":" <<
  528. Machine_X86::regName(id->longId().l()) << " */\n";
  529. o << id->name;
  530. pProc->localId.propLongId (id->longId().l(),id->longId().h(), id->name);
  531. }
  532. else /* GLB_FRAME */
  533. {
  534. if (id->id.longGlb.regi == 0) /* not indexed */
  535. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"]";
  536. else if (id->id.longGlb.regi == rBX)
  537. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"][bx]";
  538. }
  539. break;
  540. case OTHER:
  541. off = ident.idNode.other.off;
  542. o << Machine_X86::regName(ident.idNode.other.seg)<< "[";
  543. o << Machine_X86::regName(ident.idNode.other.regi);
  544. if (off < 0)
  545. o << "-"<< hexStr (-off);
  546. else if (off>0)
  547. o << "+"<< hexStr (off);
  548. o << "]";
  549. break;
  550. default:
  551. assert(false);
  552. return "";
  553. } /* eos */
  554. cCode.appendDecl(codeContents);
  555. return collectedContents;
  556. }
  557. QString UnaryOperator::wrapUnary(Function *pProc, int *numLoc,QChar op) const
  558. {
  559. QString outStr = op;
  560. QString inner = unaryExp->walkCondExpr (pProc, numLoc);
  561. if (unaryExp->m_type == IDENTIFIER)
  562. outStr += inner;
  563. else
  564. outStr += "("+inner+')';
  565. return outStr;
  566. }
  567. QString UnaryOperator::walkCondExpr(Function *pProc, int *numLoc) const
  568. {
  569. QString outStr;
  570. switch(m_type)
  571. {
  572. case NEGATION:
  573. outStr+=wrapUnary(pProc,numLoc,'!');
  574. break;
  575. case ADDRESSOF:
  576. outStr+=wrapUnary(pProc,numLoc,'&');
  577. break;
  578. case DEREFERENCE:
  579. outStr+=wrapUnary(pProc,numLoc,'*');
  580. break;
  581. case POST_INC:
  582. outStr += unaryExp->walkCondExpr (pProc, numLoc) + "++";
  583. break;
  584. case POST_DEC:
  585. outStr += unaryExp->walkCondExpr (pProc, numLoc) + "--";
  586. break;
  587. case PRE_INC:
  588. outStr += "++" + unaryExp->walkCondExpr (pProc, numLoc);
  589. break;
  590. case PRE_DEC:
  591. outStr += "--" + unaryExp->walkCondExpr (pProc, numLoc);
  592. break;
  593. }
  594. return outStr;
  595. }
  596. /* Walks the conditional expression tree and returns the result on a string */
  597. /* Changes the boolean conditional operator at the root of this expression */
  598. void BinaryOperator::changeBoolOp (condOp newOp)
  599. {
  600. m_op = newOp;
  601. }
  602. bool Expr::insertSubTreeReg (AstIdent *&tree, Expr *_expr, eReg regi,const LOCAL_ID *locsym)
  603. {
  604. Expr *nd = tree;
  605. bool res=insertSubTreeReg (nd, _expr, regi,locsym);
  606. if(res)
  607. {
  608. tree= dynamic_cast<AstIdent *>(nd);
  609. return true;
  610. assert(tree);
  611. }
  612. return false;
  613. }
  614. /* Inserts the expression exp into the tree at the location specified by the
  615. * register regi */
  616. bool Expr::insertSubTreeReg (Expr *&tree, Expr *_expr, eReg regi,const LOCAL_ID *locsym)
  617. {
  618. if (tree == nullptr)
  619. return false;
  620. Expr *temp=tree->insertSubTreeReg(_expr,regi,locsym);
  621. if(nullptr!=temp)
  622. {
  623. tree=temp;
  624. return true;
  625. }
  626. return false;
  627. }
  628. Expr *UnaryOperator::insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym)
  629. {
  630. Expr *temp;
  631. switch (m_type) {
  632. case NEGATION:
  633. case ADDRESSOF:
  634. case DEREFERENCE:
  635. temp = unaryExp->insertSubTreeReg( _expr, regi, locsym);
  636. if (nullptr!=temp)
  637. {
  638. unaryExp = temp;
  639. return this;
  640. }
  641. return nullptr;
  642. default:
  643. fprintf(stderr,"insertSubTreeReg attempt on unhandled type %d\n",m_type);
  644. }
  645. return nullptr;
  646. }
  647. Expr *BinaryOperator::insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym)
  648. {
  649. Expr *r;
  650. if(this->op()!=NOT)
  651. {
  652. assert(m_lhs);
  653. r=m_lhs->insertSubTreeReg(_expr,regi,locsym);
  654. if(r)
  655. {
  656. m_lhs = r;
  657. return this;
  658. }
  659. }
  660. assert(m_rhs);
  661. r=m_rhs->insertSubTreeReg(_expr,regi,locsym);
  662. if(r)
  663. {
  664. m_rhs = r;
  665. return this;
  666. }
  667. return nullptr;
  668. }
  669. Expr *AstIdent::insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym)
  670. {
  671. if (ident.idType == REGISTER)
  672. {
  673. assert(false);
  674. }
  675. return nullptr;
  676. }
  677. /* Inserts the expression exp into the tree at the location specified by the
  678. * long register index longIdx*/
  679. bool Expr::insertSubTreeLongReg(Expr *_expr, Expr *&tree, int longIdx)
  680. {
  681. if (tree == nullptr)
  682. return false;
  683. Expr *temp=tree->insertSubTreeLongReg(_expr,longIdx);
  684. if(nullptr!=temp)
  685. {
  686. tree=temp;
  687. return true;
  688. }
  689. return false;
  690. }
  691. Expr *UnaryOperator::insertSubTreeLongReg(Expr *_expr, int longIdx)
  692. {
  693. Expr *temp = unaryExp->insertSubTreeLongReg(_expr,longIdx);
  694. if (nullptr!=temp)
  695. {
  696. unaryExp = temp;
  697. return this;
  698. }
  699. return nullptr;
  700. }
  701. Expr *BinaryOperator::insertSubTreeLongReg(Expr *_expr, int longIdx)
  702. {
  703. Expr *r;
  704. if(m_op!=NOT)
  705. {
  706. r=m_lhs->insertSubTreeLongReg(_expr,longIdx);
  707. if(r)
  708. {
  709. m_lhs = r;
  710. return this;
  711. }
  712. }
  713. r=m_rhs->insertSubTreeLongReg(_expr,longIdx);
  714. if(r)
  715. {
  716. m_rhs = r;
  717. return this;
  718. }
  719. return nullptr;
  720. }
  721. Expr *AstIdent::insertSubTreeLongReg(Expr *_expr, int longIdx)
  722. {
  723. if (ident.idNode.longIdx == longIdx)
  724. {
  725. return _expr;
  726. }
  727. return nullptr;
  728. }
  729. /* Makes a copy of the given expression. Allocates newExp storage for each
  730. * node. Returns the copy. */
  731. Expr *BinaryOperator::clone() const
  732. {
  733. /* Expression node copy */
  734. return new BinaryOperator(m_op,m_lhs->clone(),m_rhs->clone());
  735. }
  736. Expr *BinaryOperator::inverse() const
  737. {
  738. static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL,
  739. LESS_EQUAL, LESS, DUMMY,DUMMY,DUMMY,DUMMY,
  740. DUMMY, DUMMY, DUMMY, DUMMY, DUMMY, DUMMY,
  741. DUMMY, DBL_OR, DBL_AND};
  742. BinaryOperator *res=reinterpret_cast<BinaryOperator *>(this->clone());
  743. switch (m_op)
  744. {
  745. case LESS_EQUAL: case LESS: case EQUAL:
  746. case NOT_EQUAL: case GREATER: case GREATER_EQUAL:
  747. res->m_op = invCondOp[m_op];
  748. return res;
  749. case AND: case OR: case XOR: case NOT: case ADD:
  750. case SUB: case MUL: case DIV: case SHR: case SHL: case MOD:
  751. return UnaryOperator::Create(NEGATION, res);
  752. case DBL_AND: case DBL_OR:
  753. res->m_op = invCondOp[m_op];
  754. res->m_lhs=m_lhs->inverse ();
  755. res->m_rhs=m_rhs->inverse ();
  756. return res;
  757. default:
  758. fprintf(stderr,"BinaryOperator::inverse attempt on unhandled op %d\n",m_op);
  759. } /* eos */
  760. assert(false);
  761. return res;
  762. }
  763. Expr *AstIdent::performLongRemoval(eReg regi, LOCAL_ID *locId)
  764. {
  765. eReg otherRegi; /* high or low part of long register */
  766. if (ident.idType == LONG_VAR)
  767. {
  768. otherRegi = otherLongRegi (regi, ident.idNode.longIdx, locId);
  769. delete this;
  770. return new RegisterNode(locId->newByteWordReg(TYPE_WORD_SIGN,otherRegi),WORD_REG,locId);
  771. }
  772. return this;
  773. }
  774. eReg AstIdent::otherLongRegi (eReg regi, int idx, LOCAL_ID *locTbl)
  775. {
  776. ID *id = &locTbl->id_arr[idx];
  777. if ((id->loc == REG_FRAME) and ((id->type == TYPE_LONG_SIGN) or
  778. (id->type == TYPE_LONG_UNSIGN)))
  779. {
  780. if (id->longId().h() == regi)
  781. return (id->longId().l());
  782. else if (id->longId().l() == regi)
  783. return (id->longId().h());
  784. }
  785. return rUNDEF; // Cristina: please check this!
  786. }
  787. QString Constant::walkCondExpr(Function *, int *) const
  788. {
  789. if (kte.kte < 1000)
  790. return QString::number(kte.kte);
  791. else
  792. return "0x" + QString::number(kte.kte,16);
  793. }
  794. int Constant::hlTypeSize(Function *) const
  795. {
  796. return kte.size;
  797. }
  798. hlType Constant::expType(Function *pproc) const
  799. {
  800. return TYPE_CONST;
  801. }
  802. QString FuncNode::walkCondExpr(Function *pProc, int *numLoc) const
  803. {
  804. return pProc->writeCall(call.proc,*call.args, numLoc);
  805. }
  806. int FuncNode::hlTypeSize(Function *) const
  807. {
  808. return hlSize[call.proc->retVal.type];
  809. }
  810. hlType FuncNode::expType(Function *) const
  811. {
  812. return call.proc->retVal.type;
  813. }