source: dev/trunk/ab5.0/abdev/BasicCompiler32/Compile_ProcOp.cpp@ 461

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

smoothieプロジェクトが不要になったため、破棄。

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