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

Last change on this file since 361 was 358, checked in by dai_9181, 17 years ago

Try-Catchを試験的に実装。
(まだ下記の動作しか実装していません)
・Try
・Catch(パラメータ無し)
・Throw(パラメータ無し)

File size: 25.3 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() == "_allrem" ){
107 //乗除演算用の特殊関数(64ビット整数対応)
108 BYTE Buffer_allrem[]={
109 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
110 };
111
112 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allrem, 178 ) );
113 }
114 else if( userProc.GetName() == "_aullrem" ){
115 //乗除演算用の特殊関数(64ビット整数対応)
116 BYTE Buffer_aullrem[]={
117 0x53,0x8B,0x44,0x24,0x14,0x0B,0xC0,0x75,0x18,0x8B,0x4C,0x24,0x10,0x8B,
118 0x44,0x24,0x0C,0x33,0xD2,0xF7,0xF1,0x8B,0x44,0x24,0x08,0xF7,0xF1,0x8B,
119 0xC2,0x33,0xD2,0xEB,0x50,0x8B,0xC8,0x8B,0x5C,0x24,0x10,0x8B,0x54,0x24,
120 0x0C,0x8B,0x44,0x24,0x08,0xD1,0xE9,0xD1,0xDB,0xD1,0xEA,0xD1,0xD8,0x0B,
121 0xC9,0x75,0xF4,0xF7,0xF3,0x8B,0xC8,0xF7,0x64,0x24,0x14,0x91,0xF7,0x64,
122 0x24,0x10,0x03,0xD1,0x72,0x0E,0x3B,0x54,0x24,0x0C,0x77,0x08,0x72,0x0E,
123 0x3B,0x44,0x24,0x08,0x76,0x08,0x2B,0x44,0x24,0x10,0x1B,0x54,0x24,0x14,
124 0x2B,0x44,0x24,0x08,0x1B,0x54,0x24,0x0C,0xF7,0xDA,0xF7,0xD8,0x83,0xDA,
125 0x00,0x5B,0xC2,0x10,0x00
126 };
127
128 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_aullrem, 117 ) );
129 }
130 else if( userProc.GetName() == "_allmul" ){
131 //乗算用の特殊関数(64ビット整数対応)
132 BYTE Buffer_allmul[]={
133 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
134 };
135
136 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allmul, 52 ) );
137 }
138 else if( userProc.GetName() == "_alldiv" ){
139 //除算用の特殊関数(64ビット整数対応)
140 BYTE Buffer_alldiv[]={
141 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
142 };
143
144 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_alldiv, 170 ) );
145 }
146 else if( userProc.GetName() == "_aulldiv" ){
147 //整数除算用の特殊関数(64ビット整数対応)
148 BYTE Buffer_aulldiv[]={
149 0x53,0x56,0x8B,0x44,0x24,0x18,0x0B,0xC0,0x75,0x18,0x8B,0x4C,0x24,0x14,
150 0x8B,0x44,0x24,0x10,0x33,0xD2,0xF7,0xF1,0x8B,0xD8,0x8B,0x44,0x24,0x0C,
151 0xF7,0xF1,0x8B,0xD3,0xEB,0x41,0x8B,0xC8,0x8B,0x5C,0x24,0x14,0x8B,0x54,
152 0x24,0x10,0x8B,0x44,0x24,0x0C,0xD1,0xE9,0xD1,0xDB,0xD1,0xEA,0xD1,0xD8,
153 0x0B,0xC9,0x75,0xF4,0xF7,0xF3,0x8B,0xF0,0xF7,0x64,0x24,0x18,0x8B,0xC8,
154 0x8B,0x44,0x24,0x14,0xF7,0xE6,0x03,0xD1,0x72,0x0E,0x3B,0x54,0x24,0x10,
155 0x77,0x08,0x72,0x07,0x3B,0x44,0x24,0x0C,0x76,0x01,0x4E,0x33,0xD2,0x8B,
156 0xC6,0x5E,0x5B,0xC2,0x10,0x00
157 };
158
159 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_aulldiv, 104 ) );
160 }
161 else if( userProc.GetName() == "_allshl" ){
162 //符号あり左ビットシフト用の特殊関数(64ビット整数対応)
163 BYTE Buffer_allshl[]={
164 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
165 };
166
167 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allshl, 31 ) );
168 }
169 else if( userProc.GetName() == "_allshr" ){
170 //符号あり右ビットシフト用の特殊関数(64ビット整数対応)
171 BYTE Buffer_allshr[]={
172 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
173 };
174
175 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allshr, 33 ) );
176 }
177 else if( userProc.GetName() == "_aullshr" ){
178 //符号なし右ビットシフト用の特殊関数(64ビット整数対応)
179 BYTE Buffer_aullshr[]={
180 0x80,0xF9,0x40, //cmp cl,40h
181 0x73,0x15, //jae RETZERO (0040d71a)
182 0x80,0xF9,0x20, //cmp cl,20h
183 0x73,0x06, //jae MORE32 (0040d710)
184 0x0F,0xAD,0xD0, //shrd eax,edx,cl
185 0xD3,0xEA, //shr edx,cl
186 0xC3, //ret
187 //MORE32:
188 0x8B,0xC2, //mov eax,edx
189 0x33,0xD2, //xor edx,edx
190 0x80,0xE1,0x1F, //and cl,1Fh
191 0xD3,0xE8, //shr eax,cl
192 0xC3, //ret
193 //RETZERO:
194 0x33,0xC0, //xor eax,eax
195 0x33,0xD2, //xor edx,edx
196 0xC3 //ret
197 };
198
199 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_aullshr, 31 ) );
200 }
201 else{
202 SetError();
203 }
204}
205void AutoGeneration( const UserProc &userProc){
206 if( userProc.GetName() == "InitializeUserTypes"
207 && userProc.HasParentClass()
208 && userProc.GetParentClass().GetName() == "_System_TypeBase" ){
209
210 compiler.GetObjectModule().meta.GetClasses().Compile_System_InitializeUserTypes();
211 }
212 else if( userProc.GetName() == "InitializeUserTypesForBaseType"
213 && userProc.HasParentClass()
214 && userProc.GetParentClass().GetName() == "_System_TypeBase" )
215 {
216 compiler.GetObjectModule().meta.GetClasses().Compile_System_InitializeUserTypesForBaseType();
217 }
218 else if( userProc.GetName() == "RegisterGlobalRoots"
219 && userProc.HasParentClass()
220 && userProc.GetParentClass().GetName() == "_System_CGarbageCollection" ){
221
222 Compile_AddGlobalRootsForGc();
223 }
224 else if( userProc.GetName() == compiler.globalAreaProcName ){
225 ////////////////////////////////////////
226 // グローバル領域をコンパイル
227 ////////////////////////////////////////
228
229 const UserProc *pBackUserProc = &UserProc::CompilingUserProc();
230 UserProc::CompileStartForGlobalArea();
231
232 int BackCp = cp;
233 cp=-1;
234
235 //クラスに属する静的メンバを定義
236 compiler.GetObjectModule().meta.GetClasses().InitStaticMember();
237
238 //グローバル実行領域をコンパイル開始
239 CompileBuffer(0,0);
240
241 //Goto未知ラベルスケジュールが存在したらエラーにする
242 BOOST_FOREACH( const GotoLabelSchedule *pGotoLabelSchedule, compiler.codeGenerator.gotoLabelSchedules )
243 {
244 if(pGotoLabelSchedule->GetName().size()>0){
245 SetError(6,pGotoLabelSchedule->GetName(),pGotoLabelSchedule->GetSourceCodePos());
246 }
247 else{
248 char temporary[255];
249 sprintf(temporary,"%d",pGotoLabelSchedule->GetLineNum());
250 SetError(6,temporary,pGotoLabelSchedule->GetSourceCodePos());
251 }
252 }
253
254 UserProc::CompileStartForUserProc( pBackUserProc );
255 cp=BackCp;
256 }
257 else if( userProc.HasParentClass()
258 && userProc.IsCastOperator()
259 && userProc.ReturnType().IsInterface() )
260 {
261 // インターフェイス型にキャストするためのメソッド
262
263 int vtblMasterListIndex = userProc.GetParentClass().GetVtblMasterListIndex( &userProc.ReturnType().GetClass() );
264
265 char temporary[1024];
266 sprintf( temporary,
267 "Return New %s(ObjPtr( This ),Get_LONG_PTR( (Get_LONG_PTR( ObjPtr(This) ) + SizeOf(LONG_PTR)*%d) As VoidPtr ) As VoidPtr )",
268 userProc.ReturnType().GetClass().GetName().c_str(),
269 vtblMasterListIndex
270 );
271 MakeMiddleCode( temporary );
272
273 ChangeOpcode( temporary );
274 }
275 else{
276 SetError();
277 }
278}
279
280void _compile_proc(const UserProc *pUserProc)
281{
282 extern char *basbuf;
283 extern HANDLE hHeap;
284 extern BOOL bDebugCompile;
285 int i3,i4,BaseOffset;
286 char temporary[VN_SIZE];
287
288 if( pUserProc->GetLocalVars().size() ){
289 SetError();
290 return;
291 }
292
293 trace_for_sourcecodestep( "★★★ " << pUserProc->GetFullName() << "のコンパイルを開始" );
294
295 pUserProc->CompleteCompile();
296
297 extern BOOL bSystemProc;
298 if(memcmp(pUserProc->GetName().c_str(),"_System_",8)==0) bSystemProc=1;
299 else bSystemProc=0;
300
301 extern BOOL bDebugSupportProc;
302 if(memcmp(pUserProc->GetName().c_str(),"_DebugSys_",10)==0){
303 if(!bDebugCompile){
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->bLiving=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(bDebugCompile&&bDebugSupportProc==0){
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 SetError(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 SetError(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(bDebugCompile&&bDebugSupportProc==0){
635 compiler.codeGenerator.opfix( pEspOffsetPertialSchedule, AllLocalVarSize-BaseOffset-sizeof(long) );
636
637 //call _DebugSys_EndProc
638 extern const UserProc *pSub_DebugSys_EndProc;
639 compiler.codeGenerator.op_call(pSub_DebugSys_EndProc);
640 }
641
642 if( !pUserProc->ReturnType().IsNull() ){
643 //戻り値をeax、edxに設定
644 RELATIVE_VAR RelativeVar;
645
646 const char *temp = pUserProc->GetName().c_str();
647 if( temp[0]==1 && temp[1]==ESC_OPERATOR ){
648 temp="_System_ReturnValue";
649 }
650 GetVarOffsetReadWrite(temp,&RelativeVar,Type());
651
652 const Type &returnType = pUserProc->ReturnType();
653 if( returnType.IsObject() || returnType.IsStruct() )
654 {
655 SetVarPtrToEax(&RelativeVar);
656 if( returnType.IsObject() )
657 {
658 //mov eax,dword ptr[eax]
659 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
660 }
661 }
662 else if( returnType.IsReal() )
663 {
664 //fld qword ptr[ebp+offset]
665 compiler.codeGenerator.localVarPertialSchedules.push_back(
666 compiler.codeGenerator.op_fld_base_offset( returnType.GetBasicType(), REG_EBP, RelativeVar.offset, Schedule::None, true )
667 );
668 }
669 else if( returnType.Is64() )
670 {
671 //mov eax,dword ptr[ebp+offset]
672 compiler.codeGenerator.localVarPertialSchedules.push_back(
673 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
674 );
675
676 //mov edx,dword ptr[ebp+offset+sizeof(long)]
677 compiler.codeGenerator.localVarPertialSchedules.push_back(
678 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_EBP, RelativeVar.offset+sizeof(long), MOD_BASE_DISP32, Schedule::None, true )
679 );
680 }
681 else if( returnType.GetSize() == sizeof(long) )
682 {
683 //mov eax,dword ptr[ebp+offset]
684 compiler.codeGenerator.localVarPertialSchedules.push_back(
685 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
686 );
687 }
688 else if( returnType.GetSize() == sizeof(short) )
689 {
690 //xor eax,eax(eaxを0に初期化する)
691 compiler.codeGenerator.op_zero_reg(REG_EAX);
692
693 //mov ax,word ptr[ebp+offset]
694 compiler.codeGenerator.localVarPertialSchedules.push_back(
695 compiler.codeGenerator.op_mov_RM( sizeof(short), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
696 );
697 }
698 else if( returnType.GetSize() == sizeof(char) )
699 {
700 //xor eax,eax(eaxを0に初期化する)
701 compiler.codeGenerator.op_zero_reg(REG_EAX);
702
703 //mov al,byte ptr[ebp+offset]
704 compiler.codeGenerator.localVarPertialSchedules.push_back(
705 compiler.codeGenerator.op_mov_RM( sizeof(char), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
706 );
707 }
708 else
709 {
710 SetError();
711 }
712 }
713
714 //ローカル変数アドレススケジュール
715 BOOST_FOREACH( const PertialSchedule *pPertialSchedule, compiler.codeGenerator.localVarPertialSchedules )
716 {
717 compiler.codeGenerator.opfix_offset( pPertialSchedule, AllLocalVarSize );
718 }
719 compiler.codeGenerator.localVarPertialSchedules.clear();
720 BOOST_FOREACH( Variable *pVar, pUserProc->GetLocalVars() ){
721 //後にデバッグで利用する
722 pVar->SetOffsetAddress( AllLocalVarSize - pVar->GetOffsetAddress() );
723 }
724
725 //push ebp、ret用のアドレスを考慮
726 AllLocalVarSize-=sizeof(long)*2;
727
728 //ローカル変数用メモリを確保するためのスケジュール(subコマンド)
729 compiler.codeGenerator.opfix( pAllLocalVarPertialSchedule, AllLocalVarSize - BaseOffset );
730
731 //pop edi
732 compiler.codeGenerator.op_pop( REG_EDI );
733
734 //pop esi
735 compiler.codeGenerator.op_pop( REG_ESI );
736
737 //pop ebx
738 compiler.codeGenerator.op_pop(REG_EBX);
739
740 if(bDebugCompile){
741 //cmp esp,ebp
742 compiler.codeGenerator.op_cmp_RR( REG_ESP, REG_EBP );
743
744 //je 6(次のcallとbreakpointを飛び越す)
745 compiler.codeGenerator.op_je( 6 );
746
747 //call _esp_error
748 extern const UserProc *pSub_esp_error;
749 compiler.codeGenerator.op_call( pSub_esp_error );
750
751 breakpoint;
752 }
753
754 //mov esp,ebp
755 compiler.codeGenerator.op_mov_RR( REG_ESP, REG_EBP );
756
757 //pop ebp
758 compiler.codeGenerator.op_pop(REG_EBP);
759
760 //add esp AllLocalVarSize
761 compiler.codeGenerator.op_add_esp(AllLocalVarSize-BaseOffset);
762
763 if( BaseOffset==0 || pUserProc->IsCdecl() ){
764 //ret
765 compiler.codeGenerator.op_ret();
766 }
767 else{
768 //ret BaseOffset(パラメータ分のスタック領域を解放)
769 compiler.codeGenerator.op_ret( (_int16)BaseOffset );
770 }
771
772
773 //重複エラー情報管理のメモリを解放
774 for(i3=0;i3<SynonymErrorNum;i3++) HeapDefaultFree(SynonymErrorWords[i3]);
775 HeapDefaultFree(SynonymErrorWords);
776 SynonymErrorWords=0;
777
778
779 //ローカル変数のネーム情報は後に解放する
780}
Note: See TracBrowser for help on using the repository browser.