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

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