locident.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. return (idx);
  146. }
  147. }
  148. }
  149. /* Not in the table, create new identifier */
  150. newIdent (t, REG_FRAME);
  151. id_arr[id_arr.size()-1].idx.push_back(ix_);
  152. idx = id_arr.size() - 1;
  153. id_arr[idx].id.longId.h = regH;
  154. id_arr[idx].id.longId.l = regL;
  155. return (idx);
  156. }
  157. /* Returns an identifier conditional expression node of type TYPE_LONG or
  158. * TYPE_WORD_SIGN */
  159. AstIdent * LOCAL_ID::createId(const ID *retVal, iICODE ix_)
  160. {
  161. return AstIdent::idID(retVal,this,ix_);
  162. }
  163. /* Checks if the entry exists in the locSym, if so, returns the idx to this
  164. * entry; otherwise creates a new global identifier node of type
  165. * TYPE_LONG_(UN)SIGN and returns the index to this new entry. */
  166. int LOCAL_ID::newLongGlb(int16_t seg, int16_t offH, int16_t offL,hlType t)
  167. {
  168. size_t idx;
  169. /* Check for entry in the table */
  170. for (idx = 0; idx < id_arr.size(); idx++)
  171. {
  172. if (/*(locSym->id[idx].type == t) && Not checking type */
  173. (id_arr[idx].id.longGlb.seg == seg) &&
  174. (id_arr[idx].id.longGlb.offH == offH) &&
  175. (id_arr[idx].id.longGlb.offL == offL))
  176. return (idx);
  177. }
  178. /* Not in the table, create new identifier */
  179. newIdent (t, GLB_FRAME);
  180. idx = id_arr.size() - 1;
  181. id_arr[idx].id.longGlb.seg = seg;
  182. id_arr[idx].id.longGlb.offH = offH;
  183. id_arr[idx].id.longGlb.offL = offL;
  184. return (idx);
  185. }
  186. /* Checks if the entry exists in the locSym, if so, returns the idx to this
  187. * entry; otherwise creates a new global identifier node of type
  188. * TYPE_LONG_(UN)SIGN and returns the index to this new entry. */
  189. int LOCAL_ID::newLongIdx( int16_t seg, int16_t offH, int16_t offL,uint8_t regi, hlType t)
  190. {
  191. size_t idx;
  192. /* Check for entry in the table */
  193. for (idx = 0; idx < id_arr.size(); idx++)
  194. {
  195. if (/*(locSym->id[idx].type == t) && Not checking type */
  196. (id_arr[idx].id.longGlb.seg == seg) &&
  197. (id_arr[idx].id.longGlb.offH == offH) &&
  198. (id_arr[idx].id.longGlb.offL == offL) &&
  199. (id_arr[idx].id.longGlb.regi == regi))
  200. return (idx);
  201. }
  202. /* Not in the table, create new identifier */
  203. newIdent (t, GLB_FRAME);
  204. idx = id_arr.size() - 1;
  205. id_arr[idx].id.longGlb.seg = seg;
  206. id_arr[idx].id.longGlb.offH = offH;
  207. id_arr[idx].id.longGlb.offL = offL;
  208. id_arr[idx].id.longGlb.regi = regi;
  209. return (idx);
  210. }
  211. /* Creates a new stack identifier node of type TYPE_LONG_(UN)SIGN.
  212. * Returns the index to this entry. */
  213. int LOCAL_ID::newLongStk(hlType t, int offH, int offL)
  214. {
  215. size_t idx;
  216. /* Check for entry in the table */
  217. for (idx = 0; idx < id_arr.size(); idx++)
  218. {
  219. if ((id_arr[idx].type == t) &&
  220. (id_arr[idx].id.longStkId.offH == offH) &&
  221. (id_arr[idx].id.longStkId.offL == offL))
  222. return (idx);
  223. }
  224. /* Not in the table; flag as invalid offH and offL */
  225. flagByteWordId (offH);
  226. flagByteWordId (offL);
  227. /* Create new identifier */
  228. newIdent (t, STK_FRAME);
  229. idx = id_arr.size() - 1;
  230. id_arr[idx].id.longStkId.offH = offH;
  231. id_arr[idx].id.longStkId.offL = offL;
  232. return (idx);
  233. }
  234. /* Returns the index to an appropriate long identifier.
  235. * Note: long constants should be checked first and stored as a long integer
  236. * number in an expression record. */
  237. int LOCAL_ID::newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix,operDu du, LLInst &atOffset)
  238. {
  239. size_t idx = ~0; //WARNING: clients of this method might propagate this bogus value!
  240. const LLOperand *pmH, *pmL;
  241. LLInst &p_ll(*pIcode->ll());
  242. if (f == LOW_FIRST)
  243. {
  244. pmL = p_ll.get(sd);
  245. pmH = atOffset.get(sd);
  246. }
  247. else /* HIGH_FIRST */
  248. {
  249. pmL = atOffset.get(sd);
  250. pmH = p_ll.get(sd);
  251. }
  252. if (pmL->regi == 0) /* global variable */
  253. idx = newLongGlb(pmH->segValue, pmH->off, pmL->off, TYPE_LONG_SIGN);
  254. else if (pmL->regi < INDEX_BX_SI) /* register */
  255. {
  256. idx = newLongReg(TYPE_LONG_SIGN, pmH->regi, pmL->regi, ix);
  257. if (f == HIGH_FIRST)
  258. pIcode->setRegDU( pmL->regi, du); /* low part */
  259. else
  260. pIcode->setRegDU( pmH->regi, du); /* high part */
  261. }
  262. else if (pmL->off) { /* offset */
  263. if ((pmL->seg == rSS) && (pmL->regi == INDEX_BP)) /* idx on bp */
  264. idx = newLongStk(TYPE_LONG_SIGN, pmH->off, pmL->off);
  265. else if ((pmL->seg == rDS) && (pmL->regi == INDEX_BX)) /* bx */
  266. { /* glb var indexed on bx */
  267. printf("Bx indexed global, BX is an unused parameter to newLongIdx\n");
  268. idx = newLongIdx(pmH->segValue, pmH->off, pmL->off,rBX,TYPE_LONG_SIGN);
  269. pIcode->setRegDU( rBX, eUSE);
  270. }
  271. else /* idx <> bp, bx */
  272. printf ("long not supported, idx <> bp\n");
  273. }
  274. else /* (pm->regi >= INDEXBASE && pm->off = 0) => indexed && no off */
  275. printf ("long not supported, idx && no off\n");
  276. return (idx);
  277. }
  278. /* Checks whether the long stack identifier is equivalent to the source or
  279. * destination operands of pIcode and pIcode+1 (ie. these are LOW_LEVEL
  280. * icodes at present). If so, returns the rhs and lhs of this instruction.
  281. * Arguments: longId : long stack identifier
  282. * pIcode : ptr to first LOW_LEVEL icode instruction
  283. * i : idx into local identifier table for longId
  284. * idx : idx into icode array
  285. * pProc : ptr to current procedure record
  286. * rhs, lhs : return expressions if successful. */
  287. boolT checkLongEq (LONG_STKID_TYPE longId, iICODE pIcode, int i, Function * pProc, Assignment &asgn, LLInst &atOffset)
  288. {
  289. /* pointers to LOW_LEVEL icodes */
  290. const LLOperand *pmHdst, *pmLdst, *pmHsrc, *pmLsrc;
  291. pmHdst = &pIcode->ll()->dst;
  292. pmLdst = &atOffset.dst;
  293. pmHsrc = &pIcode->ll()->src();
  294. pmLsrc = &atOffset.src();
  295. if ((longId.offH == pmHdst->off) && (longId.offL == pmLdst->off))
  296. {
  297. asgn.lhs = AstIdent::LongIdx (i);
  298. if ( not pIcode->ll()->testFlags(NO_SRC) )
  299. {
  300. asgn.rhs = AstIdent::Long (&pProc->localId, SRC, pIcode, HIGH_FIRST, pIcode, eUSE, atOffset);
  301. }
  302. return true;
  303. }
  304. else if ((longId.offH == pmHsrc->off) && (longId.offL == pmLsrc->off))
  305. {
  306. asgn.lhs = AstIdent::Long (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode,eDEF, atOffset);
  307. asgn.rhs = AstIdent::LongIdx (i);
  308. return true;
  309. }
  310. return false;
  311. }
  312. /* Checks whether the long stack identifier is equivalent to the source or
  313. * destination operands of pIcode and pIcode+1 (ie. these are LOW_LEVEL
  314. * icodes at present). If so, returns the rhs and lhs of this instruction.
  315. * Arguments: longId : long stack identifier
  316. * pIcode : ptr to first LOW_LEVEL icode instruction
  317. * i : idx into local identifier table for longId
  318. * idx : idx into icode array
  319. * pProc : ptr to current procedure record
  320. * rhs, lhs : return expressions if successful. */
  321. boolT checkLongRegEq (LONGID_TYPE longId, iICODE pIcode, int i,
  322. Function * pProc, Assignment &asgn, LLInst &atOffset)
  323. {
  324. /* pointers to LOW_LEVEL icodes */
  325. const LLOperand *pmHdst, *pmLdst, *pmHsrc, *pmLsrc;
  326. pmHdst = &pIcode->ll()->dst;
  327. pmLdst = &atOffset.dst;
  328. pmHsrc = &pIcode->ll()->src();
  329. pmLsrc = &atOffset.src();
  330. if ((longId.h == pmHdst->regi) && (longId.l == pmLdst->regi))
  331. {
  332. asgn.lhs = AstIdent::LongIdx (i);
  333. if ( not pIcode->ll()->testFlags(NO_SRC) )
  334. {
  335. asgn.rhs = AstIdent::Long (&pProc->localId, SRC, pIcode, HIGH_FIRST, pIcode, eUSE, atOffset);
  336. }
  337. return true;
  338. }
  339. else if ((longId.h == pmHsrc->regi) && (longId.l == pmLsrc->regi))
  340. {
  341. asgn.lhs = AstIdent::Long (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode, eDEF, atOffset);
  342. asgn.rhs = AstIdent::LongIdx (i);
  343. return true;
  344. }
  345. return false;
  346. }
  347. /* Given an index into the local identifier table for a long register
  348. * variable, determines whether regi is the high or low part, and returns
  349. * the other part */
  350. eReg otherLongRegi (eReg regi, int idx, LOCAL_ID *locTbl)
  351. {
  352. ID *id;
  353. id = &locTbl->id_arr[idx];
  354. if ((id->loc == REG_FRAME) && ((id->type == TYPE_LONG_SIGN) ||
  355. (id->type == TYPE_LONG_UNSIGN)))
  356. {
  357. if (id->id.longId.h == regi)
  358. return (id->id.longId.l);
  359. else if (id->id.longId.l == regi)
  360. return (id->id.longId.h);
  361. }
  362. return rUNDEF; // Cristina: please check this!
  363. }
  364. /* Checks if the registers regL and regH have been used independently in
  365. * the local identifier table. If so, macros for these registers are
  366. * placed in the local identifier table, as these registers belong to a
  367. * long register identifier. */
  368. void LOCAL_ID::propLongId (uint8_t regL, uint8_t regH, const char *name)
  369. {
  370. for (ID &rid : id_arr)
  371. {
  372. if (rid.typeBitsize()!=16)
  373. continue;
  374. if ( (rid.id.regi != regL) and (rid.id.regi != regH) )
  375. continue;
  376. // otherwise at least 1 is ok
  377. rid.name = name;
  378. rid.hasMacro = true;
  379. rid.illegal = true;
  380. if (rid.id.regi == regL)
  381. {
  382. strcpy (rid.macro, "LO");
  383. }
  384. else // if (rid.id.regi == regH)
  385. {
  386. strcpy (rid.macro, "HI");
  387. }
  388. }
  389. }