locident.cpp 15 KB

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