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

Last change on this file since 225 was 225, checked in by dai_9181, 17 years ago

CodeGeneratorクラスのベースを実装

File size: 3.2 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, codeBuffer, size );
135 obp += size;
136 }
137 void Put( const NativeCode &nativeCode )
138 {
139 Put( nativeCode.codeBuffer, nativeCode.size );
140 }
141 void Put( char c )
142 {
143 codeBuffer[size++] = c;
144 Realloc();
145
146
147
148 // 未完成
149 extern char *OpBuffer;
150 extern int obp;
151 OpBuffer[obp++]=c;
152 }
153 void Put( long l, Schedule::Type scheduleType = Schedule::None )
154 {
155 if( scheduleType != Schedule::None )
156 {
157 schedules.push_back( Schedule( scheduleType, size ) );
158 }
159
160 *((long *)(codeBuffer+size))=l;
161 size += sizeof(long);
162
163
164
165 // 未完成
166 extern char *OpBuffer;
167 extern int obp;
168 *((long *)(OpBuffer+obp))=l;
169 obp+=sizeof(long);
170 }
171};
Note: See TracBrowser for help on using the repository browser.