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

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