locident.cpp 16 KB

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