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

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

libファイルを跨ったテンプレート展開に対応。

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