locident.cpp 16 KB

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