src/lj_ir.h - luajit-2.0-src

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /*
  2. ** SSA IR (Intermediate Representation) format.
  3. ** Copyright (C) 2005-2015 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.   _(ATAN2,        N , ref, ref) \
  75.   _(LDEXP,        N , ref, ref) \
  76.   _(MIN,        C , ref, ref) \
  77.   _(MAX,        C , ref, ref) \
  78.   _(FPMATH,        N , ref, lit) \
  79.   \
  80.   /* Overflow-checking arithmetic ops. */ \
  81.   _(ADDOV,        CW, ref, ref) \
  82.   _(SUBOV,        NW, ref, ref) \
  83.   _(MULOV,        CW, ref, ref) \
  84.   \
  85.   /* Memory ops. A = array, H = hash, U = upvalue, F = field, S = stack. */ \
  86.   \
  87.   /* Memory references. */ \
  88.   _(AREF,        R , ref, ref) \
  89.   _(HREFK,        R , ref, ref) \
  90.   _(HREF,        L , ref, ref) \
  91.   _(NEWREF,        S , ref, ref) \
  92.   _(UREFO,        LW, ref, lit) \
  93.   _(UREFC,        LW, ref, lit) \
  94.   _(FREF,        R , 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, ___) \
  106.   \
  107.   _(ASTORE,        S , ref, ref) \
  108.   _(HSTORE,        S , ref, ref) \
  109.   _(USTORE,        S , ref, ref) \
  110.   _(FSTORE,        S , ref, ref) \
  111.   _(XSTORE,        S , ref, ref) \
  112.   \
  113.   /* Allocations. */ \
  114.   _(SNEW,        N , ref, ref)  /* CSE is ok, not marked as A. */ \
  115.   _(XSNEW,        A , ref, ref) \
  116.   _(TNEW,        AW, lit, lit) \
  117.   _(TDUP,        AW, ref, ___) \
  118.   _(CNEW,        AW, ref, ref) \
  119.   _(CNEWI,        NW, ref, ref)  /* CSE is ok, not marked as A. */ \
  120.   \
  121.   /* Buffer operations. */ \
  122.   _(BUFHDR,        L , ref, lit) \
  123.   _(BUFPUT,        L , ref, ref) \
  124.   _(BUFSTR,        A , ref, ref) \
  125.   \
  126.   /* Barriers. */ \
  127.   _(TBAR,        S , ref, ___) \
  128.   _(OBAR,        S , ref, ref) \
  129.   _(XBAR,        S , ___, ___) \
  130.   \
  131.   /* Type conversions. */ \
  132.   _(CONV,        NW, ref, lit) \
  133.   _(TOBIT,        N , ref, ref) \
  134.   _(TOSTR,        N , ref, lit) \
  135.   _(STRTO,        N , ref, ___) \
  136.   \
  137.   /* Calls. */ \
  138.   _(CALLN,        N , ref, lit) \
  139.   _(CALLA,        A , ref, lit) \
  140.   _(CALLL,        L , ref, lit) \
  141.   _(CALLS,        S , ref, lit) \
  142.   _(CALLXS,        S , ref, ref) \
  143.   _(CARG,        N , ref, ref) \
  144.   \
  145.   /* End of list. */

  146. /* IR opcodes (max. 256). */
  147. typedef enum {
  148. #define IRENUM(name, m, m1, m2)        IR_##name,
  149. IRDEF(IRENUM)
  150. #undef IRENUM
  151.   IR__MAX
  152. } IROp;

  153. /* Stored opcode. */
  154. typedef uint8_t IROp1;

  155. LJ_STATIC_ASSERT(((int)IR_EQ^1) == (int)IR_NE);
  156. LJ_STATIC_ASSERT(((int)IR_LT^1) == (int)IR_GE);
  157. LJ_STATIC_ASSERT(((int)IR_LE^1) == (int)IR_GT);
  158. LJ_STATIC_ASSERT(((int)IR_LT^3) == (int)IR_GT);
  159. LJ_STATIC_ASSERT(((int)IR_LT^4) == (int)IR_ULT);

  160. /* Delta between xLOAD and xSTORE. */
  161. #define IRDELTA_L2S                ((int)IR_ASTORE - (int)IR_ALOAD)

  162. LJ_STATIC_ASSERT((int)IR_HLOAD + IRDELTA_L2S == (int)IR_HSTORE);
  163. LJ_STATIC_ASSERT((int)IR_ULOAD + IRDELTA_L2S == (int)IR_USTORE);
  164. LJ_STATIC_ASSERT((int)IR_FLOAD + IRDELTA_L2S == (int)IR_FSTORE);
  165. LJ_STATIC_ASSERT((int)IR_XLOAD + IRDELTA_L2S == (int)IR_XSTORE);

  166. /* -- Named IR literals --------------------------------------------------- */

  167. /* FPMATH sub-functions. ORDER FPM. */
  168. #define IRFPMDEF(_) \
  169.   _(FLOOR) _(CEIL) _(TRUNC)  /* Must be first and in this order. */ \
  170.   _(SQRT) _(EXP) _(EXP2) _(LOG) _(LOG2) _(LOG10) \
  171.   _(SIN) _(COS) _(TAN) \
  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.   _(CDATA_CTYPEID, offsetof(GCcdata, ctypeid)) \
  196.   _(CDATA_PTR,        sizeof(GCcdata)) \
  197.   _(CDATA_INT, sizeof(GCcdata)) \
  198.   _(CDATA_INT64, sizeof(GCcdata)) \
  199.   _(CDATA_INT64_4, sizeof(GCcdata) + 4)

  200. typedef enum {
  201. #define FLENUM(name, ofs)        IRFL_##name,
  202. IRFLDEF(FLENUM)
  203. #undef FLENUM
  204.   IRFL__MAX
  205. } IRFieldID;

  206. /* SLOAD mode bits, stored in op2. */
  207. #define IRSLOAD_PARENT                0x01        /* Coalesce with parent trace. */
  208. #define IRSLOAD_FRAME                0x02        /* Load hiword of frame. */
  209. #define IRSLOAD_TYPECHECK        0x04        /* Needs type check. */
  210. #define IRSLOAD_CONVERT                0x08        /* Number to integer conversion. */
  211. #define IRSLOAD_READONLY        0x10        /* Read-only, omit slot store. */
  212. #define IRSLOAD_INHERIT                0x20        /* Inherited by exits/side traces. */

  213. /* XLOAD mode, stored in op2. */
  214. #define IRXLOAD_READONLY        1        /* Load from read-only data. */
  215. #define IRXLOAD_VOLATILE        2        /* Load from volatile data. */
  216. #define IRXLOAD_UNALIGNED        4        /* Unaligned load. */

  217. /* BUFHDR mode, stored in op2. */
  218. #define IRBUFHDR_RESET                0        /* Reset buffer. */
  219. #define IRBUFHDR_APPEND                1        /* Append to buffer. */

  220. /* CONV mode, stored in op2. */
  221. #define IRCONV_SRCMASK                0x001f        /* Source IRType. */
  222. #define IRCONV_DSTMASK                0x03e0        /* Dest. IRType (also in ir->t). */
  223. #define IRCONV_DSH                5
  224. #define IRCONV_NUM_INT                ((IRT_NUM<<IRCONV_DSH)|IRT_INT)
  225. #define IRCONV_INT_NUM                ((IRT_INT<<IRCONV_DSH)|IRT_NUM)
  226. #define IRCONV_SEXT                0x0800        /* Sign-extend integer to integer. */
  227. #define IRCONV_MODEMASK                0x0fff
  228. #define IRCONV_CONVMASK                0xf000
  229. #define IRCONV_CSH                12
  230. /* Number to integer conversion mode. Ordered by strength of the checks. */
  231. #define IRCONV_TOBIT  (0<<IRCONV_CSH)        /* None. Cache only: TOBIT conv. */
  232. #define IRCONV_ANY    (1<<IRCONV_CSH)        /* Any FP number is ok. */
  233. #define IRCONV_INDEX  (2<<IRCONV_CSH)        /* Check + special backprop rules. */
  234. #define IRCONV_CHECK  (3<<IRCONV_CSH)        /* Number checked for integerness. */

  235. /* TOSTR mode, stored in op2. */
  236. #define IRTOSTR_INT                0        /* Convert integer to string. */
  237. #define IRTOSTR_NUM                1        /* Convert number to string. */
  238. #define IRTOSTR_CHAR                2        /* Convert char value to string. */

  239. /* -- IR operands --------------------------------------------------------- */

  240. /* IR operand mode (2 bit). */
  241. typedef enum {
  242.   IRMref,                /* IR reference. */
  243.   IRMlit,                /* 16 bit unsigned literal. */
  244.   IRMcst,                /* Constant literal: i, gcr or ptr. */
  245.   IRMnone                /* Unused operand. */
  246. } IRMode;
  247. #define IRM___                IRMnone

  248. /* Mode bits: Commutative, {Normal/Ref, Alloc, Load, Store}, Non-weak guard. */
  249. #define IRM_C                        0x10

  250. #define IRM_N                        0x00
  251. #define IRM_R                        IRM_N
  252. #define IRM_A                        0x20
  253. #define IRM_L                        0x40
  254. #define IRM_S                        0x60

  255. #define IRM_W                        0x80

  256. #define IRM_NW                        (IRM_N|IRM_W)
  257. #define IRM_CW                        (IRM_C|IRM_W)
  258. #define IRM_AW                        (IRM_A|IRM_W)
  259. #define IRM_LW                        (IRM_L|IRM_W)

  260. #define irm_op1(m)                ((IRMode)((m)&3))
  261. #define irm_op2(m)                ((IRMode)(((m)>>2)&3))
  262. #define irm_iscomm(m)                ((m) & IRM_C)
  263. #define irm_kind(m)                ((m) & IRM_S)

  264. #define IRMODE(name, m, m1, m2)        (((IRM##m1)|((IRM##m2)<<2)|(IRM_##m))^IRM_W),

  265. LJ_DATA const uint8_t lj_ir_mode[IR__MAX+1];

  266. /* -- IR instruction types ------------------------------------------------ */

  267. /* Map of itypes to non-negative numbers. ORDER LJ_T.
  268. ** LJ_TUPVAL/LJ_TTRACE never appear in a TValue. Use these itypes for
  269. ** IRT_P32 and IRT_P64, which never escape the IR.
  270. ** The various integers are only used in the IR and can only escape to
  271. ** a TValue after implicit or explicit conversion. Their types must be
  272. ** contiguous and next to IRT_NUM (see the typerange macros below).
  273. */
  274. #define IRTDEF(_) \
  275.   _(NIL, 4) _(FALSE, 4) _(TRUE, 4) _(LIGHTUD, LJ_64 ? 8 : 4) _(STR, 4) \
  276.   _(P32, 4) _(THREAD, 4) _(PROTO, 4) _(FUNC, 4) _(P64, 8) _(CDATA, 4) \
  277.   _(TAB, 4) _(UDATA, 4) \
  278.   _(FLOAT, 4) _(NUM, 8) _(I8, 1) _(U8, 1) _(I16, 2) _(U16, 2) \
  279.   _(INT, 4) _(U32, 4) _(I64, 8) _(U64, 8) \
  280.   _(SOFTFP, 4/* There is room for 9 more types. */

  281. /* IR result type and flags (8 bit). */
  282. typedef enum {
  283. #define IRTENUM(name, size)        IRT_##name,
  284. IRTDEF(IRTENUM)
  285. #undef IRTENUM
  286.   IRT__MAX,

  287.   /* Native pointer type and the corresponding integer type. */
  288.   IRT_PTR = LJ_64 ? IRT_P64 : IRT_P32,
  289.   IRT_INTP = LJ_64 ? IRT_I64 : IRT_INT,
  290.   IRT_UINTP = LJ_64 ? IRT_U64 : IRT_U32,
  291.   /* TODO_GC64: major changes required for all uses of IRT_P32. */

  292.   /* Additional flags. */
  293.   IRT_MARK = 0x20,        /* Marker for misc. purposes. */
  294.   IRT_ISPHI = 0x40,        /* Instruction is left or right PHI operand. */
  295.   IRT_GUARD = 0x80,        /* Instruction is a guard. */

  296.   /* Masks. */
  297.   IRT_TYPE = 0x1f,
  298.   IRT_T = 0xff
  299. } IRType;

  300. #define irtype_ispri(irt)        ((uint32_t)(irt) <= IRT_TRUE)

  301. /* Stored IRType. */
  302. typedef struct IRType1 { uint8_t irt; } IRType1;

  303. #define IRT(o, t)                ((uint32_t)(((o)<<8) | (t)))
  304. #define IRTI(o)                        (IRT((o), IRT_INT))
  305. #define IRTN(o)                        (IRT((o), IRT_NUM))
  306. #define IRTG(o, t)                (IRT((o), IRT_GUARD|(t)))
  307. #define IRTGI(o)                (IRT((o), IRT_GUARD|IRT_INT))

  308. #define irt_t(t)                ((IRType)(t).irt)
  309. #define irt_type(t)                ((IRType)((t).irt & IRT_TYPE))
  310. #define irt_sametype(t1, t2)        ((((t1).irt ^ (t2).irt) & IRT_TYPE) == 0)
  311. #define irt_typerange(t, first, last) \
  312.   ((uint32_t)((t).irt & IRT_TYPE) - (uint32_t)(first) <= (uint32_t)(last-first))

  313. #define irt_isnil(t)                (irt_type(t) == IRT_NIL)
  314. #define irt_ispri(t)                ((uint32_t)irt_type(t) <= IRT_TRUE)
  315. #define irt_islightud(t)        (irt_type(t) == IRT_LIGHTUD)
  316. #define irt_isstr(t)                (irt_type(t) == IRT_STR)
  317. #define irt_istab(t)                (irt_type(t) == IRT_TAB)
  318. #define irt_iscdata(t)                (irt_type(t) == IRT_CDATA)
  319. #define irt_isfloat(t)                (irt_type(t) == IRT_FLOAT)
  320. #define irt_isnum(t)                (irt_type(t) == IRT_NUM)
  321. #define irt_isint(t)                (irt_type(t) == IRT_INT)
  322. #define irt_isi8(t)                (irt_type(t) == IRT_I8)
  323. #define irt_isu8(t)                (irt_type(t) == IRT_U8)
  324. #define irt_isi16(t)                (irt_type(t) == IRT_I16)
  325. #define irt_isu16(t)                (irt_type(t) == IRT_U16)
  326. #define irt_isu32(t)                (irt_type(t) == IRT_U32)
  327. #define irt_isi64(t)                (irt_type(t) == IRT_I64)
  328. #define irt_isu64(t)                (irt_type(t) == IRT_U64)

  329. #define irt_isfp(t)                (irt_isnum(t) || irt_isfloat(t))
  330. #define irt_isinteger(t)        (irt_typerange((t), IRT_I8, IRT_INT))
  331. #define irt_isgcv(t)                (irt_typerange((t), IRT_STR, IRT_UDATA))
  332. #define irt_isaddr(t)                (irt_typerange((t), IRT_LIGHTUD, IRT_UDATA))
  333. #define irt_isint64(t)                (irt_typerange((t), IRT_I64, IRT_U64))

  334. #if LJ_GC64
  335. #define IRT_IS64 \
  336.   ((1u<<IRT_NUM)|(1u<<IRT_I64)|(1u<<IRT_U64)|(1u<<IRT_P64)|\
  337.    (1u<<IRT_LIGHTUD)|(1u<<IRT_STR)|(1u<<IRT_THREAD)|(1u<<IRT_PROTO)|\
  338.    (1u<<IRT_FUNC)|(1u<<IRT_CDATA)|(1u<<IRT_TAB)|(1u<<IRT_UDATA))
  339. #elif LJ_64
  340. #define IRT_IS64 \
  341.   ((1u<<IRT_NUM)|(1u<<IRT_I64)|(1u<<IRT_U64)|(1u<<IRT_P64)|(1u<<IRT_LIGHTUD))
  342. #else
  343. #define IRT_IS64 \
  344.   ((1u<<IRT_NUM)|(1u<<IRT_I64)|(1u<<IRT_U64))
  345. #endif

  346. #define irt_is64(t)                ((IRT_IS64 >> irt_type(t)) & 1)
  347. #define irt_is64orfp(t)                (((IRT_IS64|(1u<<IRT_FLOAT))>>irt_type(t)) & 1)

  348. #define irt_size(t)                (lj_ir_type_size[irt_t((t))])

  349. LJ_DATA const uint8_t lj_ir_type_size[];

  350. static LJ_AINLINE IRType itype2irt(const TValue *tv)
  351. {
  352.   if (tvisint(tv))
  353.     return IRT_INT;
  354.   else if (tvisnum(tv))
  355.     return IRT_NUM;
  356. #if LJ_64 && !LJ_GC64
  357.   else if (tvislightud(tv))
  358.     return IRT_LIGHTUD;
  359. #endif
  360.   else
  361.     return (IRType)~itype(tv);
  362. }

  363. static LJ_AINLINE uint32_t irt_toitype_(IRType t)
  364. {
  365.   lua_assert(!LJ_64 || t != IRT_LIGHTUD);
  366.   if (LJ_DUALNUM && t > IRT_NUM) {
  367.     return LJ_TISNUM;
  368.   } else {
  369.     lua_assert(t <= IRT_NUM);
  370.     return ~(uint32_t)t;
  371.   }
  372. }

  373. #define irt_toitype(t)                irt_toitype_(irt_type((t)))

  374. #define irt_isguard(t)                ((t).irt & IRT_GUARD)
  375. #define irt_ismarked(t)                ((t).irt & IRT_MARK)
  376. #define irt_setmark(t)                ((t).irt |= IRT_MARK)
  377. #define irt_clearmark(t)        ((t).irt &= ~IRT_MARK)
  378. #define irt_isphi(t)                ((t).irt & IRT_ISPHI)
  379. #define irt_setphi(t)                ((t).irt |= IRT_ISPHI)
  380. #define irt_clearphi(t)                ((t).irt &= ~IRT_ISPHI)

  381. /* Stored combined IR opcode and type. */
  382. typedef uint16_t IROpT;

  383. /* -- IR references ------------------------------------------------------- */

  384. /* IR references. */
  385. typedef uint16_t IRRef1;        /* One stored reference. */
  386. typedef uint32_t IRRef2;        /* Two stored references. */
  387. typedef uint32_t IRRef;                /* Used to pass around references. */

  388. /* Fixed references. */
  389. enum {
  390.   REF_BIAS =        0x8000,
  391.   REF_TRUE =        REF_BIAS-3,
  392.   REF_FALSE =        REF_BIAS-2,
  393.   REF_NIL =        REF_BIAS-1,        /* \--- Constants grow downwards. */
  394.   REF_BASE =        REF_BIAS,        /* /--- IR grows upwards. */
  395.   REF_FIRST =        REF_BIAS+1,
  396.   REF_DROP =        0xffff
  397. };

  398. /* Note: IRMlit operands must be < REF_BIAS, too!
  399. ** This allows for fast and uniform manipulation of all operands
  400. ** without looking up the operand mode in lj_ir_mode:
  401. ** - CSE calculates the maximum reference of two operands.
  402. **   This must work with mixed reference/literal operands, too.
  403. ** - DCE marking only checks for operand >= REF_BIAS.
  404. ** - LOOP needs to substitute reference operands.
  405. **   Constant references and literals must not be modified.
  406. */

  407. #define IRREF2(lo, hi)                ((IRRef2)(lo) | ((IRRef2)(hi) << 16))

  408. #define irref_isk(ref)                ((ref) < REF_BIAS)

  409. /* Tagged IR references (32 bit).
  410. **
  411. ** +-------+-------+---------------+
  412. ** |  irt  | flags |      ref      |
  413. ** +-------+-------+---------------+
  414. **
  415. ** The tag holds a copy of the IRType and speeds up IR type checks.
  416. */
  417. typedef uint32_t TRef;

  418. #define TREF_REFMASK                0x0000ffff
  419. #define TREF_FRAME                0x00010000
  420. #define TREF_CONT                0x00020000

  421. #define TREF(ref, t)                ((TRef)((ref) + ((t)<<24)))

  422. #define tref_ref(tr)                ((IRRef1)(tr))
  423. #define tref_t(tr)                ((IRType)((tr)>>24))
  424. #define tref_type(tr)                ((IRType)(((tr)>>24) & IRT_TYPE))
  425. #define tref_typerange(tr, first, last) \
  426.   ((((tr)>>24) & IRT_TYPE) - (TRef)(first) <= (TRef)(last-first))

  427. #define tref_istype(tr, t)        (((tr) & (IRT_TYPE<<24)) == ((t)<<24))
  428. #define tref_isnil(tr)                (tref_istype((tr), IRT_NIL))
  429. #define tref_isfalse(tr)        (tref_istype((tr), IRT_FALSE))
  430. #define tref_istrue(tr)                (tref_istype((tr), IRT_TRUE))
  431. #define tref_islightud(tr)        (tref_istype((tr), IRT_LIGHTUD))
  432. #define tref_isstr(tr)                (tref_istype((tr), IRT_STR))
  433. #define tref_isfunc(tr)                (tref_istype((tr), IRT_FUNC))
  434. #define tref_iscdata(tr)        (tref_istype((tr), IRT_CDATA))
  435. #define tref_istab(tr)                (tref_istype((tr), IRT_TAB))
  436. #define tref_isudata(tr)        (tref_istype((tr), IRT_UDATA))
  437. #define tref_isnum(tr)                (tref_istype((tr), IRT_NUM))
  438. #define tref_isint(tr)                (tref_istype((tr), IRT_INT))

  439. #define tref_isbool(tr)                (tref_typerange((tr), IRT_FALSE, IRT_TRUE))
  440. #define tref_ispri(tr)                (tref_typerange((tr), IRT_NIL, IRT_TRUE))
  441. #define tref_istruecond(tr)        (!tref_typerange((tr), IRT_NIL, IRT_FALSE))
  442. #define tref_isinteger(tr)        (tref_typerange((tr), IRT_I8, IRT_INT))
  443. #define tref_isnumber(tr)        (tref_typerange((tr), IRT_NUM, IRT_INT))
  444. #define tref_isnumber_str(tr)        (tref_isnumber((tr)) || tref_isstr((tr)))
  445. #define tref_isgcv(tr)                (tref_typerange((tr), IRT_STR, IRT_UDATA))

  446. #define tref_isk(tr)                (irref_isk(tref_ref((tr))))
  447. #define tref_isk2(tr1, tr2)        (irref_isk(tref_ref((tr1) | (tr2))))

  448. #define TREF_PRI(t)                (TREF(REF_NIL-(t), (t)))
  449. #define TREF_NIL                (TREF_PRI(IRT_NIL))
  450. #define TREF_FALSE                (TREF_PRI(IRT_FALSE))
  451. #define TREF_TRUE                (TREF_PRI(IRT_TRUE))

  452. /* -- IR format ----------------------------------------------------------- */

  453. /* IR instruction format (64 bit).
  454. **
  455. **    16      16     8   8   8   8
  456. ** +-------+-------+---+---+---+---+
  457. ** |  op1  |  op2  | t | o | r | s |
  458. ** +-------+-------+---+---+---+---+
  459. ** |  op12/i/gco   |   ot  | prev  | (alternative fields in union)
  460. ** +---------------+-------+-------+
  461. **        32           16      16
  462. **
  463. ** prev is only valid prior to register allocation and then reused for r + s.
  464. */

  465. typedef union IRIns {
  466.   struct {
  467.     LJ_ENDIAN_LOHI(
  468.       IRRef1 op1;        /* IR operand 1. */
  469.     , IRRef1 op2;        /* IR operand 2. */
  470.     )
  471.     IROpT ot;                /* IR opcode and type (overlaps t and o). */
  472.     IRRef1 prev;        /* Previous ins in same chain (overlaps r and s). */
  473.   };
  474.   struct {
  475.     IRRef2 op12;        /* IR operand 1 and 2 (overlaps op1 and op2). */
  476.     LJ_ENDIAN_LOHI(
  477.       IRType1 t;        /* IR type. */
  478.     , IROp1 o;                /* IR opcode. */
  479.     )
  480.     LJ_ENDIAN_LOHI(
  481.       uint8_t r;        /* Register allocation (overlaps prev). */
  482.     , uint8_t s;        /* Spill slot allocation (overlaps prev). */
  483.     )
  484.   };
  485.   int32_t i;                /* 32 bit signed integer literal (overlaps op12). */
  486.   GCRef gcr;                /* GCobj constant (overlaps op12). */
  487.   MRef ptr;                /* Pointer constant (overlaps op12). */
  488. } IRIns;

  489. /* TODO_GC64: major changes required. */
  490. #define ir_kgc(ir)        check_exp((ir)->o == IR_KGC, gcref((ir)->gcr))
  491. #define ir_kstr(ir)        (gco2str(ir_kgc((ir))))
  492. #define ir_ktab(ir)        (gco2tab(ir_kgc((ir))))
  493. #define ir_kfunc(ir)        (gco2func(ir_kgc((ir))))
  494. #define ir_kcdata(ir)        (gco2cd(ir_kgc((ir))))
  495. #define ir_knum(ir)        check_exp((ir)->o == IR_KNUM, mref((ir)->ptr, cTValue))
  496. #define ir_kint64(ir)        check_exp((ir)->o == IR_KINT64, mref((ir)->ptr,cTValue))
  497. #define ir_k64(ir) \
  498.   check_exp((ir)->o == IR_KNUM || (ir)->o == IR_KINT64, mref((ir)->ptr,cTValue))
  499. #define ir_kptr(ir) \
  500.   check_exp((ir)->o == IR_KPTR || (ir)->o == IR_KKPTR, mref((ir)->ptr, void))

  501. /* A store or any other op with a non-weak guard has a side-effect. */
  502. static LJ_AINLINE int ir_sideeff(IRIns *ir)
  503. {
  504.   return (((ir->t.irt | ~IRT_GUARD) & lj_ir_mode[ir->o]) >= IRM_S);
  505. }

  506. LJ_STATIC_ASSERT((int)IRT_GUARD == (int)IRM_W);

  507. #endif