ast.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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 *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 *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 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 (uint8_t 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->ic.ll.opcode,pIcode->ic.ll.opcode);
  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. printf("%s end: %x,%x\n",__FUNCTION__,du.def,du.use);
  85. }
  86. /* Creates a conditional boolean expression and returns it */
  87. COND_EXPR *COND_EXPR::boolOp(COND_EXPR *lhs, COND_EXPR *rhs, condOp op)
  88. {
  89. //printf("%s:%d\n",__FUNCTION__,int(op));
  90. COND_EXPR *newExp;
  91. newExp = new COND_EXPR(BOOLEAN_OP);
  92. newExp->expr.boolExpr.op = op;
  93. newExp->expr.boolExpr.lhs = lhs;
  94. newExp->expr.boolExpr.rhs = rhs;
  95. return (newExp);
  96. }
  97. /* Returns a unary conditional expression node. This procedure should
  98. * only be used with the following conditional node types: NEGATION,
  99. * ADDRESSOF, DEREFERENCE, POST_INC, POST_DEC, PRE_INC, PRE_DEC */
  100. COND_EXPR *COND_EXPR::unary(condNodeType t, COND_EXPR *sub_expr)
  101. {
  102. COND_EXPR *newExp;
  103. newExp = new COND_EXPR(t);
  104. newExp->expr.unaryExp = sub_expr;
  105. return (newExp);
  106. }
  107. /* Returns an identifier conditional expression node of type GLOB_VAR */
  108. COND_EXPR *COND_EXPR::idGlob (int16_t segValue, int16_t off)
  109. {
  110. COND_EXPR *newExp;
  111. uint32_t adr;
  112. size_t i;
  113. newExp = new COND_EXPR(IDENTIFIER);
  114. newExp->expr.ident.idType = GLOB_VAR;
  115. adr = opAdr(segValue, off);
  116. for (i = 0; i < symtab.size(); i++)
  117. if (symtab[i].label == adr)
  118. break;
  119. if (i == symtab.size())
  120. printf ("Error, glob var not found in symtab\n");
  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(uint8_t 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, int off)
  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->ic.ll.flg & I) == I)) /* constant */
  226. {
  227. iICODE atOffset=pIcode;
  228. advance(atOffset,off);
  229. newExp->expr.ident.idType = CONSTANT;
  230. if (f == HIGH_FIRST)
  231. newExp->expr.ident.idNode.kte.kte = (pIcode->ic.ll.src.op() << 16) +
  232. atOffset->ic.ll.src.op();
  233. else /* LOW_FIRST */
  234. newExp->expr.ident.idNode.kte.kte =
  235. (atOffset->ic.ll.src.op() << 16)+ pIcode->ic.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, off);
  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: i : index into the icode array, used for newLongRegId only.
  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 ICODE &pIcode, 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) ? pIcode.ic.ll.src : pIcode.ic.ll.dst);
  301. if ( ((sd == DST) && pIcode.ic.ll.anyFlagSet(IM_DST)) or
  302. ((sd == SRC) && pIcode.ic.ll.anyFlagSet(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) && pIcode.ic.ll.anyFlagSet(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) && pIcode.ic.ll.anyFlagSet(I)) /* constant */
  316. newExp = COND_EXPR::idKte (pIcode.ic.ll.src.op(), 2);
  317. else if (pm.regi == 0) /* global variable */
  318. newExp = COND_EXPR::idGlob(pm.segValue, pm.off);
  319. else if (pm.regi < INDEXBASE) /* register */
  320. {
  321. newExp = COND_EXPR::idReg (pm.regi, (sd == SRC) ? pIcode.ic.ll.flg :
  322. pIcode.ic.ll.flg & NO_SRC_B, &pProc->localId);
  323. duIcode.setRegDU( pm.regi, du);
  324. }
  325. else if (pm.off) /* offset */
  326. {
  327. if ((pm.seg == rSS) && (pm.regi == INDEXBASE + 6)) /* idx on bp */
  328. {
  329. if (pm.off >= 0) /* argument */
  330. newExp = COND_EXPR::idParam (pm.off, &pProc->args);
  331. else /* local variable */
  332. newExp = COND_EXPR::idLoc (pm.off, &pProc->localId);
  333. }
  334. else if ((pm.seg == rDS) && (pm.regi == INDEXBASE + 7)) /* bx */
  335. {
  336. if (pm.off > 0) /* global variable */
  337. newExp = idCondExpIdxGlob (pm.segValue, pm.off, rBX,&pProc->localId);
  338. else
  339. newExp = COND_EXPR::idOther (pm.seg, pm.regi, pm.off);
  340. duIcode.setRegDU( rBX, eUSE);
  341. }
  342. else /* idx <> bp, bx */
  343. newExp = COND_EXPR::idOther (pm.seg, pm.regi, pm.off);
  344. /**** check long ops, indexed global var *****/
  345. }
  346. else /* (pm->regi >= INDEXBASE && pm->off = 0) => indexed && no off */
  347. {
  348. if ((pm.seg == rDS) && (pm.regi > INDEXBASE + 3)) /* dereference */
  349. {
  350. switch (pm.regi) {
  351. case INDEXBASE + 4: newExp = COND_EXPR::idReg(rSI, 0, &pProc->localId);
  352. duIcode.setRegDU( rSI, du);
  353. break;
  354. case INDEXBASE + 5: newExp = COND_EXPR::idReg(rDI, 0, &pProc->localId);
  355. duIcode.setRegDU( rDI, du);
  356. break;
  357. case INDEXBASE + 6: newExp = COND_EXPR::idReg(rBP, 0, &pProc->localId);
  358. break;
  359. case INDEXBASE + 7: 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) ? ic.ll.src : ic.ll.dst);
  377. if ((sd == SRC) && ((ic.ll.flg & I) == I))
  378. return (CONSTANT);
  379. else if (pm.regi == 0)
  380. return (GLOB_VAR);
  381. else if (pm.regi < INDEXBASE)
  382. return (REGISTER);
  383. else if ((pm.seg == rSS) && (pm.regi == INDEXBASE))
  384. {
  385. if (pm.off >= 0)
  386. return (PARAM);
  387. else
  388. return (LOCAL_VAR);
  389. }
  390. else
  391. return (OTHER);
  392. }
  393. /* Size of hl types */
  394. int hlSize[] = {2, 1, 1, 2, 2, 4, 4, 4, 2, 2, 1, 4, 4};
  395. /* Returns the type of the expression */
  396. int hlTypeSize (const COND_EXPR *expr, Function * pproc)
  397. {
  398. int first, second;
  399. if (expr == NULL)
  400. return (2); /* for TYPE_UNKNOWN */
  401. switch (expr->type) {
  402. case BOOLEAN_OP:
  403. first = hlTypeSize (expr->expr.boolExpr.lhs, pproc);
  404. second = hlTypeSize (expr->expr.boolExpr.rhs, pproc);
  405. if (first > second)
  406. return (first);
  407. else
  408. return (second);
  409. case NEGATION: case ADDRESSOF:
  410. case POST_INC: case POST_DEC:
  411. case PRE_INC: case PRE_DEC:
  412. case DEREFERENCE: return (hlTypeSize (expr->expr.unaryExp, pproc));
  413. case IDENTIFIER:
  414. switch (expr->expr.ident.idType)
  415. {
  416. case GLOB_VAR:
  417. return (symtab[expr->expr.ident.idNode.globIdx].size);
  418. case REGISTER:
  419. if (expr->expr.ident.regiType == BYTE_REG)
  420. return (1);
  421. else
  422. return (2);
  423. case LOCAL_VAR:
  424. return (hlSize[pproc->localId.id_arr[expr->expr.ident.idNode.localIdx].type]);
  425. case PARAM:
  426. return (hlSize[pproc->args.sym[expr->expr.ident.idNode.paramIdx].type]);
  427. case GLOB_VAR_IDX:
  428. return (hlSize[pproc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].type]);
  429. case CONSTANT:
  430. return (expr->expr.ident.idNode.kte.size);
  431. case STRING:
  432. return (2);
  433. case LONG_VAR:
  434. return (4);
  435. case FUNCTION:
  436. return (hlSize[expr->expr.ident.idNode.call.proc->retVal.type]);
  437. case OTHER:
  438. return (2);
  439. } /* eos */
  440. break;
  441. }
  442. return 2; // CC: is this correct?
  443. }
  444. /* Returns the type of the expression */
  445. hlType expType (const COND_EXPR *expr, Function * pproc)
  446. {
  447. hlType first, second;
  448. if (expr == NULL)
  449. return (TYPE_UNKNOWN);
  450. switch (expr->type)
  451. {
  452. case BOOLEAN_OP:
  453. first = expType (expr->expr.boolExpr.lhs, pproc);
  454. second = expType (expr->expr.boolExpr.rhs, pproc);
  455. if (first != second)
  456. {
  457. if (hlTypeSize (expr->expr.boolExpr.lhs, pproc) >
  458. hlTypeSize (expr->expr.boolExpr.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 (uint8_t regi, LOCAL_ID *locId, COND_EXPR *tree)
  508. {
  509. IDENTTYPE* ident; /* ptr to an identifier */
  510. uint8_t 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->expr.boolExpr.lhs, pProc, numLoc);
  561. outStr << condOpSym[expr->expr.boolExpr.op];
  562. outStr << walkCondExpr(expr->expr.boolExpr.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. if (id->id.regi < rAL)
  624. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,wordReg[id->id.regi - rAX]);
  625. else
  626. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,byteReg[id->id.regi - rAL]);
  627. }
  628. if (id->hasMacro)
  629. o << id->macro << "("<<id->name<<")";
  630. else
  631. o << id->name;
  632. break;
  633. case LOCAL_VAR:
  634. o << pProc->localId.id_arr[expr->expr.ident.idNode.localIdx].name;
  635. break;
  636. case PARAM:
  637. psym = &pProc->args.sym[expr->expr.ident.idNode.paramIdx];
  638. if (psym->hasMacro)
  639. o << psym->macro<<"("<<psym->name<< ")";
  640. else
  641. o << psym->name;
  642. break;
  643. case GLOB_VAR_IDX:
  644. bwGlb = &pProc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].id.bwGlb;
  645. o << (bwGlb->seg << 4) + bwGlb->off << "["<<wordReg[bwGlb->regi - rAX]<<"]";
  646. break;
  647. case CONSTANT:
  648. if (expr->expr.ident.idNode.kte.kte < 1000)
  649. o << expr->expr.ident.idNode.kte.kte;
  650. else
  651. o << "0x"<<std::hex << expr->expr.ident.idNode.kte.kte;
  652. break;
  653. case STRING:
  654. o << getString (expr->expr.ident.idNode.strIdx);
  655. break;
  656. case LONG_VAR:
  657. id = &pProc->localId.id_arr[expr->expr.ident.idNode.longIdx];
  658. if (id->name[0] != '\0') /* STK_FRAME & REG w/name*/
  659. o << id->name;
  660. else if (id->loc == REG_FRAME)
  661. {
  662. sprintf (id->name, "loc%ld", ++(*numLoc));
  663. cCode.appendDecl("%s %s; /* %s:%s */\n",hlTypes[id->type], id->name,wordReg[id->id.longId.h - rAX],wordReg[id->id.longId.l - rAX]);
  664. o << id->name;
  665. pProc->localId.propLongId (id->id.longId.l,id->id.longId.h, id->name);
  666. }
  667. else /* GLB_FRAME */
  668. {
  669. if (id->id.longGlb.regi == 0) /* not indexed */
  670. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"]";
  671. else if (id->id.longGlb.regi == rBX)
  672. o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"][bx]";
  673. }
  674. break;
  675. case FUNCTION:
  676. o << writeCall (expr->expr.ident.idNode.call.proc,expr->expr.ident.idNode.call.args, pProc, numLoc);
  677. break;
  678. case OTHER:
  679. off = expr->expr.ident.idNode.other.off;
  680. o << wordReg[expr->expr.ident.idNode.other.seg - rAX]<< "[";
  681. o << idxReg[expr->expr.ident.idNode.other.regi - INDEXBASE];
  682. if (off < 0)
  683. o << "-"<< hexStr (-off);
  684. else if (off>0)
  685. o << "+"<< hexStr (off);
  686. o << "]";
  687. } /* eos */
  688. outStr << o.str();
  689. break;
  690. }
  691. return outStr.str();
  692. }
  693. /* Makes a copy of the given expression. Allocates newExp storage for each
  694. * node. Returns the copy. */
  695. COND_EXPR *COND_EXPR::clone()
  696. {
  697. COND_EXPR* newExp=0; /* Expression node copy */
  698. switch (type)
  699. {
  700. case BOOLEAN_OP:
  701. newExp = new COND_EXPR(*this);
  702. newExp->expr.boolExpr.lhs = expr.boolExpr.lhs->clone();
  703. newExp->expr.boolExpr.rhs = expr.boolExpr.rhs->clone();
  704. break;
  705. case NEGATION:
  706. case ADDRESSOF:
  707. case DEREFERENCE:
  708. newExp = new COND_EXPR(*this);
  709. newExp->expr.unaryExp = expr.unaryExp->clone();
  710. break;
  711. case IDENTIFIER:
  712. return new COND_EXPR(*this);
  713. }
  714. return (newExp);
  715. }
  716. /* Changes the boolean conditional operator at the root of this expression */
  717. void COND_EXPR::changeBoolOp (condOp newOp)
  718. {
  719. expr.boolExpr.op = newOp;
  720. }
  721. /* Inserts the expression exp into the tree at the location specified by the
  722. * register regi */
  723. bool insertSubTreeReg (COND_EXPR *expr, COND_EXPR **tree, uint8_t regi,LOCAL_ID *locsym)
  724. {
  725. uint8_t treeReg;
  726. if (*tree == NULL)
  727. return FALSE;
  728. switch ((*tree)->type) {
  729. case IDENTIFIER:
  730. if ((*tree)->expr.ident.idType == REGISTER)
  731. {
  732. treeReg = locsym->id_arr[(*tree)->expr.ident.idNode.regiIdx].id.regi;
  733. if (treeReg == regi) /* uint16_t reg */
  734. {
  735. *tree = expr;
  736. return TRUE;
  737. }
  738. else if ((regi >= rAX) && (regi <= rBX)) /* uint16_t/uint8_t reg */
  739. {
  740. if ((treeReg == (regi + rAL-1)) || (treeReg == (regi + rAH-1)))
  741. {
  742. *tree = expr;
  743. return TRUE;
  744. }
  745. }
  746. }
  747. return FALSE;
  748. case BOOLEAN_OP:
  749. if (insertSubTreeReg (expr, &(*tree)->expr.boolExpr.lhs, regi, locsym))
  750. return TRUE;
  751. if (insertSubTreeReg (expr, &(*tree)->expr.boolExpr.rhs, regi, locsym))
  752. return TRUE;
  753. return FALSE;
  754. case NEGATION:
  755. case ADDRESSOF:
  756. case DEREFERENCE:
  757. if (insertSubTreeReg(expr, &(*tree)->expr.unaryExp,regi, locsym))
  758. return TRUE;
  759. return FALSE;
  760. }
  761. return FALSE;
  762. }
  763. /* Inserts the expression exp into the tree at the location specified by the
  764. * long register index longIdx*/
  765. bool insertSubTreeLongReg(COND_EXPR *exp, COND_EXPR **tree, int longIdx)
  766. {
  767. switch ((*tree)->type)
  768. {
  769. case IDENTIFIER:
  770. if ((*tree)->expr.ident.idNode.longIdx == longIdx)
  771. {
  772. *tree = exp;
  773. return true;
  774. }
  775. return false;
  776. case BOOLEAN_OP:
  777. if (insertSubTreeLongReg (exp, &(*tree)->expr.boolExpr.lhs, longIdx))
  778. return true;
  779. if (insertSubTreeLongReg (exp, &(*tree)->expr.boolExpr.rhs, longIdx))
  780. return true;
  781. return false;
  782. case NEGATION:
  783. case ADDRESSOF:
  784. case DEREFERENCE:
  785. if (insertSubTreeLongReg (exp, &(*tree)->expr.unaryExp, longIdx))
  786. return true;
  787. return false;
  788. }
  789. return false;
  790. }
  791. /* Recursively deallocates the abstract syntax tree rooted at *exp */
  792. void COND_EXPR::release()
  793. {
  794. switch (type)
  795. {
  796. case BOOLEAN_OP:
  797. expr.boolExpr.lhs->release();
  798. expr.boolExpr.rhs->release();
  799. break;
  800. case NEGATION:
  801. case ADDRESSOF:
  802. case DEREFERENCE:
  803. expr.unaryExp->release();
  804. break;
  805. }
  806. delete (this);
  807. }