changeset 22:39ae2535ad2c

setjmp
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Tue, 26 Jan 2016 03:16:41 +0900
parents 0d6edf8a4b67
children facab2ddc380
files asmtest2.c
diffstat 1 files changed, 51 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/asmtest2.c	Mon Jan 25 10:49:15 2016 +0900
+++ b/asmtest2.c	Tue Jan 26 03:16:41 2016 +0900
@@ -1,39 +1,74 @@
 #include <stdio.h>
 
-__code factorial(int n,int result,int orig,__code(*print)(int,int,int,__code(*)(),__code(*)(),void*),__code(*exit1)(int,void *), void *exit1env)
+
+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 (*exit1)(0,exit1env);
+      return;
+      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 main( int ac, char *av[])
 {
   int n;
   n = 10;
-  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)){
+      /* 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 print(int n,int result,int orig,__code(*print)(),__code (*exit1)(int, void*),void*exit1env)
+__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 save_env(void* env){
+}
+
+__code print(int n,int result,int orig,__code(*print)(),void*exit1env)
 {
   printf("#0032:%d! = %d\n",orig, result);
-  asm volatile(
-               "movq (%%rdi), %%r10;"
-               "movq $0, (%%r10);"
-               "movq 0x8(%%rdi), %%r10;"
-               "movq 0x8(%%r10), %%rbp;"
-               "movq 0x10(%%r10), %%rsp;"
-               "jmpq *0x38(%%r10);"
-               :"+D"(exit1env)::"r10"
-               );
+  goto return_cs(0, exit1env);
 }