jmpbuf.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* jmpbuf.h -*-C++-*-
  2. *
  3. *************************************************************************
  4. *
  5. * @copyright
  6. * Copyright (C) 2009-2013, Intel Corporation
  7. * All rights reserved.
  8. *
  9. * @copyright
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. * * Neither the name of Intel Corporation nor the names of its
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * @copyright
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  32. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  33. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  35. * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. **************************************************************************/
  38. /**
  39. * @file jmpbuf.h
  40. *
  41. * @brief Macros and functions to access the _JUMP_BUFFER initialized by a
  42. * call to CILK_SETJMP before a cilk_spawn or cilk_sync. The definition of
  43. * CILK_SETJMP and CILK_LONGJMP are OS dependent and in abi.h
  44. *
  45. */
  46. #ifndef INCLUDED_JMPBUF_DOT_H
  47. #define INCLUDED_JMPBUF_DOT_H
  48. #include <cilk/common.h>
  49. #include <internal/abi.h>
  50. #include <stddef.h>
  51. #include <setjmp.h>
  52. #if 0 /* defined CILK_USE_C_SETJMP && defined JB_RSP */
  53. # define JMPBUF_SP(ctx) (ctx)[0].__jmpbuf[JB_RSP]
  54. # define JMPBUF_FP(ctx) (ctx)[0].__jmpbuf[JB_RBP]
  55. # define JMPBUF_PC(ctx) (ctx)[0].__jmpbuf[JB_PC]
  56. #elif 0 /* defined CILK_USE_C_SETJMP && defined JB_SP */
  57. # define JMPBUF_SP(ctx) (ctx)[0].__jmpbuf[JB_SP]
  58. # define JMPBUF_FP(ctx) (ctx)[0].__jmpbuf[JB_BP]
  59. # define JMPBUF_PC(ctx) (ctx)[0].__jmpbuf[JB_PC]
  60. #elif defined _WIN64
  61. # define JMPBUF_SP(ctx) ((_JUMP_BUFFER*)(&(ctx)))->Rsp
  62. # define JMPBUF_FP(ctx) ((_JUMP_BUFFER*)(&(ctx)))->Rbp
  63. # define JMPBUF_PC(ctx) ((_JUMP_BUFFER*)(&(ctx)))->Rip
  64. #elif defined _WIN32
  65. /** Fetch stack pointer from a __cilkrts_stack_frame */
  66. # define JMPBUF_SP(ctx) (ctx).Esp
  67. /** Fetch frame pointer from a __cilkrts_stack_frame */
  68. # define JMPBUF_FP(ctx) (ctx).Ebp
  69. /** Fetch program counter from a __cilkrts_stack_frame */
  70. # define JMPBUF_PC(ctx) (ctx).Eip
  71. #else /* defined __GNUC__ || defined __ICC */
  72. /* word 0 is frame address
  73. * word 1 is resume address
  74. * word 2 is stack address */
  75. # define JMPBUF_FP(ctx) (ctx)[0]
  76. # define JMPBUF_PC(ctx) (ctx)[1]
  77. # define JMPBUF_SP(ctx) (ctx)[2]
  78. #endif
  79. /**
  80. * @brief Get frame pointer from jump buffer in__cilkrts_stack_frame.
  81. */
  82. #define FP(SF) JMPBUF_FP((SF)->ctx)
  83. /**
  84. * @brief Get program counter from jump buffer in__cilkrts_stack_frame.
  85. */
  86. #define PC(SF) JMPBUF_PC((SF)->ctx)
  87. /**
  88. * @brief Get stack pointer from jump buffer in__cilkrts_stack_frame.
  89. */
  90. #define SP(SF) JMPBUF_SP((SF)->ctx)
  91. __CILKRTS_BEGIN_EXTERN_C
  92. /**
  93. * Fetch the stack pointer from a __cilkrts_stack_frame. The jmpbuf was
  94. * initialized before a cilk_spawn or cilk_sync.
  95. *
  96. * @param sf __cilkrts_stack_frame containing the jmpbuf.
  97. *
  98. * @return the stack pointer from the ctx.
  99. */
  100. inline char *__cilkrts_get_sp(__cilkrts_stack_frame *sf)
  101. {
  102. return (char *)SP(sf);
  103. }
  104. /**
  105. * Calculate the frame size from __cilkrts_stack_frame. The jmpbuf was
  106. * initialized before a cilk_spawn or cilk_sync.
  107. *
  108. * @warning Returning an arbitrary value on Windows!
  109. *
  110. * @param sf __cilkrts_stack_frame containing the jmpbuf.
  111. *
  112. * @return the stack pointer from the ctx.
  113. */
  114. inline ptrdiff_t __cilkrts_get_frame_size(__cilkrts_stack_frame *sf)
  115. {
  116. #ifdef _WIN32
  117. if (0 == SP(sf))
  118. return 256; // Arbitrary!
  119. #endif
  120. return (ptrdiff_t)FP(sf) - (ptrdiff_t)SP(sf);
  121. }
  122. __CILKRTS_END_EXTERN_C
  123. #endif // ! defined(INCLUDED_JMPBUF_DOT_H)