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

Last change on this file since 294 was 294, checked in by dai_9181, 17 years ago
File size: 27.2 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 }
[95]222 else if( userProc.GetName() == "RegisterGlobalRoots"
223 && userProc.HasParentClass()
[131]224 && userProc.GetParentClass().GetName() == "_System_CGarbageCollection" ){
[95]225
226 Compile_AddGlobalRootsForGc();
227 }
[78]228 else{
229 SetError();
230 }
[3]231}
232
[206]233void _compile_proc(const UserProc *pUserProc){
[3]234 extern char *basbuf;
235 extern HANDLE hHeap;
236 extern BOOL bDebugCompile;
[253]237 int i3,i4,BaseOffset;
[3]238 char temporary[VN_SIZE];
239
[206]240 if( pUserProc->GetLocalVars().size() ){
[76]241 SetError();
242 return;
243 }
244
[206]245 trace_for_sourcecodestep( "★★★ " << pUserProc->GetFullName() << "のコンパイルを開始" );
246
[75]247 pUserProc->CompleteCompile();
[3]248
249 extern BOOL bSystemProc;
[75]250 if(memcmp(pUserProc->GetName().c_str(),"_System_",8)==0) bSystemProc=1;
[3]251 else bSystemProc=0;
252
253 extern BOOL bDebugSupportProc;
[75]254 if(memcmp(pUserProc->GetName().c_str(),"_DebugSys_",10)==0){
[3]255 if(!bDebugCompile){
256 return;
257 }
258 bDebugSupportProc=1;
259 }
260 else bDebugSupportProc=0;
261
[278]262 if( pUserProc->GetCodeSize() != 0 || pUserProc->GetNativeCode().GetSize() != 0 )
[276]263 {
264 // 既にコード生成が行われている場合はエラー
265 SetError();
266 }
[3]267
[89]268 //コンパイル中の関数が属するクラス
[290]269 compiler.pCompilingClass = pUserProc->GetParentClassPtr();
[89]270
271 //コンパイルスタートをクラス管理クラスに追加
[265]272 compiler.GetObjectModule().meta.GetClasses().StartCompile( pUserProc );
[89]273
274 //コンパイル中の関数
275 UserProc::CompileStartForUserProc( pUserProc );
276
[101]277 // コンパイル中の関数が属する名前空間
[199]278 compiler.GetNamespaceSupporter().SetLivingNamespaceScopes( pUserProc->GetNamespaceScopes() );
[101]279
[108]280 // コンパイル中の関数でImportsされている名前空間
[199]281 compiler.GetNamespaceSupporter().SetImportedNamespaces( pUserProc->GetImportedNamespaces() );
[108]282
[225]283 // コード生成対象を選択
284 compiler.codeGenerator.Select( (const_cast<UserProc *>(pUserProc))->GetNativeCode() );
285
[75]286 if(pUserProc->IsSystem()){
[89]287 ////////////////////
288 // 特殊関数
289 ////////////////////
290
[3]291 extern int AllLocalVarSize;
292 AllLocalVarSize=0;
293
[86]294 SystemProc(*pUserProc);
[3]295 return;
296 }
297
[285]298 if( !pUserProc->IsAutoGeneration() )
299 {
300 cp=pUserProc->GetCodePos();
301 for(;;cp++){
302 if(IsCommandDelimitation(basbuf[cp])) break;
303 }
304 cp--;
[3]305 }
306
307 //ローカル変数に関する情報
308 extern int AllLocalVarSize;
309 AllLocalVarSize=0;
310
311 //パラメータ用の変数データを考慮
[75]312 for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
313 Parameter &param = *pUserProc->RealParams()[i3];
[73]314
[275]315 Variable *pVar = new Variable( param.GetVarName(), param, false, param.IsRef(), "", false );
[3]316
[76]317 if( param.IsArray() ){
[206]318 pVar->SetArray( param.GetSubscripts() );
[3]319 }
320
[76]321 int varSize;
[73]322 if( param.IsRef() == false && param.IsStruct() ){
[64]323 //構造体のByValパラメータ
[76]324 pVar->ThisIsParameter();
325 varSize=PTR_SIZE;
[3]326 }
327 else{
[76]328 if( param.IsArray() == false ){
329 varSize = pVar->GetMemorySize();
[3]330 }
331 else{
[76]332 varSize=PTR_SIZE;
[3]333 }
334 }
[76]335 AllLocalVarSize+=varSize;
[206]336 pVar->SetOffsetAddress( AllLocalVarSize );
[3]337
338 //レキシカルスコープ情報
[248]339 pVar->SetScopeLevel( compiler.codeGenerator.lexicalScopes.GetNowLevel() );
340 pVar->SetScopeStartAddress( compiler.codeGenerator.lexicalScopes.GetStartAddress() );
[76]341 pVar->bLiving=TRUE;
[3]342
[206]343 pUserProc->GetLocalVars().push_back( pVar );
[3]344 }
345
346 //Thisポインタを示すローカルオフセット値をセット
347 extern int LocalVar_ThisPtrOffset;
348 LocalVar_ThisPtrOffset=AllLocalVarSize;
349
350 BaseOffset=AllLocalVarSize;
351
352 //ret用のアドレスを考慮
353 AllLocalVarSize+=sizeof(long);
354
355
356 ///////////////////////
357 // ここからコード生成
358
359 //sub esp,AllLocalVarSize(スケジュール)
[253]360 const PertialSchedule *pAllLocalVarPertialSchedule = compiler.codeGenerator.op_sub_esp( 0, true );
[3]361
362 //push ebp
[225]363 compiler.codeGenerator.op_push(REG_EBP);
[3]364
365 //mov ebp,esp
[235]366 compiler.codeGenerator.op_mov_RR( REG_EBP, REG_ESP );
[3]367
368 //push ebx
[225]369 compiler.codeGenerator.op_push(REG_EBX);
[3]370
371 //push esi
[235]372 compiler.codeGenerator.op_push( REG_ESI );
[3]373
374 //push edi
[235]375 compiler.codeGenerator.op_push( REG_EDI );
[3]376
[75]377 if( !pUserProc->ReturnType().IsNull() ){
[3]378 //戻り値が存在するとき
379
[75]380 const char *temp = pUserProc->GetName().c_str();
381 if( temp[0]==1&&temp[1]==ESC_OPERATOR ){
382 temp = "_System_ReturnValue";
383 }
[3]384
[75]385 if( pUserProc->ReturnType().IsStruct() ){
[64]386 //戻り値用の構造体(値型)はパラメータで引き渡される
[3]387 }
388 else{
[89]389 if( pUserProc->ReturnType().IsObject() ){
[193]390 sprintf(temporary,"%s=Nothing%c%c%s",temp,1,ESC_AS, Compiler::TypeToString( pUserProc->ReturnType() ).c_str() );
[89]391 }
392 else{
393 //戻り値用の変数の定義
[193]394 sprintf(temporary,"%s%c%c%s",temp,1,ESC_AS, Compiler::TypeToString( pUserProc->ReturnType() ).c_str() );
[89]395 }
[40]396
[3]397 OpcodeDim(temporary,0);
398 }
399 }
400
401 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
[247]402 compiler.codeGenerator.exitSubCodePositions.clear();
[3]403
[260]404 //ラベル管理オブジェクトを初期化
[261]405 compiler.codeGenerator.gotoLabels.clear();
[3]406
407 //Gotoラベルスケジュール
[246]408 compiler.codeGenerator.gotoLabelSchedules.clear();
[3]409
410 //With情報のメモリを確保
411 extern WITHINFO WithInfo;
412 WithInfo.ppName=(char **)HeapAlloc(hHeap,0,1);
413 WithInfo.pWithCp=(int *)HeapAlloc(hHeap,0,1);
414 WithInfo.num=0;
415
416 //重複エラー情報管理のメモリを確保
417 extern char **SynonymErrorWords;
418 extern int SynonymErrorNum;
419 SynonymErrorNum=0;
420 SynonymErrorWords=(char **)HeapAlloc(hHeap,0,1);
421
422 //Continueアドレスを初期化
[241]423 compiler.codeGenerator.ClearContinueArea();
[3]424
[251]425 const PertialSchedule *pEspOffsetPertialSchedule = NULL;
[3]426 if(bDebugCompile&&bDebugSupportProc==0){
427 //push dword ptr[ebp+(AllLocalVarSize-BaseOffset)](スケジュール)
[251]428 pEspOffsetPertialSchedule = compiler.codeGenerator.op_push_M( REG_EBP, 0, Schedule::None, true );
[3]429
430 //push dword ptr[ebp](以前のebp)
[235]431 compiler.codeGenerator.op_push_M( REG_EBP );
[3]432
433 //call _DebugSys_StartProc
[206]434 extern const UserProc *pSub_DebugSys_StartProc;
[225]435 compiler.codeGenerator.op_call(pSub_DebugSys_StartProc);
[3]436 }
437
[206]438 if(compiler.pCompilingClass){
439 if( pUserProc->GetName() == compiler.pCompilingClass->GetName() ){
[3]440 ////////////////////////////////////
441 // コンストラクタをコンパイルするとき
442 ////////////////////////////////////
443
[17]444 //コンストラクタのコンパイル開始を通知
[206]445 compiler.pCompilingClass->NotifyStartConstructorCompile();
[17]446
[27]447 //基底クラスかどうかの識別
448 //(継承元がインターフェイスの場合も基底クラスと見なす)
[3]449 BOOL bThisIsSuperClass;
[206]450 if( !compiler.pCompilingClass->HasSuperClass() ) bThisIsSuperClass=1;
451 else if( compiler.pCompilingClass->GetSuperClass().GetConstructorMethod() == NULL ){
[3]452 //インターフェイスを継承したときはコンストラクタを持たない
453 bThisIsSuperClass=1;
454 }
455 else bThisIsSuperClass=0;
456
457 if(!bThisIsSuperClass){
458 /* サブクラスコンストラクタをコンパイルしているときは、
[27]459 基底クラスのコンストラクタを呼び出す */
[3]460
461 i3=cp+1;
462 while(IsCommandDelimitation(basbuf[i3])) i3++;
463 for(i4=0;;i3++,i4++){
464 if(!IsVariableChar(basbuf[i3])){
465 temporary[i4]=0;
466 break;
467 }
468 temporary[i4]=basbuf[i3];
469 }
[206]470 if( compiler.pCompilingClass->GetSuperClass().GetName() == temporary ){
[27]471 //基底クラスのコンストラクタを呼び出す
[3]472 cp=i3;
473 for(i4=0;;cp++,i4++){
474 if(IsCommandDelimitation(basbuf[cp])){
475 temporary[i4]=0;
476 break;
477 }
478 temporary[i4]=basbuf[cp];
479 }
480 if(!(temporary[0]=='('&&temporary[lstrlen(temporary)-1]==')')){
481 SetError(1,NULL,cp);
482 }
483 RemoveStringPare(temporary);
484
[89]485 Type dummyType;
486 CallProc( PROC_DEFAULT
[206]487 , &compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc()
488 , compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc().GetName().c_str()
[89]489 , temporary
490 , dummyType );
[3]491 }
492 else{
[27]493 //基底クラスのコンストラクタを暗黙的に呼び出す
[3]494 Opcode_CallProc("",
[206]495 &compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc(),
[3]496 0,
[290]497 ""
498 );
[3]499 }
500 }
501
502 //仮想関数テーブルを初期化
[206]503 if( compiler.pCompilingClass->IsExistVirtualFunctions()
[282]504 && !compiler.pCompilingClass->IsAbstract() )
505 {
506 // mov eax,vtblAddress
507 compiler.codeGenerator.op_mov_RV_vtbl( REG_EAX, compiler.pCompilingClass );
[3]508
[282]509 //Thisポインタをecxにコピー
510 SetThisPtrToReg(REG_ECX);
511
512 //mov dword ptr[ecx],eax
513 compiler.codeGenerator.op_mov_MR( sizeof(long), REG_EAX, REG_ECX, 0, MOD_BASE );
514
515
516 // 仮想関数になるメソッドに使用チェックをつける
517 BOOST_FOREACH( const CMethod *pMethod, compiler.pCompilingClass->GetMethods() )
518 {
519 if( pMethod->IsVirtual() )
520 {
521 pMethod->GetUserProc().Using();
522 }
523 }
[3]524 }
525 }
[75]526 else if( pUserProc->IsDestructor() ){
[18]527 //デストラクタをコンパイルしたとき
528
529 //デストラクタのコンパイル開始を通知
[206]530 compiler.pCompilingClass->NotifyStartDestructorCompile();
[18]531 }
[3]532 }
533
534 //////////////////////////////////////////
535 //////////////////////////////////////////
536 ////// プロシージャ内をコンパイル ////////
[90]537 if( pUserProc->IsAutoGeneration() ){
538 AutoGeneration( *pUserProc );
539 }
[75]540 else{
[90]541 if(pUserProc->IsMacro()){
542 CompileBuffer(ESC_ENDMACRO,0);
543 }
544 else{
545 if(pUserProc->IsSub()){
546 CompileBuffer(ESC_ENDSUB,0);
547 }
548 else if(pUserProc->IsFunction()){
549 CompileBuffer(ESC_ENDFUNCTION,0);
550 }
551 }
[75]552 }
[3]553 //////////////////////////////////////////
554 //////////////////////////////////////////
555
[206]556 if( compiler.pCompilingClass ){
[17]557
[206]558 if( compiler.pCompilingClass->IsCompilingConstructor() ){
[17]559 // コンストラクタをコンパイルしていたとき
[18]560
[17]561 // コンストラクタのコンパイルが完了したことを通知
[206]562 compiler.pCompilingClass->NotifyFinishConstructorCompile();
[17]563 }
[75]564 else if( pUserProc->IsDestructor() ){
[3]565 ////////////////////////////////////
566 //デストラクタをコンパイルしたとき
567 ////////////////////////////////////
568
[18]569 // デストラクタのコンパイルが完了したことを通知
[206]570 compiler.pCompilingClass->NotifyFinishDestructorCompile();
[18]571
[206]572 if( compiler.pCompilingClass->HasSuperClass() ){
[3]573 /* サブクラスのデストラクタをコンパイルしているときは、
[27]574 基底クラスのデストラクタを呼び出す */
[3]575
[206]576 const CMethod *method = compiler.pCompilingClass->GetSuperClass().GetDestructorMethod();
[51]577 if( method ){
[3]578 Opcode_CallProc("",
[206]579 &method->GetUserProc(),
[3]580 0,
[290]581 ""
582 );
[3]583 }
584 }
585 }
586 }
587
588 //With情報のメモリを解放
589 for(i3=0;i3<WithInfo.num;i3++){
590 SetError(22,"With",WithInfo.pWithCp[i3]);
591 HeapDefaultFree(WithInfo.ppName[i3]);
592 }
593 HeapDefaultFree(WithInfo.ppName);
594 HeapDefaultFree(WithInfo.pWithCp);
595
596 //push ebp
597 AllLocalVarSize+=sizeof(long);
598
599 //ローカルオブジェクトの解放処理
[248]600 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
[3]601
[34]602 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
[247]603 compiler.codeGenerator.ResolveExitSubSchedule();
[34]604
605 if(bDebugCompile&&bDebugSupportProc==0){
[251]606 compiler.codeGenerator.opfix( pEspOffsetPertialSchedule, AllLocalVarSize-BaseOffset-sizeof(long) );
[34]607
608 //call _DebugSys_EndProc
[206]609 extern const UserProc *pSub_DebugSys_EndProc;
[225]610 compiler.codeGenerator.op_call(pSub_DebugSys_EndProc);
[34]611 }
612
[75]613 if( !pUserProc->ReturnType().IsNull() ){
[3]614 //戻り値をeax、edxに設定
615 RELATIVE_VAR RelativeVar;
616
[75]617 const char *temp = pUserProc->GetName().c_str();
618 if( temp[0]==1 && temp[1]==ESC_OPERATOR ){
[3]619 temp="_System_ReturnValue";
[75]620 }
[76]621 GetVarOffsetReadWrite(temp,&RelativeVar,Type());
[3]622
[290]623 const Type &returnType = pUserProc->ReturnType();
624 if( returnType.IsObject() || returnType.IsStruct() )
625 {
[3]626 SetVarPtrToEax(&RelativeVar);
[290]627 if( returnType.IsObject() )
628 {
[64]629 //mov eax,dword ptr[eax]
[225]630 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
[64]631 }
[3]632 }
[290]633 else if( returnType.IsReal() )
[231]634 {
[3]635 //fld qword ptr[ebp+offset]
[253]636 compiler.codeGenerator.localVarPertialSchedules.push_back(
[290]637 compiler.codeGenerator.op_fld_base_offset( returnType.GetBasicType(), REG_EBP, RelativeVar.offset, Schedule::None, true )
[253]638 );
[3]639 }
[290]640 else if( returnType.Is64() )
641 {
[3]642 //mov eax,dword ptr[ebp+offset]
[253]643 compiler.codeGenerator.localVarPertialSchedules.push_back(
644 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
645 );
[3]646
647 //mov edx,dword ptr[ebp+offset+sizeof(long)]
[253]648 compiler.codeGenerator.localVarPertialSchedules.push_back(
649 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_EBP, RelativeVar.offset+sizeof(long), MOD_BASE_DISP32, Schedule::None, true )
650 );
[3]651 }
[290]652 else if( returnType.GetSize() == sizeof(long) )
[253]653 {
[3]654 //mov eax,dword ptr[ebp+offset]
[253]655 compiler.codeGenerator.localVarPertialSchedules.push_back(
656 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
657 );
[3]658 }
[290]659 else if( returnType.GetSize() == sizeof(short) )
660 {
[3]661 //xor eax,eax(eaxを0に初期化する)
[225]662 compiler.codeGenerator.op_zero_reg(REG_EAX);
[3]663
664 //mov ax,word ptr[ebp+offset]
[253]665 compiler.codeGenerator.localVarPertialSchedules.push_back(
666 compiler.codeGenerator.op_mov_RM( sizeof(short), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
667 );
[3]668 }
[290]669 else if( returnType.GetSize() == sizeof(char) )
670 {
[3]671 //xor eax,eax(eaxを0に初期化する)
[225]672 compiler.codeGenerator.op_zero_reg(REG_EAX);
[3]673
674 //mov al,byte ptr[ebp+offset]
[253]675 compiler.codeGenerator.localVarPertialSchedules.push_back(
676 compiler.codeGenerator.op_mov_RM( sizeof(char), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
677 );
[3]678 }
[290]679 else
680 {
681 SetError();
682 }
[3]683 }
684
685 //ローカル変数アドレススケジュール
[253]686 BOOST_FOREACH( const PertialSchedule *pPertialSchedule, compiler.codeGenerator.localVarPertialSchedules )
687 {
688 compiler.codeGenerator.opfix_offset( pPertialSchedule, AllLocalVarSize );
[3]689 }
[253]690 compiler.codeGenerator.localVarPertialSchedules.clear();
[206]691 BOOST_FOREACH( Variable *pVar, pUserProc->GetLocalVars() ){
[76]692 //後にデバッグで利用する
[206]693 pVar->SetOffsetAddress( AllLocalVarSize - pVar->GetOffsetAddress() );
[3]694 }
695
696 //push ebp、ret用のアドレスを考慮
697 AllLocalVarSize-=sizeof(long)*2;
698
699 //ローカル変数用メモリを確保するためのスケジュール(subコマンド)
[253]700 compiler.codeGenerator.opfix( pAllLocalVarPertialSchedule, AllLocalVarSize - BaseOffset );
[3]701
702 //pop edi
[235]703 compiler.codeGenerator.op_pop( REG_EDI );
[3]704
705 //pop esi
[235]706 compiler.codeGenerator.op_pop( REG_ESI );
[3]707
708 //pop ebx
[225]709 compiler.codeGenerator.op_pop(REG_EBX);
[3]710
[64]711 if(bDebugCompile){
712 //cmp esp,ebp
[225]713 compiler.codeGenerator.op_cmp_RR( REG_ESP, REG_EBP );
[64]714
[240]715 //je 6(次のcallとbreakpointを飛び越す)
716 compiler.codeGenerator.op_je( 6 );
[64]717
718 //call _esp_error
[206]719 extern const UserProc *pSub_esp_error;
[225]720 compiler.codeGenerator.op_call( pSub_esp_error );
[64]721
722 breakpoint;
723 }
724
[3]725 //mov esp,ebp
[235]726 compiler.codeGenerator.op_mov_RR( REG_ESP, REG_EBP );
[3]727
728 //pop ebp
[225]729 compiler.codeGenerator.op_pop(REG_EBP);
[3]730
731 //add esp AllLocalVarSize
[225]732 compiler.codeGenerator.op_add_esp(AllLocalVarSize-BaseOffset);
[3]733
[75]734 if( BaseOffset==0 || pUserProc->IsCdecl() ){
[142]735 //ret
[225]736 compiler.codeGenerator.op_ret();
[3]737 }
738 else{
739 //ret BaseOffset(パラメータ分のスタック領域を解放)
[240]740 compiler.codeGenerator.op_ret( (_int16)BaseOffset );
[3]741 }
742
[76]743
[75]744 //重複エラー情報管理のメモリを解放
745 for(i3=0;i3<SynonymErrorNum;i3++) HeapDefaultFree(SynonymErrorWords[i3]);
746 HeapDefaultFree(SynonymErrorWords);
747 SynonymErrorWords=0;
748
749
[3]750 //ローカル変数のネーム情報は後に解放する
751}
[91]752
[206]753void CompileBufferInProcedure( const UserProc &userProc ){
[283]754 if( userProc.IsCompiled() ) return;
[91]755
756 _compile_proc( &userProc );
[263]757
[92]758/*
[91]759 // ログを履く
760 char temporary[8192];
761 temporary[0]=0;
762 lstrcat( temporary, "------------------------------------------------------------------\n" );
763 sprintf( temporary + lstrlen(temporary), "【 %s のコード情報】\n", userProc.GetFullName().c_str() );
764 sprintf( temporary + lstrlen(temporary), "code size: %d bytes\n", userProc.GetCodeSize() );
765 lstrcat( temporary, "------------------------------------------------------------------\n" );
766 lstrcat( temporary, "\n" );
[92]767 Smoothie::Logger::Put( temporary );*/
[91]768}
[3]769void CompileLocal(){
[266]770 if( compiler.IsDll() ){
[3]771 //DLLの場合はグローバル変数を初期化するための関数を一番初めにコンパイルする
[206]772 const UserProc *pUserProc=GetSubHash("_System_InitDllGlobalVariables");
[75]773 if(pUserProc){
[91]774 CompileBufferInProcedure( *pUserProc );
[3]775 }
776 else SetError(300,NULL,cp);
777 }
778
[94]779 //_System_TypeBase_InitializeUserTypesは一番最後にコンパイル
[206]780 extern const UserProc *pSubStaticMethod_System_TypeBase_InitializeUserTypes;
[94]781 pSubStaticMethod_System_TypeBase_InitializeUserTypes->CompleteCompile();
782
[3]783 //_System_InitStaticLocalVariablesは一番最後にコンパイル
784 //※一般関数内の静的変数オブジェクトをすべて収集しなければならない
[206]785 extern const UserProc *pSub_System_InitStaticLocalVariables;
[75]786 pSub_System_InitStaticLocalVariables->CompleteCompile();
[3]787
788 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
[206]789 extern const UserProc *pSub_System_Call_Destructor_of_GlobalObject;
[75]790 pSub_System_Call_Destructor_of_GlobalObject->CompleteCompile();
[3]791
[94]792repeat:
[265]793 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
794 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
[206]795 {
[265]796 UserProc *pUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
[206]797 CompileBufferInProcedure( *pUserProc );
[3]798 }
799
[94]800 if( IsNeedProcCompile() ){
801 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
802 goto repeat;
803 }
804
[272]805 if( !compiler.IsStaticLibrary() )
806 {
807 //_System_TypeBase_InitializeUserTypesは最後のほうでコンパイル
808 pSubStaticMethod_System_TypeBase_InitializeUserTypes->KillCompileStatus();
809 CompileBufferInProcedure( *pSubStaticMethod_System_TypeBase_InitializeUserTypes );
[94]810
[272]811 if( IsNeedProcCompile() ){
812 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
[206]813
[272]814 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
815 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
816 {
817 UserProc *pUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
818 CompileBufferInProcedure( *pUserProc );
819 }
[3]820 }
821
[272]822 //_System_InitStaticLocalVariablesは一番最後にコンパイル
823 pSub_System_InitStaticLocalVariables->KillCompileStatus();
824 CompileBufferInProcedure( *pSub_System_InitStaticLocalVariables );
[3]825
[272]826 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
827 pSub_System_Call_Destructor_of_GlobalObject->KillCompileStatus();
828 CompileBufferInProcedure( *pSub_System_Call_Destructor_of_GlobalObject );
829 }
[3]830}
Note: See TracBrowser for help on using the repository browser.