ast.cpp 30 KB

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