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

  1. // Object oriented icode code
  2.  
  3. #include <stdlib.h>
  4. #include <malloc.h>
  5. #include <memory.h>
  6.  
  7. #include "types.h"        // Common types like byte, etc
  8. #include "ast.h"        // Some icode types depend on these
  9. #include "icode.h"
  10.  
  11. void   *reallocVar(void *p, Int newsize);                   /* frontend.c !?   */
  12. /* Make this bigger to overcome memory allocation problems! */
  13. #define ICODE_DELTA 250        // Amount to allocate for new chunk
  14.  
  15.  
  16. CIcodeRec::CIcodeRec()
  17. {
  18.     numIcode = 0;
  19.     alloc = 0;
  20.     icode = 0;            // Initialise the pointer to 0
  21. }
  22.  
  23. CIcodeRec::~CIcodeRec()
  24. {
  25.     if (icode)
  26.     {
  27.         free(icode);
  28.     }
  29. }
  30.  
  31. PICODE CIcodeRec::addIcode(PICODE pIcode)
  32.  
  33. /* Copies the icode that is pointed to by pIcode to the icode array.
  34.  * If there is need to allocate extra memory, it is done so, and
  35.  * the alloc variable is adjusted.        */
  36. {
  37.     PICODE resIcode;
  38.  
  39.     if (numIcode == alloc)
  40.     {
  41.         alloc += ICODE_DELTA;
  42.         icode = (PICODE)reallocVar(icode, alloc * sizeof(ICODE));
  43.         memset (&icode[numIcode], 0, ICODE_DELTA * sizeof(ICODE));
  44.  
  45.     }
  46.     resIcode = (PICODE)memcpy (&icode[numIcode], pIcode, 
  47.                                sizeof(ICODE));
  48.     numIcode++;
  49.     return (resIcode);
  50. }
  51.  
  52. PICODE CIcodeRec::GetFirstIcode()
  53. {
  54.     return icode;
  55. }
  56.  
  57. /* Don't need this; just pIcode++ since array is guaranteed to be contiguous
  58. PICODE CIcodeRec::GetNextIcode(PICODE pCurIcode)
  59. {
  60.     int idx = pCurIcode - icode;        // Current index
  61.     ASSERT(idx+1 < numIcode);
  62.     return &icode[idx+1];
  63. }
  64. */
  65.  
  66. boolT CIcodeRec::IsValid(PICODE pCurIcode)
  67. {
  68.     int idx = pCurIcode - icode;        // Current index
  69.     return idx < numIcode;
  70. }
  71.  
  72. int CIcodeRec::GetNumIcodes()
  73. {
  74.     return numIcode;
  75. }
  76.  
  77. void CIcodeRec::SetInBB(int start, int end, struct _BB* pnewBB)
  78. {
  79.     for (int i = start; i <= end; i++)
  80.     icode[i].inBB = pnewBB;
  81. }
  82.  
  83. void CIcodeRec::SetImmediateOp(int ip, dword dw)
  84. {
  85.     icode[ip].ic.ll.immed.op = dw;
  86. }
  87.  
  88. void CIcodeRec::SetLlFlag(int ip, dword flag)
  89. {
  90.     icode[ip].ic.ll.flg |= flag;
  91. }
  92.  
  93. dword CIcodeRec::GetLlFlag(int ip)
  94. {
  95.     return icode[ip].ic.ll.flg;
  96. }
  97.  
  98. void CIcodeRec::ClearLlFlag(int ip, dword flag)
  99. {
  100.     icode[ip].ic.ll.flg &= (~flag);
  101. }
  102.  
  103. void CIcodeRec::SetLlInvalid(int ip, boolT fInv)
  104. {
  105.     icode[ip].invalid = fInv;
  106. }
  107.  
  108. dword CIcodeRec::GetLlLabel(int ip)
  109. {
  110.     return icode[ip].ic.ll.label;
  111. }
  112.  
  113. llIcode CIcodeRec::GetLlOpcode(int ip)
  114. {
  115.     return icode[ip].ic.ll.opcode;
  116. }
  117.  
  118.  
  119. boolT CIcodeRec::labelSrch(dword target, Int *pIndex)
  120. /* labelSrchRepl - Searches the icodes for instruction with label = target, and
  121.     replaces *pIndex with an icode index */
  122. {
  123.     Int  i;
  124.  
  125.     for (i = 0; i < numIcode; i++)
  126.     {
  127.         if (icode[i].ic.ll.label == target)
  128.         {
  129.             *pIndex = i;
  130.             return TRUE;
  131.         }
  132.     }
  133.     return FALSE;
  134. }
  135.  
  136. PICODE CIcodeRec::GetIcode(int ip)
  137. {
  138.     return &icode[ip];
  139. }
  140.  
  141.  
  142.  
  143.  
  144.