locident.cpp 14 KB

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