Mercurial > hg > CbC > CbC_examples
changeset 23:facab2ddc380
remove label (segmentation fault when -O2)
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 30 Jan 2016 03:40:06 +0900 |
parents | 39ae2535ad2c |
children | 19160c497905 |
files | Makefile asmtest2.c ljtes/lj ljtes/lj_as ljtes/ljtes.c ljtes/ljtes_as.c ljtes/ljtes_as2.c |
diffstat | 7 files changed, 138 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Tue Jan 26 03:16:41 2016 +0900 +++ b/Makefile Sat Jan 30 03:40:06 2016 +0900 @@ -1,4 +1,4 @@ -CC=/Users/e105711/prog/seminar/CbC/llvmInst/Debug+Asserts/bin/clang +CC=/Users/e105711/prog/seminar/CbC/llvmInst/bin/clang TARGET=bug conv fact-a loto6 normal normal2 test01 test02 test04 test05 test_array test_call test_cs test_para test_para2 test_para4 test_return test_struct test_tailcall1 test_tree tmp1 tmp2 tmp4 test_csp1 test_func2code tmpa too-long-argument test1 stack1 test2 arg test_para3 proto assignment CFLAGS= -D"CLANG"
--- a/asmtest2.c Tue Jan 26 03:16:41 2016 +0900 +++ b/asmtest2.c Sat Jan 30 03:40:06 2016 +0900 @@ -54,7 +54,6 @@ } __code return_cs(int val, void* env){ - asm volatile( "movq 0x8(%%rdi), %%r10;" // %r10 = env.env (environment) "movq 0x8(%%r10), %%rbp;" // restore frame pointer @@ -64,9 +63,6 @@ :"+D"(env)::"r10","eax"); } -__code save_env(void* env){ -} - __code print(int n,int result,int orig,__code(*print)(),void*exit1env) { printf("#0032:%d! = %d\n",orig, result);
--- a/ljtes/ljtes.c Tue Jan 26 03:16:41 2016 +0900 +++ b/ljtes/ljtes.c Sat Jan 30 03:40:06 2016 +0900 @@ -3,7 +3,7 @@ #define __return _CbC_return #endif -#define LOOP 50000 +#define LOOP 5000000 #include <stdio.h> @@ -25,6 +25,7 @@ int calc(int n){ goto factorial(n,1,n,print,__return,__environment); + return 0; } int main( int ac, char *av[]) @@ -32,7 +33,7 @@ int i; long ans; for(i=LOOP,ans=0;i>0;i--){ - ans += calc(i); + ans += calc(10); } printf("%ld\n",ans);
--- a/ljtes/ljtes_as.c Tue Jan 26 03:16:41 2016 +0900 +++ b/ljtes/ljtes_as.c Sat Jan 30 03:40:06 2016 +0900 @@ -3,28 +3,55 @@ #define __return _CbC_return #endif -#define LOOP 50000 +#define LOOP 5000000 #include <stdio.h> +struct __CbC_env{ + void *ret_p,*env; +}; -__code factorial(int n,int result,int orig,__code(*print)(int,int,int,__code(*)(),__code(*)(),void*),__code(*exit1)(int,void *), void *exit1env) +__code factorial(int n,int result,int orig,__code(*print)(int,int,int,__code(*)(),void*), void *exit1env) { if (n<0) { printf("#0008:err %d!\n",n); - goto (*exit1)(0,exit1env); + goto return_cs(0,exit1env); } if (n==0) - goto (*print)(n,result,orig,print,exit1,exit1env); + goto (*print)(n,result,orig,print,exit1env); else { result += n; n--; - goto factorial(n,result,orig,print,exit1,exit1env); + goto factorial(n,result,orig,print,exit1env); } } int calc(int n){ - goto factorial(n,1,n,print,__return,__environment); + void *env = ({ + struct __CbC_env __CbC_environment; + env = &__CbC_environment; + jmp_buf env_buf; + int retval; + __CbC_environment.ret_p = &retval; + __CbC_environment.env = &env_buf; + // if (setjmp(__CbC_environment.env)){ + /* return retval; */ + /* } */ + asm volatile( + "movq 0x8(%%rdi), %%r10;" // %r10 = env.env; + "movq %%rbp, 0x8(%%r10);" // env.env[1] = %rbp; frame pointer + "movq %%rsp, 0x10(%%r10);" // env.env[2] = %rsp; stack pointer + "jmp MAIN_AFTER_SET;" // escape from asm + "MAIN_RET_POINT:;" // return point + "movq %%rbp, %%rsp;" // fix stack pointer + "popq %%rbp;" // pop + "retq;" // return + "MAIN_AFTER_SET:;" // escape point + :"+D"(env)::"r10","eax"); + &__CbC_environment; + }); + goto factorial(n,1,n,print,env); + return 0; } int main( int ac, char *av[]) @@ -32,23 +59,24 @@ int i; long ans; for(i=LOOP,ans=0;i>0;i--){ - int a = calc(i); + int a = calc(10); ans += a; } printf("%ld\n",ans); } -__code print(int n,int result,int orig,__code(*print)(),__code (*exit1)(int, void*),void*exit1env) -{ - asm ( - "movq (%%rdi), %%r10;" - "movq $0, (%%r10);" - "movq 0x8(%%rdi), %%r10;" - "movq 0x8(%%r10), %%rbp;" - "movq 0x10(%%r10), %%rsp;" - "jmpq *0x38(%%r10);" - :"+D"(exit1env)::"r10" - ); +__code return_cs(int val, void* env){ + asm volatile( + "movq 0x8(%%rdi), %%r10;" // %r10 = env.env (environment) + "movq 0x8(%%r10), %%rbp;" // restore frame pointer + "movq 0x10(%%r10), %%rsp;" // restore stack pointer + "movl %%ecx, %%eax;" // return value + "jmp MAIN_RET_POINT;" // goto previous environment + :"+D"(env):"c"(val):"r10","eax"); } +__code print(int n,int result,int orig,__code(*print)(),void*exit1env) +{ + goto return_cs(result, exit1env); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ljtes/ljtes_as2.c Sat Jan 30 03:40:06 2016 +0900 @@ -0,0 +1,88 @@ +#ifdef GCC +#define __environment _CbC_environment +#define __return _CbC_return +#endif + +#define LOOP 5000000 + +#include <stdio.h> +struct __CbC_env{ + void *ret_p,*env; +}; + +int save_env(void *env); + +__code factorial(int n,int result,int orig,__code(*print)(int,int,int,__code(*)(),void*), void *exit1env) +{ + if (n<0) { + printf("#0008:err %d!\n",n); + goto return_cs(0,exit1env); + } + if (n==0) + goto (*print)(n,result,orig,print,exit1env); + else { + result += n; + n--; + goto factorial(n,result,orig,print,exit1env); + } +} + + +int calc(int n){ + void *env = ({ + struct __CbC_env __CbC_environment; + env = &__CbC_environment; + jmp_buf env_buf; + int retval; + __CbC_environment.ret_p = &retval; + __CbC_environment.env = &env_buf; + if (save_env(__CbC_environment.env)){ + return retval; + } + &__CbC_environment; + }); + goto factorial(n,1,n,print,env); + return 0; +} + +int main( int ac, char *av[]) +{ + int i; + long ans; + for(i=LOOP,ans=0;i>0;i--){ + int a = calc(10); + ans += a; + } + + printf("%ld\n",ans); +} + +int save_env(void *env){ + asm volatile( + "movq %%rbp,(%%rdi);" // env.env[1] = %rbp; frame pointer + "movq (%%rsp), %%rax;" // get return addres + "movq %%rax, 0x8(%%rdi);" // set return addres + "popq %%rax;" // fix stack pointer + "movq %%rsp, 0x10(%%rdi);" // set stack pointer + "movl $0, %%eax;" // set return value + "jmpq *0x8(%%rdi);" // return + :"+D"(env)::"rax","r11"); + return 0; +} + +__code return_cs(int val, void* env){ + asm volatile( + "movq (%%rdi), %%r10;" // %r10 = env.ret_p (return value) + "movq %%rcx ,(%%r10);" // set return value + "movq 0x8(%%rdi), %%r10;" // %r10 = env.env (environment) + "movq (%%r10), %%rbp;" // restore frame pointer + "movq 0x10(%%r10), %%rsp;" // restore stack pointer + "movl $1, %%eax;" // set return value + "jmpq *0x8(%%r10);" // return + :"+D"(env):"c"(val):"r10","eax"); +} + +__code print(int n,int result,int orig,__code(*print)(),void*exit1env) +{ + goto return_cs(result, exit1env); +}