[206] | 1 | #include "stdafx.h"
|
---|
| 2 |
|
---|
[357] | 3 | #ifdef _AMD64_
|
---|
[485] | 4 | #include "../../compiler_x64/opcode.h"
|
---|
[357] | 5 | #else
|
---|
[484] | 6 | #include "../../compiler_x86/opcode.h"
|
---|
[357] | 7 | #endif
|
---|
| 8 |
|
---|
[52] | 9 | namespace Exception{
|
---|
| 10 |
|
---|
[361] | 11 | class CatchScope
|
---|
| 12 | {
|
---|
| 13 | Type paramType;
|
---|
| 14 | long codePos;
|
---|
| 15 | public:
|
---|
| 16 | CatchScope( const Type ¶mType, long codePos )
|
---|
| 17 | : paramType( paramType )
|
---|
| 18 | , codePos( codePos )
|
---|
| 19 | {
|
---|
| 20 | }
|
---|
| 21 | ~CatchScope()
|
---|
| 22 | {
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | const Type &GetParamType() const
|
---|
| 26 | {
|
---|
| 27 | return paramType;
|
---|
| 28 | }
|
---|
| 29 | long GetCodePos() const
|
---|
| 30 | {
|
---|
| 31 | return codePos;
|
---|
| 32 | }
|
---|
| 33 | };
|
---|
| 34 | typedef std::vector<CatchScope> CatchScopes;
|
---|
| 35 |
|
---|
[357] | 36 | class TryScope
|
---|
| 37 | {
|
---|
[365] | 38 | int sourceCodePos;
|
---|
[359] | 39 | bool isDefinedFinally;
|
---|
| 40 |
|
---|
[361] | 41 | CatchScopes catchScopes;
|
---|
| 42 |
|
---|
[359] | 43 | std::vector<const PertialSchedule *> finallySchedules;
|
---|
| 44 | void JmpFinally()
|
---|
| 45 | {
|
---|
| 46 | finallySchedules.push_back(
|
---|
| 47 | compiler.codeGenerator.op_jmp( 0, sizeof(long), true )
|
---|
| 48 | );
|
---|
| 49 | }
|
---|
| 50 | void ResolveJmpFinally()
|
---|
| 51 | {
|
---|
[750] | 52 | foreach( const PertialSchedule *pPertialSchedule, finallySchedules )
|
---|
[359] | 53 | {
|
---|
| 54 | compiler.codeGenerator.opfix_JmpPertialSchedule( pPertialSchedule );
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[361] | 58 | const PertialSchedule *pPertialScheduleForCatchAddress;
|
---|
[359] | 59 | const PertialSchedule *pPertialScheduleForFinallyAddress;
|
---|
| 60 |
|
---|
[357] | 61 | public:
|
---|
[365] | 62 | TryScope( int sourceCodePos )
|
---|
| 63 | : sourceCodePos( sourceCodePos )
|
---|
| 64 | , isDefinedFinally( false )
|
---|
[357] | 65 | {
|
---|
| 66 | }
|
---|
| 67 | ~TryScope()
|
---|
| 68 | {
|
---|
| 69 | }
|
---|
[52] | 70 |
|
---|
[365] | 71 | int GetSourceCodePos() const
|
---|
| 72 | {
|
---|
| 73 | return sourceCodePos;
|
---|
| 74 | }
|
---|
[361] | 75 | const CatchScopes &GetCatchScopes() const
|
---|
| 76 | {
|
---|
| 77 | return catchScopes;
|
---|
| 78 | }
|
---|
[359] | 79 | bool IsCatched() const
|
---|
| 80 | {
|
---|
[361] | 81 | return ( catchScopes.size() != 0 );
|
---|
[359] | 82 | }
|
---|
| 83 | bool IsDefinedFinally() const
|
---|
| 84 | {
|
---|
| 85 | return isDefinedFinally;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[357] | 88 | void RegistPertialScheduleForCatchAddress( const PertialSchedule *pPertialScheduleForCatchAddress )
|
---|
| 89 | {
|
---|
| 90 | this->pPertialScheduleForCatchAddress = pPertialScheduleForCatchAddress;
|
---|
| 91 | }
|
---|
[361] | 92 | const PertialSchedule *GetPertialScheduleForCatchAddress() const
|
---|
| 93 | {
|
---|
| 94 | return pPertialScheduleForCatchAddress;
|
---|
| 95 | }
|
---|
[359] | 96 | void RegistPertialScheduleForFinallyAddress( const PertialSchedule *pPertialScheduleForFinallyAddress )
|
---|
| 97 | {
|
---|
| 98 | this->pPertialScheduleForFinallyAddress = pPertialScheduleForFinallyAddress;
|
---|
| 99 | }
|
---|
| 100 | const PertialSchedule *GetPertialScheduleForFinallyAddress() const
|
---|
| 101 | {
|
---|
| 102 | return pPertialScheduleForFinallyAddress;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
[357] | 106 |
|
---|
[359] | 107 | void Try()
|
---|
[357] | 108 | {
|
---|
[364] | 109 | // レキシカルスコープをレベルアップ
|
---|
| 110 | compiler.codeGenerator.lexicalScopes.Start(
|
---|
| 111 | compiler.codeGenerator.GetNativeCodeSize(),
|
---|
| 112 | LexicalScope::SCOPE_TRY
|
---|
| 113 | );
|
---|
[357] | 114 | }
|
---|
| 115 |
|
---|
[361] | 116 | void Catch( Type ¶mType )
|
---|
[359] | 117 | {
|
---|
| 118 | if( isDefinedFinally )
|
---|
| 119 | {
|
---|
[465] | 120 | compiler.errorMessenger.Output(71,NULL,cp);
|
---|
[359] | 121 | return;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[364] | 124 | if( catchScopes.size() )
|
---|
| 125 | {
|
---|
| 126 | // 既に1回以上のCatchが存在するとき
|
---|
| 127 |
|
---|
| 128 | // レキシカルスコープをレベルダウン
|
---|
| 129 | compiler.codeGenerator.lexicalScopes.End();
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[361] | 132 | JmpFinally();
|
---|
[359] | 133 |
|
---|
[361] | 134 | catchScopes.push_back( CatchScope( paramType, compiler.codeGenerator.GetNativeCodeSize() ) );
|
---|
[364] | 135 |
|
---|
| 136 | // レキシカルスコープをレベルアップ
|
---|
| 137 | compiler.codeGenerator.lexicalScopes.Start(
|
---|
| 138 | compiler.codeGenerator.GetNativeCodeSize(),
|
---|
| 139 | LexicalScope::SCOPE_CATCH
|
---|
| 140 | );
|
---|
[359] | 141 | }
|
---|
| 142 | void Finally()
|
---|
| 143 | {
|
---|
| 144 | if( isDefinedFinally )
|
---|
| 145 | {
|
---|
[465] | 146 | compiler.errorMessenger.Output(70,NULL,cp);
|
---|
[359] | 147 | return;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[364] | 150 | if( catchScopes.size() )
|
---|
| 151 | {
|
---|
| 152 | // 既に1回以上のCatchが存在するとき
|
---|
| 153 |
|
---|
| 154 | // レキシカルスコープをレベルダウン
|
---|
| 155 | compiler.codeGenerator.lexicalScopes.End();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[359] | 158 | isDefinedFinally = true;
|
---|
| 159 |
|
---|
| 160 | ResolveJmpFinally();
|
---|
[364] | 161 |
|
---|
| 162 | // レキシカルスコープをレベルアップ
|
---|
| 163 | compiler.codeGenerator.lexicalScopes.Start(
|
---|
| 164 | compiler.codeGenerator.GetNativeCodeSize(),
|
---|
| 165 | LexicalScope::SCOPE_FINALLY
|
---|
| 166 | );
|
---|
[359] | 167 | }
|
---|
| 168 |
|
---|
[357] | 169 | void EndTry()
|
---|
| 170 | {
|
---|
[359] | 171 | if( !isDefinedFinally )
|
---|
| 172 | {
|
---|
[465] | 173 | compiler.errorMessenger.OutputFatalError();
|
---|
[359] | 174 | }
|
---|
[364] | 175 |
|
---|
| 176 | if( catchScopes.size() || isDefinedFinally )
|
---|
| 177 | {
|
---|
| 178 | // 既に1回以上のCatch、またはFinallyが存在するとき
|
---|
| 179 |
|
---|
| 180 | // レキシカルスコープをレベルダウン
|
---|
| 181 | compiler.codeGenerator.lexicalScopes.End();
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | // レキシカルスコープをレベルダウン
|
---|
| 185 | compiler.codeGenerator.lexicalScopes.End();
|
---|
[357] | 186 | }
|
---|
[361] | 187 |
|
---|
| 188 | int GenerateCatchTable()
|
---|
| 189 | {
|
---|
| 190 | int size = static_cast<int>(( (catchScopes.size()+1)*2 ) * sizeof(LONG_PTR));
|
---|
| 191 | BYTE *buffer = (BYTE *)calloc( size, 1 );
|
---|
| 192 |
|
---|
| 193 | int pos = 0;
|
---|
[750] | 194 | foreach( const CatchScope &catchScope, catchScopes )
|
---|
[361] | 195 | {
|
---|
| 196 | // パラメータのクラス名
|
---|
| 197 | char paramName[VN_SIZE] = "";
|
---|
| 198 | if( catchScope.GetParamType().IsObject() )
|
---|
| 199 | {
|
---|
| 200 | lstrcpy( paramName, catchScope.GetParamType().GetClass().GetFullName().c_str() );
|
---|
| 201 | }
|
---|
[745] | 202 | int paramNameDataTableOffset = compiler.AddStringToDataTable( paramName );
|
---|
[361] | 203 | *((LONG_PTR *)(buffer+pos)) = paramNameDataTableOffset;
|
---|
| 204 | pos += sizeof(LONG_PTR);
|
---|
| 205 |
|
---|
| 206 | // Catchアドレス
|
---|
| 207 | *((LONG_PTR *)(buffer+pos)) = catchScope.GetCodePos();
|
---|
| 208 | pos += sizeof(LONG_PTR);
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[587] | 211 | int dataTableOffset = compiler.GetObjectModule().dataTable.AddBinary( buffer, size );
|
---|
[361] | 212 |
|
---|
| 213 | free( buffer );
|
---|
| 214 |
|
---|
| 215 | pos = 0;
|
---|
[750] | 216 | foreach( const CatchScope &catchScope, catchScopes )
|
---|
[361] | 217 | {
|
---|
| 218 | // パラメータのクラス名
|
---|
[587] | 219 | compiler.GetObjectModule().dataTable.schedules.push_back( Schedule( Schedule::DataTable, dataTableOffset + pos ) );
|
---|
[361] | 220 | pos += sizeof(LONG_PTR);
|
---|
| 221 |
|
---|
| 222 | // Catchアドレス
|
---|
[537] | 223 | const UserProc *pUserProc = &compiler.GetCompilingUserProc();
|
---|
| 224 | if( compiler.IsGlobalAreaCompiling() )
|
---|
[364] | 225 | {
|
---|
| 226 | pUserProc = UserProc::pGlobalProc;
|
---|
| 227 | }
|
---|
[587] | 228 | compiler.GetObjectModule().dataTable.schedules.push_back(
|
---|
[550] | 229 | Schedule( Schedule::CatchAddress, pUserProc, dataTableOffset + pos )
|
---|
| 230 | );
|
---|
[361] | 231 | pos += sizeof(LONG_PTR);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | return dataTableOffset;
|
---|
| 235 | }
|
---|
[357] | 236 | };
|
---|
| 237 | typedef std::vector<TryScope> TryScopes;
|
---|
| 238 |
|
---|
| 239 | TryScopes tryScopes;
|
---|
| 240 |
|
---|
| 241 | void TryCommand()
|
---|
| 242 | {
|
---|
[365] | 243 | tryScopes.push_back( TryScope( cp ) );
|
---|
[359] | 244 | tryScopes.back().Try();
|
---|
[357] | 245 |
|
---|
| 246 | int backCp = cp;
|
---|
| 247 |
|
---|
| 248 | char temporary[1024];
|
---|
[359] | 249 | lstrcpy( temporary, "ExceptionService.BeginTryScope( _System_GetNowScopeCatchAddresses() As VoidPtr, _System_GetNowScopeFinallyAddresses() As VoidPtr, _System_GetBp() As LONG_PTR, _System_GetSp() As LONG_PTR )" );
|
---|
[357] | 250 | MakeMiddleCode( temporary );
|
---|
| 251 | ChangeOpcode( temporary );
|
---|
| 252 |
|
---|
| 253 | cp = backCp;
|
---|
[52] | 254 | }
|
---|
[359] | 255 | void CatchCommand( const char *parameter )
|
---|
[357] | 256 | {
|
---|
| 257 | if( tryScopes.size() == 0 )
|
---|
| 258 | {
|
---|
[465] | 259 | compiler.errorMessenger.Output(1,NULL,cp);
|
---|
[357] | 260 | return;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
[364] | 263 | char varName[VN_SIZE];
|
---|
[359] | 264 | Type paramType;
|
---|
| 265 | if( parameter[0] )
|
---|
| 266 | {
|
---|
[364] | 267 | char typeName[VN_SIZE];
|
---|
[359] | 268 | SplitSyntacticForAs( parameter, varName, typeName );
|
---|
| 269 | if( !typeName[0] )
|
---|
| 270 | {
|
---|
[465] | 271 | compiler.errorMessenger.Output(72,NULL,cp);
|
---|
[359] | 272 | }
|
---|
| 273 | else
|
---|
| 274 | {
|
---|
| 275 | if( !compiler.StringToType( typeName, paramType ) )
|
---|
| 276 | {
|
---|
[465] | 277 | compiler.errorMessenger.Output(74,typeName,cp);
|
---|
[359] | 278 | }
|
---|
[391] | 279 | else
|
---|
| 280 | {
|
---|
| 281 | if( !paramType.IsObject() )
|
---|
| 282 | {
|
---|
[465] | 283 | compiler.errorMessenger.Output(73,typeName,cp);
|
---|
[391] | 284 | }
|
---|
| 285 | }
|
---|
[359] | 286 | }
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[361] | 289 | tryScopes.back().Catch( paramType );
|
---|
[364] | 290 |
|
---|
[371] | 291 | int backCp = cp;
|
---|
| 292 |
|
---|
| 293 | char temporary[1024];
|
---|
[364] | 294 | if( paramType.IsObject() )
|
---|
| 295 | {
|
---|
[465] | 296 | sprintf( temporary, "Dim %s = System.Threading.Thread.CurrentThread().__GetThrowintParamObject() As %s", varName, paramType.GetClass().GetFullName().c_str() );
|
---|
[364] | 297 | MakeMiddleCode( temporary );
|
---|
| 298 | ChangeOpcode( temporary );
|
---|
[371] | 299 | }
|
---|
[465] | 300 | lstrcpy( temporary, "System.Threading.Thread.CurrentThread().__Catched()" );
|
---|
[371] | 301 | MakeMiddleCode( temporary );
|
---|
| 302 | ChangeOpcode( temporary );
|
---|
[364] | 303 |
|
---|
[371] | 304 | cp = backCp;
|
---|
[52] | 305 | }
|
---|
[357] | 306 | void FinallyCommand()
|
---|
| 307 | {
|
---|
[359] | 308 | tryScopes.back().Finally();
|
---|
| 309 |
|
---|
| 310 | int backCp = cp;
|
---|
| 311 |
|
---|
| 312 | char temporary[1024];
|
---|
| 313 | lstrcpy( temporary, "_System_pobj_AllThreads->GetCurrentException()->FinishFinally()" );
|
---|
| 314 | MakeMiddleCode( temporary );
|
---|
| 315 | ChangeOpcode( temporary );
|
---|
| 316 |
|
---|
| 317 | cp = backCp;
|
---|
| 318 |
|
---|
| 319 | //Tryスコープに入るときに引き渡されるパラメータ値を解決
|
---|
| 320 | compiler.codeGenerator.opfix( tryScopes.back().GetPertialScheduleForFinallyAddress(), compiler.codeGenerator.GetNativeCodeSize() );
|
---|
[52] | 321 | }
|
---|
[366] | 322 | void EndTryCommand( bool isNoWarning )
|
---|
[357] | 323 | {
|
---|
| 324 | if( tryScopes.size() == 0 )
|
---|
| 325 | {
|
---|
[465] | 326 | compiler.errorMessenger.Output(12,"End Try",cp);
|
---|
[357] | 327 | return;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
[366] | 330 | if( !isNoWarning && !tryScopes.back().IsDefinedFinally() && !tryScopes.back().IsCatched() )
|
---|
[365] | 331 | {
|
---|
| 332 | // CatchもFinallyも存在しないとき
|
---|
[465] | 333 | compiler.errorMessenger.Output(-108,NULL,tryScopes.back().GetSourceCodePos() );
|
---|
[365] | 334 | }
|
---|
| 335 |
|
---|
[361] | 336 | int dataTableOffset = tryScopes.back().GenerateCatchTable();
|
---|
[357] | 337 |
|
---|
[361] | 338 | // _System_GetNowScopeCatchAddressesを解決
|
---|
| 339 | compiler.codeGenerator.opfix( tryScopes.back().GetPertialScheduleForCatchAddress(), dataTableOffset );
|
---|
[359] | 340 |
|
---|
| 341 | if( !tryScopes.back().IsDefinedFinally() )
|
---|
| 342 | {
|
---|
| 343 | // Finallyが定義されていないときは空のFinallyを定義しておく
|
---|
| 344 | FinallyCommand();
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[361] | 347 | int backCp = cp;
|
---|
| 348 |
|
---|
[357] | 349 | char temporary[1024];
|
---|
| 350 | lstrcpy( temporary, "_System_pobj_AllThreads->GetCurrentException()->EndTryScope()" );
|
---|
| 351 | MakeMiddleCode( temporary );
|
---|
| 352 | ChangeOpcode( temporary );
|
---|
| 353 |
|
---|
| 354 | cp = backCp;
|
---|
| 355 |
|
---|
[359] | 356 | tryScopes.back().EndTry();
|
---|
[357] | 357 | tryScopes.pop_back();
|
---|
[52] | 358 | }
|
---|
| 359 |
|
---|
[366] | 360 | void InspectTryScope()
|
---|
| 361 | {
|
---|
| 362 | while( tryScopes.size() > 0 )
|
---|
| 363 | {
|
---|
[465] | 364 | compiler.errorMessenger.Output(22, "Try", tryScopes.back().GetSourceCodePos() );
|
---|
[366] | 365 |
|
---|
| 366 | EndTryCommand( true );
|
---|
| 367 | }
|
---|
| 368 | }
|
---|
| 369 |
|
---|
[357] | 370 | void ThrowCommand( const char *Parameter )
|
---|
| 371 | {
|
---|
| 372 | int backCp = cp;
|
---|
| 373 |
|
---|
| 374 | char temporary[1024];
|
---|
[361] | 375 | if( Parameter[0] )
|
---|
| 376 | {
|
---|
| 377 | sprintf( temporary, "_System_pobj_AllThreads->GetCurrentException()->_ThrowWithParam(%s)", Parameter );
|
---|
| 378 | }
|
---|
| 379 | else
|
---|
| 380 | {
|
---|
| 381 | lstrcpy( temporary, "_System_pobj_AllThreads->GetCurrentException()->_ThrowNoneParam()" );
|
---|
| 382 | }
|
---|
[357] | 383 | MakeMiddleCode( temporary );
|
---|
| 384 | ChangeOpcode( temporary );
|
---|
| 385 |
|
---|
| 386 | cp = backCp;
|
---|
[52] | 387 | }
|
---|
| 388 |
|
---|
[357] | 389 | void Opcode_Func_System_GetNowScopeCatchAddress()
|
---|
| 390 | {
|
---|
| 391 | if( tryScopes.size() == 0 )
|
---|
| 392 | {
|
---|
[465] | 393 | compiler.errorMessenger.Output(1,NULL,cp);
|
---|
[357] | 394 | return;
|
---|
| 395 | }
|
---|
[52] | 396 |
|
---|
[357] | 397 | #ifdef _WIN64
|
---|
| 398 | //mov rax,catchAddress
|
---|
[361] | 399 | const PertialSchedule *pPertialSchedule = compiler.codeGenerator.op_mov_RV( sizeof(long), REG_RAX, 0, Schedule::DataTable, true );
|
---|
[357] | 400 | #else
|
---|
| 401 | //mov eax,catchAddress
|
---|
[361] | 402 | const PertialSchedule *pPertialSchedule = compiler.codeGenerator.op_mov_RV( REG_EAX, 0, Schedule::DataTable, true );
|
---|
[357] | 403 | #endif
|
---|
| 404 |
|
---|
| 405 | tryScopes.back().RegistPertialScheduleForCatchAddress( pPertialSchedule );
|
---|
| 406 |
|
---|
| 407 | /*
|
---|
[587] | 408 | int dataTableOffset = compiler.GetObjectModule().dataTable.Add( static_cast<LONG_PTR>(0) );
|
---|
[357] | 409 |
|
---|
| 410 | #ifdef _WIN64
|
---|
| 411 | //mov rax,dataTableOffset
|
---|
| 412 | compiler.codeGenerator.op_mov_RV( sizeof(_int64), REG_RAX, dataTableOffset, Schedule::DataTable);
|
---|
| 413 | #else
|
---|
| 414 | //mov eax,dataTableOffset
|
---|
| 415 | compiler.codeGenerator.op_mov_RV( REG_EAX, dataTableOffset, Schedule::DataTable);
|
---|
| 416 | #endif
|
---|
| 417 | */
|
---|
| 418 | }
|
---|
| 419 |
|
---|
[359] | 420 | void Opcode_Func_System_GetNowScopeFinallyAddress()
|
---|
| 421 | {
|
---|
| 422 | if( tryScopes.size() == 0 )
|
---|
| 423 | {
|
---|
[465] | 424 | compiler.errorMessenger.Output(1,NULL,cp);
|
---|
[359] | 425 | return;
|
---|
| 426 | }
|
---|
[357] | 427 |
|
---|
[359] | 428 | #ifdef _WIN64
|
---|
| 429 | //mov rax,finallyAddress
|
---|
| 430 | const PertialSchedule *pPertialSchedule = compiler.codeGenerator.op_mov_RV( sizeof(long), REG_RAX, 0, Schedule::CatchAddress, true );
|
---|
| 431 | #else
|
---|
| 432 | //mov eax,finallyAddress
|
---|
| 433 | const PertialSchedule *pPertialSchedule = compiler.codeGenerator.op_mov_RV( REG_EAX, 0, Schedule::CatchAddress, true );
|
---|
| 434 | #endif
|
---|
| 435 |
|
---|
| 436 | tryScopes.back().RegistPertialScheduleForFinallyAddress( pPertialSchedule );
|
---|
| 437 |
|
---|
| 438 | /*
|
---|
[587] | 439 | int dataTableOffset = compiler.GetObjectModule().dataTable.Add( static_cast<LONG_PTR>(0) );
|
---|
[359] | 440 |
|
---|
| 441 | #ifdef _WIN64
|
---|
| 442 | //mov rax,dataTableOffset
|
---|
| 443 | compiler.codeGenerator.op_mov_RV( sizeof(_int64), REG_RAX, dataTableOffset, Schedule::DataTable);
|
---|
| 444 | #else
|
---|
| 445 | //mov eax,dataTableOffset
|
---|
| 446 | compiler.codeGenerator.op_mov_RV( REG_EAX, dataTableOffset, Schedule::DataTable);
|
---|
| 447 | #endif
|
---|
| 448 | */
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 |
|
---|
[52] | 452 | } // Exception
|
---|