ADT
ast-experimental
enum
forall-pointer-decay
jacob/cs343-translation
pthread-emulation
qualifiedEnum
Last change
on this file since ff3be413 was 01f78e0, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago |
add code to prevent tail recursion optimization
|
-
Property mode
set to
100644
|
File size:
994 bytes
|
Rev | Line | |
---|
[f79ee0d] | 1 | // Resume Across Fixup
|
---|
| 2 | #include <clock.hfa>
|
---|
| 3 | #include <fstream.hfa>
|
---|
| 4 | #include <stdlib.hfa> // strto
|
---|
| 5 |
|
---|
[01f78e0] | 6 | int nounwind_fixup(unsigned int frames, void (*raised_rtn)(int &)) {
|
---|
[f79ee0d] | 7 | if (frames) {
|
---|
[01f78e0] | 8 | int rtn = nounwind_fixup(frames - 1, raised_rtn);
|
---|
| 9 | if ( rtn == 42 ) printf( "42" ); // make non-tail recursive
|
---|
| 10 | return rtn;
|
---|
| 11 |
|
---|
[f79ee0d] | 12 | } else {
|
---|
| 13 | int fixup = 17;
|
---|
| 14 | raised_rtn(fixup);
|
---|
[01f78e0] | 15 | return fixup;
|
---|
[f79ee0d] | 16 | }
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | int main(int argc, char * argv[]) {
|
---|
| 20 | unsigned int times = 1;
|
---|
| 21 | unsigned int total_frames = 1;
|
---|
| 22 | if (1 < argc) {
|
---|
| 23 | times = strto(argv[1], 0p, 10);
|
---|
| 24 | }
|
---|
| 25 | if (2 < argc) {
|
---|
| 26 | total_frames = strto(argv[2], 0p, 10);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | void raised(int & fixup) {
|
---|
| 30 | fixup = total_frames + 42; // use local scope => lexical link
|
---|
[01f78e0] | 31 | if ( total_frames == 42 ) printf( "42" );
|
---|
[f79ee0d] | 32 | }
|
---|
| 33 |
|
---|
| 34 | Time start_time = timeHiRes();
|
---|
| 35 | for (unsigned int count = 0 ; count < times ; ++count) {
|
---|
| 36 | nounwind_fixup(total_frames, raised);
|
---|
| 37 | }
|
---|
| 38 | Time end_time = timeHiRes();
|
---|
| 39 | sout | "Run-Time (s): " | wd(0,1, (end_time - start_time)`ns / 1_000_000_000.);
|
---|
| 40 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.