view ljtes/ljtes_as.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 0d6edf8a4b67
children
line wrap: on
line source

#ifdef GCC
#define __environment _CbC_environment
#define __return _CbC_return
#endif

#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(*)(),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 (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[])
{
  int i;
  long ans;
  for(i=LOOP,ans=0;i>0;i--){
    int a = calc(10);
    ans += a;
  }

  printf("%ld\n",ans);
}

__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);
}