source: dev/BasicCompiler32/Compile_ProcOp.cpp@ 101

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

名前空間機能をグローバル関数に適用(作業完了)。

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