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

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