ast.cpp 31 KB

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