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

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