[224] | 1 | #pragma once
|
---|
| 2 |
|
---|
| 3 | #include <vector>
|
---|
| 4 |
|
---|
[228] | 5 | #include <jenga/include/common/Exception.h>
|
---|
| 6 |
|
---|
[225] | 7 | #include <BoostSerializationSupport.h>
|
---|
| 8 |
|
---|
[232] | 9 | void ObpPlus( int step = 1 );
|
---|
[228] | 10 |
|
---|
[244] | 11 | class UserProc;
|
---|
| 12 |
|
---|
[224] | 13 | class Schedule
|
---|
| 14 | {
|
---|
| 15 | public:
|
---|
| 16 | enum Type
|
---|
| 17 | {
|
---|
[237] | 18 | None = 10000,
|
---|
[225] | 19 | GlobalVar, // グローバル変数スケジュール
|
---|
[237] | 20 | DataTable, // データテーブル スケジュール
|
---|
[225] | 21 | Relocation, // リロケーション情報スケジュール
|
---|
[244] | 22 | UserProc, // ユーザ定義関数呼び出し側スケジュール
|
---|
| 23 | AddressOf, // ユーザ定義関数位置スケジュール
|
---|
[224] | 24 | };
|
---|
| 25 |
|
---|
| 26 | private:
|
---|
| 27 | Type type;
|
---|
[244] | 28 | long offset;
|
---|
[224] | 29 |
|
---|
[244] | 30 | union{
|
---|
| 31 | LONG_PTR lpValue;
|
---|
| 32 | const ::UserProc *pUserProc;
|
---|
[250] | 33 | const ::DllProc *pDllProc;
|
---|
[244] | 34 | };
|
---|
| 35 |
|
---|
[224] | 36 | // XMLシリアライズ用
|
---|
| 37 | private:
|
---|
| 38 | friend class boost::serialization::access;
|
---|
| 39 | template<class Archive> void serialize(Archive& ar, const unsigned int version)
|
---|
| 40 | {
|
---|
[256] | 41 | trace_for_serialize( "serializing - Schedule" );
|
---|
| 42 |
|
---|
[224] | 43 | ar & BOOST_SERIALIZATION_NVP( type );
|
---|
| 44 | ar & BOOST_SERIALIZATION_NVP( offset );
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public:
|
---|
| 48 | Schedule()
|
---|
| 49 | {
|
---|
| 50 | }
|
---|
[244] | 51 | Schedule( Type type, long offset )
|
---|
[224] | 52 | : type( type )
|
---|
| 53 | , offset( offset )
|
---|
[244] | 54 | , lpValue( 0 )
|
---|
[224] | 55 | {
|
---|
| 56 | }
|
---|
[244] | 57 | Schedule( const ::UserProc *pUserProc, long offest )
|
---|
| 58 | : type( Schedule::UserProc )
|
---|
| 59 | , offset( offset )
|
---|
| 60 | , pUserProc( pUserProc )
|
---|
| 61 | {
|
---|
| 62 | }
|
---|
[250] | 63 | Schedule( const ::DllProc *pDllProc, long offest )
|
---|
| 64 | : type( Schedule::UserProc )
|
---|
| 65 | , offset( offset )
|
---|
| 66 | , pDllProc( pDllProc )
|
---|
| 67 | {
|
---|
| 68 | }
|
---|
[224] | 69 | ~Schedule()
|
---|
| 70 | {
|
---|
| 71 | }
|
---|
[244] | 72 |
|
---|
| 73 | void SpecifyAddressOf()
|
---|
| 74 | {
|
---|
| 75 | if( type != Schedule::UserProc )
|
---|
| 76 | {
|
---|
| 77 | SetError();
|
---|
| 78 | }
|
---|
| 79 | type = Schedule::AddressOf;
|
---|
| 80 | }
|
---|
[224] | 81 | };
|
---|
| 82 | typedef std::vector<Schedule> Schedules;
|
---|
| 83 |
|
---|
| 84 | class NativeCode
|
---|
| 85 | {
|
---|
| 86 | int allocateSize;
|
---|
| 87 | char *codeBuffer;
|
---|
| 88 | int size;
|
---|
| 89 |
|
---|
| 90 | Schedules schedules;
|
---|
| 91 |
|
---|
| 92 | // XMLシリアライズ用
|
---|
| 93 | private:
|
---|
| 94 | friend class boost::serialization::access;
|
---|
| 95 | BOOST_SERIALIZATION_SPLIT_MEMBER();
|
---|
| 96 | template<class Archive> void load(Archive& ar, const unsigned int version)
|
---|
| 97 | {
|
---|
[256] | 98 | trace_for_serialize( "serializing(load) - NativeCode" );
|
---|
| 99 |
|
---|
[224] | 100 | std::string code;
|
---|
| 101 | ar & BOOST_SERIALIZATION_NVP( code );
|
---|
| 102 | ar & BOOST_SERIALIZATION_NVP( size );
|
---|
| 103 | ar & BOOST_SERIALIZATION_NVP( schedules );
|
---|
| 104 |
|
---|
| 105 | // 読み込み後の処理
|
---|
| 106 | Realloc( size );
|
---|
| 107 | for( int i=0; i<size; i++ )
|
---|
| 108 | {
|
---|
[256] | 109 | ULONG_PTR l;
|
---|
| 110 | sscanf( code.c_str() + i*3, "%02x,", &l );
|
---|
| 111 | codeBuffer[i] = (char)l;
|
---|
[224] | 112 | }
|
---|
| 113 | }
|
---|
| 114 | template<class Archive> void save(Archive& ar, const unsigned int version) const
|
---|
| 115 | {
|
---|
[256] | 116 | trace_for_serialize( "serializing(save) - NativeCode" );
|
---|
| 117 |
|
---|
[224] | 118 | // 保存準備
|
---|
[256] | 119 | char *tempCode = (char *)calloc( (size+1) * 3, 1 );
|
---|
[224] | 120 | for( int i=0; i<size; i++ )
|
---|
| 121 | {
|
---|
| 122 | char temp[32];
|
---|
[256] | 123 | sprintf( temp, "%02x,", (unsigned char)codeBuffer[i] );
|
---|
[224] | 124 | tempCode[i*3] = temp[0];
|
---|
| 125 | tempCode[i*3+1] = temp[1];
|
---|
| 126 | tempCode[i*3+2] = temp[2];
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | std::string code = tempCode;
|
---|
| 130 | free( tempCode );
|
---|
| 131 |
|
---|
| 132 | ar & BOOST_SERIALIZATION_NVP( code );
|
---|
| 133 | ar & BOOST_SERIALIZATION_NVP( size );
|
---|
| 134 | ar & BOOST_SERIALIZATION_NVP( schedules );
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 |
|
---|
[256] | 138 | void Realloc( int newSize )
|
---|
[224] | 139 | {
|
---|
[256] | 140 | if( allocateSize < newSize + 8192 )
|
---|
[224] | 141 | {
|
---|
[256] | 142 | while( allocateSize < newSize + 8192 )
|
---|
[224] | 143 | {
|
---|
| 144 | allocateSize += 8192;
|
---|
| 145 | }
|
---|
| 146 | codeBuffer = (char *)realloc( codeBuffer, allocateSize );
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | public:
|
---|
| 151 | NativeCode()
|
---|
| 152 | : allocateSize( 8192 )
|
---|
| 153 | , codeBuffer( (char *)malloc( allocateSize ) )
|
---|
| 154 | , size( 0 )
|
---|
| 155 | {
|
---|
| 156 | }
|
---|
[256] | 157 | NativeCode( const NativeCode &nativeCode )
|
---|
| 158 | : allocateSize( 8192 )
|
---|
| 159 | , codeBuffer( (char *)malloc( allocateSize ) )
|
---|
| 160 | , size( 0 )
|
---|
| 161 | {
|
---|
| 162 | Put( nativeCode );
|
---|
| 163 | }
|
---|
[252] | 164 | NativeCode( const char *codeBuffer, int size )
|
---|
| 165 | : allocateSize( 8192 )
|
---|
| 166 | , codeBuffer( (char *)malloc( allocateSize ) )
|
---|
| 167 | , size( 0 )
|
---|
| 168 | {
|
---|
| 169 | Put( codeBuffer, size );
|
---|
| 170 | }
|
---|
[224] | 171 | ~NativeCode()
|
---|
| 172 | {
|
---|
| 173 | free( codeBuffer );
|
---|
| 174 | }
|
---|
| 175 | void Clear()
|
---|
| 176 | {
|
---|
| 177 | size = 0;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[256] | 180 | void operator =( const NativeCode &nativeCode )
|
---|
| 181 | {
|
---|
| 182 | Clear();
|
---|
| 183 | Put( nativeCode );
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[237] | 186 | int GetSize() const
|
---|
| 187 | {
|
---|
| 188 | return size;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[253] | 191 | long GetLong( int codePos ) const
|
---|
| 192 | {
|
---|
| 193 | return *(long *)(this->codeBuffer+codePos);
|
---|
| 194 | }
|
---|
| 195 | long _GetLong_ObpOld( int _obpOld ) const
|
---|
| 196 | {
|
---|
| 197 | extern char *OpBuffer;
|
---|
| 198 | return *(long *)(OpBuffer+_obpOld);
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[237] | 201 | void Overwrite( int codePos, char c )
|
---|
| 202 | {
|
---|
| 203 | codeBuffer[codePos] = c;
|
---|
| 204 | }
|
---|
| 205 | void OverwriteOld( int _obpOld, char c )
|
---|
| 206 | {
|
---|
| 207 | // 未完成
|
---|
| 208 | extern char *OpBuffer;
|
---|
| 209 | OpBuffer[_obpOld] = c;
|
---|
| 210 | }
|
---|
[241] | 211 | void Overwrite( int codePos, long newLongValue )
|
---|
| 212 | {
|
---|
| 213 | *(long *)(this->codeBuffer+codePos) = newLongValue;
|
---|
| 214 | }
|
---|
| 215 | void OverwriteOld( int _obpOld, long newLongValue )
|
---|
| 216 | {
|
---|
| 217 | // 未完成
|
---|
| 218 | extern char *OpBuffer;
|
---|
| 219 | *(long *)(OpBuffer+_obpOld) = newLongValue;
|
---|
| 220 | }
|
---|
[237] | 221 |
|
---|
[224] | 222 | void Put( const char *codeBuffer, int size )
|
---|
| 223 | {
|
---|
[256] | 224 | Realloc( this->size + size );
|
---|
[224] | 225 |
|
---|
| 226 | memcpy( this->codeBuffer + this->size, codeBuffer, size );
|
---|
| 227 | this->size += size;
|
---|
[225] | 228 |
|
---|
| 229 | // 未完成
|
---|
| 230 | extern char *OpBuffer;
|
---|
| 231 | extern int obp;
|
---|
[226] | 232 | memcpy( OpBuffer + obp, codeBuffer, size );
|
---|
[232] | 233 | ObpPlus( size );
|
---|
[224] | 234 | }
|
---|
| 235 | void Put( const NativeCode &nativeCode )
|
---|
| 236 | {
|
---|
| 237 | Put( nativeCode.codeBuffer, nativeCode.size );
|
---|
| 238 | }
|
---|
[226] | 239 | void Put( _int64 i64data )
|
---|
[224] | 240 | {
|
---|
[226] | 241 | Put( (const char *)(&i64data), sizeof(_int64) );
|
---|
[224] | 242 | }
|
---|
| 243 | void Put( long l, Schedule::Type scheduleType = Schedule::None )
|
---|
| 244 | {
|
---|
| 245 | if( scheduleType != Schedule::None )
|
---|
| 246 | {
|
---|
| 247 | schedules.push_back( Schedule( scheduleType, size ) );
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | *((long *)(codeBuffer+size))=l;
|
---|
| 251 | size += sizeof(long);
|
---|
[225] | 252 |
|
---|
| 253 |
|
---|
| 254 |
|
---|
| 255 | // 未完成
|
---|
[228] | 256 | switch( scheduleType )
|
---|
| 257 | {
|
---|
| 258 | case Schedule::None:
|
---|
[244] | 259 | // 何もしない
|
---|
[228] | 260 | break;
|
---|
| 261 | case Schedule::GlobalVar:
|
---|
| 262 | extern CSchedule *pobj_GlobalVarSchedule;
|
---|
| 263 | pobj_GlobalVarSchedule->add();
|
---|
| 264 | break;
|
---|
[237] | 265 | case Schedule::DataTable:
|
---|
| 266 | extern CSchedule *pobj_DataTableSchedule;
|
---|
| 267 | pobj_DataTableSchedule->add();
|
---|
| 268 | break;
|
---|
[228] | 269 | case Schedule::Relocation:
|
---|
[244] | 270 | // 未完成
|
---|
[228] | 271 | break;
|
---|
| 272 | default:
|
---|
| 273 | Jenga::Throw( "scheduleTypeが無効な値を保持している" );
|
---|
| 274 | break;
|
---|
| 275 | }
|
---|
[225] | 276 | extern char *OpBuffer;
|
---|
| 277 | extern int obp;
|
---|
| 278 | *((long *)(OpBuffer+obp))=l;
|
---|
[232] | 279 | ObpPlus( sizeof(long) );
|
---|
[224] | 280 | }
|
---|
[245] | 281 | void PutUserProcSchedule( const UserProc *pUserProc, bool isCall );
|
---|
[250] | 282 | void PutDllProcSchedule( const DllProc *pDllProc );
|
---|
[226] | 283 | void Put( short s )
|
---|
| 284 | {
|
---|
| 285 | Put( (const char *)(&s), sizeof(short) );
|
---|
| 286 | }
|
---|
| 287 | void Put( char c )
|
---|
| 288 | {
|
---|
[256] | 289 | Realloc( size + 1 );
|
---|
[226] | 290 | codeBuffer[size++] = c;
|
---|
| 291 |
|
---|
| 292 |
|
---|
| 293 |
|
---|
| 294 | // 未完成
|
---|
| 295 | extern char *OpBuffer;
|
---|
| 296 | extern int obp;
|
---|
[232] | 297 | OpBuffer[obp]=c;
|
---|
| 298 | ObpPlus();
|
---|
[226] | 299 | }
|
---|
[224] | 300 | };
|
---|