Changeset b7898ac


Ignore:
Timestamp:
Dec 18, 2023, 12:24:06 PM (4 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
9fba8e6
Parents:
5546eee4
Message:

Another attempt at fixing execptions. It is very close to the last attempt but the offsets have been updated and checked.

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/exception.c

    r5546eee4 rb7898ac  
    5555}
    5656
     57struct __cfaehm_base_exception_t * __cfaehm_get_current_termination(void) {
     58        return this_exception_context()->current_exception;
     59}
     60
    5761
    5862// RESUMPTION ================================================================
     
    309313                struct _Unwind_Context * unwind_context)
    310314{
    311         //! __cfadbg_print_safe(exception, "CFA: 0x%lx\n", _Unwind_GetCFA(unwind_context));
     315        __cfadbg_print_safe(exception, "CFA: 0x%p\n", (void*)_Unwind_GetCFA(unwind_context));
    312316        __cfadbg_print_safe(exception, "Personality function (%d, %x, %llu, %p, %p):",
    313317                        version, actions, exception_class, unwind_exception, unwind_context);
     
    411415                                _Unwind_Word match_pos =
    412416#                               if defined( __x86_64 )
    413                                     _Unwind_GetCFA(unwind_context) + 8;
     417                                        _Unwind_GetCFA(unwind_context);
    414418#                               elif defined( __i386 )
    415                                     _Unwind_GetCFA(unwind_context) + 24;
     419                                        _Unwind_GetCFA(unwind_context) + 20;
    416420#                               elif defined( __ARM_ARCH )
    417                                     _Unwind_GetCFA(unwind_context) + 40;
     421                                        _Unwind_GetCFA(unwind_context) + 16;
    418422#                               endif
     423                                //! printf("match_pos: %p\n", (void*)match_pos);
     424                                //! fflush(stdout);
    419425                                int (*matcher)(exception_t *) = *(int(**)(exception_t *))match_pos;
    420426
     
    479485// and simply linked from libcfa but there is one problem left, see the exception table for details
    480486__attribute__((noinline))
    481 void __cfaehm_try_terminate(void (*try_block)(),
    482                 void (*catch_block)(int index, exception_t * except),
     487int __cfaehm_try_terminate(void (*try_block)(),
    483488                __attribute__((unused)) int (*match_block)(exception_t * except)) {
    484489        //! volatile int xy = 0;
    485         //! printf("%p %p %p %p\n", &try_block, &catch_block, &match_block, &xy);
     490        //! printf("%p %p %p\n", &try_block, &match_block, &xy);
     491        //! fflush(stdout);
    486492
    487493        // Setup the personality routine and exception table.
     
    507513
    508514        // Normal return for when there is no throw.
    509         return;
     515        return 0;
    510516
    511517        // Exceptionnal path
     
    518524        asm volatile (".CATCH:");
    519525
    520         // Exception handler
    521         // Note: Saving the exception context on the stack breaks termination exceptions.
    522         catch_block( EXCEPT_TO_NODE( this_exception_context()->current_exception )->handler_index,
    523                      this_exception_context()->current_exception );
     526        return EXCEPT_TO_NODE( this_exception_context()->current_exception )->handler_index;
    524527}
    525528
  • libcfa/src/exception.h

    r5546eee4 rb7898ac  
    4444
    4545
     46struct __cfaehm_base_exception_t * __cfaehm_get_current_termination(void);
     47
     48
    4649void __cfaehm_cancel_stack(exception_t * except) __attribute__((noreturn));
    4750
     
    5760
    5861// Function catches termination exceptions.
    59 void __cfaehm_try_terminate(
     62int __cfaehm_try_terminate(
    6063        void (*try_block)(),
    61         void (*catch_block)(int index, exception_t * except),
    6264        int (*match_block)(exception_t * except));
    6365
  • src/ControlStruct/ExceptTranslate.cpp

    r5546eee4 rb7898ac  
    482482                ast::FunctionDecl * terminate_catch,
    483483                ast::FunctionDecl * terminate_match ) {
    484         // { __cfaehm_try_terminate(`try`, `catch`, `match`); }
    485 
    486         ast::UntypedExpr * caller = new ast::UntypedExpr(loc, new ast::NameExpr(loc,
    487                 "__cfaehm_try_terminate" ) );
    488         caller->args.push_back( new ast::VariableExpr(loc, try_wrapper ) );
    489         caller->args.push_back( new ast::VariableExpr(loc, terminate_catch ) );
    490         caller->args.push_back( new ast::VariableExpr(loc, terminate_match ) );
    491 
    492         ast::CompoundStmt * callStmt = new ast::CompoundStmt(loc);
    493         callStmt->push_back( new ast::ExprStmt( loc, caller ) );
    494         return callStmt;
     484        // {
     485        //     int __handler_index = __cfaehm_try_terminate(`try`, `match`);
     486        //     if ( __handler_index ) {
     487        //         `catch`( __handler_index, __cfaehm_get_current_termination() );
     488        //     }
     489        // }
     490
     491        ast::ObjectDecl * index = new ast::ObjectDecl( loc, "__handler_index",
     492                new ast::BasicType( ast::BasicType::SignedInt ),
     493                new ast::SingleInit( loc,
     494                        new ast::UntypedExpr( loc,
     495                                new ast::NameExpr( loc, "__cfaehm_try_terminate" ),
     496                                {
     497                                        new ast::VariableExpr( loc, try_wrapper ),
     498                                        new ast::VariableExpr( loc, terminate_match ),
     499                                }
     500                        )
     501                )
     502        );
     503
     504        return new ast::CompoundStmt( loc, {
     505                new ast::DeclStmt( loc, index ),
     506                new ast::IfStmt( loc,
     507                        new ast::VariableExpr( loc, index ),
     508                        new ast::ExprStmt( loc,
     509                                new ast::UntypedExpr( loc,
     510                                        new ast::VariableExpr( loc, terminate_catch ),
     511                                        {
     512                                                new ast::VariableExpr( loc, index ),
     513                                                new ast::UntypedExpr( loc,
     514                                                        new ast::NameExpr( loc, "__cfaehm_get_current_termination" )
     515                                                ),
     516                                        }
     517                                )
     518                        )
     519                ),
     520        } );
    495521}
    496522
Note: See TracChangeset for help on using the changeset viewer.