source: dev/BasicCompiler32/Compile_ProcOp.cpp@ 95

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

RegisterGlobalRootsの自動生成に対応

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