source: dev/BasicCompiler32/Compile_ProcOp.cpp@ 72

Last change on this file since 72 was 64, checked in by dai_9181, 18 years ago

すべてのオブジェクトを参照型に切り替えた。

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