Mercurial > hg > Members > kaito > longjump
changeset 14:1cd0fad44dac
change goal.c
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 14 Jan 2014 02:01:41 +0900 |
parents | 89d24aeb122e |
children | e0c89847fb78 |
files | goal.c struct_longjump.c |
diffstat | 2 files changed, 37 insertions(+), 30 deletions(-) [+] |
line wrap: on
line diff
--- a/goal.c Sun Jan 12 19:21:18 2014 +0900 +++ b/goal.c Tue Jan 14 02:01:41 2014 +0900 @@ -1,36 +1,43 @@ -#include <stdio.h> -#include <setjmp.h> -#include <stdlib.h> +int setjmp(int*); +int longjmp(int*,int); +int printf(const char*,...); -__attribute__((noinline)) -__code code1(void *__return,void *__enviroment){ - void(*ret)(void *); - printf("code1\n"); - ret = (void(*)(void *))__return; - ret(__enviroment); +struct CbC_env { + void *ret_p,*env; +}; + +__code code1(int n,__code(*__return)(int,void *),void *__environment){ + printf("code1 : code entry1\n"); + goto __return(n,__environment); } -void *return1 (void* env){ - printf("return1\n"); - longjmp(*(jmp_buf*)env,1); +__code return1 (int n,void* env){ + printf("return1 : __return entry\n"); + *(int*)((struct CbC_env *)(env))->ret_p = n; + longjmp((int*)(((struct CbC_env *)env)->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 main1 (){ + __code (*__return)(); + struct CbC_env __environment; + int i_buf[128]; + int retval; + __environment.ret_p = &retval; + __environment.env = &i_buf; + printf("main1 : main1 entry\n"); + if (setjmp(__environment.env)){ + printf("main1 : main1 return\n"); + return *((int *)__environment.ret_p); + } + __return = return1; + goto code1(30,__return,&__environment); + return 0; } int main (){ - main1(); - printf("returned\n"); - return 1; + int n; + n = main1(); + printf("returned\n"); + printf("return = %d\n",n); + return 1; }
--- a/struct_longjump.c Sun Jan 12 19:21:18 2014 +0900 +++ b/struct_longjump.c Tue Jan 14 02:01:41 2014 +0900 @@ -2,11 +2,11 @@ __code code1(int n,__code(*exit___code)(int,void *),void *exit_env){ printf("code1 : code entry1\n"); - goto exit___code(n,exit_env); + double aua; + goto 1; + // goto exit___code(n,exit_env); } -double aua; - int main1 (){ printf("main1 : main1 entry\n"); goto code1(30,__return,__environment);