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

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