lj_ir.h 17 KB

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