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