source: doc/theses/andrew_beach_MMath/code/fixup-other-f.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.2 KB
Line 
1// Resume Across Fixup
2#include <clock.hfa>
3#include <fstream.hfa>
4#include <stdlib.hfa> // strto
5
6unsigned int frames; // use global because of gcc thunk problem
7
8void nounwind_fixup(unsigned int dummy, void (*raised_rtn)(int &), void (*not_raised_rtn)(int &)) {
9 void not_raised(int & fixup) {
10 fixup = frames + 42; // use local scope => lexical link
11 }
12
13 if (frames) {
14 frames -= 1;
15 nounwind_fixup(42, raised_rtn, not_raised);
16 if ( frames == -1 ) printf( "42" ); // prevent recursion optimizations
17 } else {
18 int fixup = dummy;
19 raised_rtn(fixup);
20 }
21}
22
23int main(int argc, char * argv[]) {
24 unsigned int times = 1;
25 unsigned int total_frames = 1;
26 if (1 < argc) {
27 times = strto(argv[1], 0p, 10);
28 }
29 if (2 < argc) {
30 total_frames = strto(argv[2], 0p, 10);
31 }
32 frames = total_frames;
33
34 void raised(int & fixup) {
35 fixup = total_frames + 42; // use local scope => lexical link
36 }
37 void not_raised(int & fixup) {
38 fixup = total_frames + 42; // use local scope => lexical link
39 }
40
41 Time start_time = timeHiRes();
42 for (int count = 0 ; count < times ; ++count) {
43 nounwind_fixup(42, raised, not_raised);
44 }
45 Time end_time = timeHiRes();
46 sout | "Run-Time (s): " | wd(0,1, (end_time - start_time)`ns / 1_000_000_000.);
47}
Note: See TracBrowser for help on using the repository browser.