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