locident.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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, uint8_t 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.isSigned()) &&
  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("Entry not found 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, uint8_t 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, uint8_t regH, uint8_t regL, iICODE ix_)
  128. {
  129. int 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. int 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. { int idx;
  187. /* Check for entry in the table */
  188. for (idx = 0; idx < id_arr.size(); idx++)
  189. {
  190. if (/*(locSym->id[idx].type == t) && Not checking type */
  191. (id_arr[idx].id.longGlb.seg == seg) &&
  192. (id_arr[idx].id.longGlb.offH == offH) &&
  193. (id_arr[idx].id.longGlb.offL == offL) &&
  194. (id_arr[idx].id.longGlb.regi == regi))
  195. return (idx);
  196. }
  197. /* Not in the table, create new identifier */
  198. newIdent (t, GLB_FRAME);
  199. idx = id_arr.size() - 1;
  200. id_arr[idx].id.longGlb.seg = seg;
  201. id_arr[idx].id.longGlb.offH = offH;
  202. id_arr[idx].id.longGlb.offL = offL;
  203. id_arr[idx].id.longGlb.regi = regi;
  204. return (idx);
  205. }
  206. /* Creates a new stack identifier node of type TYPE_LONG_(UN)SIGN.
  207. * Returns the index to this entry. */
  208. int LOCAL_ID::newLongStk(hlType t, int offH, int offL)
  209. {
  210. int idx;
  211. /* Check for entry in the table */
  212. for (idx = 0; idx < id_arr.size(); idx++)
  213. {
  214. if ((id_arr[idx].type == t) &&
  215. (id_arr[idx].id.longStkId.offH == offH) &&
  216. (id_arr[idx].id.longStkId.offL == offL))
  217. return (idx);
  218. }
  219. /* Not in the table; flag as invalid offH and offL */
  220. flagByteWordId (offH);
  221. flagByteWordId (offL);
  222. /* Create new identifier */
  223. newIdent (t, STK_FRAME);
  224. idx = id_arr.size() - 1;
  225. id_arr[idx].id.longStkId.offH = offH;
  226. id_arr[idx].id.longStkId.offL = offL;
  227. return (idx);
  228. }
  229. /* Returns the index to an appropriate long identifier.
  230. * Note: long constants should be checked first and stored as a long integer
  231. * number in an expression record. */
  232. int LOCAL_ID::newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix,operDu du, int off)
  233. {
  234. int idx;
  235. LLOperand *pmH, *pmL;
  236. iICODE atOffset(pIcode);
  237. advance(atOffset,off);
  238. if (f == LOW_FIRST)
  239. {
  240. pmL = (sd == SRC) ? &pIcode->ll()->src : &pIcode->ll()->dst;
  241. pmH = (sd == SRC) ? &atOffset->ll()->src : &atOffset->ll()->dst;
  242. }
  243. else /* HIGH_FIRST */
  244. {
  245. pmH = (sd == SRC) ? &pIcode->ll()->src : &pIcode->ll()->dst;
  246. pmL = (sd == SRC) ? &atOffset->ll()->src : &atOffset->ll()->dst;
  247. }
  248. if (pmL->regi == 0) /* global variable */
  249. idx = newLongGlb(pmH->segValue, pmH->off, pmL->off, TYPE_LONG_SIGN);
  250. else if (pmL->regi < INDEXBASE) /* register */
  251. {
  252. idx = newLongReg(TYPE_LONG_SIGN, pmH->regi, pmL->regi, ix);
  253. if (f == HIGH_FIRST)
  254. pIcode->setRegDU( pmL->regi, du); /* low part */
  255. else
  256. pIcode->setRegDU( pmH->regi, du); /* high part */
  257. }
  258. else if (pmL->off) { /* offset */
  259. if ((pmL->seg == rSS) && (pmL->regi == INDEXBASE + 6)) /* idx on bp */
  260. idx = newLongStk(TYPE_LONG_SIGN, pmH->off, pmL->off);
  261. else if ((pmL->seg == rDS) && (pmL->regi == INDEXBASE + 7)) /* bx */
  262. { /* glb var indexed on bx */
  263. printf("Bx indexed global, BX is an unused parameter to newLongIdx\n");
  264. idx = newLongIdx(pmH->segValue, pmH->off, pmL->off,rBX,TYPE_LONG_SIGN);
  265. pIcode->setRegDU( rBX, eUSE);
  266. }
  267. else /* idx <> bp, bx */
  268. printf ("long not supported, idx <> bp\n");
  269. }
  270. else /* (pm->regi >= INDEXBASE && pm->off = 0) => indexed && no off */
  271. printf ("long not supported, idx && no off\n");
  272. return (idx);
  273. }
  274. /* Checks whether the long stack identifier is equivalent to the source or
  275. * destination operands of pIcode and pIcode+1 (ie. these are LOW_LEVEL
  276. * icodes at present). If so, returns the rhs and lhs of this instruction.
  277. * Arguments: longId : long stack identifier
  278. * pIcode : ptr to first LOW_LEVEL icode instruction
  279. * i : idx into local identifier table for longId
  280. * idx : idx into icode array
  281. * pProc : ptr to current procedure record
  282. * rhs, lhs : return expressions if successful. */
  283. boolT checkLongEq (LONG_STKID_TYPE longId, iICODE pIcode, int i,
  284. Function * pProc, Assignment &asgn, int off)
  285. {
  286. LLOperand *pmHdst, *pmLdst, *pmHsrc, *pmLsrc; /* pointers to LOW_LEVEL icodes */
  287. iICODE atOffset(pIcode);
  288. advance(atOffset,off);
  289. pmHdst = &pIcode->ll()->dst;
  290. pmLdst = &atOffset->ll()->dst;
  291. pmHsrc = &pIcode->ll()->src;
  292. pmLsrc = &atOffset->ll()->src;
  293. if ((longId.offH == pmHdst->off) && (longId.offL == pmLdst->off))
  294. {
  295. asgn.lhs = COND_EXPR::idLongIdx (i);
  296. if ( not pIcode->ll()->testFlags(NO_SRC) )
  297. {
  298. asgn.rhs = COND_EXPR::idLong (&pProc->localId, SRC, pIcode, HIGH_FIRST, pIcode, eUSE, off);
  299. }
  300. return true;
  301. }
  302. else if ((longId.offH == pmHsrc->off) && (longId.offL == pmLsrc->off))
  303. {
  304. asgn.lhs = COND_EXPR::idLong (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode,eDEF, off);
  305. asgn.rhs = COND_EXPR::idLongIdx (i);
  306. return true;
  307. }
  308. return false;
  309. }
  310. /* Checks whether the long stack identifier is equivalent to the source or
  311. * destination operands of pIcode and pIcode+1 (ie. these are LOW_LEVEL
  312. * icodes at present). If so, returns the rhs and lhs of this instruction.
  313. * Arguments: longId : long stack identifier
  314. * pIcode : ptr to first LOW_LEVEL icode instruction
  315. * i : idx into local identifier table for longId
  316. * idx : idx into icode array
  317. * pProc : ptr to current procedure record
  318. * rhs, lhs : return expressions if successful. */
  319. boolT checkLongRegEq (LONGID_TYPE longId, iICODE pIcode, int i,
  320. Function * pProc, COND_EXPR *&rhs, COND_EXPR *&lhs, int off)
  321. {
  322. LLOperand *pmHdst, *pmLdst, *pmHsrc, *pmLsrc; /* pointers to LOW_LEVEL icodes */
  323. iICODE atOffset(pIcode);
  324. advance(atOffset,off);
  325. pmHdst = &pIcode->ll()->dst;
  326. pmLdst = &atOffset->ll()->dst;
  327. pmHsrc = &pIcode->ll()->src;
  328. pmLsrc = &atOffset->ll()->src;
  329. if ((longId.h == pmHdst->regi) && (longId.l == pmLdst->regi))
  330. {
  331. lhs = COND_EXPR::idLongIdx (i);
  332. if ( not pIcode->ll()->testFlags(NO_SRC) )
  333. {
  334. rhs = COND_EXPR::idLong (&pProc->localId, SRC, pIcode, HIGH_FIRST, pIcode, eUSE, off);
  335. }
  336. return true;
  337. }
  338. else if ((longId.h == pmHsrc->regi) && (longId.l == pmLsrc->regi))
  339. {
  340. lhs = COND_EXPR::idLong (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode, eDEF, off);
  341. rhs = COND_EXPR::idLongIdx (i);
  342. return true;
  343. }
  344. return false;
  345. }
  346. /* Given an index into the local identifier table for a long register
  347. * variable, determines whether regi is the high or low part, and returns
  348. * the other part */
  349. uint8_t otherLongRegi (uint8_t regi, int idx, LOCAL_ID *locTbl)
  350. {
  351. ID *id;
  352. id = &locTbl->id_arr[idx];
  353. if ((id->loc == REG_FRAME) && ((id->type == TYPE_LONG_SIGN) ||
  354. (id->type == TYPE_LONG_UNSIGN)))
  355. {
  356. if (id->id.longId.h == regi)
  357. return (id->id.longId.l);
  358. else if (id->id.longId.l == regi)
  359. return (id->id.longId.h);
  360. }
  361. return 0; // Cristina: please check this!
  362. }
  363. /* Checks if the registers regL and regH have been used independently in
  364. * the local identifier table. If so, macros for these registers are
  365. * placed in the local identifier table, as these registers belong to a
  366. * long register identifier. */
  367. void LOCAL_ID::propLongId (uint8_t regL, uint8_t regH, const char *name)
  368. {
  369. int i;
  370. ID *_id;
  371. for (i = 0; i < id_arr.size(); i++)
  372. {
  373. _id = &id_arr[i];
  374. if (_id->typeBitsize()==16)
  375. {
  376. if (_id->id.regi == regL)
  377. {
  378. strcpy (_id->name, name);
  379. strcpy (_id->macro, "LO");
  380. _id->hasMacro = TRUE;
  381. _id->illegal = TRUE;
  382. }
  383. else if (_id->id.regi == regH)
  384. {
  385. strcpy (_id->name, name);
  386. strcpy (_id->macro, "HI");
  387. _id->hasMacro = TRUE;
  388. _id->illegal = TRUE;
  389. }
  390. }
  391. }
  392. }