source: dev/BasicCompiler32/Compile_ProcOp.cpp@ 17

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

定数メンバ機能を有効にした。

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