locident.cpp 16 KB

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