home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / POINTER.C < prev    next >
Text File  |  1996-04-16  |  14KB  |  244 lines

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*   pointer.c                                                               */
  4. /*                                                                           */
  5. /* Description:                                                              */
  6. /*                                                                           */
  7. /*   pointer handling routines.                                              */
  8. /*                                                                           */
  9. /*                                                                           */
  10. /* History:                                                                  */
  11. /*                                                                           */
  12. /*   08/15/91 Creation of 32-bit SD386.                                      */
  13. /*                                                                           */
  14. /*...Release 1.00 (Pre-release 1)                                            */
  15. /*...                                                                        */
  16. /*... 07/09/91  205   srinivas  Hooking up of register variables.            */
  17. /*... 07/26/91  219   srinivas  handling near pointers.                      */
  18. /*... 09/25/91  240   Srinivas  recursive PLX based variables.               */
  19. /*...                                                                        */
  20. /*...Release 1.00 (Pre-release 1.08 10/10/91)                                */
  21. /*...                                                                        */
  22. /*... 02/07/92  512   Srinivas  Handle Toronto "C" userdefs.                 */
  23. /*...                                                                        */
  24. /*...Release 1.02 (10/22/92)                                                 */
  25. /*...                                                                        */
  26. /*... 03/03/93  813   Joe       Revised types handling for HL03.             */
  27. /*...                                                                        */
  28. /*****************************************************************************/
  29.  
  30. #include "all.h"                        /* SD386 include files               */
  31.  
  32. /**External declararions******************************************************/
  33.  
  34. extern uint         ExprTid;            /* Set by ParseExpr and findlvar     */
  35. extern SCOPE        ExprScope;          /* Set by ParseExpr and findlvar.    */
  36. extern ushort       DgroupDS;           /* ds for handling near pointers  219*/
  37.  
  38. /*****************************************************************************/
  39. /*  DerefPointer()                                                           */
  40. /*                                                                           */
  41. /* Description:                                                              */
  42. /*   Dereference a pointer.                                                  */
  43. /*                                                                           */
  44. /* Parameters:                                                               */
  45. /*                                                                           */
  46. /*   ptr       input - pointer that we want to dereference.                  */
  47. /*   mid       input - module id.                                            */
  48. /*                                                                           */
  49. /* Return:                                                                   */
  50. /*   *p        the new pointer.                                              */
  51. /*   NULL                                                                    */
  52. /*                                                                           */
  53. /* Assumptions:                                                              */
  54. /*                                                                           */
  55. /*   none.                                                                   */
  56. /*                                                                           */
  57. /*****************************************************************************/
  58.  uint                                   /*                                   */
  59. DerefPointer( uint ptr , uint mid )     /*                            240 112*/
  60. {                                       /*                                   */
  61.  uint     sfx;                          /* stack frame index.                */
  62.  uint    *p;                            /*                                   */
  63.  uint     read;                         /* number of bytes read by DBGet     */
  64.  uint     size;                         /*                                   */
  65.  uchar    type;                         /* type of pointer                219*/
  66.  uint     derefPtr;                     /* dereferneced pointer           219*/
  67.  int      IsStackAddr = 0;
  68.  
  69.  sfx = StackFrameIndex( ExprScope );
  70.  IsStackAddr = TestBit(ptr,STACKADDRBIT);
  71.  if( IsStackAddr )
  72.  {
  73.    if( sfx == 0 )
  74.      goto BadAddr;
  75.    ptr = StackBPRelToAddr( ptr , sfx );
  76.    if( ptr == NULL )
  77.      goto BadAddr;
  78.  }
  79.  else
  80.   if( (ptr >> REGADDCHECKPOS) == REGISTERTYPEADDR ){                    /*205*/
  81.      if( sfx != 1 )                     /* If not in the executing frame  112*/
  82.          goto BadAddr;                                                  /*112*/
  83.  }                                                                      /*112*/
  84.  
  85.  type = GetPtrType(mid,ExprTid);                                     /*813240*/
  86.                                         /* get the type of pointer        219*/
  87.  if ( type == PTR_0_16 )                /*                                219*/
  88.    size = 2;                            /* set the size of ptr depending  219*/
  89.  else                                   /* on type of ptr.                219*/
  90.    size = 4;                            /*                                219*/
  91.  
  92. /*****************************************************************************/
  93. /* At this point, we are ready to return the fruits of our labor. We get     */
  94. /* "pointer size" bytes from the users stack at location ptr if we can.      */
  95. /*                                                                           */
  96. /*****************************************************************************/
  97.  p = (uint *)DBGet(ptr, size, &read );
  98.  if( p == NULL )
  99.   goto BadAddr;
  100.  
  101.  
  102.  derefPtr = (uint) *p;                 /* get the value of deref pointer 219*/
  103.  
  104. /*****************************************************************************/
  105. /* convert the deref pointer into correct flat adddress depending on type 219*/
  106. /*****************************************************************************/
  107.  switch (type)                                                          /*219*/
  108.  {                                                                      /*219*/
  109.    case PTR_0_16:                                                       /*219*/
  110.      derefPtr = Data_SelOff2Flat(DgroupDS,LoFlat(derefPtr));
  111.      break;                                                             /*219*/
  112.                                                                         /*219*/
  113.    case PTR_16_16:                                                      /*219*/
  114.      derefPtr = Data_SelOff2Flat( HiFlat(derefPtr) , LoFlat(derefPtr) );
  115.      break;                                                             /*219*/
  116.  }                                                                      /*219*/
  117.                                                                         /*219*/
  118.  return( derefPtr );                                                    /*219*/
  119.  
  120. BadAddr:
  121.  return( NULL );
  122. }
  123.  
  124. /*****************************************************************************/
  125. /*  ResolveAddr()                                                            */
  126. /*                                                                           */
  127. /* Description:                                                              */
  128. /*   Resolves a address to the correct flat address.                         */
  129. /*                                                                           */
  130. /* Parameters:                                                               */
  131. /*                                                                           */
  132. /*   addr      input - the address to be resolved.                           */
  133. /*   dfp       input - -> to the dfile node.                                 */
  134. /*   typeno    input - the typeno of the variable.                           */
  135. /*                                                                           */
  136. /* Return:                                                                   */
  137. /*   FlatAddr  resolved correct address.                                     */
  138. /*                                                                           */
  139. /* Assumptions:                                                              */
  140. /*                                                                           */
  141. /*****************************************************************************/
  142.  uint                                                                   /*219*/
  143. ResolveAddr( uint Addr , DFILE *dfp , uint typeno)                      /*219*/
  144. {                                                                       /*219*/
  145.   uint FlatAddr;                                                        /*219*/
  146.                                                                         /*219*/
  147.   FlatAddr = Addr;                                                      /*219*/
  148.   switch (GetPtrType(dfp->mid,typeno) )                              /*813219*/
  149.   {                                                                     /*219*/
  150.     case PTR_0_16:                                                      /*219*/
  151.       FlatAddr = Data_SelOff2Flat(DgroupDS,LoFlat(FlatAddr));
  152.       break;                                                            /*219*/
  153.                                                                         /*219*/
  154.     case PTR_16_16:                                                     /*219*/
  155.       FlatAddr = Data_SelOff2Flat( HiFlat(Addr),LoFlat(Addr) );
  156.       break;                                                            /*219*/
  157.   }                                                                     /*219*/
  158.   return(FlatAddr);                                                     /*219*/
  159. }
  160.  
  161.  
  162. /*****************************************************************************/
  163. /* GetPtrType()                                                           813*/
  164. /*                                                                           */
  165. /* Description:                                                              */
  166. /*                                                                           */
  167. /*   Resolve a typeno to its pointer type.                                   */
  168. /*                                                                           */
  169. /* Parameters:                                                               */
  170. /*                                                                           */
  171. /*   mid        input - module id.                                           */
  172. /*   typeno     input - primitive or complex pointer typeno.                 */
  173. /*                                                                           */
  174. /* Return:                                                                   */
  175. /*              PTR_0_16.         16 bit near.                               */
  176. /*              PTR_0_32.         32 bit near.                               */
  177. /*              PTR_16_16.        16 bit far.                                */
  178. /*                                                                           */
  179. /* Assumptions:                                                              */
  180. /*                                                                           */
  181. /*   typeno must be a primitive pointer type or a complex TD_POINTER.        */
  182. /*                                                                           */
  183. /*****************************************************************************/
  184. UINT GetPtrType( UINT mid, USHORT typeno )
  185. {
  186.  TD_POINTER  *PtrToBaseTypeRec;
  187.  
  188. ReCycle:
  189.  if( typeno < 512 )
  190.  {
  191.   /***************************************************************************/
  192.   /* Handle primitive pointer types.                                         */
  193.   /***************************************************************************/
  194.   switch(typeno)
  195.   {
  196.  
  197.     case TYPE_PCHAR:
  198.     case TYPE_PSHORT:
  199.     case TYPE_PLONG:
  200.     case TYPE_PUCHAR:
  201.     case TYPE_PUSHORT:
  202.     case TYPE_PULONG:
  203.     case TYPE_PFLOAT:
  204.     case TYPE_PDOUBLE:
  205.     case TYPE_PLDOUBLE:
  206.     case TYPE_PVOID:
  207.      return(PTR_0_32);
  208.  
  209.     case TYPE_N16PCHAR:
  210.     case TYPE_N16PSHORT:
  211.     case TYPE_N16PLONG:
  212.     case TYPE_N16PUCHAR:
  213.     case TYPE_N16PUSHORT:
  214.     case TYPE_N16PULONG:
  215.     case TYPE_N16PFLOAT:
  216.     case TYPE_N16PDOUBLE:
  217.     case TYPE_N16PLDOUBLE:
  218.     case TYPE_N16PVOID:
  219.      return(PTR_0_16);
  220.  
  221.    case TYPE_FPCHAR:
  222.    case TYPE_FPSHORT:
  223.    case TYPE_FPLONG:
  224.    case TYPE_FPUCHAR:
  225.    case TYPE_FPUSHORT:
  226.    case TYPE_FPULONG:
  227.    case TYPE_FPFLOAT:
  228.    case TYPE_FPDOUBLE:
  229.    case TYPE_FPLDOUBLE:
  230.    case TYPE_FPVOID:
  231.     return(PTR_16_16);
  232.  
  233.    default:
  234.     return(PTR_0_32);
  235.   }
  236.  }
  237.  
  238.  /****************************************************************************/
  239.  /* Handle TD_POINTER type.                                                  */
  240.  /****************************************************************************/
  241.  PtrToBaseTypeRec = (TD_POINTER*)QbasetypeRec(mid, typeno);
  242.  return( PtrToBaseTypeRec->Flags );
  243. }
  244.