source: dev/BasicCompiler64/Compile_ProcOp.cpp@ 60

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

ppobj_Member及びppobj_StaticMemberを廃止し、vectorに統一した(methods及びstaticMethods)。

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