source: dev/BasicCompiler64/Compile_ProcOp.cpp@ 48

Last change on this file since 48 was 44, 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
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
47 //クラスに属する静的メンバを定義
48 CMember::InitStaticMember();
49
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;
86 CallConstractor(
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
114 obj_LexScopes.CallDestructorsOfScopeEnd();
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;
211 int i3,i4,i5,VarSize,BufferSize;
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
332 // TODO: パラメータのConst定義の指定が未完成
333 LocalVar[MaxLocalVarNum].bConst=false;
334
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;
379
380 //コンパイルスタートをクラス管理クラスに追加
381 pobj_DBClass->StartCompile( psi );
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
466 if(psi->ReturnType==DEF_OBJECT && psi->isReturnRef == false){
467 //戻り値用オブジェクトのコンストラクタを呼び出す
468 if(psi->u.Return_pobj_c->ConstructorMemberSubIndex!=-1){
469 sprintf(temporary,"%s.%s()",temp,psi->u.Return_pobj_c->name);
470 OpcodeOthers(temporary);
471 }
472 }
473 else{
474 //戻り値用の変数の定義
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);
481 GetTypeName(psi->ReturnType,psi->u.ReturnIndex,temporary+lstrlen(temporary));
482
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
512 //コンストラクタのコンパイル開始を通知
513 pobj_CompilingClass->NotifyStartConstructorCompile();
514
515 //基底クラスかどうかの識別
516 //(継承元がインターフェイスの場合も基底クラスと見なす)
517 BOOL bThisIsSuperClass;
518 if(pobj_CompilingClass->pobj_InheritsClass==0) bThisIsSuperClass=1;
519 else if(pobj_CompilingClass->pobj_InheritsClass->ConstructorMemberSubIndex==-1){
520 //インターフェイスを継承したときはコンストラクタを持たない
521 bThisIsSuperClass=1;
522 }
523 else bThisIsSuperClass=0;
524
525 if(!bThisIsSuperClass){
526 /* サブクラスコンストラクタをコンパイルしているときは、
527 基底クラスのコンストラクタを呼び出す */
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){
540 //基底クラスのコンストラクタを呼び出す
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 i3=pobj_CompilingClass->pobj_InheritsClass->ConstructorMemberSubIndex;
555 Opcode_CallProc(temporary,
556 pobj_CompilingClass->ppobj_Method[i3]->psi,
557 0,
558 "",
559 0);
560 }
561 else{
562 //基底クラスのコンストラクタを暗黙的に呼び出す
563 i3=pobj_CompilingClass->pobj_InheritsClass->ConstructorMemberSubIndex;
564 Opcode_CallProc("",
565 pobj_CompilingClass->ppobj_Method[i3]->psi,
566 0,
567 "",
568 0);
569 }
570 }
571
572 //実体クラスを持つメンバのコンストラクタ(引数有りを除く)を呼び出す
573 int offset;
574 int MemberTypeSize;
575 int MemberObjectNum;
576 if(pobj_CompilingClass->vtbl_num) offset=PTR_SIZE;
577 else offset=0;
578 for(i3=0;i3<pobj_CompilingClass->iMemberNum;i3++){
579 CMember *pMember = pobj_CompilingClass->ppobj_Member[i3];
580 MemberTypeSize=
581 GetTypeSize(pMember->TypeInfo.type,
582 pMember->TypeInfo.u.lpIndex);
583
584 MemberObjectNum=
585 JumpSubScripts(pMember->SubScripts);
586
587 offset=GetSizeOfClassMember(pobj_CompilingClass,pMember->name,NULL);
588
589 if(pMember->TypeInfo.type==DEF_OBJECT && pMember->IsRef() == false){
590 if(pMember->TypeInfo.u.pobj_Class->ConstructorMemberSubIndex!=-1){
591 i5=pMember->TypeInfo.u.pobj_Class->ConstructorMemberSubIndex;
592 if(pMember->TypeInfo.u.pobj_Class->ppobj_Method[i5]->psi->RealParmNum==1){ //Thisポインタを第一引数に持ち合わせるため、引数なしの場合はこの値が1になる
593 for(i4=0;i4<MemberObjectNum;i4++){
594 //Thisポインタをrcxにコピー
595 SetThisPtrToReg(REG_RCX);
596
597 //add rcx,offset
598 op_add64_value(REG_RCX,offset+i4*MemberTypeSize);
599
600 //call constructor
601 op_call(pMember->TypeInfo.u.pobj_Class->ppobj_Method[i5]->psi);
602 }
603 }
604 }
605 }
606 }
607
608 //仮想関数テーブルを初期化
609 if(pobj_CompilingClass->vtbl_num&&
610 pobj_CompilingClass->IsAbstract()==false){
611 //関数テーブルに値をセット
612 offset=(int)pobj_CompilingClass->GetVtblGlobalOffset();
613
614 //mov rax,offset
615 op_mov_RV(sizeof(_int64),REG_RAX,offset);
616 obp-=sizeof(long);
617 pobj_DataTableSchedule->add();
618 obp+=sizeof(long);
619
620 //Thisポインタをrcxにコピー
621 SetThisPtrToReg(REG_RCX);
622
623 //mov qword ptr[rcx],rax
624 op_mov_MR(sizeof(_int64),REG_RAX,REG_RCX,0,MOD_BASE);
625 }
626 }
627 else if(psi->name[0]=='~'){
628 //デストラクタをコンパイルしたとき
629
630 //デストラクタのコンパイル開始を通知
631 pobj_CompilingClass->NotifyStartDestructorCompile();
632 }
633 }
634
635 //////////////////////////////////////////
636 //////////////////////////////////////////
637 ////// プロシージャ内をコンパイル ////////
638 if(psi->dwType==SUBTYPE_SUB) CompileBuffer(ESC_ENDSUB,0);
639 else if(psi->dwType==SUBTYPE_FUNCTION) CompileBuffer(ESC_ENDFUNCTION,0);
640 else if(psi->dwType==SUBTYPE_MACRO) CompileBuffer(ESC_ENDMACRO,0);
641 //////////////////////////////////////////
642 //////////////////////////////////////////
643
644 if( pobj_CompilingClass ){
645
646 if( pobj_CompilingClass->IsCompilingConstructor() ){
647 // コンストラクタをコンパイルしていたとき
648
649 // コンストラクタのコンパイルが完了したことを通知
650 pobj_CompilingClass->NotifyFinishConstructorCompile();
651 }
652 else if(psi->name[0]=='~'){
653 ////////////////////////////////////
654 //デストラクタをコンパイルしたとき
655 ////////////////////////////////////
656
657 // デストラクタのコンパイルが完了したことを通知
658 pobj_CompilingClass->NotifyFinishDestructorCompile();
659
660 if(pobj_CompilingClass->pobj_InheritsClass){
661 /* サブクラスのデストラクタをコンパイルしているときは、
662 基底クラスのデストラクタを呼び出す */
663
664 i3=pobj_CompilingClass->pobj_InheritsClass->DestructorMemberSubIndex;
665 if(i3!=-1){
666 Opcode_CallProc("",
667 pobj_CompilingClass->ppobj_Method[i3]->psi,
668 0,
669 "",
670 0);
671 }
672 }
673
674 //実体クラスを持つメンバのデストラクタを呼び出す
675 //※コンストラクタと逆順序で呼び出す
676 int offset;
677 int MemberTypeSize;
678 int MemberObjectNum;
679 for(i3=pobj_CompilingClass->iMemberNum-1;i3>=0;i3--){
680 CMember *pMember = pobj_CompilingClass->ppobj_Member[i3];
681 MemberTypeSize=
682 GetTypeSize(pMember->TypeInfo.type,
683 pMember->TypeInfo.u.lpIndex);
684
685 MemberObjectNum=
686 JumpSubScripts(pMember->SubScripts);
687
688 offset=GetSizeOfClassMember(pobj_CompilingClass,pMember->name,NULL);
689
690 if(pMember->TypeInfo.type==DEF_OBJECT && pMember->IsRef() == false){
691 if(pMember->TypeInfo.u.pobj_Class->DestructorMemberSubIndex!=-1){
692 for(i4=MemberObjectNum-1;i4>=0;i4--){
693 //Thisポインタをrcxにコピー
694 SetThisPtrToReg(REG_RCX);
695
696 //add rcx,offset
697 op_add64_value(REG_RCX,offset+i4*MemberTypeSize);
698
699 //call destructor
700 i5=pMember->TypeInfo.u.pobj_Class->DestructorMemberSubIndex;
701 op_call(pMember->TypeInfo.u.pobj_Class->ppobj_Method[i5]->psi);
702 }
703 }
704 }
705 }
706 }
707 }
708
709 //ラベル用のメモリを解放
710 for(i3=0;i3<MaxLabelNum;i3++){
711 if(pLabelNames[i3].pName) HeapDefaultFree(pLabelNames[i3].pName);
712 }
713 HeapDefaultFree(pLabelNames);
714
715 //Goto未知ラベルスケジュールを解放
716 for(i3=0;i3<GotoLabelScheduleNum;i3++){
717 if(pGotoLabelSchedule[i3].pName){
718 SetError(6,pGotoLabelSchedule[i3].pName,pGotoLabelSchedule[i3].now_cp);
719 HeapDefaultFree(pGotoLabelSchedule[i3].pName);
720 }
721 else{
722 sprintf(temporary,"%d",pGotoLabelSchedule[i3].line);
723 SetError(6,temporary,pGotoLabelSchedule[i3].now_cp);
724 }
725 }
726 HeapDefaultFree(pGotoLabelSchedule);
727
728 //With情報のメモリを解放
729 for(i3=0;i3<WithInfo.num;i3++){
730 SetError(22,"With",WithInfo.pWithCp[i3]);
731 HeapDefaultFree(WithInfo.ppName[i3]);
732 }
733 HeapDefaultFree(WithInfo.ppName);
734 HeapDefaultFree(WithInfo.pWithCp);
735
736 //重複エラー情報管理のメモリを解放
737 for(i3=0;i3<SynonymErrorNum;i3++) HeapDefaultFree(SynonymErrorWords[i3]);
738 HeapDefaultFree(SynonymErrorWords);
739
740 //ローカルオブジェクト(レキシカルスコープレベル=0)の解放処理
741 obj_LexScopes.CallDestructorsOfScopeEnd();
742
743 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
744 for(i3=0;i3<ExitSubScheduleNum;i3++){
745 *((long *)(OpBuffer+pExitSubSchedule[i3]))=obp-(pExitSubSchedule[i3]+sizeof(long));
746 }
747 HeapDefaultFree(pExitSubSchedule);
748
749 if(bDebugCompile&&bDebugSupportProc==0){
750 //call _DebugSys_EndProc
751 extern SUBINFO *pSub_DebugSys_EndProc;
752 op_call(pSub_DebugSys_EndProc);
753 }
754
755 if(psi->ReturnType!=-1){
756 //////////////////////////////////
757 // 戻り値をraxまたはxmm0に設定
758 //////////////////////////////////
759
760 RELATIVE_VAR RelativeVar;
761 LONG_PTR lpIndex;
762
763 char *temp;
764 if(psi->name[0]==1&&psi->name[1]==ESC_OPERATOR)
765 temp="_System_ReturnValue";
766 else temp=psi->name;
767 GetVarOffsetReadWrite(temp,&i3,&RelativeVar,&lpIndex);
768
769 i3=psi->ReturnType;
770
771 if(i3==DEF_OBJECT){
772 SetVarPtrToReg(REG_RAX,&RelativeVar);
773 }
774 else if(i3==DEF_DOUBLE){
775 //64ビット実数型
776 SetXmmReg_DoubleVariable(&RelativeVar,REG_XMM0);
777 }
778 else if(i3==DEF_SINGLE){
779 //32ビット実数型
780 SetXmmReg_SingleVariable(&RelativeVar,REG_XMM0);
781 }
782 else if(IsWholeNumberType(i3)){
783 //整数型
784 SetReg_WholeVariable(i3,&RelativeVar,REG_RAX);
785 }
786 else SetError(300,NULL,cp);
787 }
788
789 //ローカル変数領域のサイズをスタックフレームに通知
790 int iLocalParmSize;
791 iLocalParmSize=AllLocalVarSize-BaseLocalVar;
792 pobj_sf->SetLocalParmSize(iLocalParmSize);
793
794 //ローカル変数アドレススケジュール
795 for(i3=0;i3<LocalVarAddrScheduleNum;i3++){
796 *((long *)(OpBuffer+pLocalVarAddrSchedule[i3]))+=AllLocalVarSize+pobj_sf->GetFrameSize();
797 }
798 HeapDefaultFree(pLocalVarAddrSchedule);
799 for(i3=0;i3<MaxLocalVarNum;i3++){
800 //後にデバッグで利用する
801 LocalVar[i3].offset=AllLocalVarSize+pobj_sf->GetFrameSize()-LocalVar[i3].offset;
802 }
803
804 //mov reg,qword ptr[rsp+offset] ※スタックフレームを利用
805 pobj_sf->pop(REG_R15);
806 pobj_sf->pop(REG_R14);
807 pobj_sf->pop(REG_R13);
808 pobj_sf->pop(REG_R12);
809 pobj_sf->pop(REG_RDI);
810 pobj_sf->pop(REG_RSI);
811 pobj_sf->pop(REG_RBX);
812
813 int iStackFrameSize;
814 iStackFrameSize=iLocalParmSize + pobj_sf->GetFrameSize();
815
816 //add rsp,スタックフレームサイズ
817 op_add_rsp(iStackFrameSize);
818
819 //ret 0
820 OpBuffer[obp++]=(char)0xC3;
821
822
823 //デバッグ用
824 if(RspOffsetSchedule){
825 *((long *)(OpBuffer+RspOffsetSchedule))=iStackFrameSize;
826 *((long *)(OpBuffer+RspOffsetSchedule2))=iStackFrameSize+sizeof(_int64);
827 }
828
829
830 //スタックフレームスケジュール(subコマンド)
831 *((long *)(OpBuffer+StackFrameSchedule))=iStackFrameSize;
832
833 //スタックフレームスケジュールを実行
834 pobj_sf->RunningSchedule();
835 delete pobj_sf;
836 pobj_sf=0;
837
838
839 psi->EndOpAddr=obp;
840 psi->pVar=LocalVar;
841 psi->VarNum=MaxLocalVarNum;
842
843 //ローカル変数のネーム情報は後に解放する
844}
845void CompileLocal(){
846 extern SUBINFO **ppSubHash;
847 int i2;
848 SUBINFO *psi;
849
850 extern BOOL bDll;
851 if(bDll){
852 //DLLの場合はグローバル変数を初期化するための関数を一番初めにコンパイルする
853 psi=GetSubHash("_System_InitDllGlobalVariables");
854 if(psi){
855 CompileBufferInProcedure(psi);
856 }
857 else SetError(300,NULL,cp);
858 }
859
860 //_System_InitStaticLocalVariablesは一番最後にコンパイル
861 //※一般関数内の静的変数オブジェクトをすべて収集しなければならない
862 extern SUBINFO *pSub_System_InitStaticLocalVariables;
863 pSub_System_InitStaticLocalVariables->bCompile=1;
864
865 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
866 extern SUBINFO *pSub_System_Call_Destructor_of_GlobalObject;
867 pSub_System_Call_Destructor_of_GlobalObject->bCompile=1;
868
869Repeat:
870 for(i2=0;i2<MAX_HASH;i2++){
871 psi=ppSubHash[i2];
872 while(psi){
873 CompileBufferInProcedure(psi);
874 psi=psi->pNextData;
875 }
876 }
877
878 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
879 for(i2=0;i2<MAX_HASH;i2++){
880 psi=ppSubHash[i2];
881 while(psi){
882 if(psi->bUse&&psi->bCompile==0&&psi->bVirtual==0){
883 goto Repeat;
884 }
885
886 psi=psi->pNextData;
887 }
888 }
889
890 //_System_InitStaticLocalVariablesは一番最後にコンパイル
891 pSub_System_InitStaticLocalVariables->bCompile=0;
892 CompileBufferInProcedure(pSub_System_InitStaticLocalVariables);
893
894 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
895 pSub_System_Call_Destructor_of_GlobalObject->bCompile=0;
896 CompileBufferInProcedure(pSub_System_Call_Destructor_of_GlobalObject);
897}
Note: See TracBrowser for help on using the repository browser.