source: dev/trunk/abdev/BasicCompiler64/Compile_ProcOp.cpp@ 254

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