source: dev/trunk/ab5.0/abdev/BasicCompiler32/Compile_ProcOp.cpp@ 472

Last change on this file since 472 was 465, checked in by dai_9181, 17 years ago

Messenger/ErrorMessengerクラスを導入。SetError関数によるエラー生成を廃止した。

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