lj_ir.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. ** SSA IR (Intermediate Representation) format.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #ifndef _LJ_IR_H
  6. #define _LJ_IR_H
  7. #include "lj_obj.h"
  8. /* -- IR instructions ----------------------------------------------------- */
  9. /* IR instruction definition. Order matters, see below. ORDER IR */
  10. #define IRDEF(_) \
  11. /* Guarded assertions. */ \
  12. /* Must be properly aligned to flip opposites (^1) and (un)ordered (^4). */ \
  13. _(LT, N , ref, ref) \
  14. _(GE, N , ref, ref) \
  15. _(LE, N , ref, ref) \
  16. _(GT, N , ref, ref) \
  17. \
  18. _(ULT, N , ref, ref) \
  19. _(UGE, N , ref, ref) \
  20. _(ULE, N , ref, ref) \
  21. _(UGT, N , ref, ref) \
  22. \
  23. _(EQ, C , ref, ref) \
  24. _(NE, C , ref, ref) \
  25. \
  26. _(ABC, N , ref, ref) \
  27. _(RETF, S , ref, ref) \
  28. \
  29. /* Miscellaneous ops. */ \
  30. _(NOP, N , ___, ___) \
  31. _(BASE, N , lit, lit) \
  32. _(PVAL, N , lit, ___) \
  33. _(GCSTEP, S , ___, ___) \
  34. _(HIOP, S , ref, ref) \
  35. _(LOOP, S , ___, ___) \
  36. _(USE, S , ref, ___) \
  37. _(PHI, S , ref, ref) \
  38. _(RENAME, S , ref, lit) \
  39. _(PROF, S , ___, ___) \
  40. \
  41. /* Constants. */ \
  42. _(KPRI, N , ___, ___) \
  43. _(KINT, N , cst, ___) \
  44. _(KGC, N , cst, ___) \
  45. _(KPTR, N , cst, ___) \
  46. _(KKPTR, N , cst, ___) \
  47. _(KNULL, N , cst, ___) \
  48. _(KNUM, N , cst, ___) \
  49. _(KINT64, N , cst, ___) \
  50. _(KSLOT, N , ref, lit) \
  51. \
  52. /* Bit ops. */ \
  53. _(BNOT, N , ref, ___) \
  54. _(BSWAP, N , ref, ___) \
  55. _(BAND, C , ref, ref) \
  56. _(BOR, C , ref, ref) \
  57. _(BXOR, C , ref, ref) \
  58. _(BSHL, N , ref, ref) \
  59. _(BSHR, N , ref, ref) \
  60. _(BSAR, N , ref, ref) \
  61. _(BROL, N , ref, ref) \
  62. _(BROR, N , ref, ref) \
  63. \
  64. /* Arithmetic ops. ORDER ARITH */ \
  65. _(ADD, C , ref, ref) \
  66. _(SUB, N , ref, ref) \
  67. _(MUL, C , ref, ref) \
  68. _(DIV, N , ref, ref) \
  69. _(MOD, N , ref, ref) \
  70. _(POW, N , ref, ref) \
  71. _(NEG, N , ref, ref) \
  72. \
  73. _(ABS, N , ref, ref) \
  74. _(LDEXP, N , ref, ref) \
  75. _(MIN, N , ref, ref) \
  76. _(MAX, N , ref, ref) \
  77. _(FPMATH, N , ref, lit) \
  78. \
  79. /* Overflow-checking arithmetic ops. */ \
  80. _(ADDOV, CW, ref, ref) \
  81. _(SUBOV, NW, ref, ref) \
  82. _(MULOV, CW, ref, ref) \
  83. \
  84. /* Memory ops. A = array, H = hash, U = upvalue, F = field, S = stack. */ \
  85. \
  86. /* Memory references. */ \
  87. _(AREF, R , ref, ref) \
  88. _(HREFK, R , ref, ref) \
  89. _(HREF, L , ref, ref) \
  90. _(NEWREF, S , ref, ref) \
  91. _(UREFO, LW, ref, lit) \
  92. _(UREFC, LW, ref, lit) \
  93. _(FREF, R , ref, lit) \
  94. _(TMPREF, S , ref, lit) \
  95. _(STRREF, N , ref, ref) \
  96. _(LREF, L , ___, ___) \
  97. \
  98. /* Loads and Stores. These must be in the same order. */ \
  99. _(ALOAD, L , ref, ___) \
  100. _(HLOAD, L , ref, ___) \
  101. _(ULOAD, L , ref, ___) \
  102. _(FLOAD, L , ref, lit) \
  103. _(XLOAD, L , ref, lit) \
  104. _(SLOAD, L , lit, lit) \
  105. _(VLOAD, L , ref, lit) \
  106. _(ALEN, L , ref, ref) \
  107. \
  108. _(ASTORE, S , ref, ref) \
  109. _(HSTORE, S , ref, ref) \
  110. _(USTORE, S , ref, ref) \
  111. _(FSTORE, S , ref, ref) \
  112. _(XSTORE, S , ref, ref) \
  113. \
  114. /* Allocations. */ \
  115. _(SNEW, N , ref, ref) /* CSE is ok, not marked as A. */ \
  116. _(XSNEW, A , ref, ref) \
  117. _(TNEW, AW, lit, lit) \
  118. _(TDUP, AW, ref, ___) \
  119. _(CNEW, AW, ref, ref) \
  120. _(CNEWI, NW, ref, ref) /* CSE is ok, not marked as A. */ \
  121. \
  122. /* Buffer operations. */ \
  123. _(BUFHDR, L , ref, lit) \
  124. _(BUFPUT, LW, ref, ref) \
  125. _(BUFSTR, AW, ref, ref) \
  126. \
  127. /* Barriers. */ \
  128. _(TBAR, S , ref, ___) \
  129. _(OBAR, S , ref, ref) \
  130. _(XBAR, S , ___, ___) \
  131. \
  132. /* Type conversions. */ \
  133. _(CONV, N , ref, lit) \
  134. _(TOBIT, N , ref, ref) \
  135. _(TOSTR, N , ref, lit) \
  136. _(STRTO, N , ref, ___) \
  137. \
  138. /* Calls. */ \
  139. _(CALLN, NW, ref, lit) \
  140. _(CALLA, AW, ref, lit) \
  141. _(CALLL, LW, ref, lit) \
  142. _(CALLS, S , ref, lit) \
  143. _(CALLXS, S , ref, ref) \
  144. _(CARG, N , ref, ref) \
  145. \
  146. /* End of list. */
  147. /* IR opcodes (max. 256). */
  148. typedef enum {
  149. #define IRENUM(name, m, m1, m2) IR_##name,
  150. IRDEF(IRENUM)
  151. #undef IRENUM
  152. IR__MAX
  153. } IROp;
  154. /* Stored opcode. */
  155. typedef uint8_t IROp1;
  156. LJ_STATIC_ASSERT(((int)IR_EQ^1) == (int)IR_NE);
  157. LJ_STATIC_ASSERT(((int)IR_LT^1) == (int)IR_GE);
  158. LJ_STATIC_ASSERT(((int)IR_LE^1) == (int)IR_GT);
  159. LJ_STATIC_ASSERT(((int)IR_LT^3) == (int)IR_GT);
  160. LJ_STATIC_ASSERT(((int)IR_LT^4) == (int)IR_ULT);
  161. /* Delta between xLOAD and xSTORE. */
  162. #define IRDELTA_L2S ((int)IR_ASTORE - (int)IR_ALOAD)
  163. LJ_STATIC_ASSERT((int)IR_HLOAD + IRDELTA_L2S == (int)IR_HSTORE);
  164. LJ_STATIC_ASSERT((int)IR_ULOAD + IRDELTA_L2S == (int)IR_USTORE);
  165. LJ_STATIC_ASSERT((int)IR_FLOAD + IRDELTA_L2S == (int)IR_FSTORE);
  166. LJ_STATIC_ASSERT((int)IR_XLOAD + IRDELTA_L2S == (int)IR_XSTORE);
  167. /* -- Named IR literals --------------------------------------------------- */
  168. /* FPMATH sub-functions. ORDER FPM. */
  169. #define IRFPMDEF(_) \
  170. _(FLOOR) _(CEIL) _(TRUNC) /* Must be first and in this order. */ \
  171. _(SQRT) _(LOG) _(LOG2) \
  172. _(OTHER)
  173. typedef enum {
  174. #define FPMENUM(name) IRFPM_##name,
  175. IRFPMDEF(FPMENUM)
  176. #undef FPMENUM
  177. IRFPM__MAX
  178. } IRFPMathOp;
  179. /* FLOAD fields. */
  180. #define IRFLDEF(_) \
  181. _(STR_LEN, offsetof(GCstr, len)) \
  182. _(FUNC_ENV, offsetof(GCfunc, l.env)) \
  183. _(FUNC_PC, offsetof(GCfunc, l.pc)) \
  184. _(FUNC_FFID, offsetof(GCfunc, l.ffid)) \
  185. _(THREAD_ENV, offsetof(lua_State, env)) \
  186. _(TAB_META, offsetof(GCtab, metatable)) \
  187. _(TAB_ARRAY, offsetof(GCtab, array)) \
  188. _(TAB_NODE, offsetof(GCtab, node)) \
  189. _(TAB_ASIZE, offsetof(GCtab, asize)) \
  190. _(TAB_HMASK, offsetof(GCtab, hmask)) \
  191. _(TAB_NOMM, offsetof(GCtab, nomm)) \
  192. _(UDATA_META, offsetof(GCudata, metatable)) \
  193. _(UDATA_UDTYPE, offsetof(GCudata, udtype)) \
  194. _(UDATA_FILE, sizeof(GCudata)) \
  195. _(SBUF_W, sizeof(GCudata) + offsetof(SBufExt, w)) \
  196. _(SBUF_E, sizeof(GCudata) + offsetof(SBufExt, e)) \
  197. _(SBUF_B, sizeof(GCudata) + offsetof(SBufExt, b)) \
  198. _(SBUF_L, sizeof(GCudata) + offsetof(SBufExt, L)) \
  199. _(SBUF_REF, sizeof(GCudata) + offsetof(SBufExt, cowref)) \
  200. _(SBUF_R, sizeof(GCudata) + offsetof(SBufExt, r)) \
  201. _(CDATA_CTYPEID, offsetof(GCcdata, ctypeid)) \
  202. _(CDATA_PTR, sizeof(GCcdata)) \
  203. _(CDATA_INT, sizeof(GCcdata)) \
  204. _(CDATA_INT64, sizeof(GCcdata)) \
  205. _(CDATA_INT64_4, sizeof(GCcdata) + 4)
  206. typedef enum {
  207. #define FLENUM(name, ofs) IRFL_##name,
  208. IRFLDEF(FLENUM)
  209. #undef FLENUM
  210. IRFL__MAX
  211. } IRFieldID;
  212. /* TMPREF mode bits, stored in op2. */
  213. #define IRTMPREF_IN1 0x01 /* First input value. */
  214. #define IRTMPREF_OUT1 0x02 /* First output value. */
  215. #define IRTMPREF_OUT2 0x04 /* Second output value. */
  216. /* SLOAD mode bits, stored in op2. */
  217. #define IRSLOAD_PARENT 0x01 /* Coalesce with parent trace. */
  218. #define IRSLOAD_FRAME 0x02 /* Load 32 bits of ftsz. */
  219. #define IRSLOAD_TYPECHECK 0x04 /* Needs type check. */
  220. #define IRSLOAD_CONVERT 0x08 /* Number to integer conversion. */
  221. #define IRSLOAD_READONLY 0x10 /* Read-only, omit slot store. */
  222. #define IRSLOAD_INHERIT 0x20 /* Inherited by exits/side traces. */
  223. #define IRSLOAD_KEYINDEX 0x40 /* Table traversal key index. */
  224. /* XLOAD mode bits, stored in op2. */
  225. #define IRXLOAD_READONLY 0x01 /* Load from read-only data. */
  226. #define IRXLOAD_VOLATILE 0x02 /* Load from volatile data. */
  227. #define IRXLOAD_UNALIGNED 0x04 /* Unaligned load. */
  228. /* BUFHDR mode, stored in op2. */
  229. #define IRBUFHDR_RESET 0 /* Reset buffer. */
  230. #define IRBUFHDR_APPEND 1 /* Append to buffer. */
  231. #define IRBUFHDR_WRITE 2 /* Write to string buffer. */
  232. /* CONV mode, stored in op2. */
  233. #define IRCONV_SRCMASK 0x001f /* Source IRType. */
  234. #define IRCONV_DSTMASK 0x03e0 /* Dest. IRType (also in ir->t). */
  235. #define IRCONV_DSH 5
  236. #define IRCONV_NUM_INT ((IRT_NUM<<IRCONV_DSH)|IRT_INT)
  237. #define IRCONV_INT_NUM ((IRT_INT<<IRCONV_DSH)|IRT_NUM)
  238. #define IRCONV_SEXT 0x0800 /* Sign-extend integer to integer. */
  239. #define IRCONV_MODEMASK 0x0fff
  240. #define IRCONV_CONVMASK 0xf000
  241. #define IRCONV_CSH 12
  242. /* Number to integer conversion mode. Ordered by strength of the checks. */
  243. #define IRCONV_TOBIT (0<<IRCONV_CSH) /* None. Cache only: TOBIT conv. */
  244. #define IRCONV_ANY (1<<IRCONV_CSH) /* Any FP number is ok. */
  245. #define IRCONV_INDEX (2<<IRCONV_CSH) /* Check + special backprop rules. */
  246. #define IRCONV_CHECK (3<<IRCONV_CSH) /* Number checked for integerness. */
  247. #define IRCONV_NONE IRCONV_ANY /* INT|*64 no conv, but change type. */
  248. /* TOSTR mode, stored in op2. */
  249. #define IRTOSTR_INT 0 /* Convert integer to string. */
  250. #define IRTOSTR_NUM 1 /* Convert number to string. */
  251. #define IRTOSTR_CHAR 2 /* Convert char value to string. */
  252. /* -- IR operands --------------------------------------------------------- */
  253. /* IR operand mode (2 bit). */
  254. typedef enum {
  255. IRMref, /* IR reference. */
  256. IRMlit, /* 16 bit unsigned literal. */
  257. IRMcst, /* Constant literal: i, gcr or ptr. */
  258. IRMnone /* Unused operand. */
  259. } IRMode;
  260. #define IRM___ IRMnone
  261. /* Mode bits: Commutative, {Normal/Ref, Alloc, Load, Store}, Non-weak guard. */
  262. #define IRM_C 0x10
  263. #define IRM_N 0x00
  264. #define IRM_R IRM_N
  265. #define IRM_A 0x20
  266. #define IRM_L 0x40
  267. #define IRM_S 0x60
  268. #define IRM_W 0x80
  269. #define IRM_NW (IRM_N|IRM_W)
  270. #define IRM_CW (IRM_C|IRM_W)
  271. #define IRM_AW (IRM_A|IRM_W)
  272. #define IRM_LW (IRM_L|IRM_W)
  273. #define irm_op1(m) ((IRMode)((m)&3))
  274. #define irm_op2(m) ((IRMode)(((m)>>2)&3))
  275. #define irm_iscomm(m) ((m) & IRM_C)
  276. #define irm_kind(m) ((m) & IRM_S)
  277. #define IRMODE(name, m, m1, m2) (((IRM##m1)|((IRM##m2)<<2)|(IRM_##m))^IRM_W),
  278. LJ_DATA const uint8_t lj_ir_mode[IR__MAX+1];
  279. /* -- IR instruction types ------------------------------------------------ */
  280. #define IRTSIZE_PGC (LJ_GC64 ? 8 : 4)
  281. /* Map of itypes to non-negative numbers and their sizes. ORDER LJ_T.
  282. ** LJ_TUPVAL/LJ_TTRACE never appear in a TValue. Use these itypes for
  283. ** IRT_P32 and IRT_P64, which never escape the IR.
  284. ** The various integers are only used in the IR and can only escape to
  285. ** a TValue after implicit or explicit conversion. Their types must be
  286. ** contiguous and next to IRT_NUM (see the typerange macros below).
  287. */
  288. #define IRTDEF(_) \
  289. _(NIL, 4) _(FALSE, 4) _(TRUE, 4) _(LIGHTUD, LJ_64 ? 8 : 4) \
  290. _(STR, IRTSIZE_PGC) _(P32, 4) _(THREAD, IRTSIZE_PGC) _(PROTO, IRTSIZE_PGC) \
  291. _(FUNC, IRTSIZE_PGC) _(P64, 8) _(CDATA, IRTSIZE_PGC) _(TAB, IRTSIZE_PGC) \
  292. _(UDATA, IRTSIZE_PGC) \
  293. _(FLOAT, 4) _(NUM, 8) _(I8, 1) _(U8, 1) _(I16, 2) _(U16, 2) \
  294. _(INT, 4) _(U32, 4) _(I64, 8) _(U64, 8) \
  295. _(SOFTFP, 4) /* There is room for 8 more types. */
  296. /* IR result type and flags (8 bit). */
  297. typedef enum {
  298. #define IRTENUM(name, size) IRT_##name,
  299. IRTDEF(IRTENUM)
  300. #undef IRTENUM
  301. IRT__MAX,
  302. /* Native pointer type and the corresponding integer type. */
  303. IRT_PTR = LJ_64 ? IRT_P64 : IRT_P32,
  304. IRT_PGC = LJ_GC64 ? IRT_P64 : IRT_P32,
  305. IRT_IGC = LJ_GC64 ? IRT_I64 : IRT_INT,
  306. IRT_INTP = LJ_64 ? IRT_I64 : IRT_INT,
  307. IRT_UINTP = LJ_64 ? IRT_U64 : IRT_U32,
  308. /* Additional flags. */
  309. IRT_MARK = 0x20, /* Marker for misc. purposes. */
  310. IRT_ISPHI = 0x40, /* Instruction is left or right PHI operand. */
  311. IRT_GUARD = 0x80, /* Instruction is a guard. */
  312. /* Masks. */
  313. IRT_TYPE = 0x1f,
  314. IRT_T = 0xff
  315. } IRType;
  316. #define irtype_ispri(irt) ((uint32_t)(irt) <= IRT_TRUE)
  317. /* Stored IRType. */
  318. typedef struct IRType1 { uint8_t irt; } IRType1;
  319. #define IRT(o, t) ((uint32_t)(((o)<<8) | (t)))
  320. #define IRTI(o) (IRT((o), IRT_INT))
  321. #define IRTN(o) (IRT((o), IRT_NUM))
  322. #define IRTG(o, t) (IRT((o), IRT_GUARD|(t)))
  323. #define IRTGI(o) (IRT((o), IRT_GUARD|IRT_INT))
  324. #define irt_t(t) ((IRType)(t).irt)
  325. #define irt_type(t) ((IRType)((t).irt & IRT_TYPE))
  326. #define irt_sametype(t1, t2) ((((t1).irt ^ (t2).irt) & IRT_TYPE) == 0)
  327. #define irt_typerange(t, first, last) \
  328. ((uint32_t)((t).irt & IRT_TYPE) - (uint32_t)(first) <= (uint32_t)(last-first))
  329. #define irt_isnil(t) (irt_type(t) == IRT_NIL)
  330. #define irt_ispri(t) ((uint32_t)irt_type(t) <= IRT_TRUE)
  331. #define irt_islightud(t) (irt_type(t) == IRT_LIGHTUD)
  332. #define irt_isstr(t) (irt_type(t) == IRT_STR)
  333. #define irt_istab(t) (irt_type(t) == IRT_TAB)
  334. #define irt_iscdata(t) (irt_type(t) == IRT_CDATA)
  335. #define irt_isfloat(t) (irt_type(t) == IRT_FLOAT)
  336. #define irt_isnum(t) (irt_type(t) == IRT_NUM)
  337. #define irt_isint(t) (irt_type(t) == IRT_INT)
  338. #define irt_isi8(t) (irt_type(t) == IRT_I8)
  339. #define irt_isu8(t) (irt_type(t) == IRT_U8)
  340. #define irt_isi16(t) (irt_type(t) == IRT_I16)
  341. #define irt_isu16(t) (irt_type(t) == IRT_U16)
  342. #define irt_isu32(t) (irt_type(t) == IRT_U32)
  343. #define irt_isi64(t) (irt_type(t) == IRT_I64)
  344. #define irt_isu64(t) (irt_type(t) == IRT_U64)
  345. #define irt_isp32(t) (irt_type(t) == IRT_P32)
  346. #define irt_isfp(t) (irt_isnum(t) || irt_isfloat(t))
  347. #define irt_isinteger(t) (irt_typerange((t), IRT_I8, IRT_INT))
  348. #define irt_isgcv(t) (irt_typerange((t), IRT_STR, IRT_UDATA))
  349. #define irt_isaddr(t) (irt_typerange((t), IRT_LIGHTUD, IRT_UDATA))
  350. #define irt_isint64(t) (irt_typerange((t), IRT_I64, IRT_U64))
  351. #if LJ_GC64
  352. /* Include IRT_NIL, so IR(ASMREF_L) (aka REF_NIL) is considered 64 bit. */
  353. #define IRT_IS64 \
  354. ((1u<<IRT_NUM)|(1u<<IRT_I64)|(1u<<IRT_U64)|(1u<<IRT_P64)|\
  355. (1u<<IRT_LIGHTUD)|(1u<<IRT_STR)|(1u<<IRT_THREAD)|(1u<<IRT_PROTO)|\
  356. (1u<<IRT_FUNC)|(1u<<IRT_CDATA)|(1u<<IRT_TAB)|(1u<<IRT_UDATA)|\
  357. (1u<<IRT_NIL))
  358. #elif LJ_64
  359. #define IRT_IS64 \
  360. ((1u<<IRT_NUM)|(1u<<IRT_I64)|(1u<<IRT_U64)|(1u<<IRT_P64)|(1u<<IRT_LIGHTUD))
  361. #else
  362. #define IRT_IS64 \
  363. ((1u<<IRT_NUM)|(1u<<IRT_I64)|(1u<<IRT_U64))
  364. #endif
  365. #define irt_is64(t) ((IRT_IS64 >> irt_type(t)) & 1)
  366. #define irt_is64orfp(t) (((IRT_IS64|(1u<<IRT_FLOAT))>>irt_type(t)) & 1)
  367. #define irt_size(t) (lj_ir_type_size[irt_t((t))])
  368. LJ_DATA const uint8_t lj_ir_type_size[];
  369. static LJ_AINLINE IRType itype2irt(const TValue *tv)
  370. {
  371. if (tvisint(tv))
  372. return IRT_INT;
  373. else if (tvisnum(tv))
  374. return IRT_NUM;
  375. #if LJ_64 && !LJ_GC64
  376. else if (tvislightud(tv))
  377. return IRT_LIGHTUD;
  378. #endif
  379. else
  380. return (IRType)~itype(tv);
  381. }
  382. static LJ_AINLINE uint32_t irt_toitype_(IRType t)
  383. {
  384. lj_assertX(!LJ_64 || LJ_GC64 || t != IRT_LIGHTUD,
  385. "no plain type tag for lightuserdata");
  386. if (LJ_DUALNUM && t > IRT_NUM) {
  387. return LJ_TISNUM;
  388. } else {
  389. lj_assertX(t <= IRT_NUM, "no plain type tag for IR type %d", t);
  390. return ~(uint32_t)t;
  391. }
  392. }
  393. #define irt_toitype(t) irt_toitype_(irt_type((t)))
  394. #define irt_isguard(t) ((t).irt & IRT_GUARD)
  395. #define irt_ismarked(t) ((t).irt & IRT_MARK)
  396. #define irt_setmark(t) ((t).irt |= IRT_MARK)
  397. #define irt_clearmark(t) ((t).irt &= ~IRT_MARK)
  398. #define irt_isphi(t) ((t).irt & IRT_ISPHI)
  399. #define irt_setphi(t) ((t).irt |= IRT_ISPHI)
  400. #define irt_clearphi(t) ((t).irt &= ~IRT_ISPHI)
  401. /* Stored combined IR opcode and type. */
  402. typedef uint16_t IROpT;
  403. /* -- IR references ------------------------------------------------------- */
  404. /* IR references. */
  405. typedef uint16_t IRRef1; /* One stored reference. */
  406. typedef uint32_t IRRef2; /* Two stored references. */
  407. typedef uint32_t IRRef; /* Used to pass around references. */
  408. /* Fixed references. */
  409. enum {
  410. REF_BIAS = 0x8000,
  411. REF_TRUE = REF_BIAS-3,
  412. REF_FALSE = REF_BIAS-2,
  413. REF_NIL = REF_BIAS-1, /* \--- Constants grow downwards. */
  414. REF_BASE = REF_BIAS, /* /--- IR grows upwards. */
  415. REF_FIRST = REF_BIAS+1,
  416. REF_DROP = 0xffff
  417. };
  418. /* Note: IRMlit operands must be < REF_BIAS, too!
  419. ** This allows for fast and uniform manipulation of all operands
  420. ** without looking up the operand mode in lj_ir_mode:
  421. ** - CSE calculates the maximum reference of two operands.
  422. ** This must work with mixed reference/literal operands, too.
  423. ** - DCE marking only checks for operand >= REF_BIAS.
  424. ** - LOOP needs to substitute reference operands.
  425. ** Constant references and literals must not be modified.
  426. */
  427. #define IRREF2(lo, hi) ((IRRef2)(lo) | ((IRRef2)(hi) << 16))
  428. #define irref_isk(ref) ((ref) < REF_BIAS)
  429. /* Tagged IR references (32 bit).
  430. **
  431. ** +-------+-------+---------------+
  432. ** | irt | flags | ref |
  433. ** +-------+-------+---------------+
  434. **
  435. ** The tag holds a copy of the IRType and speeds up IR type checks.
  436. */
  437. typedef uint32_t TRef;
  438. #define TREF_REFMASK 0x0000ffff
  439. #define TREF_FRAME 0x00010000
  440. #define TREF_CONT 0x00020000
  441. #define TREF_KEYINDEX 0x00100000
  442. #define TREF(ref, t) ((TRef)((ref) + ((t)<<24)))
  443. #define tref_ref(tr) ((IRRef1)(tr))
  444. #define tref_t(tr) ((IRType)((tr)>>24))
  445. #define tref_type(tr) ((IRType)(((tr)>>24) & IRT_TYPE))
  446. #define tref_typerange(tr, first, last) \
  447. ((((tr)>>24) & IRT_TYPE) - (TRef)(first) <= (TRef)(last-first))
  448. #define tref_istype(tr, t) (((tr) & (IRT_TYPE<<24)) == ((t)<<24))
  449. #define tref_isnil(tr) (tref_istype((tr), IRT_NIL))
  450. #define tref_isfalse(tr) (tref_istype((tr), IRT_FALSE))
  451. #define tref_istrue(tr) (tref_istype((tr), IRT_TRUE))
  452. #define tref_islightud(tr) (tref_istype((tr), IRT_LIGHTUD))
  453. #define tref_isstr(tr) (tref_istype((tr), IRT_STR))
  454. #define tref_isfunc(tr) (tref_istype((tr), IRT_FUNC))
  455. #define tref_iscdata(tr) (tref_istype((tr), IRT_CDATA))
  456. #define tref_istab(tr) (tref_istype((tr), IRT_TAB))
  457. #define tref_isudata(tr) (tref_istype((tr), IRT_UDATA))
  458. #define tref_isnum(tr) (tref_istype((tr), IRT_NUM))
  459. #define tref_isint(tr) (tref_istype((tr), IRT_INT))
  460. #define tref_isbool(tr) (tref_typerange((tr), IRT_FALSE, IRT_TRUE))
  461. #define tref_ispri(tr) (tref_typerange((tr), IRT_NIL, IRT_TRUE))
  462. #define tref_istruecond(tr) (!tref_typerange((tr), IRT_NIL, IRT_FALSE))
  463. #define tref_isinteger(tr) (tref_typerange((tr), IRT_I8, IRT_INT))
  464. #define tref_isnumber(tr) (tref_typerange((tr), IRT_NUM, IRT_INT))
  465. #define tref_isnumber_str(tr) (tref_isnumber((tr)) || tref_isstr((tr)))
  466. #define tref_isgcv(tr) (tref_typerange((tr), IRT_STR, IRT_UDATA))
  467. #define tref_isk(tr) (irref_isk(tref_ref((tr))))
  468. #define tref_isk2(tr1, tr2) (irref_isk(tref_ref((tr1) | (tr2))))
  469. #define TREF_PRI(t) (TREF(REF_NIL-(t), (t)))
  470. #define TREF_NIL (TREF_PRI(IRT_NIL))
  471. #define TREF_FALSE (TREF_PRI(IRT_FALSE))
  472. #define TREF_TRUE (TREF_PRI(IRT_TRUE))
  473. /* -- IR format ----------------------------------------------------------- */
  474. /* IR instruction format (64 bit).
  475. **
  476. ** 16 16 8 8 8 8
  477. ** +-------+-------+---+---+---+---+
  478. ** | op1 | op2 | t | o | r | s |
  479. ** +-------+-------+---+---+---+---+
  480. ** | op12/i/gco32 | ot | prev | (alternative fields in union)
  481. ** +-------+-------+---+---+---+---+
  482. ** | TValue/gco64 | (2nd IR slot for 64 bit constants)
  483. ** +---------------+-------+-------+
  484. ** 32 16 16
  485. **
  486. ** prev is only valid prior to register allocation and then reused for r + s.
  487. */
  488. typedef union IRIns {
  489. struct {
  490. LJ_ENDIAN_LOHI(
  491. IRRef1 op1; /* IR operand 1. */
  492. , IRRef1 op2; /* IR operand 2. */
  493. )
  494. IROpT ot; /* IR opcode and type (overlaps t and o). */
  495. IRRef1 prev; /* Previous ins in same chain (overlaps r and s). */
  496. };
  497. struct {
  498. IRRef2 op12; /* IR operand 1 and 2 (overlaps op1 and op2). */
  499. LJ_ENDIAN_LOHI(
  500. IRType1 t; /* IR type. */
  501. , IROp1 o; /* IR opcode. */
  502. )
  503. LJ_ENDIAN_LOHI(
  504. uint8_t r; /* Register allocation (overlaps prev). */
  505. , uint8_t s; /* Spill slot allocation (overlaps prev). */
  506. )
  507. };
  508. int32_t i; /* 32 bit signed integer literal (overlaps op12). */
  509. GCRef gcr; /* GCobj constant (overlaps op12 or entire slot). */
  510. MRef ptr; /* Pointer constant (overlaps op12 or entire slot). */
  511. TValue tv; /* TValue constant (overlaps entire slot). */
  512. } IRIns;
  513. #define ir_isk64(ir) \
  514. ((ir)->o == IR_KNUM || (ir)->o == IR_KINT64 || \
  515. (LJ_GC64 && \
  516. ((ir)->o == IR_KGC || (ir)->o == IR_KPTR || (ir)->o == IR_KKPTR)))
  517. #define ir_kgc(ir) check_exp((ir)->o == IR_KGC, gcref((ir)[LJ_GC64].gcr))
  518. #define ir_kstr(ir) (gco2str(ir_kgc((ir))))
  519. #define ir_ktab(ir) (gco2tab(ir_kgc((ir))))
  520. #define ir_kfunc(ir) (gco2func(ir_kgc((ir))))
  521. #define ir_kcdata(ir) (gco2cd(ir_kgc((ir))))
  522. #define ir_knum(ir) check_exp((ir)->o == IR_KNUM, &(ir)[1].tv)
  523. #define ir_kint64(ir) check_exp((ir)->o == IR_KINT64, &(ir)[1].tv)
  524. #define ir_k64(ir) check_exp(ir_isk64(ir), &(ir)[1].tv)
  525. #define ir_kptr(ir) \
  526. check_exp((ir)->o == IR_KPTR || (ir)->o == IR_KKPTR, \
  527. mref((ir)[LJ_GC64].ptr, void))
  528. /* A store or any other op with a non-weak guard has a side-effect. */
  529. static LJ_AINLINE int ir_sideeff(IRIns *ir)
  530. {
  531. return (((ir->t.irt | ~IRT_GUARD) & lj_ir_mode[ir->o]) >= IRM_S);
  532. }
  533. LJ_STATIC_ASSERT((int)IRT_GUARD == (int)IRM_W);
  534. /* Replace IR instruction with NOP. */
  535. static LJ_AINLINE void lj_ir_nop(IRIns *ir)
  536. {
  537. ir->ot = IRT(IR_NOP, IRT_NIL);
  538. ir->op1 = ir->op2 = 0;
  539. ir->prev = 0;
  540. }
  541. #endif