home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dosdisas.zip / dccsrcoo.zip / locident.cpp < prev    next >
C/C++ Source or Header  |  1997-04-09  |  16KB  |  475 lines

  1. /*$Log:    locident.c,v $
  2.  * Revision 1.7  94/02/22  15:17:21  cifuente
  3.  * Code generation done.
  4.  * 
  5.  * Revision 1.6  93/12/13  12:22:01  cifuente
  6.  * Interprocedural live analysis finished.
  7.  * 
  8.  * Revision 1.5  93/11/10  17:29:09  cifuente
  9.  * Procedure header, locals
  10.  * 
  11.  * Revision 1.4  93/11/08  12:04:33  cifuente
  12.  * du1 analysis finished.  Instantiates procedure arguments for user
  13.  * declared procedures.
  14.  * 
  15.  * Revision 1.3  93/11/01  15:04:49  cifuente
  16.  * Finds byte and integer expressions
  17.  * 
  18.  * Revision 1.2  93/10/25  10:58:25  cifuente
  19.  * New SYNTHETIC instructions for d/u analysis
  20.  * 
  21.  * Revision 1.1  93/10/11  11:38:57  cifuente
  22.  * Initial revision
  23.  * 
  24.  * File: locIdent.c
  25.  * Purpose: support routines for high-level local identifier definitions.
  26.  * Date: October 1993
  27.  */
  28.  
  29. #include "dcc.h"
  30. #include <string.h>
  31.  
  32.  
  33. #define LOCAL_ID_DELTA  25
  34. #define IDX_ARRAY_DELTA  5
  35.  
  36.  
  37. void insertIdx (IDX_ARRAY *list, Int idx)
  38. /* Inserts the new index at the end of the list.  If there is need to
  39.  * allocate extra storage, it does so.  */
  40. {
  41.     if (list->csym == list->alloc)
  42.     {
  43.         list->alloc += IDX_ARRAY_DELTA;
  44.         list->idx = (Int*)reallocVar(list->idx, list->alloc * sizeof(Int));
  45.         memset (&list->idx[list->csym], 0, IDX_ARRAY_DELTA * sizeof(Int));
  46.     }
  47.     list->idx[list->csym] = idx;
  48.     list->csym++;
  49. }
  50.  
  51.  
  52. static boolT inList (IDX_ARRAY *list, Int idx)
  53. /* Returns whether idx is in the list array or not */
  54. { Int i;
  55.  
  56.     for (i = 0; i < list->csym; i++)
  57.         if (list->idx[i] == idx)
  58.             return (TRUE);
  59.     return (FALSE);
  60. }
  61.  
  62.  
  63. static void newIdent (LOCAL_ID *locSym, hlType t, frameType f)
  64. /* Creates a new identifier node of type t and returns it.
  65.  * Arguments: locSym : local long symbol table
  66.  *            t : type of LONG identifier
  67.  *            f : frame where this variable is located
  68.  *            ix : index into icode array where this var is used */
  69. {
  70.     if (locSym->csym == locSym->alloc)
  71.     {
  72.         locSym->alloc += LOCAL_ID_DELTA;
  73.         locSym->id = (ID*)reallocVar(locSym->id, locSym->alloc * sizeof(ID));
  74.         memset (&locSym->id[locSym->csym], 0, LOCAL_ID_DELTA * sizeof(ID));
  75.     }
  76.     locSym->id[locSym->csym].type = t;
  77.     locSym->id[locSym->csym].loc = f;
  78.     locSym->csym++;
  79. }
  80.  
  81.  
  82. Int newByteWordRegId (LOCAL_ID *locSym, hlType t, byte regi)
  83. /* Creates a new register identifier node of TYPE_BYTE_(UN)SIGN or
  84.  * TYPE_WORD_(UN)SIGN type.  Returns the index to this new entry.       */
  85. { Int idx;
  86.  
  87.     /* Check for entry in the table */
  88.     for (idx = 0; idx < locSym->csym; idx++)
  89.     {
  90.         if ((locSym->id[idx].type == t) &&
  91.             (locSym->id[idx].id.regi == regi))
  92.             return (idx);
  93.     }
  94.  
  95.     /* Not in table, create new identifier */
  96.     newIdent (locSym, t, REG_FRAME);
  97.     idx = locSym->csym - 1;
  98.     locSym->id[idx].id.regi = regi;
  99.     return (idx);
  100. }
  101.  
  102.  
  103. static void flagByteWordId (LOCAL_ID *locsym, Int off)
  104. /* Flags the entry associated with the offset off to illegal, as this
  105.  * offset is part of a long stack variable. 
  106.  * Note: it is easier enough to remove this entry by moving the rest of
  107.  *       the array 1 position.  The problem is that indexes into this 
  108.  *       array have already been saved in several positions; therefore,
  109.  *       flagging this entry as illegal is all that can be done.    */
  110. { Int idx;
  111.  
  112.     for (idx = 0; idx < locsym->csym; idx++)
  113.     {
  114.         if (((locsym->id[idx].type == TYPE_WORD_SIGN) ||
  115.              (locsym->id[idx].type == TYPE_BYTE_SIGN)) &&
  116.             (locsym->id[idx].id.bwId.off == off) &&
  117.             (locsym->id[idx].id.bwId.regOff == 0))
  118.         {
  119.             locsym->id[idx].illegal = TRUE;
  120.             break;
  121.         }
  122.     }
  123. }
  124.  
  125.  
  126. Int newByteWordStkId (LOCAL_ID *locSym, hlType t, Int off, byte regOff) 
  127. /* Creates a new stack identifier node of TYPE_BYTE_(UN)SIGN or
  128.  * TYPE_WORD_(UN)SIGN type.  Returns the index to this new entry.       */
  129. { Int idx;
  130.  
  131.     /* Check for entry in the table */
  132.     for (idx = 0; idx < locSym->csym; idx++)
  133.     {
  134.         if ((locSym->id[idx].id.bwId.off == off) &&
  135.             (locSym->id[idx].id.bwId.regOff == regOff))
  136.             return (idx);
  137.     }
  138.  
  139.     /* Not in table, create new identifier */
  140.     newIdent (locSym, t, STK_FRAME);
  141.     idx = locSym->csym - 1;
  142.     locSym->id[idx].id.bwId.regOff = regOff;
  143.     locSym->id[idx].id.bwId.off = off;
  144.     return (idx);
  145. }
  146.  
  147.  
  148. Int newIntIdxId (LOCAL_ID *locSym, int16 seg, int16 off, byte regi, 
  149.                  Int ix, hlType t)
  150. /* Checks if the entry exists in the locSym, if so, returns the idx to this
  151.  * entry; otherwise creates a new global identifier node of type 
  152.  * TYPE_WORD_(UN)SIGN and returns the index to this new entry.  
  153.  * Arguments: locSym : ptr to the local symbol table
  154.  *            seg: segment value for global variable
  155.  *            off: offset from segment
  156.  *            regi: indexed register into global variable
  157.  *            ix: index into icode array
  158.  *            t: HIGH_LEVEL type            */
  159. { Int idx;
  160.  
  161.     /* Check for entry in the table */
  162.     for (idx = 0; idx < locSym->csym; idx++)
  163.     {
  164.         if (/*(locSym->id[idx].type == t) &&   Not checking type */
  165.             (locSym->id[idx].id.bwGlb.seg == seg) &&  
  166.             (locSym->id[idx].id.bwGlb.off == off) &&  
  167.             (locSym->id[idx].id.bwGlb.regi == regi))
  168.             return (idx);
  169.     }
  170.  
  171.     /* Not in the table, create new identifier */
  172.     newIdent (locSym, t, GLB_FRAME);
  173.     idx = locSym->csym - 1;
  174.     locSym->id[idx].id.bwGlb.seg = seg;
  175.     locSym->id[idx].id.bwGlb.off = off;
  176.     locSym->id[idx].id.bwGlb.regi = regi;
  177.     return (idx);
  178. }
  179.  
  180.  
  181. Int newLongRegId (LOCAL_ID *locSym, hlType t, byte regH, byte regL, Int ix)
  182. /* Checks if the entry exists in the locSym, if so, returns the idx to this
  183.  * entry; otherwise creates a new register identifier node of type 
  184.  * TYPE_LONG_(UN)SIGN and returns the index to this new entry.  */
  185. { Int idx;
  186.  
  187.     /* Check for entry in the table */
  188.     for (idx = 0; idx < locSym->csym; idx++)
  189.     {
  190.         if (/*(locSym->id[idx].type == t) &&   Not checking type */
  191.             (locSym->id[idx].id.longId.h == regH) &&  
  192.             (locSym->id[idx].id.longId.l == regL))
  193.         {
  194.             /* Check for occurrence in the list */
  195.             if (inList (&locSym->id[idx].idx, ix)) 
  196.                 return (idx);
  197.             else
  198.             {
  199.                 /* Insert icode index in list */
  200.                 insertIdx (&locSym->id[idx].idx, ix);
  201.                 return (idx);
  202.             }
  203.         }
  204.     }
  205.  
  206.     /* Not in the table, create new identifier */
  207.     newIdent (locSym, t, REG_FRAME);
  208.     insertIdx (&locSym->id[locSym->csym-1].idx, ix);
  209.     idx = locSym->csym - 1;
  210.     locSym->id[idx].id.longId.h = regH;
  211.     locSym->id[idx].id.longId.l = regL;
  212.     return (idx);
  213. }
  214.  
  215.  
  216. static Int newLongGlbId (LOCAL_ID *locSym, int16 seg, int16 offH, int16 offL,
  217.                          Int ix, hlType t)
  218. /* Checks if the entry exists in the locSym, if so, returns the idx to this
  219.  * entry; otherwise creates a new global identifier node of type 
  220.  * TYPE_LONG_(UN)SIGN and returns the index to this new entry.  */
  221. { Int idx;
  222.  
  223.     /* Check for entry in the table */
  224.     for (idx = 0; idx < locSym->csym; idx++)
  225.     {
  226.         if (/*(locSym->id[idx].type == t) &&   Not checking type */
  227.             (locSym->id[idx].id.longGlb.seg == seg) &&  
  228.             (locSym->id[idx].id.longGlb.offH == offH) &&  
  229.             (locSym->id[idx].id.longGlb.offL == offL))
  230.             return (idx);
  231.     }
  232.  
  233.     /* Not in the table, create new identifier */
  234.     newIdent (locSym, t, GLB_FRAME);
  235.     idx = locSym->csym - 1;
  236.     locSym->id[idx].id.longGlb.seg = seg;
  237.     locSym->id[idx].id.longGlb.offH = offH;
  238.     locSym->id[idx].id.longGlb.offL = offL;
  239.     return (idx);
  240. }
  241.  
  242.  
  243. static Int newLongIdxId (LOCAL_ID *locSym, int16 seg, int16 offH, int16 offL,
  244.                          byte regi, Int ix, hlType t)
  245. /* Checks if the entry exists in the locSym, if so, returns the idx to this
  246.  * entry; otherwise creates a new global identifier node of type 
  247.  * TYPE_LONG_(UN)SIGN and returns the index to this new entry.  */
  248. { Int idx;
  249.  
  250.     /* Check for entry in the table */
  251.     for (idx = 0; idx < locSym->csym; idx++)
  252.     {
  253.         if (/*(locSym->id[idx].type == t) &&   Not checking type */
  254.             (locSym->id[idx].id.longGlb.seg == seg) &&  
  255.             (locSym->id[idx].id.longGlb.offH == offH) &&  
  256.             (locSym->id[idx].id.longGlb.offL == offL) &&  
  257.             (locSym->id[idx].id.longGlb.regi == regi))
  258.             return (idx);
  259.     }
  260.  
  261.     /* Not in the table, create new identifier */
  262.     newIdent (locSym, t, GLB_FRAME);
  263.     idx = locSym->csym - 1;
  264.     locSym->id[idx].id.longGlb.seg = seg;
  265.     locSym->id[idx].id.longGlb.offH = offH;
  266.     locSym->id[idx].id.longGlb.offL = offL;
  267.     locSym->id[idx].id.longGlb.regi = regi;
  268.     return (idx);
  269. }
  270.  
  271.  
  272. Int newLongStkId (LOCAL_ID *locSym, hlType t, Int offH, Int offL)
  273. /* Creates a new stack identifier node of type TYPE_LONG_(UN)SIGN.
  274.  * Returns the index to this entry. */
  275. { Int idx;
  276.  
  277.     /* Check for entry in the table */
  278.     for (idx = 0; idx < locSym->csym; idx++)
  279.     {
  280.         if ((locSym->id[idx].type == t) && 
  281.             (locSym->id[idx].id.longStkId.offH == offH) &&  
  282.             (locSym->id[idx].id.longStkId.offL == offL))
  283.             return (idx);
  284.     }
  285.  
  286.     /* Not in the table; flag as invalid offH and offL */ 
  287.     flagByteWordId (locSym, offH);
  288.     flagByteWordId (locSym, offL); 
  289.  
  290.     /* Create new identifier */
  291.     newIdent (locSym, t, STK_FRAME);
  292.     idx = locSym->csym - 1;
  293.     locSym->id[idx].id.longStkId.offH = offH;
  294.     locSym->id[idx].id.longStkId.offL = offL;
  295.     return (idx);
  296. }
  297.  
  298.  
  299. Int newLongId (LOCAL_ID *locSym, opLoc sd, PICODE pIcode, hlFirst f, Int ix,
  300.                operDu du, Int off)
  301. /* Returns the index to an appropriate long identifier. 
  302.  * Note: long constants should be checked first and stored as a long integer
  303.  *       number in an expression record.    */
  304. { Int idx;
  305.   PMEM pmH, pmL;
  306.  
  307.     if (f == LOW_FIRST)
  308.     {
  309.         pmL = (sd == SRC) ? &pIcode->ic.ll.src : &pIcode->ic.ll.dst;
  310.         pmH = (sd == SRC) ? &(pIcode+off)->ic.ll.src : &(pIcode+off)->ic.ll.dst;
  311.     }
  312.     else        /* HIGH_FIRST */
  313.     {
  314.         pmH = (sd == SRC) ? &pIcode->ic.ll.src : &pIcode->ic.ll.dst;
  315.         pmL = (sd == SRC) ? &(pIcode+off)->ic.ll.src : &(pIcode+off)->ic.ll.dst;
  316.     }
  317.  
  318.     if (pmL->regi == 0)                                 /* global variable */
  319.         idx = newLongGlbId (locSym, pmH->segValue, pmH->off, pmL->off, ix, 
  320.                             TYPE_LONG_SIGN); 
  321.  
  322.     else if (pmL->regi < INDEXBASE)                     /* register */
  323.     {
  324.         idx = newLongRegId (locSym, TYPE_LONG_SIGN, pmH->regi, pmL->regi, ix);
  325.         if (f == HIGH_FIRST)
  326.             setRegDU (pIcode, pmL->regi, du);   /* low part */
  327.         else
  328.             setRegDU (pIcode, pmH->regi, du);   /* high part */
  329.     }
  330.  
  331.     else if (pmL->off) {                                /* offset */
  332.         if ((pmL->seg == rSS) && (pmL->regi == INDEXBASE + 6)) /* idx on bp */
  333.             idx = newLongStkId (locSym, TYPE_LONG_SIGN, pmH->off, pmL->off);
  334.         else if ((pmL->seg == rDS) && (pmL->regi == INDEXBASE + 7))   /* bx */
  335.         {                                       /* glb var indexed on bx */
  336.             idx = newLongIdxId (locSym, pmH->segValue, pmH->off, pmL->off,
  337.                                 rBX, ix, TYPE_LONG_SIGN); 
  338.             setRegDU (pIcode, rBX, E_USE);
  339.         }
  340.         else                                            /* idx <> bp, bx */
  341.             printf ("long not supported, idx <> bp\n");
  342.     }
  343.  
  344.     else  /* (pm->regi >= INDEXBASE && pm->off = 0) => indexed && no off */ 
  345.         printf ("long not supported, idx && no off\n");
  346.  
  347.     return (idx);
  348. }
  349.  
  350.  
  351. boolT checkLongEq (LONG_STKID_TYPE longId, PICODE pIcode, Int i, Int idx, 
  352.                   PPROC pProc, COND_EXPR **rhs, COND_EXPR **lhs, Int off)
  353. /* Checks whether the long stack identifier is equivalent to the source or
  354.  * destination operands of pIcode and pIcode+1 (ie. these are LOW_LEVEL
  355.  * icodes at present).  If so, returns the rhs and lhs of this instruction.
  356.  * Arguments: longId    : long stack identifier
  357.  *            pIcode    : ptr to first LOW_LEVEL icode instruction
  358.  *            i         : idx into local identifier table for longId
  359.  *            idx       : idx into icode array
  360.  *            pProc     : ptr to current procedure record 
  361.  *            rhs, lhs  : return expressions if successful. */
  362. { PMEM pmHdst, pmLdst, pmHsrc, pmLsrc;  /* pointers to LOW_LEVEL icodes */
  363.  
  364.     pmHdst = &pIcode->ic.ll.dst;
  365.     pmLdst = &(pIcode+off)->ic.ll.dst;
  366.     pmHsrc = &pIcode->ic.ll.src;
  367.     pmLsrc = &(pIcode+off)->ic.ll.src;
  368.  
  369.     if ((longId.offH == pmHdst->off) && (longId.offL == pmLdst->off))
  370.     {
  371.         *lhs = idCondExpLongIdx (i);
  372.         if ((pIcode->ic.ll.flg & NO_SRC) != NO_SRC)
  373.             *rhs = idCondExpLong (&pProc->localId, SRC, pIcode, HIGH_FIRST, 
  374.                                   idx, E_USE, off);
  375.         return (TRUE);
  376.     }
  377.     else if ((longId.offH == pmHsrc->off) && (longId.offL == pmLsrc->off))
  378.     {
  379.         *lhs = idCondExpLong (&pProc->localId, DST, pIcode, HIGH_FIRST, idx,
  380.                               E_DEF, off);
  381.         *rhs = idCondExpLongIdx (i);
  382.         return (TRUE);
  383.     }
  384.     return (FALSE);
  385. }
  386.  
  387.  
  388. boolT checkLongRegEq (LONGID_TYPE longId, PICODE pIcode, Int i, Int idx, 
  389.                   PPROC pProc, COND_EXPR **rhs, COND_EXPR **lhs, Int off)
  390. /* Checks whether the long stack identifier is equivalent to the source or
  391.  * destination operands of pIcode and pIcode+1 (ie. these are LOW_LEVEL
  392.  * icodes at present).  If so, returns the rhs and lhs of this instruction.
  393.  * Arguments: longId    : long stack identifier
  394.  *            pIcode    : ptr to first LOW_LEVEL icode instruction
  395.  *            i         : idx into local identifier table for longId
  396.  *            idx       : idx into icode array
  397.  *            pProc     : ptr to current procedure record 
  398.  *            rhs, lhs  : return expressions if successful. */
  399. { PMEM pmHdst, pmLdst, pmHsrc, pmLsrc;  /* pointers to LOW_LEVEL icodes */
  400.  
  401.     pmHdst = &pIcode->ic.ll.dst;
  402.     pmLdst = &(pIcode+off)->ic.ll.dst;
  403.     pmHsrc = &pIcode->ic.ll.src;
  404.     pmLsrc = &(pIcode+off)->ic.ll.src;
  405.  
  406.     if ((longId.h == pmHdst->regi) && (longId.l == pmLdst->regi))
  407.     {
  408.         *lhs = idCondExpLongIdx (i);
  409.         if ((pIcode->ic.ll.flg & NO_SRC) != NO_SRC)
  410.             *rhs = idCondExpLong (&pProc->localId, SRC, pIcode, HIGH_FIRST, 
  411.                                   idx, E_USE, off);
  412.         return (TRUE);
  413.     }
  414.     else if ((longId.h == pmHsrc->regi) && (longId.l == pmLsrc->regi))
  415.     {
  416.         *lhs = idCondExpLong (&pProc->localId, DST, pIcode, HIGH_FIRST, idx,
  417.                               E_DEF, off);
  418.         *rhs = idCondExpLongIdx (i);
  419.         return (TRUE);
  420.     }
  421.     return (FALSE);
  422. }
  423.  
  424.  
  425.  
  426. byte otherLongRegi (byte regi, Int idx, LOCAL_ID *locTbl)
  427. /* Given an index into the local identifier table for a long register
  428.  * variable, determines whether regi is the high or low part, and returns
  429.  * the other part   */
  430. { ID *id;
  431.  
  432.     id = &locTbl->id[idx];
  433.     if ((id->loc == REG_FRAME) && ((id->type == TYPE_LONG_SIGN) ||
  434.         (id->type == TYPE_LONG_UNSIGN)))
  435.     {
  436.         if (id->id.longId.h == regi)
  437.             return (id->id.longId.l);
  438.         else if (id->id.longId.l == regi)
  439.             return (id->id.longId.h);
  440.     }
  441.     return 0;            // Cristina: please check this!
  442. }
  443.  
  444.  
  445. void propLongId (LOCAL_ID *locid, byte regL, byte regH, char *name)
  446. /* Checks if the registers regL and regH have been used independently in
  447.  * the local identifier table.  If so, macros for these registers are
  448.  * placed in the local identifier table, as these registers belong to a 
  449.  * long register identifier.    */
  450. { Int i;
  451.   ID *id;
  452.  
  453.     for (i = 0; i < locid->csym; i++)
  454.     {
  455.         id = &locid->id[i];
  456.         if ((id->type == TYPE_WORD_SIGN) || (id->type == TYPE_WORD_UNSIGN)) 
  457.         {
  458.             if (id->id.regi == regL)
  459.             {
  460.                 strcpy (id->name, name);
  461.                 strcpy (id->macro, "LO");
  462.                 id->hasMacro = TRUE;
  463.                 id->illegal = TRUE;
  464.             }
  465.             else if (id->id.regi == regH)
  466.             {
  467.                 strcpy (id->name, name);
  468.                 strcpy (id->macro, "HI");
  469.                 id->hasMacro = TRUE;
  470.                 id->illegal = TRUE;
  471.             }
  472.         }    
  473.     }
  474. }
  475.