source: dev/trunk/abdev/BasicCompiler_Common/include/NativeCode.h@ 278

Last change on this file since 278 was 278, checked in by dai_9181, 17 years ago
File size: 7.5 KB
RevLine 
[224]1#pragma once
2
3#include <vector>
4
[228]5#include <jenga/include/common/Exception.h>
6
[225]7#include <BoostSerializationSupport.h>
8
[244]9class UserProc;
10
[224]11class Schedule
12{
13public:
14 enum Type
15 {
[237]16 None = 10000,
[225]17 GlobalVar, // グローバル変数スケジュール
[237]18 DataTable, // データテーブル スケジュール
[225]19 Relocation, // リロケーション情報スケジュール
[244]20 UserProc, // ユーザ定義関数呼び出し側スケジュール
21 AddressOf, // ユーザ定義関数位置スケジュール
[257]22 DllProc, // DLL関数位置スケジュール
[224]23 };
24
25private:
26 Type type;
[244]27 long offset;
[224]28
[244]29 union{
30 LONG_PTR lpValue;
31 const ::UserProc *pUserProc;
[250]32 const ::DllProc *pDllProc;
[244]33 };
34
[224]35 // XMLシリアライズ用
36private:
37 friend class boost::serialization::access;
38 template<class Archive> void serialize(Archive& ar, const unsigned int version)
39 {
[256]40 trace_for_serialize( "serializing - Schedule" );
41
[224]42 ar & BOOST_SERIALIZATION_NVP( type );
43 ar & BOOST_SERIALIZATION_NVP( offset );
[272]44
45 switch( type )
46 {
47 case UserProc:
48 case AddressOf:
[278]49 ar & boost::serialization::make_nvp("pUserProc", const_cast<::UserProc *&>(pUserProc));
[272]50 break;
51 case DllProc:
[278]52 ar & boost::serialization::make_nvp("pDllProc", const_cast<::DllProc *&>(pDllProc));
[272]53 break;
54 default:
55 ar & BOOST_SERIALIZATION_NVP( lpValue );
56 break;
57 }
[224]58 }
59
60public:
61 Schedule()
62 {
63 }
[258]64 Schedule( Type type, long offset, LONG_PTR lpValue = 0 )
[224]65 : type( type )
66 , offset( offset )
[258]67 , lpValue( lpValue )
[224]68 {
69 }
[258]70 Schedule( const ::UserProc *pUserProc, long offset )
[244]71 : type( Schedule::UserProc )
72 , offset( offset )
73 , pUserProc( pUserProc )
74 {
75 }
[258]76 Schedule( const ::DllProc *pDllProc, long offset )
[257]77 : type( Schedule::DllProc )
[250]78 , offset( offset )
79 , pDllProc( pDllProc )
80 {
81 }
[224]82 ~Schedule()
83 {
84 }
[244]85
[257]86 Type GetType() const
87 {
88 return type;
89 }
90 long GetOffset() const
91 {
92 return offset;
93 }
[258]94 LONG_PTR GetLongPtrValue() const
95 {
96 return lpValue;
97 }
[257]98 const ::DllProc &GetDllProc() const
99 {
100 if( type != Schedule::DllProc )
101 {
102 SetError();
103 }
104 return *pDllProc;
105 }
106 const ::UserProc &GetUserProc() const
107 {
[258]108 if( !( type == Schedule::UserProc || type == Schedule::AddressOf ) )
[257]109 {
110 SetError();
111 }
112 return *pUserProc;
113 }
114
[244]115 void SpecifyAddressOf()
116 {
117 if( type != Schedule::UserProc )
118 {
119 SetError();
120 }
121 type = Schedule::AddressOf;
122 }
[224]123};
124typedef std::vector<Schedule> Schedules;
125
[263]126#define CODETYPE_SYSTEMPROC 0x0001
127#define CODETYPE_DEBUGPROC 0x0002
128class SourceLine
129{
130 int lineNum;
131 long nativeCodePos;
132 long sourceCodePos;
133 DWORD codeType;
134
135 // XMLシリアライズ用
136private:
137 friend class boost::serialization::access;
138 template<class Archive> void serialize(Archive& ar, const unsigned int version)
139 {
140 trace_for_serialize( "serializing - SourceLine" );
141
142 ar & BOOST_SERIALIZATION_NVP( lineNum );
143 ar & BOOST_SERIALIZATION_NVP( nativeCodePos );
144 ar & BOOST_SERIALIZATION_NVP( sourceCodePos );
145 ar & BOOST_SERIALIZATION_NVP( codeType );
146 }
147
148public:
149 SourceLine( int lineNum, int nativeCodePos, int sourceCodePos, DWORD codeType )
150 : lineNum( lineNum )
151 , nativeCodePos( nativeCodePos )
152 , sourceCodePos( sourceCodePos )
153 , codeType( codeType )
154 {
155 }
156 SourceLine()
157 {
158 }
159
160 int GetLineNum() const
161 {
162 return lineNum;
163 }
164 long GetNativeCodePos() const
165 {
166 return nativeCodePos;
167 }
168 long GetSourceCodePos() const
169 {
170 return sourceCodePos;
171 }
172 void SetSourceCodePos( int sourceCodePos )
173 {
174 this->sourceCodePos = sourceCodePos;
175 }
176 DWORD GetCodeType() const
177 {
178 return codeType;
179 }
180 bool IsInSystemProc() const
181 {
182 return ( (codeType&CODETYPE_SYSTEMPROC) != 0 );
183 }
184 bool IsInDebugProc() const
185 {
186 return ( (codeType&CODETYPE_DEBUGPROC) != 0 );
187 }
188};
189typedef std::vector<SourceLine> SourceLines;
190
[224]191class NativeCode
192{
193 int allocateSize;
194 char *codeBuffer;
195 int size;
196
[263]197 // リンカで解決しなければならないスケジュール
[224]198 Schedules schedules;
199
[263]200 // ソースコード行番号とネイティブコード位置の対応情報
201 SourceLines sourceLines;
202
[224]203 // XMLシリアライズ用
204private:
205 friend class boost::serialization::access;
206 BOOST_SERIALIZATION_SPLIT_MEMBER();
207 template<class Archive> void load(Archive& ar, const unsigned int version)
208 {
[256]209 trace_for_serialize( "serializing(load) - NativeCode" );
210
[224]211 std::string code;
212 ar & BOOST_SERIALIZATION_NVP( code );
213 ar & BOOST_SERIALIZATION_NVP( size );
214 ar & BOOST_SERIALIZATION_NVP( schedules );
[263]215 ar & BOOST_SERIALIZATION_NVP( sourceLines );
[224]216
217 // 読み込み後の処理
218 Realloc( size );
219 for( int i=0; i<size; i++ )
220 {
[256]221 ULONG_PTR l;
222 sscanf( code.c_str() + i*3, "%02x,", &l );
223 codeBuffer[i] = (char)l;
[224]224 }
225 }
226 template<class Archive> void save(Archive& ar, const unsigned int version) const
227 {
[256]228 trace_for_serialize( "serializing(save) - NativeCode" );
229
[224]230 // 保存準備
[256]231 char *tempCode = (char *)calloc( (size+1) * 3, 1 );
[224]232 for( int i=0; i<size; i++ )
233 {
234 char temp[32];
[256]235 sprintf( temp, "%02x,", (unsigned char)codeBuffer[i] );
[224]236 tempCode[i*3] = temp[0];
237 tempCode[i*3+1] = temp[1];
238 tempCode[i*3+2] = temp[2];
239 }
240
241 std::string code = tempCode;
242 free( tempCode );
243
244 ar & BOOST_SERIALIZATION_NVP( code );
245 ar & BOOST_SERIALIZATION_NVP( size );
246 ar & BOOST_SERIALIZATION_NVP( schedules );
[263]247 ar & BOOST_SERIALIZATION_NVP( sourceLines );
[224]248 }
249
250
[256]251 void Realloc( int newSize )
[224]252 {
[256]253 if( allocateSize < newSize + 8192 )
[224]254 {
[256]255 while( allocateSize < newSize + 8192 )
[224]256 {
257 allocateSize += 8192;
258 }
259 codeBuffer = (char *)realloc( codeBuffer, allocateSize );
260 }
261 }
262
263public:
264 NativeCode()
265 : allocateSize( 8192 )
266 , codeBuffer( (char *)malloc( allocateSize ) )
267 , size( 0 )
268 {
269 }
[276]270 NativeCode( const NativeCode &nativeCode )
[256]271 : allocateSize( 8192 )
272 , codeBuffer( (char *)malloc( allocateSize ) )
273 , size( 0 )
274 {
[276]275 Put( nativeCode );
[256]276 }
[276]277 NativeCode( const char *codeBuffer, int size )
[252]278 : allocateSize( 8192 )
279 , codeBuffer( (char *)malloc( allocateSize ) )
280 , size( 0 )
281 {
[276]282 Put( codeBuffer, size );
[252]283 }
[224]284 ~NativeCode()
285 {
286 free( codeBuffer );
287 }
288 void Clear()
289 {
290 size = 0;
291 }
292
[256]293 void operator =( const NativeCode &nativeCode )
294 {
295 Clear();
[276]296 Put( nativeCode );
[256]297 }
298
[263]299 const char *GetCodeBuffer() const
300 {
301 return codeBuffer;
302 }
[237]303 int GetSize() const
304 {
305 return size;
306 }
[257]307 const Schedules &GetSchedules() const
308 {
309 return schedules;
310 }
[237]311
[253]312 long GetLong( int codePos ) const
313 {
314 return *(long *)(this->codeBuffer+codePos);
315 }
316
[237]317 void Overwrite( int codePos, char c )
318 {
319 codeBuffer[codePos] = c;
320 }
[241]321 void Overwrite( int codePos, long newLongValue )
322 {
323 *(long *)(this->codeBuffer+codePos) = newLongValue;
324 }
[237]325
[276]326 void Put( const char *codeBuffer, int size )
[224]327 {
[256]328 Realloc( this->size + size );
[224]329
330 memcpy( this->codeBuffer + this->size, codeBuffer, size );
331 this->size += size;
332 }
[276]333 void Put( const NativeCode &nativeCode );
[226]334 void Put( _int64 i64data )
[224]335 {
[226]336 Put( (const char *)(&i64data), sizeof(_int64) );
[224]337 }
338 void Put( long l, Schedule::Type scheduleType = Schedule::None )
339 {
340 if( scheduleType != Schedule::None )
341 {
342 schedules.push_back( Schedule( scheduleType, size ) );
343 }
344
345 *((long *)(codeBuffer+size))=l;
346 size += sizeof(long);
347 }
[245]348 void PutUserProcSchedule( const UserProc *pUserProc, bool isCall );
[250]349 void PutDllProcSchedule( const DllProc *pDllProc );
[226]350 void Put( short s )
351 {
352 Put( (const char *)(&s), sizeof(short) );
353 }
354 void Put( char c )
355 {
[256]356 Realloc( size + 1 );
[226]357 codeBuffer[size++] = c;
358 }
[263]359
[265]360 const SourceLines &GetSourceLines() const
361 {
362 return sourceLines;
363 }
[263]364 void NextSourceLine();
[273]365
366 void ResetDataSectionBaseOffset( long dataSectionBaseOffset );
[224]367};
Note: See TracBrowser for help on using the repository browser.