locident.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. auto found=std::find_if(id_arr.begin(),id_arr.end(),[off](ID &en)->bool {
  65. //if (((en.type == TYPE_WORD_SIGN) || (en.type == TYPE_BYTE_SIGN)) &&
  66. if ((en.typeBitsize()<=16) &&
  67. (en.id.bwId.off == off) && (en.id.bwId.regOff == 0))
  68. return true;
  69. return false;
  70. });
  71. if(found==id_arr.end())
  72. {
  73. printf("No entry to flag as invalid in LOCAL_ID::flagByteWordId \n");
  74. return;
  75. }
  76. found->illegal = true;
  77. }
  78. /* Creates a new stack identifier node of TYPE_BYTE_(UN)SIGN or
  79. * TYPE_WORD_(UN)SIGN type. Returns the index to this new entry. */
  80. int LOCAL_ID::newByteWordStk(hlType t, int off, uint8_t regOff)
  81. {
  82. int idx;
  83. /* Check for entry in the table */
  84. auto found=std::find_if(id_arr.begin(),id_arr.end(),[off,regOff](ID &el)->bool {
  85. if ((el.id.bwId.off == off) && (el.id.bwId.regOff == regOff))
  86. return true;
  87. return false;
  88. });
  89. if(found!=id_arr.end())
  90. return found-id_arr.begin(); //return Index to found element
  91. /* Not in table, create new identifier */
  92. newIdent (t, STK_FRAME);
  93. idx = id_arr.size() - 1;
  94. id_arr[idx].id.bwId.regOff = regOff;
  95. id_arr[idx].id.bwId.off = off;
  96. return (idx);
  97. }
  98. /* Checks if the entry exists in the locSym, if so, returns the idx to this
  99. * entry; otherwise creates a new global identifier node of type
  100. * TYPE_WORD_(UN)SIGN and returns the index to this new entry.
  101. * Arguments: locSym : ptr to the local symbol table
  102. * seg: segment value for global variable
  103. * off: offset from segment
  104. * regi: indexed register into global variable
  105. * ix: index into icode array
  106. * t: HIGH_LEVEL type */
  107. int LOCAL_ID::newIntIdx(int16_t seg, int16_t off, eReg regi, hlType t)
  108. {
  109. /* Check for entry in the table */
  110. for (size_t idx = 0; idx < id_arr.size(); idx++)
  111. {
  112. if (/*(locSym->id[idx].type == t) && Not checking type */
  113. (id_arr[idx].id.bwGlb.seg == seg) &&
  114. (id_arr[idx].id.bwGlb.off == off) &&
  115. (id_arr[idx].id.bwGlb.regi == regi))
  116. return (idx);
  117. }
  118. /* Not in the table, create new identifier */
  119. newIdent (t, GLB_FRAME);
  120. id_arr.back().id.bwGlb.seg = seg;
  121. id_arr.back().id.bwGlb.off = off;
  122. id_arr.back().id.bwGlb.regi = regi;
  123. return id_arr.size() - 1;
  124. }
  125. /* Checks if the entry exists in the locSym, if so, returns the idx to this
  126. * entry; otherwise creates a new register identifier node of type
  127. * TYPE_LONG_(UN)SIGN and returns the index to this new entry. */
  128. int LOCAL_ID::newLongReg(hlType t, eReg regH, eReg regL, iICODE ix_)
  129. {
  130. size_t idx;
  131. //iICODE ix_;
  132. /* Check for entry in the table */
  133. for (idx = 0; idx < id_arr.size(); idx++)
  134. {
  135. ID &entry(id_arr[idx]);
  136. if (/*(locSym->id[idx].type == t) && Not checking type */
  137. (entry.id.longId.h == regH) &&
  138. (entry.id.longId.l == regL))
  139. {
  140. /* Check for occurrence in the list */
  141. if (entry.idx.inList(ix_))
  142. return (idx);
  143. else
  144. {
  145. /* Insert icode index in list */
  146. entry.idx.push_back(ix_);
  147. //entry.idx.insert(ix_);
  148. return (idx);
  149. }
  150. }
  151. }
  152. /* Not in the table, create new identifier */
  153. newIdent (t, REG_FRAME);
  154. id_arr[id_arr.size()-1].idx.push_back(ix_);//insert(ix_);
  155. idx = id_arr.size() - 1;
  156. id_arr[idx].id.longId.h = regH;
  157. id_arr[idx].id.longId.l = regL;
  158. return (idx);
  159. }
  160. /* Returns an identifier conditional expression node of type TYPE_LONG or
  161. * TYPE_WORD_SIGN */
  162. COND_EXPR * LOCAL_ID::createId(const ID *retVal, iICODE ix_)
  163. {
  164. return COND_EXPR::idID(retVal,this,ix_);
  165. }
  166. /* Checks if the entry exists in the locSym, if so, returns the idx to this
  167. * entry; otherwise creates a new global identifier node of type
  168. * TYPE_LONG_(UN)SIGN and returns the index to this new entry. */
  169. int LOCAL_ID::newLongGlb(int16_t seg, int16_t offH, int16_t offL,hlType t)
  170. {
  171. size_t idx;
  172. /* Check for entry in the table */
  173. for (idx = 0; idx < id_arr.size(); idx++)
  174. {
  175. if (/*(locSym->id[idx].type == t) && Not checking type */
  176. (id_arr[idx].id.longGlb.seg == seg) &&
  177. (id_arr[idx].id.longGlb.offH == offH) &&
  178. (id_arr[idx].id.longGlb.offL == offL))
  179. return (idx);
  180. }
  181. /* Not in the table, create new identifier */
  182. newIdent (t, GLB_FRAME);
  183. idx = id_arr.size() - 1;
  184. id_arr[idx].id.longGlb.seg = seg;
  185. id_arr[idx].id.longGlb.offH = offH;
  186. id_arr[idx].id.longGlb.offL = offL;
  187. return (idx);
  188. }
  189. /* Checks if the entry exists in the locSym, if so, returns the idx to this
  190. * entry; otherwise creates a new global identifier node of type
  191. * TYPE_LONG_(UN)SIGN and returns the index to this new entry. */
  192. int LOCAL_ID::newLongIdx( int16_t seg, int16_t offH, int16_t offL,uint8_t regi, hlType t)
  193. {
  194. size_t idx;
  195. /* Check for entry in the table */
  196. for (idx = 0; idx < id_arr.size(); idx++)
  197. {
  198. if (/*(locSym->id[idx].type == t) && Not checking type */
  199. (id_arr[idx].id.longGlb.seg == seg) &&
  200. (id_arr[idx].id.longGlb.offH == offH) &&
  201. (id_arr[idx].id.longGlb.offL == offL) &&
  202. (id_arr[idx].id.longGlb.regi == regi))
  203. return (idx);
  204. }
  205. /* Not in the table, create new identifier */
  206. newIdent (t, GLB_FRAME);
  207. idx = id_arr.size() - 1;
  208. id_arr[idx].id.longGlb.seg = seg;
  209. id_arr[idx].id.longGlb.offH = offH;
  210. id_arr[idx].id.longGlb.offL = offL;
  211. id_arr[idx].id.longGlb.regi = regi;
  212. return (idx);
  213. }
  214. /* Creates a new stack identifier node of type TYPE_LONG_(UN)SIGN.
  215. * Returns the index to this entry. */
  216. int LOCAL_ID::newLongStk(hlType t, int offH, int offL)
  217. {
  218. size_t idx;
  219. /* Check for entry in the table */
  220. for (idx = 0; idx < id_arr.size(); idx++)
  221. {
  222. if ((id_arr[idx].type == t) &&
  223. (id_arr[idx].id.longStkId.offH == offH) &&
  224. (id_arr[idx].id.longStkId.offL == offL))
  225. return (idx);
  226. }
  227. /* Not in the table; flag as invalid offH and offL */
  228. flagByteWordId (offH);
  229. flagByteWordId (offL);
  230. /* Create new identifier */
  231. newIdent (t, STK_FRAME);
  232. idx = id_arr.size() - 1;
  233. id_arr[idx].id.longStkId.offH = offH;
  234. id_arr[idx].id.longStkId.offL = offL;
  235. return (idx);
  236. }
  237. /* Returns the index to an appropriate long identifier.
  238. * Note: long constants should be checked first and stored as a long integer
  239. * number in an expression record. */
  240. int LOCAL_ID::newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix,operDu du, LLInst &atOffset)
  241. {
  242. size_t idx;
  243. const LLOperand *pmH, *pmL;
  244. LLInst &p_ll(*pIcode->ll());
  245. if (f == LOW_FIRST)
  246. {
  247. pmL = p_ll.get(sd);
  248. pmH = atOffset.get(sd);
  249. }
  250. else /* HIGH_FIRST */
  251. {
  252. pmL = atOffset.get(sd);
  253. pmH = p_ll.get(sd);
  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, LLInst &atOffset)
  291. {
  292. /* pointers to LOW_LEVEL icodes */
  293. const LLOperand *pmHdst, *pmLdst, *pmHsrc, *pmLsrc;
  294. pmHdst = &pIcode->ll()->dst;
  295. pmLdst = &atOffset.dst;
  296. pmHsrc = &pIcode->ll()->src();
  297. pmLsrc = &atOffset.src();
  298. if ((longId.offH == pmHdst->off) && (longId.offL == pmLdst->off))
  299. {
  300. asgn.lhs = COND_EXPR::idLongIdx (i);
  301. if ( not pIcode->ll()->testFlags(NO_SRC) )
  302. {
  303. asgn.rhs = COND_EXPR::idLong (&pProc->localId, SRC, pIcode, HIGH_FIRST, pIcode, eUSE, atOffset);
  304. }
  305. return true;
  306. }
  307. else if ((longId.offH == pmHsrc->off) && (longId.offL == pmLsrc->off))
  308. {
  309. asgn.lhs = COND_EXPR::idLong (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode,eDEF, atOffset);
  310. asgn.rhs = COND_EXPR::idLongIdx (i);
  311. return true;
  312. }
  313. return false;
  314. }
  315. /* Checks whether the long stack identifier is equivalent to the source or
  316. * destination operands of pIcode and pIcode+1 (ie. these are LOW_LEVEL
  317. * icodes at present). If so, returns the rhs and lhs of this instruction.
  318. * Arguments: longId : long stack identifier
  319. * pIcode : ptr to first LOW_LEVEL icode instruction
  320. * i : idx into local identifier table for longId
  321. * idx : idx into icode array
  322. * pProc : ptr to current procedure record
  323. * rhs, lhs : return expressions if successful. */
  324. boolT checkLongRegEq (LONGID_TYPE longId, iICODE pIcode, int i,
  325. Function * pProc, Assignment &asgn, LLInst &atOffset)
  326. {
  327. /* pointers to LOW_LEVEL icodes */
  328. const LLOperand *pmHdst, *pmLdst, *pmHsrc, *pmLsrc;
  329. pmHdst = &pIcode->ll()->dst;
  330. pmLdst = &atOffset.dst;
  331. pmHsrc = &pIcode->ll()->src();
  332. pmLsrc = &atOffset.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. }