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

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