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

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