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

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

コード全体のリファクタリングを実施

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