Ignore:
Timestamp:
Aug 16, 2007, 7:55:02 PM (17 years ago)
Author:
dai_9181
Message:

Binaryクラスを追加

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/abdev/BasicCompiler_Common/include/NativeCode.h

    r282 r287  
    66
    77#include <BoostSerializationSupport.h>
     8
     9#include <Binary.h>
    810
    911class UserProc;
     
    219221typedef std::vector<SourceLine> SourceLines;
    220222
    221 class NativeCode
     223class NativeCode : public Binary
    222224{
    223     int allocateSize;
    224     char *codeBuffer;
    225     int size;
    226 
    227225    // リンカで解決しなければならないスケジュール
    228226    Schedules schedules;
     
    234232private:
    235233    friend class boost::serialization::access;
    236     BOOST_SERIALIZATION_SPLIT_MEMBER();
    237     template<class Archive> void load(Archive& ar, const unsigned int version)
     234    template<class Archive> void serialize(Archive& ar, const unsigned int version)
    238235    {
    239236        trace_for_serialize( "serializing(load) - NativeCode" );
    240237
    241         std::string code;
    242         ar & BOOST_SERIALIZATION_NVP( code );
    243         ar & BOOST_SERIALIZATION_NVP( size );
     238        ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( Binary );
    244239        ar & BOOST_SERIALIZATION_NVP( schedules );
    245240        ar & BOOST_SERIALIZATION_NVP( sourceLines );
    246 
    247         // 読み込み後の処理
    248         Realloc( size );
    249         for( int i=0; i<size; i++ )
    250         {
    251             ULONG_PTR l1 = ( ( code[i*3] >= 'a' ) ? ( code[i*3] - 'a' + 0x0a ) : ( code[i*3] - '0' ) ) * 0x10;
    252             ULONG_PTR l2 = ( code[i*3+1] >= 'a' ) ? ( code[i*3+1] - 'a' + 0x0a ) : ( code[i*3+1] - '0' );
    253             ULONG_PTR l = l1 + l2;
    254             codeBuffer[i] = static_cast<char>(l);
    255         }
    256     }
    257     template<class Archive> void save(Archive& ar, const unsigned int version) const
    258     {
    259         trace_for_serialize( "serializing(save) - NativeCode" );
    260 
    261         // 保存準備
    262         char *tempCode = (char *)calloc( (size+1) * 3, 1 );
    263         for( int i=0; i<size; i++ )
    264         {
    265             char temp[32];
    266             sprintf( temp, "%02x,", (unsigned char)codeBuffer[i] );
    267             tempCode[i*3] = temp[0];
    268             tempCode[i*3+1] = temp[1];
    269             tempCode[i*3+2] = temp[2];
    270         }
    271 
    272         std::string code = tempCode;
    273         free( tempCode );
    274 
    275         ar & BOOST_SERIALIZATION_NVP( code );
    276         ar & BOOST_SERIALIZATION_NVP( size );
    277         ar & BOOST_SERIALIZATION_NVP( schedules );
    278         ar & BOOST_SERIALIZATION_NVP( sourceLines );
    279     }
    280 
    281 
    282     void Realloc( int newSize )
    283     {
    284         if( allocateSize < newSize + 8192 )
    285         {
    286             while( allocateSize < newSize + 8192 )
    287             {
    288                 allocateSize += 8192;
    289             }
    290             codeBuffer = (char *)realloc( codeBuffer, allocateSize );
    291         }
    292241    }
    293242
    294243public:
    295244    NativeCode()
    296         : allocateSize( 8192 )
    297         , codeBuffer( (char *)malloc( allocateSize ) )
    298         , size( 0 )
     245        : Binary()
    299246    {
    300247    }
    301248    NativeCode( const NativeCode &nativeCode )
    302         : allocateSize( 8192 )
    303         , codeBuffer( (char *)malloc( allocateSize ) )
    304         , size( 0 )
    305     {
    306         Put( nativeCode );
     249        : Binary()
     250    {
     251        PutEx( nativeCode );
    307252    }
    308253    NativeCode( const char *codeBuffer, int size )
    309         : allocateSize( 8192 )
    310         , codeBuffer( (char *)malloc( allocateSize ) )
    311         , size( 0 )
    312     {
    313         Put( codeBuffer, size );
     254        : Binary( codeBuffer, size )
     255    {
    314256    }
    315257    ~NativeCode()
    316258    {
    317         free( codeBuffer );
    318     }
    319     void Clear()
    320     {
    321         size = 0;
    322259    }
    323260
     
    325262    {
    326263        Clear();
    327         Put( nativeCode );
    328     }
    329 
    330     const char *GetCodeBuffer() const
    331     {
    332         return codeBuffer;
    333     }
    334     int GetSize() const
    335     {
    336         return size;
    337     }
     264        PutEx( nativeCode );
     265    }
     266
    338267    const Schedules &GetSchedules() const
    339268    {
     
    341270    }
    342271
    343     long GetLong( int codePos ) const
    344     {
    345         return *(long *)(this->codeBuffer+codePos);
    346     }
    347 
    348     void Overwrite( int codePos, char c )
    349     {
    350         codeBuffer[codePos] = c;
    351     }
    352     void Overwrite( int codePos, long newLongValue )
    353     {
    354         *(long *)(this->codeBuffer+codePos) = newLongValue;
    355     }
    356 
    357     void Put( const char *codeBuffer, int size )
    358     {
    359         Realloc( this->size + size );
    360 
    361         memcpy( this->codeBuffer + this->size, codeBuffer, size );
    362         this->size += size;
    363     }
    364     void Put( const NativeCode &nativeCode );
    365     void Put( _int64 i64data )
    366     {
    367         Put( (const char *)(&i64data), sizeof(_int64) );
    368     }
    369     void Put( long l, Schedule::Type scheduleType = Schedule::None )
     272    void PutEx( const NativeCode &nativeCode );
     273    void PutEx( long l, Schedule::Type scheduleType )
    370274    {
    371275        if( scheduleType != Schedule::None )
    372276        {
    373             schedules.push_back( Schedule( scheduleType, size ) );
    374         }
    375 
    376         *((long *)(codeBuffer+size))=l;
    377         size += sizeof(long);
     277            schedules.push_back( Schedule( scheduleType, GetSize() ) );
     278        }
     279
     280        Put( l );
    378281    }
    379282    void PutUserProcSchedule( const UserProc *pUserProc, bool isCall );
    380283    void PutDllProcSchedule( const DllProc *pDllProc );
    381284    void PutVtblSchedule( const CClass *pClass );
    382     void Put( short s )
    383     {
    384         Put( (const char *)(&s), sizeof(short) );
    385     }
    386     void Put( char c )
    387     {
    388         Realloc( size + 1 );
    389         codeBuffer[size++] = c;
    390     }
    391285
    392286    const SourceLines &GetSourceLines() const
Note: See TracChangeset for help on using the changeset viewer.