2009-12-08 18:46:35 +00:00
|
|
|
/*
|
|
|
|
** Common definitions for the JIT compiler.
|
2021-01-02 20:49:41 +00:00
|
|
|
** Copyright (C) 2005-2021 Mike Pall. See Copyright Notice in luajit.h
|
2009-12-08 18:46:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LJ_JIT_H
|
|
|
|
#define _LJ_JIT_H
|
|
|
|
|
|
|
|
#include "lj_obj.h"
|
|
|
|
#include "lj_ir.h"
|
|
|
|
|
2020-05-20 18:42:04 +00:00
|
|
|
/* -- JIT engine flags ---------------------------------------------------- */
|
|
|
|
|
|
|
|
/* General JIT engine flags. 4 bits. */
|
2009-12-08 18:46:35 +00:00
|
|
|
#define JIT_F_ON 0x00000001
|
|
|
|
|
2020-05-20 18:42:04 +00:00
|
|
|
/* CPU-specific JIT engine flags. 12 bits. Flags and strings must match. */
|
|
|
|
#define JIT_F_CPU 0x00000010
|
|
|
|
|
2009-12-08 18:46:35 +00:00
|
|
|
#if LJ_TARGET_X86ORX64
|
2020-05-20 18:42:04 +00:00
|
|
|
|
|
|
|
#define JIT_F_SSE3 (JIT_F_CPU << 0)
|
|
|
|
#define JIT_F_SSE4_1 (JIT_F_CPU << 1)
|
|
|
|
#define JIT_F_BMI2 (JIT_F_CPU << 2)
|
|
|
|
|
|
|
|
|
|
|
|
#define JIT_F_CPUSTRING "\4SSE3\6SSE4.1\4BMI2"
|
|
|
|
|
2011-05-26 15:58:29 +00:00
|
|
|
#elif LJ_TARGET_ARM
|
2020-05-20 18:42:04 +00:00
|
|
|
|
|
|
|
#define JIT_F_ARMV6_ (JIT_F_CPU << 0)
|
|
|
|
#define JIT_F_ARMV6T2_ (JIT_F_CPU << 1)
|
|
|
|
#define JIT_F_ARMV7 (JIT_F_CPU << 2)
|
|
|
|
#define JIT_F_ARMV8 (JIT_F_CPU << 3)
|
|
|
|
#define JIT_F_VFPV2 (JIT_F_CPU << 4)
|
|
|
|
#define JIT_F_VFPV3 (JIT_F_CPU << 5)
|
|
|
|
|
|
|
|
#define JIT_F_ARMV6 (JIT_F_ARMV6_|JIT_F_ARMV6T2_|JIT_F_ARMV7|JIT_F_ARMV8)
|
|
|
|
#define JIT_F_ARMV6T2 (JIT_F_ARMV6T2_|JIT_F_ARMV7|JIT_F_ARMV8)
|
2012-07-08 20:20:11 +00:00
|
|
|
#define JIT_F_VFP (JIT_F_VFPV2|JIT_F_VFPV3)
|
2011-05-26 15:58:29 +00:00
|
|
|
|
2020-05-20 18:42:04 +00:00
|
|
|
#define JIT_F_CPUSTRING "\5ARMv6\7ARMv6T2\5ARMv7\5ARMv8\5VFPv2\5VFPv3"
|
|
|
|
|
2012-06-09 18:54:34 +00:00
|
|
|
#elif LJ_TARGET_PPC
|
|
|
|
|
2020-05-20 18:42:04 +00:00
|
|
|
#define JIT_F_SQRT (JIT_F_CPU << 0)
|
|
|
|
#define JIT_F_ROUND (JIT_F_CPU << 1)
|
|
|
|
|
2012-06-09 23:38:44 +00:00
|
|
|
#define JIT_F_CPUSTRING "\4SQRT\5ROUND"
|
2020-05-20 18:42:04 +00:00
|
|
|
|
2012-03-29 23:34:17 +00:00
|
|
|
#elif LJ_TARGET_MIPS
|
|
|
|
|
2020-05-20 18:42:04 +00:00
|
|
|
#define JIT_F_MIPSXXR2 (JIT_F_CPU << 0)
|
|
|
|
|
2016-05-28 03:10:55 +00:00
|
|
|
#if LJ_TARGET_MIPS32
|
2020-01-20 21:15:45 +00:00
|
|
|
#if LJ_TARGET_MIPSR6
|
|
|
|
#define JIT_F_CPUSTRING "\010MIPS32R6"
|
|
|
|
#else
|
2012-03-29 23:34:17 +00:00
|
|
|
#define JIT_F_CPUSTRING "\010MIPS32R2"
|
2020-01-20 21:15:45 +00:00
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#if LJ_TARGET_MIPSR6
|
|
|
|
#define JIT_F_CPUSTRING "\010MIPS64R6"
|
2009-12-08 18:46:35 +00:00
|
|
|
#else
|
2016-05-28 03:10:55 +00:00
|
|
|
#define JIT_F_CPUSTRING "\010MIPS64R2"
|
|
|
|
#endif
|
2020-01-20 21:15:45 +00:00
|
|
|
#endif
|
2020-05-20 18:42:04 +00:00
|
|
|
|
2016-05-28 03:10:55 +00:00
|
|
|
#else
|
2020-05-20 18:42:04 +00:00
|
|
|
|
2011-05-09 16:16:39 +00:00
|
|
|
#define JIT_F_CPUSTRING ""
|
2020-05-20 18:42:04 +00:00
|
|
|
|
2009-12-08 18:46:35 +00:00
|
|
|
#endif
|
|
|
|
|
2020-05-20 18:42:04 +00:00
|
|
|
/* Optimization flags. 12 bits. */
|
|
|
|
#define JIT_F_OPT 0x00010000
|
2010-03-15 16:02:53 +00:00
|
|
|
#define JIT_F_OPT_MASK 0x0fff0000
|
2009-12-08 18:46:35 +00:00
|
|
|
|
2020-05-20 18:42:04 +00:00
|
|
|
#define JIT_F_OPT_FOLD (JIT_F_OPT << 0)
|
|
|
|
#define JIT_F_OPT_CSE (JIT_F_OPT << 1)
|
|
|
|
#define JIT_F_OPT_DCE (JIT_F_OPT << 2)
|
|
|
|
#define JIT_F_OPT_FWD (JIT_F_OPT << 3)
|
|
|
|
#define JIT_F_OPT_DSE (JIT_F_OPT << 4)
|
|
|
|
#define JIT_F_OPT_NARROW (JIT_F_OPT << 5)
|
|
|
|
#define JIT_F_OPT_LOOP (JIT_F_OPT << 6)
|
|
|
|
#define JIT_F_OPT_ABC (JIT_F_OPT << 7)
|
|
|
|
#define JIT_F_OPT_SINK (JIT_F_OPT << 8)
|
|
|
|
#define JIT_F_OPT_FUSE (JIT_F_OPT << 9)
|
2009-12-08 18:46:35 +00:00
|
|
|
|
|
|
|
/* Optimizations names for -O. Must match the order above. */
|
|
|
|
#define JIT_F_OPTSTRING \
|
2012-07-02 21:47:12 +00:00
|
|
|
"\4fold\3cse\3dce\3fwd\3dse\6narrow\4loop\3abc\4sink\4fuse"
|
2009-12-08 18:46:35 +00:00
|
|
|
|
|
|
|
/* Optimization levels set a fixed combination of flags. */
|
|
|
|
#define JIT_F_OPT_0 0
|
|
|
|
#define JIT_F_OPT_1 (JIT_F_OPT_FOLD|JIT_F_OPT_CSE|JIT_F_OPT_DCE)
|
|
|
|
#define JIT_F_OPT_2 (JIT_F_OPT_1|JIT_F_OPT_NARROW|JIT_F_OPT_LOOP)
|
2012-07-02 21:47:12 +00:00
|
|
|
#define JIT_F_OPT_3 (JIT_F_OPT_2|\
|
|
|
|
JIT_F_OPT_FWD|JIT_F_OPT_DSE|JIT_F_OPT_ABC|JIT_F_OPT_SINK|JIT_F_OPT_FUSE)
|
2009-12-08 18:46:35 +00:00
|
|
|
#define JIT_F_OPT_DEFAULT JIT_F_OPT_3
|
|
|
|
|
2020-05-20 18:42:04 +00:00
|
|
|
/* -- JIT engine parameters ----------------------------------------------- */
|
|
|
|
|
2010-11-16 13:06:59 +00:00
|
|
|
#if LJ_TARGET_WINDOWS || LJ_64
|
2009-12-08 18:46:35 +00:00
|
|
|
/* See: http://blogs.msdn.com/oldnewthing/archive/2003/10/08/55239.aspx */
|
|
|
|
#define JIT_P_sizemcode_DEFAULT 64
|
|
|
|
#else
|
|
|
|
/* Could go as low as 4K, but the mmap() overhead would be rather high. */
|
|
|
|
#define JIT_P_sizemcode_DEFAULT 32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Optimization parameters and their defaults. Length is a char in octal! */
|
|
|
|
#define JIT_PARAMDEF(_) \
|
|
|
|
_(\010, maxtrace, 1000) /* Max. # of traces in cache. */ \
|
2011-05-03 19:14:18 +00:00
|
|
|
_(\011, maxrecord, 4000) /* Max. # of recorded IR instructions. */ \
|
2009-12-08 18:46:35 +00:00
|
|
|
_(\012, maxirconst, 500) /* Max. # of IR constants of a trace. */ \
|
|
|
|
_(\007, maxside, 100) /* Max. # of side traces of a root trace. */ \
|
2011-05-03 19:14:18 +00:00
|
|
|
_(\007, maxsnap, 500) /* Max. # of snapshots for a trace. */ \
|
2013-12-25 01:55:25 +00:00
|
|
|
_(\011, minstitch, 0) /* Min. # of IR ins for a stitched trace. */ \
|
2009-12-08 18:46:35 +00:00
|
|
|
\
|
2010-02-23 17:27:39 +00:00
|
|
|
_(\007, hotloop, 56) /* # of iter. to detect a hot loop/call. */ \
|
2009-12-08 18:46:35 +00:00
|
|
|
_(\007, hotexit, 10) /* # of taken exits to start a side trace. */ \
|
|
|
|
_(\007, tryside, 4) /* # of attempts to compile a side trace. */ \
|
|
|
|
\
|
|
|
|
_(\012, instunroll, 4) /* Max. unroll for instable loops. */ \
|
2011-05-03 19:14:18 +00:00
|
|
|
_(\012, loopunroll, 15) /* Max. unroll for loop ops in side traces. */ \
|
2009-12-08 18:46:35 +00:00
|
|
|
_(\012, callunroll, 3) /* Max. unroll for recursive calls. */ \
|
2010-02-23 17:27:39 +00:00
|
|
|
_(\011, recunroll, 2) /* Min. unroll for true recursion. */ \
|
2009-12-08 18:46:35 +00:00
|
|
|
\
|
|
|
|
/* Size of each machine code area (in KBytes). */ \
|
|
|
|
_(\011, sizemcode, JIT_P_sizemcode_DEFAULT) \
|
|
|
|
/* Max. total size of all machine code areas (in KBytes). */ \
|
|
|
|
_(\010, maxmcode, 512) \
|
|
|
|
/* End of list. */
|
|
|
|
|
|
|
|
enum {
|
|
|
|
#define JIT_PARAMENUM(len, name, value) JIT_P_##name,
|
|
|
|
JIT_PARAMDEF(JIT_PARAMENUM)
|
|
|
|
#undef JIT_PARAMENUM
|
|
|
|
JIT_P__MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
#define JIT_PARAMSTR(len, name, value) #len #name
|
|
|
|
#define JIT_P_STRING JIT_PARAMDEF(JIT_PARAMSTR)
|
|
|
|
|
2020-05-20 18:42:04 +00:00
|
|
|
/* -- JIT engine data structures ------------------------------------------ */
|
|
|
|
|
2009-12-08 18:46:35 +00:00
|
|
|
/* Trace compiler state. */
|
|
|
|
typedef enum {
|
|
|
|
LJ_TRACE_IDLE, /* Trace compiler idle. */
|
|
|
|
LJ_TRACE_ACTIVE = 0x10,
|
|
|
|
LJ_TRACE_RECORD, /* Bytecode recording active. */
|
|
|
|
LJ_TRACE_START, /* New trace started. */
|
|
|
|
LJ_TRACE_END, /* End of trace. */
|
|
|
|
LJ_TRACE_ASM, /* Assemble trace. */
|
2011-01-17 00:20:10 +00:00
|
|
|
LJ_TRACE_ERR /* Trace aborted with error. */
|
2009-12-08 18:46:35 +00:00
|
|
|
} TraceState;
|
|
|
|
|
2011-01-17 00:20:10 +00:00
|
|
|
/* Post-processing action. */
|
|
|
|
typedef enum {
|
|
|
|
LJ_POST_NONE, /* No action. */
|
2011-02-05 17:54:08 +00:00
|
|
|
LJ_POST_FIXCOMP, /* Fixup comparison and emit pending guard. */
|
2011-02-07 23:09:33 +00:00
|
|
|
LJ_POST_FIXGUARD, /* Fixup and emit pending guard. */
|
2011-11-25 18:36:35 +00:00
|
|
|
LJ_POST_FIXGUARDSNAP, /* Fixup and emit pending guard and snapshot. */
|
2011-03-07 19:03:38 +00:00
|
|
|
LJ_POST_FIXBOOL, /* Fixup boolean result. */
|
2012-10-10 16:16:18 +00:00
|
|
|
LJ_POST_FIXCONST, /* Fixup constant results. */
|
2011-03-07 19:03:38 +00:00
|
|
|
LJ_POST_FFRETRY /* Suppress recording of retried fast functions. */
|
2011-01-17 00:20:10 +00:00
|
|
|
} PostProc;
|
|
|
|
|
2009-12-08 18:46:35 +00:00
|
|
|
/* Machine code type. */
|
2011-05-09 16:16:39 +00:00
|
|
|
#if LJ_TARGET_X86ORX64
|
2009-12-08 18:46:35 +00:00
|
|
|
typedef uint8_t MCode;
|
2011-05-09 16:16:39 +00:00
|
|
|
#else
|
|
|
|
typedef uint32_t MCode;
|
|
|
|
#endif
|
2009-12-08 18:46:35 +00:00
|
|
|
|
2017-06-07 17:36:46 +00:00
|
|
|
/* Linked list of MCode areas. */
|
|
|
|
typedef struct MCLink {
|
|
|
|
MCode *next; /* Next area. */
|
|
|
|
size_t size; /* Size of current area. */
|
|
|
|
} MCLink;
|
|
|
|
|
2009-12-08 18:46:35 +00:00
|
|
|
/* Stack snapshot header. */
|
|
|
|
typedef struct SnapShot {
|
2019-01-10 11:19:30 +00:00
|
|
|
uint32_t mapofs; /* Offset into snapshot map. */
|
2009-12-08 18:46:35 +00:00
|
|
|
IRRef1 ref; /* First IR ref for this snapshot. */
|
2021-03-22 23:35:46 +00:00
|
|
|
uint16_t mcofs; /* Offset into machine code in MCode units. */
|
2010-01-26 20:49:04 +00:00
|
|
|
uint8_t nslots; /* Number of valid slots. */
|
2011-11-20 16:56:47 +00:00
|
|
|
uint8_t topslot; /* Maximum frame extent. */
|
2010-01-26 20:49:04 +00:00
|
|
|
uint8_t nent; /* Number of compressed entries. */
|
2009-12-08 18:46:35 +00:00
|
|
|
uint8_t count; /* Count of taken exits for this snapshot. */
|
|
|
|
} SnapShot;
|
|
|
|
|
|
|
|
#define SNAPCOUNT_DONE 255 /* Already compiled and linked a side trace. */
|
2010-01-25 18:51:52 +00:00
|
|
|
|
2010-01-26 20:49:04 +00:00
|
|
|
/* Compressed snapshot entry. */
|
2010-01-25 18:51:52 +00:00
|
|
|
typedef uint32_t SnapEntry;
|
2010-01-26 20:49:04 +00:00
|
|
|
|
2010-01-27 01:17:56 +00:00
|
|
|
#define SNAP_FRAME 0x010000 /* Frame slot. */
|
|
|
|
#define SNAP_CONT 0x020000 /* Continuation slot. */
|
2010-02-23 02:08:49 +00:00
|
|
|
#define SNAP_NORESTORE 0x040000 /* No need to restore slot. */
|
2011-05-16 00:38:07 +00:00
|
|
|
#define SNAP_SOFTFPNUM 0x080000 /* Soft-float number. */
|
2010-01-27 01:17:56 +00:00
|
|
|
LJ_STATIC_ASSERT(SNAP_FRAME == TREF_FRAME);
|
|
|
|
LJ_STATIC_ASSERT(SNAP_CONT == TREF_CONT);
|
|
|
|
|
|
|
|
#define SNAP(slot, flags, ref) (((SnapEntry)(slot) << 24) + (flags) + (ref))
|
|
|
|
#define SNAP_TR(slot, tr) \
|
|
|
|
(((SnapEntry)(slot) << 24) + ((tr) & (TREF_CONT|TREF_FRAME|TREF_REFMASK)))
|
2016-05-22 23:49:00 +00:00
|
|
|
#if !LJ_FR2
|
2010-01-26 20:49:04 +00:00
|
|
|
#define SNAP_MKPC(pc) ((SnapEntry)u32ptr(pc))
|
2016-05-22 23:49:00 +00:00
|
|
|
#endif
|
2010-01-26 20:49:04 +00:00
|
|
|
#define SNAP_MKFTSZ(ftsz) ((SnapEntry)(ftsz))
|
2010-01-25 18:51:52 +00:00
|
|
|
#define snap_ref(sn) ((sn) & 0xffff)
|
2010-01-26 20:49:04 +00:00
|
|
|
#define snap_slot(sn) ((BCReg)((sn) >> 24))
|
|
|
|
#define snap_isframe(sn) ((sn) & SNAP_FRAME)
|
2010-04-07 23:28:51 +00:00
|
|
|
#define snap_setref(sn, ref) (((sn) & (0xffff0000&~SNAP_NORESTORE)) | (ref))
|
2009-12-08 18:46:35 +00:00
|
|
|
|
2016-05-22 23:49:00 +00:00
|
|
|
static LJ_AINLINE const BCIns *snap_pc(SnapEntry *sn)
|
|
|
|
{
|
|
|
|
#if LJ_FR2
|
|
|
|
uint64_t pcbase;
|
|
|
|
memcpy(&pcbase, sn, sizeof(uint64_t));
|
|
|
|
return (const BCIns *)(pcbase >> 8);
|
|
|
|
#else
|
|
|
|
return (const BCIns *)(uintptr_t)*sn;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-12-08 18:46:35 +00:00
|
|
|
/* Snapshot and exit numbers. */
|
|
|
|
typedef uint32_t SnapNo;
|
|
|
|
typedef uint32_t ExitNo;
|
|
|
|
|
|
|
|
/* Trace number. */
|
|
|
|
typedef uint32_t TraceNo; /* Used to pass around trace numbers. */
|
|
|
|
typedef uint16_t TraceNo1; /* Stored trace number. */
|
|
|
|
|
2011-06-28 21:23:34 +00:00
|
|
|
/* Type of link. ORDER LJ_TRLINK */
|
|
|
|
typedef enum {
|
|
|
|
LJ_TRLINK_NONE, /* Incomplete trace. No link, yet. */
|
|
|
|
LJ_TRLINK_ROOT, /* Link to other root trace. */
|
|
|
|
LJ_TRLINK_LOOP, /* Loop to same trace. */
|
|
|
|
LJ_TRLINK_TAILREC, /* Tail-recursion. */
|
|
|
|
LJ_TRLINK_UPREC, /* Up-recursion. */
|
|
|
|
LJ_TRLINK_DOWNREC, /* Down-recursion. */
|
|
|
|
LJ_TRLINK_INTERP, /* Fallback to interpreter. */
|
2013-12-25 01:55:25 +00:00
|
|
|
LJ_TRLINK_RETURN, /* Return to interpreter. */
|
|
|
|
LJ_TRLINK_STITCH /* Trace stitching. */
|
2011-06-28 21:23:34 +00:00
|
|
|
} TraceLink;
|
2009-12-08 18:46:35 +00:00
|
|
|
|
2010-04-25 01:32:29 +00:00
|
|
|
/* Trace object. */
|
|
|
|
typedef struct GCtrace {
|
|
|
|
GCHeader;
|
2019-01-10 11:19:30 +00:00
|
|
|
uint16_t nsnap; /* Number of snapshots. */
|
2009-12-08 18:46:35 +00:00
|
|
|
IRRef nins; /* Next IR instruction. Biased with REF_BIAS. */
|
2015-01-03 14:23:58 +00:00
|
|
|
#if LJ_GC64
|
|
|
|
uint32_t unused_gc64;
|
|
|
|
#endif
|
2010-04-25 01:32:29 +00:00
|
|
|
GCRef gclist;
|
|
|
|
IRIns *ir; /* IR instructions/constants. Biased with REF_BIAS. */
|
2009-12-08 18:46:35 +00:00
|
|
|
IRRef nk; /* Lowest IR constant. Biased with REF_BIAS. */
|
2019-01-10 11:19:30 +00:00
|
|
|
uint32_t nsnapmap; /* Number of snapshot map elements. */
|
2010-04-25 01:32:29 +00:00
|
|
|
SnapShot *snap; /* Snapshot array. */
|
|
|
|
SnapEntry *snapmap; /* Snapshot map. */
|
2009-12-08 18:46:35 +00:00
|
|
|
GCRef startpt; /* Starting prototype. */
|
2011-01-18 20:08:23 +00:00
|
|
|
MRef startpc; /* Bytecode PC of starting instruction. */
|
2009-12-08 18:46:35 +00:00
|
|
|
BCIns startins; /* Original bytecode of starting instruction. */
|
|
|
|
MSize szmcode; /* Size of machine code. */
|
2011-01-18 20:08:23 +00:00
|
|
|
MCode *mcode; /* Start of machine code. */
|
2009-12-08 18:46:35 +00:00
|
|
|
MSize mcloop; /* Offset of loop start in machine code. */
|
2010-04-25 01:32:29 +00:00
|
|
|
uint16_t nchild; /* Number of child traces (root trace only). */
|
|
|
|
uint16_t spadjust; /* Stack pointer adjustment (offset in bytes). */
|
|
|
|
TraceNo1 traceno; /* Trace number. */
|
2009-12-08 18:46:35 +00:00
|
|
|
TraceNo1 link; /* Linked trace (or self for loops). */
|
|
|
|
TraceNo1 root; /* Root trace of side trace (or 0 for root traces). */
|
|
|
|
TraceNo1 nextroot; /* Next root trace for same prototype. */
|
|
|
|
TraceNo1 nextside; /* Next side trace of same root trace. */
|
2012-07-11 14:45:15 +00:00
|
|
|
uint8_t sinktags; /* Trace has SINK tags. */
|
2019-01-10 11:19:30 +00:00
|
|
|
uint8_t topslot; /* Top stack slot already checked to be allocated. */
|
|
|
|
uint8_t linktype; /* Type of link. */
|
2012-07-11 14:45:15 +00:00
|
|
|
uint8_t unused1;
|
2009-12-08 18:46:35 +00:00
|
|
|
#ifdef LUAJIT_USE_GDBJIT
|
|
|
|
void *gdbjit_entry; /* GDB JIT entry. */
|
|
|
|
#endif
|
2010-04-25 01:32:29 +00:00
|
|
|
} GCtrace;
|
|
|
|
|
|
|
|
#define gco2trace(o) check_exp((o)->gch.gct == ~LJ_TTRACE, (GCtrace *)(o))
|
|
|
|
#define traceref(J, n) \
|
|
|
|
check_exp((n)>0 && (MSize)(n)<J->sizetrace, (GCtrace *)gcref(J->trace[(n)]))
|
|
|
|
|
|
|
|
LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCtrace, gclist));
|
2009-12-08 18:46:35 +00:00
|
|
|
|
2011-11-20 12:23:25 +00:00
|
|
|
static LJ_AINLINE MSize snap_nextofs(GCtrace *T, SnapShot *snap)
|
|
|
|
{
|
|
|
|
if (snap+1 == &T->snap[T->nsnap])
|
|
|
|
return T->nsnapmap;
|
|
|
|
else
|
|
|
|
return (snap+1)->mapofs;
|
|
|
|
}
|
|
|
|
|
2009-12-08 18:46:35 +00:00
|
|
|
/* Round-robin penalty cache for bytecodes leading to aborted traces. */
|
|
|
|
typedef struct HotPenalty {
|
2010-02-23 17:27:39 +00:00
|
|
|
MRef pc; /* Starting bytecode PC. */
|
2009-12-08 18:46:35 +00:00
|
|
|
uint16_t val; /* Penalty value, i.e. hotcount start. */
|
|
|
|
uint16_t reason; /* Abort reason (really TraceErr). */
|
|
|
|
} HotPenalty;
|
|
|
|
|
2010-02-23 17:27:39 +00:00
|
|
|
#define PENALTY_SLOTS 64 /* Penalty cache slot. Must be a power of 2. */
|
2011-06-28 23:51:39 +00:00
|
|
|
#define PENALTY_MIN (36*2) /* Minimum penalty value. */
|
2010-02-23 17:27:39 +00:00
|
|
|
#define PENALTY_MAX 60000 /* Maximum penalty value. */
|
|
|
|
#define PENALTY_RNDBITS 4 /* # of random bits to add to penalty value. */
|
2009-12-08 18:46:35 +00:00
|
|
|
|
|
|
|
/* Round-robin backpropagation cache for narrowing conversions. */
|
|
|
|
typedef struct BPropEntry {
|
|
|
|
IRRef1 key; /* Key: original reference. */
|
|
|
|
IRRef1 val; /* Value: reference after conversion. */
|
2010-12-31 02:56:30 +00:00
|
|
|
IRRef mode; /* Mode for this entry (currently IRCONV_*). */
|
2009-12-08 18:46:35 +00:00
|
|
|
} BPropEntry;
|
|
|
|
|
|
|
|
/* Number of slots for the backpropagation cache. Must be a power of 2. */
|
|
|
|
#define BPROP_SLOTS 16
|
|
|
|
|
2010-03-15 15:23:02 +00:00
|
|
|
/* Scalar evolution analysis cache. */
|
|
|
|
typedef struct ScEvEntry {
|
2014-03-27 22:29:30 +00:00
|
|
|
MRef pc; /* Bytecode PC of FORI. */
|
2010-03-15 15:23:02 +00:00
|
|
|
IRRef1 idx; /* Index reference. */
|
|
|
|
IRRef1 start; /* Constant start reference. */
|
|
|
|
IRRef1 stop; /* Constant stop reference. */
|
|
|
|
IRRef1 step; /* Constant step reference. */
|
|
|
|
IRType1 t; /* Scalar type. */
|
2010-10-11 19:13:37 +00:00
|
|
|
uint8_t dir; /* Direction. 1: +, 0: -. */
|
2010-03-15 15:23:02 +00:00
|
|
|
} ScEvEntry;
|
|
|
|
|
2015-05-18 23:59:29 +00:00
|
|
|
/* Reverse bytecode map (IRRef -> PC). Only for selected instructions. */
|
|
|
|
typedef struct RBCHashEntry {
|
|
|
|
MRef pc; /* Bytecode PC. */
|
2015-05-21 14:38:31 +00:00
|
|
|
GCRef pt; /* Prototype. */
|
2015-05-18 23:59:29 +00:00
|
|
|
IRRef ref; /* IR reference. */
|
|
|
|
} RBCHashEntry;
|
|
|
|
|
|
|
|
/* Number of slots in the reverse bytecode hash table. Must be a power of 2. */
|
|
|
|
#define RBCHASH_SLOTS 8
|
|
|
|
|
2010-02-24 22:17:17 +00:00
|
|
|
/* 128 bit SIMD constants. */
|
|
|
|
enum {
|
|
|
|
LJ_KSIMD_ABS,
|
|
|
|
LJ_KSIMD_NEG,
|
|
|
|
LJ_KSIMD__MAX
|
|
|
|
};
|
|
|
|
|
2016-05-20 22:02:45 +00:00
|
|
|
enum {
|
|
|
|
#if LJ_TARGET_X86ORX64
|
|
|
|
LJ_K64_TOBIT, /* 2^52 + 2^51 */
|
|
|
|
LJ_K64_2P64, /* 2^64 */
|
|
|
|
LJ_K64_M2P64, /* -2^64 */
|
|
|
|
#if LJ_32
|
|
|
|
LJ_K64_M2P64_31, /* -2^64 or -2^31 */
|
|
|
|
#else
|
|
|
|
LJ_K64_M2P64_31 = LJ_K64_M2P64,
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#if LJ_TARGET_MIPS
|
|
|
|
LJ_K64_2P31, /* 2^31 */
|
2017-02-20 02:43:10 +00:00
|
|
|
#if LJ_64
|
|
|
|
LJ_K64_2P63, /* 2^63 */
|
|
|
|
LJ_K64_M2P64, /* -2^64 */
|
|
|
|
#endif
|
2016-05-20 22:02:45 +00:00
|
|
|
#endif
|
|
|
|
LJ_K64__MAX,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
#if LJ_TARGET_X86ORX64
|
|
|
|
LJ_K32_M2P64_31, /* -2^64 or -2^31 */
|
|
|
|
#endif
|
|
|
|
#if LJ_TARGET_PPC
|
|
|
|
LJ_K32_2P52_2P31, /* 2^52 + 2^31 */
|
|
|
|
LJ_K32_2P52, /* 2^52 */
|
|
|
|
#endif
|
|
|
|
#if LJ_TARGET_PPC || LJ_TARGET_MIPS
|
|
|
|
LJ_K32_2P31, /* 2^31 */
|
2017-02-20 02:43:10 +00:00
|
|
|
#endif
|
|
|
|
#if LJ_TARGET_MIPS64
|
|
|
|
LJ_K32_2P63, /* 2^63 */
|
|
|
|
LJ_K32_M2P64, /* -2^64 */
|
2016-05-20 22:02:45 +00:00
|
|
|
#endif
|
|
|
|
LJ_K32__MAX
|
|
|
|
};
|
|
|
|
|
2010-02-24 22:17:17 +00:00
|
|
|
/* Get 16 byte aligned pointer to SIMD constant. */
|
|
|
|
#define LJ_KSIMD(J, n) \
|
|
|
|
((TValue *)(((intptr_t)&J->ksimd[2*(n)] + 15) & ~(intptr_t)15))
|
|
|
|
|
2011-02-02 01:29:37 +00:00
|
|
|
/* Set/reset flag to activate the SPLIT pass for the current trace. */
|
2017-06-07 21:56:54 +00:00
|
|
|
#if LJ_SOFTFP32 || (LJ_32 && LJ_HASFFI)
|
2011-02-02 01:29:37 +00:00
|
|
|
#define lj_needsplit(J) (J->needsplit = 1)
|
|
|
|
#define lj_resetsplit(J) (J->needsplit = 0)
|
|
|
|
#else
|
|
|
|
#define lj_needsplit(J) UNUSED(J)
|
|
|
|
#define lj_resetsplit(J) UNUSED(J)
|
|
|
|
#endif
|
|
|
|
|
2009-12-08 18:46:35 +00:00
|
|
|
/* Fold state is used to fold instructions on-the-fly. */
|
|
|
|
typedef struct FoldState {
|
|
|
|
IRIns ins; /* Currently emitted instruction. */
|
2016-05-22 22:25:29 +00:00
|
|
|
IRIns left[2]; /* Instruction referenced by left operand. */
|
|
|
|
IRIns right[2]; /* Instruction referenced by right operand. */
|
2009-12-08 18:46:35 +00:00
|
|
|
} FoldState;
|
|
|
|
|
|
|
|
/* JIT compiler state. */
|
|
|
|
typedef struct jit_State {
|
2010-04-25 01:32:29 +00:00
|
|
|
GCtrace cur; /* Current trace. */
|
2016-05-22 21:25:28 +00:00
|
|
|
GCtrace *curfinal; /* Final address of current trace (set during asm). */
|
2009-12-08 18:46:35 +00:00
|
|
|
|
|
|
|
lua_State *L; /* Current Lua state. */
|
|
|
|
const BCIns *pc; /* Current PC. */
|
|
|
|
GCfunc *fn; /* Current function. */
|
|
|
|
GCproto *pt; /* Current prototype. */
|
2010-02-24 22:17:17 +00:00
|
|
|
TRef *base; /* Current frame base, points into J->slots. */
|
2009-12-08 18:46:35 +00:00
|
|
|
|
2010-02-24 22:17:17 +00:00
|
|
|
uint32_t flags; /* JIT engine flags. */
|
|
|
|
BCReg maxslot; /* Relative to baseslot. */
|
|
|
|
BCReg baseslot; /* Current frame base, offset into J->slots. */
|
2009-12-08 18:46:35 +00:00
|
|
|
|
|
|
|
uint8_t mergesnap; /* Allowed to merge with next snapshot. */
|
|
|
|
uint8_t needsnap; /* Need snapshot before recording next bytecode. */
|
|
|
|
IRType1 guardemit; /* Accumulated IRT_GUARD for emitted instructions. */
|
2010-09-12 23:23:19 +00:00
|
|
|
uint8_t bcskip; /* Number of bytecode instructions to skip. */
|
2009-12-08 18:46:35 +00:00
|
|
|
|
2010-02-24 22:17:17 +00:00
|
|
|
FoldState fold; /* Fold state. */
|
|
|
|
|
2009-12-08 18:46:35 +00:00
|
|
|
const BCIns *bc_min; /* Start of allowed bytecode range for root trace. */
|
|
|
|
MSize bc_extent; /* Extent of the range. */
|
|
|
|
|
|
|
|
TraceState state; /* Trace compiler state. */
|
|
|
|
|
|
|
|
int32_t instunroll; /* Unroll counter for instable loops. */
|
|
|
|
int32_t loopunroll; /* Unroll counter for loop ops in side traces. */
|
2010-02-22 15:57:59 +00:00
|
|
|
int32_t tailcalled; /* Number of successive tailcalls. */
|
2009-12-08 18:46:35 +00:00
|
|
|
int32_t framedepth; /* Current frame depth. */
|
2010-02-03 13:55:56 +00:00
|
|
|
int32_t retdepth; /* Return frame depth (count of RETF). */
|
2009-12-08 18:46:35 +00:00
|
|
|
|
2020-06-15 10:21:05 +00:00
|
|
|
uint32_t k32[LJ_K32__MAX]; /* Common 4 byte constants used by backends. */
|
2010-02-24 22:17:17 +00:00
|
|
|
TValue ksimd[LJ_KSIMD__MAX*2+1]; /* 16 byte aligned SIMD constants. */
|
2020-06-15 10:21:05 +00:00
|
|
|
TValue k64[LJ_K64__MAX]; /* Common 8 byte constants. */
|
2009-12-08 18:46:35 +00:00
|
|
|
|
|
|
|
IRIns *irbuf; /* Temp. IR instruction buffer. Biased with REF_BIAS. */
|
|
|
|
IRRef irtoplim; /* Upper limit of instuction buffer (biased). */
|
|
|
|
IRRef irbotlim; /* Lower limit of instuction buffer (biased). */
|
|
|
|
IRRef loopref; /* Last loop reference or ref of final LOOP (or 0). */
|
|
|
|
|
2010-02-24 22:17:17 +00:00
|
|
|
MSize sizesnap; /* Size of temp. snapshot buffer. */
|
2009-12-08 18:46:35 +00:00
|
|
|
SnapShot *snapbuf; /* Temp. snapshot buffer. */
|
2010-01-25 18:51:52 +00:00
|
|
|
SnapEntry *snapmapbuf; /* Temp. snapshot map buffer. */
|
2009-12-08 18:46:35 +00:00
|
|
|
MSize sizesnapmap; /* Size of temp. snapshot map buffer. */
|
|
|
|
|
2011-01-17 00:20:10 +00:00
|
|
|
PostProc postproc; /* Required post-processing after execution. */
|
2017-06-07 21:56:54 +00:00
|
|
|
#if LJ_SOFTFP32 || (LJ_32 && LJ_HASFFI)
|
2015-05-18 23:59:29 +00:00
|
|
|
uint8_t needsplit; /* Need SPLIT pass. */
|
2011-02-02 01:29:37 +00:00
|
|
|
#endif
|
2015-05-18 23:59:29 +00:00
|
|
|
uint8_t retryrec; /* Retry recording. */
|
2011-01-17 00:20:10 +00:00
|
|
|
|
2010-04-25 01:32:29 +00:00
|
|
|
GCRef *trace; /* Array of traces. */
|
2009-12-08 18:46:35 +00:00
|
|
|
TraceNo freetrace; /* Start of scan for next free trace. */
|
|
|
|
MSize sizetrace; /* Size of trace array. */
|
2016-05-22 21:40:37 +00:00
|
|
|
IRRef1 ktrace; /* Reference to KGC with GCtrace. */
|
2009-12-08 18:46:35 +00:00
|
|
|
|
|
|
|
IRRef1 chain[IR__MAX]; /* IR instruction skip-list chain anchors. */
|
|
|
|
TRef slot[LJ_MAX_JSLOTS+LJ_STACK_EXTRA]; /* Stack slot map. */
|
|
|
|
|
|
|
|
int32_t param[JIT_P__MAX]; /* JIT engine parameters. */
|
|
|
|
|
|
|
|
MCode *exitstubgroup[LJ_MAX_EXITSTUBGR]; /* Exit stub group addresses. */
|
|
|
|
|
|
|
|
HotPenalty penalty[PENALTY_SLOTS]; /* Penalty slots. */
|
|
|
|
uint32_t penaltyslot; /* Round-robin index into penalty slots. */
|
|
|
|
|
2015-06-12 22:42:38 +00:00
|
|
|
#ifdef LUAJIT_ENABLE_TABLE_BUMP
|
2015-05-18 23:59:29 +00:00
|
|
|
RBCHashEntry rbchash[RBCHASH_SLOTS]; /* Reverse bytecode map. */
|
2015-06-12 22:42:38 +00:00
|
|
|
#endif
|
2015-05-18 23:59:29 +00:00
|
|
|
|
2009-12-08 18:46:35 +00:00
|
|
|
BPropEntry bpropcache[BPROP_SLOTS]; /* Backpropagation cache slots. */
|
|
|
|
uint32_t bpropslot; /* Round-robin index into bpropcache slots. */
|
|
|
|
|
2010-03-15 15:23:02 +00:00
|
|
|
ScEvEntry scev; /* Scalar evolution analysis cache slots. */
|
|
|
|
|
2009-12-08 18:46:35 +00:00
|
|
|
const BCIns *startpc; /* Bytecode PC of starting instruction. */
|
|
|
|
TraceNo parent; /* Parent of current side trace (0 for root traces). */
|
|
|
|
ExitNo exitno; /* Exit number in parent of current side trace. */
|
2021-03-22 23:35:46 +00:00
|
|
|
int exitcode; /* Exit code from unwound trace. */
|
2009-12-08 18:46:35 +00:00
|
|
|
|
2010-03-01 05:45:30 +00:00
|
|
|
BCIns *patchpc; /* PC for pending re-patch. */
|
|
|
|
BCIns patchins; /* Instruction for pending re-patch. */
|
|
|
|
|
2010-03-15 22:29:10 +00:00
|
|
|
int mcprot; /* Protection of current mcode area. */
|
2009-12-08 18:46:35 +00:00
|
|
|
MCode *mcarea; /* Base of current mcode area. */
|
|
|
|
MCode *mctop; /* Top of current mcode area. */
|
|
|
|
MCode *mcbot; /* Bottom of current mcode area. */
|
|
|
|
size_t szmcarea; /* Size of current mcode area. */
|
|
|
|
size_t szallmcarea; /* Total size of all allocated mcode areas. */
|
2010-03-15 22:29:10 +00:00
|
|
|
|
|
|
|
TValue errinfo; /* Additional info element for trace errors. */
|
2013-09-08 00:53:23 +00:00
|
|
|
|
|
|
|
#if LJ_HASPROFILE
|
|
|
|
GCproto *prev_pt; /* Previous prototype. */
|
|
|
|
BCLine prev_line; /* Previous line. */
|
|
|
|
int prof_mode; /* Profiling mode: 0, 'f', 'l'. */
|
|
|
|
#endif
|
2020-08-05 13:21:00 +00:00
|
|
|
} jit_State;
|
2009-12-08 18:46:35 +00:00
|
|
|
|
2020-06-12 22:52:54 +00:00
|
|
|
#ifdef LUA_USE_ASSERT
|
|
|
|
#define lj_assertJ(c, ...) lj_assertG_(J2G(J), (c), __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define lj_assertJ(c, ...) ((void)J)
|
|
|
|
#endif
|
|
|
|
|
2009-12-08 18:46:35 +00:00
|
|
|
#endif
|