| 1 | // Resume Across Fixup
 | 
|---|
| 2 | #include <clock.hfa>
 | 
|---|
| 3 | #include <fstream.hfa>
 | 
|---|
| 4 | #include <stdlib.hfa>
 | 
|---|
| 5 | 
 | 
|---|
| 6 | // Using a global value to allow hoisting (and avoid thunks).
 | 
|---|
| 7 | unsigned int frames;
 | 
|---|
| 8 | 
 | 
|---|
| 9 | void nounwind_fixup(unsigned int dummy, void (*raised_rtn)(int &), void (*not_raised_rtn)(int &)) {
 | 
|---|
| 10 |         void not_raised(int & fixup) {
 | 
|---|
| 11 |                 fixup = frames + 42;
 | 
|---|
| 12 |         }
 | 
|---|
| 13 | 
 | 
|---|
| 14 |         if (frames) {
 | 
|---|
| 15 |                 frames -= 1;
 | 
|---|
| 16 |                 nounwind_fixup(42, raised_rtn, not_raised);
 | 
|---|
| 17 |                 // Always false, but prevents recursion elimination.
 | 
|---|
| 18 |                 if (-1 == frames) printf("~");
 | 
|---|
| 19 |         } else {
 | 
|---|
| 20 |                 int fixup = dummy;
 | 
|---|
| 21 |                 raised_rtn(fixup);
 | 
|---|
| 22 |         }
 | 
|---|
| 23 | }
 | 
|---|
| 24 | 
 | 
|---|
| 25 | int main(int argc, char * argv[]) {
 | 
|---|
| 26 |         unsigned int times = 1;
 | 
|---|
| 27 |         unsigned int total_frames = 1;
 | 
|---|
| 28 |         if (1 < argc) {
 | 
|---|
| 29 |                 times = strto(argv[1], 0p, 10);
 | 
|---|
| 30 |         }
 | 
|---|
| 31 |         if (2 < argc) {
 | 
|---|
| 32 |                 total_frames = strto(argv[2], 0p, 10);
 | 
|---|
| 33 |         }
 | 
|---|
| 34 |         frames = total_frames;
 | 
|---|
| 35 | 
 | 
|---|
| 36 |         // Closures at the top level are allowed to be true closures.
 | 
|---|
| 37 |         void raised(int & fixup) {
 | 
|---|
| 38 |                 fixup = total_frames + 42;
 | 
|---|
| 39 |         }
 | 
|---|
| 40 |         void not_raised(int & fixup) {
 | 
|---|
| 41 |                 fixup = total_frames + 42;
 | 
|---|
| 42 |         }
 | 
|---|
| 43 | 
 | 
|---|
| 44 |         Time start_time = timeHiRes();
 | 
|---|
| 45 |         for (int count = 0 ; count < times ; ++count) {
 | 
|---|
| 46 |                 nounwind_fixup(42, raised, not_raised);
 | 
|---|
| 47 |         }
 | 
|---|
| 48 |         Time end_time = timeHiRes();
 | 
|---|
| 49 |         sout | "Run-Time (s): " | wd(0,1, (end_time - start_time)`ns / 1_000_000_000.);
 | 
|---|
| 50 | }
 | 
|---|