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

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

静的リンクライブラリにより、複数のグローバル領域が存在することになったのでそれぞれを関数ベースに分けた

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