source: doc/theses/andrew_beach_MMath/code/resume-fixup-empty-r.cfa @ 01f78e0

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationpthread-emulationqualifiedEnum
Last change on this file since 01f78e0 was 01f78e0, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

add code to prevent 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
12int nounwind_empty(unsigned int frames) {
13        if (frames) {
14                int rtn = nounwind_empty(frames - 1);
15                if ( rtn == 42 ) printf( "42" );                                // make non-tail recursive
16                return rtn;
17        } else {
18                int fixup = 17;
19                throwResume (fixup_exception){&fixup_vt, fixup}; // change bad fixup
20                return fixup;
21        }
22}
23
24int main(int argc, char * argv[]) {
25        unsigned int times = 1;
26        unsigned int total_frames = 1;
27        if (1 < argc) {
28                times = strto(argv[1], 0p, 10);
29        }
30        if (2 < argc) {
31                total_frames = strto(argv[2], 0p, 10);
32        }
33
34        Time start_time = timeHiRes();
35        for (unsigned int count = 0 ; count < times ; ++count) {
36                try {
37                        nounwind_empty(total_frames);
38                } catchResume (fixup_exception * ex) {
39                        ex->fixup = total_frames + 42;
40                }
41        }
42        Time end_time = timeHiRes();
43        sout | "Run-Time (s): " | wd(0,1, (end_time - start_time)`ns / 1_000_000_000.);
44}
Note: See TracBrowser for help on using the repository browser.