| [fa4805f] | 1 | // | 
|---|
|  | 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo | 
|---|
|  | 3 | // | 
|---|
|  | 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
|  | 5 | // file "LICENCE" distributed with Cforall. | 
|---|
|  | 6 | // | 
|---|
| [c960331] | 7 | // exception.h -- Internal exception handling definitions. | 
|---|
| [fa4805f] | 8 | // | 
|---|
|  | 9 | // Author           : Andrew Beach | 
|---|
|  | 10 | // Created On       : Mon Jun 26 15:11:00 2017 | 
|---|
| [8a97248] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
|  | 12 | // Last Modified On : Thu Feb  2 11:20:19 2023 | 
|---|
|  | 13 | // Update Count     : 13 | 
|---|
| [fa4805f] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [6b0b624] | 16 | #pragma once | 
|---|
| [fa4805f] | 17 |  | 
|---|
| [c960331] | 18 | // This could be considered several headers. All are internal to the exception | 
|---|
|  | 19 | // system but needed to depending on whether they are C/Cforall code and | 
|---|
|  | 20 | // whether or not they are part of the builtins. | 
|---|
| [fa4805f] | 21 |  | 
|---|
| [0cf5b79] | 22 | #ifdef __cforall | 
|---|
| [0fc9756b] | 23 | extern "C" { | 
|---|
| [fa4805f] | 24 | #endif | 
|---|
|  | 25 |  | 
|---|
| [c960331] | 26 | // Included in C code or the built-ins. | 
|---|
|  | 27 | #if !defined(__cforall) || defined(__cforall_builtins__) | 
|---|
|  | 28 |  | 
|---|
| [3090127] | 29 | struct __cfaehm_base_exception_t; | 
|---|
|  | 30 | typedef struct __cfaehm_base_exception_t exception_t; | 
|---|
| [8f910430] | 31 | struct __cfavir_type_info; | 
|---|
| [3090127] | 32 | struct __cfaehm_base_exception_t_vtable { | 
|---|
| [8f910430] | 33 | const struct __cfavir_type_info * __cfavir_typeid; | 
|---|
| [86d5ba7c] | 34 | size_t size; | 
|---|
| [3090127] | 35 | void (*copy)(struct __cfaehm_base_exception_t *this, | 
|---|
|  | 36 | struct __cfaehm_base_exception_t * other); | 
|---|
|  | 37 | void (*free)(struct __cfaehm_base_exception_t *this); | 
|---|
|  | 38 | const char * (*msg)(struct __cfaehm_base_exception_t *this); | 
|---|
| [86d5ba7c] | 39 | }; | 
|---|
| [3090127] | 40 | struct __cfaehm_base_exception_t { | 
|---|
|  | 41 | struct __cfaehm_base_exception_t_vtable const * virtual_table; | 
|---|
| [86d5ba7c] | 42 | }; | 
|---|
| [8f910430] | 43 | extern struct __cfavir_type_info __cfatid_exception_t; | 
|---|
| [86d5ba7c] | 44 |  | 
|---|
|  | 45 |  | 
|---|
| [b7898ac] | 46 | struct __cfaehm_base_exception_t * __cfaehm_get_current_termination(void); | 
|---|
|  | 47 |  | 
|---|
|  | 48 |  | 
|---|
| [979df46] | 49 | void __cfaehm_cancel_stack(exception_t * except) __attribute__((noreturn)); | 
|---|
|  | 50 |  | 
|---|
| [fa4805f] | 51 | // Used in throw statement translation. | 
|---|
| [046a890] | 52 | void __cfaehm_throw_terminate(exception_t * except, void (*)(exception_t *)); | 
|---|
| [3090127] | 53 | void __cfaehm_rethrow_terminate() __attribute__((noreturn)); | 
|---|
| [046a890] | 54 | void __cfaehm_throw_resume(exception_t * except, void (*)(exception_t *)); | 
|---|
| [fa4805f] | 55 |  | 
|---|
| [57fd66d] | 56 | // Used in non-local ehm (see coroutine.cfa) | 
|---|
|  | 57 | void __cfaehm_allocate_exception( exception_t * except ); | 
|---|
|  | 58 | void __cfaehm_begin_unwind(void(*defaultHandler)(exception_t *)); | 
|---|
|  | 59 |  | 
|---|
|  | 60 |  | 
|---|
| [fa4805f] | 61 | // Function catches termination exceptions. | 
|---|
| [b7898ac] | 62 | int __cfaehm_try_terminate( | 
|---|
| [c960331] | 63 | void (*try_block)(), | 
|---|
|  | 64 | int (*match_block)(exception_t * except)); | 
|---|
| [fa4805f] | 65 |  | 
|---|
| [86d5ba7c] | 66 | // Clean-up the exception in catch blocks. | 
|---|
| [3090127] | 67 | void __cfaehm_cleanup_terminate(void * except); | 
|---|
| [86d5ba7c] | 68 |  | 
|---|
| [fa4805f] | 69 | // Data structure creates a list of resume handlers. | 
|---|
| [3090127] | 70 | struct __cfaehm_try_resume_node { | 
|---|
| [c960331] | 71 | struct __cfaehm_try_resume_node * next; | 
|---|
|  | 72 | _Bool (*handler)(exception_t * except); | 
|---|
| [fa4805f] | 73 | }; | 
|---|
|  | 74 |  | 
|---|
| [86d5ba7c] | 75 | // These act as constructor and destructor for the resume node. | 
|---|
| [3090127] | 76 | void __cfaehm_try_resume_setup( | 
|---|
| [c960331] | 77 | struct __cfaehm_try_resume_node * node, | 
|---|
|  | 78 | _Bool (*handler)(exception_t * except)); | 
|---|
| [3090127] | 79 | void __cfaehm_try_resume_cleanup( | 
|---|
| [c960331] | 80 | struct __cfaehm_try_resume_node * node); | 
|---|
| [fa4805f] | 81 |  | 
|---|
|  | 82 | // Check for a standard way to call fake deconstructors. | 
|---|
| [3090127] | 83 | struct __cfaehm_cleanup_hook {}; | 
|---|
| [fa4805f] | 84 |  | 
|---|
| [c960331] | 85 | #endif | 
|---|
|  | 86 |  | 
|---|
|  | 87 | // Included in C code and the library. | 
|---|
|  | 88 | #if !defined(__cforall) || !defined(__cforall_builtins__) | 
|---|
|  | 89 | struct __cfaehm_node { | 
|---|
|  | 90 | struct _Unwind_Exception unwind_exception; | 
|---|
|  | 91 | struct __cfaehm_node * next; | 
|---|
|  | 92 | int handler_index; | 
|---|
|  | 93 | }; | 
|---|
|  | 94 |  | 
|---|
|  | 95 | static inline exception_t * __cfaehm_cancellation_exception( | 
|---|
|  | 96 | struct _Unwind_Exception * unwind_exception ) { | 
|---|
|  | 97 | return (exception_t *)(1 + (struct __cfaehm_node *)unwind_exception); | 
|---|
|  | 98 | } | 
|---|
|  | 99 | #endif | 
|---|
|  | 100 |  | 
|---|
| [0cf5b79] | 101 | #ifdef __cforall | 
|---|
| [fa4805f] | 102 | } | 
|---|
| [046a890] | 103 |  | 
|---|
| [c960331] | 104 | // Built-ins not visible in C. | 
|---|
|  | 105 | #if defined(__cforall_builtins__) | 
|---|
|  | 106 |  | 
|---|
| [046a890] | 107 | // Not all the built-ins can be expressed in C. These can't be | 
|---|
|  | 108 | // implemented in the .c file either so they all have to be inline. | 
|---|
|  | 109 |  | 
|---|
| [8a97248] | 110 | forall( exceptT &, virtualT & ) | 
|---|
|  | 111 | trait is_exception { | 
|---|
| [046a890] | 112 | /* The first field must be a pointer to a virtual table. | 
|---|
| [69c5c00] | 113 | * That virtual table must be a decendent of the base exception virtual table. | 
|---|
| [ecfd758] | 114 | * The virtual table must point at the prober type-id. | 
|---|
|  | 115 | * None of these can be enforced in an assertion. | 
|---|
| [046a890] | 116 | */ | 
|---|
|  | 117 | }; | 
|---|
|  | 118 |  | 
|---|
| [8a97248] | 119 | forall( exceptT &, virtualT & | is_exception(exceptT, virtualT) ) | 
|---|
|  | 120 | trait is_termination_exception { | 
|---|
| [1c01c58] | 121 | void defaultTerminationHandler(exceptT &); | 
|---|
| [046a890] | 122 | }; | 
|---|
|  | 123 |  | 
|---|
| [8a97248] | 124 | forall( exceptT &, virtualT & | is_exception(exceptT, virtualT) ) | 
|---|
|  | 125 | trait is_resumption_exception { | 
|---|
| [1c01c58] | 126 | void defaultResumptionHandler(exceptT &); | 
|---|
| [046a890] | 127 | }; | 
|---|
|  | 128 |  | 
|---|
| [fd54fef] | 129 | forall(exceptT &, virtualT & | is_termination_exception(exceptT, virtualT)) | 
|---|
| [1c01c58] | 130 | static inline void $throw(exceptT & except) { | 
|---|
| [046a890] | 131 | __cfaehm_throw_terminate( | 
|---|
|  | 132 | (exception_t *)&except, | 
|---|
|  | 133 | (void(*)(exception_t *))defaultTerminationHandler | 
|---|
|  | 134 | ); | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
| [fd54fef] | 137 | forall(exceptT &, virtualT & | is_resumption_exception(exceptT, virtualT)) | 
|---|
| [1c01c58] | 138 | static inline void $throwResume(exceptT & except) { | 
|---|
| [046a890] | 139 | __cfaehm_throw_resume( | 
|---|
|  | 140 | (exception_t *)&except, | 
|---|
|  | 141 | (void(*)(exception_t *))defaultResumptionHandler | 
|---|
|  | 142 | ); | 
|---|
|  | 143 | } | 
|---|
|  | 144 |  | 
|---|
| [fd54fef] | 145 | forall(exceptT &, virtualT & | is_exception(exceptT, virtualT)) | 
|---|
| [1c01c58] | 146 | static inline void cancel_stack(exceptT & except) __attribute__((noreturn)) { | 
|---|
| [046a890] | 147 | __cfaehm_cancel_stack( (exception_t *)&except ); | 
|---|
|  | 148 | } | 
|---|
|  | 149 |  | 
|---|
| [fd54fef] | 150 | forall(exceptT &, virtualT & | is_exception(exceptT, virtualT)) | 
|---|
| [1c01c58] | 151 | static inline void defaultTerminationHandler(exceptT & except) { | 
|---|
| [046a890] | 152 | return cancel_stack( except ); | 
|---|
|  | 153 | } | 
|---|
|  | 154 |  | 
|---|
| [78de1e5] | 155 | forall(exceptT &, virtualT & | is_termination_exception(exceptT, virtualT)) | 
|---|
| [1c01c58] | 156 | static inline void defaultResumptionHandler(exceptT & except) { | 
|---|
| [046a890] | 157 | throw except; | 
|---|
|  | 158 | } | 
|---|
|  | 159 |  | 
|---|
| [fa4805f] | 160 | #endif | 
|---|
| [c960331] | 161 |  | 
|---|
|  | 162 | #endif | 
|---|