ast.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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 (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->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. return 0;
  121. }
  122. newExp->expr.ident.idNode.globIdx = i;
  123. return (newExp);
  124. }
  125. /* Returns an identifier conditional expression node of type REGISTER */
  126. COND_EXPR *COND_EXPR::idReg(uint8_t regi, uint32_t icodeFlg, LOCAL_ID *locsym)
  127. {
  128. COND_EXPR *newExp;
  129. newExp = new COND_EXPR(IDENTIFIER);
  130. newExp->expr.ident.idType = REGISTER;
  131. if ((icodeFlg & B) || (icodeFlg & SRC_B))
  132. {
  133. newExp->expr.ident.idNode.regiIdx = locsym->newByteWordReg(TYPE_BYTE_SIGN, regi);
  134. newExp->expr.ident.regiType = BYTE_REG;
  135. }
  136. else /* uint16_t */
  137. {
  138. newExp->expr.ident.idNode.regiIdx = locsym->newByteWordReg( TYPE_WORD_SIGN, regi);
  139. newExp->expr.ident.regiType = WORD_REG;
  140. }
  141. return (newExp);
  142. }
  143. /* Returns an identifier conditional expression node of type REGISTER */
  144. COND_EXPR *COND_EXPR::idRegIdx(int idx, regType reg_type)
  145. {
  146. COND_EXPR *newExp;
  147. newExp = new COND_EXPR(IDENTIFIER);
  148. newExp->expr.ident.idType = REGISTER;
  149. newExp->expr.ident.regiType = reg_type;
  150. newExp->expr.ident.idNode.regiIdx = idx;
  151. return (newExp);
  152. }
  153. /* Returns an identifier conditional expression node of type LOCAL_VAR */
  154. COND_EXPR *COND_EXPR::idLoc(int off, LOCAL_ID *localId)
  155. {
  156. COND_EXPR *newExp;
  157. size_t i;
  158. newExp = new COND_EXPR(IDENTIFIER);
  159. newExp->expr.ident.idType = LOCAL_VAR;
  160. for (i = 0; i < localId->csym(); i++)
  161. if ((localId->id_arr[i].id.bwId.off == off) &&
  162. (localId->id_arr[i].id.bwId.regOff == 0))
  163. break;
  164. if (i == localId->csym())
  165. printf ("Error, cannot find local var\n");
  166. newExp->expr.ident.idNode.localIdx = i;
  167. sprintf (localId->id_arr[i].name, "loc%ld", i);
  168. return (newExp);
  169. }
  170. /* Returns an identifier conditional expression node of type PARAM */
  171. COND_EXPR *COND_EXPR::idParam(int off, const STKFRAME * argSymtab)
  172. {
  173. COND_EXPR *newExp;
  174. size_t i;
  175. newExp = new COND_EXPR(IDENTIFIER);
  176. newExp->expr.ident.idType = PARAM;
  177. for (i = 0; i < argSymtab->sym.size(); i++)
  178. if (argSymtab->sym[i].off == off)
  179. break;
  180. if (i == argSymtab->sym.size()) printf ("Error, cannot find argument var\n");
  181. newExp->expr.ident.idNode.localIdx = i;
  182. return (newExp);
  183. }
  184. /* Returns an identifier conditional expression node of type GLOB_VAR_IDX.
  185. * This global variable is indexed by regi. */
  186. COND_EXPR *idCondExpIdxGlob (int16_t segValue, int16_t off, uint8_t regi, const LOCAL_ID *locSym)
  187. {
  188. COND_EXPR *newExp;
  189. size_t i;
  190. newExp = new COND_EXPR(IDENTIFIER);
  191. newExp->expr.ident.idType = GLOB_VAR_IDX;
  192. for (i = 0; i < locSym->csym(); i++)
  193. if ((locSym->id_arr[i].id.bwGlb.seg == segValue) &&
  194. (locSym->id_arr[i].id.bwGlb.off == off) &&
  195. (locSym->id_arr[i].id.bwGlb.regi == regi))
  196. break;
  197. if (i == locSym->csym())
  198. printf ("Error, indexed-glob var not found in local id table\n");
  199. newExp->expr.ident.idNode.idxGlbIdx = i;
  200. return (newExp);
  201. }
  202. /* Returns an identifier conditional expression node of type CONSTANT */
  203. COND_EXPR *COND_EXPR::idKte(uint32_t kte, uint8_t size)
  204. {
  205. COND_EXPR *newExp = new COND_EXPR(IDENTIFIER);
  206. newExp->expr.ident.idType = CONSTANT;
  207. newExp->expr.ident.idNode.kte.kte = kte;
  208. newExp->expr.ident.idNode.kte.size = size;
  209. return (newExp);
  210. }
  211. /* Returns an identifier conditional expression node of type LONG_VAR,
  212. * that points to the given index idx. */
  213. COND_EXPR *COND_EXPR::idLongIdx (int idx)
  214. {
  215. COND_EXPR *newExp = new COND_EXPR(IDENTIFIER);
  216. newExp->expr.ident.idType = LONG_VAR;
  217. newExp->expr.ident.idNode.longIdx = idx;
  218. return (newExp);
  219. }
  220. /* Returns an identifier conditional expression node of type LONG_VAR */
  221. COND_EXPR *COND_EXPR::idLong(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, iICODE atOffset)
  222. {
  223. int idx;
  224. COND_EXPR *newExp = new COND_EXPR(IDENTIFIER);
  225. /* Check for long constant and save it as a constant expression */
  226. if ((sd == SRC) && pIcode->ll()->testFlags(I)) /* constant */
  227. {
  228. newExp->expr.ident.idType = CONSTANT;
  229. if (f == HIGH_FIRST)
  230. newExp->expr.ident.idNode.kte.kte = (pIcode->ll()->src.op() << 16) +
  231. atOffset->ll()->src.op();
  232. else /* LOW_FIRST */
  233. newExp->expr.ident.idNode.kte.kte =
  234. (atOffset->ll()->src.op() << 16)+ pIcode->ll()->src.op();
  235. newExp->expr.ident.idNode.kte.size = 4;
  236. }
  237. /* Save it as a long expression (reg, stack or glob) */
  238. else
  239. {
  240. idx = localId->newLong(sd, pIcode, f, ix, du, atOffset);
  241. newExp->expr.ident.idType = LONG_VAR;
  242. newExp->expr.ident.idNode.longIdx = idx;
  243. }
  244. return (newExp);
  245. }
  246. /* Returns an identifier conditional expression node of type FUNCTION */
  247. COND_EXPR *COND_EXPR::idFunc(Function * pproc, STKFRAME * args)
  248. {
  249. COND_EXPR *newExp;
  250. newExp = new COND_EXPR(IDENTIFIER);
  251. newExp->expr.ident.idType = FUNCTION;
  252. newExp->expr.ident.idNode.call.proc = pproc;
  253. newExp->expr.ident.idNode.call.args = args;
  254. return (newExp);
  255. }
  256. /* Returns an identifier conditional expression node of type OTHER.
  257. * Temporary solution, should really be encoded as an indexed type (eg.
  258. * arrays). */
  259. COND_EXPR *COND_EXPR::idOther(uint8_t seg, uint8_t regi, int16_t off)
  260. {
  261. COND_EXPR *newExp;
  262. newExp = new COND_EXPR(IDENTIFIER);
  263. newExp->expr.ident.idType = OTHER;
  264. newExp->expr.ident.idNode.other.seg = seg;
  265. newExp->expr.ident.idNode.other.regi = regi;
  266. newExp->expr.ident.idNode.other.off = off;
  267. return (newExp);
  268. }
  269. /* Returns an identifier conditional expression node of type TYPE_LONG or
  270. * TYPE_WORD_SIGN */
  271. COND_EXPR *COND_EXPR::idID (const ID *retVal, LOCAL_ID *locsym, iICODE ix_)
  272. {
  273. COND_EXPR *newExp;
  274. int idx;
  275. newExp = new COND_EXPR(IDENTIFIER);
  276. if (retVal->type == TYPE_LONG_SIGN)
  277. {
  278. idx = locsym->newLongReg (TYPE_LONG_SIGN, retVal->id.longId.h,retVal->id.longId.l, ix_);
  279. newExp->expr.ident.idType = LONG_VAR;
  280. newExp->expr.ident.idNode.longIdx = idx;
  281. }
  282. else if (retVal->type == TYPE_WORD_SIGN)
  283. {
  284. newExp->expr.ident.idType = REGISTER;
  285. newExp->expr.ident.idNode.regiIdx = locsym->newByteWordReg(TYPE_WORD_SIGN, retVal->id.regi);
  286. newExp->expr.ident.regiType = WORD_REG;
  287. }
  288. return (newExp);
  289. }
  290. /* Returns an identifier conditional expression node, according to the given
  291. * type.
  292. * Arguments:
  293. * duIcode: icode instruction that needs the du set.
  294. * du: operand is defined or used in current instruction. */
  295. COND_EXPR *COND_EXPR::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICODE ix_,ICODE &duIcode, operDu du)
  296. {
  297. COND_EXPR *newExp;
  298. int idx; /* idx into pIcode->localId table */
  299. const LLOperand &pm((sd == SRC) ? ll_insn.src : ll_insn.dst);
  300. if ( ((sd == DST) && ll_insn.testFlags(IM_DST)) or
  301. ((sd == SRC) && ll_insn.testFlags(IM_SRC)) or
  302. (sd == LHS_OP)) /* for MUL lhs */
  303. { /* implicit dx:ax */
  304. idx = pProc->localId.newLongReg (TYPE_LONG_SIGN, rDX, rAX, ix_);
  305. newExp = COND_EXPR::idLongIdx (idx);
  306. duIcode.setRegDU (rDX, du);
  307. duIcode.setRegDU (rAX, du);
  308. }
  309. else if ((sd == DST) && ll_insn.testFlags(IM_TMP_DST))
  310. { /* implicit tmp */
  311. newExp = COND_EXPR::idReg (rTMP, 0, &pProc->localId);
  312. duIcode.setRegDU(rTMP, (operDu)eUSE);
  313. }
  314. else if ((sd == SRC) && ll_insn.testFlags(I)) /* constant */
  315. newExp = COND_EXPR::idKte (ll_insn.src.op(), 2);
  316. else if (pm.regi == 0) /* global variable */
  317. newExp = GlobalVariable::Create(pm.segValue, pm.off);
  318. else if (pm.regi < INDEXBASE) /* register */
  319. {
  320. newExp = COND_EXPR::idReg (pm.regi, (sd == SRC) ? ll_insn.getFlag() :
  321. ll_insn.getFlag() & NO_SRC_B,
  322. &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) ? 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.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->lhs(), pproc);
  404. second = hlTypeSize (expr->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->lhs(), pproc);
  454. second = expType (expr->rhs(), pproc);
  455. if (first != second)
  456. {
  457. if (hlTypeSize (expr->lhs(), pproc) > hlTypeSize (expr->rhs(), pproc))
  458. return (first);
  459. else
  460. return (second);
  461. }
  462. else
  463. return (first);
  464. case POST_INC: case POST_DEC:
  465. case PRE_INC: case PRE_DEC:
  466. case NEGATION:
  467. return (expType (expr->expr.unaryExp, pproc));
  468. case ADDRESSOF: return (TYPE_PTR); /***????****/
  469. case DEREFERENCE: return (TYPE_PTR);
  470. case IDENTIFIER:
  471. switch (expr->expr.ident.idType)
  472. {
  473. case GLOB_VAR:
  474. return (symtab[expr->expr.ident.idNode.globIdx].type);
  475. case REGISTER:
  476. if (expr->expr.ident.regiType == BYTE_REG)
  477. return (TYPE_BYTE_SIGN);
  478. else
  479. return (TYPE_WORD_SIGN);
  480. case LOCAL_VAR:
  481. return (pproc->localId.id_arr[expr->expr.ident.idNode.localIdx].type);
  482. case PARAM:
  483. return (pproc->args.sym[expr->expr.ident.idNode.paramIdx].type);
  484. case GLOB_VAR_IDX:
  485. return (pproc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].type);
  486. case CONSTANT:
  487. return (TYPE_CONST);
  488. case STRING:
  489. return (TYPE_STR);
  490. case LONG_VAR:
  491. return (pproc->localId.id_arr[expr->expr.ident.idNode.longIdx].type);
  492. case FUNCTION:
  493. return (expr->expr.ident.idNode.call.proc->retVal.type);
  494. case OTHER:
  495. return (TYPE_UNKNOWN);
  496. } /* eos */
  497. case UNKNOWN_OP:
  498. assert(false);
  499. return (TYPE_UNKNOWN);
  500. }
  501. return TYPE_UNKNOWN; // CC: Correct?
  502. }
  503. /* Removes the register from the tree. If the register was part of a long
  504. * register (eg. dx:ax), the node gets transformed into an integer register
  505. * node. */
  506. void HlTypeSupport::performLongRemoval (uint8_t regi, LOCAL_ID *locId, COND_EXPR *tree)
  507. {
  508. IDENTTYPE* ident; /* ptr to an identifier */
  509. uint8_t otherRegi; /* high or low part of long register */
  510. switch (tree->type) {
  511. case BOOLEAN_OP:
  512. break;
  513. case POST_INC: case POST_DEC:
  514. case PRE_INC: case PRE_DEC:
  515. case NEGATION: case ADDRESSOF:
  516. case DEREFERENCE:
  517. break;
  518. case IDENTIFIER:
  519. ident = &tree->expr.ident;
  520. if (ident->idType == LONG_VAR)
  521. {
  522. otherRegi = otherLongRegi (regi, ident->idNode.longIdx, locId);
  523. ident->idType = REGISTER;
  524. ident->regiType = WORD_REG;
  525. ident->idNode.regiIdx = locId->newByteWordReg(TYPE_WORD_SIGN,otherRegi);
  526. }
  527. break;
  528. }
  529. }
  530. /* Returns the string located in image, formatted in C format. */
  531. static std::string getString (int offset)
  532. {
  533. ostringstream o;
  534. int strLen, i;
  535. strLen = strSize (&prog.Image[offset], '\0');
  536. o << '"';
  537. for (i = 0; i < strLen; i++)
  538. o<<cChar(prog.Image[offset+i]);
  539. o << "\"\0";
  540. return (o.str());
  541. }
  542. /* Walks the conditional expression tree and returns the result on a string */
  543. string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc)
  544. {
  545. int16_t off; /* temporal - for OTHER */
  546. ID* id; /* Pointer to local identifier table */
  547. //char* o; /* Operand string pointer */
  548. bool needBracket; /* Determine whether parenthesis is needed */
  549. BWGLB_TYPE* bwGlb; /* Ptr to BWGLB_TYPE (global indexed var) */
  550. STKSYM * psym; /* Pointer to argument in the stack */
  551. std::ostringstream outStr;
  552. if (expr == NULL)
  553. return "";
  554. needBracket = true;
  555. switch (expr->type)
  556. {
  557. case BOOLEAN_OP:
  558. outStr << "(";
  559. outStr << walkCondExpr(expr->lhs(), pProc, numLoc);
  560. outStr << condOpSym[expr->op()];
  561. outStr << walkCondExpr(expr->rhs(), pProc, numLoc);
  562. outStr << ")";
  563. break;
  564. case NEGATION:
  565. if (expr->expr.unaryExp->type == IDENTIFIER)
  566. {
  567. needBracket = FALSE;
  568. outStr << "!";
  569. }
  570. else
  571. outStr << "! (";
  572. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  573. if (needBracket == TRUE)
  574. outStr << ")";
  575. break;
  576. case ADDRESSOF:
  577. if (expr->expr.unaryExp->type == IDENTIFIER)
  578. {
  579. needBracket = FALSE;
  580. outStr << "&";
  581. }
  582. else
  583. outStr << "&(";
  584. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  585. if (needBracket == TRUE)
  586. outStr << ")";
  587. break;
  588. case DEREFERENCE:
  589. outStr << "*";
  590. if (expr->expr.unaryExp->type == IDENTIFIER)
  591. needBracket = FALSE;
  592. else
  593. outStr << "(";
  594. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  595. if (needBracket == TRUE)
  596. outStr << ")";
  597. break;
  598. case POST_INC:
  599. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc) << "++";
  600. break;
  601. case POST_DEC:
  602. outStr << walkCondExpr (expr->expr.unaryExp, pProc, numLoc) << "--";
  603. break;
  604. case PRE_INC:
  605. outStr << "++"<< walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  606. break;
  607. case PRE_DEC:
  608. outStr << "--"<< walkCondExpr (expr->expr.unaryExp, pProc, numLoc);
  609. break;
  610. case IDENTIFIER:
  611. std::ostringstream o;
  612. switch (expr->expr.ident.idType)
  613. {
  614. case GLOB_VAR:
  615. o << symtab[expr->expr.ident.idNode.globIdx].name;
  616. break;
  617. case REGISTER:
  618. id = &pProc->localId.id_arr[expr->expr.ident.idNode.regiIdx];
  619. if (id->name[0] == '\0') /* no name */
  620. {
  621. sprintf (id->name, "loc%ld", ++(*numLoc));
  622. if (id->id.regi < rAL)
  623. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,wordReg[id->id.regi - rAX]);
  624. else
  625. cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,byteReg[id->id.regi - rAL]);
  626. }
  627. if (id->hasMacro)
  628. o << id->macro << "("<<id->name<<")";
  629. else
  630. o << id->name;
  631. break;
  632. case LOCAL_VAR:
  633. o << pProc->localId.id_arr[expr->expr.ident.idNode.localIdx].name;
  634. break;
  635. case PARAM:
  636. psym = &pProc->args.sym[expr->expr.ident.idNode.paramIdx];
  637. if (psym->hasMacro)
  638. o << psym->macro<<"("<<psym->name<< ")";
  639. else
  640. o << psym->name;
  641. break;
  642. case GLOB_VAR_IDX:
  643. bwGlb = &pProc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].id.bwGlb;
  644. o << (bwGlb->seg << 4) + bwGlb->off << "["<<wordReg[bwGlb->regi - rAX]<<"]";
  645. break;
  646. case CONSTANT:
  647. if (expr->expr.ident.idNode.kte.kte < 1000)
  648. o << expr->expr.ident.idNode.kte.kte;
  649. else
  650. o << "0x"<<std::hex << expr->expr.ident.idNode.kte.kte;
  651. break;
  652. case STRING:
  653. o << getString (expr->expr.ident.idNode.strIdx);
  654. break;
  655. case LONG_VAR:
  656. id = &pProc->localId.id_arr[expr->expr.ident.idNode.longIdx];
  657. if (id->name[0] != '\0') /* STK_FRAME & REG w/name*/
  658. o << id->name;
  659. else if (id->loc == REG_FRAME)
  660. {
  661. sprintf (id->name, "loc%ld", ++(*numLoc));
  662. cCode.appendDecl("%s %s; /* %s:%s */\n",hlTypes[id->type], id->name,wordReg[id->id.longId.h - rAX],wordReg[id->id.longId.l - rAX]);
  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 << wordReg[expr->expr.ident.idNode.other.seg - rAX]<< "[";
  680. o << idxReg[expr->expr.ident.idNode.other.regi - INDEXBASE];
  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()
  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, uint8_t 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. COND_EXPR *COND_EXPR::insertSubTreeReg (COND_EXPR *_expr, uint8_t regi,LOCAL_ID *locsym)
  735. {
  736. HlTypeSupport *set_val;
  737. uint8_t treeReg;
  738. COND_EXPR *temp;
  739. switch (type) {
  740. case IDENTIFIER:
  741. if (expr.ident.idType == REGISTER)
  742. {
  743. treeReg = locsym->id_arr[expr.ident.idNode.regiIdx].id.regi;
  744. if (treeReg == regi) /* uint16_t reg */
  745. {
  746. return _expr;
  747. }
  748. else if ((regi >= rAX) && (regi <= rBX)) /* uint16_t/uint8_t reg */
  749. {
  750. if ((treeReg == (regi + rAL-1)) || (treeReg == (regi + rAH-1)))
  751. {
  752. return _expr;
  753. }
  754. }
  755. }
  756. return FALSE;
  757. case BOOLEAN_OP:
  758. temp = lhs()->insertSubTreeReg( _expr, regi, locsym);
  759. if (nullptr!=temp)
  760. {
  761. boolExpr.lhs = temp;
  762. return this;
  763. }
  764. temp = rhs()->insertSubTreeReg( _expr, regi, locsym);
  765. if (nullptr!=temp)
  766. {
  767. boolExpr.rhs = temp;
  768. return this;
  769. }
  770. return nullptr;
  771. case NEGATION:
  772. case ADDRESSOF:
  773. case DEREFERENCE:
  774. temp = expr.unaryExp->insertSubTreeReg( _expr, regi, locsym);
  775. if (nullptr!=temp)
  776. {
  777. expr.unaryExp = temp;
  778. return this;
  779. }
  780. return nullptr;
  781. }
  782. return nullptr;
  783. }
  784. COND_EXPR *BinaryOperator::insertSubTreeReg(COND_EXPR *_expr, uint8_t regi, LOCAL_ID *locsym)
  785. {
  786. COND_EXPR *r;
  787. r=m_lhs->insertSubTreeReg(_expr,regi,locsym);
  788. if(r)
  789. {
  790. m_lhs = r;
  791. return this;
  792. }
  793. r=m_rhs->insertSubTreeReg(_expr,regi,locsym);
  794. if(r)
  795. {
  796. m_rhs = r;
  797. return this;
  798. }
  799. return nullptr;
  800. }
  801. /* Inserts the expression exp into the tree at the location specified by the
  802. * long register index longIdx*/
  803. bool COND_EXPR::insertSubTreeLongReg(COND_EXPR *_expr, COND_EXPR **tree, int longIdx)
  804. {
  805. if (tree == NULL)
  806. return false;
  807. COND_EXPR *temp=(*tree)->insertSubTreeLongReg(_expr,longIdx);
  808. if(nullptr!=temp)
  809. {
  810. *tree=temp;
  811. return true;
  812. }
  813. return false;
  814. }
  815. COND_EXPR *COND_EXPR::insertSubTreeLongReg(COND_EXPR *_expr, int longIdx)
  816. {
  817. COND_EXPR *temp;
  818. switch (type)
  819. {
  820. case IDENTIFIER:
  821. if (expr.ident.idNode.longIdx == longIdx)
  822. {
  823. return _expr;
  824. }
  825. return nullptr;
  826. case BOOLEAN_OP:
  827. temp = lhs()->insertSubTreeLongReg( _expr,longIdx);
  828. if (nullptr!=temp)
  829. {
  830. boolExpr.lhs = temp;
  831. return this;
  832. }
  833. temp = rhs()->insertSubTreeLongReg( _expr,longIdx);
  834. if (nullptr!=temp)
  835. {
  836. boolExpr.rhs = temp;
  837. return this;
  838. }
  839. return nullptr;
  840. case NEGATION:
  841. case ADDRESSOF:
  842. case DEREFERENCE:
  843. COND_EXPR *temp = expr.unaryExp->insertSubTreeLongReg(_expr,longIdx);
  844. if (nullptr!=temp)
  845. {
  846. expr.unaryExp = temp;
  847. return this;
  848. }
  849. return nullptr;
  850. }
  851. return nullptr;
  852. }
  853. COND_EXPR *BinaryOperator::insertSubTreeLongReg(COND_EXPR *_expr, int longIdx)
  854. {
  855. COND_EXPR *r;
  856. r=m_lhs->insertSubTreeLongReg(_expr,longIdx);
  857. if(r)
  858. {
  859. m_lhs = r;
  860. return this;
  861. }
  862. r=m_rhs->insertSubTreeLongReg(_expr,longIdx);
  863. if(r)
  864. {
  865. m_rhs = r;
  866. return this;
  867. }
  868. return nullptr;
  869. }
  870. /* Recursively deallocates the abstract syntax tree rooted at *exp */
  871. void COND_EXPR::release()
  872. {
  873. switch (type)
  874. {
  875. case BOOLEAN_OP:
  876. lhs()->release();
  877. rhs()->release();
  878. break;
  879. case NEGATION:
  880. case ADDRESSOF:
  881. case DEREFERENCE:
  882. expr.unaryExp->release();
  883. break;
  884. }
  885. delete (this);
  886. }
  887. //
  888. COND_EXPR *BinaryOperator::inverse()
  889. {
  890. static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL,
  891. LESS_EQUAL, LESS, DUMMY,DUMMY,DUMMY,DUMMY,
  892. DUMMY, DUMMY, DUMMY, DUMMY, DUMMY, DUMMY,
  893. DUMMY, DBL_OR, DBL_AND};
  894. BinaryOperator *res=0;
  895. switch (m_op)
  896. {
  897. case LESS_EQUAL: case LESS: case EQUAL:
  898. case NOT_EQUAL: case GREATER: case GREATER_EQUAL:
  899. res = static_cast<BinaryOperator *>(clone());
  900. res->m_op = invCondOp[m_op];
  901. return res;
  902. case AND: case OR: case XOR: case NOT: case ADD:
  903. case SUB: case MUL: case DIV: case SHR: case SHL: case MOD:
  904. return COND_EXPR::unary (NEGATION, clone());
  905. case DBL_AND: case DBL_OR:
  906. res = static_cast<BinaryOperator *>(clone());
  907. res->m_op = invCondOp[m_op];
  908. res->m_lhs=m_lhs->inverse ();
  909. res->m_rhs=m_rhs->inverse ();
  910. return res;
  911. } /* eos */
  912. assert(false);
  913. }
  914. /* Makes a copy of the given expression. Allocates newExp storage for each
  915. * node. Returns the copy. */
  916. COND_EXPR *BinaryOperator::clone()
  917. {
  918. BinaryOperator* newExp=new BinaryOperator(m_op); /* Expression node copy */
  919. newExp->m_lhs = m_lhs->clone();
  920. newExp->m_rhs = m_rhs->clone();
  921. return newExp;
  922. }