ast.cpp 26 KB

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