setjump.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. // xmm6..xmm15, xmm control word and FPU control word are nonvolatile in win64.
  16. // Using movdqu because 16-byte aligning of local records with XMM members
  17. // was broken the last time I checked it (Sergei)
  18. movq %rbx,jmp_buf.rbx(%rcx)
  19. movq %rbp,jmp_buf.rbp(%rcx)
  20. movq %r12,jmp_buf.r12(%rcx)
  21. movq %r13,jmp_buf.r13(%rcx)
  22. movq %r14,jmp_buf.r14(%rcx)
  23. movq %r15,jmp_buf.r15(%rcx)
  24. movq %rsi,jmp_buf.rsi(%rcx)
  25. movq %rdi,jmp_buf.rdi(%rcx)
  26. leaq 8(%rsp),%rax
  27. movq %rax,jmp_buf.rsp(%rcx)
  28. movq (%rsp),%rax
  29. movq %rax,jmp_buf.rip(%rcx)
  30. movdqu %xmm6,jmp_buf.xmm6(%rcx)
  31. movdqu %xmm7,jmp_buf.xmm7(%rcx)
  32. movdqu %xmm8,jmp_buf.xmm8(%rcx)
  33. movdqu %xmm9,jmp_buf.xmm9(%rcx)
  34. movdqu %xmm10,jmp_buf.xmm10(%rcx)
  35. movdqu %xmm11,jmp_buf.xmm11(%rcx)
  36. movdqu %xmm12,jmp_buf.xmm12(%rcx)
  37. movdqu %xmm13,jmp_buf.xmm13(%rcx)
  38. movdqu %xmm14,jmp_buf.xmm14(%rcx)
  39. movdqu %xmm15,jmp_buf.xmm15(%rcx)
  40. stmxcsr jmp_buf.mxcsr(%rcx)
  41. fnstcw jmp_buf.fpucw(%rcx)
  42. {$else win64}
  43. movq %rbx,jmp_buf.rbx(%rdi)
  44. movq %rbp,jmp_buf.rbp(%rdi)
  45. movq %r12,jmp_buf.r12(%rdi)
  46. movq %r13,jmp_buf.r13(%rdi)
  47. movq %r14,jmp_buf.r14(%rdi)
  48. movq %r15,jmp_buf.r15(%rdi)
  49. leaq 8(%rsp),%rax
  50. movq %rax,jmp_buf.rsp(%rdi)
  51. movq (%rsp),%rax
  52. movq %rax,jmp_buf.rip(%rdi)
  53. {$endif win64}
  54. xorl %eax,%eax
  55. end;
  56. procedure fpc_longjmp(var S:jmp_buf;value:longint);assembler;[public,alias:'FPC_LONGJMP'];nostackframe;compilerproc;
  57. asm
  58. {$ifdef win64}
  59. test %edx,%edx
  60. jne .L1
  61. incl %edx
  62. .L1:
  63. movl %edx,%eax
  64. movq jmp_buf.rbx(%rcx),%rbx
  65. movq jmp_buf.rbp(%rcx),%rbp
  66. movq jmp_buf.r12(%rcx),%r12
  67. movq jmp_buf.r13(%rcx),%r13
  68. movq jmp_buf.r14(%rcx),%r14
  69. movq jmp_buf.r15(%rcx),%r15
  70. movq jmp_buf.rsi(%rcx),%rsi
  71. movq jmp_buf.rdi(%rcx),%rdi
  72. movq jmp_buf.rsp(%rcx),%rsp
  73. movdqu jmp_buf.xmm6(%rcx),%xmm6
  74. movdqu jmp_buf.xmm7(%rcx),%xmm7
  75. movdqu jmp_buf.xmm8(%rcx),%xmm8
  76. movdqu jmp_buf.xmm9(%rcx),%xmm9
  77. movdqu jmp_buf.xmm10(%rcx),%xmm10
  78. movdqu jmp_buf.xmm11(%rcx),%xmm11
  79. movdqu jmp_buf.xmm12(%rcx),%xmm12
  80. movdqu jmp_buf.xmm13(%rcx),%xmm13
  81. movdqu jmp_buf.xmm14(%rcx),%xmm14
  82. movdqu jmp_buf.xmm15(%rcx),%xmm15
  83. ldmxcsr jmp_buf.mxcsr(%rcx)
  84. fnclex
  85. fldcw jmp_buf.fpucw(%rcx)
  86. jmpq jmp_buf.rip(%rcx)
  87. {$else win64}
  88. test %esi,%esi
  89. jne .L1
  90. incl %esi
  91. .L1:
  92. movl %esi,%eax
  93. movq jmp_buf.rbx(%rdi),%rbx
  94. movq jmp_buf.rbp(%rdi),%rbp
  95. movq jmp_buf.r12(%rdi),%r12
  96. movq jmp_buf.r13(%rdi),%r13
  97. movq jmp_buf.r14(%rdi),%r14
  98. movq jmp_buf.r15(%rdi),%r15
  99. movq jmp_buf.rsp(%rdi),%rsp
  100. jmpq jmp_buf.rip(%rdi)
  101. {$endif win64}
  102. end;