source: dev/BasicCompiler32/Compile_ProcOp.cpp@ 47

Last change on this file since 47 was 42, checked in by dai_9181, 18 years ago

EnumメンバにOpenやPrintを指定できないバグを修正。
DLLコンパイル時に静的メンバが認識されないバグを修正。

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