source: dev/trunk/ab5.0/abdev/compiler_x86/Compile_ProcOp.cpp@ 641

Last change on this file since 641 was 641, checked in by dai_9181, 16 years ago

・デバッグトレース時、グローバル領域の終端行でステップインまたはステップアウトしたときにデバッグ情報の取得に失敗して強制終了してしまう不具合を修正。
・グローバル領域のデバッグ実行ができなくなっている不具合を修正。

File size: 24.6 KB
Line 
1#include "stdafx.h"
2
3#include "Opcode.h"
4
5
6void SystemProc( const UserProc &userProc ){
7 if( userProc.GetName() == "_System_GetEip" ){
8 //mov eax,dword ptr[esp]
9 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_ESP, 0, MOD_BASE );
10
11 //ret
12 compiler.codeGenerator.op_ret();
13 }
14 else if( userProc.GetName() == "_System_InitDllGlobalVariables" ){
15 ////////////////////////////////////////
16 // DLLのグローバル領域をコンパイル
17 ////////////////////////////////////////
18
19 if( !compiler.IsDll() ){
20 //ret
21 compiler.codeGenerator.op_ret();
22
23 return;
24 }
25
26 int BackCp;
27 BackCp=cp;
28 cp=-1;
29
30 if( compiler.IsDebug() )
31 {
32 //デバッグ用の変数を定義
33 DebugVariable();
34 }
35
36 //GC用の変数を定義
37 InitGCVariables();
38
39 //_System_StartupProgramの呼び出し
40 extern const UserProc *pSub_System_StartupProgram;
41 compiler.codeGenerator.op_call(pSub_System_StartupProgram);
42
43 //クラスに属する静的メンバを定義
44 ActiveBasic::Compiler::ProcedureGenerator::Generate_InitStaticMember(
45 compiler.GetObjectModule().meta.GetClasses()
46 );
47
48 GetGlobalDataForDll();
49
50 cp=BackCp;
51
52 //ret
53 compiler.codeGenerator.op_ret();
54 }
55 else if( userProc.GetName() == "_System_InitStaticLocalVariables" ){
56 //静的ローカルオブジェクトのコンストラクタ呼び出し
57
58 BOOST_FOREACH( Variable *pVar, compiler.GetObjectModule().meta.GetGlobalVars() ){
59 if(memicmp(pVar->GetName().c_str(),"Static%",7)==0){
60 //コンストラクタ呼び出し
61 if( pVar->GetType().IsObject() ){
62
63 //エラー用
64 cp=pVar->source_code_address;
65
66 CallConstructor(
67 pVar->GetName().c_str(),
68 pVar->GetSubscripts(),
69 pVar->GetType(),
70 pVar->GetParamStrForConstructor().c_str());
71 }
72 }
73 }
74
75 //ret
76 compiler.codeGenerator.op_ret();
77 }
78 else if( userProc.GetName() == "_System_Call_Destructor_of_GlobalObject" )
79 {
80 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
81
82 compiler.SetCompilingUserProc( pBackUserProc );
83
84 //ret
85 compiler.codeGenerator.op_ret();
86 }
87 else if( userProc.GetName() == "_allrem" ){
88 //乗除演算用の特殊関数(64ビット整数対応)
89 BYTE Buffer_allrem[]={
90 0x53,0x57,0x33,0xFF,0x8B,0x44,0x24,0x10,0x0B,0xC0,0x7D,0x14,0x47,0x8B,0x54,0x24,0x0C,0xF7,0xD8,0xF7,0xDA,0x83,0xD8,0x00,0x89,0x44,0x24,0x10,0x89,0x54,0x24,0x0C,0x8B,0x44,0x24,0x18,0x0B,0xC0,0x7D,0x13,0x8B,0x54,0x24,0x14,0xF7,0xD8,0xF7,0xDA,0x83,0xD8,0x00,0x89,0x44,0x24,0x18,0x89,0x54,0x24,0x14,0x0B,0xC0,0x75,0x1B,0x8B,0x4C,0x24,0x14,0x8B,0x44,0x24,0x10,0x33,0xD2,0xF7,0xF1,0x8B,0x44,0x24,0x0C,0xF7,0xF1,0x8B,0xC2,0x33,0xD2,0x4F,0x79,0x4E,0xEB,0x53,0x8B,0xD8,0x8B,0x4C,0x24,0x14,0x8B,0x54,0x24,0x10,0x8B,0x44,0x24,0x0C,0xD1,0xEB,0xD1,0xD9,0xD1,0xEA,0xD1,0xD8,0x0B,0xDB,0x75,0xF4,0xF7,0xF1,0x8B,0xC8,0xF7,0x64,0x24,0x18,0x91,0xF7,0x64,0x24,0x14,0x03,0xD1,0x72,0x0E,0x3B,0x54,0x24,0x10,0x77,0x08,0x72,0x0E,0x3B,0x44,0x24,0x0C,0x76,0x08,0x2B,0x44,0x24,0x14,0x1B,0x54,0x24,0x18,0x2B,0x44,0x24,0x0C,0x1B,0x54,0x24,0x10,0x4F,0x79,0x07,0xF7,0xDA,0xF7,0xD8,0x83,0xDA,0x00,0x5F,0x5B,0xC2,0x10,0x00
91 };
92
93 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allrem, 178 ) );
94 }
95 else if( userProc.GetName() == "_aullrem" ){
96 //乗除演算用の特殊関数(64ビット整数対応)
97 BYTE Buffer_aullrem[]={
98 0x53,0x8B,0x44,0x24,0x14,0x0B,0xC0,0x75,0x18,0x8B,0x4C,0x24,0x10,0x8B,
99 0x44,0x24,0x0C,0x33,0xD2,0xF7,0xF1,0x8B,0x44,0x24,0x08,0xF7,0xF1,0x8B,
100 0xC2,0x33,0xD2,0xEB,0x50,0x8B,0xC8,0x8B,0x5C,0x24,0x10,0x8B,0x54,0x24,
101 0x0C,0x8B,0x44,0x24,0x08,0xD1,0xE9,0xD1,0xDB,0xD1,0xEA,0xD1,0xD8,0x0B,
102 0xC9,0x75,0xF4,0xF7,0xF3,0x8B,0xC8,0xF7,0x64,0x24,0x14,0x91,0xF7,0x64,
103 0x24,0x10,0x03,0xD1,0x72,0x0E,0x3B,0x54,0x24,0x0C,0x77,0x08,0x72,0x0E,
104 0x3B,0x44,0x24,0x08,0x76,0x08,0x2B,0x44,0x24,0x10,0x1B,0x54,0x24,0x14,
105 0x2B,0x44,0x24,0x08,0x1B,0x54,0x24,0x0C,0xF7,0xDA,0xF7,0xD8,0x83,0xDA,
106 0x00,0x5B,0xC2,0x10,0x00
107 };
108
109 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_aullrem, 117 ) );
110 }
111 else if( userProc.GetName() == "_allmul" ){
112 //乗算用の特殊関数(64ビット整数対応)
113 BYTE Buffer_allmul[]={
114 0x8B,0x44,0x24,0x08,0x8B,0x4C,0x24,0x10,0x0B,0xC8,0x8B,0x4C,0x24,0x0C,0x75,0x09,0x8B,0x44,0x24,0x04,0xF7,0xE1,0xC2,0x10,0x00,0x53,0xF7,0xE1,0x8B,0xD8,0x8B,0x44,0x24,0x08,0xF7,0x64,0x24,0x14,0x03,0xD8,0x8B,0x44,0x24,0x08,0xF7,0xE1,0x03,0xD3,0x5B,0xC2,0x10,0x00
115 };
116
117 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allmul, 52 ) );
118 }
119 else if( userProc.GetName() == "_alldiv" ){
120 //除算用の特殊関数(64ビット整数対応)
121 BYTE Buffer_alldiv[]={
122 0x57,0x56,0x53,0x33,0xFF,0x8B,0x44,0x24,0x14,0x0B,0xC0,0x7D,0x14,0x47,0x8B,0x54,0x24,0x10,0xF7,0xD8,0xF7,0xDA,0x83,0xD8,0x00,0x89,0x44,0x24,0x14,0x89,0x54,0x24,0x10,0x8B,0x44,0x24,0x1C,0x0B,0xC0,0x7D,0x14,0x47,0x8B,0x54,0x24,0x18,0xF7,0xD8,0xF7,0xDA,0x83,0xD8,0x00,0x89,0x44,0x24,0x1C,0x89,0x54,0x24,0x18,0x0B,0xC0,0x75,0x18,0x8B,0x4C,0x24,0x18,0x8B,0x44,0x24,0x14,0x33,0xD2,0xF7,0xF1,0x8B,0xD8,0x8B,0x44,0x24,0x10,0xF7,0xF1,0x8B,0xD3,0xEB,0x41,0x8B,0xD8,0x8B,0x4C,0x24,0x18,0x8B,0x54,0x24,0x14,0x8B,0x44,0x24,0x10,0xD1,0xEB,0xD1,0xD9,0xD1,0xEA,0xD1,0xD8,0x0B,0xDB,0x75,0xF4,0xF7,0xF1,0x8B,0xF0,0xF7,0x64,0x24,0x1C,0x8B,0xC8,0x8B,0x44,0x24,0x18,0xF7,0xE6,0x03,0xD1,0x72,0x0E,0x3B,0x54,0x24,0x14,0x77,0x08,0x72,0x07,0x3B,0x44,0x24,0x10,0x76,0x01,0x4E,0x33,0xD2,0x8B,0xC6,0x4F,0x75,0x07,0xF7,0xDA,0xF7,0xD8,0x83,0xDA,0x00,0x5B,0x5E,0x5F,0xC2,0x10,0x00
123 };
124
125 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_alldiv, 170 ) );
126 }
127 else if( userProc.GetName() == "_aulldiv" ){
128 //整数除算用の特殊関数(64ビット整数対応)
129 BYTE Buffer_aulldiv[]={
130 0x53,0x56,0x8B,0x44,0x24,0x18,0x0B,0xC0,0x75,0x18,0x8B,0x4C,0x24,0x14,
131 0x8B,0x44,0x24,0x10,0x33,0xD2,0xF7,0xF1,0x8B,0xD8,0x8B,0x44,0x24,0x0C,
132 0xF7,0xF1,0x8B,0xD3,0xEB,0x41,0x8B,0xC8,0x8B,0x5C,0x24,0x14,0x8B,0x54,
133 0x24,0x10,0x8B,0x44,0x24,0x0C,0xD1,0xE9,0xD1,0xDB,0xD1,0xEA,0xD1,0xD8,
134 0x0B,0xC9,0x75,0xF4,0xF7,0xF3,0x8B,0xF0,0xF7,0x64,0x24,0x18,0x8B,0xC8,
135 0x8B,0x44,0x24,0x14,0xF7,0xE6,0x03,0xD1,0x72,0x0E,0x3B,0x54,0x24,0x10,
136 0x77,0x08,0x72,0x07,0x3B,0x44,0x24,0x0C,0x76,0x01,0x4E,0x33,0xD2,0x8B,
137 0xC6,0x5E,0x5B,0xC2,0x10,0x00
138 };
139
140 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_aulldiv, 104 ) );
141 }
142 else if( userProc.GetName() == "_allshl" ){
143 //符号あり左ビットシフト用の特殊関数(64ビット整数対応)
144 BYTE Buffer_allshl[]={
145 0x80,0xF9,0x40,0x73,0x15,0x80,0xF9,0x20,0x73,0x06,0x0F,0xA5,0xC2,0xD3,0xE0,0xC3,0x8B,0xD0,0x33,0xC0,0x80,0xE1,0x1F,0xD3,0xE2,0xC3,0x33,0xC0,0x33,0xD2,0xC3
146 };
147
148 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allshl, 31 ) );
149 }
150 else if( userProc.GetName() == "_allshr" ){
151 //符号あり右ビットシフト用の特殊関数(64ビット整数対応)
152 BYTE Buffer_allshr[]={
153 0x80,0xF9,0x40,0x73,0x16,0x80,0xF9,0x20,0x73,0x06,0x0F,0xAD,0xD0,0xD3,0xFA,0xC3,0x8B,0xC2,0xC1,0xFA,0x1F,0x80,0xE1,0x1F,0xD3,0xF8,0xC3,0xC1,0xFA,0x1F,0x8B,0xC2,0xC3
154 };
155
156 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allshr, 33 ) );
157 }
158 else if( userProc.GetName() == "_aullshr" ){
159 //符号なし右ビットシフト用の特殊関数(64ビット整数対応)
160 BYTE Buffer_aullshr[]={
161 0x80,0xF9,0x40, //cmp cl,40h
162 0x73,0x15, //jae RETZERO (0040d71a)
163 0x80,0xF9,0x20, //cmp cl,20h
164 0x73,0x06, //jae MORE32 (0040d710)
165 0x0F,0xAD,0xD0, //shrd eax,edx,cl
166 0xD3,0xEA, //shr edx,cl
167 0xC3, //ret
168 //MORE32:
169 0x8B,0xC2, //mov eax,edx
170 0x33,0xD2, //xor edx,edx
171 0x80,0xE1,0x1F, //and cl,1Fh
172 0xD3,0xE8, //shr eax,cl
173 0xC3, //ret
174 //RETZERO:
175 0x33,0xC0, //xor eax,eax
176 0x33,0xD2, //xor edx,edx
177 0xC3 //ret
178 };
179
180 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_aullshr, 31 ) );
181 }
182 else{
183 compiler.errorMessenger.OutputFatalError();
184 }
185}
186void AutoGeneration( const UserProc &userProc){
187 if( userProc.GetName() == "InitializeUserTypes"
188 && userProc.HasParentClass()
189 && userProc.GetParentClass().GetName() == "_System_TypeBase" )
190 {
191 ActiveBasic::Compiler::ProcedureGenerator::Generate_System_InitializeUserTypes(
192 compiler.GetObjectModule().meta.GetClasses()
193 );
194 }
195 else if( userProc.GetName() == "InitializeUserTypesForBaseType"
196 && userProc.HasParentClass()
197 && userProc.GetParentClass().GetName() == "_System_TypeBase" )
198 {
199 ActiveBasic::Compiler::ProcedureGenerator::Generate_System_InitializeUserTypesForBaseType(
200 compiler.GetObjectModule().meta.GetClasses()
201 );
202 }
203 else if( userProc.GetName() == "RegisterGlobalRoots"
204 && userProc.HasParentClass()
205 && userProc.GetParentClass().GetName() == "_System_CGarbageCollection" )
206 {
207 Compile_AddGlobalRootsForGc();
208 }
209 else if( userProc.GetName() == compiler.globalAreaProcName )
210 {
211 ////////////////////////////////////////
212 // グローバル領域をコンパイル
213 ////////////////////////////////////////
214
215 UserProc::pGlobalProc = &userProc;
216
217 int BackCp = cp;
218 cp=-1;
219
220 //クラスに属する静的メンバを定義
221 ActiveBasic::Compiler::ProcedureGenerator::Generate_InitStaticMember(
222 compiler.GetObjectModule().meta.GetClasses()
223 );
224
225 //グローバル実行領域をコンパイル開始
226 CompileBuffer(0,0);
227
228 //Goto未知ラベルスケジュールが存在したらエラーにする
229 BOOST_FOREACH( const GotoLabelSchedule *pGotoLabelSchedule, compiler.codeGenerator.gotoLabelSchedules )
230 {
231 if(pGotoLabelSchedule->GetName().size()>0){
232 compiler.errorMessenger.Output(6,pGotoLabelSchedule->GetName(),pGotoLabelSchedule->GetSourceCodePos());
233 }
234 else{
235 char temporary[255];
236 sprintf(temporary,"%d",pGotoLabelSchedule->GetLineNum());
237 compiler.errorMessenger.Output(6,temporary,pGotoLabelSchedule->GetSourceCodePos());
238 }
239 }
240
241 cp=BackCp;
242 }
243 else if( userProc.HasParentClass()
244 && userProc.IsCastOperator()
245 && userProc.ReturnType().IsInterface() )
246 {
247 // インターフェイス型にキャストするためのメソッド
248
249 int vtblMasterListIndex = userProc.GetParentClass().GetVtblMasterListIndex( &userProc.ReturnType().GetClass() );
250
251 char temporary[1024];
252 sprintf( temporary,
253 "Return New %s(ObjPtr( This ),Get_LONG_PTR( (Get_LONG_PTR( ObjPtr(This)+SizeOf(VoidPtr) ) + SizeOf(LONG_PTR)*%d) As VoidPtr ) As VoidPtr )",
254 userProc.ReturnType().GetClass().GetName().c_str(),
255 vtblMasterListIndex
256 );
257 MakeMiddleCode( temporary );
258
259 ChangeOpcode( temporary );
260 }
261 else{
262 compiler.errorMessenger.OutputFatalError();
263 }
264}
265
266void _compile_proc(const UserProc *pUserProc)
267{
268 extern char *basbuf;
269 extern HANDLE hHeap;
270 int i3,i4,BaseOffset;
271 char temporary[VN_SIZE];
272
273 if( pUserProc->GetLocalVars().size() ){
274 compiler.errorMessenger.OutputFatalError();
275 return;
276 }
277
278 trace_for_sourcecodestep( "★★★ " << FormatEscapeSequenceStringToDefaultString( pUserProc->GetFullName() ) << "のコンパイルを開始" );
279
280 pUserProc->CompleteCompile();
281
282 extern BOOL bDebugSupportProc;
283 if(memcmp(pUserProc->GetName().c_str(),"_DebugSys_",10)==0)
284 {
285 if( !compiler.IsDebug() )
286 {
287 return;
288 }
289 bDebugSupportProc=1;
290 }
291 else bDebugSupportProc=0;
292
293 if( pUserProc->GetCodeSize() != 0 || pUserProc->GetNativeCode().GetSize() != 0 )
294 {
295 // 既にコード生成が行われている場合はエラー
296 compiler.errorMessenger.OutputFatalError();
297 }
298
299 compiler.StartProcedureCompile( pUserProc );
300
301 if(pUserProc->IsAutoGenerationSystem()){
302 ////////////////////
303 // 特殊関数
304 ////////////////////
305
306 extern int AllLocalVarSize;
307 AllLocalVarSize=0;
308
309 SystemProc(*pUserProc);
310 return;
311 }
312
313 if( !pUserProc->IsAutoGeneration() )
314 {
315 // 対象のソースコードを含むオブジェクトモジュールのインデックスを指定する
316 // ※テンプレート展開がされている場合、対象のソースコードが現在のオブジェクトモジュールではない場合がある
317 compiler.SetCurrentRelationalObjectModuleIndexForSource( pUserProc->GetSourceCodePosition().GetRelationalObjectModuleIndex() );
318 basbuf = const_cast<char *>(compiler.GetCurrentSource().GetBuffer());
319
320 cp=pUserProc->GetSourceCodePosition().GetPos();
321 for(;;cp++){
322 if(IsCommandDelimitation(basbuf[cp])) break;
323 }
324 cp--;
325 }
326
327 //ローカル変数に関する情報
328 extern int AllLocalVarSize;
329 AllLocalVarSize=0;
330
331 //パラメータ用の変数データを考慮
332 for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
333 Parameter &param = *pUserProc->RealParams()[i3];
334
335 Variable *pVar = new Variable(
336 ActiveBasic::Compiler::LexicalAnalyzer::FullNameToSymbol( param.GetVarName().c_str() ),
337 param,
338 false,
339 param.IsRef(),
340 "",
341 false
342 );
343
344 if( param.IsArray() ){
345 pVar->SetArray( param.GetSubscripts() );
346 }
347
348 int varSize;
349 if( param.IsRef() == false && param.IsStruct() ){
350 //構造体のByValパラメータ
351 pVar->ThisIsParameter();
352 varSize=PTR_SIZE;
353 }
354 else{
355 if( param.IsArray() == false ){
356 varSize = pVar->GetMemorySize();
357 }
358 else{
359 varSize=PTR_SIZE;
360 }
361 }
362 AllLocalVarSize+=varSize;
363 pVar->SetOffsetAddress( AllLocalVarSize );
364
365 //レキシカルスコープ情報
366 pVar->SetScopeLevel( compiler.codeGenerator.lexicalScopes.GetNowLevel() );
367 pVar->SetScopeStartAddress( compiler.codeGenerator.lexicalScopes.GetStartAddress() );
368 pVar->isLiving = true;
369
370 pUserProc->GetLocalVars().push_back( pVar );
371 }
372
373 //Thisポインタを示すローカルオフセット値をセット
374 extern int LocalVar_ThisPtrOffset;
375 LocalVar_ThisPtrOffset=AllLocalVarSize;
376
377 BaseOffset=AllLocalVarSize;
378
379 //ret用のアドレスを考慮
380 AllLocalVarSize+=sizeof(long);
381
382
383 ///////////////////////
384 // ここからコード生成
385
386 //sub esp,AllLocalVarSize(スケジュール)
387 const PertialSchedule *pAllLocalVarPertialSchedule = compiler.codeGenerator.op_sub_esp( 0, true );
388
389 //push ebp
390 compiler.codeGenerator.op_push(REG_EBP);
391
392 //mov ebp,esp
393 compiler.codeGenerator.op_mov_RR( REG_EBP, REG_ESP );
394
395 //push ebx
396 compiler.codeGenerator.op_push(REG_EBX);
397
398 //push esi
399 compiler.codeGenerator.op_push( REG_ESI );
400
401 //push edi
402 compiler.codeGenerator.op_push( REG_EDI );
403
404 if( !pUserProc->ReturnType().IsNull() ){
405 //戻り値が存在するとき
406
407 const char *temp = pUserProc->GetName().c_str();
408 if( temp[0]==1&&temp[1]==ESC_OPERATOR ){
409 temp = "_System_ReturnValue";
410 }
411
412 if( pUserProc->ReturnType().IsStruct() ){
413 //戻り値用の構造体(値型)はパラメータで引き渡される
414 }
415 else{
416 if( pUserProc->ReturnType().IsObject() ){
417 sprintf(temporary,"%s=Nothing%c%c%s",temp,1,ESC_AS, compiler.TypeToString( pUserProc->ReturnType() ).c_str() );
418 }
419 else{
420 //戻り値用の変数の定義
421 sprintf(temporary,"%s%c%c%s",temp,1,ESC_AS, compiler.TypeToString( pUserProc->ReturnType() ).c_str() );
422 }
423
424 OpcodeDim(temporary,0);
425 }
426 }
427
428 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
429 compiler.codeGenerator.exitSubCodePositions.clear();
430
431 //ラベル管理オブジェクトを初期化
432 compiler.codeGenerator.gotoLabels.clear();
433
434 //Gotoラベルスケジュール
435 compiler.codeGenerator.gotoLabelSchedules.clear();
436
437 //With情報のメモリを確保
438 extern WITHINFO WithInfo;
439 WithInfo.ppName=(char **)HeapAlloc(hHeap,0,1);
440 WithInfo.pWithCp=(int *)HeapAlloc(hHeap,0,1);
441 WithInfo.num=0;
442
443 // 重複エラー情報をクリア
444 compiler.errorMessenger.ClearSynonymKeyWords();
445
446 //Continueアドレスを初期化
447 compiler.codeGenerator.ClearContinueArea();
448
449 //レキシカルスコープ情報を初期化
450 compiler.codeGenerator.lexicalScopes.Init( compiler.codeGenerator.GetNativeCodeSize() );
451
452 const PertialSchedule *pEspOffsetPertialSchedule = NULL;
453 if( compiler.IsDebug() && bDebugSupportProc == 0 )
454 {
455 //push dword ptr[ebp+(AllLocalVarSize-BaseOffset)](スケジュール)
456 pEspOffsetPertialSchedule = compiler.codeGenerator.op_push_M( REG_EBP, 0, Schedule::None, true );
457
458 //push dword ptr[ebp](以前のebp)
459 compiler.codeGenerator.op_push_M( REG_EBP );
460
461 //call _DebugSys_StartProc
462 extern const UserProc *pSub_DebugSys_StartProc;
463 compiler.codeGenerator.op_call(pSub_DebugSys_StartProc);
464 }
465
466 if( compiler.IsCompilingClass() ){
467 if( pUserProc->GetName() == compiler.GetCompilingClass().GetName() ){
468 ////////////////////////////////////
469 // コンストラクタをコンパイルするとき
470 ////////////////////////////////////
471
472 //コンストラクタのコンパイル開始を通知
473 compiler.GetCompilingClass().NotifyStartConstructorCompile();
474
475 //基底クラスかどうかの識別
476 //(継承元がインターフェイスの場合も基底クラスと見なす)
477 BOOL bThisIsSuperClass;
478 if( !compiler.GetCompilingClass().HasSuperClass() ) bThisIsSuperClass=1;
479 else if( compiler.GetCompilingClass().GetSuperClass().GetConstructorMethod() == NULL ){
480 //インターフェイスを継承したときはコンストラクタを持たない
481 bThisIsSuperClass=1;
482 }
483 else bThisIsSuperClass=0;
484
485 if(!bThisIsSuperClass){
486 /* サブクラスコンストラクタをコンパイルしているときは、
487 基底クラスのコンストラクタを呼び出す */
488
489 i3=cp+1;
490 while(IsCommandDelimitation(basbuf[i3])) i3++;
491 for(i4=0;;i3++,i4++){
492 if(!IsVariableChar(basbuf[i3])){
493 temporary[i4]=0;
494 break;
495 }
496 temporary[i4]=basbuf[i3];
497 }
498 if( compiler.GetCompilingClass().GetSuperClass().GetName() == temporary ){
499 //基底クラスのコンストラクタを呼び出す
500 cp=i3;
501 for(i4=0;;cp++,i4++){
502 if(IsCommandDelimitation(basbuf[cp])){
503 temporary[i4]=0;
504 break;
505 }
506 temporary[i4]=basbuf[cp];
507 }
508 if(!(temporary[0]=='('&&temporary[lstrlen(temporary)-1]==')')){
509 compiler.errorMessenger.Output(1,NULL,cp);
510 }
511 RemoveStringPare(temporary);
512
513 Type dummyType;
514 CallProc( PROC_DEFAULT
515 , &compiler.GetCompilingClass().GetSuperClass().GetConstructorMethod()->GetUserProc()
516 , compiler.GetCompilingClass().GetSuperClass().GetConstructorMethod()->GetUserProc().GetName().c_str()
517 , temporary
518 , Type() // baseTypeはなし
519 , dummyType
520 );
521 }
522 else{
523 //基底クラスのコンストラクタを暗黙的に呼び出す
524 Opcode_CallProc("",
525 &compiler.GetCompilingClass().GetSuperClass().GetConstructorMethod()->GetUserProc(),
526 0,
527 ""
528 );
529 }
530 }
531 }
532 else if( pUserProc->IsDestructor() ){
533 //デストラクタをコンパイルしたとき
534
535 //デストラクタのコンパイル開始を通知
536 compiler.GetCompilingClass().NotifyStartDestructorCompile();
537 }
538 }
539
540 //////////////////////////////////////////
541 //////////////////////////////////////////
542 ////// プロシージャ内をコンパイル ////////
543 if( pUserProc->IsAutoGeneration() ){
544 AutoGeneration( *pUserProc );
545 }
546 else{
547 if(pUserProc->IsMacro()){
548 CompileBuffer(ESC_ENDMACRO,0);
549 }
550 else{
551 if(pUserProc->IsSub()){
552 CompileBuffer(ESC_ENDSUB,0);
553 }
554 else if(pUserProc->IsFunction()){
555 CompileBuffer(ESC_ENDFUNCTION,0);
556 }
557 }
558 }
559 //////////////////////////////////////////
560 //////////////////////////////////////////
561
562 if( compiler.IsCompilingClass() ){
563
564 if( compiler.GetCompilingClass().IsCompilingConstructor() ){
565 // コンストラクタをコンパイルしていたとき
566
567 // コンストラクタのコンパイルが完了したことを通知
568 compiler.GetCompilingClass().NotifyFinishConstructorCompile();
569 }
570 else if( pUserProc->IsDestructor() ){
571 ////////////////////////////////////
572 //デストラクタをコンパイルしたとき
573 ////////////////////////////////////
574
575 // デストラクタのコンパイルが完了したことを通知
576 compiler.GetCompilingClass().NotifyFinishDestructorCompile();
577
578 if( compiler.GetCompilingClass().HasSuperClass() ){
579 /* サブクラスのデストラクタをコンパイルしているときは、
580 基底クラスのデストラクタを呼び出す */
581
582 const CMethod *method = compiler.GetCompilingClass().GetSuperClass().GetDestructorMethod();
583 if( method ){
584 Opcode_CallProc("",
585 &method->GetUserProc(),
586 0,
587 ""
588 );
589 }
590 }
591 }
592 }
593
594 //With情報のメモリを解放
595 for(i3=0;i3<WithInfo.num;i3++){
596 compiler.errorMessenger.Output(22,"With",WithInfo.pWithCp[i3]);
597 HeapDefaultFree(WithInfo.ppName[i3]);
598 }
599 HeapDefaultFree(WithInfo.ppName);
600 HeapDefaultFree(WithInfo.pWithCp);
601
602 //push ebp
603 AllLocalVarSize+=sizeof(long);
604
605 //ローカルオブジェクトの解放処理
606 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
607
608 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
609 compiler.codeGenerator.ResolveExitSubSchedule();
610
611 if( compiler.IsDebug() && bDebugSupportProc == 0 )
612 {
613 compiler.codeGenerator.opfix( pEspOffsetPertialSchedule, AllLocalVarSize-BaseOffset-sizeof(long) );
614
615 //call _DebugSys_EndProc
616 extern const UserProc *pSub_DebugSys_EndProc;
617 compiler.codeGenerator.op_call(pSub_DebugSys_EndProc);
618 }
619
620 if( !pUserProc->ReturnType().IsNull() ){
621 //戻り値をeax、edxに設定
622 RELATIVE_VAR RelativeVar;
623
624 const char *temp = pUserProc->GetName().c_str();
625 if( temp[0]==1 && temp[1]==ESC_OPERATOR ){
626 temp="_System_ReturnValue";
627 }
628 GetVarOffsetReadWrite(temp,&RelativeVar,Type());
629
630 const Type &returnType = pUserProc->ReturnType();
631 if( returnType.IsObject() || returnType.IsStruct() )
632 {
633 SetVarPtrToEax(&RelativeVar);
634 if( returnType.IsObject() )
635 {
636 //mov eax,dword ptr[eax]
637 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
638 }
639 }
640 else if( returnType.IsReal() )
641 {
642 //fld qword ptr[ebp+offset]
643 compiler.codeGenerator.localVarPertialSchedules.push_back(
644 compiler.codeGenerator.op_fld_base_offset( returnType.GetBasicType(), REG_EBP, RelativeVar.offset, Schedule::None, true )
645 );
646 }
647 else if( returnType.Is64() )
648 {
649 //mov eax,dword ptr[ebp+offset]
650 compiler.codeGenerator.localVarPertialSchedules.push_back(
651 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
652 );
653
654 //mov edx,dword ptr[ebp+offset+sizeof(long)]
655 compiler.codeGenerator.localVarPertialSchedules.push_back(
656 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_EBP, RelativeVar.offset+sizeof(long), MOD_BASE_DISP32, Schedule::None, true )
657 );
658 }
659 else if( returnType.GetSize() == sizeof(long) )
660 {
661 //mov eax,dword ptr[ebp+offset]
662 compiler.codeGenerator.localVarPertialSchedules.push_back(
663 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
664 );
665 }
666 else if( returnType.GetSize() == sizeof(short) )
667 {
668 //xor eax,eax(eaxを0に初期化する)
669 compiler.codeGenerator.op_zero_reg(REG_EAX);
670
671 //mov ax,word ptr[ebp+offset]
672 compiler.codeGenerator.localVarPertialSchedules.push_back(
673 compiler.codeGenerator.op_mov_RM( sizeof(short), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
674 );
675 }
676 else if( returnType.GetSize() == sizeof(char) )
677 {
678 //xor eax,eax(eaxを0に初期化する)
679 compiler.codeGenerator.op_zero_reg(REG_EAX);
680
681 //mov al,byte ptr[ebp+offset]
682 compiler.codeGenerator.localVarPertialSchedules.push_back(
683 compiler.codeGenerator.op_mov_RM( sizeof(char), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
684 );
685 }
686 else
687 {
688 compiler.errorMessenger.OutputFatalError();
689 }
690 }
691
692 //ローカル変数アドレススケジュール
693 BOOST_FOREACH( const PertialSchedule *pPertialSchedule, compiler.codeGenerator.localVarPertialSchedules )
694 {
695 compiler.codeGenerator.opfix_offset( pPertialSchedule, AllLocalVarSize );
696 }
697 compiler.codeGenerator.localVarPertialSchedules.clear();
698 BOOST_FOREACH( Variable *pVar, pUserProc->GetLocalVars() ){
699 //後にデバッグで利用する
700 pVar->SetOffsetAddress( AllLocalVarSize - pVar->GetOffsetAddress() );
701 }
702
703 //push ebp、ret用のアドレスを考慮
704 AllLocalVarSize-=sizeof(long)*2;
705
706 //ローカル変数用メモリを確保するためのスケジュール(subコマンド)
707 compiler.codeGenerator.opfix( pAllLocalVarPertialSchedule, AllLocalVarSize - BaseOffset );
708
709 //pop edi
710 compiler.codeGenerator.op_pop( REG_EDI );
711
712 //pop esi
713 compiler.codeGenerator.op_pop( REG_ESI );
714
715 //pop ebx
716 compiler.codeGenerator.op_pop(REG_EBX);
717
718 if( compiler.IsDebug() )
719 {
720 //cmp esp,ebp
721 compiler.codeGenerator.op_cmp_RR( REG_ESP, REG_EBP );
722
723 //je 6(次のcallとbreakpointを飛び越す)
724 compiler.codeGenerator.op_je( 6 );
725
726 //call _esp_error
727 extern const UserProc *pSub_esp_error;
728 compiler.codeGenerator.op_call( pSub_esp_error );
729
730 breakpoint;
731 }
732
733 //mov esp,ebp
734 compiler.codeGenerator.op_mov_RR( REG_ESP, REG_EBP );
735
736 //pop ebp
737 compiler.codeGenerator.op_pop(REG_EBP);
738
739 //add esp AllLocalVarSize
740 compiler.codeGenerator.op_add_esp(AllLocalVarSize-BaseOffset);
741
742 if( BaseOffset==0 || pUserProc->IsCdecl() ){
743 //ret
744 compiler.codeGenerator.op_ret();
745 }
746 else{
747 //ret BaseOffset(パラメータ分のスタック領域を解放)
748 compiler.codeGenerator.op_ret( (_int16)BaseOffset );
749 }
750
751 compiler.FinishProcedureCompile();
752
753
754 //ローカル変数のネーム情報は後に解放する
755}
Note: See TracBrowser for help on using the repository browser.