[4] | 1 | #include "common.h"
|
---|
| 2 | #ifdef _AMD64_
|
---|
| 3 | #include "../BasicCompiler64/opcode.h"
|
---|
| 4 | #else
|
---|
[5] | 5 | #include "../BasicCompiler32/opcode.h"
|
---|
[4] | 6 | #endif
|
---|
| 7 |
|
---|
| 8 |
|
---|
| 9 | CLexicalScopes obj_LexScopes;
|
---|
| 10 |
|
---|
[34] | 11 |
|
---|
| 12 | CScope::CScope( int level, int addr, SCOPE_TYPE TypeOfStatement ){
|
---|
| 13 | this->level = level;
|
---|
| 14 | this->StartAddress = addr;
|
---|
| 15 | this->TypeOfStatement = TypeOfStatement;
|
---|
| 16 |
|
---|
| 17 | pBreakSchedule = (DWORD *)malloc( 1 );
|
---|
| 18 | nBreakSchedule = 0;
|
---|
| 19 | }
|
---|
| 20 | CScope::~CScope(){
|
---|
| 21 | free( pBreakSchedule );
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | int CScope::GetStartAddress(){
|
---|
| 25 | return StartAddress;
|
---|
| 26 | }
|
---|
| 27 | SCOPE_TYPE CScope::GetTypeOfStatement(){
|
---|
| 28 | return TypeOfStatement;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | void CScope::Break(){
|
---|
| 32 | //未解放のローカルオブジェクトを解放する
|
---|
| 33 | obj_LexScopes.CallDestructorsOfReturn( level );
|
---|
| 34 |
|
---|
| 35 | //jmp ...(Next addr)
|
---|
| 36 | OpBuffer[obp++]=(char)0xE9;
|
---|
| 37 |
|
---|
| 38 | pBreakSchedule=(DWORD *)realloc( pBreakSchedule, ( nBreakSchedule + 1 ) * sizeof(DWORD) );
|
---|
| 39 | pBreakSchedule[nBreakSchedule]=obp;
|
---|
| 40 | nBreakSchedule++;
|
---|
| 41 |
|
---|
| 42 | obp+=sizeof(long);
|
---|
| 43 | }
|
---|
| 44 | void CScope::RunScheduleOfBreak(){
|
---|
| 45 | for(int i=0;i<nBreakSchedule;i++){
|
---|
| 46 | *((long *)(OpBuffer+pBreakSchedule[i]))=obp-(pBreakSchedule[i]+sizeof(long));
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | CScope *CLexicalScopes::SearchScope( SCOPE_TYPE TypeOfStatement ){
|
---|
| 52 | for( int i = level; i>=0; i-- ){
|
---|
| 53 | if( ppScopes[i]->GetTypeOfStatement() == TypeOfStatement ){
|
---|
| 54 | return ppScopes[i];
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | return NULL;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[4] | 60 | CLexicalScopes::CLexicalScopes(){
|
---|
[34] | 61 | ppScopes = (CScope **)malloc( 1 );
|
---|
[4] | 62 | level=0;
|
---|
| 63 | }
|
---|
| 64 | CLexicalScopes::~CLexicalScopes(){
|
---|
[34] | 65 | free( ppScopes );
|
---|
[4] | 66 | }
|
---|
| 67 | void CLexicalScopes::Init(int addr){
|
---|
[34] | 68 | // TODO: エラーチェック
|
---|
| 69 |
|
---|
| 70 | level = -1;
|
---|
| 71 | Start( addr, SCOPE_TYPE_BASE );
|
---|
[4] | 72 | }
|
---|
[34] | 73 | void CLexicalScopes::Start( int addr, SCOPE_TYPE TypeOfStatement ){
|
---|
[4] | 74 | level++;
|
---|
[34] | 75 | ppScopes = (CScope **)realloc( ppScopes, ( level + 1 ) * sizeof( CScope * ) );
|
---|
| 76 | ppScopes[level] = new CScope( level, addr, TypeOfStatement );
|
---|
[4] | 77 | }
|
---|
[34] | 78 | void CLexicalScopes::End(){
|
---|
| 79 | if( level <= 0 ){
|
---|
[4] | 80 | SetError(300,NULL,cp);
|
---|
| 81 | return;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[34] | 84 | //デストラクタを呼ぶ
|
---|
| 85 | CallDestructorsOfScopeEnd();
|
---|
| 86 |
|
---|
[75] | 87 | Variables &vars = UserProc::IsGlobalAreaCompiling()?
|
---|
| 88 | globalVars :
|
---|
| 89 | UserProc::CompilingUserProc().localVars;
|
---|
[4] | 90 |
|
---|
[34] | 91 | //使用済みローカル変数の生存チェックを外す
|
---|
[75] | 92 | foreach( Variable *pVar, vars ){
|
---|
| 93 | if(pVar->bLiving&&pVar->ScopeLevel==level){
|
---|
| 94 | pVar->bLiving=0;
|
---|
[4] | 95 | extern int obp;
|
---|
[75] | 96 | pVar->ScopeEndAddress=obp;
|
---|
[4] | 97 | }
|
---|
| 98 | }
|
---|
[34] | 99 |
|
---|
| 100 |
|
---|
| 101 | //スコープ抜け出しスケジュール
|
---|
| 102 | ppScopes[level]->RunScheduleOfBreak();
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 | //スコープレベルを下げる
|
---|
| 106 | delete ppScopes[level];
|
---|
| 107 | level--;
|
---|
[4] | 108 | }
|
---|
| 109 |
|
---|
[34] | 110 | void CLexicalScopes::ExitFor(){
|
---|
| 111 | CScope *pScope = SearchScope( SCOPE_TYPE_FOR );
|
---|
| 112 | if( !pScope ){
|
---|
| 113 | SetError(12,"Exit For",cp);
|
---|
| 114 | return;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | pScope->Break();
|
---|
| 118 | }
|
---|
| 119 | void CLexicalScopes::ExitWhile(){
|
---|
| 120 | CScope *pScope = SearchScope( SCOPE_TYPE_WHILE );
|
---|
| 121 | if( !pScope ){
|
---|
| 122 | SetError(12,"Exit While",cp);
|
---|
| 123 | return;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | pScope->Break();
|
---|
| 127 | }
|
---|
| 128 | void CLexicalScopes::ExitDo(){
|
---|
| 129 | CScope *pScope = SearchScope( SCOPE_TYPE_DO );
|
---|
| 130 | if( !pScope ){
|
---|
| 131 | SetError(12,"Exit Do",cp);
|
---|
| 132 | return;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | pScope->Break();
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | int CLexicalScopes::GetNowLevel(){
|
---|
[4] | 139 | return level;
|
---|
| 140 | }
|
---|
[34] | 141 | void CLexicalScopes::SetNowLevel( int level ){
|
---|
| 142 | this->level = level;
|
---|
[4] | 143 | }
|
---|
[34] | 144 | int CLexicalScopes::GetStartAddress(){
|
---|
| 145 | return ppScopes[level]->GetStartAddress();
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 | // スコープ終了時のデストラクタ呼び出し
|
---|
| 150 | void CLexicalScopes::CallDestructorsOfScopeEnd(){
|
---|
| 151 |
|
---|
[75] | 152 | Variables &vars = UserProc::IsGlobalAreaCompiling()?
|
---|
| 153 | globalVars :
|
---|
| 154 | UserProc::CompilingUserProc().localVars;
|
---|
[34] | 155 |
|
---|
[75] | 156 |
|
---|
[64] | 157 | int i3;
|
---|
[34] | 158 | int indexSystemGC=-1;
|
---|
[75] | 159 | for( i3 = (int)vars.size() - 1; i3 >= 0; i3-- ){ //確保したのと逆順序で解放するため、バックサーチにする
|
---|
[34] | 160 |
|
---|
[75] | 161 | Variable *pVar = vars[i3];
|
---|
| 162 |
|
---|
| 163 | if( UserProc::IsGlobalAreaCompiling() && GetNowLevel() == 0 ){
|
---|
| 164 | if( pVar->GetName() == "_System_GC" ){
|
---|
[34] | 165 | indexSystemGC=i3;
|
---|
| 166 | continue;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | //同一レベルのレキシカルスコープのみを検知
|
---|
[75] | 171 | if(!pVar->bLiving) continue;
|
---|
| 172 | if( pVar->ScopeLevel != GetNowLevel() ) continue;
|
---|
[34] | 173 |
|
---|
[75] | 174 | if( pVar->IsStruct() && pVar->IsParameter() ){
|
---|
[64] | 175 | //構造体パラメータを持つとき
|
---|
[34] | 176 |
|
---|
| 177 | //メモリを解放する
|
---|
| 178 |
|
---|
| 179 | #ifdef _AMD64_
|
---|
| 180 | //x64ビットコード
|
---|
| 181 |
|
---|
| 182 | //mov rcx,qword ptr[rsp+offset]
|
---|
| 183 | op_mov_RM(sizeof(_int64),REG_RCX,REG_RSP,
|
---|
[75] | 184 | -pVar->offset,
|
---|
[34] | 185 | MOD_BASE_DISP32);
|
---|
| 186 | obp-=sizeof(long);
|
---|
| 187 | AddLocalVarAddrSchedule();
|
---|
| 188 | obp+=sizeof(long);
|
---|
| 189 | #else
|
---|
| 190 | //x86コード
|
---|
| 191 |
|
---|
| 192 | //mov ecx,dword ptr[ebp+offset]
|
---|
[75] | 193 | op_mov_RM(sizeof(long),REG_ECX,REG_EBP,-pVar->offset,MOD_BASE_DISP32);
|
---|
[34] | 194 | obp-=sizeof(long);
|
---|
| 195 | AddLocalVarAddrSchedule();
|
---|
| 196 | obp+=sizeof(long);
|
---|
| 197 |
|
---|
| 198 | //push ecx
|
---|
| 199 | op_push(REG_ECX);
|
---|
| 200 | #endif
|
---|
| 201 |
|
---|
| 202 | //call free
|
---|
[75] | 203 | extern UserProc *pSub_free;
|
---|
[34] | 204 | op_call(pSub_free);
|
---|
| 205 |
|
---|
| 206 |
|
---|
[75] | 207 | if( UserProc::IsGlobalAreaCompiling() ){
|
---|
[34] | 208 | //ここには来ないハズ
|
---|
| 209 | SetError(300,NULL,cp);
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | if(indexSystemGC!=-1){
|
---|
| 215 | //_System_GCオブジェクトのデストラクタの呼び出し処理
|
---|
[135] | 216 | const CMethod *method = vars[indexSystemGC]->GetClass().GetDestructorMethod();
|
---|
[51] | 217 | if( method ){
|
---|
[75] | 218 | Opcode_CallProc("",method->pUserProc,0,vars[indexSystemGC]->GetName().c_str(),DEF_OBJECT);
|
---|
[34] | 219 | }
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | // Returnステートメントで発行されるデストラクタを生成
|
---|
| 224 | void CLexicalScopes::CallDestructorsOfReturn( int BaseLevel ){
|
---|
| 225 | //現在のスコープレベルを退避
|
---|
| 226 | int backupScopeLevel = GetNowLevel();
|
---|
| 227 |
|
---|
| 228 | for( int i = GetNowLevel(); i >= BaseLevel; i-- ){
|
---|
| 229 | SetNowLevel( i );
|
---|
| 230 |
|
---|
| 231 | CallDestructorsOfScopeEnd();
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | //現在のスコープレベルを復元
|
---|
| 235 | SetNowLevel( backupScopeLevel );
|
---|
| 236 | }
|
---|