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