# HG changeset patch # User Kaito Tokumori # Date 1384223559 -32400 # Node ID dee9711aeb06f01cfbc3e2fc881d8ae784dfabb7 the first commit diff -r 000000000000 -r dee9711aeb06 factorial.c --- /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 +#include +#include + +__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); +} diff -r 000000000000 -r dee9711aeb06 global_longjump.c --- /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 +#include +#include + +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; +} diff -r 000000000000 -r dee9711aeb06 longjump.c --- /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 +#include +#include + +__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; +} diff -r 000000000000 -r dee9711aeb06 pointer_longjump.c --- /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 +#include +#include + +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; +}