locident.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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_)) //count(
  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, ICODE *pIcode, hlFirst f, iICODE ix,operDu du, Int off)
  219. {
  220. Int idx;
  221. LLOperand *pmH, *pmL;
  222. if (f == LOW_FIRST)
  223. {
  224. pmL = (sd == SRC) ? &pIcode->ic.ll.src : &pIcode->ic.ll.dst;
  225. pmH = (sd == SRC) ? &(pIcode+off)->ic.ll.src : &(pIcode+off)->ic.ll.dst;
  226. }
  227. else /* HIGH_FIRST */
  228. {
  229. pmH = (sd == SRC) ? &pIcode->ic.ll.src : &pIcode->ic.ll.dst;
  230. pmL = (sd == SRC) ? &(pIcode+off)->ic.ll.src : &(pIcode+off)->ic.ll.dst;
  231. }
  232. if (pmL->regi == 0) /* global variable */
  233. idx = newLongGlb(pmH->segValue, pmH->off, pmL->off, TYPE_LONG_SIGN);
  234. else if (pmL->regi < INDEXBASE) /* register */
  235. {
  236. idx = newLongReg(TYPE_LONG_SIGN, pmH->regi, pmL->regi, ix);
  237. if (f == HIGH_FIRST)
  238. pIcode->setRegDU( pmL->regi, du); /* low part */
  239. else
  240. pIcode->setRegDU( pmH->regi, du); /* high part */
  241. }
  242. else if (pmL->off) { /* offset */
  243. if ((pmL->seg == rSS) && (pmL->regi == INDEXBASE + 6)) /* idx on bp */
  244. idx = newLongStk(TYPE_LONG_SIGN, pmH->off, pmL->off);
  245. else if ((pmL->seg == rDS) && (pmL->regi == INDEXBASE + 7)) /* bx */
  246. { /* glb var indexed on bx */
  247. printf("Bx indexed global, BX is an unused parameter to newLongIdx\n");
  248. idx = newLongIdx(pmH->segValue, pmH->off, pmL->off,rBX,TYPE_LONG_SIGN);
  249. pIcode->setRegDU( rBX, eUSE);
  250. }
  251. else /* idx <> bp, bx */
  252. printf ("long not supported, idx <> bp\n");
  253. }
  254. else /* (pm->regi >= INDEXBASE && pm->off = 0) => indexed && no off */
  255. printf ("long not supported, idx && no off\n");
  256. return (idx);
  257. }
  258. /* Checks whether the long stack identifier is equivalent to the source or
  259. * destination operands of pIcode and pIcode+1 (ie. these are LOW_LEVEL
  260. * icodes at present). If so, returns the rhs and lhs of this instruction.
  261. * Arguments: longId : long stack identifier
  262. * pIcode : ptr to first LOW_LEVEL icode instruction
  263. * i : idx into local identifier table for longId
  264. * idx : idx into icode array
  265. * pProc : ptr to current procedure record
  266. * rhs, lhs : return expressions if successful. */
  267. boolT checkLongEq (LONG_STKID_TYPE longId, iICODE pIcode, Int i,
  268. Function * pProc, Assignment &asgn, Int off)
  269. {
  270. LLOperand *pmHdst, *pmLdst, *pmHsrc, *pmLsrc; /* pointers to LOW_LEVEL icodes */
  271. pmHdst = &pIcode->ic.ll.dst;
  272. pmLdst = &(pIcode+off)->ic.ll.dst;
  273. pmHsrc = &pIcode->ic.ll.src;
  274. pmLsrc = &(pIcode+off)->ic.ll.src;
  275. if ((longId.offH == pmHdst->off) && (longId.offL == pmLdst->off))
  276. {
  277. asgn.lhs = COND_EXPR::idLongIdx (i);
  278. if ((pIcode->ic.ll.flg & NO_SRC) != NO_SRC)
  279. {
  280. asgn.rhs = COND_EXPR::idLong (&pProc->localId, SRC, pIcode, HIGH_FIRST, pIcode, eUSE, off);
  281. }
  282. return true;
  283. }
  284. else if ((longId.offH == pmHsrc->off) && (longId.offL == pmLsrc->off))
  285. {
  286. asgn.lhs = COND_EXPR::idLong (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode,eDEF, off);
  287. asgn.rhs = COND_EXPR::idLongIdx (i);
  288. return true;
  289. }
  290. return false;
  291. }
  292. /* Checks whether the long stack identifier is equivalent to the source or
  293. * destination operands of pIcode and pIcode+1 (ie. these are LOW_LEVEL
  294. * icodes at present). If so, returns the rhs and lhs of this instruction.
  295. * Arguments: longId : long stack identifier
  296. * pIcode : ptr to first LOW_LEVEL icode instruction
  297. * i : idx into local identifier table for longId
  298. * idx : idx into icode array
  299. * pProc : ptr to current procedure record
  300. * rhs, lhs : return expressions if successful. */
  301. boolT checkLongRegEq (LONGID_TYPE longId, iICODE pIcode, Int i,
  302. Function * pProc, COND_EXPR *&rhs, COND_EXPR *&lhs, Int off)
  303. {
  304. LLOperand *pmHdst, *pmLdst, *pmHsrc, *pmLsrc; /* pointers to LOW_LEVEL icodes */
  305. pmHdst = &pIcode->ic.ll.dst;
  306. pmLdst = &(pIcode+off)->ic.ll.dst;
  307. pmHsrc = &pIcode->ic.ll.src;
  308. pmLsrc = &(pIcode+off)->ic.ll.src;
  309. if ((longId.h == pmHdst->regi) && (longId.l == pmLdst->regi))
  310. {
  311. lhs = COND_EXPR::idLongIdx (i);
  312. if ((pIcode->ic.ll.flg & NO_SRC) != NO_SRC)
  313. {
  314. //rhs = COND_EXPR::idLong (&pProc->localId, SRC, pIcode, HIGH_FIRST, idx, eUSE, off);
  315. rhs = COND_EXPR::idLong (&pProc->localId, SRC, pIcode, HIGH_FIRST, pIcode, eUSE, off);
  316. }
  317. return true;
  318. }
  319. else if ((longId.h == pmHsrc->regi) && (longId.l == pmLsrc->regi))
  320. {
  321. lhs = COND_EXPR::idLong (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode, eDEF, off);
  322. //lhs = COND_EXPR::idLong (&pProc->localId, DST, pIcode, HIGH_FIRST, idx, eDEF, off);
  323. rhs = COND_EXPR::idLongIdx (i);
  324. return true;
  325. }
  326. return false;
  327. }
  328. /* Given an index into the local identifier table for a long register
  329. * variable, determines whether regi is the high or low part, and returns
  330. * the other part */
  331. byte otherLongRegi (byte regi, Int idx, LOCAL_ID *locTbl)
  332. {
  333. ID *id;
  334. id = &locTbl->id_arr[idx];
  335. if ((id->loc == REG_FRAME) && ((id->type == TYPE_LONG_SIGN) ||
  336. (id->type == TYPE_LONG_UNSIGN)))
  337. {
  338. if (id->id.longId.h == regi)
  339. return (id->id.longId.l);
  340. else if (id->id.longId.l == regi)
  341. return (id->id.longId.h);
  342. }
  343. return 0; // Cristina: please check this!
  344. }
  345. /* Checks if the registers regL and regH have been used independently in
  346. * the local identifier table. If so, macros for these registers are
  347. * placed in the local identifier table, as these registers belong to a
  348. * long register identifier. */
  349. void LOCAL_ID::propLongId (byte regL, byte regH, const char *name)
  350. {
  351. Int i;
  352. ID *_id;
  353. for (i = 0; i < id_arr.size(); i++)
  354. {
  355. _id = &id_arr[i];
  356. if (_id->typeBitsize()==16)
  357. {
  358. if (_id->id.regi == regL)
  359. {
  360. strcpy (_id->name, name);
  361. strcpy (_id->macro, "LO");
  362. _id->hasMacro = TRUE;
  363. _id->illegal = TRUE;
  364. }
  365. else if (_id->id.regi == regH)
  366. {
  367. strcpy (_id->name, name);
  368. strcpy (_id->macro, "HI");
  369. _id->hasMacro = TRUE;
  370. _id->illegal = TRUE;
  371. }
  372. }
  373. }
  374. }