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

Last change on this file since 421 was 402, checked in by dai_9181, 17 years ago

UserProc::SetParamsAndReturnTypeメソッドをリファクタリング
LexicalAnalysis.hのインクルードを除去した

File size: 25.3 KB
Line 
1#include "stdafx.h"
2
3#include <jenga/include/smoothie/Smoothie.h>
4
5#include <Program.h>
6#include <Compiler.h>
7#include <LexicalScope.h>
8#include <Class.h>
9#include <Variable.h>
10#include <NamespaceSupporter.h>
11
12#include "../BasicCompiler_Common/common.h"
13#include "Opcode.h"
14
15
16void SystemProc( const UserProc &userProc ){
17 if( userProc.GetName() == "_System_GetEip" ){
18 //mov eax,dword ptr[esp]
19 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_ESP, 0, MOD_BASE );
20
21 //ret
22 compiler.codeGenerator.op_ret();
23 }
24 else if( userProc.GetName() == "_System_InitDllGlobalVariables" ){
25 ////////////////////////////////////////
26 // DLLのグローバル領域をコンパイル
27 ////////////////////////////////////////
28
29 if( !compiler.IsDll() ){
30 //ret
31 compiler.codeGenerator.op_ret();
32
33 return;
34 }
35
36 const UserProc *pBackUserProc;
37 pBackUserProc = &UserProc::CompilingUserProc();
38 UserProc::CompileStartForGlobalArea();
39
40 int BackCp;
41 BackCp=cp;
42 cp=-1;
43
44 extern BOOL bDebugCompile;
45 if(bDebugCompile){
46 //デバッグ用の変数を定義
47 DebugVariable();
48 }
49
50 //GC用の変数を定義
51 InitGCVariables();
52
53 //_System_StartupProgramの呼び出し
54 extern const UserProc *pSub_System_StartupProgram;
55 compiler.codeGenerator.op_call(pSub_System_StartupProgram);
56
57 //クラスに属する静的メンバを定義
58 compiler.GetObjectModule().meta.GetClasses().InitStaticMember();
59
60 GetGlobalDataForDll();
61
62 UserProc::CompileStartForUserProc( pBackUserProc );
63 cp=BackCp;
64
65 //ret
66 compiler.codeGenerator.op_ret();
67 }
68 else if( userProc.GetName() == "_System_InitStaticLocalVariables" ){
69 //静的ローカルオブジェクトのコンストラクタ呼び出し
70
71 BOOST_FOREACH( Variable *pVar, compiler.GetObjectModule().meta.GetGlobalVars() ){
72 if(memicmp(pVar->GetName().c_str(),"Static%",7)==0){
73 //コンストラクタ呼び出し
74 if( pVar->GetType().IsObject() ){
75
76 //エラー用
77 cp=pVar->source_code_address;
78
79 CallConstructor(
80 pVar->GetName().c_str(),
81 pVar->GetSubscripts(),
82 pVar->GetType(),
83 pVar->GetParamStrForConstructor().c_str());
84 }
85 }
86 }
87
88 //ret
89 compiler.codeGenerator.op_ret();
90 }
91 else if( userProc.GetName() == "_System_Call_Destructor_of_GlobalObject" ){
92
93 const UserProc *pBackUserProc;
94 pBackUserProc = &UserProc::CompilingUserProc();
95 UserProc::CompileStartForGlobalArea();
96
97 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
98
99 UserProc::CompileStartForUserProc( pBackUserProc );
100
101
102 //ret
103 compiler.codeGenerator.op_ret();
104 }
105 else if( userProc.GetName() == "_allrem" ){
106 //乗除演算用の特殊関数(64ビット整数対応)
107 BYTE Buffer_allrem[]={
108 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
109 };
110
111 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allrem, 178 ) );
112 }
113 else if( userProc.GetName() == "_aullrem" ){
114 //乗除演算用の特殊関数(64ビット整数対応)
115 BYTE Buffer_aullrem[]={
116 0x53,0x8B,0x44,0x24,0x14,0x0B,0xC0,0x75,0x18,0x8B,0x4C,0x24,0x10,0x8B,
117 0x44,0x24,0x0C,0x33,0xD2,0xF7,0xF1,0x8B,0x44,0x24,0x08,0xF7,0xF1,0x8B,
118 0xC2,0x33,0xD2,0xEB,0x50,0x8B,0xC8,0x8B,0x5C,0x24,0x10,0x8B,0x54,0x24,
119 0x0C,0x8B,0x44,0x24,0x08,0xD1,0xE9,0xD1,0xDB,0xD1,0xEA,0xD1,0xD8,0x0B,
120 0xC9,0x75,0xF4,0xF7,0xF3,0x8B,0xC8,0xF7,0x64,0x24,0x14,0x91,0xF7,0x64,
121 0x24,0x10,0x03,0xD1,0x72,0x0E,0x3B,0x54,0x24,0x0C,0x77,0x08,0x72,0x0E,
122 0x3B,0x44,0x24,0x08,0x76,0x08,0x2B,0x44,0x24,0x10,0x1B,0x54,0x24,0x14,
123 0x2B,0x44,0x24,0x08,0x1B,0x54,0x24,0x0C,0xF7,0xDA,0xF7,0xD8,0x83,0xDA,
124 0x00,0x5B,0xC2,0x10,0x00
125 };
126
127 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_aullrem, 117 ) );
128 }
129 else if( userProc.GetName() == "_allmul" ){
130 //乗算用の特殊関数(64ビット整数対応)
131 BYTE Buffer_allmul[]={
132 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
133 };
134
135 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allmul, 52 ) );
136 }
137 else if( userProc.GetName() == "_alldiv" ){
138 //除算用の特殊関数(64ビット整数対応)
139 BYTE Buffer_alldiv[]={
140 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
141 };
142
143 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_alldiv, 170 ) );
144 }
145 else if( userProc.GetName() == "_aulldiv" ){
146 //整数除算用の特殊関数(64ビット整数対応)
147 BYTE Buffer_aulldiv[]={
148 0x53,0x56,0x8B,0x44,0x24,0x18,0x0B,0xC0,0x75,0x18,0x8B,0x4C,0x24,0x14,
149 0x8B,0x44,0x24,0x10,0x33,0xD2,0xF7,0xF1,0x8B,0xD8,0x8B,0x44,0x24,0x0C,
150 0xF7,0xF1,0x8B,0xD3,0xEB,0x41,0x8B,0xC8,0x8B,0x5C,0x24,0x14,0x8B,0x54,
151 0x24,0x10,0x8B,0x44,0x24,0x0C,0xD1,0xE9,0xD1,0xDB,0xD1,0xEA,0xD1,0xD8,
152 0x0B,0xC9,0x75,0xF4,0xF7,0xF3,0x8B,0xF0,0xF7,0x64,0x24,0x18,0x8B,0xC8,
153 0x8B,0x44,0x24,0x14,0xF7,0xE6,0x03,0xD1,0x72,0x0E,0x3B,0x54,0x24,0x10,
154 0x77,0x08,0x72,0x07,0x3B,0x44,0x24,0x0C,0x76,0x01,0x4E,0x33,0xD2,0x8B,
155 0xC6,0x5E,0x5B,0xC2,0x10,0x00
156 };
157
158 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_aulldiv, 104 ) );
159 }
160 else if( userProc.GetName() == "_allshl" ){
161 //符号あり左ビットシフト用の特殊関数(64ビット整数対応)
162 BYTE Buffer_allshl[]={
163 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
164 };
165
166 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allshl, 31 ) );
167 }
168 else if( userProc.GetName() == "_allshr" ){
169 //符号あり右ビットシフト用の特殊関数(64ビット整数対応)
170 BYTE Buffer_allshr[]={
171 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
172 };
173
174 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allshr, 33 ) );
175 }
176 else if( userProc.GetName() == "_aullshr" ){
177 //符号なし右ビットシフト用の特殊関数(64ビット整数対応)
178 BYTE Buffer_aullshr[]={
179 0x80,0xF9,0x40, //cmp cl,40h
180 0x73,0x15, //jae RETZERO (0040d71a)
181 0x80,0xF9,0x20, //cmp cl,20h
182 0x73,0x06, //jae MORE32 (0040d710)
183 0x0F,0xAD,0xD0, //shrd eax,edx,cl
184 0xD3,0xEA, //shr edx,cl
185 0xC3, //ret
186 //MORE32:
187 0x8B,0xC2, //mov eax,edx
188 0x33,0xD2, //xor edx,edx
189 0x80,0xE1,0x1F, //and cl,1Fh
190 0xD3,0xE8, //shr eax,cl
191 0xC3, //ret
192 //RETZERO:
193 0x33,0xC0, //xor eax,eax
194 0x33,0xD2, //xor edx,edx
195 0xC3 //ret
196 };
197
198 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_aullshr, 31 ) );
199 }
200 else{
201 SetError();
202 }
203}
204void AutoGeneration( const UserProc &userProc){
205 if( userProc.GetName() == "InitializeUserTypes"
206 && userProc.HasParentClass()
207 && userProc.GetParentClass().GetName() == "_System_TypeBase" ){
208
209 compiler.GetObjectModule().meta.GetClasses().Compile_System_InitializeUserTypes();
210 }
211 else if( userProc.GetName() == "InitializeUserTypesForBaseType"
212 && userProc.HasParentClass()
213 && userProc.GetParentClass().GetName() == "_System_TypeBase" )
214 {
215 compiler.GetObjectModule().meta.GetClasses().Compile_System_InitializeUserTypesForBaseType();
216 }
217 else if( userProc.GetName() == "RegisterGlobalRoots"
218 && userProc.HasParentClass()
219 && userProc.GetParentClass().GetName() == "_System_CGarbageCollection" ){
220
221 Compile_AddGlobalRootsForGc();
222 }
223 else if( userProc.GetName() == compiler.globalAreaProcName ){
224 ////////////////////////////////////////
225 // グローバル領域をコンパイル
226 ////////////////////////////////////////
227
228 UserProc::pGlobalProc = &userProc;
229
230 const UserProc *pBackUserProc = &UserProc::CompilingUserProc();
231 UserProc::CompileStartForGlobalArea();
232
233 int BackCp = cp;
234 cp=-1;
235
236 //クラスに属する静的メンバを定義
237 compiler.GetObjectModule().meta.GetClasses().InitStaticMember();
238
239 //グローバル実行領域をコンパイル開始
240 CompileBuffer(0,0);
241
242 //Goto未知ラベルスケジュールが存在したらエラーにする
243 BOOST_FOREACH( const GotoLabelSchedule *pGotoLabelSchedule, compiler.codeGenerator.gotoLabelSchedules )
244 {
245 if(pGotoLabelSchedule->GetName().size()>0){
246 SetError(6,pGotoLabelSchedule->GetName(),pGotoLabelSchedule->GetSourceCodePos());
247 }
248 else{
249 char temporary[255];
250 sprintf(temporary,"%d",pGotoLabelSchedule->GetLineNum());
251 SetError(6,temporary,pGotoLabelSchedule->GetSourceCodePos());
252 }
253 }
254
255 UserProc::CompileStartForUserProc( pBackUserProc );
256 cp=BackCp;
257 }
258 else if( userProc.HasParentClass()
259 && userProc.IsCastOperator()
260 && userProc.ReturnType().IsInterface() )
261 {
262 // インターフェイス型にキャストするためのメソッド
263
264 int vtblMasterListIndex = userProc.GetParentClass().GetVtblMasterListIndex( &userProc.ReturnType().GetClass() );
265
266 char temporary[1024];
267 sprintf( temporary,
268 "Return New %s(ObjPtr( This ),Get_LONG_PTR( (Get_LONG_PTR( ObjPtr(This)+SizeOf(VoidPtr) ) + SizeOf(LONG_PTR)*%d) As VoidPtr ) As VoidPtr )",
269 userProc.ReturnType().GetClass().GetName().c_str(),
270 vtblMasterListIndex
271 );
272 MakeMiddleCode( temporary );
273
274 ChangeOpcode( temporary );
275 }
276 else{
277 SetError();
278 }
279}
280
281void _compile_proc(const UserProc *pUserProc)
282{
283 extern char *basbuf;
284 extern HANDLE hHeap;
285 extern BOOL bDebugCompile;
286 int i3,i4,BaseOffset;
287 char temporary[VN_SIZE];
288
289 if( pUserProc->GetLocalVars().size() ){
290 SetError();
291 return;
292 }
293
294 trace_for_sourcecodestep( "★★★ " << pUserProc->GetFullName() << "のコンパイルを開始" );
295
296 pUserProc->CompleteCompile();
297
298 extern BOOL bSystemProc;
299 if(memcmp(pUserProc->GetName().c_str(),"_System_",8)==0) bSystemProc=1;
300 else bSystemProc=0;
301
302 extern BOOL bDebugSupportProc;
303 if(memcmp(pUserProc->GetName().c_str(),"_DebugSys_",10)==0){
304 if(!bDebugCompile){
305 return;
306 }
307 bDebugSupportProc=1;
308 }
309 else bDebugSupportProc=0;
310
311 if( pUserProc->GetCodeSize() != 0 || pUserProc->GetNativeCode().GetSize() != 0 )
312 {
313 // 既にコード生成が行われている場合はエラー
314 SetError();
315 }
316
317 //コンパイル中の関数が属するクラス
318 compiler.pCompilingClass = pUserProc->GetParentClassPtr();
319
320 //コンパイルスタートをクラス管理クラスに追加
321 compiler.GetObjectModule().meta.GetClasses().StartCompile( pUserProc );
322
323 //コンパイル中の関数
324 UserProc::CompileStartForUserProc( pUserProc );
325
326 // コンパイル中の関数が属する名前空間
327 compiler.GetNamespaceSupporter().SetLivingNamespaceScopes( pUserProc->GetNamespaceScopes() );
328
329 // コンパイル中の関数でImportsされている名前空間
330 compiler.GetNamespaceSupporter().SetImportedNamespaces( pUserProc->GetImportedNamespaces() );
331
332 // コード生成対象を選択
333 compiler.codeGenerator.Select( (const_cast<UserProc *>(pUserProc))->GetNativeCode() );
334
335 if(pUserProc->IsSystem()){
336 ////////////////////
337 // 特殊関数
338 ////////////////////
339
340 extern int AllLocalVarSize;
341 AllLocalVarSize=0;
342
343 SystemProc(*pUserProc);
344 return;
345 }
346
347 if( !pUserProc->IsAutoGeneration() )
348 {
349 cp=pUserProc->GetCodePos();
350 for(;;cp++){
351 if(IsCommandDelimitation(basbuf[cp])) break;
352 }
353 cp--;
354 }
355
356 //ローカル変数に関する情報
357 extern int AllLocalVarSize;
358 AllLocalVarSize=0;
359
360 //パラメータ用の変数データを考慮
361 for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
362 Parameter &param = *pUserProc->RealParams()[i3];
363
364 Variable *pVar = new Variable( param.GetVarName(), param, false, param.IsRef(), "", false );
365
366 if( param.IsArray() ){
367 pVar->SetArray( param.GetSubscripts() );
368 }
369
370 int varSize;
371 if( param.IsRef() == false && param.IsStruct() ){
372 //構造体のByValパラメータ
373 pVar->ThisIsParameter();
374 varSize=PTR_SIZE;
375 }
376 else{
377 if( param.IsArray() == false ){
378 varSize = pVar->GetMemorySize();
379 }
380 else{
381 varSize=PTR_SIZE;
382 }
383 }
384 AllLocalVarSize+=varSize;
385 pVar->SetOffsetAddress( AllLocalVarSize );
386
387 //レキシカルスコープ情報
388 pVar->SetScopeLevel( compiler.codeGenerator.lexicalScopes.GetNowLevel() );
389 pVar->SetScopeStartAddress( compiler.codeGenerator.lexicalScopes.GetStartAddress() );
390 pVar->isLiving = true;
391
392 pUserProc->GetLocalVars().push_back( pVar );
393 }
394
395 //Thisポインタを示すローカルオフセット値をセット
396 extern int LocalVar_ThisPtrOffset;
397 LocalVar_ThisPtrOffset=AllLocalVarSize;
398
399 BaseOffset=AllLocalVarSize;
400
401 //ret用のアドレスを考慮
402 AllLocalVarSize+=sizeof(long);
403
404
405 ///////////////////////
406 // ここからコード生成
407
408 //sub esp,AllLocalVarSize(スケジュール)
409 const PertialSchedule *pAllLocalVarPertialSchedule = compiler.codeGenerator.op_sub_esp( 0, true );
410
411 //push ebp
412 compiler.codeGenerator.op_push(REG_EBP);
413
414 //mov ebp,esp
415 compiler.codeGenerator.op_mov_RR( REG_EBP, REG_ESP );
416
417 //push ebx
418 compiler.codeGenerator.op_push(REG_EBX);
419
420 //push esi
421 compiler.codeGenerator.op_push( REG_ESI );
422
423 //push edi
424 compiler.codeGenerator.op_push( REG_EDI );
425
426 if( !pUserProc->ReturnType().IsNull() ){
427 //戻り値が存在するとき
428
429 const char *temp = pUserProc->GetName().c_str();
430 if( temp[0]==1&&temp[1]==ESC_OPERATOR ){
431 temp = "_System_ReturnValue";
432 }
433
434 if( pUserProc->ReturnType().IsStruct() ){
435 //戻り値用の構造体(値型)はパラメータで引き渡される
436 }
437 else{
438 if( pUserProc->ReturnType().IsObject() ){
439 sprintf(temporary,"%s=Nothing%c%c%s",temp,1,ESC_AS, compiler.TypeToString( pUserProc->ReturnType() ).c_str() );
440 }
441 else{
442 //戻り値用の変数の定義
443 sprintf(temporary,"%s%c%c%s",temp,1,ESC_AS, compiler.TypeToString( pUserProc->ReturnType() ).c_str() );
444 }
445
446 OpcodeDim(temporary,0);
447 }
448 }
449
450 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
451 compiler.codeGenerator.exitSubCodePositions.clear();
452
453 //ラベル管理オブジェクトを初期化
454 compiler.codeGenerator.gotoLabels.clear();
455
456 //Gotoラベルスケジュール
457 compiler.codeGenerator.gotoLabelSchedules.clear();
458
459 //With情報のメモリを確保
460 extern WITHINFO WithInfo;
461 WithInfo.ppName=(char **)HeapAlloc(hHeap,0,1);
462 WithInfo.pWithCp=(int *)HeapAlloc(hHeap,0,1);
463 WithInfo.num=0;
464
465 //重複エラー情報管理のメモリを確保
466 extern char **SynonymErrorWords;
467 extern int SynonymErrorNum;
468 SynonymErrorNum=0;
469 SynonymErrorWords=(char **)HeapAlloc(hHeap,0,1);
470
471 //Continueアドレスを初期化
472 compiler.codeGenerator.ClearContinueArea();
473
474 //レキシカルスコープ情報を初期化
475 compiler.codeGenerator.lexicalScopes.Init( compiler.codeGenerator.GetNativeCodeSize() );
476
477 const PertialSchedule *pEspOffsetPertialSchedule = NULL;
478 if(bDebugCompile&&bDebugSupportProc==0){
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(bDebugCompile&&bDebugSupportProc==0){
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 SetError();
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(bDebugCompile){
742 //cmp esp,ebp
743 compiler.codeGenerator.op_cmp_RR( REG_ESP, REG_EBP );
744
745 //je 6(次のcallとbreakpointを飛び越す)
746 compiler.codeGenerator.op_je( 6 );
747
748 //call _esp_error
749 extern const UserProc *pSub_esp_error;
750 compiler.codeGenerator.op_call( pSub_esp_error );
751
752 breakpoint;
753 }
754
755 //mov esp,ebp
756 compiler.codeGenerator.op_mov_RR( REG_ESP, REG_EBP );
757
758 //pop ebp
759 compiler.codeGenerator.op_pop(REG_EBP);
760
761 //add esp AllLocalVarSize
762 compiler.codeGenerator.op_add_esp(AllLocalVarSize-BaseOffset);
763
764 if( BaseOffset==0 || pUserProc->IsCdecl() ){
765 //ret
766 compiler.codeGenerator.op_ret();
767 }
768 else{
769 //ret BaseOffset(パラメータ分のスタック領域を解放)
770 compiler.codeGenerator.op_ret( (_int16)BaseOffset );
771 }
772
773
774 //重複エラー情報管理のメモリを解放
775 for(i3=0;i3<SynonymErrorNum;i3++) HeapDefaultFree(SynonymErrorWords[i3]);
776 HeapDefaultFree(SynonymErrorWords);
777 SynonymErrorWords=0;
778
779
780 //ローカル変数のネーム情報は後に解放する
781}
Note: See TracBrowser for help on using the repository browser.