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

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