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