ast.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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;
  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. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,Machine_X86::regName(id->id.regi).c_str());
  624. }
  625. if (id->hasMacro)
  626. o << id->macro << "("<<id->name<<")";
  627. else
  628. o << id->name;
  629. break;
  630. case LOCAL_VAR:
  631. o << pProc->localId.id_arr[expr->expr.ident.idNode.localIdx].name;
  632. break;
  633. case PARAM:
  634. psym = &pProc->args.sym[expr->expr.ident.idNode.paramIdx];
  635. if (psym->hasMacro)
  636. o << psym->macro<<"("<<psym->name<< ")";
  637. else
  638. o << psym->name;
  639. break;
  640. case GLOB_VAR_IDX:
  641. bwGlb = &pProc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].id.bwGlb;
  642. o << (bwGlb->seg << 4) + bwGlb->off << "["<<Machine_X86::regName(bwGlb->regi)<<"]";
  643. break;
  644. case CONSTANT:
  645. if (expr->expr.ident.idNode.kte.kte < 1000)
  646. o << expr->expr.ident.idNode.kte.kte;
  647. else
  648. o << "0x"<<std::hex << expr->expr.ident.idNode.kte.kte;
  649. break;
  650. case STRING:
  651. o << getString (expr->expr.ident.idNode.strIdx);
  652. break;
  653. case LONG_VAR:
  654. id = &pProc->localId.id_arr[expr->expr.ident.idNode.longIdx];
  655. if (id->name[0] != '\0') /* STK_FRAME & REG w/name*/
  656. o << id->name;
  657. else if (id->loc == REG_FRAME)
  658. {
  659. sprintf (id->name, "loc%ld", ++(*numLoc));
  660. cCode.appendDecl("%s %s; /* %s:%s */\n",hlTypes[id->type], id->name,
  661. Machine_X86::regName(id->id.longId.h).c_str(),
  662. Machine_X86::regName(id->id.longId.l).c_str());
  663. o << id->name;
  664. pProc->localId.propLongId (id->id.longId.l,id->id.longId.h, id->name);
  665. }
  666. else /* GLB_FRAME */
  667. {
  668. if (id->id.longGlb.regi == 0) /* not indexed */
  669. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"]";
  670. else if (id->id.longGlb.regi == rBX)
  671. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"][bx]";
  672. }
  673. break;
  674. case FUNCTION:
  675. o << writeCall (expr->expr.ident.idNode.call.proc,expr->expr.ident.idNode.call.args, pProc, numLoc);
  676. break;
  677. case OTHER:
  678. off = expr->expr.ident.idNode.other.off;
  679. o << Machine_X86::regName(expr->expr.ident.idNode.other.seg)<< "[";
  680. o << Machine_X86::regName(expr->expr.ident.idNode.other.regi);
  681. if (off < 0)
  682. o << "-"<< hexStr (-off);
  683. else if (off>0)
  684. o << "+"<< hexStr (off);
  685. o << "]";
  686. } /* eos */
  687. outStr << o.str();
  688. break;
  689. }
  690. return outStr.str();
  691. }
  692. /* Makes a copy of the given expression. Allocates newExp storage for each
  693. * node. Returns the copy. */
  694. COND_EXPR *COND_EXPR::clone() const
  695. {
  696. COND_EXPR* newExp=0; /* Expression node copy */
  697. switch (type)
  698. {
  699. case BOOLEAN_OP:
  700. newExp = new COND_EXPR(*this);
  701. newExp->boolExpr.lhs = lhs()->clone();
  702. newExp->boolExpr.rhs = rhs()->clone();
  703. break;
  704. case NEGATION:
  705. case ADDRESSOF:
  706. case DEREFERENCE:
  707. newExp = new COND_EXPR(*this);
  708. newExp->expr.unaryExp = expr.unaryExp->clone();
  709. break;
  710. case IDENTIFIER:
  711. return new COND_EXPR(*this);
  712. }
  713. return (newExp);
  714. }
  715. /* Changes the boolean conditional operator at the root of this expression */
  716. void COND_EXPR::changeBoolOp (condOp newOp)
  717. {
  718. boolExpr.op = newOp;
  719. }
  720. /* Inserts the expression exp into the tree at the location specified by the
  721. * register regi */
  722. bool COND_EXPR::insertSubTreeReg (COND_EXPR *&tree, COND_EXPR *_expr, eReg regi,LOCAL_ID *locsym)
  723. {
  724. if (tree == NULL)
  725. return false;
  726. COND_EXPR *temp=tree->insertSubTreeReg(_expr,regi,locsym);
  727. if(nullptr!=temp)
  728. {
  729. tree=temp;
  730. return true;
  731. }
  732. return false;
  733. }
  734. bool isSubRegisterOf(eReg reg,eReg parent)
  735. {
  736. if ((parent < rAX) || (parent > rBX))
  737. return false; // only AX -> BX are coverede by subregisters
  738. return ((reg==subRegH(parent)) || (reg == subRegL(parent)));
  739. }
  740. COND_EXPR *COND_EXPR::insertSubTreeReg (COND_EXPR *_expr, eReg regi,LOCAL_ID *locsym)
  741. {
  742. eReg treeReg;
  743. COND_EXPR *temp;
  744. switch (type) {
  745. case IDENTIFIER:
  746. if (expr.ident.idType == REGISTER)
  747. {
  748. treeReg = locsym->id_arr[expr.ident.idNode.regiIdx].id.regi;
  749. if (treeReg == regi) /* uint16_t reg */
  750. {
  751. return _expr;
  752. }
  753. else if(isSubRegisterOf(treeReg,regi)) /* uint16_t/uint8_t reg */
  754. {
  755. return _expr;
  756. }
  757. }
  758. return false;
  759. case BOOLEAN_OP:
  760. temp = lhs()->insertSubTreeReg( _expr, regi, locsym);
  761. if (nullptr!=temp)
  762. {
  763. boolExpr.lhs = temp;
  764. return this;
  765. }
  766. temp = rhs()->insertSubTreeReg( _expr, regi, locsym);
  767. if (nullptr!=temp)
  768. {
  769. boolExpr.rhs = temp;
  770. return this;
  771. }
  772. return nullptr;
  773. case NEGATION:
  774. case ADDRESSOF:
  775. case DEREFERENCE:
  776. temp = expr.unaryExp->insertSubTreeReg( _expr, regi, locsym);
  777. if (nullptr!=temp)
  778. {
  779. expr.unaryExp = temp;
  780. return this;
  781. }
  782. return nullptr;
  783. }
  784. return nullptr;
  785. }
  786. COND_EXPR *BinaryOperator::insertSubTreeReg(COND_EXPR *_expr, eReg regi, LOCAL_ID *locsym)
  787. {
  788. COND_EXPR *r;
  789. r=m_lhs->insertSubTreeReg(_expr,regi,locsym);
  790. if(r)
  791. {
  792. m_lhs = r;
  793. return this;
  794. }
  795. r=m_rhs->insertSubTreeReg(_expr,regi,locsym);
  796. if(r)
  797. {
  798. m_rhs = r;
  799. return this;
  800. }
  801. return nullptr;
  802. }
  803. /* Inserts the expression exp into the tree at the location specified by the
  804. * long register index longIdx*/
  805. bool COND_EXPR::insertSubTreeLongReg(COND_EXPR *_expr, COND_EXPR **tree, int longIdx)
  806. {
  807. if (tree == NULL)
  808. return false;
  809. COND_EXPR *temp=(*tree)->insertSubTreeLongReg(_expr,longIdx);
  810. if(nullptr!=temp)
  811. {
  812. *tree=temp;
  813. return true;
  814. }
  815. return false;
  816. }
  817. COND_EXPR *COND_EXPR::insertSubTreeLongReg(COND_EXPR *_expr, int longIdx)
  818. {
  819. COND_EXPR *temp;
  820. switch (type)
  821. {
  822. case IDENTIFIER:
  823. if (expr.ident.idNode.longIdx == longIdx)
  824. {
  825. return _expr;
  826. }
  827. return nullptr;
  828. case BOOLEAN_OP:
  829. temp = lhs()->insertSubTreeLongReg( _expr,longIdx);
  830. if (nullptr!=temp)
  831. {
  832. boolExpr.lhs = temp;
  833. return this;
  834. }
  835. temp = rhs()->insertSubTreeLongReg( _expr,longIdx);
  836. if (nullptr!=temp)
  837. {
  838. boolExpr.rhs = temp;
  839. return this;
  840. }
  841. return nullptr;
  842. case NEGATION:
  843. case ADDRESSOF:
  844. case DEREFERENCE:
  845. temp = expr.unaryExp->insertSubTreeLongReg(_expr,longIdx);
  846. if (nullptr!=temp)
  847. {
  848. expr.unaryExp = temp;
  849. return this;
  850. }
  851. return nullptr;
  852. }
  853. return nullptr;
  854. }
  855. COND_EXPR *BinaryOperator::insertSubTreeLongReg(COND_EXPR *_expr, int longIdx)
  856. {
  857. COND_EXPR *r;
  858. r=m_lhs->insertSubTreeLongReg(_expr,longIdx);
  859. if(r)
  860. {
  861. m_lhs = r;
  862. return this;
  863. }
  864. r=m_rhs->insertSubTreeLongReg(_expr,longIdx);
  865. if(r)
  866. {
  867. m_rhs = r;
  868. return this;
  869. }
  870. return nullptr;
  871. }
  872. /* Recursively deallocates the abstract syntax tree rooted at *exp */
  873. void COND_EXPR::release()
  874. {
  875. switch (type)
  876. {
  877. case BOOLEAN_OP:
  878. lhs()->release();
  879. rhs()->release();
  880. break;
  881. case NEGATION:
  882. case ADDRESSOF:
  883. case DEREFERENCE:
  884. expr.unaryExp->release();
  885. break;
  886. }
  887. delete (this);
  888. }
  889. /* Makes a copy of the given expression. Allocates newExp storage for each
  890. * node. Returns the copy. */
  891. COND_EXPR *BinaryOperator::clone()
  892. {
  893. BinaryOperator* newExp=new BinaryOperator(m_op); /* Expression node copy */
  894. newExp->m_lhs = m_lhs->clone();
  895. newExp->m_rhs = m_rhs->clone();
  896. return newExp;
  897. }
  898. COND_EXPR *BinaryOperator::inverse()
  899. {
  900. static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL,
  901. LESS_EQUAL, LESS, DUMMY,DUMMY,DUMMY,DUMMY,
  902. DUMMY, DUMMY, DUMMY, DUMMY, DUMMY, DUMMY,
  903. DUMMY, DBL_OR, DBL_AND};
  904. BinaryOperator *res=reinterpret_cast<BinaryOperator *>(this->clone());
  905. switch (m_op)
  906. {
  907. case LESS_EQUAL: case LESS: case EQUAL:
  908. case NOT_EQUAL: case GREATER: case GREATER_EQUAL:
  909. res->m_op = invCondOp[m_op];
  910. return res;
  911. case AND: case OR: case XOR: case NOT: case ADD:
  912. case SUB: case MUL: case DIV: case SHR: case SHL: case MOD:
  913. return COND_EXPR::unary (NEGATION, res);
  914. case DBL_AND: case DBL_OR:
  915. res->m_op = invCondOp[m_op];
  916. res->m_lhs=m_lhs->inverse ();
  917. res->m_rhs=m_rhs->inverse ();
  918. return res;
  919. } /* eos */
  920. assert(false);
  921. return res;
  922. }