setjmp.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <setjmp.h>
  21. #include <stdlib.h>
  22. void
  23. longjmp (jmp_buf env, int val)
  24. {
  25. #if 0 //MES_CCAMD64
  26. asm ("push___%rdi");
  27. #endif
  28. val = val == 0 ? 1 : val;
  29. #if 0 //MES_CCAMD64
  30. asm ("pop____%rdi");
  31. asm ("mov____0x8(%rdi),%rbp !0x00"); // env->__bp
  32. asm ("mov____0x8(%rdi),%rbx !0x08"); // env->__pc
  33. asm ("mov____0x8(%rdi),%rsp !0x10"); // env->__sp
  34. asm ("jmp____*%rbx"); // jmp *PC
  35. #else
  36. asm ("mov____0x8(%rbp),%rbp !0x10"); // env*
  37. asm ("mov____0x8(%rbp),%rbx !0x08"); // env.__pc
  38. asm ("mov____0x8(%rbp),%rsp !0x10"); // env.__sp
  39. asm ("mov____0x8(%rbp),%rbp !0x00"); // env.__bp
  40. asm ("jmp____*%rbx");
  41. #endif
  42. // not reached
  43. exit (42);
  44. }
  45. int
  46. setjmp (__jmp_buf * env)
  47. {
  48. #if 0 //MES_CCAMD64
  49. asm ("mov____%rbp,%rax");
  50. asm ("add____$i32,%rax %0x80");
  51. asm ("mov____0x8(%rax),%rsi !0x00");
  52. asm ("mov____%rsi,0x8(%rdi) !0x00");
  53. asm ("mov____0x8(%rax),%rsi !0x08");
  54. asm ("mov____%rsi,0x8(%rdi) !0x08");
  55. asm ("mov____%rax,%rsi");
  56. asm ("add____$i32,%rsi %0x10");
  57. asm ("mov____%rsi,0x8(%rdi) !0x10");
  58. #else
  59. long *p = (long *) &env;
  60. env[0].__bp = p[-2];
  61. env[0].__pc = p[-1];
  62. env[0].__sp = (long) &env;
  63. #endif
  64. return 0;
  65. }