source: dev/BasicCompiler32/Compile_ProcOp.cpp@ 60

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

・コマンドラインオプション "/unicode" に対応。
・Unicode文字セットを使用する場合に "UNICODE" が自動的に#defineされるようにしました。
・SByte型(8ビット符号付き整数型)を追加。
・Char型を文字型に変更。
・プロジェクトオプションから「Unicode文字セットを使用する」チェックを選択できるようにしました。

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 //クラスに属する静的メンバを定義
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 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->GetConstructorMethod() ){
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->GetConstructorMethod() == NULL ){
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 Opcode_CallProc(temporary,
500 pobj_CompilingClass->pobj_InheritsClass->GetConstructorMethod()->psi,
501 0,
502 "",
503 0);
504 }
505 else{
506 //基底クラスのコンストラクタを暗黙的に呼び出す
507 Opcode_CallProc("",
508 pobj_CompilingClass->pobj_InheritsClass->GetConstructorMethod()->psi,
509 0,
510 "",
511 0);
512 }
513 }
514
515
516 //実体クラスを持つメンバのコンストラクタ(引数有りを除く)を呼び出す
517 int offset;
518 int MemberTypeSize;
519 int MemberObjectNum;
520 if(pobj_CompilingClass->vtbl_num) offset=PTR_SIZE;
521 else offset=0;
522 for(i3=0;i3<pobj_CompilingClass->iMemberNum;i3++){
523 CMember *pMember = pobj_CompilingClass->ppobj_Member[i3];
524 MemberTypeSize=
525 GetTypeSize(pMember->TypeInfo.type,
526 pMember->TypeInfo.u.lpIndex);
527
528 MemberObjectNum=
529 JumpSubScripts(pMember->SubScripts);
530
531 offset=GetSizeOfClassMember(pobj_CompilingClass,pMember->name,NULL);
532
533 if(pMember->TypeInfo.type==DEF_OBJECT && pMember->IsRef() == false){
534 CMethod *method = pMember->TypeInfo.u.pobj_Class->GetConstructorMethod();
535 if( method ){
536 if( method->psi->RealParmNum == 1 ){ //Thisポインタを第一引数に持ち合わせるため、引数なしの場合はこの値が1になる
537 for(i4=0;i4<MemberObjectNum;i4++){
538 //Thisポインタをecxにコピー
539 SetThisPtrToReg(REG_ECX);
540
541 //add ecx,offset
542 OpBuffer[obp++]=(char)0x81;
543 OpBuffer[obp++]=(char)0xC1;
544 *((long *)(OpBuffer+obp))=offset+i4*MemberTypeSize;
545 obp+=sizeof(long);
546
547 //push ecx
548 op_push(REG_ECX);
549
550 //call constructor
551 op_call( method->psi );
552 }
553 }
554 }
555 }
556 }
557
558
559 //仮想関数テーブルを初期化
560 if(pobj_CompilingClass->vtbl_num&&
561 pobj_CompilingClass->IsAbstract()==false){
562 //関数テーブルに値をセット
563 offset=pobj_CompilingClass->GetVtblGlobalOffset();
564
565 //mov eax,offset
566 OpBuffer[obp++]=(char)0xB8;
567 *((long *)(OpBuffer+obp))=offset;
568 pobj_DataTableSchedule->add();
569 obp+=sizeof(long);
570
571 //Thisポインタをecxにコピー
572 SetThisPtrToReg(REG_ECX);
573
574 //mov dword ptr[ecx],eax
575 OpBuffer[obp++]=(char)0x89;
576 OpBuffer[obp++]=(char)0x01;
577 }
578 }
579 else if(psi->name[0]=='~'){
580 //デストラクタをコンパイルしたとき
581
582 //デストラクタのコンパイル開始を通知
583 pobj_CompilingClass->NotifyStartDestructorCompile();
584 }
585 }
586
587 //////////////////////////////////////////
588 //////////////////////////////////////////
589 ////// プロシージャ内をコンパイル ////////
590 if(psi->dwType==SUBTYPE_SUB) CompileBuffer(ESC_ENDSUB,0);
591 else if(psi->dwType==SUBTYPE_FUNCTION) CompileBuffer(ESC_ENDFUNCTION,0);
592 else if(psi->dwType==SUBTYPE_MACRO) CompileBuffer(ESC_ENDMACRO,0);
593 //////////////////////////////////////////
594 //////////////////////////////////////////
595
596 if( pobj_CompilingClass ){
597
598 if( pobj_CompilingClass->IsCompilingConstructor() ){
599 // コンストラクタをコンパイルしていたとき
600
601 // コンストラクタのコンパイルが完了したことを通知
602 pobj_CompilingClass->NotifyFinishConstructorCompile();
603 }
604 else if(psi->name[0]=='~'){
605 ////////////////////////////////////
606 //デストラクタをコンパイルしたとき
607 ////////////////////////////////////
608
609 // デストラクタのコンパイルが完了したことを通知
610 pobj_CompilingClass->NotifyFinishDestructorCompile();
611
612 if(pobj_CompilingClass->pobj_InheritsClass){
613 /* サブクラスのデストラクタをコンパイルしているときは、
614 基底クラスのデストラクタを呼び出す */
615
616 CMethod *method = pobj_CompilingClass->pobj_InheritsClass->GetDestructorMethod();
617 if( method ){
618 Opcode_CallProc("",
619 method->psi,
620 0,
621 "",
622 0);
623 }
624 }
625
626 //実体クラスを持つメンバのデストラクタを呼び出す
627 //※コンストラクタと逆順序で呼び出す
628 int offset;
629 int MemberTypeSize;
630 int MemberObjectNum;
631 offset=GetTypeSize(DEF_OBJECT,(LONG_PTR)pobj_CompilingClass);
632 for(i3=pobj_CompilingClass->iMemberNum-1;i3>=0;i3--){
633 CMember *pMember = pobj_CompilingClass->ppobj_Member[i3];
634
635 MemberTypeSize=
636 GetTypeSize(pMember->TypeInfo.type,
637 pMember->TypeInfo.u.lpIndex);
638
639 MemberObjectNum=
640 JumpSubScripts(pMember->SubScripts);
641
642 offset-=MemberTypeSize*MemberObjectNum;
643
644 if(pMember->TypeInfo.type==DEF_OBJECT){
645 CMethod *method = pMember->TypeInfo.u.pobj_Class->GetDestructorMethod();
646 if( method ){
647 for(i4=MemberObjectNum-1;i4>=0;i4--){
648 //Thisポインタをecxにコピー
649 SetThisPtrToReg(REG_ECX);
650
651 //add ecx,offset
652 OpBuffer[obp++]=(char)0x81;
653 OpBuffer[obp++]=(char)0xC1;
654 *((long *)(OpBuffer+obp))=offset+i4*MemberTypeSize;
655 obp+=sizeof(long);
656
657 //push ecx
658 op_push(REG_ECX);
659
660 //call destructor
661 op_call( method->psi );
662 }
663 }
664 }
665 }
666 }
667 }
668
669 //ラベル用のメモリを解放
670 for(i3=0;i3<MaxLabelNum;i3++){
671 if(pLabelNames[i3].pName) HeapDefaultFree(pLabelNames[i3].pName);
672 }
673 HeapDefaultFree(pLabelNames);
674
675 //Goto未知ラベルスケジュールを解放
676 for(i3=0;i3<GotoLabelScheduleNum;i3++){
677 if(pGotoLabelSchedule[i3].pName){
678 SetError(6,pGotoLabelSchedule[i3].pName,pGotoLabelSchedule[i3].now_cp);
679 HeapDefaultFree(pGotoLabelSchedule[i3].pName);
680 }
681 else{
682 sprintf(temporary,"%d",pGotoLabelSchedule[i3].line);
683 SetError(6,temporary,pGotoLabelSchedule[i3].now_cp);
684 }
685 }
686 HeapDefaultFree(pGotoLabelSchedule);
687
688 //With情報のメモリを解放
689 for(i3=0;i3<WithInfo.num;i3++){
690 SetError(22,"With",WithInfo.pWithCp[i3]);
691 HeapDefaultFree(WithInfo.ppName[i3]);
692 }
693 HeapDefaultFree(WithInfo.ppName);
694 HeapDefaultFree(WithInfo.pWithCp);
695
696 //重複エラー情報管理のメモリを解放
697 for(i3=0;i3<SynonymErrorNum;i3++) HeapDefaultFree(SynonymErrorWords[i3]);
698 HeapDefaultFree(SynonymErrorWords);
699
700 //push ebp
701 AllLocalVarSize+=sizeof(long);
702
703 //ローカルオブジェクトの解放処理
704 obj_LexScopes.CallDestructorsOfScopeEnd();
705
706 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
707 for(i3=0;i3<ExitSubScheduleNum;i3++){
708 *((long *)(OpBuffer+pExitSubSchedule[i3]))=obp-(pExitSubSchedule[i3]+sizeof(long));
709 }
710 HeapDefaultFree(pExitSubSchedule);
711
712 if(bDebugCompile&&bDebugSupportProc==0){
713 *((long *)(OpBuffer+EspOffsetSchedule))=AllLocalVarSize-BaseOffset;
714
715 //call _DebugSys_EndProc
716 extern SUBINFO *pSub_DebugSys_EndProc;
717 op_call(pSub_DebugSys_EndProc);
718 }
719
720 if(psi->ReturnType!=-1){
721 //戻り値をeax、edxに設定
722 RELATIVE_VAR RelativeVar;
723 LONG_PTR lpIndex;
724
725 char *temp;
726 if(psi->name[0]==1&&psi->name[1]==ESC_OPERATOR)
727 temp="_System_ReturnValue";
728 else temp=psi->name;
729 GetVarOffsetReadWrite(temp,&i3,&RelativeVar,&lpIndex);
730
731 i3=psi->ReturnType;
732
733 if(i3==DEF_OBJECT){
734 SetVarPtrToEax(&RelativeVar);
735 }
736 else if(i3==DEF_DOUBLE){
737 //fld qword ptr[ebp+offset]
738 OpBuffer[obp++]=(char)0xDD;
739 OpBuffer[obp++]=(char)0x85;
740 *((long *)(OpBuffer+obp))=RelativeVar.offset;
741 AddLocalVarAddrSchedule();
742 obp+=sizeof(long);
743 }
744 else if(i3==DEF_SINGLE){
745 //fld dword ptr[ebp+offset]
746 OpBuffer[obp++]=(char)0xD9;
747 OpBuffer[obp++]=(char)0x85;
748 *((long *)(OpBuffer+obp))=RelativeVar.offset;
749 AddLocalVarAddrSchedule();
750 obp+=sizeof(long);
751 }
752 else if(i3==DEF_INT64||i3==DEF_QWORD){
753 //mov eax,dword ptr[ebp+offset]
754 OpBuffer[obp++]=(char)0x8B;
755 OpBuffer[obp++]=(char)0x85;
756 *((long *)(OpBuffer+obp))=RelativeVar.offset;
757 AddLocalVarAddrSchedule();
758 obp+=sizeof(long);
759
760 //mov edx,dword ptr[ebp+offset+sizeof(long)]
761 OpBuffer[obp++]=(char)0x8B;
762 OpBuffer[obp++]=(char)0x95;
763 *((long *)(OpBuffer+obp))=RelativeVar.offset+sizeof(long);
764 AddLocalVarAddrSchedule();
765 obp+=sizeof(long);
766 }
767 else if(i3==DEF_LONG||i3==DEF_DWORD||
768 IsPtrType(i3)){
769 //mov eax,dword ptr[ebp+offset]
770 OpBuffer[obp++]=(char)0x8B;
771 OpBuffer[obp++]=(char)0x85;
772 *((long *)(OpBuffer+obp))=RelativeVar.offset;
773 AddLocalVarAddrSchedule();
774 obp+=sizeof(long);
775 }
776 else if(i3==DEF_INTEGER||i3==DEF_WORD || (isUnicode&&i3==DEF_CHAR)){
777 //xor eax,eax(eaxを0に初期化する)
778 op_zero_reg(REG_EAX);
779
780 //mov ax,word ptr[ebp+offset]
781 OpBuffer[obp++]=(char)0x66;
782 OpBuffer[obp++]=(char)0x8B;
783 OpBuffer[obp++]=(char)0x85;
784 *((long *)(OpBuffer+obp))=RelativeVar.offset;
785 AddLocalVarAddrSchedule();
786 obp+=sizeof(long);
787 }
788 else if(i3==DEF_SBYTE||i3==DEF_BYTE||i3==DEF_BOOLEAN || (isUnicode==false&&i3==DEF_CHAR)){
789 //xor eax,eax(eaxを0に初期化する)
790 op_zero_reg(REG_EAX);
791
792 //mov al,byte ptr[ebp+offset]
793 OpBuffer[obp++]=(char)0x8A;
794 OpBuffer[obp++]=(char)0x85;
795 *((long *)(OpBuffer+obp))=RelativeVar.offset;
796 AddLocalVarAddrSchedule();
797 obp+=sizeof(long);
798 }
799 }
800
801 //ローカル変数アドレススケジュール
802 for(i3=0;i3<LocalVarAddrScheduleNum;i3++){
803 *((long *)(OpBuffer+pLocalVarAddrSchedule[i3]))+=AllLocalVarSize;
804 }
805 HeapDefaultFree(pLocalVarAddrSchedule);
806 for(i3=0;i3<MaxLocalVarNum;i3++){
807 LocalVar[i3].offset=AllLocalVarSize-LocalVar[i3].offset;//後にデバッグで利用する
808 }
809
810 //push ebp、ret用のアドレスを考慮
811 AllLocalVarSize-=sizeof(long)*2;
812
813 //ローカル変数用メモリを確保するためのスケジュール(subコマンド)
814 *((long *)(OpBuffer+LocalVarSchedule))=AllLocalVarSize-BaseOffset;
815
816 //pop edi
817 OpBuffer[obp++]=(char)0x5F;
818
819 //pop esi
820 OpBuffer[obp++]=(char)0x5E;
821
822 //pop ebx
823 op_pop(REG_EBX);
824
825 //mov esp,ebp
826 OpBuffer[obp++]=(char)0x8B;
827 OpBuffer[obp++]=(char)0xE5;
828
829 //pop ebp
830 op_pop(REG_EBP);
831
832 //add esp AllLocalVarSize
833 op_add_esp(AllLocalVarSize-BaseOffset);
834
835 if(BaseOffset==0||psi->bCdecl){
836 //ret 0
837 OpBuffer[obp++]=(char)0xC3;
838 }
839 else{
840 //ret BaseOffset(パラメータ分のスタック領域を解放)
841 OpBuffer[obp++]=(char)0xC2;
842 *((_int16 *)(OpBuffer+obp))=(_int16)BaseOffset;
843 obp+=sizeof(_int16);
844 }
845
846 psi->EndOpAddr=obp;
847 psi->pVar=LocalVar;
848 psi->VarNum=MaxLocalVarNum;
849
850 //ローカル変数のネーム情報は後に解放する
851}
852void CompileLocal(){
853 extern SUBINFO **ppSubHash;
854 int i2;
855 SUBINFO *psi;
856
857 extern BOOL bDll;
858 if(bDll){
859 //DLLの場合はグローバル変数を初期化するための関数を一番初めにコンパイルする
860 psi=GetSubHash("_System_InitDllGlobalVariables");
861 if(psi){
862 CompileBufferInProcedure(psi);
863 }
864 else SetError(300,NULL,cp);
865 }
866
867 //_System_InitStaticLocalVariablesは一番最後にコンパイル
868 //※一般関数内の静的変数オブジェクトをすべて収集しなければならない
869 extern SUBINFO *pSub_System_InitStaticLocalVariables;
870 pSub_System_InitStaticLocalVariables->bCompile=1;
871
872 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
873 extern SUBINFO *pSub_System_Call_Destructor_of_GlobalObject;
874 pSub_System_Call_Destructor_of_GlobalObject->bCompile=1;
875
876Repeat:
877 for(i2=0;i2<MAX_HASH;i2++){
878 psi=ppSubHash[i2];
879 while(psi){
880 CompileBufferInProcedure(psi);
881 psi=psi->pNextData;
882 }
883 }
884
885 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
886 for(i2=0;i2<MAX_HASH;i2++){
887 psi=ppSubHash[i2];
888 while(psi){
889 if(psi->bUse&&psi->bCompile==0){
890 goto Repeat;
891 }
892
893 psi=psi->pNextData;
894 }
895 }
896
897 //_System_InitStaticLocalVariablesは一番最後にコンパイル
898 pSub_System_InitStaticLocalVariables->bCompile=0;
899 CompileBufferInProcedure(pSub_System_InitStaticLocalVariables);
900
901 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
902 pSub_System_Call_Destructor_of_GlobalObject->bCompile=0;
903 CompileBufferInProcedure(pSub_System_Call_Destructor_of_GlobalObject);
904}
Note: See TracBrowser for help on using the repository browser.