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

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