From e50c00c8564f7feac91bd902f4b1e4e535a423fd Mon Sep 17 00:00:00 2001 From: Randy Heit Date: Wed, 21 Aug 2013 22:36:46 -0500 Subject: [PATCH] Add generic integer setters and getters for PInt and PFloat types --- src/dobjtype.cpp | 126 +++++++++++++++++++++++++++++++++++++++++++++++ src/dobjtype.h | 12 +++++ 2 files changed, 138 insertions(+) diff --git a/src/dobjtype.cpp b/src/dobjtype.cpp index 68e5e8b31..22dec47a8 100644 --- a/src/dobjtype.cpp +++ b/src/dobjtype.cpp @@ -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 @@ -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 *****************************************************************/ 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 ****************************************************************/ IMPLEMENT_CLASS(PString) diff --git a/src/dobjtype.h b/src/dobjtype.h index 17748b13b..beab89712 100644 --- a/src/dobjtype.h +++ b/src/dobjtype.h @@ -167,6 +167,12 @@ public: PType(unsigned int size, unsigned int align); 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 // above table, any type is identified by at most two characteristics. Each // type that implements this function will cast these to the appropriate type. @@ -219,6 +225,9 @@ class PInt : public PBasicType public: PInt(unsigned int size, bool unsign); + virtual void SetValue(void *addr, int val); + virtual int GetValueInt(void *addr); + bool Unsigned; protected: PInt(); @@ -229,6 +238,9 @@ class PFloat : public PBasicType DECLARE_CLASS(PFloat, PBasicType); public: PFloat(unsigned int size); + + virtual void SetValue(void *addr, int val); + virtual int GetValueInt(void *addr); protected: PFloat(); };