locident.cpp 15 KB

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