Changeset 046a890 for libcfa


Ignore:
Timestamp:
May 19, 2020, 5:31:20 PM (4 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:
918b90c
Parents:
7d6e01d
Message:

That should get default operations working for throws. More tests to come.

Location:
libcfa/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/exception.c

    r7d6e01d r046a890  
    1010// Created On       : Mon Jun 26 15:13:00 2017
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Tue Apr 14 12:01:00 2020
    13 // Update Count     : 18
     12// Last Modified On : Tue May 19 14:17:00 2020
     13// Update Count     : 19
    1414//
    1515
     
    8080}
    8181
    82 void __cfaehm_throw_resume(exception_t * except) {
     82void __cfaehm_throw_resume(exception_t * except, void (*defaultHandler)(exception_t *)) {
    8383        struct exception_context_t * context = this_exception_context();
    8484
     
    9696        }
    9797
     98        // No handler found, fall back to the default operation.
    9899        __cfadbg_print_safe(exception, "Unhandled exception\n");
    99 
    100         // Fall back to termination:
    101         __cfaehm_throw_terminate(except);
    102         // TODO: Default handler for resumption.
     100        defaultHandler(except);
    103101}
    104102
     
    240238
    241239// The exception that is being thrown must already be stored.
    242 static __attribute__((noreturn)) void __cfaehm_begin_unwind(void) {
     240static void __cfaehm_begin_unwind(void(*defaultHandler)(exception_t *)) {
    243241        if ( ! this_exception_context()->current_exception ) {
    244242                printf("UNWIND ERROR missing exception in begin unwind\n");
     
    257255
    258256        // No handler found, go to the default operation.
    259         // Currently this will always be a cancellation.
    260257        if ( ret == _URC_END_OF_STACK ) {
    261258                __cfadbg_print_safe(exception, "Uncaught exception %p\n", &this_exception_storage);
    262259
    263                 __cfaehm_cancel_stack(this_exception_context()->current_exception);
     260                defaultHandler( this_exception_context()->current_exception );
    264261        }
    265262
     
    269266}
    270267
    271 void __cfaehm_throw_terminate( exception_t * val ) {
     268void __cfaehm_throw_terminate( exception_t * val, void (*defaultHandler)(exception_t *) ) {
    272269        __cfadbg_print_safe(exception, "Throwing termination exception\n");
    273270
    274271        __cfaehm_allocate_exception( val );
    275         __cfaehm_begin_unwind();
     272        __cfaehm_begin_unwind( defaultHandler );
     273}
     274
     275static __attribute__((noreturn)) void __cfaehm_rethrow_adapter( exception_t * except ) {
     276        // TODO: Print some error message.
     277        (void)except;
     278        abort();
    276279}
    277280
     
    279282        __cfadbg_print_safe(exception, "Rethrowing termination exception\n");
    280283
    281         __cfaehm_begin_unwind();
     284        __cfaehm_begin_unwind( __cfaehm_rethrow_adapter );
     285        abort();
    282286}
    283287
  • libcfa/src/exception.h

    r7d6e01d r046a890  
    1010// Created On       : Mon Jun 26 15:11:00 2017
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Fri Mar 27 10:16:00 2020
    13 // Update Count     : 9
     12// Last Modified On : Tue May 19 14:17:00 2020
     13// Update Count     : 10
    1414//
    1515
     
    4141
    4242// Used in throw statement translation.
    43 void __cfaehm_throw_terminate(exception_t * except) __attribute__((noreturn));
     43void __cfaehm_throw_terminate(exception_t * except, void (*)(exception_t *));
    4444void __cfaehm_rethrow_terminate() __attribute__((noreturn));
    45 void __cfaehm_throw_resume(exception_t * except);
     45void __cfaehm_throw_resume(exception_t * except, void (*)(exception_t *));
    4646
    4747// Function catches termination exceptions.
     
    7272#ifdef __cforall
    7373}
     74
     75// Not all the built-ins can be expressed in C. These can't be
     76// implemented in the .c file either so they all have to be inline.
     77
     78trait is_exception(dtype T) {
     79        /* The first field must be a pointer to a virtual table.
     80         * That virtual table must be a decendent of the base exception virtual tab$
     81         */
     82        void mark_exception(T *);
     83        // This is never used and should be a no-op.
     84};
     85
     86trait is_termination_exception(dtype T | is_exception(T)) {
     87        void defaultTerminationHandler(T &);
     88};
     89
     90trait is_resumption_exception(dtype T | is_exception(T)) {
     91        void defaultResumptionHandler(T &);
     92};
     93
     94forall(dtype T | is_termination_exception(T))
     95static inline void $throw(T & except) {
     96        __cfaehm_throw_terminate(
     97                (exception_t *)&except,
     98                (void(*)(exception_t *))defaultTerminationHandler
     99        );
     100}
     101
     102forall(dtype T | is_resumption_exception(T))
     103static inline void $throwResume(T & except) {
     104        __cfaehm_throw_resume(
     105                (exception_t *)&except,
     106                (void(*)(exception_t *))defaultResumptionHandler
     107        );
     108}
     109
     110forall(dtype T | is_exception(T))
     111static inline void cancel_stack(T & except) __attribute__((noreturn)) {
     112        __cfaehm_cancel_stack( (exception_t *)&except );
     113}
     114
     115forall(dtype T | is_exception(T))
     116static inline void defaultTerminationHandler(T & except) {
     117        return cancel_stack( except );
     118}
     119
     120forall(dtype T | is_exception(T))
     121static inline void defaultResumptionHandler(T & except) {
     122        throw except;
     123}
     124
    74125#endif
  • libcfa/src/exception.hfa

    r7d6e01d r046a890  
    1010// Created On       : Thu Apr  7 10:25:00 2020
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Wed Apr 13 15:42:00 2020
    13 // Update Count     : 1
     12// Last Modified On : Tue May 19 14:17:00 2020
     13// Update Count     : 2
    1414//
    15 
    16 trait is_exception(dtype T) {
    17         // The trait system can't describe the actual constrants.
    18         // Unused, should always be a no-op.
    19         void mark_exception(T *);
    20 };
    21 
    22 forall(dtype T | is_exception(T))
    23 inline void cancel_stack(T & except) __attribute__((noreturn)) {
    24         __cfaehm_cancel_stack( (exception_t *)&except );
    25 }
    2615
    2716// Everything below this line should be considered a patch while the exception
Note: See TracChangeset for help on using the changeset viewer.