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

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

・WithInfo周りをリファクタリング。

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