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
Line 
1#pragma once
2
3#include <vector>
4
5#include <jenga/include/common/Exception.h>
6
7#include <BoostSerializationSupport.h>
8
9class UserProc;
10
11class Schedule
12{
13public:
14 enum Type
15 {
16 None = 10000,
17 GlobalVar, // グローバル変数スケジュール
18 DataTable, // データテーブル スケジュール
19 Relocation, // リロケーション情報スケジュール
20 UserProc, // ユーザ定義関数呼び出し側スケジュール
21 AddressOf, // ユーザ定義関数位置スケジュール
22 DllProc, // DLL関数位置スケジュール
23 };
24
25private:
26 Type type;
27 long offset;
28
29 union{
30 LONG_PTR lpValue;
31 const ::UserProc *pUserProc;
32 const ::DllProc *pDllProc;
33 };
34
35 // XMLシリアライズ用
36private:
37 friend class boost::serialization::access;
38 template<class Archive> void serialize(Archive& ar, const unsigned int version)
39 {
40 trace_for_serialize( "serializing - Schedule" );
41
42 ar & BOOST_SERIALIZATION_NVP( type );
43 ar & BOOST_SERIALIZATION_NVP( offset );
44
45 switch( type )
46 {
47 case UserProc:
48 case AddressOf:
49 ar & boost::serialization::make_nvp("pUserProc", const_cast<::UserProc *&>(pUserProc));
50 break;
51 case DllProc:
52 ar & boost::serialization::make_nvp("pDllProc", const_cast<::DllProc *&>(pDllProc));
53 break;
54 default:
55 ar & BOOST_SERIALIZATION_NVP( lpValue );
56 break;
57 }
58 }
59
60public:
61 Schedule()
62 {
63 }
64 Schedule( Type type, long offset, LONG_PTR lpValue = 0 )
65 : type( type )
66 , offset( offset )
67 , lpValue( lpValue )
68 {
69 }
70 Schedule( const ::UserProc *pUserProc, long offset )
71 : type( Schedule::UserProc )
72 , offset( offset )
73 , pUserProc( pUserProc )
74 {
75 }
76 Schedule( const ::DllProc *pDllProc, long offset )
77 : type( Schedule::DllProc )
78 , offset( offset )
79 , pDllProc( pDllProc )
80 {
81 }
82 ~Schedule()
83 {
84 }
85
86 Type GetType() const
87 {
88 return type;
89 }
90 long GetOffset() const
91 {
92 return offset;
93 }
94 LONG_PTR GetLongPtrValue() const
95 {
96 return lpValue;
97 }
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 {
108 if( !( type == Schedule::UserProc || type == Schedule::AddressOf ) )
109 {
110 SetError();
111 }
112 return *pUserProc;
113 }
114
115 void SpecifyAddressOf()
116 {
117 if( type != Schedule::UserProc )
118 {
119 SetError();
120 }
121 type = Schedule::AddressOf;
122 }
123};
124typedef std::vector<Schedule> Schedules;
125
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
191class NativeCode
192{
193 int allocateSize;
194 char *codeBuffer;
195 int size;
196
197 // リンカで解決しなければならないスケジュール
198 Schedules schedules;
199
200 // ソースコード行番号とネイティブコード位置の対応情報
201 SourceLines sourceLines;
202
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 {
209 trace_for_serialize( "serializing(load) - NativeCode" );
210
211 std::string code;
212 ar & BOOST_SERIALIZATION_NVP( code );
213 ar & BOOST_SERIALIZATION_NVP( size );
214 ar & BOOST_SERIALIZATION_NVP( schedules );
215 ar & BOOST_SERIALIZATION_NVP( sourceLines );
216
217 // 読み込み後の処理
218 Realloc( size );
219 for( int i=0; i<size; i++ )
220 {
221 ULONG_PTR l;
222 sscanf( code.c_str() + i*3, "%02x,", &l );
223 codeBuffer[i] = (char)l;
224 }
225 }
226 template<class Archive> void save(Archive& ar, const unsigned int version) const
227 {
228 trace_for_serialize( "serializing(save) - NativeCode" );
229
230 // 保存準備
231 char *tempCode = (char *)calloc( (size+1) * 3, 1 );
232 for( int i=0; i<size; i++ )
233 {
234 char temp[32];
235 sprintf( temp, "%02x,", (unsigned char)codeBuffer[i] );
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 );
247 ar & BOOST_SERIALIZATION_NVP( sourceLines );
248 }
249
250
251 void Realloc( int newSize )
252 {
253 if( allocateSize < newSize + 8192 )
254 {
255 while( allocateSize < newSize + 8192 )
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 }
270 NativeCode( const NativeCode &nativeCode )
271 : allocateSize( 8192 )
272 , codeBuffer( (char *)malloc( allocateSize ) )
273 , size( 0 )
274 {
275 Put( nativeCode );
276 }
277 NativeCode( const char *codeBuffer, int size )
278 : allocateSize( 8192 )
279 , codeBuffer( (char *)malloc( allocateSize ) )
280 , size( 0 )
281 {
282 Put( codeBuffer, size );
283 }
284 ~NativeCode()
285 {
286 free( codeBuffer );
287 }
288 void Clear()
289 {
290 size = 0;
291 }
292
293 void operator =( const NativeCode &nativeCode )
294 {
295 Clear();
296 Put( nativeCode );
297 }
298
299 const char *GetCodeBuffer() const
300 {
301 return codeBuffer;
302 }
303 int GetSize() const
304 {
305 return size;
306 }
307 const Schedules &GetSchedules() const
308 {
309 return schedules;
310 }
311
312 long GetLong( int codePos ) const
313 {
314 return *(long *)(this->codeBuffer+codePos);
315 }
316
317 void Overwrite( int codePos, char c )
318 {
319 codeBuffer[codePos] = c;
320 }
321 void Overwrite( int codePos, long newLongValue )
322 {
323 *(long *)(this->codeBuffer+codePos) = newLongValue;
324 }
325
326 void Put( const char *codeBuffer, int size )
327 {
328 Realloc( this->size + size );
329
330 memcpy( this->codeBuffer + this->size, codeBuffer, size );
331 this->size += size;
332 }
333 void Put( const NativeCode &nativeCode );
334 void Put( _int64 i64data )
335 {
336 Put( (const char *)(&i64data), sizeof(_int64) );
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 }
348 void PutUserProcSchedule( const UserProc *pUserProc, bool isCall );
349 void PutDllProcSchedule( const DllProc *pDllProc );
350 void Put( short s )
351 {
352 Put( (const char *)(&s), sizeof(short) );
353 }
354 void Put( char c )
355 {
356 Realloc( size + 1 );
357 codeBuffer[size++] = c;
358 }
359
360 const SourceLines &GetSourceLines() const
361 {
362 return sourceLines;
363 }
364 void NextSourceLine();
365
366 void ResetDataSectionBaseOffset( long dataSectionBaseOffset );
367};
Note: See TracBrowser for help on using the repository browser.