2
0

setjump.inc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2002 by Jonas Maebe 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. {#define ENV(base,reg) [%base + (reg * 4)]
  13. #define ST_FLUSH_WINDOWS 3
  14. #define RW_FP [%fp + 0x48]
  15. }
  16. procedure fpc_longjmp(var s : jmp_buf;value:longint);assembler;nostackframe;[Public,alias:'FPC_LONGJMP'];compilerproc;
  17. asm
  18. // Store our arguments in global registers so we can still
  19. // use them while unwinding frames and their register windows.
  20. ld [%o0+4], %g3 // Cache target FP in register %g3.
  21. mov %o0, %g1 // s in %g1
  22. orcc %o1, %g0, %g2 // value in %g2
  23. be,a .L0 // Branch if zero; else skip delay slot.
  24. mov 1, %g2 // Delay slot only hit if zero: VAL = 1.
  25. .L0:
  26. xor %fp, %g3, %o0
  27. add %fp, 512, %o1
  28. andncc %o0, 4095, %o0
  29. bne .Lthread
  30. cmp %o1, %g3
  31. bl .Lthread
  32. // Now we will loop, unwinding the register windows up the stack
  33. // until the restored %fp value matches the target value in %g3.
  34. .Lloop:
  35. cmp %fp, %g3 // Have we reached the target frame?
  36. bl,a .Lloop // Loop while current fp is below target.
  37. restore // Unwind register window in delay slot.
  38. be,a .Lfound // Better have hit it exactly.
  39. ld [%g1], %o0 // Delay slot: extract target SP.
  40. .Lthread:
  41. {
  42. * Do a "flush register windows trap". The trap handler in the
  43. * kernel writes all the register windows to their stack slots, and
  44. * marks them all as invalid (needing to be sucked up from the
  45. * stack when used). This ensures that all information needed to
  46. * unwind to these callers is in memory, not in the register
  47. * windows.
  48. }
  49. ta 3
  50. mov %g1,%o1 // use %o1, since %g1 will be destroyed by the call below
  51. ld [%o1], %fp // Set saved SP on restore below.
  52. sub %fp, 64, %sp // Allocate a register frame.
  53. st %g3, [%fp+48] // Set saved FP on restore below.
  54. ld [%o1+8], %o7 // Set return PC.
  55. retl
  56. restore %g2, 0, %o0 // Restore values from above register frame.
  57. .Lfound:
  58. // We have unwound register windows so %fp matches the target.
  59. mov %o0, %sp // OK, install new SP.
  60. .Lsp_ok:
  61. ld [%g1+8], %o0 // Extract target return PC.
  62. jmp %o0+8 // Return there.
  63. mov %g2, %o0 // Delay slot: set return value.
  64. end;
  65. function fpc_setjmp(var S:jmp_buf):longint;assembler;nostackframe;[Public,alias:'FPC_SETJMP'];compilerproc;
  66. asm
  67. // We don't create a stackframe so we can save PC,SP and FP of the caller
  68. st %o7, [%o0+8]
  69. st %sp, [%o0]
  70. st %fp, [%o0+4]
  71. ld [%o0+8], %o7
  72. mov %g0, %o0
  73. end;