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

Last change on this file since 256 was 256, checked in by dai_9181, 17 years ago

MetaImplを廃止し、Metaにした。
ObjectModuleクラス、Linkerクラスを用意。

File size: 6.0 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 };
25
26private:
27 Type type;
28 long offset;
29
30 union{
31 LONG_PTR lpValue;
32 const ::UserProc *pUserProc;
33 const ::DllProc *pDllProc;
34 };
35
36 // XMLシリアライズ用
37private:
38 friend class boost::serialization::access;
39 template<class Archive> void serialize(Archive& ar, const unsigned int version)
40 {
41 trace_for_serialize( "serializing - Schedule" );
42
43 ar & BOOST_SERIALIZATION_NVP( type );
44 ar & BOOST_SERIALIZATION_NVP( offset );
45 }
46
47public:
48 Schedule()
49 {
50 }
51 Schedule( Type type, long offset )
52 : type( type )
53 , offset( offset )
54 , lpValue( 0 )
55 {
56 }
57 Schedule( const ::UserProc *pUserProc, long offest )
58 : type( Schedule::UserProc )
59 , offset( offset )
60 , pUserProc( pUserProc )
61 {
62 }
63 Schedule( const ::DllProc *pDllProc, long offest )
64 : type( Schedule::UserProc )
65 , offset( offset )
66 , pDllProc( pDllProc )
67 {
68 }
69 ~Schedule()
70 {
71 }
72
73 void SpecifyAddressOf()
74 {
75 if( type != Schedule::UserProc )
76 {
77 SetError();
78 }
79 type = Schedule::AddressOf;
80 }
81};
82typedef std::vector<Schedule> Schedules;
83
84class NativeCode
85{
86 int allocateSize;
87 char *codeBuffer;
88 int size;
89
90 Schedules schedules;
91
92 // XMLシリアライズ用
93private:
94 friend class boost::serialization::access;
95 BOOST_SERIALIZATION_SPLIT_MEMBER();
96 template<class Archive> void load(Archive& ar, const unsigned int version)
97 {
98 trace_for_serialize( "serializing(load) - NativeCode" );
99
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 {
109 ULONG_PTR l;
110 sscanf( code.c_str() + i*3, "%02x,", &l );
111 codeBuffer[i] = (char)l;
112 }
113 }
114 template<class Archive> void save(Archive& ar, const unsigned int version) const
115 {
116 trace_for_serialize( "serializing(save) - NativeCode" );
117
118 // 保存準備
119 char *tempCode = (char *)calloc( (size+1) * 3, 1 );
120 for( int i=0; i<size; i++ )
121 {
122 char temp[32];
123 sprintf( temp, "%02x,", (unsigned char)codeBuffer[i] );
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
138 void Realloc( int newSize )
139 {
140 if( allocateSize < newSize + 8192 )
141 {
142 while( allocateSize < newSize + 8192 )
143 {
144 allocateSize += 8192;
145 }
146 codeBuffer = (char *)realloc( codeBuffer, allocateSize );
147 }
148 }
149
150public:
151 NativeCode()
152 : allocateSize( 8192 )
153 , codeBuffer( (char *)malloc( allocateSize ) )
154 , size( 0 )
155 {
156 }
157 NativeCode( const NativeCode &nativeCode )
158 : allocateSize( 8192 )
159 , codeBuffer( (char *)malloc( allocateSize ) )
160 , size( 0 )
161 {
162 Put( nativeCode );
163 }
164 NativeCode( const char *codeBuffer, int size )
165 : allocateSize( 8192 )
166 , codeBuffer( (char *)malloc( allocateSize ) )
167 , size( 0 )
168 {
169 Put( codeBuffer, size );
170 }
171 ~NativeCode()
172 {
173 free( codeBuffer );
174 }
175 void Clear()
176 {
177 size = 0;
178 }
179
180 void operator =( const NativeCode &nativeCode )
181 {
182 Clear();
183 Put( nativeCode );
184 }
185
186 int GetSize() const
187 {
188 return size;
189 }
190
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
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 }
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 }
221
222 void Put( const char *codeBuffer, int size )
223 {
224 Realloc( this->size + size );
225
226 memcpy( this->codeBuffer + this->size, codeBuffer, size );
227 this->size += size;
228
229 // 未完成
230 extern char *OpBuffer;
231 extern int obp;
232 memcpy( OpBuffer + obp, codeBuffer, size );
233 ObpPlus( size );
234 }
235 void Put( const NativeCode &nativeCode )
236 {
237 Put( nativeCode.codeBuffer, nativeCode.size );
238 }
239 void Put( _int64 i64data )
240 {
241 Put( (const char *)(&i64data), sizeof(_int64) );
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);
252
253
254
255 // 未完成
256 switch( scheduleType )
257 {
258 case Schedule::None:
259 // 何もしない
260 break;
261 case Schedule::GlobalVar:
262 extern CSchedule *pobj_GlobalVarSchedule;
263 pobj_GlobalVarSchedule->add();
264 break;
265 case Schedule::DataTable:
266 extern CSchedule *pobj_DataTableSchedule;
267 pobj_DataTableSchedule->add();
268 break;
269 case Schedule::Relocation:
270 // 未完成
271 break;
272 default:
273 Jenga::Throw( "scheduleTypeが無効な値を保持している" );
274 break;
275 }
276 extern char *OpBuffer;
277 extern int obp;
278 *((long *)(OpBuffer+obp))=l;
279 ObpPlus( sizeof(long) );
280 }
281 void PutUserProcSchedule( const UserProc *pUserProc, bool isCall );
282 void PutDllProcSchedule( const DllProc *pDllProc );
283 void Put( short s )
284 {
285 Put( (const char *)(&s), sizeof(short) );
286 }
287 void Put( char c )
288 {
289 Realloc( size + 1 );
290 codeBuffer[size++] = c;
291
292
293
294 // 未完成
295 extern char *OpBuffer;
296 extern int obp;
297 OpBuffer[obp]=c;
298 ObpPlus();
299 }
300};
Note: See TracBrowser for help on using the repository browser.