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