setjump.inc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2008 by the Free Pascal development team.
  4. SetJmp and LongJmp implementation for exception handling
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. function fpc_setjmp(var S : jmp_buf) : shortint;assembler;[Public, alias : 'FPC_SETJMP'];nostackframe;compilerproc;
  12. asm
  13. push ix
  14. ld ix, 0
  15. add ix,sp
  16. ld l, (ix+4) { (S) }
  17. ld h, (ix+5) { (S+1) }
  18. { save caller ix }
  19. ld c, (ix)
  20. ld b, (ix+1)
  21. ld (hl), c
  22. inc hl
  23. ld (hl), b
  24. inc hl
  25. { save caller sp (i.e. what its value was, right before the call instrunction) }
  26. ld iy, 4
  27. add iy, sp
  28. push iy
  29. pop bc
  30. ld (hl), c
  31. inc hl
  32. ld (hl), b
  33. inc hl
  34. { save ret address }
  35. ld c, (ix+2)
  36. ld b, (ix+3)
  37. ld (hl), c
  38. inc hl
  39. ld (hl), b
  40. inc hl
  41. ld l, 0
  42. pop ix
  43. end;
  44. procedure fpc_longjmp(var S : jmp_buf;value : shortint);assembler;[Public, alias : 'FPC_LONGJMP'];nostackframe;compilerproc;
  45. asm
  46. push ix
  47. ld ix, 0
  48. add ix, sp
  49. ld d, (ix+6) { (value) }
  50. ld l, (ix+4) { (S) }
  51. ld h, (ix+5) { (S+1) }
  52. { restore ix }
  53. ld c, (hl)
  54. inc hl
  55. ld b, (hl)
  56. inc hl
  57. push bc
  58. pop ix
  59. { restore sp }
  60. ld c, (hl)
  61. inc hl
  62. ld b, (hl)
  63. inc hl
  64. push bc
  65. pop iy
  66. ld sp, iy
  67. { restore pc }
  68. ld c, (hl)
  69. inc hl
  70. ld b, (hl)
  71. inc hl
  72. { prepare the new return address, will be popped by the ret instruction }
  73. push bc
  74. { return result }
  75. ld l, d
  76. end;