locident.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * File: locIdent.c
  3. * Purpose: support routines for high-level local identifier definitions.
  4. * Date: October 1993
  5. * (C) Cristina Cifuentes
  6. */
  7. #include <cstring>
  8. #include "locident.h"
  9. #include "dcc.h"
  10. ID::ID() : type(TYPE_UNKNOWN),illegal(false),loc(STK_FRAME),hasMacro(false)
  11. {
  12. name[0]=0;
  13. macro[0]=0;
  14. memset(&id,0,sizeof(id));
  15. }
  16. ID::ID(hlType t, frameType f) : type(t),illegal(false),hasMacro(false)
  17. {
  18. name[0]=0;
  19. macro[0]=0;
  20. memset(&id,0,sizeof(id));
  21. loc=f;
  22. }
  23. #define LOCAL_ID_DELTA 25
  24. #define IDX_ARRAY_DELTA 5
  25. /* Creates a new identifier node of type t and returns it.
  26. * Arguments: locSym : local long symbol table
  27. * t : type of LONG identifier
  28. * f : frame where this variable is located
  29. * ix : index into icode array where this var is used */
  30. void LOCAL_ID::newIdent(hlType t, frameType f)
  31. {
  32. ID newid(t,f);
  33. id_arr.push_back(newid);
  34. }
  35. /* Creates a new register identifier node of TYPE_BYTE_(UN)SIGN or
  36. * TYPE_WORD_(UN)SIGN type. Returns the index to this new entry. */
  37. int LOCAL_ID::newByteWordReg(hlType t, eReg regi)
  38. {
  39. int idx;
  40. /* Check for entry in the table */
  41. auto found=std::find_if(id_arr.begin(),id_arr.end(),[t,regi](ID &el)->bool {
  42. return ((el.type == t) && (el.id.regi == regi));
  43. });
  44. if(found!=id_arr.end())
  45. return found-id_arr.begin();
  46. /* Not in table, create new identifier */
  47. newIdent (t, REG_FRAME);
  48. idx = id_arr.size() - 1;
  49. id_arr[idx].id.regi = regi;
  50. return (idx);
  51. }
  52. /* Flags the entry associated with the offset off to illegal, as this
  53. * offset is part of a long stack variable.
  54. * Note: it is easier enough to remove this entry by moving the rest of
  55. * the array 1 position. The problem is that indexes into this
  56. * array have already been saved in several positions; therefore,
  57. * flagging this entry as illegal is all that can be done. */
  58. void LOCAL_ID::flagByteWordId (int off)
  59. {
  60. int idx;
  61. auto found=std::find_if(id_arr.begin(),id_arr.end(),[off](ID &en)->bool {
  62. //if (((en.type == TYPE_WORD_SIGN) || (en.type == TYPE_BYTE_SIGN)) &&
  63. if ((en.typeBitsize()<=16) &&
  64. (en.id.bwId.off == off) && (en.id.bwId.regOff == 0))
  65. return true;
  66. return false;
  67. });
  68. if(found==id_arr.end())
  69. {
  70. printf("No entry to flag as invalid in LOCAL_ID::flagByteWordId \n");
  71. return;
  72. }
  73. found->illegal = true;
  74. }
  75. /* Creates a new stack identifier node of TYPE_BYTE_(UN)SIGN or
  76. * TYPE_WORD_(UN)SIGN type. Returns the index to this new entry. */
  77. int LOCAL_ID::newByteWordStk(hlType t, int off, uint8_t regOff)
  78. {
  79. int idx;
  80. /* Check for entry in the table */
  81. auto found=std::find_if(id_arr.begin(),id_arr.end(),[off,regOff](ID &el)->bool {
  82. if ((el.id.bwId.off == off) && (el.id.bwId.regOff == regOff))
  83. return true;
  84. return false;
  85. });
  86. if(found!=id_arr.end())
  87. return found-id_arr.begin(); //return Index to found element
  88. /* Not in table, create new identifier */
  89. newIdent (t, STK_FRAME);
  90. idx = id_arr.size() - 1;
  91. id_arr[idx].id.bwId.regOff = regOff;
  92. id_arr[idx].id.bwId.off = off;
  93. return (idx);
  94. }
  95. /* Checks if the entry exists in the locSym, if so, returns the idx to this
  96. * entry; otherwise creates a new global identifier node of type
  97. * TYPE_WORD_(UN)SIGN and returns the index to this new entry.
  98. * Arguments: locSym : ptr to the local symbol table
  99. * seg: segment value for global variable
  100. * off: offset from segment
  101. * regi: indexed register into global variable
  102. * ix: index into icode array
  103. * t: HIGH_LEVEL type */
  104. int LOCAL_ID::newIntIdx(int16_t seg, int16_t off, eReg regi, int ix, hlType t)
  105. {
  106. int idx;
  107. /* Check for entry in the table */
  108. for (idx = 0; idx < id_arr.size(); idx++)
  109. {
  110. if (/*(locSym->id[idx].type == t) && Not checking type */
  111. (id_arr[idx].id.bwGlb.seg == seg) &&
  112. (id_arr[idx].id.bwGlb.off == off) &&
  113. (id_arr[idx].id.bwGlb.regi == regi))
  114. return (idx);
  115. }
  116. /* Not in the table, create new identifier */
  117. newIdent (t, GLB_FRAME);
  118. idx = id_arr.size() - 1;
  119. id_arr[idx].id.bwGlb.seg = seg;
  120. id_arr[idx].id.bwGlb.off = off;
  121. id_arr[idx].id.bwGlb.regi = regi;
  122. return (idx);
  123. }
  124. /* Checks if the entry exists in the locSym, if so, returns the idx to this
  125. * entry; otherwise creates a new register identifier node of type
  126. * TYPE_LONG_(UN)SIGN and returns the index to this new entry. */
  127. int LOCAL_ID::newLongReg(hlType t, eReg regH, eReg regL, iICODE ix_)
  128. {
  129. size_t idx;
  130. //iICODE ix_;
  131. /* Check for entry in the table */
  132. for (idx = 0; idx < id_arr.size(); idx++)
  133. {
  134. ID &entry(id_arr[idx]);
  135. if (/*(locSym->id[idx].type == t) && Not checking type */
  136. (entry.id.longId.h == regH) &&
  137. (entry.id.longId.l == regL))
  138. {
  139. /* Check for occurrence in the list */
  140. if (entry.idx.inList(ix_))
  141. return (idx);
  142. else
  143. {
  144. /* Insert icode index in list */
  145. entry.idx.push_back(ix_);
  146. //entry.idx.insert(ix_);
  147. return (idx);
  148. }
  149. }
  150. }
  151. /* Not in the table, create new identifier */
  152. newIdent (t, REG_FRAME);
  153. id_arr[id_arr.size()-1].idx.push_back(ix_);//insert(ix_);
  154. idx = id_arr.size() - 1;
  155. id_arr[idx].id.longId.h = regH;
  156. id_arr[idx].id.longId.l = regL;
  157. return (idx);
  158. }
  159. /* Returns an identifier conditional expression node of type TYPE_LONG or
  160. * TYPE_WORD_SIGN */
  161. COND_EXPR * LOCAL_ID::createId(const ID *retVal, iICODE ix_)
  162. {
  163. return COND_EXPR::idID(retVal,this,ix_);
  164. }
  165. /* Checks if the entry exists in the locSym, if so, returns the idx to this
  166. * entry; otherwise creates a new global identifier node of type
  167. * TYPE_LONG_(UN)SIGN and returns the index to this new entry. */
  168. int LOCAL_ID::newLongGlb(int16_t seg, int16_t offH, int16_t offL,hlType t)
  169. {
  170. size_t idx;
  171. /* Check for entry in the table */
  172. for (idx = 0; idx < id_arr.size(); idx++)
  173. {
  174. if (/*(locSym->id[idx].type == t) && Not checking type */
  175. (id_arr[idx].id.longGlb.seg == seg) &&
  176. (id_arr[idx].id.longGlb.offH == offH) &&
  177. (id_arr[idx].id.longGlb.offL == offL))
  178. return (idx);
  179. }
  180. /* Not in the table, create new identifier */
  181. newIdent (t, GLB_FRAME);
  182. idx = id_arr.size() - 1;
  183. id_arr[idx].id.longGlb.seg = seg;
  184. id_arr[idx].id.longGlb.offH = offH;
  185. id_arr[idx].id.longGlb.offL = offL;
  186. return (idx);
  187. }
  188. /* Checks if the entry exists in the locSym, if so, returns the idx to this
  189. * entry; otherwise creates a new global identifier node of type
  190. * TYPE_LONG_(UN)SIGN and returns the index to this new entry. */
  191. int LOCAL_ID::newLongIdx( int16_t seg, int16_t offH, int16_t offL,uint8_t regi, hlType t)
  192. {
  193. size_t idx;
  194. /* Check for entry in the table */
  195. for (idx = 0; idx < id_arr.size(); idx++)
  196. {
  197. if (/*(locSym->id[idx].type == t) && Not checking type */
  198. (id_arr[idx].id.longGlb.seg == seg) &&
  199. (id_arr[idx].id.longGlb.offH == offH) &&
  200. (id_arr[idx].id.longGlb.offL == offL) &&
  201. (id_arr[idx].id.longGlb.regi == regi))
  202. return (idx);
  203. }
  204. /* Not in the table, create new identifier */
  205. newIdent (t, GLB_FRAME);
  206. idx = id_arr.size() - 1;
  207. id_arr[idx].id.longGlb.seg = seg;
  208. id_arr[idx].id.longGlb.offH = offH;
  209. id_arr[idx].id.longGlb.offL = offL;
  210. id_arr[idx].id.longGlb.regi = regi;
  211. return (idx);
  212. }
  213. /* Creates a new stack identifier node of type TYPE_LONG_(UN)SIGN.
  214. * Returns the index to this entry. */
  215. int LOCAL_ID::newLongStk(hlType t, int offH, int offL)
  216. {
  217. size_t idx;
  218. /* Check for entry in the table */
  219. for (idx = 0; idx < id_arr.size(); idx++)
  220. {
  221. if ((id_arr[idx].type == t) &&
  222. (id_arr[idx].id.longStkId.offH == offH) &&
  223. (id_arr[idx].id.longStkId.offL == offL))
  224. return (idx);
  225. }
  226. /* Not in the table; flag as invalid offH and offL */
  227. flagByteWordId (offH);
  228. flagByteWordId (offL);
  229. /* Create new identifier */
  230. newIdent (t, STK_FRAME);
  231. idx = id_arr.size() - 1;
  232. id_arr[idx].id.longStkId.offH = offH;
  233. id_arr[idx].id.longStkId.offL = offL;
  234. return (idx);
  235. }
  236. /* Returns the index to an appropriate long identifier.
  237. * Note: long constants should be checked first and stored as a long integer
  238. * number in an expression record. */
  239. int LOCAL_ID::newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix,operDu du, iICODE atOffset)
  240. {
  241. size_t idx;
  242. LLOperand *pmH, *pmL;
  243. // iICODE atOffset(pIcode);
  244. // advance(atOffset,off);
  245. if (f == LOW_FIRST)
  246. {
  247. pmL = (sd == SRC) ? &pIcode->ll()->src : &pIcode->ll()->dst;
  248. pmH = (sd == SRC) ? &atOffset->ll()->src : &atOffset->ll()->dst;
  249. }
  250. else /* HIGH_FIRST */
  251. {
  252. pmH = (sd == SRC) ? &pIcode->ll()->src : &pIcode->ll()->dst;
  253. pmL = (sd == SRC) ? &atOffset->ll()->src : &atOffset->ll()->dst;
  254. }
  255. if (pmL->regi == 0) /* global variable */
  256. idx = newLongGlb(pmH->segValue, pmH->off, pmL->off, TYPE_LONG_SIGN);
  257. else if (pmL->regi < INDEX_BX_SI) /* register */
  258. {
  259. idx = newLongReg(TYPE_LONG_SIGN, pmH->regi, pmL->regi, ix);
  260. if (f == HIGH_FIRST)
  261. pIcode->setRegDU( pmL->regi, du); /* low part */
  262. else
  263. pIcode->setRegDU( pmH->regi, du); /* high part */
  264. }
  265. else if (pmL->off) { /* offset */
  266. if ((pmL->seg == rSS) && (pmL->regi == INDEX_BP)) /* idx on bp */
  267. idx = newLongStk(TYPE_LONG_SIGN, pmH->off, pmL->off);
  268. else if ((pmL->seg == rDS) && (pmL->regi == INDEX_BX)) /* bx */
  269. { /* glb var indexed on bx */
  270. printf("Bx indexed global, BX is an unused parameter to newLongIdx\n");
  271. idx = newLongIdx(pmH->segValue, pmH->off, pmL->off,rBX,TYPE_LONG_SIGN);
  272. pIcode->setRegDU( rBX, eUSE);
  273. }
  274. else /* idx <> bp, bx */
  275. printf ("long not supported, idx <> bp\n");
  276. }
  277. else /* (pm->regi >= INDEXBASE && pm->off = 0) => indexed && no off */
  278. printf ("long not supported, idx && no off\n");
  279. return (idx);
  280. }
  281. /* Checks whether the long stack identifier is equivalent to the source or
  282. * destination operands of pIcode and pIcode+1 (ie. these are LOW_LEVEL
  283. * icodes at present). If so, returns the rhs and lhs of this instruction.
  284. * Arguments: longId : long stack identifier
  285. * pIcode : ptr to first LOW_LEVEL icode instruction
  286. * i : idx into local identifier table for longId
  287. * idx : idx into icode array
  288. * pProc : ptr to current procedure record
  289. * rhs, lhs : return expressions if successful. */
  290. boolT checkLongEq (LONG_STKID_TYPE longId, iICODE pIcode, int i, Function * pProc, Assignment &asgn, iICODE atOffset)
  291. {
  292. LLOperand *pmHdst, *pmLdst, *pmHsrc, *pmLsrc; /* pointers to LOW_LEVEL icodes */
  293. pmHdst = &pIcode->ll()->dst;
  294. pmLdst = &atOffset->ll()->dst;
  295. pmHsrc = &pIcode->ll()->src;
  296. pmLsrc = &atOffset->ll()->src;
  297. if ((longId.offH == pmHdst->off) && (longId.offL == pmLdst->off))
  298. {
  299. asgn.lhs = COND_EXPR::idLongIdx (i);
  300. if ( not pIcode->ll()->testFlags(NO_SRC) )
  301. {
  302. asgn.rhs = COND_EXPR::idLong (&pProc->localId, SRC, pIcode, HIGH_FIRST, pIcode, eUSE, atOffset);
  303. }
  304. return true;
  305. }
  306. else if ((longId.offH == pmHsrc->off) && (longId.offL == pmLsrc->off))
  307. {
  308. asgn.lhs = COND_EXPR::idLong (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode,eDEF, atOffset);
  309. asgn.rhs = COND_EXPR::idLongIdx (i);
  310. return true;
  311. }
  312. return false;
  313. }
  314. /* Checks whether the long stack identifier is equivalent to the source or
  315. * destination operands of pIcode and pIcode+1 (ie. these are LOW_LEVEL
  316. * icodes at present). If so, returns the rhs and lhs of this instruction.
  317. * Arguments: longId : long stack identifier
  318. * pIcode : ptr to first LOW_LEVEL icode instruction
  319. * i : idx into local identifier table for longId
  320. * idx : idx into icode array
  321. * pProc : ptr to current procedure record
  322. * rhs, lhs : return expressions if successful. */
  323. boolT checkLongRegEq (LONGID_TYPE longId, iICODE pIcode, int i,
  324. Function * pProc, Assignment &asgn, iICODE atOffset)
  325. {
  326. LLOperand *pmHdst, *pmLdst, *pmHsrc, *pmLsrc; /* pointers to LOW_LEVEL icodes */
  327. // iICODE atOffset(pIcode);
  328. // advance(atOffset,off);
  329. pmHdst = &pIcode->ll()->dst;
  330. pmLdst = &atOffset->ll()->dst;
  331. pmHsrc = &pIcode->ll()->src;
  332. pmLsrc = &atOffset->ll()->src;
  333. if ((longId.h == pmHdst->regi) && (longId.l == pmLdst->regi))
  334. {
  335. asgn.lhs = COND_EXPR::idLongIdx (i);
  336. if ( not pIcode->ll()->testFlags(NO_SRC) )
  337. {
  338. asgn.rhs = COND_EXPR::idLong (&pProc->localId, SRC, pIcode, HIGH_FIRST, pIcode, eUSE, atOffset);
  339. }
  340. return true;
  341. }
  342. else if ((longId.h == pmHsrc->regi) && (longId.l == pmLsrc->regi))
  343. {
  344. asgn.lhs = COND_EXPR::idLong (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode, eDEF, atOffset);
  345. asgn.rhs = COND_EXPR::idLongIdx (i);
  346. return true;
  347. }
  348. return false;
  349. }
  350. /* Given an index into the local identifier table for a long register
  351. * variable, determines whether regi is the high or low part, and returns
  352. * the other part */
  353. eReg otherLongRegi (eReg regi, int idx, LOCAL_ID *locTbl)
  354. {
  355. ID *id;
  356. id = &locTbl->id_arr[idx];
  357. if ((id->loc == REG_FRAME) && ((id->type == TYPE_LONG_SIGN) ||
  358. (id->type == TYPE_LONG_UNSIGN)))
  359. {
  360. if (id->id.longId.h == regi)
  361. return (id->id.longId.l);
  362. else if (id->id.longId.l == regi)
  363. return (id->id.longId.h);
  364. }
  365. return rUNDEF; // Cristina: please check this!
  366. }
  367. /* Checks if the registers regL and regH have been used independently in
  368. * the local identifier table. If so, macros for these registers are
  369. * placed in the local identifier table, as these registers belong to a
  370. * long register identifier. */
  371. void LOCAL_ID::propLongId (uint8_t regL, uint8_t regH, const char *name)
  372. {
  373. for (ID &rid : id_arr)
  374. {
  375. if (rid.typeBitsize()!=16)
  376. continue;
  377. if ( (rid.id.regi != regL) and (rid.id.regi != regH) )
  378. continue;
  379. // otherwise at least 1 is ok
  380. rid.name = name;
  381. rid.hasMacro = true;
  382. rid.illegal = true;
  383. if (rid.id.regi == regL)
  384. {
  385. strcpy (rid.macro, "LO");
  386. }
  387. else // if (rid.id.regi == regH)
  388. {
  389. strcpy (rid.macro, "HI");
  390. }
  391. }
  392. }