source: dev/BasicCompiler64/Compile_ProcOp.cpp@ 6

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