source: doc/theses/andrew_beach_MMath/code/fixup-other-r.cfa@ 49b3389

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since 49b3389 was 18783b4, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

attempt to remove tail-recursion optimization

  • Property mode set to 100644
File size: 1.3 KB
Line 
1// Resume Across Other Handler
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;
11exception not_raised_exception {
12 int & fixup;
13};
14
15unsigned int frames; // use global because of gcc thunk problem
16
17void nounwind_other(unsigned int dummy) {
18 if (frames) {
19 frames -= 1;
20 try {
21 nounwind_other(42);
22 if ( frames == -1 ) printf( "42" ); // prevent recursion optimizations
23 } catchResume (not_raised_exception * ex) {
24 ex->fixup = frames + 42; // use local scope => lexical link
25 }
26 } else {
27 int fixup = dummy;
28 throwResume (fixup_exception){&fixup_vt, fixup}; // change bad fixup
29 }
30}
31
32int main(int argc, char * argv[]) {
33 unsigned int times = 1;
34 unsigned int total_frames = 1;
35 if (1 < argc) {
36 times = strto(argv[1], 0p, 10);
37 }
38 if (2 < argc) {
39 total_frames = strto(argv[2], 0p, 10);
40 }
41 frames = total_frames;
42
43 Time start_time = timeHiRes();
44 for (int count = 0 ; count < times ; ++count) {
45 try {
46 nounwind_other(42);
47 } catchResume (fixup_exception * ex) {
48 ex->fixup = total_frames + 42;
49 }
50 }
51 Time end_time = timeHiRes();
52 sout | "Run-Time (s): " | wd(0,1, (end_time - start_time)`ns / 1_000_000_000.);
53}
Note: See TracBrowser for help on using the repository browser.