Changeset 2a3b019


Ignore:
Timestamp:
Mar 25, 2020, 4:36:25 PM (5 years ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
9cb89b87
Parents:
44a88528
Message:

Cleaning exception code: no direct access to shared_stack anymore, small reorganization that fixes debug print.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/exception.c

    r44a88528 r2a3b019  
    6363// There can be a single global until multithreading occurs, then each stack
    6464// needs its own. It will have to be updated to handle that.
    65 struct exception_context_t * this_exception_context() {
     65inline static struct exception_context_t * this_exception_context() {
    6666        return &shared_stack;
    6767}
    68 //#define SAVE_EXCEPTION_CONTEXT(to_name)
    69 //struct exception_context_t * to_name = this_exception_context();
    70 //exception * this_exception() {
    71 //    return this_exception_context()->current_exception;
    72 //}
    7368
    7469
     
    7671
    7772void __cfaabi_ehm__throw_resume(exception_t * except) {
     73        struct exception_context_t * context = this_exception_context();
    7874
    7975        __cfaabi_dbg_print_safe("Throwing resumption exception\n");
    8076
    81         struct __cfaabi_ehm__try_resume_node * original_head = shared_stack.current_resume;
     77        struct __cfaabi_ehm__try_resume_node * original_head = context->current_resume;
    8278        struct __cfaabi_ehm__try_resume_node * current =
    83                 (original_head) ? original_head->next : shared_stack.top_resume;
     79                (original_head) ? original_head->next : context->top_resume;
    8480
    8581        for ( ; current ; current = current->next) {
    86                 shared_stack.current_resume = current;
     82                context->current_resume = current;
    8783                if (current->handler(except)) {
    88                         shared_stack.current_resume = original_head;
     84                        context->current_resume = original_head;
    8985                        return;
    9086                }
     
    9288
    9389        __cfaabi_dbg_print_safe("Unhandled exception\n");
    94         shared_stack.current_resume = original_head;
     90        context->current_resume = original_head;
    9591
    9692        // Fall back to termination:
     
    105101void __cfaabi_ehm__try_resume_setup(struct __cfaabi_ehm__try_resume_node * node,
    106102                        _Bool (*handler)(exception_t * except)) {
    107         node->next = shared_stack.top_resume;
     103        struct exception_context_t * context = this_exception_context();
     104        node->next = context->top_resume;
    108105        node->handler = handler;
    109         shared_stack.top_resume = node;
     106        context->top_resume = node;
    110107}
    111108
    112109void __cfaabi_ehm__try_resume_cleanup(struct __cfaabi_ehm__try_resume_node * node) {
    113         shared_stack.top_resume = node->next;
     110        struct exception_context_t * context = this_exception_context();
     111        context->top_resume = node->next;
    114112}
    115113
     
    191189                _Unwind_Exception_Class exceptionClass,
    192190                struct _Unwind_Exception * unwind_exception,
    193                 struct _Unwind_Context * context,
    194                 void * some_param) {
     191                struct _Unwind_Context * unwind_context,
     192                void * stop_param) {
    195193        if( actions & _UA_END_OF_STACK  ) exit(1);
    196194        if( actions & _UA_CLEANUP_PHASE ) return _URC_NO_REASON;
     
    251249                int version, _Unwind_Action actions, unsigned long long exceptionClass,
    252250                struct _Unwind_Exception* unwind_exception,
    253                 struct _Unwind_Context* context)
     251                struct _Unwind_Context* unwind_context)
    254252{
    255253
    256254        //__cfaabi_dbg_print_safe("CFA: 0x%lx\n", _Unwind_GetCFA(context));
    257255        __cfaabi_dbg_print_safe("Personality function (%d, %x, %llu, %p, %p):",
    258                         version, actions, exceptionClass, unwind_exception, context);
     256                        version, actions, exceptionClass, unwind_exception, unwind_context);
    259257
    260258        // If we've reached the end of the stack then there is nothing much we can do...
     
    274272
    275273        // Get a pointer to the language specific data from which we will read what we need
    276         const unsigned char * lsd = (const unsigned char*) _Unwind_GetLanguageSpecificData( context );
     274        const unsigned char * lsd = (const unsigned char*) _Unwind_GetLanguageSpecificData( unwind_context );
    277275
    278276        if( !lsd ) {    //Nothing to do, keep unwinding
     
    283281        // Get the instuction pointer and a reading pointer into the exception table
    284282        lsda_header_info lsd_info;
    285         const unsigned char * cur_ptr = parse_lsda_header(context, lsd, &lsd_info);
    286         _Unwind_Ptr instruction_ptr = _Unwind_GetIP( context );
     283        const unsigned char * cur_ptr = parse_lsda_header(unwind_context, lsd, &lsd_info);
     284        _Unwind_Ptr instruction_ptr = _Unwind_GetIP(unwind_context);
     285
     286        struct exception_context_t * context = this_exception_context();
    287287
    288288        // Linearly search the table for stuff to do
     
    321321
    322322                // Something to do?
    323                 if( callsite_landing_pad ) {
     323                if ( 0 == callsite_landing_pad ) {
     324                        // Nothing to do, move along
     325                        __cfaabi_dbg_print_safe(" no landing pad");
     326                } else {
    324327                        // Which phase are we in
    325328                        if (actions & _UA_SEARCH_PHASE) {
     
    348351
    349352#                                       if defined( __x86_64 )
    350                                         _Unwind_Word match_pos = _Unwind_GetCFA(context) + 8;
     353                                        _Unwind_Word match_pos = _Unwind_GetCFA(unwind_context) + 8;
    351354#                                       elif defined( __i386 )
    352                                         _Unwind_Word match_pos = _Unwind_GetCFA(context) + 24;
     355                                        _Unwind_Word match_pos = _Unwind_GetCFA(unwind_context) + 24;
    353356#                                       endif
    354357                                        int (*matcher)(exception_t *) = *(int(**)(exception_t *))match_pos;
    355358
    356                                         int index = matcher(shared_stack.current_exception);
     359                                        int index = matcher(context->current_exception);
    357360                                        _Unwind_Reason_Code ret = (0 == index)
    358361                                                ? _URC_CONTINUE_UNWIND : _URC_HANDLER_FOUND;
    359                                         shared_stack.current_handler_index = index;
     362                                        context->current_handler_index = index;
    360363
    361364                                        // Based on the return value, check if we matched the exception
     
    382385                                // We need to run some clean-up or a handler
    383386                                // These statment do the right thing but I don't know any specifics at all
    384                                 _Unwind_SetGR( context, __builtin_eh_return_data_regno(0), (_Unwind_Ptr) unwind_exception );
    385                                 _Unwind_SetGR( context, __builtin_eh_return_data_regno(1), 0 );
     387                                _Unwind_SetGR( unwind_context, __builtin_eh_return_data_regno(0),
     388                                        (_Unwind_Ptr)unwind_exception );
     389                                _Unwind_SetGR( unwind_context, __builtin_eh_return_data_regno(1), 0 );
    386390
    387391                                // I assume this sets the instruction pointer to the adress of the landing pad
    388392                                // It doesn't actually set it, it only state the value that needs to be set once we return _URC_INSTALL_CONTEXT
    389                                 _Unwind_SetIP( context, ((lsd_info.LPStart) + (callsite_landing_pad)) );
     393                                _Unwind_SetIP( unwind_context, ((lsd_info.LPStart) + (callsite_landing_pad)) );
    390394
    391395                                __cfaabi_dbg_print_safe(" action\n");
     
    395399                        }
    396400                }
    397 
    398                 // Nothing to do, move along
    399                 __cfaabi_dbg_print_safe(" no landing pad");
    400401        }
    401402        // No handling found
     
    457458
    458459        // Exception handler
    459         catch_block( shared_stack.current_handler_index,
    460                      shared_stack.current_exception );
     460        // Note: Saving the exception context on the stack breaks termination exceptions.
     461        catch_block( this_exception_context()->current_handler_index,
     462                     this_exception_context()->current_exception );
    461463}
    462464
Note: See TracChangeset for help on using the changeset viewer.