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

Last change on this file since 702 was 702, checked in by dai_9181, 16 years ago
  • コンストラクタ、デストラクタの直接呼出しをエラー扱いにした。
    • コンストラクタ … New演算子にて、呼ばれるものとする。また、コンストラクタの1ステップ目にて、基底クラスのコンストラクタ呼び出しを許可する。
    • デストラクタ … Delete演算子にて呼ばれるものとする。また、デストラクタ内にて、基底クラスのコンストラクタ呼び出しが自動的に行われるものとする。

※まずは32bit版のみ対応。速やかに64bit版にマージすること。

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