ast.cpp 32 KB

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