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

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

静的領域に初期オブジェクトを配置可能にした

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