source: dev/trunk/abdev/BasicCompiler32/Compile_ProcOp.cpp@ 345

Last change on this file since 345 was 331, checked in by dai_9181, 17 years ago

AddressOfの左辺値を加味した処理を簡潔にした

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