Add generic integer setters and getters for PInt and PFloat types

This commit is contained in:
Randy Heit 2013-08-21 22:36:46 -05:00
parent 6198c000af
commit e50c00c856
2 changed files with 138 additions and 0 deletions

View file

@ -206,6 +206,29 @@ PType::~PType()
{ {
} }
//==========================================================================
//
// PType :: SetValue
//
//==========================================================================
void PType::SetValue(void *addr, int val)
{
assert(0 && "Cannot set value for this type");
}
//==========================================================================
//
// PType :: GetValue
//
//==========================================================================
int PType::GetValueInt(void *addr)
{
assert(0 && "Cannot get value for this type");
return 0;
}
//========================================================================== //==========================================================================
// //
// PType :: IsMatch // PType :: IsMatch
@ -360,6 +383,69 @@ PInt::PInt(unsigned int size, bool unsign)
{ {
} }
//==========================================================================
//
// PInt :: SetValue
//
//==========================================================================
void PInt::SetValue(void *addr, int val)
{
assert(((intptr_t)addr & (Align - 1)) == 0 && "unaligned address");
if (Size == 4)
{
*(int *)addr = val;
}
else if (Size == 1)
{
*(BYTE *)addr = val;
}
else if (Size == 2)
{
*(WORD *)addr = val;
}
else if (Size == 8)
{
*(QWORD *)addr = val;
}
else
{
assert(0 && "Unhandled integer size");
}
}
//==========================================================================
//
// PInt :: GetValueInt
//
//==========================================================================
int PInt::GetValueInt(void *addr)
{
assert(((intptr_t)addr & (Align - 1)) == 0 && "unaligned address");
if (Size == 4)
{
return *(int *)addr;
}
else if (Size == 1)
{
return Unsigned ? *(BYTE *)addr : *(SBYTE *)addr;
}
else if (Size == 2)
{
return Unsigned ? *(WORD *)addr : *(SWORD *)addr;
}
else if (Size == 8)
{ // truncated output
return (int)*(QWORD *)addr;
}
else
{
assert(0 && "Unhandled integer size");
return 0;
}
}
/* PFloat *****************************************************************/ /* PFloat *****************************************************************/
IMPLEMENT_CLASS(PFloat) IMPLEMENT_CLASS(PFloat)
@ -386,6 +472,46 @@ PFloat::PFloat(unsigned int size)
{ {
} }
//==========================================================================
//
// PFloat :: SetValue
//
//==========================================================================
void PFloat::SetValue(void *addr, int val)
{
assert(((intptr_t)addr & (Align - 1)) == 0 && "unaligned address");
if (Size == 4)
{
*(float *)addr = (float)val;
}
else
{
assert(Size == 8);
*(double *)addr = val;
}
}
//==========================================================================
//
// PFloat :: GetValueInt
//
//==========================================================================
int PFloat::GetValueInt(void *addr)
{
assert(((intptr_t)addr & (Align - 1)) == 0 && "unaligned address");
if (Size == 4)
{
return xs_ToInt(*(float *)addr);
}
else
{
assert(Size == 8);
return xs_ToInt(*(double *)addr);
}
}
/* PString ****************************************************************/ /* PString ****************************************************************/
IMPLEMENT_CLASS(PString) IMPLEMENT_CLASS(PString)

View file

@ -167,6 +167,12 @@ public:
PType(unsigned int size, unsigned int align); PType(unsigned int size, unsigned int align);
virtual ~PType(); virtual ~PType();
// Sets the value of a variable of this type at (addr)
virtual void SetValue(void *addr, int val);
// Gets the value of a variable of this type at (addr)
virtual int GetValueInt(void *addr);
// Returns true if this type matches the two identifiers. Referring to the // Returns true if this type matches the two identifiers. Referring to the
// above table, any type is identified by at most two characteristics. Each // above table, any type is identified by at most two characteristics. Each
// type that implements this function will cast these to the appropriate type. // type that implements this function will cast these to the appropriate type.
@ -219,6 +225,9 @@ class PInt : public PBasicType
public: public:
PInt(unsigned int size, bool unsign); PInt(unsigned int size, bool unsign);
virtual void SetValue(void *addr, int val);
virtual int GetValueInt(void *addr);
bool Unsigned; bool Unsigned;
protected: protected:
PInt(); PInt();
@ -229,6 +238,9 @@ class PFloat : public PBasicType
DECLARE_CLASS(PFloat, PBasicType); DECLARE_CLASS(PFloat, PBasicType);
public: public:
PFloat(unsigned int size); PFloat(unsigned int size);
virtual void SetValue(void *addr, int val);
virtual int GetValueInt(void *addr);
protected: protected:
PFloat(); PFloat();
}; };