locident.cpp 16 KB

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