ast.cpp 26 KB

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