src/lj_frame.h - luajit-2.0-src

Macros defined

Source code

  1. /*
  2. ** Stack frames.
  3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
  4. */

  5. #ifndef _LJ_FRAME_H
  6. #define _LJ_FRAME_H

  7. #include "lj_obj.h"
  8. #include "lj_bc.h"

  9. /* -- Lua stack frame ----------------------------------------------------- */

  10. /* Frame type markers in LSB of PC (4-byte aligned) or delta (8-byte aligned:
  11. **
  12. **    PC  00  Lua frame
  13. ** delta 001  C frame
  14. ** delta 010  Continuation frame
  15. ** delta 011  Lua vararg frame
  16. ** delta 101  cpcall() frame
  17. ** delta 110  ff pcall() frame
  18. ** delta 111  ff pcall() frame with active hook
  19. */
  20. enum {
  21.   FRAME_LUA, FRAME_C, FRAME_CONT, FRAME_VARG,
  22.   FRAME_LUAP, FRAME_CP, FRAME_PCALL, FRAME_PCALLH
  23. };
  24. #define FRAME_TYPE                3
  25. #define FRAME_P                        4
  26. #define FRAME_TYPEP                (FRAME_TYPE|FRAME_P)

  27. /* Macros to access and modify Lua frames. */
  28. #if LJ_FR2
  29. /* Two-slot frame info, required for 64 bit PC/GCRef:
  30. **
  31. **                   base-2  base-1      |  base  base+1 ...
  32. **                  [func   PC/delta/ft] | [slots ...]
  33. **                  ^-- frame            | ^-- base   ^-- top
  34. **
  35. ** Continuation frames:
  36. **
  37. **   base-4  base-3  base-2  base-1      |  base  base+1 ...
  38. **  [cont      PC ] [func   PC/delta/ft] | [slots ...]
  39. **                  ^-- frame            | ^-- base   ^-- top
  40. */
  41. #define frame_gc(f)                (gcval((f)-1))
  42. #define frame_ftsz(f)                ((ptrdiff_t)(f)->ftsz)
  43. #define frame_pc(f)                ((const BCIns *)frame_ftsz(f))
  44. #define setframe_gc(f, p, tp)        (setgcVraw((f)-1, (p), (tp)))
  45. #define setframe_ftsz(f, sz)        ((f)->ftsz = (sz))
  46. #define setframe_pc(f, pc)        ((f)->ftsz = (int64_t)(intptr_t)(pc))
  47. #else
  48. /* One-slot frame info, sufficient for 32 bit PC/GCRef:
  49. **
  50. **              base-1              |  base  base+1 ...
  51. **              lo     hi           |
  52. **             [func | PC/delta/ft] | [slots ...]
  53. **             ^-- frame            | ^-- base   ^-- top
  54. **
  55. ** Continuation frames:
  56. **
  57. **  base-2      base-1              |  base  base+1 ...
  58. **  lo     hi   lo     hi           |
  59. ** [cont | PC] [func | PC/delta/ft] | [slots ...]
  60. **             ^-- frame            | ^-- base   ^-- top
  61. */
  62. #define frame_gc(f)                (gcref((f)->fr.func))
  63. #define frame_ftsz(f)                ((ptrdiff_t)(f)->fr.tp.ftsz)
  64. #define frame_pc(f)                (mref((f)->fr.tp.pcr, const BCIns))
  65. #define setframe_gc(f, p, tp)        (setgcref((f)->fr.func, (p)), UNUSED(tp))
  66. #define setframe_ftsz(f, sz)        ((f)->fr.tp.ftsz = (int32_t)(sz))
  67. #define setframe_pc(f, pc)        (setmref((f)->fr.tp.pcr, (pc)))
  68. #endif

  69. #define frame_type(f)                (frame_ftsz(f) & FRAME_TYPE)
  70. #define frame_typep(f)                (frame_ftsz(f) & FRAME_TYPEP)
  71. #define frame_islua(f)                (frame_type(f) == FRAME_LUA)
  72. #define frame_isc(f)                (frame_type(f) == FRAME_C)
  73. #define frame_iscont(f)                (frame_typep(f) == FRAME_CONT)
  74. #define frame_isvarg(f)                (frame_typep(f) == FRAME_VARG)
  75. #define frame_ispcall(f)        ((frame_ftsz(f) & 6) == FRAME_PCALL)

  76. #define frame_func(f)                (&frame_gc(f)->fn)
  77. #define frame_delta(f)                (frame_ftsz(f) >> 3)
  78. #define frame_sized(f)                (frame_ftsz(f) & ~FRAME_TYPEP)

  79. enum { LJ_CONT_TAILCALL, LJ_CONT_FFI_CALLBACK };  /* Special continuations. */

  80. #if LJ_FR2
  81. #define frame_contpc(f)                (frame_pc((f)-2))
  82. #define frame_contv(f)                (((f)-3)->u64)
  83. #else
  84. #define frame_contpc(f)                (frame_pc((f)-1))
  85. #define frame_contv(f)                (((f)-1)->u32.lo)
  86. #endif
  87. #if LJ_FR2
  88. #define frame_contf(f)                ((ASMFunction)(uintptr_t)((f)-3)->u64)
  89. #elif LJ_64
  90. #define frame_contf(f) \
  91.   ((ASMFunction)(void *)((intptr_t)lj_vm_asm_begin + \
  92.                          (intptr_t)(int32_t)((f)-1)->u32.lo))
  93. #else
  94. #define frame_contf(f)                ((ASMFunction)gcrefp(((f)-1)->gcr, void))
  95. #endif
  96. #define frame_iscont_fficb(f) \
  97.   (LJ_HASFFI && frame_contv(f) == LJ_CONT_FFI_CALLBACK)

  98. #define frame_prevl(f)                ((f) - (1+LJ_FR2+bc_a(frame_pc(f)[-1])))
  99. #define frame_prevd(f)                ((TValue *)((char *)(f) - frame_sized(f)))
  100. #define frame_prev(f)                (frame_islua(f)?frame_prevl(f):frame_prevd(f))
  101. /* Note: this macro does not skip over FRAME_VARG. */

  102. /* -- C stack frame ------------------------------------------------------- */

  103. /* Macros to access and modify the C stack frame chain. */

  104. /* These definitions must match with the arch-specific *.dasc files. */
  105. #if LJ_TARGET_X86
  106. #define CFRAME_OFS_ERRF                (15*4)
  107. #define CFRAME_OFS_NRES                (14*4)
  108. #define CFRAME_OFS_PREV                (13*4)
  109. #define CFRAME_OFS_L                (12*4)
  110. #define CFRAME_OFS_PC                (6*4)
  111. #define CFRAME_OFS_MULTRES        (5*4)
  112. #define CFRAME_SIZE                (12*4)
  113. #define CFRAME_SHIFT_MULTRES        0
  114. #elif LJ_TARGET_X64
  115. #if LJ_ABI_WIN
  116. #define CFRAME_OFS_PREV                (13*8)
  117. #define CFRAME_OFS_PC                (25*4)
  118. #define CFRAME_OFS_L                (24*4)
  119. #define CFRAME_OFS_ERRF                (23*4)
  120. #define CFRAME_OFS_NRES                (22*4)
  121. #define CFRAME_OFS_MULTRES        (21*4)
  122. #define CFRAME_SIZE                (10*8)
  123. #define CFRAME_SIZE_JIT                (CFRAME_SIZE + 9*16 + 4*8)
  124. #define CFRAME_SHIFT_MULTRES        0
  125. #else
  126. #define CFRAME_OFS_PREV                (4*8)
  127. #define CFRAME_OFS_PC                (7*4)
  128. #define CFRAME_OFS_L                (6*4)
  129. #define CFRAME_OFS_ERRF                (5*4)
  130. #define CFRAME_OFS_NRES                (4*4)
  131. #define CFRAME_OFS_MULTRES        (1*4)
  132. #define CFRAME_SIZE                (10*8)
  133. #define CFRAME_SIZE_JIT                (CFRAME_SIZE + 16)
  134. #define CFRAME_SHIFT_MULTRES        0
  135. #endif
  136. #elif LJ_TARGET_ARM
  137. #define CFRAME_OFS_ERRF                24
  138. #define CFRAME_OFS_NRES                20
  139. #define CFRAME_OFS_PREV                16
  140. #define CFRAME_OFS_L                12
  141. #define CFRAME_OFS_PC                8
  142. #define CFRAME_OFS_MULTRES        4
  143. #if LJ_ARCH_HASFPU
  144. #define CFRAME_SIZE                128
  145. #else
  146. #define CFRAME_SIZE                64
  147. #endif
  148. #define CFRAME_SHIFT_MULTRES        3
  149. #elif LJ_TARGET_ARM64
  150. #define CFRAME_OFS_ERRF                196
  151. #define CFRAME_OFS_NRES                200
  152. #define CFRAME_OFS_PREV                160
  153. #define CFRAME_OFS_L                176
  154. #define CFRAME_OFS_PC                168
  155. #define CFRAME_OFS_MULTRES        192
  156. #define CFRAME_SIZE                208
  157. #define CFRAME_SHIFT_MULTRES        3
  158. #elif LJ_TARGET_PPC
  159. #if LJ_TARGET_XBOX360
  160. #define CFRAME_OFS_ERRF                424
  161. #define CFRAME_OFS_NRES                420
  162. #define CFRAME_OFS_PREV                400
  163. #define CFRAME_OFS_L                416
  164. #define CFRAME_OFS_PC                412
  165. #define CFRAME_OFS_MULTRES        408
  166. #define CFRAME_SIZE                384
  167. #define CFRAME_SHIFT_MULTRES        3
  168. #elif LJ_ARCH_PPC32ON64
  169. #define CFRAME_OFS_ERRF                472
  170. #define CFRAME_OFS_NRES                468
  171. #define CFRAME_OFS_PREV                448
  172. #define CFRAME_OFS_L                464
  173. #define CFRAME_OFS_PC                460
  174. #define CFRAME_OFS_MULTRES        456
  175. #define CFRAME_SIZE                400
  176. #define CFRAME_SHIFT_MULTRES        3
  177. #else
  178. #define CFRAME_OFS_ERRF                48
  179. #define CFRAME_OFS_NRES                44
  180. #define CFRAME_OFS_PREV                40
  181. #define CFRAME_OFS_L                36
  182. #define CFRAME_OFS_PC                32
  183. #define CFRAME_OFS_MULTRES        28
  184. #define CFRAME_SIZE                272
  185. #define CFRAME_SHIFT_MULTRES        3
  186. #endif
  187. #elif LJ_TARGET_MIPS
  188. #define CFRAME_OFS_ERRF                124
  189. #define CFRAME_OFS_NRES                120
  190. #define CFRAME_OFS_PREV                116
  191. #define CFRAME_OFS_L                112
  192. #define CFRAME_OFS_PC                20
  193. #define CFRAME_OFS_MULTRES        16
  194. #define CFRAME_SIZE                112
  195. #define CFRAME_SHIFT_MULTRES        3
  196. #else
  197. #error "Missing CFRAME_* definitions for this architecture"
  198. #endif

  199. #ifndef CFRAME_SIZE_JIT
  200. #define CFRAME_SIZE_JIT                CFRAME_SIZE
  201. #endif

  202. #define CFRAME_RESUME                1
  203. #define CFRAME_UNWIND_FF        2  /* Only used in unwinder. */
  204. #define CFRAME_RAWMASK                (~(intptr_t)(CFRAME_RESUME|CFRAME_UNWIND_FF))

  205. #define cframe_errfunc(cf)        (*(int32_t *)(((char *)(cf))+CFRAME_OFS_ERRF))
  206. #define cframe_nres(cf)                (*(int32_t *)(((char *)(cf))+CFRAME_OFS_NRES))
  207. #define cframe_prev(cf)                (*(void **)(((char *)(cf))+CFRAME_OFS_PREV))
  208. #define cframe_multres(cf)  (*(uint32_t *)(((char *)(cf))+CFRAME_OFS_MULTRES))
  209. #define cframe_multres_n(cf)        (cframe_multres((cf)) >> CFRAME_SHIFT_MULTRES)
  210. #define cframe_L(cf) \
  211.   (&gcref(*(GCRef *)(((char *)(cf))+CFRAME_OFS_L))->th)
  212. #define cframe_pc(cf) \
  213.   (mref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), const BCIns))
  214. #define setcframe_L(cf, L) \
  215.   (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_L), (L)))
  216. #define setcframe_pc(cf, pc) \
  217.   (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), (pc)))
  218. #define cframe_canyield(cf)        ((intptr_t)(cf) & CFRAME_RESUME)
  219. #define cframe_unwind_ff(cf)        ((intptr_t)(cf) & CFRAME_UNWIND_FF)
  220. #define cframe_raw(cf)                ((void *)((intptr_t)(cf) & CFRAME_RAWMASK))
  221. #define cframe_Lpc(L)                cframe_pc(cframe_raw(L->cframe))

  222. #endif