ast.cpp 31 KB

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