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

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

CodeGeneratorクラスのベースを実装

File size: 27.8 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 compiler.codeGenerator.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 compiler.codeGenerator.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 compiler.codeGenerator.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 compiler.codeGenerator.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 compiler.codeGenerator.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 compiler.codeGenerator.op_ret();
108 }
109 else if( userProc.GetName() == "_System_GetSp" ){
110 //mov eax,esp
111 compiler.codeGenerator.op_mov_RR(REG_EAX,REG_ESP);
112
113 //add eax,PTR_SIZE
114 compiler.codeGenerator.op_add_RV8(REG_EAX,PTR_SIZE);
115
116 //ret
117 compiler.codeGenerator.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 // コード生成対象を選択
293 compiler.codeGenerator.Select( (const_cast<UserProc *>(pUserProc))->GetNativeCode() );
294
295 if(pUserProc->IsSystem()){
296 ////////////////////
297 // 特殊関数
298 ////////////////////
299
300 extern int AllLocalVarSize;
301 AllLocalVarSize=0;
302
303 SystemProc(*pUserProc);
304
305 pUserProc->SetEndOpAddress( obp );
306 return;
307 }
308
309 cp=pUserProc->GetCodePos();
310 for(;;cp++){
311 if(IsCommandDelimitation(basbuf[cp])) break;
312 }
313 cp--;
314
315 //ローカル変数に関する情報
316 extern int AllLocalVarSize;
317 AllLocalVarSize=0;
318
319 //ローカル変数アドレススケジュール
320 extern DWORD *pLocalVarAddrSchedule;
321 extern int LocalVarAddrScheduleNum;
322 pLocalVarAddrSchedule=(DWORD *)HeapAlloc(hHeap,0,1);
323 LocalVarAddrScheduleNum=0;
324
325 //パラメータ用の変数データを考慮
326 for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
327 Parameter &param = *pUserProc->RealParams()[i3];
328
329 Variable *pVar = new Variable( param.GetVarName(), param, false, param.IsRef(), "" );
330
331 if( param.IsArray() ){
332 pVar->SetArray( param.GetSubscripts() );
333 }
334
335 int varSize;
336 if( param.IsRef() == false && param.IsStruct() ){
337 //構造体のByValパラメータ
338 pVar->ThisIsParameter();
339 varSize=PTR_SIZE;
340 }
341 else{
342 if( param.IsArray() == false ){
343 varSize = pVar->GetMemorySize();
344 }
345 else{
346 varSize=PTR_SIZE;
347 }
348 }
349 AllLocalVarSize+=varSize;
350 pVar->SetOffsetAddress( AllLocalVarSize );
351
352 //レキシカルスコープ情報
353 pVar->SetScopeLevel( GetLexicalScopes().GetNowLevel() );
354 pVar->SetScopeStartAddress( GetLexicalScopes().GetStartAddress() );
355 pVar->bLiving=TRUE;
356
357 pUserProc->GetLocalVars().push_back( pVar );
358 }
359
360 //Thisポインタを示すローカルオフセット値をセット
361 extern int LocalVar_ThisPtrOffset;
362 LocalVar_ThisPtrOffset=AllLocalVarSize;
363
364 BaseOffset=AllLocalVarSize;
365
366 //ret用のアドレスを考慮
367 AllLocalVarSize+=sizeof(long);
368
369
370 ///////////////////////
371 // ここからコード生成
372
373 //sub esp,AllLocalVarSize(スケジュール)
374 compiler.codeGenerator.op_sub_esp(0xFFFFFFFF);
375 LocalVarSchedule=obp-sizeof(long);
376
377 //push ebp
378 compiler.codeGenerator.op_push(REG_EBP);
379
380 //mov ebp,esp
381 OpBuffer[obp++]=(char)0x8B;
382 OpBuffer[obp++]=(char)0xEC;
383
384 //push ebx
385 compiler.codeGenerator.op_push(REG_EBX);
386
387 //push esi
388 OpBuffer[obp++]=(char)0x56;
389
390 //push edi
391 OpBuffer[obp++]=(char)0x57;
392
393 if( !pUserProc->ReturnType().IsNull() ){
394 //戻り値が存在するとき
395
396 const char *temp = pUserProc->GetName().c_str();
397 if( temp[0]==1&&temp[1]==ESC_OPERATOR ){
398 temp = "_System_ReturnValue";
399 }
400
401 if( pUserProc->ReturnType().IsStruct() ){
402 //戻り値用の構造体(値型)はパラメータで引き渡される
403 }
404 else{
405 if( pUserProc->ReturnType().IsObject() ){
406 sprintf(temporary,"%s=Nothing%c%c%s",temp,1,ESC_AS, Compiler::TypeToString( pUserProc->ReturnType() ).c_str() );
407 }
408 else{
409 //戻り値用の変数の定義
410 sprintf(temporary,"%s%c%c%s",temp,1,ESC_AS, Compiler::TypeToString( pUserProc->ReturnType() ).c_str() );
411 }
412
413 OpcodeDim(temporary,0);
414 }
415 }
416
417 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
418 extern DWORD *pExitSubSchedule;
419 extern int ExitSubScheduleNum;
420 pExitSubSchedule=(DWORD *)HeapAlloc(hHeap,0,1);
421 ExitSubScheduleNum=0;
422
423 //ラベル用のメモリを確保
424 extern LABEL *pLabelNames;
425 extern int MaxLabelNum;
426 pLabelNames=(LABEL *)HeapAlloc(hHeap,0,1);
427 MaxLabelNum=0;
428
429 //Gotoラベルスケジュール
430 extern GOTOLABELSCHEDULE *pGotoLabelSchedule;
431 extern int GotoLabelScheduleNum;
432 pGotoLabelSchedule=(GOTOLABELSCHEDULE *)HeapAlloc(hHeap,0,1);
433 GotoLabelScheduleNum=0;
434
435 //With情報のメモリを確保
436 extern WITHINFO WithInfo;
437 WithInfo.ppName=(char **)HeapAlloc(hHeap,0,1);
438 WithInfo.pWithCp=(int *)HeapAlloc(hHeap,0,1);
439 WithInfo.num=0;
440
441 //重複エラー情報管理のメモリを確保
442 extern char **SynonymErrorWords;
443 extern int SynonymErrorNum;
444 SynonymErrorNum=0;
445 SynonymErrorWords=(char **)HeapAlloc(hHeap,0,1);
446
447 //Continueアドレスを初期化
448 extern DWORD dwContinueAddress;
449 dwContinueAddress=-1;
450
451 if(bDebugCompile&&bDebugSupportProc==0){
452 //push dword ptr[ebp+(AllLocalVarSize-BaseOffset)](スケジュール)
453 OpBuffer[obp++]=(char)0xFF;
454 OpBuffer[obp++]=(char)0xB5;
455 EspOffsetSchedule=obp;
456 obp+=sizeof(long);
457
458 //push dword ptr[ebp](以前のebp)
459 OpBuffer[obp++]=(char)0xFF;
460 OpBuffer[obp++]=(char)0x75;
461 OpBuffer[obp++]=(char)0x00;
462
463 //call _DebugSys_StartProc
464 extern const UserProc *pSub_DebugSys_StartProc;
465 compiler.codeGenerator.op_call(pSub_DebugSys_StartProc);
466 }
467
468 if(compiler.pCompilingClass){
469 if( pUserProc->GetName() == compiler.pCompilingClass->GetName() ){
470 ////////////////////////////////////
471 // コンストラクタをコンパイルするとき
472 ////////////////////////////////////
473
474 //コンストラクタのコンパイル開始を通知
475 compiler.pCompilingClass->NotifyStartConstructorCompile();
476
477 //基底クラスかどうかの識別
478 //(継承元がインターフェイスの場合も基底クラスと見なす)
479 BOOL bThisIsSuperClass;
480 if( !compiler.pCompilingClass->HasSuperClass() ) bThisIsSuperClass=1;
481 else if( compiler.pCompilingClass->GetSuperClass().GetConstructorMethod() == NULL ){
482 //インターフェイスを継承したときはコンストラクタを持たない
483 bThisIsSuperClass=1;
484 }
485 else bThisIsSuperClass=0;
486
487 if(!bThisIsSuperClass){
488 /* サブクラスコンストラクタをコンパイルしているときは、
489 基底クラスのコンストラクタを呼び出す */
490
491 i3=cp+1;
492 while(IsCommandDelimitation(basbuf[i3])) i3++;
493 for(i4=0;;i3++,i4++){
494 if(!IsVariableChar(basbuf[i3])){
495 temporary[i4]=0;
496 break;
497 }
498 temporary[i4]=basbuf[i3];
499 }
500 if( compiler.pCompilingClass->GetSuperClass().GetName() == temporary ){
501 //基底クラスのコンストラクタを呼び出す
502 cp=i3;
503 for(i4=0;;cp++,i4++){
504 if(IsCommandDelimitation(basbuf[cp])){
505 temporary[i4]=0;
506 break;
507 }
508 temporary[i4]=basbuf[cp];
509 }
510 if(!(temporary[0]=='('&&temporary[lstrlen(temporary)-1]==')')){
511 SetError(1,NULL,cp);
512 }
513 RemoveStringPare(temporary);
514
515 Type dummyType;
516 CallProc( PROC_DEFAULT
517 , &compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc()
518 , compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc().GetName().c_str()
519 , temporary
520 , dummyType );
521 }
522 else{
523 //基底クラスのコンストラクタを暗黙的に呼び出す
524 Opcode_CallProc("",
525 &compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc(),
526 0,
527 "",
528 0);
529 }
530 }
531
532 //仮想関数テーブルを初期化
533 if( compiler.pCompilingClass->IsExistVirtualFunctions()
534 && !compiler.pCompilingClass->IsAbstract() ){
535 //関数テーブルに値をセット
536 int offset = (int)compiler.pCompilingClass->GetVtblGlobalOffset();
537
538 //mov eax,offset
539 OpBuffer[obp++]=(char)0xB8;
540 *((long *)(OpBuffer+obp))=offset;
541 pobj_DataTableSchedule->add();
542 obp+=sizeof(long);
543
544 //Thisポインタをecxにコピー
545 SetThisPtrToReg(REG_ECX);
546
547 //mov dword ptr[ecx],eax
548 compiler.codeGenerator.op_mov_MR( sizeof(long), REG_EAX, REG_ECX, 0, MOD_BASE );
549 }
550 }
551 else if( pUserProc->IsDestructor() ){
552 //デストラクタをコンパイルしたとき
553
554 //デストラクタのコンパイル開始を通知
555 compiler.pCompilingClass->NotifyStartDestructorCompile();
556 }
557 }
558
559 //////////////////////////////////////////
560 //////////////////////////////////////////
561 ////// プロシージャ内をコンパイル ////////
562 if( pUserProc->IsAutoGeneration() ){
563 AutoGeneration( *pUserProc );
564 }
565 else{
566 if(pUserProc->IsMacro()){
567 CompileBuffer(ESC_ENDMACRO,0);
568 }
569 else{
570 if(pUserProc->IsSub()){
571 CompileBuffer(ESC_ENDSUB,0);
572 }
573 else if(pUserProc->IsFunction()){
574 CompileBuffer(ESC_ENDFUNCTION,0);
575 }
576 }
577 }
578 //////////////////////////////////////////
579 //////////////////////////////////////////
580
581 if( compiler.pCompilingClass ){
582
583 if( compiler.pCompilingClass->IsCompilingConstructor() ){
584 // コンストラクタをコンパイルしていたとき
585
586 // コンストラクタのコンパイルが完了したことを通知
587 compiler.pCompilingClass->NotifyFinishConstructorCompile();
588 }
589 else if( pUserProc->IsDestructor() ){
590 ////////////////////////////////////
591 //デストラクタをコンパイルしたとき
592 ////////////////////////////////////
593
594 // デストラクタのコンパイルが完了したことを通知
595 compiler.pCompilingClass->NotifyFinishDestructorCompile();
596
597 if( compiler.pCompilingClass->HasSuperClass() ){
598 /* サブクラスのデストラクタをコンパイルしているときは、
599 基底クラスのデストラクタを呼び出す */
600
601 const CMethod *method = compiler.pCompilingClass->GetSuperClass().GetDestructorMethod();
602 if( method ){
603 Opcode_CallProc("",
604 &method->GetUserProc(),
605 0,
606 "",
607 0);
608 }
609 }
610 }
611 }
612
613 //ラベル用のメモリを解放
614 for(i3=0;i3<MaxLabelNum;i3++){
615 if(pLabelNames[i3].pName) HeapDefaultFree(pLabelNames[i3].pName);
616 }
617 HeapDefaultFree(pLabelNames);
618
619 //Goto未知ラベルスケジュールを解放
620 for(i3=0;i3<GotoLabelScheduleNum;i3++){
621 if(pGotoLabelSchedule[i3].pName){
622 SetError(6,pGotoLabelSchedule[i3].pName,pGotoLabelSchedule[i3].now_cp);
623 HeapDefaultFree(pGotoLabelSchedule[i3].pName);
624 }
625 else{
626 sprintf(temporary,"%d",pGotoLabelSchedule[i3].line);
627 SetError(6,temporary,pGotoLabelSchedule[i3].now_cp);
628 }
629 }
630 HeapDefaultFree(pGotoLabelSchedule);
631
632 //With情報のメモリを解放
633 for(i3=0;i3<WithInfo.num;i3++){
634 SetError(22,"With",WithInfo.pWithCp[i3]);
635 HeapDefaultFree(WithInfo.ppName[i3]);
636 }
637 HeapDefaultFree(WithInfo.ppName);
638 HeapDefaultFree(WithInfo.pWithCp);
639
640 //push ebp
641 AllLocalVarSize+=sizeof(long);
642
643 //ローカルオブジェクトの解放処理
644 GetLexicalScopes().CallDestructorsOfScopeEnd();
645
646 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
647 for(i3=0;i3<ExitSubScheduleNum;i3++){
648 *((long *)(OpBuffer+pExitSubSchedule[i3]))=obp-(pExitSubSchedule[i3]+sizeof(long));
649 }
650 HeapDefaultFree(pExitSubSchedule);
651
652 if(bDebugCompile&&bDebugSupportProc==0){
653 *((long *)(OpBuffer+EspOffsetSchedule))=AllLocalVarSize-BaseOffset-sizeof(long);
654
655 //call _DebugSys_EndProc
656 extern const UserProc *pSub_DebugSys_EndProc;
657 compiler.codeGenerator.op_call(pSub_DebugSys_EndProc);
658 }
659
660 if( !pUserProc->ReturnType().IsNull() ){
661 //戻り値をeax、edxに設定
662 RELATIVE_VAR RelativeVar;
663
664 const char *temp = pUserProc->GetName().c_str();
665 if( temp[0]==1 && temp[1]==ESC_OPERATOR ){
666 temp="_System_ReturnValue";
667 }
668 GetVarOffsetReadWrite(temp,&RelativeVar,Type());
669
670 i3=pUserProc->ReturnType().GetBasicType();
671
672 if(i3==DEF_OBJECT || i3==DEF_STRUCT){
673 SetVarPtrToEax(&RelativeVar);
674 if( i3==DEF_OBJECT ){
675 //mov eax,dword ptr[eax]
676 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
677 }
678 }
679 else if(i3==DEF_DOUBLE){
680 //fld qword ptr[ebp+offset]
681 OpBuffer[obp++]=(char)0xDD;
682 OpBuffer[obp++]=(char)0x85;
683 *((long *)(OpBuffer+obp))=RelativeVar.offset;
684 AddLocalVarAddrSchedule();
685 obp+=sizeof(long);
686 }
687 else if(i3==DEF_SINGLE){
688 //fld dword ptr[ebp+offset]
689 OpBuffer[obp++]=(char)0xD9;
690 OpBuffer[obp++]=(char)0x85;
691 *((long *)(OpBuffer+obp))=RelativeVar.offset;
692 AddLocalVarAddrSchedule();
693 obp+=sizeof(long);
694 }
695 else if(i3==DEF_INT64||i3==DEF_QWORD){
696 //mov eax,dword ptr[ebp+offset]
697 OpBuffer[obp++]=(char)0x8B;
698 OpBuffer[obp++]=(char)0x85;
699 *((long *)(OpBuffer+obp))=RelativeVar.offset;
700 AddLocalVarAddrSchedule();
701 obp+=sizeof(long);
702
703 //mov edx,dword ptr[ebp+offset+sizeof(long)]
704 OpBuffer[obp++]=(char)0x8B;
705 OpBuffer[obp++]=(char)0x95;
706 *((long *)(OpBuffer+obp))=RelativeVar.offset+sizeof(long);
707 AddLocalVarAddrSchedule();
708 obp+=sizeof(long);
709 }
710 else if(i3==DEF_LONG||i3==DEF_DWORD||
711 IsPtrType(i3)){
712 //mov eax,dword ptr[ebp+offset]
713 OpBuffer[obp++]=(char)0x8B;
714 OpBuffer[obp++]=(char)0x85;
715 *((long *)(OpBuffer+obp))=RelativeVar.offset;
716 AddLocalVarAddrSchedule();
717 obp+=sizeof(long);
718 }
719 else if(i3==DEF_INTEGER||i3==DEF_WORD || (Smoothie::IsUnicode()&&i3==DEF_CHAR)){
720 //xor eax,eax(eaxを0に初期化する)
721 compiler.codeGenerator.op_zero_reg(REG_EAX);
722
723 //mov ax,word ptr[ebp+offset]
724 OpBuffer[obp++]=(char)0x66;
725 OpBuffer[obp++]=(char)0x8B;
726 OpBuffer[obp++]=(char)0x85;
727 *((long *)(OpBuffer+obp))=RelativeVar.offset;
728 AddLocalVarAddrSchedule();
729 obp+=sizeof(long);
730 }
731 else if(i3==DEF_SBYTE||i3==DEF_BYTE||i3==DEF_BOOLEAN || (Smoothie::IsUnicode()==false&&i3==DEF_CHAR)){
732 //xor eax,eax(eaxを0に初期化する)
733 compiler.codeGenerator.op_zero_reg(REG_EAX);
734
735 //mov al,byte ptr[ebp+offset]
736 OpBuffer[obp++]=(char)0x8A;
737 OpBuffer[obp++]=(char)0x85;
738 *((long *)(OpBuffer+obp))=RelativeVar.offset;
739 AddLocalVarAddrSchedule();
740 obp+=sizeof(long);
741 }
742 }
743
744 //ローカル変数アドレススケジュール
745 for(i3=0;i3<LocalVarAddrScheduleNum;i3++){
746 *((long *)(OpBuffer+pLocalVarAddrSchedule[i3]))+=AllLocalVarSize;
747 }
748 HeapDefaultFree(pLocalVarAddrSchedule);
749 BOOST_FOREACH( Variable *pVar, pUserProc->GetLocalVars() ){
750 //後にデバッグで利用する
751 pVar->SetOffsetAddress( AllLocalVarSize - pVar->GetOffsetAddress() );
752 }
753
754 //push ebp、ret用のアドレスを考慮
755 AllLocalVarSize-=sizeof(long)*2;
756
757 //ローカル変数用メモリを確保するためのスケジュール(subコマンド)
758 *((long *)(OpBuffer+LocalVarSchedule))=AllLocalVarSize-BaseOffset;
759
760 //pop edi
761 OpBuffer[obp++]=(char)0x5F;
762
763 //pop esi
764 OpBuffer[obp++]=(char)0x5E;
765
766 //pop ebx
767 compiler.codeGenerator.op_pop(REG_EBX);
768
769 if(bDebugCompile){
770 //cmp esp,ebp
771 compiler.codeGenerator.op_cmp_RR( REG_ESP, REG_EBP );
772
773 //jz 6(次のcallとbreakpointを飛び越す)
774 OpBuffer[obp++]=(char)0x74;
775 OpBuffer[obp++]=(char)0x06;
776
777 //call _esp_error
778 extern const UserProc *pSub_esp_error;
779 compiler.codeGenerator.op_call( pSub_esp_error );
780
781 breakpoint;
782 }
783
784 //mov esp,ebp
785 OpBuffer[obp++]=(char)0x8B;
786 OpBuffer[obp++]=(char)0xE5;
787
788 //pop ebp
789 compiler.codeGenerator.op_pop(REG_EBP);
790
791 //add esp AllLocalVarSize
792 compiler.codeGenerator.op_add_esp(AllLocalVarSize-BaseOffset);
793
794 if( BaseOffset==0 || pUserProc->IsCdecl() ){
795 //ret
796 compiler.codeGenerator.op_ret();
797 }
798 else{
799 //ret BaseOffset(パラメータ分のスタック領域を解放)
800 OpBuffer[obp++]=(char)0xC2;
801 *((_int16 *)(OpBuffer+obp))=(_int16)BaseOffset;
802 obp+=sizeof(_int16);
803 }
804
805
806 pUserProc->SetEndOpAddress( obp );
807
808
809 //重複エラー情報管理のメモリを解放
810 for(i3=0;i3<SynonymErrorNum;i3++) HeapDefaultFree(SynonymErrorWords[i3]);
811 HeapDefaultFree(SynonymErrorWords);
812 SynonymErrorWords=0;
813
814
815 //ローカル変数のネーム情報は後に解放する
816}
817
818void CompileBufferInProcedure( const UserProc &userProc ){
819 if( userProc.IsUsing() == false || userProc.IsCompiled() ) return;
820
821 _compile_proc( &userProc );
822/*
823 // ログを履く
824 char temporary[8192];
825 temporary[0]=0;
826 lstrcat( temporary, "------------------------------------------------------------------\n" );
827 sprintf( temporary + lstrlen(temporary), "【 %s のコード情報】\n", userProc.GetFullName().c_str() );
828 sprintf( temporary + lstrlen(temporary), "code size: %d bytes\n", userProc.GetCodeSize() );
829 lstrcat( temporary, "------------------------------------------------------------------\n" );
830 lstrcat( temporary, "\n" );
831 Smoothie::Logger::Put( temporary );*/
832}
833void CompileLocal(){
834 extern BOOL bDll;
835 if(bDll){
836 //DLLの場合はグローバル変数を初期化するための関数を一番初めにコンパイルする
837 const UserProc *pUserProc=GetSubHash("_System_InitDllGlobalVariables");
838 if(pUserProc){
839 CompileBufferInProcedure( *pUserProc );
840 }
841 else SetError(300,NULL,cp);
842 }
843
844 //_System_TypeBase_InitializeUserTypesは一番最後にコンパイル
845 extern const UserProc *pSubStaticMethod_System_TypeBase_InitializeUserTypes;
846 pSubStaticMethod_System_TypeBase_InitializeUserTypes->CompleteCompile();
847
848 //_System_InitStaticLocalVariablesは一番最後にコンパイル
849 //※一般関数内の静的変数オブジェクトをすべて収集しなければならない
850 extern const UserProc *pSub_System_InitStaticLocalVariables;
851 pSub_System_InitStaticLocalVariables->CompleteCompile();
852
853 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
854 extern const UserProc *pSub_System_Call_Destructor_of_GlobalObject;
855 pSub_System_Call_Destructor_of_GlobalObject->CompleteCompile();
856
857repeat:
858 compiler.GetMeta().GetUserProcs().Iterator_Reset();
859 while( compiler.GetMeta().GetUserProcs().Iterator_HasNext() )
860 {
861 UserProc *pUserProc = compiler.GetMeta().GetUserProcs().Iterator_GetNext();
862 CompileBufferInProcedure( *pUserProc );
863 }
864
865 if( IsNeedProcCompile() ){
866 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
867 goto repeat;
868 }
869
870 //_System_TypeBase_InitializeUserTypesは最後のほうでコンパイル
871 pSubStaticMethod_System_TypeBase_InitializeUserTypes->KillCompileStatus();
872 CompileBufferInProcedure( *pSubStaticMethod_System_TypeBase_InitializeUserTypes );
873
874 if( IsNeedProcCompile() ){
875 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
876
877 compiler.GetMeta().GetUserProcs().Iterator_Reset();
878 while( compiler.GetMeta().GetUserProcs().Iterator_HasNext() )
879 {
880 UserProc *pUserProc = compiler.GetMeta().GetUserProcs().Iterator_GetNext();
881 CompileBufferInProcedure( *pUserProc );
882 }
883 }
884
885 //_System_InitStaticLocalVariablesは一番最後にコンパイル
886 pSub_System_InitStaticLocalVariables->KillCompileStatus();
887 CompileBufferInProcedure( *pSub_System_InitStaticLocalVariables );
888
889 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
890 pSub_System_Call_Destructor_of_GlobalObject->KillCompileStatus();
891 CompileBufferInProcedure( *pSub_System_Call_Destructor_of_GlobalObject );
892}
Note: See TracBrowser for help on using the repository browser.