Mercurial > hg > CbC > CbC_examples
view asmtest2.c @ 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 |
line wrap: on
line source
#include <stdio.h> struct __CbC_env{ void *ret_p,*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); return; 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 main( int ac, char *av[]) { int n; n = 10; 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)){ /* if (save_env(__CbC_environment)){ */ /* 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 10; } __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 $12, %%eax;" // return value "jmp MAIN_RET_POINT;" // goto previous environment :"+D"(env)::"r10","eax"); } __code print(int n,int result,int orig,__code(*print)(),void*exit1env) { printf("#0032:%d! = %d\n",orig, result); goto return_cs(0, exit1env); }