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