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

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

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

File size: 28.5 KB
Line 
1#include "stdafx.h"
2
3#include <jenga/include/smoothie/Smoothie.h>
4#include <jenga/include/smoothie/LexicalAnalysis.h>
5
6#include <Program.h>
7#include <Compiler.h>
8#include <LexicalScope.h>
9#include <Class.h>
10#include <Variable.h>
11#include <NamespaceSupporter.h>
12
13#include "../BasicCompiler_Common/common.h"
14#include "Opcode.h"
15
16
17void SystemProc( const UserProc &userProc ){
18 if( userProc.GetName() == "_System_GetEip" ){
19 //mov eax,dword ptr[esp]
20 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_ESP, 0, MOD_BASE );
21
22 //ret
23 compiler.codeGenerator.op_ret();
24 }
25 else if( userProc.GetName() == "_System_InitDllGlobalVariables" ){
26 ////////////////////////////////////////
27 // DLLのグローバル領域をコンパイル
28 ////////////////////////////////////////
29
30 if( !compiler.IsDll() ){
31 //ret
32 compiler.codeGenerator.op_ret();
33
34 return;
35 }
36
37 const UserProc *pBackUserProc;
38 pBackUserProc = &UserProc::CompilingUserProc();
39 UserProc::CompileStartForGlobalArea();
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
54 //_System_StartupProgramの呼び出し
55 extern const UserProc *pSub_System_StartupProgram;
56 compiler.codeGenerator.op_call(pSub_System_StartupProgram);
57
58 //クラスに属する静的メンバを定義
59 compiler.GetObjectModule().meta.GetClasses().InitStaticMember();
60
61 GetGlobalDataForDll();
62
63 UserProc::CompileStartForUserProc( pBackUserProc );
64 cp=BackCp;
65
66 //ret
67 compiler.codeGenerator.op_ret();
68 }
69 else if( userProc.GetName() == "_System_InitStaticLocalVariables" ){
70 //静的ローカルオブジェクトのコンストラクタ呼び出し
71
72 BOOST_FOREACH( Variable *pVar, compiler.GetObjectModule().meta.GetGlobalVars() ){
73 if(memicmp(pVar->GetName().c_str(),"Static%",7)==0){
74 //コンストラクタ呼び出し
75 if( pVar->GetType().IsObject() ){
76
77 //エラー用
78 cp=pVar->source_code_address;
79
80 CallConstructor(
81 pVar->GetName().c_str(),
82 pVar->GetSubscripts(),
83 pVar->GetType(),
84 pVar->GetParamStrForConstructor().c_str());
85 }
86 }
87 }
88
89 //ret
90 compiler.codeGenerator.op_ret();
91 }
92 else if( userProc.GetName() == "_System_Call_Destructor_of_GlobalObject" ){
93
94 const UserProc *pBackUserProc;
95 pBackUserProc = &UserProc::CompilingUserProc();
96 UserProc::CompileStartForGlobalArea();
97
98 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
99
100 UserProc::CompileStartForUserProc( pBackUserProc );
101
102
103 //ret
104 compiler.codeGenerator.op_ret();
105 }
106 else if( userProc.GetName() == "_System_GetSp" ){
107 //mov eax,esp
108 compiler.codeGenerator.op_mov_RR(REG_EAX,REG_ESP);
109
110 //add eax,PTR_SIZE
111 compiler.codeGenerator.op_add_RV8(REG_EAX,PTR_SIZE);
112
113 //ret
114 compiler.codeGenerator.op_ret();
115 }
116 else if( userProc.GetName() == "_allrem" ){
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
122 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allrem, 178 ) );
123 }
124 else if( userProc.GetName() == "_aullrem" ){
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
138 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_aullrem, 117 ) );
139 }
140 else if( userProc.GetName() == "_allmul" ){
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
146 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allmul, 52 ) );
147 }
148 else if( userProc.GetName() == "_alldiv" ){
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
154 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_alldiv, 170 ) );
155 }
156 else if( userProc.GetName() == "_aulldiv" ){
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
169 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_aulldiv, 104 ) );
170 }
171 else if( userProc.GetName() == "_allshl" ){
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
177 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allshl, 31 ) );
178 }
179 else if( userProc.GetName() == "_allshr" ){
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
185 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allshr, 33 ) );
186 }
187 else if( userProc.GetName() == "_aullshr" ){
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
209 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_aullshr, 31 ) );
210 }
211 else{
212 SetError();
213 }
214}
215void AutoGeneration( const UserProc &userProc){
216 if( userProc.GetName() == "InitializeUserTypes"
217 && userProc.HasParentClass()
218 && userProc.GetParentClass().GetName() == "_System_TypeBase" ){
219
220 compiler.GetObjectModule().meta.GetClasses().Compile_System_InitializeUserTypes();
221 }
222 else if( userProc.GetName() == "RegisterGlobalRoots"
223 && userProc.HasParentClass()
224 && userProc.GetParentClass().GetName() == "_System_CGarbageCollection" ){
225
226 Compile_AddGlobalRootsForGc();
227 }
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 }
261 else{
262 SetError();
263 }
264}
265
266void _compile_proc(const UserProc *pUserProc){
267 extern char *basbuf;
268 extern HANDLE hHeap;
269 extern BOOL bDebugCompile;
270 int i3,i4,BaseOffset;
271 char temporary[VN_SIZE];
272
273 if( pUserProc->GetLocalVars().size() ){
274 SetError();
275 return;
276 }
277
278 trace_for_sourcecodestep( "★★★ " << pUserProc->GetFullName() << "のコンパイルを開始" );
279
280 pUserProc->CompleteCompile();
281
282 extern BOOL bSystemProc;
283 if(memcmp(pUserProc->GetName().c_str(),"_System_",8)==0) bSystemProc=1;
284 else bSystemProc=0;
285
286 extern BOOL bDebugSupportProc;
287 if(memcmp(pUserProc->GetName().c_str(),"_DebugSys_",10)==0){
288 if(!bDebugCompile){
289 return;
290 }
291 bDebugSupportProc=1;
292 }
293 else bDebugSupportProc=0;
294
295 if( pUserProc->GetCodeSize() != 0 || pUserProc->GetNativeCode().GetSize() != 0 )
296 {
297 // 既にコード生成が行われている場合はエラー
298 SetError();
299 }
300
301 //コンパイル中の関数が属するクラス
302 compiler.pCompilingClass = pUserProc->GetParentClassPtr();
303
304 //コンパイルスタートをクラス管理クラスに追加
305 compiler.GetObjectModule().meta.GetClasses().StartCompile( pUserProc );
306
307 //コンパイル中の関数
308 UserProc::CompileStartForUserProc( pUserProc );
309
310 // コンパイル中の関数が属する名前空間
311 compiler.GetNamespaceSupporter().SetLivingNamespaceScopes( pUserProc->GetNamespaceScopes() );
312
313 // コンパイル中の関数でImportsされている名前空間
314 compiler.GetNamespaceSupporter().SetImportedNamespaces( pUserProc->GetImportedNamespaces() );
315
316 // コード生成対象を選択
317 compiler.codeGenerator.Select( (const_cast<UserProc *>(pUserProc))->GetNativeCode() );
318
319 if(pUserProc->IsSystem()){
320 ////////////////////
321 // 特殊関数
322 ////////////////////
323
324 extern int AllLocalVarSize;
325 AllLocalVarSize=0;
326
327 SystemProc(*pUserProc);
328 return;
329 }
330
331 if( !pUserProc->IsAutoGeneration() )
332 {
333 cp=pUserProc->GetCodePos();
334 for(;;cp++){
335 if(IsCommandDelimitation(basbuf[cp])) break;
336 }
337 cp--;
338 }
339
340 //ローカル変数に関する情報
341 extern int AllLocalVarSize;
342 AllLocalVarSize=0;
343
344 //パラメータ用の変数データを考慮
345 for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
346 Parameter &param = *pUserProc->RealParams()[i3];
347
348 Variable *pVar = new Variable( param.GetVarName(), param, false, param.IsRef(), "", false );
349
350 if( param.IsArray() ){
351 pVar->SetArray( param.GetSubscripts() );
352 }
353
354 int varSize;
355 if( param.IsRef() == false && param.IsStruct() ){
356 //構造体のByValパラメータ
357 pVar->ThisIsParameter();
358 varSize=PTR_SIZE;
359 }
360 else{
361 if( param.IsArray() == false ){
362 varSize = pVar->GetMemorySize();
363 }
364 else{
365 varSize=PTR_SIZE;
366 }
367 }
368 AllLocalVarSize+=varSize;
369 pVar->SetOffsetAddress( AllLocalVarSize );
370
371 //レキシカルスコープ情報
372 pVar->SetScopeLevel( compiler.codeGenerator.lexicalScopes.GetNowLevel() );
373 pVar->SetScopeStartAddress( compiler.codeGenerator.lexicalScopes.GetStartAddress() );
374 pVar->bLiving=TRUE;
375
376 pUserProc->GetLocalVars().push_back( pVar );
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(スケジュール)
393 const PertialSchedule *pAllLocalVarPertialSchedule = compiler.codeGenerator.op_sub_esp( 0, true );
394
395 //push ebp
396 compiler.codeGenerator.op_push(REG_EBP);
397
398 //mov ebp,esp
399 compiler.codeGenerator.op_mov_RR( REG_EBP, REG_ESP );
400
401 //push ebx
402 compiler.codeGenerator.op_push(REG_EBX);
403
404 //push esi
405 compiler.codeGenerator.op_push( REG_ESI );
406
407 //push edi
408 compiler.codeGenerator.op_push( REG_EDI );
409
410 if( !pUserProc->ReturnType().IsNull() ){
411 //戻り値が存在するとき
412
413 const char *temp = pUserProc->GetName().c_str();
414 if( temp[0]==1&&temp[1]==ESC_OPERATOR ){
415 temp = "_System_ReturnValue";
416 }
417
418 if( pUserProc->ReturnType().IsStruct() ){
419 //戻り値用の構造体(値型)はパラメータで引き渡される
420 }
421 else{
422 if( pUserProc->ReturnType().IsObject() ){
423 sprintf(temporary,"%s=Nothing%c%c%s",temp,1,ESC_AS, compiler.TypeToString( pUserProc->ReturnType() ).c_str() );
424 }
425 else{
426 //戻り値用の変数の定義
427 sprintf(temporary,"%s%c%c%s",temp,1,ESC_AS, compiler.TypeToString( pUserProc->ReturnType() ).c_str() );
428 }
429
430 OpcodeDim(temporary,0);
431 }
432 }
433
434 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
435 compiler.codeGenerator.exitSubCodePositions.clear();
436
437 //ラベル管理オブジェクトを初期化
438 compiler.codeGenerator.gotoLabels.clear();
439
440 //Gotoラベルスケジュール
441 compiler.codeGenerator.gotoLabelSchedules.clear();
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アドレスを初期化
456 compiler.codeGenerator.ClearContinueArea();
457
458 //レキシカルスコープ情報を初期化
459 compiler.codeGenerator.lexicalScopes.Init( compiler.codeGenerator.GetNativeCodeSize() );
460
461 const PertialSchedule *pEspOffsetPertialSchedule = NULL;
462 if(bDebugCompile&&bDebugSupportProc==0){
463 //push dword ptr[ebp+(AllLocalVarSize-BaseOffset)](スケジュール)
464 pEspOffsetPertialSchedule = compiler.codeGenerator.op_push_M( REG_EBP, 0, Schedule::None, true );
465
466 //push dword ptr[ebp](以前のebp)
467 compiler.codeGenerator.op_push_M( REG_EBP );
468
469 //call _DebugSys_StartProc
470 extern const UserProc *pSub_DebugSys_StartProc;
471 compiler.codeGenerator.op_call(pSub_DebugSys_StartProc);
472 }
473
474 if(compiler.pCompilingClass){
475 if( pUserProc->GetName() == compiler.pCompilingClass->GetName() ){
476 ////////////////////////////////////
477 // コンストラクタをコンパイルするとき
478 ////////////////////////////////////
479
480 //コンストラクタのコンパイル開始を通知
481 compiler.pCompilingClass->NotifyStartConstructorCompile();
482
483 //基底クラスかどうかの識別
484 //(継承元がインターフェイスの場合も基底クラスと見なす)
485 BOOL bThisIsSuperClass;
486 if( !compiler.pCompilingClass->HasSuperClass() ) bThisIsSuperClass=1;
487 else if( compiler.pCompilingClass->GetSuperClass().GetConstructorMethod() == NULL ){
488 //インターフェイスを継承したときはコンストラクタを持たない
489 bThisIsSuperClass=1;
490 }
491 else bThisIsSuperClass=0;
492
493 if(!bThisIsSuperClass){
494 /* サブクラスコンストラクタをコンパイルしているときは、
495 基底クラスのコンストラクタを呼び出す */
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 }
506 if( compiler.pCompilingClass->GetSuperClass().GetName() == temporary ){
507 //基底クラスのコンストラクタを呼び出す
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
521 Type dummyType;
522 CallProc( PROC_DEFAULT
523 , &compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc()
524 , compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc().GetName().c_str()
525 , temporary
526 , dummyType );
527 }
528 else{
529 //基底クラスのコンストラクタを暗黙的に呼び出す
530 Opcode_CallProc("",
531 &compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc(),
532 0,
533 ""
534 );
535 }
536 }
537
538 //仮想関数テーブルを初期化
539 if( compiler.pCompilingClass->IsExistVirtualFunctions()
540 && !compiler.pCompilingClass->IsAbstract() )
541 {
542 // mov eax,vtblAddress
543 compiler.codeGenerator.op_mov_RV_vtbl( REG_EAX, compiler.pCompilingClass );
544
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 }
560 }
561 }
562 else if( pUserProc->IsDestructor() ){
563 //デストラクタをコンパイルしたとき
564
565 //デストラクタのコンパイル開始を通知
566 compiler.pCompilingClass->NotifyStartDestructorCompile();
567 }
568 }
569
570 //////////////////////////////////////////
571 //////////////////////////////////////////
572 ////// プロシージャ内をコンパイル ////////
573 if( pUserProc->IsAutoGeneration() ){
574 AutoGeneration( *pUserProc );
575 }
576 else{
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 }
588 }
589 //////////////////////////////////////////
590 //////////////////////////////////////////
591
592 if( compiler.pCompilingClass ){
593
594 if( compiler.pCompilingClass->IsCompilingConstructor() ){
595 // コンストラクタをコンパイルしていたとき
596
597 // コンストラクタのコンパイルが完了したことを通知
598 compiler.pCompilingClass->NotifyFinishConstructorCompile();
599 }
600 else if( pUserProc->IsDestructor() ){
601 ////////////////////////////////////
602 //デストラクタをコンパイルしたとき
603 ////////////////////////////////////
604
605 // デストラクタのコンパイルが完了したことを通知
606 compiler.pCompilingClass->NotifyFinishDestructorCompile();
607
608 if( compiler.pCompilingClass->HasSuperClass() ){
609 /* サブクラスのデストラクタをコンパイルしているときは、
610 基底クラスのデストラクタを呼び出す */
611
612 const CMethod *method = compiler.pCompilingClass->GetSuperClass().GetDestructorMethod();
613 if( method ){
614 Opcode_CallProc("",
615 &method->GetUserProc(),
616 0,
617 ""
618 );
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 //ローカルオブジェクトの解放処理
636 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
637
638 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
639 compiler.codeGenerator.ResolveExitSubSchedule();
640
641 if(bDebugCompile&&bDebugSupportProc==0){
642 compiler.codeGenerator.opfix( pEspOffsetPertialSchedule, AllLocalVarSize-BaseOffset-sizeof(long) );
643
644 //call _DebugSys_EndProc
645 extern const UserProc *pSub_DebugSys_EndProc;
646 compiler.codeGenerator.op_call(pSub_DebugSys_EndProc);
647 }
648
649 if( !pUserProc->ReturnType().IsNull() ){
650 //戻り値をeax、edxに設定
651 RELATIVE_VAR RelativeVar;
652
653 const char *temp = pUserProc->GetName().c_str();
654 if( temp[0]==1 && temp[1]==ESC_OPERATOR ){
655 temp="_System_ReturnValue";
656 }
657 GetVarOffsetReadWrite(temp,&RelativeVar,Type());
658
659 const Type &returnType = pUserProc->ReturnType();
660 if( returnType.IsObject() || returnType.IsStruct() )
661 {
662 SetVarPtrToEax(&RelativeVar);
663 if( returnType.IsObject() )
664 {
665 //mov eax,dword ptr[eax]
666 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
667 }
668 }
669 else if( returnType.IsReal() )
670 {
671 //fld qword ptr[ebp+offset]
672 compiler.codeGenerator.localVarPertialSchedules.push_back(
673 compiler.codeGenerator.op_fld_base_offset( returnType.GetBasicType(), REG_EBP, RelativeVar.offset, Schedule::None, true )
674 );
675 }
676 else if( returnType.Is64() )
677 {
678 //mov eax,dword ptr[ebp+offset]
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 );
682
683 //mov edx,dword ptr[ebp+offset+sizeof(long)]
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 );
687 }
688 else if( returnType.GetSize() == sizeof(long) )
689 {
690 //mov eax,dword ptr[ebp+offset]
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 );
694 }
695 else if( returnType.GetSize() == sizeof(short) )
696 {
697 //xor eax,eax(eaxを0に初期化する)
698 compiler.codeGenerator.op_zero_reg(REG_EAX);
699
700 //mov ax,word ptr[ebp+offset]
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 );
704 }
705 else if( returnType.GetSize() == sizeof(char) )
706 {
707 //xor eax,eax(eaxを0に初期化する)
708 compiler.codeGenerator.op_zero_reg(REG_EAX);
709
710 //mov al,byte ptr[ebp+offset]
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 );
714 }
715 else
716 {
717 SetError();
718 }
719 }
720
721 //ローカル変数アドレススケジュール
722 BOOST_FOREACH( const PertialSchedule *pPertialSchedule, compiler.codeGenerator.localVarPertialSchedules )
723 {
724 compiler.codeGenerator.opfix_offset( pPertialSchedule, AllLocalVarSize );
725 }
726 compiler.codeGenerator.localVarPertialSchedules.clear();
727 BOOST_FOREACH( Variable *pVar, pUserProc->GetLocalVars() ){
728 //後にデバッグで利用する
729 pVar->SetOffsetAddress( AllLocalVarSize - pVar->GetOffsetAddress() );
730 }
731
732 //push ebp、ret用のアドレスを考慮
733 AllLocalVarSize-=sizeof(long)*2;
734
735 //ローカル変数用メモリを確保するためのスケジュール(subコマンド)
736 compiler.codeGenerator.opfix( pAllLocalVarPertialSchedule, AllLocalVarSize - BaseOffset );
737
738 //pop edi
739 compiler.codeGenerator.op_pop( REG_EDI );
740
741 //pop esi
742 compiler.codeGenerator.op_pop( REG_ESI );
743
744 //pop ebx
745 compiler.codeGenerator.op_pop(REG_EBX);
746
747 if(bDebugCompile){
748 //cmp esp,ebp
749 compiler.codeGenerator.op_cmp_RR( REG_ESP, REG_EBP );
750
751 //je 6(次のcallとbreakpointを飛び越す)
752 compiler.codeGenerator.op_je( 6 );
753
754 //call _esp_error
755 extern const UserProc *pSub_esp_error;
756 compiler.codeGenerator.op_call( pSub_esp_error );
757
758 breakpoint;
759 }
760
761 //mov esp,ebp
762 compiler.codeGenerator.op_mov_RR( REG_ESP, REG_EBP );
763
764 //pop ebp
765 compiler.codeGenerator.op_pop(REG_EBP);
766
767 //add esp AllLocalVarSize
768 compiler.codeGenerator.op_add_esp(AllLocalVarSize-BaseOffset);
769
770 if( BaseOffset==0 || pUserProc->IsCdecl() ){
771 //ret
772 compiler.codeGenerator.op_ret();
773 }
774 else{
775 //ret BaseOffset(パラメータ分のスタック領域を解放)
776 compiler.codeGenerator.op_ret( (_int16)BaseOffset );
777 }
778
779
780 //重複エラー情報管理のメモリを解放
781 for(i3=0;i3<SynonymErrorNum;i3++) HeapDefaultFree(SynonymErrorWords[i3]);
782 HeapDefaultFree(SynonymErrorWords);
783 SynonymErrorWords=0;
784
785
786 //ローカル変数のネーム情報は後に解放する
787}
788
789void CompileBufferInProcedure( const UserProc &userProc ){
790 if( userProc.IsCompiled() ) return;
791
792 _compile_proc( &userProc );
793
794/*
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" );
803 Smoothie::Logger::Put( temporary );*/
804}
805void CompileLocal(){
806 if( compiler.IsDll() )
807 {
808 //DLLの場合はグローバル変数を初期化するための関数を一番初めにコンパイルする
809 const UserProc *pUserProc=GetSubHash("_System_InitDllGlobalVariables");
810 if(pUserProc){
811 CompileBufferInProcedure( *pUserProc );
812 }
813 else SetError(300,NULL,cp);
814 }
815 else
816 {
817 // グローバル領域を一番初めにコンパイルする
818 extern const UserProc *pSub_System_GlobalArea;
819 CompileBufferInProcedure( *pSub_System_GlobalArea );
820 }
821
822 //_System_TypeBase_InitializeUserTypesは一番最後にコンパイル
823 extern const UserProc *pSubStaticMethod_System_TypeBase_InitializeUserTypes;
824 pSubStaticMethod_System_TypeBase_InitializeUserTypes->CompleteCompile();
825
826 //_System_InitStaticLocalVariablesは一番最後にコンパイル
827 //※一般関数内の静的変数オブジェクトをすべて収集しなければならない
828 extern const UserProc *pSub_System_InitStaticLocalVariables;
829 pSub_System_InitStaticLocalVariables->CompleteCompile();
830
831 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
832 extern const UserProc *pSub_System_Call_Destructor_of_GlobalObject;
833 pSub_System_Call_Destructor_of_GlobalObject->CompleteCompile();
834
835repeat:
836 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
837 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
838 {
839 UserProc *pUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
840 CompileBufferInProcedure( *pUserProc );
841 }
842
843 if( IsNeedProcCompile() ){
844 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
845 goto repeat;
846 }
847
848 if( !compiler.IsStaticLibrary() )
849 {
850 //_System_TypeBase_InitializeUserTypesは最後のほうでコンパイル
851 pSubStaticMethod_System_TypeBase_InitializeUserTypes->KillCompileStatus();
852 CompileBufferInProcedure( *pSubStaticMethod_System_TypeBase_InitializeUserTypes );
853
854 if( IsNeedProcCompile() ){
855 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
856
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 }
863 }
864
865 //_System_InitStaticLocalVariablesは一番最後にコンパイル
866 pSub_System_InitStaticLocalVariables->KillCompileStatus();
867 CompileBufferInProcedure( *pSub_System_InitStaticLocalVariables );
868
869 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
870 pSub_System_Call_Destructor_of_GlobalObject->KillCompileStatus();
871 CompileBufferInProcedure( *pSub_System_Call_Destructor_of_GlobalObject );
872 }
873}
Note: See TracBrowser for help on using the repository browser.