source: doc/theses/andrew_beach_MMath/code/fixup-empty-r.cfa @ 18783b4

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 18783b4 was 18783b4, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

attempt to remove tail-recursion optimization

  • Property mode set to 100644
File size: 1.0 KB
Line 
1// Resume Across Empty Function
2#include <clock.hfa>
3#include <exception.hfa>
4#include <fstream.hfa>
5#include <stdlib.hfa>                                                                   // strto
6
7exception fixup_exception {
8        int & fixup;
9};
10vtable(fixup_exception) fixup_vt;
11
12void nounwind_empty(unsigned int frames) {
13        if (frames) {
14                nounwind_empty(frames - 1);
15                if ( frames == -1 ) printf( "42" );                             // prevent recursion optimizations
16        } else {
17                int fixup = 17;
18                throwResume (fixup_exception){&fixup_vt, fixup}; // change bad fixup
19        }
20}
21
22int main(int argc, char * argv[]) {
23        unsigned int times = 1;
24        unsigned int total_frames = 1;
25        if (1 < argc) {
26                times = strto(argv[1], 0p, 10);
27        }
28        if (2 < argc) {
29                total_frames = strto(argv[2], 0p, 10);
30        }
31
32        Time start_time = timeHiRes();
33        for (unsigned int count = 0 ; count < times ; ++count) {
34                try {
35                        nounwind_empty(total_frames);
36                } catchResume (fixup_exception * ex) {
37                        ex->fixup = total_frames + 42;
38                }
39        }
40        Time end_time = timeHiRes();
41        sout | "Run-Time (s): " | wd(0,1, (end_time - start_time)`ns / 1_000_000_000.);
42}
Note: See TracBrowser for help on using the repository browser.