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

Last change on this file since 247 was 245, checked in by dai_9181, 17 years ago
File size: 5.1 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
[228]9void AddLocalVarAddrSchedule();
[232]10void ObpPlus( int step = 1 );
[228]11
[244]12class UserProc;
13
[224]14class Schedule
15{
16public:
17 enum Type
18 {
[237]19 None = 10000,
[225]20 GlobalVar, // グローバル変数スケジュール
21 LocalVar, // ローカル変数スケジュール
[237]22 DataTable, // データテーブル スケジュール
[225]23 Relocation, // リロケーション情報スケジュール
[244]24 UserProc, // ユーザ定義関数呼び出し側スケジュール
25 AddressOf, // ユーザ定義関数位置スケジュール
[224]26 };
27
28private:
29 Type type;
[244]30 long offset;
[224]31
[244]32 union{
33 LONG_PTR lpValue;
34 const ::UserProc *pUserProc;
35 };
36
[224]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 }
[244]50 Schedule( Type type, long offset )
[224]51 : type( type )
52 , offset( offset )
[244]53 , lpValue( 0 )
[224]54 {
55 }
[244]56 Schedule( const ::UserProc *pUserProc, long offest )
57 : type( Schedule::UserProc )
58 , offset( offset )
59 , pUserProc( pUserProc )
60 {
61 }
[224]62 ~Schedule()
63 {
64 }
[244]65
66 void SpecifyAddressOf()
67 {
68 if( type != Schedule::UserProc )
69 {
70 SetError();
71 }
72 type = Schedule::AddressOf;
73 }
[224]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
[237]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 }
[241]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 }
[237]180
[224]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;
[225]187
188 // 未完成
189 extern char *OpBuffer;
190 extern int obp;
[226]191 memcpy( OpBuffer + obp, codeBuffer, size );
[232]192 ObpPlus( size );
[224]193 }
194 void Put( const NativeCode &nativeCode )
195 {
196 Put( nativeCode.codeBuffer, nativeCode.size );
197 }
[226]198 void Put( _int64 i64data )
[224]199 {
[226]200 Put( (const char *)(&i64data), sizeof(_int64) );
[224]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);
[225]211
212
213
214 // 未完成
[228]215 switch( scheduleType )
216 {
217 case Schedule::None:
[244]218 // 何もしない
[228]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;
[237]227 case Schedule::DataTable:
228 extern CSchedule *pobj_DataTableSchedule;
229 pobj_DataTableSchedule->add();
230 break;
[228]231 case Schedule::Relocation:
[244]232 // 未完成
[228]233 break;
234 default:
235 Jenga::Throw( "scheduleTypeが無効な値を保持している" );
236 break;
237 }
[225]238 extern char *OpBuffer;
239 extern int obp;
240 *((long *)(OpBuffer+obp))=l;
[232]241 ObpPlus( sizeof(long) );
[224]242 }
[245]243 void PutUserProcSchedule( const UserProc *pUserProc, bool isCall );
[226]244 void Put( short s )
245 {
246 Put( (const char *)(&s), sizeof(short) );
247 }
248 void Put( char c )
249 {
250 codeBuffer[size++] = c;
251 Realloc();
252
253
254
255 // 未完成
256 extern char *OpBuffer;
257 extern int obp;
[232]258 OpBuffer[obp]=c;
259 ObpPlus();
[226]260 }
[224]261};
Note: See TracBrowser for help on using the repository browser.