Ignore:
Timestamp:
Dec 5, 2017, 2:35:03 PM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
f9feab8
Parents:
9c35431 (diff), 65197c2 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into cleanup-dtors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/exception.c

    r9c35431 rc13e8dc8  
    2323#include <stdio.h>
    2424#include <unwind.h>
    25 #include <libhdr/libdebug.h>
     25#include <bits/debug.h>
    2626
    2727// FIX ME: temporary hack to keep ARM build working
     
    3737
    3838// Base exception vtable is abstract, you should not have base exceptions.
    39 struct __cfaehm__base_exception_t_vtable
    40                 ___cfaehm__base_exception_t_vtable_instance = {
     39struct __cfaabi_ehm__base_exception_t_vtable
     40                ___cfaabi_ehm__base_exception_t_vtable_instance = {
    4141        .parent = NULL,
    4242        .size = 0,
     
    4949// Temperary global exception context. Does not work with concurency.
    5050struct exception_context_t {
    51     struct __cfaehm__try_resume_node * top_resume;
    52     struct __cfaehm__try_resume_node * current_resume;
     51    struct __cfaabi_ehm__try_resume_node * top_resume;
     52    struct __cfaabi_ehm__try_resume_node * current_resume;
    5353
    5454    exception * current_exception;
     
    7878// RESUMPTION ================================================================
    7979
    80 void __cfaehm__throw_resume(exception * except) {
    81 
    82         LIB_DEBUG_PRINT_SAFE("Throwing resumption exception\n");
    83 
    84         struct __cfaehm__try_resume_node * original_head = shared_stack.current_resume;
    85         struct __cfaehm__try_resume_node * current =
     80void __cfaabi_ehm__throw_resume(exception * except) {
     81
     82        __cfaabi_dbg_print_safe("Throwing resumption exception\n");
     83
     84        struct __cfaabi_ehm__try_resume_node * original_head = shared_stack.current_resume;
     85        struct __cfaabi_ehm__try_resume_node * current =
    8686                (original_head) ? original_head->next : shared_stack.top_resume;
    8787
     
    9494        }
    9595
    96         LIB_DEBUG_PRINT_SAFE("Unhandled exception\n");
     96        __cfaabi_dbg_print_safe("Unhandled exception\n");
    9797        shared_stack.current_resume = original_head;
    9898
    9999        // Fall back to termination:
    100         __cfaehm__throw_terminate(except);
     100        __cfaabi_ehm__throw_terminate(except);
    101101        // TODO: Default handler for resumption.
    102102}
     
    105105// hook has to be added after the node is built but before it is made the top node.
    106106
    107 void __cfaehm__try_resume_setup(struct __cfaehm__try_resume_node * node,
     107void __cfaabi_ehm__try_resume_setup(struct __cfaabi_ehm__try_resume_node * node,
    108108                        _Bool (*handler)(exception * except)) {
    109109        node->next = shared_stack.top_resume;
     
    112112}
    113113
    114 void __cfaehm__try_resume_cleanup(struct __cfaehm__try_resume_node * node) {
     114void __cfaabi_ehm__try_resume_cleanup(struct __cfaabi_ehm__try_resume_node * node) {
    115115        shared_stack.top_resume = node->next;
    116116}
     
    122122// May have to move to cfa for constructors and destructors (references).
    123123
    124 struct __cfaehm__node {
    125         struct __cfaehm__node * next;
     124struct __cfaabi_ehm__node {
     125        struct __cfaabi_ehm__node * next;
    126126};
    127127
    128128#define NODE_TO_EXCEPT(node) ((exception *)(1 + (node)))
    129 #define EXCEPT_TO_NODE(except) ((struct __cfaehm__node *)(except) - 1)
     129#define EXCEPT_TO_NODE(except) ((struct __cfaabi_ehm__node *)(except) - 1)
    130130
    131131// Creates a copy of the indicated exception and sets current_exception to it.
    132 static void __cfaehm__allocate_exception( exception * except ) {
     132static void __cfaabi_ehm__allocate_exception( exception * except ) {
    133133        struct exception_context_t * context = this_exception_context();
    134134
    135135        // Allocate memory for the exception.
    136         struct __cfaehm__node * store = malloc(
    137                 sizeof( struct __cfaehm__node ) + except->virtual_table->size );
     136        struct __cfaabi_ehm__node * store = malloc(
     137                sizeof( struct __cfaabi_ehm__node ) + except->virtual_table->size );
    138138
    139139        if ( ! store ) {
     
    151151
    152152// Delete the provided exception, unsetting current_exception if relivant.
    153 static void __cfaehm__delete_exception( exception * except ) {
     153static void __cfaabi_ehm__delete_exception( exception * except ) {
    154154        struct exception_context_t * context = this_exception_context();
    155155
    156         LIB_DEBUG_PRINT_SAFE("Deleting Exception\n");
     156        __cfaabi_dbg_print_safe("Deleting Exception\n");
    157157
    158158        // Remove the exception from the list.
    159         struct __cfaehm__node * to_free = EXCEPT_TO_NODE(except);
    160         struct __cfaehm__node * node;
     159        struct __cfaabi_ehm__node * to_free = EXCEPT_TO_NODE(except);
     160        struct __cfaabi_ehm__node * node;
    161161
    162162        if ( context->current_exception == except ) {
     
    178178
    179179// If this isn't a rethrow (*except==0), delete the provided exception.
    180 void __cfaehm__cleanup_terminate( void * except ) {
    181         if ( *(void**)except ) __cfaehm__delete_exception( *(exception**)except );
     180void __cfaabi_ehm__cleanup_terminate( void * except ) {
     181        if ( *(void**)except ) __cfaabi_ehm__delete_exception( *(exception**)except );
    182182}
    183183
     
    202202
    203203// The exception that is being thrown must already be stored.
    204 __attribute__((noreturn)) void __cfaehm__begin_unwind(void) {
     204__attribute__((noreturn)) void __cfaabi_ehm__begin_unwind(void) {
    205205        if ( ! this_exception_context()->current_exception ) {
    206206                printf("UNWIND ERROR missing exception in begin unwind\n");
     
    233233}
    234234
    235 void __cfaehm__throw_terminate( exception * val ) {
    236         LIB_DEBUG_PRINT_SAFE("Throwing termination exception\n");
    237 
    238         __cfaehm__allocate_exception( val );
    239         __cfaehm__begin_unwind();
    240 }
    241 
    242 void __cfaehm__rethrow_terminate(void) {
    243         LIB_DEBUG_PRINT_SAFE("Rethrowing termination exception\n");
    244 
    245         __cfaehm__begin_unwind();
     235void __cfaabi_ehm__throw_terminate( exception * val ) {
     236        __cfaabi_dbg_print_safe("Throwing termination exception\n");
     237
     238        __cfaabi_ehm__allocate_exception( val );
     239        __cfaabi_ehm__begin_unwind();
     240}
     241
     242void __cfaabi_ehm__rethrow_terminate(void) {
     243        __cfaabi_dbg_print_safe("Rethrowing termination exception\n");
     244
     245        __cfaabi_ehm__begin_unwind();
    246246}
    247247
     
    254254{
    255255
    256         //LIB_DEBUG_PRINT_SAFE("CFA: 0x%lx\n", _Unwind_GetCFA(context));
    257         LIB_DEBUG_PRINT_SAFE("Personality function (%d, %x, %llu, %p, %p):", version, actions, exceptionClass, unwind_exception, context);
     256        //__cfaabi_dbg_print_safe("CFA: 0x%lx\n", _Unwind_GetCFA(context));
     257        __cfaabi_dbg_print_safe("Personality function (%d, %x, %llu, %p, %p):", version, actions, exceptionClass, unwind_exception, context);
    258258
    259259        // If we've reached the end of the stack then there is nothing much we can do...
     
    261261
    262262        if (actions & _UA_SEARCH_PHASE) {
    263                 LIB_DEBUG_PRINT_SAFE(" lookup phase");
     263                __cfaabi_dbg_print_safe(" lookup phase");
    264264        }
    265265        else if (actions & _UA_CLEANUP_PHASE) {
    266                 LIB_DEBUG_PRINT_SAFE(" cleanup phase");
     266                __cfaabi_dbg_print_safe(" cleanup phase");
    267267        }
    268268        // Just in case, probably can't actually happen
     
    307307                        void * ep = (void*)lsd_info.Start + callsite_start + callsite_len;
    308308                        void * ip = (void*)instruction_ptr;
    309                         LIB_DEBUG_PRINT_SAFE("\nfound %p - %p (%p, %p, %p), looking for %p\n", bp, ep, ls, cs, cl, ip);
     309                        __cfaabi_dbg_print_safe("\nfound %p - %p (%p, %p, %p), looking for %p\n", bp, ep, ls, cs, cl, ip);
    310310#endif // __CFA_DEBUG_PRINT__
    311311                        continue;
     
    346346
    347347                                        // Get a function pointer from the relative offset and call it
    348                                         // _Unwind_Reason_Code (*matcher)() = (_Unwind_Reason_Code (*)())lsd_info.LPStart + imatcher;                                   
     348                                        // _Unwind_Reason_Code (*matcher)() = (_Unwind_Reason_Code (*)())lsd_info.LPStart + imatcher;
    349349
    350350                                        _Unwind_Reason_Code (*matcher)(exception *) =
     
    357357                                        // Based on the return value, check if we matched the exception
    358358                                        if( ret == _URC_HANDLER_FOUND) {
    359                                                 LIB_DEBUG_PRINT_SAFE(" handler found\n");
     359                                                __cfaabi_dbg_print_safe(" handler found\n");
    360360                                        } else {
    361                                                 LIB_DEBUG_PRINT_SAFE(" no handler\n");
     361                                                __cfaabi_dbg_print_safe(" no handler\n");
    362362                                        }
    363363                                        return ret;
     
    365365
    366366                                // This is only a cleanup handler, ignore it
    367                                 LIB_DEBUG_PRINT_SAFE(" no action");
     367                                __cfaabi_dbg_print_safe(" no action");
    368368                        }
    369369                        else if (actions & _UA_CLEANUP_PHASE) {
     
    385385                                _Unwind_SetIP( context, ((lsd_info.LPStart) + (callsite_landing_pad)) );
    386386
    387                                 LIB_DEBUG_PRINT_SAFE(" action\n");
     387                                __cfaabi_dbg_print_safe(" action\n");
    388388
    389389                                // Return have some action to run
     
    393393
    394394                // Nothing to do, move along
    395                 LIB_DEBUG_PRINT_SAFE(" no landing pad");
     395                __cfaabi_dbg_print_safe(" no landing pad");
    396396        }
    397397        // No handling found
    398         LIB_DEBUG_PRINT_SAFE(" table end reached\n");
     398        __cfaabi_dbg_print_safe(" table end reached\n");
    399399
    400400        UNWIND:
    401         LIB_DEBUG_PRINT_SAFE(" unwind\n");
     401        __cfaabi_dbg_print_safe(" unwind\n");
    402402
    403403        // Keep unwinding the stack
     
    408408// libcfa but there is one problem left, see the exception table for details
    409409__attribute__((noinline))
    410 void __cfaehm__try_terminate(void (*try_block)(),
     410void __cfaabi_ehm__try_terminate(void (*try_block)(),
    411411                void (*catch_block)(int index, exception * except),
    412412                __attribute__((unused)) int (*match_block)(exception * except)) {
     
    466466        // Body uses language specific data and therefore could be modified arbitrarily
    467467        ".LLSDACSBCFA2:\n"                                              // BODY start
    468         "       .uleb128 .TRYSTART-__cfaehm__try_terminate\n"           // Handled area start  (relative to start of function)
     468        "       .uleb128 .TRYSTART-__cfaabi_ehm__try_terminate\n"               // Handled area start  (relative to start of function)
    469469        "       .uleb128 .TRYEND-.TRYSTART\n"                           // Handled area length
    470         "       .uleb128 .CATCH-__cfaehm__try_terminate\n"                              // Hanlder landing pad adress  (relative to start of function)
     470        "       .uleb128 .CATCH-__cfaabi_ehm__try_terminate\n"                          // Hanlder landing pad adress  (relative to start of function)
    471471        "       .uleb128 1\n"                                           // Action code, gcc seems to use always 0
    472472        ".LLSDACSECFA2:\n"                                              // BODY end
    473473        "       .text\n"                                                        // TABLE footer
    474         "       .size   __cfaehm__try_terminate, .-__cfaehm__try_terminate\n"
     474        "       .size   __cfaabi_ehm__try_terminate, .-__cfaabi_ehm__try_terminate\n"
    475475        "       .ident  \"GCC: (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901\"\n"
    476476//      "       .section        .note.GNU-stack,\"x\",@progbits\n"
Note: See TracChangeset for help on using the changeset viewer.