| 1 | #include "stdafx.h"
 | 
|---|
| 2 | 
 | 
|---|
| 3 | #include "Opcode.h"
 | 
|---|
| 4 | 
 | 
|---|
| 5 | void SystemProc( const UserProc &userProc ){
 | 
|---|
| 6 |     if( userProc.GetName() == "_System_GetEip" ){
 | 
|---|
| 7 |         //mov rax,qword ptr[rsp]
 | 
|---|
| 8 |         compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_RAX,REG_RSP,0,MOD_BASE);
 | 
|---|
| 9 | 
 | 
|---|
| 10 |         //ret
 | 
|---|
| 11 |         compiler.codeGenerator.op_ret();
 | 
|---|
| 12 |     }
 | 
|---|
| 13 |     else if( userProc.GetName() == "_System_InitDllGlobalVariables" ){
 | 
|---|
| 14 |         ////////////////////////////////////////
 | 
|---|
| 15 |         // DLLのグローバル領域をコンパイル
 | 
|---|
| 16 |         ////////////////////////////////////////
 | 
|---|
| 17 |         if(!compiler.IsDll()){
 | 
|---|
| 18 |             //ret
 | 
|---|
| 19 |             compiler.codeGenerator.op_ret();
 | 
|---|
| 20 | 
 | 
|---|
| 21 |             return;
 | 
|---|
| 22 |         }
 | 
|---|
| 23 | 
 | 
|---|
| 24 |         int BackCp;
 | 
|---|
| 25 |         BackCp=cp;
 | 
|---|
| 26 |         cp=-1;
 | 
|---|
| 27 | 
 | 
|---|
| 28 |         //sub rsp,スタックフレームサイズ
 | 
|---|
| 29 |         const PertialSchedule *pStackFramePertialSchedule = compiler.codeGenerator.op_sub_rsp( 0, true );
 | 
|---|
| 30 | 
 | 
|---|
| 31 |         if( compiler.IsDebug() )
 | 
|---|
| 32 |         {
 | 
|---|
| 33 |             //デバッグ用の変数を定義
 | 
|---|
| 34 |             DebugVariable();
 | 
|---|
| 35 |         }
 | 
|---|
| 36 | 
 | 
|---|
| 37 |         //GC用の変数を定義
 | 
|---|
| 38 |         InitGCVariables();
 | 
|---|
| 39 | 
 | 
|---|
| 40 |         //_System_StartupProgramの呼び出し
 | 
|---|
| 41 |         extern const UserProc *pSub_System_StartupProgram;
 | 
|---|
| 42 |         compiler.codeGenerator.op_call(pSub_System_StartupProgram);
 | 
|---|
| 43 | 
 | 
|---|
| 44 |         //クラスに属する静的メンバを定義
 | 
|---|
| 45 |         ActiveBasic::Compiler::ProcedureGenerator::Generate_InitStaticMember(
 | 
|---|
| 46 |             compiler.GetObjectModule().meta.GetClasses()
 | 
|---|
| 47 |         );
 | 
|---|
| 48 | 
 | 
|---|
| 49 |         GetGlobalDataForDll();
 | 
|---|
| 50 | 
 | 
|---|
| 51 |         //add rsp,スタックフレームサイズ
 | 
|---|
| 52 |         compiler.codeGenerator.op_add_RV(REG_RSP,pobj_sf->GetFrameSize(0));
 | 
|---|
| 53 | 
 | 
|---|
| 54 |         //スタックフレームスケジュール(subコマンドに渡す値)
 | 
|---|
| 55 |         compiler.codeGenerator.opfix( pStackFramePertialSchedule, pobj_sf->GetFrameSize(0) );
 | 
|---|
| 56 | 
 | 
|---|
| 57 |         cp=BackCp;
 | 
|---|
| 58 | 
 | 
|---|
| 59 |         //ret
 | 
|---|
| 60 |         compiler.codeGenerator.op_ret();
 | 
|---|
| 61 |     }
 | 
|---|
| 62 |     else if( userProc.GetName() == "_System_InitStaticLocalVariables" ){
 | 
|---|
| 63 |         //静的ローカルオブジェクトのコンストラクタ呼び出し
 | 
|---|
| 64 | 
 | 
|---|
| 65 |         //sub rsp,スタックフレームサイズ
 | 
|---|
| 66 |         const PertialSchedule *pStackFramePertialSchedule = compiler.codeGenerator.op_sub_rsp( 0, true );
 | 
|---|
| 67 | 
 | 
|---|
| 68 |         BOOST_FOREACH( Variable *pVar, compiler.GetObjectModule().meta.GetGlobalVars() ){
 | 
|---|
| 69 |             if(memicmp(pVar->GetName().c_str(),"Static%",7)==0){
 | 
|---|
| 70 |                 //コンストラクタ呼び出し
 | 
|---|
| 71 |                 if( pVar->GetType().IsObject() ){
 | 
|---|
| 72 | 
 | 
|---|
| 73 |                     //エラー用
 | 
|---|
| 74 |                     cp=pVar->source_code_address;
 | 
|---|
| 75 | 
 | 
|---|
| 76 |                     CallConstructor(
 | 
|---|
| 77 |                         pVar->GetName().c_str(),
 | 
|---|
| 78 |                         pVar->GetSubscripts(),
 | 
|---|
| 79 |                         pVar->GetType(),
 | 
|---|
| 80 |                         pVar->GetParamStrForConstructor().c_str());
 | 
|---|
| 81 |                 }
 | 
|---|
| 82 |             }
 | 
|---|
| 83 |         }
 | 
|---|
| 84 | 
 | 
|---|
| 85 |         //add rsp,スタックフレームサイズ
 | 
|---|
| 86 |         compiler.codeGenerator.op_add_RV(REG_RSP,pobj_sf->GetFrameSize(0));
 | 
|---|
| 87 | 
 | 
|---|
| 88 |         //スタックフレームスケジュール(subコマンドに渡す値)
 | 
|---|
| 89 |         compiler.codeGenerator.opfix( pStackFramePertialSchedule, pobj_sf->GetFrameSize(0) );
 | 
|---|
| 90 | 
 | 
|---|
| 91 |         //ret
 | 
|---|
| 92 |         compiler.codeGenerator.op_ret();
 | 
|---|
| 93 |     }
 | 
|---|
| 94 |     else if( userProc.GetName() == "_System_Call_Destructor_of_GlobalObject" )
 | 
|---|
| 95 |     {
 | 
|---|
| 96 |         //sub rsp,8(※RSPを16バイト境界にあわせるため)
 | 
|---|
| 97 |         compiler.codeGenerator.op_sub_rsp(0x8);
 | 
|---|
| 98 |         compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
 | 
|---|
| 99 | 
 | 
|---|
| 100 |         //add rsp,8
 | 
|---|
| 101 |         compiler.codeGenerator.op_add_RV(REG_RSP,0x8);
 | 
|---|
| 102 | 
 | 
|---|
| 103 |         //ret
 | 
|---|
| 104 |         compiler.codeGenerator.op_ret();
 | 
|---|
| 105 |     }
 | 
|---|
| 106 |     else{
 | 
|---|
| 107 |         compiler.errorMessenger.OutputFatalError();
 | 
|---|
| 108 |     }
 | 
|---|
| 109 | }
 | 
|---|
| 110 | void AutoGeneration(const UserProc &userProc){
 | 
|---|
| 111 |     if( userProc.GetName() == "InitializeUserTypes"
 | 
|---|
| 112 |         && userProc.HasParentClass()
 | 
|---|
| 113 |         && userProc.GetParentClass().GetName() == "_System_TypeBase" )
 | 
|---|
| 114 |     {
 | 
|---|
| 115 |         ActiveBasic::Compiler::ProcedureGenerator::Generate_System_InitializeUserTypes(
 | 
|---|
| 116 |             compiler.GetObjectModule().meta.GetClasses()
 | 
|---|
| 117 |         );
 | 
|---|
| 118 |     }
 | 
|---|
| 119 |     else if( userProc.GetName() == "InitializeUserTypesForBaseType"
 | 
|---|
| 120 |         && userProc.HasParentClass()
 | 
|---|
| 121 |         && userProc.GetParentClass().GetName() == "_System_TypeBase" )
 | 
|---|
| 122 |     {
 | 
|---|
| 123 |         ActiveBasic::Compiler::ProcedureGenerator::Generate_System_InitializeUserTypesForBaseType(
 | 
|---|
| 124 |             compiler.GetObjectModule().meta.GetClasses()
 | 
|---|
| 125 |         );
 | 
|---|
| 126 |     }
 | 
|---|
| 127 |     else if( userProc.GetName() == "RegisterGlobalRoots"
 | 
|---|
| 128 |         && userProc.HasParentClass()
 | 
|---|
| 129 |         && userProc.GetParentClass().GetName() == "_System_CGarbageCollection" )
 | 
|---|
| 130 |     {
 | 
|---|
| 131 |         Compile_AddGlobalRootsForGc();
 | 
|---|
| 132 |     }
 | 
|---|
| 133 |     else if( userProc.GetName() == compiler.globalAreaProcName )
 | 
|---|
| 134 |     {
 | 
|---|
| 135 |         ////////////////////////////////////////
 | 
|---|
| 136 |         // グローバル領域をコンパイル
 | 
|---|
| 137 |         ////////////////////////////////////////
 | 
|---|
| 138 | 
 | 
|---|
| 139 |         UserProc::pGlobalProc = &userProc;
 | 
|---|
| 140 | 
 | 
|---|
| 141 |         int BackCp = cp;
 | 
|---|
| 142 |         cp=-1;
 | 
|---|
| 143 | 
 | 
|---|
| 144 |         //クラスに属する静的メンバを定義
 | 
|---|
| 145 |         ActiveBasic::Compiler::ProcedureGenerator::Generate_InitStaticMember(
 | 
|---|
| 146 |             compiler.GetObjectModule().meta.GetClasses()
 | 
|---|
| 147 |         );
 | 
|---|
| 148 | 
 | 
|---|
| 149 |         //グローバル実行領域をコンパイル開始
 | 
|---|
| 150 |         CompileBuffer(0,0);
 | 
|---|
| 151 | 
 | 
|---|
| 152 |         //Goto未知ラベルスケジュールが存在したらエラーにする
 | 
|---|
| 153 |         BOOST_FOREACH( const GotoLabelSchedule *pGotoLabelSchedule, compiler.codeGenerator.gotoLabelSchedules )
 | 
|---|
| 154 |         {
 | 
|---|
| 155 |             if(pGotoLabelSchedule->GetName().size()>0){
 | 
|---|
| 156 |                 compiler.errorMessenger.Output(6,pGotoLabelSchedule->GetName(),pGotoLabelSchedule->GetSourceCodePos());
 | 
|---|
| 157 |             }
 | 
|---|
| 158 |             else{
 | 
|---|
| 159 |                 char temporary[255];
 | 
|---|
| 160 |                 sprintf(temporary,"%d",pGotoLabelSchedule->GetLineNum());
 | 
|---|
| 161 |                 compiler.errorMessenger.Output(6,temporary,pGotoLabelSchedule->GetSourceCodePos());
 | 
|---|
| 162 |             }
 | 
|---|
| 163 |         }
 | 
|---|
| 164 | 
 | 
|---|
| 165 |         cp=BackCp;
 | 
|---|
| 166 |     }
 | 
|---|
| 167 |     else if( userProc.HasParentClass()
 | 
|---|
| 168 |         && userProc.IsCastOperator()
 | 
|---|
| 169 |         && userProc.ReturnType().IsInterface() )
 | 
|---|
| 170 |     {
 | 
|---|
| 171 |         // インターフェイス型にキャストするためのメソッド
 | 
|---|
| 172 | 
 | 
|---|
| 173 |         int vtblMasterListIndex = userProc.GetParentClass().GetVtblMasterListIndex( &userProc.ReturnType().GetClass() );
 | 
|---|
| 174 | 
 | 
|---|
| 175 |         char temporary[1024];
 | 
|---|
| 176 |         sprintf( temporary,
 | 
|---|
| 177 |             "Return New %s(ObjPtr( This ),Get_LONG_PTR( (Get_LONG_PTR( ObjPtr(This)+SizeOf(VoidPtr) ) + SizeOf(LONG_PTR)*%d) As VoidPtr ) As VoidPtr )",
 | 
|---|
| 178 |             userProc.ReturnType().GetClass().GetName().c_str(),
 | 
|---|
| 179 |             vtblMasterListIndex
 | 
|---|
| 180 |         );
 | 
|---|
| 181 |         MakeMiddleCode( temporary );
 | 
|---|
| 182 | 
 | 
|---|
| 183 |         ChangeOpcode( temporary );
 | 
|---|
| 184 |     }
 | 
|---|
| 185 |     else{
 | 
|---|
| 186 |         compiler.errorMessenger.OutputFatalError();
 | 
|---|
| 187 |     }
 | 
|---|
| 188 | }
 | 
|---|
| 189 | void _compile_proc(const UserProc *pUserProc){
 | 
|---|
| 190 |     extern char *basbuf;
 | 
|---|
| 191 |     extern HANDLE hHeap;
 | 
|---|
| 192 |     int i3,i4;
 | 
|---|
| 193 |     char temporary[VN_SIZE];
 | 
|---|
| 194 | 
 | 
|---|
| 195 |     if( pUserProc->GetLocalVars().size() ){
 | 
|---|
| 196 |         compiler.errorMessenger.OutputFatalError();
 | 
|---|
| 197 |         return;
 | 
|---|
| 198 |     }
 | 
|---|
| 199 | 
 | 
|---|
| 200 |     trace_for_sourcecodestep( "★★★ " << FormatEscapeSequenceStringToDefaultString( pUserProc->GetFullName() ) << "のコンパイルを開始" );
 | 
|---|
| 201 | 
 | 
|---|
| 202 |     pUserProc->CompleteCompile();
 | 
|---|
| 203 | 
 | 
|---|
| 204 |     extern BOOL bDebugSupportProc;
 | 
|---|
| 205 |     if(memcmp(pUserProc->GetName().c_str(),"_DebugSys_",10)==0){
 | 
|---|
| 206 |         if( !compiler.IsDebug() )
 | 
|---|
| 207 |         {
 | 
|---|
| 208 |             return;
 | 
|---|
| 209 |         }
 | 
|---|
| 210 |         bDebugSupportProc=1;
 | 
|---|
| 211 |     }
 | 
|---|
| 212 |     else bDebugSupportProc=0;
 | 
|---|
| 213 | 
 | 
|---|
| 214 |     compiler.StartProcedureCompile( pUserProc );
 | 
|---|
| 215 | 
 | 
|---|
| 216 |     if(pUserProc->IsAutoGenerationSystem()){
 | 
|---|
| 217 |         ////////////////////
 | 
|---|
| 218 |         // 特殊関数
 | 
|---|
| 219 |         ////////////////////
 | 
|---|
| 220 | 
 | 
|---|
| 221 |         extern int AllLocalVarSize;
 | 
|---|
| 222 |         AllLocalVarSize=0;
 | 
|---|
| 223 | 
 | 
|---|
| 224 |         //スタックフレーム管理用オブジェクトを初期化
 | 
|---|
| 225 |         extern StackFrame *pobj_sf;
 | 
|---|
| 226 |         pobj_sf=new StackFrame();
 | 
|---|
| 227 | 
 | 
|---|
| 228 |         SystemProc(*pUserProc);
 | 
|---|
| 229 | 
 | 
|---|
| 230 |         //スタックフレーム管理用オブジェクトを破棄
 | 
|---|
| 231 |         delete pobj_sf;
 | 
|---|
| 232 |         pobj_sf=0;
 | 
|---|
| 233 | 
 | 
|---|
| 234 |         return;
 | 
|---|
| 235 |     }
 | 
|---|
| 236 | 
 | 
|---|
| 237 |     if( !pUserProc->IsAutoGeneration() )
 | 
|---|
| 238 |     {
 | 
|---|
| 239 |         // 対象のソースコードを含むオブジェクトモジュールのインデックスを指定する
 | 
|---|
| 240 |         // ※テンプレート展開がされている場合、対象のソースコードが現在のオブジェクトモジュールではない場合がある
 | 
|---|
| 241 |         compiler.SetCurrentRelationalObjectModuleIndexForSource( pUserProc->GetSourceCodePosition().GetRelationalObjectModuleIndex() );
 | 
|---|
| 242 |         basbuf = const_cast<char *>(compiler.GetCurrentSource().GetBuffer());
 | 
|---|
| 243 | 
 | 
|---|
| 244 |         cp=pUserProc->GetSourceCodePosition().GetPos();
 | 
|---|
| 245 |         for(;;cp++){
 | 
|---|
| 246 |             if(IsCommandDelimitation(basbuf[cp])) break;
 | 
|---|
| 247 |         }
 | 
|---|
| 248 |         cp--;
 | 
|---|
| 249 |     }
 | 
|---|
| 250 | 
 | 
|---|
| 251 |     //プロシージャ抜け出しスケジュール(Exit Sub/Function)
 | 
|---|
| 252 |     compiler.codeGenerator.exitSubCodePositions.clear();
 | 
|---|
| 253 | 
 | 
|---|
| 254 |     //ラベル用のメモリを確保
 | 
|---|
| 255 |     compiler.codeGenerator.gotoLabels.clear();
 | 
|---|
| 256 | 
 | 
|---|
| 257 |     //Gotoラベルスケジュール
 | 
|---|
| 258 |     compiler.codeGenerator.gotoLabelSchedules.clear();
 | 
|---|
| 259 | 
 | 
|---|
| 260 |     //With情報のメモリを確保
 | 
|---|
| 261 |     extern WITHINFO WithInfo;
 | 
|---|
| 262 |     WithInfo.ppName=(char **)HeapAlloc(hHeap,0,1);
 | 
|---|
| 263 |     WithInfo.pWithCp=(int *)HeapAlloc(hHeap,0,1);
 | 
|---|
| 264 |     WithInfo.num=0;
 | 
|---|
| 265 | 
 | 
|---|
| 266 |     //Continueアドレスを初期化
 | 
|---|
| 267 |     compiler.codeGenerator.ClearContinueArea();
 | 
|---|
| 268 | 
 | 
|---|
| 269 |     //ローカル変数に関する情報
 | 
|---|
| 270 |     extern int AllLocalVarSize;
 | 
|---|
| 271 |     AllLocalVarSize=0;
 | 
|---|
| 272 | 
 | 
|---|
| 273 |     //レキシカルスコープ情報を初期化
 | 
|---|
| 274 |     compiler.codeGenerator.lexicalScopes.Init( compiler.codeGenerator.GetNativeCodeSize() );
 | 
|---|
| 275 | 
 | 
|---|
| 276 | 
 | 
|---|
| 277 |     /////////////////////////////////////
 | 
|---|
| 278 |     // パラメータ用の変数データを考慮
 | 
|---|
| 279 |     /////////////////////////////////////
 | 
|---|
| 280 | 
 | 
|---|
| 281 |     //パラメータ用の変数データを考慮
 | 
|---|
| 282 |     for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
 | 
|---|
| 283 |         Parameter ¶m = *pUserProc->RealParams()[i3];
 | 
|---|
| 284 | 
 | 
|---|
| 285 |         Variable *pVar = new Variable(
 | 
|---|
| 286 |             ActiveBasic::Compiler::LexicalAnalyzer::FullNameToSymbol( param.GetVarName().c_str() ),
 | 
|---|
| 287 |             param,
 | 
|---|
| 288 |             false,
 | 
|---|
| 289 |             param.IsRef(),
 | 
|---|
| 290 |             "",
 | 
|---|
| 291 |             false
 | 
|---|
| 292 |         );
 | 
|---|
| 293 | 
 | 
|---|
| 294 |         if( param.IsArray() ){
 | 
|---|
| 295 |             pVar->SetArray( param.GetSubscripts() );
 | 
|---|
| 296 |         }
 | 
|---|
| 297 | 
 | 
|---|
| 298 |         int varSize;
 | 
|---|
| 299 |         if( param.IsRef() == false && param.IsStruct() ){
 | 
|---|
| 300 |             //構造体のByValパラメータ
 | 
|---|
| 301 |             pVar->ThisIsParameter();
 | 
|---|
| 302 |             varSize=PTR_SIZE;
 | 
|---|
| 303 |         }
 | 
|---|
| 304 |         else{
 | 
|---|
| 305 |             if( param.IsArray() == false ){
 | 
|---|
| 306 |                 varSize = pVar->GetMemorySize();
 | 
|---|
| 307 |             }
 | 
|---|
| 308 |             else{
 | 
|---|
| 309 |                 varSize=PTR_SIZE;
 | 
|---|
| 310 |             }
 | 
|---|
| 311 |         }
 | 
|---|
| 312 |         AllLocalVarSize+=varSize;
 | 
|---|
| 313 |         pVar->SetOffsetAddress( AllLocalVarSize );
 | 
|---|
| 314 | 
 | 
|---|
| 315 |         //レキシカルスコープ情報
 | 
|---|
| 316 |         pVar->SetScopeLevel( compiler.codeGenerator.lexicalScopes.GetNowLevel() );
 | 
|---|
| 317 |         pVar->SetScopeStartAddress( compiler.codeGenerator.lexicalScopes.GetStartAddress() );
 | 
|---|
| 318 |         pVar->isLiving = true;
 | 
|---|
| 319 | 
 | 
|---|
| 320 |         pUserProc->GetLocalVars().push_back( pVar );
 | 
|---|
| 321 |     }
 | 
|---|
| 322 | 
 | 
|---|
| 323 |     //Thisポインタを示すローカルオフセット値をセット
 | 
|---|
| 324 |     extern int LocalVar_ThisPtrOffset;
 | 
|---|
| 325 |     LocalVar_ThisPtrOffset=AllLocalVarSize;
 | 
|---|
| 326 | 
 | 
|---|
| 327 |     //スタックフレーム管理用クラスを初期化
 | 
|---|
| 328 |     extern StackFrame *pobj_sf;
 | 
|---|
| 329 |     pobj_sf=new StackFrame();
 | 
|---|
| 330 | 
 | 
|---|
| 331 | 
 | 
|---|
| 332 |     ///////////////////////
 | 
|---|
| 333 |     // ここからコード生成
 | 
|---|
| 334 | 
 | 
|---|
| 335 |     for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
 | 
|---|
| 336 |         Parameter ¶m = *pUserProc->RealParams()[i3];
 | 
|---|
| 337 |         if(i3==3){
 | 
|---|
| 338 |             if(param.IsReal()&¶m.IsRef() == false){
 | 
|---|
| 339 |                 //movsd qword ptr[rsp+0x20],xmm3
 | 
|---|
| 340 |                 compiler.codeGenerator.op_movsd_MR(REG_XMM3,REG_RSP,0x20,MOD_BASE_DISP32);
 | 
|---|
| 341 |             }
 | 
|---|
| 342 |             else{
 | 
|---|
| 343 |                 //mov qword ptr[rsp+0x20],r9
 | 
|---|
| 344 |                 compiler.codeGenerator.op_mov_MR(sizeof(_int64),REG_R9,REG_RSP,0x20,MOD_BASE_DISP32);
 | 
|---|
| 345 |             }
 | 
|---|
| 346 |         }
 | 
|---|
| 347 |         if(i3==2){
 | 
|---|
| 348 |             if(param.IsReal()&¶m.IsRef() == false){
 | 
|---|
| 349 |                 //movsd qword ptr[rsp+0x18],xmm2
 | 
|---|
| 350 |                 compiler.codeGenerator.op_movsd_MR(REG_XMM2,REG_RSP,0x18,MOD_BASE_DISP32);
 | 
|---|
| 351 |             }
 | 
|---|
| 352 |             else{
 | 
|---|
| 353 |                 //mov qword ptr[rsp+0x18],r8
 | 
|---|
| 354 |                 compiler.codeGenerator.op_mov_MR(sizeof(_int64),REG_R8,REG_RSP,0x18,MOD_BASE_DISP32);
 | 
|---|
| 355 |             }
 | 
|---|
| 356 |         }
 | 
|---|
| 357 |         if(i3==1){
 | 
|---|
| 358 |             if(param.IsReal()&¶m.IsRef() == false){
 | 
|---|
| 359 |                 //movsd qword ptr[rsp+0x10],xmm1
 | 
|---|
| 360 |                 compiler.codeGenerator.op_movsd_MR(REG_XMM1,REG_RSP,0x10,MOD_BASE_DISP32);
 | 
|---|
| 361 |             }
 | 
|---|
| 362 |             else{
 | 
|---|
| 363 |                 //mov qword ptr[rsp+0x10],rdx
 | 
|---|
| 364 |                 compiler.codeGenerator.op_mov_MR(sizeof(_int64),REG_RDX,REG_RSP,0x10,MOD_BASE_DISP32);
 | 
|---|
| 365 |             }
 | 
|---|
| 366 |         }
 | 
|---|
| 367 |         if(i3==0){
 | 
|---|
| 368 |             if(param.IsReal()&¶m.IsRef() == false){
 | 
|---|
| 369 |                 //movsd qword ptr[rsp+0x8],xmm0
 | 
|---|
| 370 |                 compiler.codeGenerator.op_movsd_MR(REG_XMM0,REG_RSP,0x8,MOD_BASE_DISP32);
 | 
|---|
| 371 |             }
 | 
|---|
| 372 |             else{
 | 
|---|
| 373 |                 //mov qword ptr[rsp+0x8],rcx
 | 
|---|
| 374 |                 compiler.codeGenerator.op_mov_MR(sizeof(_int64),REG_RCX,REG_RSP,0x8,MOD_BASE_DISP32);
 | 
|---|
| 375 |             }
 | 
|---|
| 376 |         }
 | 
|---|
| 377 |     }
 | 
|---|
| 378 | 
 | 
|---|
| 379 |     //ret用のアドレスを考慮
 | 
|---|
| 380 |     AllLocalVarSize+=sizeof(_int64);
 | 
|---|
| 381 | 
 | 
|---|
| 382 |     //sub rsp,スタックフレームサイズ
 | 
|---|
| 383 |     const PertialSchedule *pStackFramePertialSchedule = compiler.codeGenerator.op_sub_rsp( 0, true );
 | 
|---|
| 384 | 
 | 
|---|
| 385 |     //mov qword ptr[rsp+offset],reg     ※スタックフレームを利用
 | 
|---|
| 386 |     pobj_sf->push(REG_RBX);
 | 
|---|
| 387 |     pobj_sf->push(REG_RSI);
 | 
|---|
| 388 |     pobj_sf->push(REG_RDI);
 | 
|---|
| 389 |     pobj_sf->push(REG_R12);
 | 
|---|
| 390 |     pobj_sf->push(REG_R13);
 | 
|---|
| 391 |     pobj_sf->push(REG_R14);
 | 
|---|
| 392 |     pobj_sf->push(REG_R15);
 | 
|---|
| 393 | 
 | 
|---|
| 394 |     //ローカル変数のベース値
 | 
|---|
| 395 |     int BaseLocalVar;
 | 
|---|
| 396 |     BaseLocalVar=AllLocalVarSize;
 | 
|---|
| 397 | 
 | 
|---|
| 398 |     if( !pUserProc->ReturnType().IsNull() ){
 | 
|---|
| 399 |         //戻り値が存在するとき
 | 
|---|
| 400 | 
 | 
|---|
| 401 |         const char *temp = pUserProc->GetName().c_str();
 | 
|---|
| 402 |         if( temp[0]==1&&temp[1]==ESC_OPERATOR ){
 | 
|---|
| 403 |             temp = "_System_ReturnValue";
 | 
|---|
| 404 |         }
 | 
|---|
| 405 | 
 | 
|---|
| 406 |         if( pUserProc->ReturnType().IsStruct() ){
 | 
|---|
| 407 |             //戻り値用の構造体(値型)はパラメータで引き渡される
 | 
|---|
| 408 |         }
 | 
|---|
| 409 |         else{
 | 
|---|
| 410 |             if( pUserProc->ReturnType().IsObject() ){
 | 
|---|
| 411 |                 sprintf(temporary,"%s=Nothing%c%c%s",temp,1,ESC_AS, compiler.TypeToString( pUserProc->ReturnType() ).c_str() );
 | 
|---|
| 412 |             }
 | 
|---|
| 413 |             else{
 | 
|---|
| 414 |                 //戻り値用の変数の定義
 | 
|---|
| 415 |                 sprintf(temporary,"%s%c%c%s",temp,1,ESC_AS, compiler.TypeToString( pUserProc->ReturnType() ).c_str() );
 | 
|---|
| 416 |             }
 | 
|---|
| 417 | 
 | 
|---|
| 418 |             OpcodeDim(temporary,0);
 | 
|---|
| 419 |         }
 | 
|---|
| 420 |     }
 | 
|---|
| 421 | 
 | 
|---|
| 422 |     const PertialSchedule *pRspOffsetPertialSchedule1 = NULL;
 | 
|---|
| 423 |     const PertialSchedule *pRspOffsetPertialSchedule2 = NULL;
 | 
|---|
| 424 |     if( compiler.IsDebug() && bDebugSupportProc == 0 )
 | 
|---|
| 425 |     {
 | 
|---|
| 426 |         //mov rdx, qword ptr[rsp+スタックフレームサイズ]
 | 
|---|
| 427 |         pRspOffsetPertialSchedule1 = compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_RDX,REG_RSP,0,MOD_BASE_DISP32, Schedule::None, true );
 | 
|---|
| 428 | 
 | 
|---|
| 429 |         //mov rcx,rsp
 | 
|---|
| 430 |         compiler.codeGenerator.op_mov_RR(REG_RCX,REG_RSP);
 | 
|---|
| 431 | 
 | 
|---|
| 432 |         //add rcx,スタックフレームサイズ+sizeof(_int64) ※ret用のサイズを考慮
 | 
|---|
| 433 |         pRspOffsetPertialSchedule2 = compiler.codeGenerator.op_add_RV(REG_RCX,0, Schedule::None, true );
 | 
|---|
| 434 | 
 | 
|---|
| 435 |         //call _DebugSys_StartProc
 | 
|---|
| 436 |         extern const UserProc *pSub_DebugSys_StartProc;
 | 
|---|
| 437 |         compiler.codeGenerator.op_call(pSub_DebugSys_StartProc);
 | 
|---|
| 438 |     }
 | 
|---|
| 439 | 
 | 
|---|
| 440 |     if( compiler.IsCompilingClass() ){
 | 
|---|
| 441 |         if( pUserProc->GetName() == compiler.GetCompilingClass().GetName() ){
 | 
|---|
| 442 |             ////////////////////////////////////
 | 
|---|
| 443 |             // コンストラクタをコンパイルするとき
 | 
|---|
| 444 |             ////////////////////////////////////
 | 
|---|
| 445 | 
 | 
|---|
| 446 |             //コンストラクタのコンパイル開始を通知
 | 
|---|
| 447 |             compiler.GetCompilingClass().NotifyStartConstructorCompile();
 | 
|---|
| 448 | 
 | 
|---|
| 449 |             //基底クラスかどうかの識別
 | 
|---|
| 450 |             //(継承元がインターフェイスの場合も基底クラスと見なす)
 | 
|---|
| 451 |             BOOL bThisIsSuperClass;
 | 
|---|
| 452 |             if( !compiler.GetCompilingClass().HasSuperClass() ) bThisIsSuperClass=1;
 | 
|---|
| 453 |             else if( compiler.GetCompilingClass().GetSuperClass().GetConstructorMethod() == NULL ){
 | 
|---|
| 454 |                 //インターフェイスを継承したときはコンストラクタを持たない
 | 
|---|
| 455 |                 bThisIsSuperClass=1;
 | 
|---|
| 456 |             }
 | 
|---|
| 457 |             else bThisIsSuperClass=0;
 | 
|---|
| 458 | 
 | 
|---|
| 459 |             if(!bThisIsSuperClass){
 | 
|---|
| 460 |                 /* サブクラスコンストラクタをコンパイルしているときは、
 | 
|---|
| 461 |                     基底クラスのコンストラクタを呼び出す */
 | 
|---|
| 462 | 
 | 
|---|
| 463 |                 i3=cp+1;
 | 
|---|
| 464 |                 while(IsCommandDelimitation(basbuf[i3])) i3++;
 | 
|---|
| 465 |                 for(i4=0;;i3++,i4++){
 | 
|---|
| 466 |                     if(!IsVariableChar(basbuf[i3])){
 | 
|---|
| 467 |                         temporary[i4]=0;
 | 
|---|
| 468 |                         break;
 | 
|---|
| 469 |                     }
 | 
|---|
| 470 |                     temporary[i4]=basbuf[i3];
 | 
|---|
| 471 |                 }
 | 
|---|
| 472 |                 if( compiler.GetCompilingClass().GetSuperClass().GetName() == temporary ){
 | 
|---|
| 473 |                     //基底クラスのコンストラクタを呼び出す
 | 
|---|
| 474 |                     cp=i3;
 | 
|---|
| 475 |                     for(i4=0;;cp++,i4++){
 | 
|---|
| 476 |                         if(IsCommandDelimitation(basbuf[cp])){
 | 
|---|
| 477 |                             temporary[i4]=0;
 | 
|---|
| 478 |                             break;
 | 
|---|
| 479 |                         }
 | 
|---|
| 480 |                         temporary[i4]=basbuf[cp];
 | 
|---|
| 481 |                     }
 | 
|---|
| 482 |                     if(!(temporary[0]=='('&&temporary[lstrlen(temporary)-1]==')')){
 | 
|---|
| 483 |                         compiler.errorMessenger.Output(1,NULL,cp);
 | 
|---|
| 484 |                     }
 | 
|---|
| 485 |                     RemoveStringPare(temporary);
 | 
|---|
| 486 | 
 | 
|---|
| 487 |                     Type dummyType;
 | 
|---|
| 488 |                     CallProc( PROC_DEFAULT
 | 
|---|
| 489 |                         , &compiler.GetCompilingClass().GetSuperClass().GetConstructorMethod()->GetUserProc()
 | 
|---|
| 490 |                         , compiler.GetCompilingClass().GetSuperClass().GetConstructorMethod()->GetUserProc().GetName().c_str()
 | 
|---|
| 491 |                         , temporary
 | 
|---|
| 492 |                         , Type()        // baseTypeはなし
 | 
|---|
| 493 |                         , dummyType
 | 
|---|
| 494 |                     );
 | 
|---|
| 495 |                 }
 | 
|---|
| 496 |                 else{
 | 
|---|
| 497 |                     //基底クラスのコンストラクタを暗黙的に呼び出す
 | 
|---|
| 498 |                     Opcode_CallProc("",
 | 
|---|
| 499 |                         &compiler.GetCompilingClass().GetSuperClass().GetConstructorMethod()->GetUserProc(),
 | 
|---|
| 500 |                         0,
 | 
|---|
| 501 |                         "");
 | 
|---|
| 502 |                 }
 | 
|---|
| 503 |             }
 | 
|---|
| 504 |         }
 | 
|---|
| 505 |         else if( pUserProc->IsDestructor() ){
 | 
|---|
| 506 |             //デストラクタをコンパイルしたとき
 | 
|---|
| 507 | 
 | 
|---|
| 508 |             //デストラクタのコンパイル開始を通知
 | 
|---|
| 509 |             compiler.GetCompilingClass().NotifyStartDestructorCompile();
 | 
|---|
| 510 |         }
 | 
|---|
| 511 |     }
 | 
|---|
| 512 | 
 | 
|---|
| 513 |     //////////////////////////////////////////
 | 
|---|
| 514 |     //////////////////////////////////////////
 | 
|---|
| 515 |     ////// プロシージャ内をコンパイル ////////
 | 
|---|
| 516 |     if( pUserProc->IsAutoGeneration() ){
 | 
|---|
| 517 |         AutoGeneration( *pUserProc );
 | 
|---|
| 518 |     }
 | 
|---|
| 519 |     else{
 | 
|---|
| 520 |         if(pUserProc->IsMacro()){
 | 
|---|
| 521 |             CompileBuffer(ESC_ENDMACRO,0);
 | 
|---|
| 522 |         }
 | 
|---|
| 523 |         else{
 | 
|---|
| 524 |             if(pUserProc->IsSub()){
 | 
|---|
| 525 |                 CompileBuffer(ESC_ENDSUB,0);
 | 
|---|
| 526 |             }
 | 
|---|
| 527 |             else if(pUserProc->IsFunction()){
 | 
|---|
| 528 |                 CompileBuffer(ESC_ENDFUNCTION,0);
 | 
|---|
| 529 |             }
 | 
|---|
| 530 |         }
 | 
|---|
| 531 |     }
 | 
|---|
| 532 |     //////////////////////////////////////////
 | 
|---|
| 533 |     //////////////////////////////////////////
 | 
|---|
| 534 | 
 | 
|---|
| 535 |     if( compiler.IsCompilingClass() ){
 | 
|---|
| 536 | 
 | 
|---|
| 537 |         if( compiler.GetCompilingClass().IsCompilingConstructor() ){
 | 
|---|
| 538 |             // コンストラクタをコンパイルしていたとき
 | 
|---|
| 539 | 
 | 
|---|
| 540 |             // コンストラクタのコンパイルが完了したことを通知
 | 
|---|
| 541 |             compiler.GetCompilingClass().NotifyFinishConstructorCompile();
 | 
|---|
| 542 |         }
 | 
|---|
| 543 |         else if( pUserProc->IsDestructor() ){
 | 
|---|
| 544 |             ////////////////////////////////////
 | 
|---|
| 545 |             //デストラクタをコンパイルしたとき
 | 
|---|
| 546 |             ////////////////////////////////////
 | 
|---|
| 547 | 
 | 
|---|
| 548 |             // デストラクタのコンパイルが完了したことを通知
 | 
|---|
| 549 |             compiler.GetCompilingClass().NotifyFinishDestructorCompile();
 | 
|---|
| 550 | 
 | 
|---|
| 551 |             if( compiler.GetCompilingClass().HasSuperClass() ){
 | 
|---|
| 552 |                 /* サブクラスのデストラクタをコンパイルしているときは、
 | 
|---|
| 553 |                     基底クラスのデストラクタを呼び出す */
 | 
|---|
| 554 | 
 | 
|---|
| 555 |                 const CMethod *method = compiler.GetCompilingClass().GetSuperClass().GetDestructorMethod();
 | 
|---|
| 556 |                 if( method ){
 | 
|---|
| 557 |                     Opcode_CallProc("",
 | 
|---|
| 558 |                         &method->GetUserProc(),
 | 
|---|
| 559 |                         0,
 | 
|---|
| 560 |                         "");
 | 
|---|
| 561 |                 }
 | 
|---|
| 562 |             }
 | 
|---|
| 563 |         }
 | 
|---|
| 564 |     }
 | 
|---|
| 565 | 
 | 
|---|
| 566 |     // Tryスコープの検証
 | 
|---|
| 567 |     Exception::InspectTryScope();
 | 
|---|
| 568 | 
 | 
|---|
| 569 |     //With情報のメモリを解放
 | 
|---|
| 570 |     for(i3=0;i3<WithInfo.num;i3++){
 | 
|---|
| 571 |         compiler.errorMessenger.Output(22,"With",WithInfo.pWithCp[i3]);
 | 
|---|
| 572 |         HeapDefaultFree(WithInfo.ppName[i3]);
 | 
|---|
| 573 |     }
 | 
|---|
| 574 |     HeapDefaultFree(WithInfo.ppName);
 | 
|---|
| 575 |     HeapDefaultFree(WithInfo.pWithCp);
 | 
|---|
| 576 | 
 | 
|---|
| 577 |     //ローカルオブジェクト(レキシカルスコープレベル=0)の解放処理
 | 
|---|
| 578 |     compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
 | 
|---|
| 579 | 
 | 
|---|
| 580 |     //プロシージャ抜け出しスケジュール(Exit Sub/Function)
 | 
|---|
| 581 |     compiler.codeGenerator.ResolveExitSubSchedule();
 | 
|---|
| 582 | 
 | 
|---|
| 583 |     if( compiler.IsDebug() && bDebugSupportProc == 0 )
 | 
|---|
| 584 |     {
 | 
|---|
| 585 |         //call _DebugSys_EndProc
 | 
|---|
| 586 |         extern const UserProc *pSub_DebugSys_EndProc;
 | 
|---|
| 587 |         compiler.codeGenerator.op_call(pSub_DebugSys_EndProc);
 | 
|---|
| 588 |     }
 | 
|---|
| 589 | 
 | 
|---|
| 590 |     if( !pUserProc->ReturnType().IsNull() ){
 | 
|---|
| 591 |         //////////////////////////////////
 | 
|---|
| 592 |         // 戻り値をraxまたはxmm0に設定
 | 
|---|
| 593 |         //////////////////////////////////
 | 
|---|
| 594 | 
 | 
|---|
| 595 |         RELATIVE_VAR RelativeVar;
 | 
|---|
| 596 | 
 | 
|---|
| 597 |         const char *temp = pUserProc->GetName().c_str();
 | 
|---|
| 598 |         if( temp[0]==1 && temp[1]==ESC_OPERATOR ){
 | 
|---|
| 599 |             temp="_System_ReturnValue";
 | 
|---|
| 600 |         }
 | 
|---|
| 601 |         GetVarOffsetReadWrite(temp,&RelativeVar,Type());
 | 
|---|
| 602 | 
 | 
|---|
| 603 |         const Type &returnType = pUserProc->ReturnType();
 | 
|---|
| 604 |         if( returnType.IsObject() || returnType.IsStruct() )
 | 
|---|
| 605 |         {
 | 
|---|
| 606 |             SetVarPtrToReg(REG_RAX,&RelativeVar);
 | 
|---|
| 607 |             if( returnType.IsObject() )
 | 
|---|
| 608 |             {
 | 
|---|
| 609 |                 //mov rax,qword ptr[rax]
 | 
|---|
| 610 |                 compiler.codeGenerator.op_mov_RM( sizeof(_int64), REG_RAX, REG_RAX, 0, MOD_BASE );
 | 
|---|
| 611 |             }
 | 
|---|
| 612 |         }
 | 
|---|
| 613 |         else if( returnType.IsDouble() )
 | 
|---|
| 614 |         {
 | 
|---|
| 615 |             //64ビット実数型
 | 
|---|
| 616 |             SetXmmReg_DoubleVariable(&RelativeVar,REG_XMM0);
 | 
|---|
| 617 |         }
 | 
|---|
| 618 |         else if( returnType.IsSingle() )
 | 
|---|
| 619 |         {
 | 
|---|
| 620 |             //32ビット実数型
 | 
|---|
| 621 |             SetXmmReg_SingleVariable(&RelativeVar,REG_XMM0);
 | 
|---|
| 622 |         }
 | 
|---|
| 623 |         else if( returnType.IsWhole() )
 | 
|---|
| 624 |         {
 | 
|---|
| 625 |             //整数型
 | 
|---|
| 626 |             SetReg_WholeVariable(returnType,&RelativeVar,REG_RAX);
 | 
|---|
| 627 |         }
 | 
|---|
| 628 |         else compiler.errorMessenger.Output(300,NULL,cp);
 | 
|---|
| 629 |     }
 | 
|---|
| 630 | 
 | 
|---|
| 631 |     //ローカル変数領域のサイズをスタックフレームに通知
 | 
|---|
| 632 |     int localParmSize = AllLocalVarSize - BaseLocalVar;
 | 
|---|
| 633 |     int stackFrameSize = pobj_sf->GetFrameSize( localParmSize );
 | 
|---|
| 634 | 
 | 
|---|
| 635 |     //ローカル変数アドレススケジュール
 | 
|---|
| 636 |     BOOST_FOREACH( const PertialSchedule *pPertialSchedule, compiler.codeGenerator.localVarPertialSchedules )
 | 
|---|
| 637 |     {
 | 
|---|
| 638 |         compiler.codeGenerator.opfix_offset( pPertialSchedule, AllLocalVarSize + stackFrameSize );
 | 
|---|
| 639 |     }
 | 
|---|
| 640 |     compiler.codeGenerator.localVarPertialSchedules.clear();
 | 
|---|
| 641 |     BOOST_FOREACH( Variable *pVar, pUserProc->GetLocalVars() ){
 | 
|---|
| 642 |         //後にデバッグで利用する
 | 
|---|
| 643 |         pVar->SetOffsetAddress(
 | 
|---|
| 644 |             AllLocalVarSize + stackFrameSize - pVar->GetOffsetAddress()
 | 
|---|
| 645 |         );
 | 
|---|
| 646 |     }
 | 
|---|
| 647 | 
 | 
|---|
| 648 |     //mov reg,qword ptr[rsp+offset]     ※スタックフレームを利用
 | 
|---|
| 649 |     pobj_sf->pop(REG_R15);
 | 
|---|
| 650 |     pobj_sf->pop(REG_R14);
 | 
|---|
| 651 |     pobj_sf->pop(REG_R13);
 | 
|---|
| 652 |     pobj_sf->pop(REG_R12);
 | 
|---|
| 653 |     pobj_sf->pop(REG_RDI);
 | 
|---|
| 654 |     pobj_sf->pop(REG_RSI);
 | 
|---|
| 655 |     pobj_sf->pop(REG_RBX);
 | 
|---|
| 656 | 
 | 
|---|
| 657 |     int stackFrameAndLocalParamSize = localParmSize + stackFrameSize;
 | 
|---|
| 658 | 
 | 
|---|
| 659 |     //add rsp,スタックフレームサイズ
 | 
|---|
| 660 |     compiler.codeGenerator.op_add_rsp(stackFrameAndLocalParamSize);
 | 
|---|
| 661 | 
 | 
|---|
| 662 |     //ret
 | 
|---|
| 663 |     compiler.codeGenerator.op_ret();
 | 
|---|
| 664 | 
 | 
|---|
| 665 | 
 | 
|---|
| 666 |     //デバッグ用
 | 
|---|
| 667 |     if( pRspOffsetPertialSchedule1 ){
 | 
|---|
| 668 |         compiler.codeGenerator.opfix( pRspOffsetPertialSchedule1, stackFrameAndLocalParamSize );
 | 
|---|
| 669 |         compiler.codeGenerator.opfix( pRspOffsetPertialSchedule2, stackFrameAndLocalParamSize + sizeof(_int64) );
 | 
|---|
| 670 |     }
 | 
|---|
| 671 | 
 | 
|---|
| 672 | 
 | 
|---|
| 673 |     //スタックフレームスケジュール(subコマンド)
 | 
|---|
| 674 |     compiler.codeGenerator.opfix( pStackFramePertialSchedule, stackFrameAndLocalParamSize );
 | 
|---|
| 675 | 
 | 
|---|
| 676 |     //スタックフレームスケジュールを実行
 | 
|---|
| 677 |     pobj_sf->RunningSchedule( stackFrameSize );
 | 
|---|
| 678 |     delete pobj_sf;
 | 
|---|
| 679 |     pobj_sf=0;
 | 
|---|
| 680 | 
 | 
|---|
| 681 | 
 | 
|---|
| 682 |     compiler.FinishProcedureCompile();
 | 
|---|
| 683 | 
 | 
|---|
| 684 | 
 | 
|---|
| 685 |     //ローカル変数のネーム情報は後に解放する
 | 
|---|
| 686 | }
 | 
|---|