locident.cpp 15 KB

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