setjump.inc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2003 by Florian Klaempfl and other members of the
  4. Free Pascal development team
  5. SetJmp and LongJmp implementation for exception handling
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. function fpc_setjmp(var S : jmp_buf) : longint;assembler;[Public, alias : 'FPC_SETJMP'];nostackframe;compilerproc;
  13. asm
  14. {$ifdef win64}
  15. // Save registers.
  16. movq %rbx,(%rcx)
  17. movq %rbp,8(%rcx)
  18. movq %r12,16(%rcx)
  19. movq %r13,24(%rcx)
  20. movq %r14,32(%rcx)
  21. movq %r15,40(%rcx)
  22. movq %rsi,64(%rcx)
  23. movq %rdi,72(%rcx)
  24. leaq 8(%rsp),%rdx // Save SP as it will be after we return.
  25. movq %rdx,48(%rcx)
  26. movq 0(%rsp),%r8 // Save PC we are returning to now.
  27. movq %r8,56(%rcx)
  28. xorq %rax,%rax
  29. {$else win64}
  30. // Save registers.
  31. movq %rbx,(%rdi)
  32. movq %rbp,8(%rdi)
  33. movq %r12,16(%rdi)
  34. movq %r13,24(%rdi)
  35. movq %r14,32(%rdi)
  36. movq %r15,40(%rdi)
  37. leaq 8(%rsp),%rdx // Save SP as it will be after we return.
  38. movq %rdx,48(%rdi)
  39. movq 0(%rsp),%rsi // Save PC we are returning to now.
  40. movq %rsi,56(%rdi)
  41. xorq %rax,%rax
  42. {$endif win64}
  43. end;
  44. procedure fpc_longjmp(var S : jmp_buf;value : longint);assembler;[Public, alias : 'FPC_LONGJMP'];compilerproc;
  45. asm
  46. {$ifdef win64}
  47. // Restore registers.
  48. movq (%rcx),%rbx
  49. movq 8(%rcx),%rbp
  50. movq 16(%rcx),%r12
  51. movq 24(%rcx),%r13
  52. movq 32(%rcx),%r14
  53. movq 40(%rcx),%r15
  54. // Set return value for setjmp.
  55. test %edx,%edx
  56. mov $01,%eax
  57. cmove %eax,%edx
  58. mov %edx,%eax
  59. movq 48(%rcx),%rsp
  60. movq 56(%rcx),%rdx
  61. movq 64(%rcx),%rsi
  62. movq 72(%rcx),%rdi
  63. jmpq *%rdx
  64. {$else win64}
  65. // Restore registers.
  66. movq (%rdi),%rbx
  67. movq 8(%rdi),%rbp
  68. movq 16(%rdi),%r12
  69. movq 24(%rdi),%r13
  70. movq 32(%rdi),%r14
  71. movq 40(%rdi),%r15
  72. // Set return value for setjmp.
  73. test %esi,%esi
  74. mov $01,%eax
  75. cmove %eax,%esi
  76. mov %esi,%eax
  77. movq 56(%rdi),%rdx
  78. movq 48(%rdi),%rsp
  79. jmpq *%rdx
  80. {$endif win64}
  81. end;