source: dev/BasicCompiler32/Compile_ProcOp.cpp@ 122

Last change on this file since 122 was 108, checked in by dai_9181, 18 years ago

関数、クラスメソッドにImports機構を適用。

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