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