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

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