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