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

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

Labelクラスをリファクタリング

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