setjmp.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2017,2018,2019,2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. * Copyright © 2019,2020 Danny Milosavljevic <dannym@scratchpost.org>
  5. *
  6. * This file is part of GNU Mes.
  7. *
  8. * GNU Mes is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * GNU Mes is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <setjmp.h>
  22. #include <stdlib.h>
  23. #ifndef HAVE_FLOAT
  24. #define HAVE_FLOAT 0
  25. #endif
  26. #if !__TINYC__
  27. void
  28. __attribute__ ((noinline))
  29. longjmp (jmp_buf env, int val)
  30. {
  31. // *INDENT-OFF*
  32. asm (
  33. "mov r0, %0\n\t"
  34. "mov r1, %1\n\t"
  35. "cmp r1, #0\n\t"
  36. "moveq r1, #1\n\t" /* returning 0 is not allowed, even when the user wanted to. */
  37. "ldr r13, [r0], #4\n\t" /* stack pointer (sp) */
  38. "ldr r14, [r0], #4\n\t" /* link register (lr) */
  39. "ldmia r0!, {r4, r5, r6, r7, r8, r9, r10, r11}\n\t"
  40. #if HAVE_FLOAT
  41. // TODO: If using VFP, vldmia r0!, {d8-d15}
  42. #endif
  43. "mov r0, r1\n\t"
  44. :
  45. : "r" (env), "r" (val));
  46. // *INDENT-ON*
  47. // not reached
  48. }
  49. #else //__TINYC__
  50. #ifndef SETJMP_ASM
  51. #define SETJMP_ASM 0
  52. #endif
  53. #if SETJMP_ASM
  54. __asm__ (".global longjmp\n");
  55. __asm__ ("longjmp:\n");
  56. __asm__ (".int 0xe52db004\n"); //push {fp} ; (str fp, [sp, #-4]!)
  57. __asm__ (".int 0xe28db000\n"); //add fp, sp, #0
  58. __asm__ (".int 0xe24dd00c\n"); //sub sp, sp, #12
  59. __asm__ (".int 0xe50b0008\n"); //str r0, [fp, #-8]
  60. __asm__ (".int 0xe50b100c\n"); //str r1, [fp, #-12]
  61. __asm__ (".int 0xe51b3008\n"); //ldr r3, [fp, #-8]
  62. __asm__ (".int 0xe51b200c\n"); //ldr r2, [fp, #-12]
  63. __asm__ (".int 0xe1a00003\n"); //mov r0, r3
  64. __asm__ (".int 0xe1a01002\n"); //mov r1, r2
  65. __asm__ (".int 0xe3510000\n"); //cmp r1, #0
  66. __asm__ (".int 0x03a01001\n"); //moveq r1, #1
  67. __asm__ (".int 0xe490d004\n"); //ldr sp, [r0], #4
  68. __asm__ (".int 0xe490e004\n"); //ldr lr, [r0], #4
  69. __asm__ (".int 0xe8b00ff0\n"); //ldm r0!, {r4, r5, r6, r7, r8, r9, sl, fp}
  70. #if HAVE_FLOAT
  71. // TODO: If using VFP, vldmia r0!, {d8-d15}
  72. #endif
  73. __asm__ (".int 0xe1a00001\n"); //mov r0, r1
  74. __asm__ (".int 0xe12fff1e\n"); //bx lr
  75. #else //!SETJMP_ASM
  76. void
  77. __attribute__ ((noinline))
  78. longjmp (jmp_buf env, int val)
  79. {
  80. __asm__ (".int 0xe3510000\n"); //cmp r1, #0
  81. __asm__ (".int 0x03a01001\n"); //moveq r1, #1
  82. __asm__ (".int 0xe490d004\n"); //ldr sp, [r0], #4
  83. __asm__ (".int 0xe490e004\n"); //ldr lr, [r0], #4
  84. __asm__ (".int 0xe8b00ff0\n"); //ldm r0!, {r4, r5, r6, r7, r8, r9, sl, fp}
  85. #if HAVE_FLOAT
  86. // TODO: If using VFP, vldmia r0!, {d8-d15}
  87. #endif
  88. __asm__ (".int 0xe1a00001\n"); //mov r0, r1
  89. __asm__ (".int 0xe12fff1e\n"); //bx lr
  90. }
  91. #endif //!SETJMP_ASM
  92. #endif //__TINYC__
  93. #if !__TINYC__
  94. int
  95. __attribute__ ((noinline))
  96. setjmp (jmp_buf env)
  97. {
  98. // *INDENT-OFF*
  99. asm (
  100. "mov r0, %0\n\t"
  101. "str r13, [r0], #4\n\t" /* stack pointer (sp) */
  102. "str r14, [r0], #4\n\t" /* link register (lr) */
  103. "stmia r0!, {r4, r5, r6, r7, r8, r9, r10, r11}\n\t"
  104. #if HAVE_FLOAT
  105. // TODO: If using VFP, vstmia r0!, {d8-d15}
  106. #endif
  107. :
  108. : "r" (env)
  109. : "r0");
  110. // *INDENT-ON*
  111. return 0;
  112. }
  113. #else //__TINYC__
  114. #if SETJMP_ASM
  115. __asm__ (".global setjmp\n");
  116. __asm__ ("setjmp:\n");
  117. __asm__ (".int 0xe52db004\n"); //push {fp} ; (str fp, [sp, #-4]!)
  118. __asm__ (".int 0xe28db000\n"); //add fp, sp, #0
  119. __asm__ (".int 0xe24dd00c\n"); //sub sp, sp, #12
  120. __asm__ (".int 0xe50b0008\n"); //str r0, [fp, #-8]
  121. __asm__ (".int 0xe51b3008\n"); //ldr r3, [fp, #-8]
  122. __asm__ (".int 0xe1a00003\n"); //mov r0, r3
  123. __asm__ (".int 0xe480d004\n"); //str sp, [r0], #4
  124. __asm__ (".int 0xe480e004\n"); //str lr, [r0], #4
  125. __asm__ (".int 0xe28bd000\n"); //add sp, fp, #0
  126. __asm__ (".int 0xe49db004\n"); //pop {fp} ; (ldr fp, [sp], #4)
  127. __asm__ (".int 0xe8a00ff0\n"); //stmia r0!, {r4, r5, r6, r7, r8, r9, sl, fp}
  128. #if HAVE_FLOAT
  129. // TODO: If using VFP, vstmia r0!, {d8-d15}
  130. #endif
  131. __asm__ (".int 0xe1a0b00d\n"); //mov fp, sp
  132. __asm__ (".int 0xe3a03000\n"); //mov r3, #0
  133. __asm__ (".int 0xe1a00003\n"); //mov r0, r3
  134. __asm__ (".int 0xe12fff1e\n"); //bx lr
  135. #else //!SETJMP_ASM
  136. int
  137. __attribute__ ((noinline))
  138. setjmp (jmp_buf env)
  139. {
  140. __asm__ (".int 0xe480d004\n"); //str sp, [r0], #4
  141. __asm__ (".int 0xe480e004\n"); //str lr, [r0], #4
  142. //__asm__ (".int 0xe1a0300b\n"); //mov r3, fp
  143. __asm__ (".int 0xe59bb000\n"); //ldr fp, [fp]
  144. __asm__ (".int 0xe8a00ff0\n"); //stmia r0!, {r4, r5, r6, r7, r8, r9, sl, fp}
  145. #if HAVE_FLOAT
  146. // TODO: If using VFP, vstmia r0!, {d8-d15}
  147. #endif
  148. __asm__ (".int 0xe1a0b00d\n"); //mov fp, sp
  149. //__asm__ (".int 0xe1a0b003\n"); //mov fp, r3
  150. __asm__ (".int 0xe3a00000\n"); // mov r0, #0
  151. }
  152. #endif //!SETJMP_ASM
  153. #endif //__TINYC__