source: dev/trunk/abdev/BasicCompiler_Common/src/CommonCodeGenerator.cpp@ 251

Last change on this file since 251 was 251, checked in by dai_9181, 17 years ago
File size: 7.8 KB
Line 
1#include "stdafx.h"
2
3#include <CodeGenerator.h>
4
5
6void CodeGenerator::ResolveExitSubSchedule()
7{
8 BOOST_FOREACH( long exitSubCodePosition, exitSubCodePositions )
9 {
10 pNativeCode->Overwrite( exitSubCodePosition, (long)( pNativeCode->GetSize()-(exitSubCodePosition+sizeof(long)) ) );
11 }
12
13 // TODO: 未完成
14 BOOST_FOREACH( long exitSubCodePositionOld, _exitSubCodePositions_ObpOld )
15 {
16 extern int obp;
17 pNativeCode->OverwriteOld( exitSubCodePositionOld, (long)( obp-(exitSubCodePositionOld+sizeof(long)) ) );
18 }
19}
20
21void CodeGenerator::CheckUnresolveSchedule()
22{
23 if( pertialSchedules.size() > 0 )
24 {
25 SetError();
26 }
27}
28
29void CodeGenerator::opfix( const PertialSchedule *pPertialSchedule, long newValue )
30{
31 bool isSuccessful = false;
32
33 PertialSchedules::iterator it = pertialSchedules.begin();
34 while( it != pertialSchedules.end() )
35 {
36 if( (*it) == pPertialSchedule )
37 {
38 if( pPertialSchedule->GetTypeSize() == sizeof(char) )
39 {
40 if( newValue < -128 || 127 < newValue )
41 {
42 // 範囲外
43 SetError();
44 }
45
46 pNativeCode->Overwrite( pPertialSchedule->GetCodePos(), (char)newValue );
47
48 // TODO: 未完成(用が無くなったら消す)
49 pNativeCode->OverwriteOld( pPertialSchedule->GetObpOld(), (char)newValue );
50 }
51 else if( pPertialSchedule->GetTypeSize() == sizeof(long) )
52 {
53 pNativeCode->Overwrite( pPertialSchedule->GetCodePos(), newValue );
54
55 // TODO: 未完成(用が無くなったら消す)
56 pNativeCode->OverwriteOld( pPertialSchedule->GetObpOld(), newValue );
57 }
58 else
59 {
60 SetError();
61 }
62
63 it = pertialSchedules.erase( it );
64 delete pPertialSchedule;
65
66 isSuccessful = true;
67 }
68 else
69 {
70 it++;
71 }
72 }
73
74 if( isSuccessful == false )
75 {
76 SetError();
77 }
78}
79
80
81// 分岐関連
82void CodeGenerator::opfix_JmpPertialSchedule( const PertialSchedule *pPertialSchedule )
83{
84 bool isSuccessful = false;
85
86 PertialSchedules::iterator it = pertialSchedules.begin();
87 while( it != pertialSchedules.end() )
88 {
89 if( (*it) == pPertialSchedule )
90 {
91 long newValue = pNativeCode->GetSize() - (pPertialSchedule->GetCodePos()+pPertialSchedule->GetTypeSize());
92
93 extern int obp;
94 long newValueOld = obp - (pPertialSchedule->GetObpOld()+pPertialSchedule->GetTypeSize());
95
96 if( pPertialSchedule->GetTypeSize() == sizeof(char) )
97 {
98 if( newValue < -128 || 127 < newValue )
99 {
100 // 範囲外
101 SetError();
102 }
103
104 pNativeCode->Overwrite( pPertialSchedule->GetCodePos(), (char)newValue );
105
106 // TODO: 未完成(用が無くなったら消す)
107 pNativeCode->OverwriteOld( pPertialSchedule->GetObpOld(), (char)newValueOld );
108 }
109 else if( pPertialSchedule->GetTypeSize() == sizeof(long) )
110 {
111 pNativeCode->Overwrite( pPertialSchedule->GetCodePos(), newValue );
112
113 // TODO: 未完成(用が無くなったら消す)
114 pNativeCode->OverwriteOld( pPertialSchedule->GetObpOld(), newValueOld );
115 }
116 else
117 {
118 SetError();
119 }
120
121 it = pertialSchedules.erase( it );
122 delete pPertialSchedule;
123
124 isSuccessful = true;
125 }
126 else
127 {
128 it++;
129 }
130 }
131
132 if( isSuccessful == false )
133 {
134 SetError();
135 }
136}
137const PertialSchedule *CodeGenerator::__jmp_op_format( char opcode, long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
138{
139 long beginCodePos = pNativeCode->GetSize();
140 {
141 // TODO: 未完成
142 extern int obp;
143 beginCodePos = obp;
144 }
145
146 if( opcode == (char)0xEB )
147 {
148 // jmp命令のとき
149 if( op_size == sizeof(char) )
150 {
151 pNativeCode->Put( (char)opcode );
152 }
153 else if( op_size == sizeof(long) )
154 {
155 pNativeCode->Put( (char)0xE9 );
156 }
157 else
158 {
159 SetError();
160 }
161 }
162 else
163 {
164 if( op_size == sizeof(char) )
165 {
166 pNativeCode->Put( (char)((char)0x70 | opcode) );
167 }
168 else if( op_size == sizeof(long) )
169 {
170 pNativeCode->Put( (char)0x0F );
171 pNativeCode->Put( (char)((char)0x80 | opcode) );
172 }
173 else
174 {
175 SetError();
176 }
177 }
178
179 const PertialSchedule *pPertialSchedule = NULL;
180 if( isPertialSchedule )
181 {
182 pertialSchedules.push_back( new PertialSchedule( pNativeCode->GetSize(), op_size ) );
183 pPertialSchedule = pertialSchedules.back();
184 }
185
186 if( isSelfOpcodeOffset )
187 {
188 // 自分自身の命令サイズを考慮する場合
189 //offset += ( pNativeCode->GetSize() - beginCodePos ) + op_size;
190
191 // TODO: 未完成
192 extern int obp;
193 offset -= ( obp - beginCodePos ) + op_size;
194 }
195
196 if( op_size == sizeof(char) )
197 {
198 pNativeCode->Put( (char)offset );
199 }
200 else if( op_size == sizeof(long) )
201 {
202 pNativeCode->Put( offset );
203 }
204 else
205 {
206 SetError();
207 }
208
209 return pPertialSchedule;
210}
211const PertialSchedule *CodeGenerator::op_jle( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
212{
213 return __jmp_op_format( (char)0x0E, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
214}
215const PertialSchedule *CodeGenerator::op_jbe( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
216{
217 return __jmp_op_format( (char)0x06, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
218}
219const PertialSchedule *CodeGenerator::op_jge( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
220{
221 return __jmp_op_format( (char)0x0D, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
222}
223const PertialSchedule *CodeGenerator::op_jae( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
224{
225 return __jmp_op_format( (char)0x03, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
226}
227const PertialSchedule *CodeGenerator::op_jl( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
228{
229 return __jmp_op_format( (char)0x0C, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
230}
231const PertialSchedule *CodeGenerator::op_jb( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
232{
233 return __jmp_op_format( (char)0x02, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
234}
235const PertialSchedule *CodeGenerator::op_jg( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
236{
237 return __jmp_op_format( (char)0x0F, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
238}
239const PertialSchedule *CodeGenerator::op_ja( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
240{
241 return __jmp_op_format( (char)0x07, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
242}
243const PertialSchedule *CodeGenerator::op_jne( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
244{
245 return __jmp_op_format( (char)0x05, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
246}
247const PertialSchedule *CodeGenerator::op_je( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
248{
249 return __jmp_op_format( (char)0x04, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
250}
251const PertialSchedule *CodeGenerator::op_jmp( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
252{
253 return __jmp_op_format( (char)0xEB, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
254}
255void CodeGenerator::op_jmp_continue()
256{
257 //op_jmp( GetContinueCodePos()-(pNativeCode->GetSize()+sizeof(long)), sizeof(long) );
258
259 // TODO: 未完成(OpBuffer/obp廃止が整ったら上記のコードを有効にすべし)
260
261 if( GetContinueCodePosOld() == -1 )
262 {
263 SetError(12,"Continue",cp);
264 return;
265 }
266
267 extern int obp;
268 op_jmp( GetContinueCodePosOld()-obp, sizeof(long), false, true );
269}
270void CodeGenerator::op_jmp_exitsub()
271{
272 // オペコード
273 pNativeCode->Put( (char)0xE9 );
274
275 exitSubCodePositions.push_back( pNativeCode->GetSize() );
276
277 extern int obp;
278 _exitSubCodePositions_ObpOld.push_back( obp );
279
280 pNativeCode->Put( (long)0 );
281}
282void CodeGenerator::op_jmp_goto_schedule( const GotoLabelSchedule &gotoLabelSchedule )
283{
284 // オペコード
285 pNativeCode->Put( (char)0xE9 );
286
287 gotoLabelSchedules.push_back( gotoLabelSchedule );
288
289 pNativeCode->Put( (long)0 );
290}
Note: See TracBrowser for help on using the repository browser.