source: dev/BasicCompiler64/Compile_ProcOp.cpp@ 89

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

実行時型情報の生成に対応。
関数の戻り値の型に抽象クラスを指定できるようにした。

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