ast.cpp 31 KB

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