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