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

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

静的リンクライブラリにより、複数のグローバル領域が存在することになったのでそれぞれを関数ベースに分けた

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