source: dev/BasicCompiler64/Compile_ProcOp.cpp@ 90

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

実行時型情報の生成にほぼ対応した。

File size: 25.3 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{
198 SetError();
199 }
200}
201void AutoGeneration(UserProc &userProc){
202 if( userProc.GetName() == "InitializeUserTypes"
203 && userProc.HasParentClass()
204 && (string)userProc.GetParentClass().name == "_System_TypeBase" ){
205
206 pobj_DBClass->Compile_System_InitializeUserTypes();
207 }
208 else{
209 SetError();
210 }
211}
212void CompileBufferInProcedure(UserProc *pUserProc){
213 extern char *basbuf;
214 extern HANDLE hHeap;
215 extern UserProc **ppSubHash;
216 extern BOOL bDebugCompile;
217 int i3,i4;
218 char temporary[VN_SIZE];
219
220 if( pUserProc->IsUsing() == false || pUserProc->IsCompiled() ) return;
221
222 if( pUserProc->localVars.size() ){
223 SetError();
224 return;
225 }
226
227 pUserProc->CompleteCompile();
228
229 extern BOOL bSystemProc;
230 if(memcmp(pUserProc->GetName().c_str(),"_System_",8)==0) bSystemProc=1;
231 else bSystemProc=0;
232
233 extern BOOL bDebugSupportProc;
234 if(memcmp(pUserProc->GetName().c_str(),"_DebugSys_",10)==0){
235 if(!bDebugCompile){
236 return;
237 }
238 bDebugSupportProc=1;
239 }
240 else bDebugSupportProc=0;
241
242 pUserProc->beginOpAddress=obp;
243
244 //コンパイル中の関数が属するクラス
245 pobj_CompilingClass=pUserProc->GetParentClassPtr();
246
247 //コンパイルスタートをクラス管理クラスに追加
248 pobj_DBClass->StartCompile( pUserProc );
249
250 //コンパイル中の関数
251 UserProc::CompileStartForUserProc( pUserProc );
252
253 if(pUserProc->IsSystem()){
254 ////////////////////
255 // 特殊関数
256 ////////////////////
257
258 extern int AllLocalVarSize;
259 AllLocalVarSize=0;
260
261 //スタックフレーム管理用オブジェクトを初期化
262 extern CStackFrame *pobj_sf;
263 pobj_sf=new CStackFrame();
264
265 SystemProc(*pUserProc);
266
267 //スタックフレーム管理用オブジェクトを破棄
268 delete pobj_sf;
269 pobj_sf=0;
270
271 pUserProc->endOpAddress=obp;
272 return;
273 }
274
275 cp=pUserProc->GetCodePos();
276 for(;;cp++){
277 if(IsCommandDelimitation(basbuf[cp])) break;
278 }
279 cp--;
280
281 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
282 extern DWORD *pExitSubSchedule;
283 extern int ExitSubScheduleNum;
284 pExitSubSchedule=(DWORD *)HeapAlloc(hHeap,0,1);
285 ExitSubScheduleNum=0;
286
287 //ラベル用のメモリを確保
288 extern LABEL *pLabelNames;
289 extern int MaxLabelNum;
290 pLabelNames=(LABEL *)HeapAlloc(hHeap,0,1);
291 MaxLabelNum=0;
292
293 //Gotoラベルスケジュール
294 extern GOTOLABELSCHEDULE *pGotoLabelSchedule;
295 extern int GotoLabelScheduleNum;
296 pGotoLabelSchedule=(GOTOLABELSCHEDULE *)HeapAlloc(hHeap,0,1);
297 GotoLabelScheduleNum=0;
298
299 //With情報のメモリを確保
300 extern WITHINFO WithInfo;
301 WithInfo.ppName=(char **)HeapAlloc(hHeap,0,1);
302 WithInfo.pWithCp=(int *)HeapAlloc(hHeap,0,1);
303 WithInfo.num=0;
304
305 //重複エラー情報管理のメモリを確保
306 extern char **SynonymErrorWords;
307 extern int SynonymErrorNum;
308 SynonymErrorNum=0;
309 SynonymErrorWords=(char **)HeapAlloc(hHeap,0,1);
310
311 //Continueアドレスを初期化
312 extern DWORD dwContinueAddress;
313 dwContinueAddress=-1;
314
315 //ローカル変数に関する情報
316 extern int AllLocalVarSize;
317 AllLocalVarSize=0;
318
319 //ローカル変数アドレススケジュール
320 extern DWORD *pLocalVarAddrSchedule;
321 extern int LocalVarAddrScheduleNum;
322 pLocalVarAddrSchedule=(DWORD *)HeapAlloc(hHeap,0,1);
323 LocalVarAddrScheduleNum=0;
324
325 //レキシカルスコープ情報を初期化
326 obj_LexScopes.Init(obp);
327
328
329 /////////////////////////////////////
330 // パラメータ用の変数データを考慮
331 /////////////////////////////////////
332
333 //パラメータ用の変数データを考慮
334 for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
335 Parameter &param = *pUserProc->RealParams()[i3];
336
337 Variable *pVar = new Variable( param.GetVarName(), param, false, param.IsRef() );
338
339 if( param.IsArray() ){
340 pVar->SetArray( param.GetSubScriptsPtr() );
341 }
342
343 int varSize;
344 if( param.IsRef() == false && param.IsStruct() ){
345 //構造体のByValパラメータ
346 pVar->ThisIsParameter();
347 varSize=PTR_SIZE;
348 }
349 else{
350 if( param.IsArray() == false ){
351 varSize = pVar->GetMemorySize();
352 }
353 else{
354 varSize=PTR_SIZE;
355 }
356 }
357 AllLocalVarSize+=varSize;
358 pVar->offset=AllLocalVarSize;
359
360 //レキシカルスコープ情報
361 pVar->ScopeLevel=obj_LexScopes.GetNowLevel();
362 pVar->ScopeStartAddress=obj_LexScopes.GetStartAddress();
363 pVar->bLiving=TRUE;
364
365 pUserProc->localVars.push_back( pVar );
366 }
367
368 //Thisポインタを示すローカルオフセット値をセット
369 extern int LocalVar_ThisPtrOffset;
370 LocalVar_ThisPtrOffset=AllLocalVarSize;
371
372 //スタックフレーム管理用クラスを初期化
373 extern CStackFrame *pobj_sf;
374 pobj_sf=new CStackFrame();
375
376
377 ///////////////////////
378 // ここからコード生成
379
380 for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
381 Parameter &param = *pUserProc->RealParams()[i3];
382 if(i3==3){
383 if(param.IsReal()&&param.IsRef() == false){
384 //movsd qword ptr[rsp+0x20],xmm3
385 op_movsd_MR(REG_XMM3,REG_RSP,0x20,MOD_BASE_DISP32);
386 }
387 else{
388 //mov qword ptr[rsp+0x20],r9
389 op_mov_MR(sizeof(_int64),REG_R9,REG_RSP,0x20,MOD_BASE_DISP32);
390 }
391 }
392 if(i3==2){
393 if(param.IsReal()&&param.IsRef() == false){
394 //movsd qword ptr[rsp+0x18],xmm2
395 op_movsd_MR(REG_XMM2,REG_RSP,0x18,MOD_BASE_DISP32);
396 }
397 else{
398 //mov qword ptr[rsp+0x18],r8
399 op_mov_MR(sizeof(_int64),REG_R8,REG_RSP,0x18,MOD_BASE_DISP32);
400 }
401 }
402 if(i3==1){
403 if(param.IsReal()&&param.IsRef() == false){
404 //movsd qword ptr[rsp+0x10],xmm1
405 op_movsd_MR(REG_XMM1,REG_RSP,0x10,MOD_BASE_DISP32);
406 }
407 else{
408 //mov qword ptr[rsp+0x10],rdx
409 op_mov_MR(sizeof(_int64),REG_RDX,REG_RSP,0x10,MOD_BASE_DISP32);
410 }
411 }
412 if(i3==0){
413 if(param.IsReal()&&param.IsRef() == false){
414 //movsd qword ptr[rsp+0x8],xmm0
415 op_movsd_MR(REG_XMM0,REG_RSP,0x8,MOD_BASE_DISP32);
416 }
417 else{
418 //mov qword ptr[rsp+0x8],rcx
419 op_mov_MR(sizeof(_int64),REG_RCX,REG_RSP,0x8,MOD_BASE_DISP32);
420 }
421 }
422 }
423
424 //ret用のアドレスを考慮
425 AllLocalVarSize+=sizeof(_int64);
426
427 //sub rsp,スタックフレームサイズ
428 int StackFrameSchedule;
429 op_sub_rsp(0xFFFFFFFF);
430 StackFrameSchedule=obp-sizeof(long);
431
432 //mov qword ptr[rsp+offset],reg ※スタックフレームを利用
433 pobj_sf->push(REG_RBX);
434 pobj_sf->push(REG_RSI);
435 pobj_sf->push(REG_RDI);
436 pobj_sf->push(REG_R12);
437 pobj_sf->push(REG_R13);
438 pobj_sf->push(REG_R14);
439 pobj_sf->push(REG_R15);
440
441 //ローカル変数のベース値
442 int BaseLocalVar;
443 BaseLocalVar=AllLocalVarSize;
444
445 if( !pUserProc->ReturnType().IsNull() ){
446 //戻り値が存在するとき
447
448 const char *temp = pUserProc->GetName().c_str();
449 if( temp[0]==1&&temp[1]==ESC_OPERATOR ){
450 temp = "_System_ReturnValue";
451 }
452
453 if( pUserProc->ReturnType().IsStruct() ){
454 //戻り値用の構造体(値型)はパラメータで引き渡される
455 }
456 else{
457 if( pUserProc->ReturnType().IsObject() ){
458 sprintf(temporary,"%s=Nothing%c%c%s",temp,1,ESC_AS, pUserProc->ReturnType().ToString().c_str() );
459 }
460 else{
461 //戻り値用の変数の定義
462 sprintf(temporary,"%s%c%c%s",temp,1,ESC_AS, pUserProc->ReturnType().ToString().c_str() );
463 }
464
465 OpcodeDim(temporary,0);
466 }
467 }
468
469 int RspOffsetSchedule=0;
470 int RspOffsetSchedule2;
471 if(bDebugCompile&&bDebugSupportProc==0){
472 //mov rdx, qword ptr[rsp+スタックフレームサイズ]
473 op_mov_RM(sizeof(_int64),REG_RDX,REG_RSP,0,MOD_BASE_DISP32);
474 RspOffsetSchedule=obp-sizeof(long);
475
476 //mov rcx,rsp
477 op_mov_RR(REG_RCX,REG_RSP);
478
479 //add rcx,スタックフレームサイズ+sizeof(_int64) ※ret用のサイズを考慮
480 op_add_RV(REG_RCX,0);
481 RspOffsetSchedule2=obp-sizeof(long);
482
483 //call _DebugSys_StartProc
484 extern UserProc *pSub_DebugSys_StartProc;
485 op_call(pSub_DebugSys_StartProc);
486 }
487
488 if(pobj_CompilingClass){
489 if( pUserProc->GetName() == pobj_CompilingClass->name ){
490 ////////////////////////////////////
491 // コンストラクタをコンパイルするとき
492 ////////////////////////////////////
493
494 //コンストラクタのコンパイル開始を通知
495 pobj_CompilingClass->NotifyStartConstructorCompile();
496
497 //基底クラスかどうかの識別
498 //(継承元がインターフェイスの場合も基底クラスと見なす)
499 BOOL bThisIsSuperClass;
500 if(pobj_CompilingClass->pobj_InheritsClass==0) bThisIsSuperClass=1;
501 else if( pobj_CompilingClass->pobj_InheritsClass->GetConstructorMethod() == NULL ){
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 Type dummyType;
537 CallProc( PROC_DEFAULT
538 , pobj_CompilingClass->pobj_InheritsClass->GetConstructorMethod()->pUserProc
539 , pobj_CompilingClass->pobj_InheritsClass->GetConstructorMethod()->pUserProc->GetName().c_str()
540 , temporary
541 , dummyType );
542 }
543 else{
544 //基底クラスのコンストラクタを暗黙的に呼び出す
545 Opcode_CallProc("",
546 pobj_CompilingClass->pobj_InheritsClass->GetConstructorMethod()->pUserProc,
547 0,
548 "",
549 0);
550 }
551 }
552
553 //新しいオブジェクト領域は0で初期化されているため、Nothingを明示的に代入する必要はない
554/*
555 //実体クラスを持つメンバのコンストラクタ(引数有りを除く)を呼び出す
556 for(i3=0;i3<pobj_CompilingClass->iMemberNum;i3++){
557 CMember *pMember = pobj_CompilingClass->ppobj_Member[i3];
558 if(pMember->IsObject()){
559 // オブジェクトメンバを発見したとき
560
561 sprintf(temporary, "This.%s=Nothing",
562 pMember->name );
563 OpcodeCalc( temporary );
564 }
565 }
566*/
567
568 //仮想関数テーブルを初期化
569 if(pobj_CompilingClass->vtbl_num&&
570 pobj_CompilingClass->IsAbstract()==false){
571 //関数テーブルに値をセット
572 int offset = (int)pobj_CompilingClass->GetVtblGlobalOffset();
573
574 //mov rax,offset
575 op_mov_RV(sizeof(_int64),REG_RAX,offset);
576 obp-=sizeof(long);
577 pobj_DataTableSchedule->add();
578 obp+=sizeof(long);
579
580 //Thisポインタをrcxにコピー
581 SetThisPtrToReg(REG_RCX);
582
583 //mov qword ptr[rcx],rax
584 op_mov_MR(sizeof(_int64),REG_RAX,REG_RCX,0,MOD_BASE);
585 }
586 }
587 else if( pUserProc->IsDestructor() ){
588 //デストラクタをコンパイルしたとき
589
590 //デストラクタのコンパイル開始を通知
591 pobj_CompilingClass->NotifyStartDestructorCompile();
592 }
593 }
594
595 //////////////////////////////////////////
596 //////////////////////////////////////////
597 ////// プロシージャ内をコンパイル ////////
598 if( pUserProc->IsAutoGeneration() ){
599 AutoGeneration( *pUserProc );
600 }
601 else{
602 if(pUserProc->IsMacro()){
603 CompileBuffer(ESC_ENDMACRO,0);
604 }
605 else{
606 if(pUserProc->IsSub()){
607 CompileBuffer(ESC_ENDSUB,0);
608 }
609 else if(pUserProc->IsFunction()){
610 CompileBuffer(ESC_ENDFUNCTION,0);
611 }
612 }
613 }
614 //////////////////////////////////////////
615 //////////////////////////////////////////
616
617 if( pobj_CompilingClass ){
618
619 if( pobj_CompilingClass->IsCompilingConstructor() ){
620 // コンストラクタをコンパイルしていたとき
621
622 // コンストラクタのコンパイルが完了したことを通知
623 pobj_CompilingClass->NotifyFinishConstructorCompile();
624 }
625 else if( pUserProc->IsDestructor() ){
626 ////////////////////////////////////
627 //デストラクタをコンパイルしたとき
628 ////////////////////////////////////
629
630 // デストラクタのコンパイルが完了したことを通知
631 pobj_CompilingClass->NotifyFinishDestructorCompile();
632
633 if(pobj_CompilingClass->pobj_InheritsClass){
634 /* サブクラスのデストラクタをコンパイルしているときは、
635 基底クラスのデストラクタを呼び出す */
636
637 CMethod *method = pobj_CompilingClass->pobj_InheritsClass->GetDestructorMethod();
638 if( method ){
639 Opcode_CallProc("",
640 method->pUserProc,
641 0,
642 "",
643 0);
644 }
645 }
646
647 //実体クラスを持つメンバのデストラクタ呼び出しはGCに任せる
648 /*
649 //※コンストラクタと逆順序で呼び出す
650 for(i3=pobj_CompilingClass->iMemberNum-1;i3>=0;i3--){
651 CMember *pMember = pobj_CompilingClass->ppobj_Member[i3];
652 int MemberTypeSize=
653 GetTypeSize(pMember->TypeInfo.type,
654 pMember->TypeInfo.u.lpIndex);
655
656 int MemberObjectNum=
657 JumpSubScripts(pMember->SubScripts);
658
659 int offset = pobj_CompilingClass->GetMemberOffset( pMember->name, NULL );
660
661 if(pMember->IsObject()){
662 CMethod *method = pMember->GetClass().GetDestructorMethod();
663 if( method ){
664 for(i4=MemberObjectNum-1;i4>=0;i4--){
665 //Thisポインタをrcxにコピー
666 SetThisPtrToReg(REG_RCX);
667
668 //add rcx,offset
669 op_add_RV(REG_RCX,offset+i4*MemberTypeSize);
670
671 //call destructor
672 op_call(method->pUserProc);
673 }
674 }
675 }
676 }*/
677 }
678 }
679
680 //ラベル用のメモリを解放
681 for(i3=0;i3<MaxLabelNum;i3++){
682 if(pLabelNames[i3].pName) HeapDefaultFree(pLabelNames[i3].pName);
683 }
684 HeapDefaultFree(pLabelNames);
685
686 //Goto未知ラベルスケジュールを解放
687 for(i3=0;i3<GotoLabelScheduleNum;i3++){
688 if(pGotoLabelSchedule[i3].pName){
689 SetError(6,pGotoLabelSchedule[i3].pName,pGotoLabelSchedule[i3].now_cp);
690 HeapDefaultFree(pGotoLabelSchedule[i3].pName);
691 }
692 else{
693 sprintf(temporary,"%d",pGotoLabelSchedule[i3].line);
694 SetError(6,temporary,pGotoLabelSchedule[i3].now_cp);
695 }
696 }
697 HeapDefaultFree(pGotoLabelSchedule);
698
699 //With情報のメモリを解放
700 for(i3=0;i3<WithInfo.num;i3++){
701 SetError(22,"With",WithInfo.pWithCp[i3]);
702 HeapDefaultFree(WithInfo.ppName[i3]);
703 }
704 HeapDefaultFree(WithInfo.ppName);
705 HeapDefaultFree(WithInfo.pWithCp);
706
707 //ローカルオブジェクト(レキシカルスコープレベル=0)の解放処理
708 obj_LexScopes.CallDestructorsOfScopeEnd();
709
710 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
711 for(i3=0;i3<ExitSubScheduleNum;i3++){
712 *((long *)(OpBuffer+pExitSubSchedule[i3]))=obp-(pExitSubSchedule[i3]+sizeof(long));
713 }
714 HeapDefaultFree(pExitSubSchedule);
715
716 if(bDebugCompile&&bDebugSupportProc==0){
717 //call _DebugSys_EndProc
718 extern UserProc *pSub_DebugSys_EndProc;
719 op_call(pSub_DebugSys_EndProc);
720 }
721
722 if( !pUserProc->ReturnType().IsNull() ){
723 //////////////////////////////////
724 // 戻り値をraxまたはxmm0に設定
725 //////////////////////////////////
726
727 RELATIVE_VAR RelativeVar;
728
729 const char *temp = pUserProc->GetName().c_str();
730 if( temp[0]==1 && temp[1]==ESC_OPERATOR ){
731 temp="_System_ReturnValue";
732 }
733 GetVarOffsetReadWrite(temp,&RelativeVar,Type());
734
735 i3=pUserProc->ReturnType().GetBasicType();
736
737 if(i3==DEF_OBJECT || i3==DEF_STRUCT){
738 SetVarPtrToReg(REG_RAX,&RelativeVar);
739 if( i3==DEF_OBJECT ){
740 //mov rax,qword ptr[rax]
741 op_mov_RM( sizeof(_int64), REG_RAX, REG_RAX, 0, MOD_BASE );
742 }
743 }
744 else if(i3==DEF_DOUBLE){
745 //64ビット実数型
746 SetXmmReg_DoubleVariable(&RelativeVar,REG_XMM0);
747 }
748 else if(i3==DEF_SINGLE){
749 //32ビット実数型
750 SetXmmReg_SingleVariable(&RelativeVar,REG_XMM0);
751 }
752 else if(IsWholeNumberType(i3)){
753 //整数型
754 SetReg_WholeVariable(i3,&RelativeVar,REG_RAX);
755 }
756 else SetError(300,NULL,cp);
757 }
758
759 //ローカル変数領域のサイズをスタックフレームに通知
760 int iLocalParmSize;
761 iLocalParmSize=AllLocalVarSize-BaseLocalVar;
762 pobj_sf->SetLocalParmSize(iLocalParmSize);
763
764 //ローカル変数アドレススケジュール
765 for(i3=0;i3<LocalVarAddrScheduleNum;i3++){
766 *((long *)(OpBuffer+pLocalVarAddrSchedule[i3]))+=AllLocalVarSize+pobj_sf->GetFrameSize();
767 }
768 HeapDefaultFree(pLocalVarAddrSchedule);
769 foreach( Variable *pVar, pUserProc->localVars ){
770 //後にデバッグで利用する
771 pVar->offset = AllLocalVarSize + pobj_sf->GetFrameSize() - pVar->offset;
772 }
773
774 //mov reg,qword ptr[rsp+offset] ※スタックフレームを利用
775 pobj_sf->pop(REG_R15);
776 pobj_sf->pop(REG_R14);
777 pobj_sf->pop(REG_R13);
778 pobj_sf->pop(REG_R12);
779 pobj_sf->pop(REG_RDI);
780 pobj_sf->pop(REG_RSI);
781 pobj_sf->pop(REG_RBX);
782
783 int iStackFrameSize;
784 iStackFrameSize=iLocalParmSize + pobj_sf->GetFrameSize();
785
786 //add rsp,スタックフレームサイズ
787 op_add_rsp(iStackFrameSize);
788
789 //ret 0
790 OpBuffer[obp++]=(char)0xC3;
791
792
793 //デバッグ用
794 if(RspOffsetSchedule){
795 *((long *)(OpBuffer+RspOffsetSchedule))=iStackFrameSize;
796 *((long *)(OpBuffer+RspOffsetSchedule2))=iStackFrameSize+sizeof(_int64);
797 }
798
799
800 //スタックフレームスケジュール(subコマンド)
801 *((long *)(OpBuffer+StackFrameSchedule))=iStackFrameSize;
802
803 //スタックフレームスケジュールを実行
804 pobj_sf->RunningSchedule();
805 delete pobj_sf;
806 pobj_sf=0;
807
808
809 pUserProc->endOpAddress=obp;
810
811
812 //重複エラー情報管理のメモリを解放
813 for(i3=0;i3<SynonymErrorNum;i3++) HeapDefaultFree(SynonymErrorWords[i3]);
814 HeapDefaultFree(SynonymErrorWords);
815}
816void CompileLocal(){
817 extern UserProc **ppSubHash;
818 int i2;
819 UserProc *pUserProc;
820
821 extern BOOL bDll;
822 if(bDll){
823 //DLLの場合はグローバル変数を初期化するための関数を一番初めにコンパイルする
824 pUserProc=GetSubHash("_System_InitDllGlobalVariables");
825 if(pUserProc){
826 CompileBufferInProcedure(pUserProc);
827 }
828 else SetError(300,NULL,cp);
829 }
830
831 //_System_InitStaticLocalVariablesは一番最後にコンパイル
832 //※一般関数内の静的変数オブジェクトをすべて収集しなければならない
833 extern UserProc *pSub_System_InitStaticLocalVariables;
834 pSub_System_InitStaticLocalVariables->CompleteCompile();
835
836 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
837 extern UserProc *pSub_System_Call_Destructor_of_GlobalObject;
838 pSub_System_Call_Destructor_of_GlobalObject->CompleteCompile();
839
840Repeat:
841 for(i2=0;i2<MAX_HASH;i2++){
842 pUserProc=ppSubHash[i2];
843 while(pUserProc){
844 CompileBufferInProcedure(pUserProc);
845 pUserProc=pUserProc->pNextData;
846 }
847 }
848
849 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
850 for(i2=0;i2<MAX_HASH;i2++){
851 pUserProc=ppSubHash[i2];
852 while(pUserProc){
853 if( pUserProc->IsUsing() && pUserProc->IsCompiled() == false ){
854 goto Repeat;
855 }
856
857 pUserProc=pUserProc->pNextData;
858 }
859 }
860
861 //_System_InitStaticLocalVariablesは一番最後にコンパイル
862 pSub_System_InitStaticLocalVariables->KillCompileStatus();
863 CompileBufferInProcedure(pSub_System_InitStaticLocalVariables);
864
865 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
866 pSub_System_Call_Destructor_of_GlobalObject->KillCompileStatus();
867 CompileBufferInProcedure(pSub_System_Call_Destructor_of_GlobalObject);
868}
Note: See TracBrowser for help on using the repository browser.