locident.cpp 15 KB

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