source: doc/working/exception/impl/main.c @ 4a58895

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since 4a58895 was 4a58895, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Added a proof of consept resumption exception handler. Has comparable speed to the existing termination handler under O2.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1#include <stdio.h>
2#include "except.h"
3
4#define EXCEPTION 2
5
6struct type_raii_t {
7        char * msg;
8};
9
10//Dtor function to test clean up routines
11void dtor( struct type_raii_t * this ) {
12        printf("%s\n", this->msg);
13}
14
15//Type macro use to make scope unwinding easier to see.
16#define raii_t __attribute__(( cleanup(dtor) )) struct type_raii_t
17
18//Leaf functions that raises exception
19void bar() {
20        raii_t a = { "Bar dtor" };
21
22        throw( EXCEPTION );
23}
24
25//Matcher function which will check if the exception was correctly caught
26extern int this_exception;
27_Unwind_Reason_Code foo_try_match() {
28        return this_exception == 3 ? _URC_HANDLER_FOUND : _URC_CONTINUE_UNWIND;
29}
30
31//Try statements are hoisted out see comments for details
32//With this could probably be unique and simply linked from
33//libcfa but there is one problem left, see the exception table
34//for details
35__attribute__((noinline))
36void try( void (*try_block)(), void (*catch_block)() )
37{
38        //Setup statments
39        //These 2 statments won't actually result in any code,
40        //they only setup global tables.
41        //However, they clobber gcc cancellation support from gcc.
42        //We can replace the personality routine but replacing the exception
43        //table gcc generates is not really doable, it generates labels based
44        //on how the assembly works.
45        //Setup the personality routine
46        asm volatile (".cfi_personality 0x3,__gcfa_personality_v0");
47        //Setup the exception table
48        asm volatile (".cfi_lsda 0x3, .LLSDACFA2");
49
50        //Label which defines the start of the area for which the handler is setup
51        asm volatile (".TRYSTART:");
52
53        //The actual statements of the try blocks
54        try_block();
55
56        //asm statement to prevent deadcode removal
57        asm volatile goto ("" : : : : CATCH );
58
59        //Normal return
60        return;
61
62        //Exceptionnal path
63        CATCH : __attribute__(( unused ));
64        //Label which defines the end of the area for which the handler is setup
65        asm volatile (".TRYEND:");
66        //Label which defines the start of the exception landing pad
67        //basically what will be called when the exception is caught
68        //Note, if multiple handlers are given, the multiplexing should be done
69        //by the generated code, not the exception runtime
70        asm volatile (".CATCH:");
71
72        //Exception handler
73        catch_block();
74}
75
76//Exception table data we need to generate
77//While this is almost generic, the custom data refers to
78//foo_try_match try match, which is no way generic
79//Some more works need to be done if we want to have a single
80//call to the try routine
81asm (
82        //HEADER
83        ".LFECFA1:\n"
84        "       .globl  __gcfa_personality_v0\n"
85        "       .section        .gcc_except_table,\"a\",@progbits\n"
86        ".LLSDACFA2:\n"                                                 //TABLE header
87        "       .byte   0xff\n"
88        "       .byte   0xff\n"
89        "       .byte   0x1\n"
90        "       .uleb128 .LLSDACSECFA2-.LLSDACSBCFA2\n"         //BODY length
91        //Body uses language specific data and therefore could be modified arbitrarily
92        ".LLSDACSBCFA2:\n"                                              //BODY start
93        "       .uleb128 .TRYSTART-try\n"                               //Handled area start  (relative to start of function)
94        "       .uleb128 .TRYEND-.TRYSTART\n"                           //Handled area length
95        "       .uleb128 .CATCH-try\n"                          //Hanlder landing pad adress  (relative to start of function)
96        "       .uleb128 1\n"                                           //Action code, gcc seems to use always 0
97        //Beyond this point we don't match gcc data'
98        "       .uleb128 foo_try_match-try\n"                   //Handler routine to check if the exception is matched
99        ".LLSDACSECFA2:\n"                                              //BODY end
100        "       .text\n"                                                        //TABLE footer
101        "       .size   try, .-try\n"
102        "       .ident  \"GCC: (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901\"\n"
103        "       .section        .note.GNU-stack,\"x\",@progbits\n"
104);
105
106void foo() {
107        raii_t a = { "Foo dtor" };
108
109        //Since try will clobber the gcc exception table assembly,
110        //We need to nest this to have gcc regenerate the data
111        void foo_try_block() {
112                raii_t b = { "Foo try dtor" };
113
114                bar();
115
116                printf("Called bar successfully\n");
117        }
118
119        void foo_catch_block() {
120                printf("Exception caught\n");
121        }
122
123        //Actual call to the try block
124        try( foo_try_block, foo_catch_block );
125
126        printf( "Foo exited normally\n" );
127}
128
129int main() {
130        raii_t a = { "Main dtor" };
131
132        for (unsigned int i = 0 ; i < 100000000 ; ++i) foo();
133
134        printf("End of program reached\n");
135}
Note: See TracBrowser for help on using the repository browser.