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

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

NamespaceSupporterクラスをab_commonプロジェクトに移動した。

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