Mercurial > hg > Members > kaito > longjump
changeset 0:dee9711aeb06
the first commit
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 12 Nov 2013 11:32:39 +0900 |
parents | |
children | 6d11ed2a5bed |
files | factorial.c global_longjump.c longjump.c pointer_longjump.c |
diffstat | 4 files changed, 161 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/factorial.c Tue Nov 12 11:32:39 2013 +0900 @@ -0,0 +1,45 @@ +#include <stdio.h> +#include <stdlib.h> +#include <setjmp.h> + +__code print_factorial(int prod) +{ + printf("factorial = %d\n",prod); + exit(0); +} + +__code factorial0(int prod, int x) +{ + if ( x >= 1) { + goto factorial0(prod*x, x-1); + }else{ + goto print_factorial(prod); + } + +} + +int factorial(int x) +{ + void *__return; + void *__enviroment; + printf("factorial entry\n"); + __enviroment = (void*)malloc(sizeof(jmp_buf)); + if (setjmp(__enviroment)){ + free(__enviroment); + printf("factorial return\n"); + return 0; + } + __return = (void*)return1; + goto factorial0(1, x); + return -1; +} + +int +main(int argc, char **argv) +{ + int i,ans; + i = atoi(argv[1]); + + ans = factorial(i); + printf("answer = %d\n",ans); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/global_longjump.c Tue Nov 12 11:32:39 2013 +0900 @@ -0,0 +1,40 @@ +#include <stdio.h> +#include <setjmp.h> +#include <stdlib.h> + +int retval; +__code code1(int n,void *__return,void *__enviroment){ + void(*ret)(int,void *); + printf("code1\n"); + ret = (void(*)(int,void *))__return; + ret(n,__enviroment); +} + +void *return1 (int n,void* env){ + printf("return1\n"); + retval = n; + longjmp(*(jmp_buf*)env,1); +} + +int main1 (){ + void *__return; + void *__enviroment; + printf("main1 entry\n"); + __enviroment = (void*)malloc(sizeof(jmp_buf)); + if (setjmp(__enviroment)){ + free(__enviroment); + printf("main1 return\n"); + return retval; + } + __return = (void*)return1; + goto code1(30,__return,__enviroment); + return 0; +} + +int main (){ + int n; + n = main1(); + printf("returned\n"); + printf("return = %d\n",n); + return 1; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/longjump.c Tue Nov 12 11:32:39 2013 +0900 @@ -0,0 +1,36 @@ +#include <stdio.h> +#include <setjmp.h> +#include <stdlib.h> + +__attribute__((noinline)) +__code code1(void *__return,void *__enviroment){ + void(*ret)(void *); + printf("code1\n"); + ret = (void(*)(void *))__return; + ret(__enviroment); +} + +void *return1 (void* env){ + printf("return1\n"); + longjmp(*(jmp_buf*)env,1); +} + +void main1 (){ + void *__return; + void *__enviroment; + printf("main1 entry\n"); + __enviroment = (void*)malloc(sizeof(jmp_buf)); + if (setjmp(__enviroment)){ + free(__enviroment); + printf("main1 return\n"); + return; + } + __return = (void*)return1; + goto code1(__return,__enviroment); +} + +int main (){ + main1(); + printf("returned\n"); + return 1; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pointer_longjump.c Tue Nov 12 11:32:39 2013 +0900 @@ -0,0 +1,40 @@ +#include <stdio.h> +#include <setjmp.h> +#include <stdlib.h> + +int retval; +__code code1(int n,void *__return,void *__enviroment){ + void(*ret)(int,void *); + printf("code1\n"); + ret = (void(*)(int,void *))__return; + ret(n,__enviroment); +} + +void *return1 (int n,void* env){ + printf("return1\n"); + retval = n; + longjmp(*(jmp_buf*)env,1); +} + +int main1 (){ + void *__return; + void *__enviroment; + printf("main1 entry\n"); + __enviroment = (void*)malloc(sizeof(jmp_buf)); + if (setjmp(__enviroment)){ + free(__enviroment); + printf("main1 return\n"); + return retval; + } + __return = (void*)return1; + goto code1(30,__return,__enviroment); + return 0; +} + +int main (){ + int n; + n = main1(); + printf("returned\n"); + printf("return = %d\n",n); + return 1; +}