source: dev/BasicCompiler32/Compile_ProcOp.cpp@ 79

Last change on this file since 79 was 78, checked in by dai_9181, 18 years ago

CTypeDef → TypeDef
Houseクラスを追加。
オーバーロードレベルの種類を追加(レベル1に挿入)

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