lj_frame.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. ** Stack frames.
  3. ** Copyright (C) 2005-2023 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), (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. #if LJ_ABI_WIN
  107. #define CFRAME_OFS_ERRF (19*4)
  108. #define CFRAME_OFS_NRES (18*4)
  109. #define CFRAME_OFS_PREV (17*4)
  110. #define CFRAME_OFS_L (16*4)
  111. #define CFRAME_OFS_SEH (9*4)
  112. #define CFRAME_OFS_PC (6*4)
  113. #define CFRAME_OFS_MULTRES (5*4)
  114. #define CFRAME_SIZE (16*4)
  115. #define CFRAME_SHIFT_MULTRES 0
  116. #else
  117. #define CFRAME_OFS_ERRF (15*4)
  118. #define CFRAME_OFS_NRES (14*4)
  119. #define CFRAME_OFS_PREV (13*4)
  120. #define CFRAME_OFS_L (12*4)
  121. #define CFRAME_OFS_PC (6*4)
  122. #define CFRAME_OFS_MULTRES (5*4)
  123. #define CFRAME_SIZE (12*4)
  124. #define CFRAME_SHIFT_MULTRES 0
  125. #endif
  126. #elif LJ_TARGET_X64
  127. #if LJ_ABI_WIN
  128. #define CFRAME_OFS_PREV (13*8)
  129. #if LJ_GC64
  130. #define CFRAME_OFS_PC (12*8)
  131. #define CFRAME_OFS_L (11*8)
  132. #define CFRAME_OFS_ERRF (21*4)
  133. #define CFRAME_OFS_NRES (20*4)
  134. #define CFRAME_OFS_MULTRES (8*4)
  135. #else
  136. #define CFRAME_OFS_PC (25*4)
  137. #define CFRAME_OFS_L (24*4)
  138. #define CFRAME_OFS_ERRF (23*4)
  139. #define CFRAME_OFS_NRES (22*4)
  140. #define CFRAME_OFS_MULTRES (21*4)
  141. #endif
  142. #define CFRAME_SIZE (10*8)
  143. #define CFRAME_SIZE_JIT (CFRAME_SIZE + 9*16 + 4*8)
  144. #define CFRAME_SHIFT_MULTRES 0
  145. #else
  146. #define CFRAME_OFS_PREV (4*8)
  147. #if LJ_GC64
  148. #define CFRAME_OFS_PC (3*8)
  149. #define CFRAME_OFS_L (2*8)
  150. #define CFRAME_OFS_ERRF (3*4)
  151. #define CFRAME_OFS_NRES (2*4)
  152. #define CFRAME_OFS_MULTRES (0*4)
  153. #else
  154. #define CFRAME_OFS_PC (7*4)
  155. #define CFRAME_OFS_L (6*4)
  156. #define CFRAME_OFS_ERRF (5*4)
  157. #define CFRAME_OFS_NRES (4*4)
  158. #define CFRAME_OFS_MULTRES (1*4)
  159. #endif
  160. #if LJ_NO_UNWIND
  161. #define CFRAME_SIZE (12*8)
  162. #else
  163. #define CFRAME_SIZE (10*8)
  164. #endif
  165. #define CFRAME_SIZE_JIT (CFRAME_SIZE + 16)
  166. #define CFRAME_SHIFT_MULTRES 0
  167. #endif
  168. #elif LJ_TARGET_ARM
  169. #define CFRAME_OFS_ERRF 24
  170. #define CFRAME_OFS_NRES 20
  171. #define CFRAME_OFS_PREV 16
  172. #define CFRAME_OFS_L 12
  173. #define CFRAME_OFS_PC 8
  174. #define CFRAME_OFS_MULTRES 4
  175. #if LJ_ARCH_HASFPU
  176. #define CFRAME_SIZE 128
  177. #else
  178. #define CFRAME_SIZE 64
  179. #endif
  180. #define CFRAME_SHIFT_MULTRES 3
  181. #elif LJ_TARGET_ARM64
  182. #define CFRAME_OFS_ERRF 36
  183. #define CFRAME_OFS_NRES 40
  184. #define CFRAME_OFS_PREV 0
  185. #define CFRAME_OFS_L 16
  186. #define CFRAME_OFS_PC 8
  187. #define CFRAME_OFS_MULTRES 32
  188. #define CFRAME_SIZE 208
  189. #define CFRAME_SHIFT_MULTRES 3
  190. #elif LJ_TARGET_PPC
  191. #if LJ_TARGET_XBOX360
  192. #define CFRAME_OFS_ERRF 424
  193. #define CFRAME_OFS_NRES 420
  194. #define CFRAME_OFS_PREV 400
  195. #define CFRAME_OFS_L 416
  196. #define CFRAME_OFS_PC 412
  197. #define CFRAME_OFS_MULTRES 408
  198. #define CFRAME_SIZE 384
  199. #define CFRAME_SHIFT_MULTRES 3
  200. #elif LJ_ARCH_PPC32ON64
  201. #define CFRAME_OFS_ERRF 472
  202. #define CFRAME_OFS_NRES 468
  203. #define CFRAME_OFS_PREV 448
  204. #define CFRAME_OFS_L 464
  205. #define CFRAME_OFS_PC 460
  206. #define CFRAME_OFS_MULTRES 456
  207. #define CFRAME_SIZE 400
  208. #define CFRAME_SHIFT_MULTRES 3
  209. #else
  210. #define CFRAME_OFS_ERRF 48
  211. #define CFRAME_OFS_NRES 44
  212. #define CFRAME_OFS_PREV 40
  213. #define CFRAME_OFS_L 36
  214. #define CFRAME_OFS_PC 32
  215. #define CFRAME_OFS_MULTRES 28
  216. #define CFRAME_SIZE (LJ_ARCH_HASFPU ? 272 : 128)
  217. #define CFRAME_SHIFT_MULTRES 3
  218. #endif
  219. #elif LJ_TARGET_MIPS32
  220. #if LJ_ARCH_HASFPU
  221. #define CFRAME_OFS_ERRF 124
  222. #define CFRAME_OFS_NRES 120
  223. #define CFRAME_OFS_PREV 116
  224. #define CFRAME_OFS_L 112
  225. #define CFRAME_SIZE 112
  226. #else
  227. #define CFRAME_OFS_ERRF 76
  228. #define CFRAME_OFS_NRES 72
  229. #define CFRAME_OFS_PREV 68
  230. #define CFRAME_OFS_L 64
  231. #define CFRAME_SIZE 64
  232. #endif
  233. #define CFRAME_OFS_PC 20
  234. #define CFRAME_OFS_MULTRES 16
  235. #define CFRAME_SHIFT_MULTRES 3
  236. #elif LJ_TARGET_MIPS64
  237. #if LJ_ARCH_HASFPU
  238. #define CFRAME_OFS_ERRF 188
  239. #define CFRAME_OFS_NRES 184
  240. #define CFRAME_OFS_PREV 176
  241. #define CFRAME_OFS_L 168
  242. #define CFRAME_OFS_PC 160
  243. #define CFRAME_SIZE 192
  244. #else
  245. #define CFRAME_OFS_ERRF 124
  246. #define CFRAME_OFS_NRES 120
  247. #define CFRAME_OFS_PREV 112
  248. #define CFRAME_OFS_L 104
  249. #define CFRAME_OFS_PC 96
  250. #define CFRAME_SIZE 128
  251. #endif
  252. #define CFRAME_OFS_MULTRES 0
  253. #define CFRAME_SHIFT_MULTRES 3
  254. #else
  255. #error "Missing CFRAME_* definitions for this architecture"
  256. #endif
  257. #ifndef CFRAME_SIZE_JIT
  258. #define CFRAME_SIZE_JIT CFRAME_SIZE
  259. #endif
  260. #define CFRAME_RESUME 1
  261. #define CFRAME_UNWIND_FF 2 /* Only used in unwinder. */
  262. #define CFRAME_RAWMASK (~(intptr_t)(CFRAME_RESUME|CFRAME_UNWIND_FF))
  263. #define cframe_errfunc(cf) (*(int32_t *)(((char *)(cf))+CFRAME_OFS_ERRF))
  264. #define cframe_nres(cf) (*(int32_t *)(((char *)(cf))+CFRAME_OFS_NRES))
  265. #define cframe_prev(cf) (*(void **)(((char *)(cf))+CFRAME_OFS_PREV))
  266. #define cframe_multres(cf) (*(uint32_t *)(((char *)(cf))+CFRAME_OFS_MULTRES))
  267. #define cframe_multres_n(cf) (cframe_multres((cf)) >> CFRAME_SHIFT_MULTRES)
  268. #define cframe_L(cf) \
  269. (&gcref(*(GCRef *)(((char *)(cf))+CFRAME_OFS_L))->th)
  270. #define cframe_pc(cf) \
  271. (mref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), const BCIns))
  272. #define setcframe_L(cf, L) \
  273. (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_L), (L)))
  274. #define setcframe_pc(cf, pc) \
  275. (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), (pc)))
  276. #define cframe_canyield(cf) ((intptr_t)(cf) & CFRAME_RESUME)
  277. #define cframe_unwind_ff(cf) ((intptr_t)(cf) & CFRAME_UNWIND_FF)
  278. #define cframe_raw(cf) ((void *)((intptr_t)(cf) & CFRAME_RAWMASK))
  279. #define cframe_Lpc(L) cframe_pc(cframe_raw(L->cframe))
  280. #endif