ljx

FORK: LuaJIT with native 5.2 and 5.3 support
git clone https://git.neptards.moe/neptards/ljx.git
Log | Files | Refs | README

lj_ir.h (19242B)


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