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

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