locident.cpp 14 KB

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