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

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

[641]のコミット漏れ

File size: 24.6 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パラメータ
[76]349 pVar->ThisIsParameter();
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
435 //With情報のメモリを確保
436 extern WITHINFO WithInfo;
437 WithInfo.ppName=(char **)HeapAlloc(hHeap,0,1);
438 WithInfo.pWithCp=(int *)HeapAlloc(hHeap,0,1);
439 WithInfo.num=0;
440
[541]441 // 重複エラー情報をクリア
442 compiler.errorMessenger.ClearSynonymKeyWords();
[3]443
444 //Continueアドレスを初期化
[241]445 compiler.codeGenerator.ClearContinueArea();
[3]446
[308]447 //レキシカルスコープ情報を初期化
448 compiler.codeGenerator.lexicalScopes.Init( compiler.codeGenerator.GetNativeCodeSize() );
449
[251]450 const PertialSchedule *pEspOffsetPertialSchedule = NULL;
[459]451 if( compiler.IsDebug() && bDebugSupportProc == 0 )
452 {
[3]453 //push dword ptr[ebp+(AllLocalVarSize-BaseOffset)](スケジュール)
[251]454 pEspOffsetPertialSchedule = compiler.codeGenerator.op_push_M( REG_EBP, 0, Schedule::None, true );
[3]455
456 //push dword ptr[ebp](以前のebp)
[235]457 compiler.codeGenerator.op_push_M( REG_EBP );
[3]458
459 //call _DebugSys_StartProc
[206]460 extern const UserProc *pSub_DebugSys_StartProc;
[225]461 compiler.codeGenerator.op_call(pSub_DebugSys_StartProc);
[3]462 }
463
[536]464 if( compiler.IsCompilingClass() ){
465 if( pUserProc->GetName() == compiler.GetCompilingClass().GetName() ){
[3]466 ////////////////////////////////////
467 // コンストラクタをコンパイルするとき
468 ////////////////////////////////////
469
[17]470 //コンストラクタのコンパイル開始を通知
[536]471 compiler.GetCompilingClass().NotifyStartConstructorCompile();
[17]472
[27]473 //基底クラスかどうかの識別
474 //(継承元がインターフェイスの場合も基底クラスと見なす)
[3]475 BOOL bThisIsSuperClass;
[536]476 if( !compiler.GetCompilingClass().HasSuperClass() ) bThisIsSuperClass=1;
477 else if( compiler.GetCompilingClass().GetSuperClass().GetConstructorMethod() == NULL ){
[3]478 //インターフェイスを継承したときはコンストラクタを持たない
479 bThisIsSuperClass=1;
480 }
481 else bThisIsSuperClass=0;
482
483 if(!bThisIsSuperClass){
484 /* サブクラスコンストラクタをコンパイルしているときは、
[27]485 基底クラスのコンストラクタを呼び出す */
[3]486
487 i3=cp+1;
488 while(IsCommandDelimitation(basbuf[i3])) i3++;
489 for(i4=0;;i3++,i4++){
490 if(!IsVariableChar(basbuf[i3])){
491 temporary[i4]=0;
492 break;
493 }
494 temporary[i4]=basbuf[i3];
495 }
[536]496 if( compiler.GetCompilingClass().GetSuperClass().GetName() == temporary ){
[27]497 //基底クラスのコンストラクタを呼び出す
[3]498 cp=i3;
499 for(i4=0;;cp++,i4++){
500 if(IsCommandDelimitation(basbuf[cp])){
501 temporary[i4]=0;
502 break;
503 }
504 temporary[i4]=basbuf[cp];
505 }
506 if(!(temporary[0]=='('&&temporary[lstrlen(temporary)-1]==')')){
[465]507 compiler.errorMessenger.Output(1,NULL,cp);
[3]508 }
509 RemoveStringPare(temporary);
510
[89]511 Type dummyType;
512 CallProc( PROC_DEFAULT
[536]513 , &compiler.GetCompilingClass().GetSuperClass().GetConstructorMethod()->GetUserProc()
514 , compiler.GetCompilingClass().GetSuperClass().GetConstructorMethod()->GetUserProc().GetName().c_str()
[89]515 , temporary
[331]516 , Type() // baseTypeはなし
517 , dummyType
518 );
[3]519 }
520 else{
[27]521 //基底クラスのコンストラクタを暗黙的に呼び出す
[3]522 Opcode_CallProc("",
[536]523 &compiler.GetCompilingClass().GetSuperClass().GetConstructorMethod()->GetUserProc(),
[3]524 0,
[290]525 ""
526 );
[3]527 }
528 }
529 }
[75]530 else if( pUserProc->IsDestructor() ){
[18]531 //デストラクタをコンパイルしたとき
532
533 //デストラクタのコンパイル開始を通知
[536]534 compiler.GetCompilingClass().NotifyStartDestructorCompile();
[18]535 }
[3]536 }
537
538 //////////////////////////////////////////
539 //////////////////////////////////////////
540 ////// プロシージャ内をコンパイル ////////
[90]541 if( pUserProc->IsAutoGeneration() ){
542 AutoGeneration( *pUserProc );
543 }
[75]544 else{
[90]545 if(pUserProc->IsMacro()){
546 CompileBuffer(ESC_ENDMACRO,0);
547 }
548 else{
549 if(pUserProc->IsSub()){
550 CompileBuffer(ESC_ENDSUB,0);
551 }
552 else if(pUserProc->IsFunction()){
553 CompileBuffer(ESC_ENDFUNCTION,0);
554 }
555 }
[75]556 }
[3]557 //////////////////////////////////////////
558 //////////////////////////////////////////
559
[536]560 if( compiler.IsCompilingClass() ){
[17]561
[536]562 if( compiler.GetCompilingClass().IsCompilingConstructor() ){
[17]563 // コンストラクタをコンパイルしていたとき
[18]564
[17]565 // コンストラクタのコンパイルが完了したことを通知
[536]566 compiler.GetCompilingClass().NotifyFinishConstructorCompile();
[17]567 }
[75]568 else if( pUserProc->IsDestructor() ){
[3]569 ////////////////////////////////////
570 //デストラクタをコンパイルしたとき
571 ////////////////////////////////////
572
[18]573 // デストラクタのコンパイルが完了したことを通知
[536]574 compiler.GetCompilingClass().NotifyFinishDestructorCompile();
[18]575
[536]576 if( compiler.GetCompilingClass().HasSuperClass() ){
[3]577 /* サブクラスのデストラクタをコンパイルしているときは、
[27]578 基底クラスのデストラクタを呼び出す */
[3]579
[536]580 const CMethod *method = compiler.GetCompilingClass().GetSuperClass().GetDestructorMethod();
[51]581 if( method ){
[3]582 Opcode_CallProc("",
[206]583 &method->GetUserProc(),
[3]584 0,
[290]585 ""
586 );
[3]587 }
588 }
589 }
590 }
591
592 //With情報のメモリを解放
593 for(i3=0;i3<WithInfo.num;i3++){
[465]594 compiler.errorMessenger.Output(22,"With",WithInfo.pWithCp[i3]);
[3]595 HeapDefaultFree(WithInfo.ppName[i3]);
596 }
597 HeapDefaultFree(WithInfo.ppName);
598 HeapDefaultFree(WithInfo.pWithCp);
599
600 //push ebp
601 AllLocalVarSize+=sizeof(long);
602
603 //ローカルオブジェクトの解放処理
[248]604 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
[3]605
[34]606 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
[247]607 compiler.codeGenerator.ResolveExitSubSchedule();
[34]608
[459]609 if( compiler.IsDebug() && bDebugSupportProc == 0 )
610 {
[251]611 compiler.codeGenerator.opfix( pEspOffsetPertialSchedule, AllLocalVarSize-BaseOffset-sizeof(long) );
[34]612
613 //call _DebugSys_EndProc
[206]614 extern const UserProc *pSub_DebugSys_EndProc;
[225]615 compiler.codeGenerator.op_call(pSub_DebugSys_EndProc);
[34]616 }
617
[75]618 if( !pUserProc->ReturnType().IsNull() ){
[3]619 //戻り値をeax、edxに設定
620 RELATIVE_VAR RelativeVar;
621
[75]622 const char *temp = pUserProc->GetName().c_str();
623 if( temp[0]==1 && temp[1]==ESC_OPERATOR ){
[3]624 temp="_System_ReturnValue";
[75]625 }
[76]626 GetVarOffsetReadWrite(temp,&RelativeVar,Type());
[3]627
[290]628 const Type &returnType = pUserProc->ReturnType();
629 if( returnType.IsObject() || returnType.IsStruct() )
630 {
[3]631 SetVarPtrToEax(&RelativeVar);
[290]632 if( returnType.IsObject() )
633 {
[64]634 //mov eax,dword ptr[eax]
[225]635 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
[64]636 }
[3]637 }
[290]638 else if( returnType.IsReal() )
[231]639 {
[3]640 //fld qword ptr[ebp+offset]
[253]641 compiler.codeGenerator.localVarPertialSchedules.push_back(
[290]642 compiler.codeGenerator.op_fld_base_offset( returnType.GetBasicType(), REG_EBP, RelativeVar.offset, Schedule::None, true )
[253]643 );
[3]644 }
[290]645 else if( returnType.Is64() )
646 {
[3]647 //mov eax,dword ptr[ebp+offset]
[253]648 compiler.codeGenerator.localVarPertialSchedules.push_back(
649 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
650 );
[3]651
652 //mov edx,dword ptr[ebp+offset+sizeof(long)]
[253]653 compiler.codeGenerator.localVarPertialSchedules.push_back(
654 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_EBP, RelativeVar.offset+sizeof(long), MOD_BASE_DISP32, Schedule::None, true )
655 );
[3]656 }
[290]657 else if( returnType.GetSize() == sizeof(long) )
[253]658 {
[3]659 //mov eax,dword ptr[ebp+offset]
[253]660 compiler.codeGenerator.localVarPertialSchedules.push_back(
661 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
662 );
[3]663 }
[290]664 else if( returnType.GetSize() == sizeof(short) )
665 {
[3]666 //xor eax,eax(eaxを0に初期化する)
[225]667 compiler.codeGenerator.op_zero_reg(REG_EAX);
[3]668
669 //mov ax,word ptr[ebp+offset]
[253]670 compiler.codeGenerator.localVarPertialSchedules.push_back(
671 compiler.codeGenerator.op_mov_RM( sizeof(short), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
672 );
[3]673 }
[290]674 else if( returnType.GetSize() == sizeof(char) )
675 {
[3]676 //xor eax,eax(eaxを0に初期化する)
[225]677 compiler.codeGenerator.op_zero_reg(REG_EAX);
[3]678
679 //mov al,byte ptr[ebp+offset]
[253]680 compiler.codeGenerator.localVarPertialSchedules.push_back(
681 compiler.codeGenerator.op_mov_RM( sizeof(char), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
682 );
[3]683 }
[290]684 else
685 {
[465]686 compiler.errorMessenger.OutputFatalError();
[290]687 }
[3]688 }
689
690 //ローカル変数アドレススケジュール
[253]691 BOOST_FOREACH( const PertialSchedule *pPertialSchedule, compiler.codeGenerator.localVarPertialSchedules )
692 {
693 compiler.codeGenerator.opfix_offset( pPertialSchedule, AllLocalVarSize );
[3]694 }
[253]695 compiler.codeGenerator.localVarPertialSchedules.clear();
[206]696 BOOST_FOREACH( Variable *pVar, pUserProc->GetLocalVars() ){
[76]697 //後にデバッグで利用する
[206]698 pVar->SetOffsetAddress( AllLocalVarSize - pVar->GetOffsetAddress() );
[3]699 }
700
701 //push ebp、ret用のアドレスを考慮
702 AllLocalVarSize-=sizeof(long)*2;
703
704 //ローカル変数用メモリを確保するためのスケジュール(subコマンド)
[253]705 compiler.codeGenerator.opfix( pAllLocalVarPertialSchedule, AllLocalVarSize - BaseOffset );
[3]706
707 //pop edi
[235]708 compiler.codeGenerator.op_pop( REG_EDI );
[3]709
710 //pop esi
[235]711 compiler.codeGenerator.op_pop( REG_ESI );
[3]712
713 //pop ebx
[225]714 compiler.codeGenerator.op_pop(REG_EBX);
[3]715
[459]716 if( compiler.IsDebug() )
717 {
[64]718 //cmp esp,ebp
[225]719 compiler.codeGenerator.op_cmp_RR( REG_ESP, REG_EBP );
[64]720
[240]721 //je 6(次のcallとbreakpointを飛び越す)
722 compiler.codeGenerator.op_je( 6 );
[64]723
724 //call _esp_error
[206]725 extern const UserProc *pSub_esp_error;
[225]726 compiler.codeGenerator.op_call( pSub_esp_error );
[64]727
728 breakpoint;
729 }
730
[3]731 //mov esp,ebp
[235]732 compiler.codeGenerator.op_mov_RR( REG_ESP, REG_EBP );
[3]733
734 //pop ebp
[225]735 compiler.codeGenerator.op_pop(REG_EBP);
[3]736
737 //add esp AllLocalVarSize
[225]738 compiler.codeGenerator.op_add_esp(AllLocalVarSize-BaseOffset);
[3]739
[75]740 if( BaseOffset==0 || pUserProc->IsCdecl() ){
[142]741 //ret
[225]742 compiler.codeGenerator.op_ret();
[3]743 }
744 else{
745 //ret BaseOffset(パラメータ分のスタック領域を解放)
[240]746 compiler.codeGenerator.op_ret( (_int16)BaseOffset );
[3]747 }
748
[533]749 compiler.FinishProcedureCompile();
[75]750
[533]751
[3]752 //ローカル変数のネーム情報は後に解放する
753}
Note: See TracBrowser for help on using the repository browser.