locident.cpp 16 KB

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