CONSOLE: Handle unimplemented features for console toolchains.
[luajit-2.0.git] / src / lj_jit.h
blob6d317a9e79cbbcc8ac2b89d0f33a7be8394b2b03
1 /*
2 ** Common definitions for the JIT compiler.
3 ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #ifndef _LJ_JIT_H
7 #define _LJ_JIT_H
9 #include "lj_obj.h"
10 #include "lj_ir.h"
12 /* JIT engine flags. */
13 #define JIT_F_ON 0x00000001
15 /* CPU-specific JIT engine flags. */
16 #if LJ_TARGET_X86ORX64
17 #define JIT_F_CMOV 0x00000010
18 #define JIT_F_SSE2 0x00000020
19 #define JIT_F_SSE3 0x00000040
20 #define JIT_F_SSE4_1 0x00000080
21 #define JIT_F_P4 0x00000100
22 #define JIT_F_PREFER_IMUL 0x00000200
23 #define JIT_F_SPLIT_XMM 0x00000400
24 #define JIT_F_LEA_AGU 0x00000800
26 /* Names for the CPU-specific flags. Must match the order above. */
27 #define JIT_F_CPU_FIRST JIT_F_CMOV
28 #define JIT_F_CPUSTRING "\4CMOV\4SSE2\4SSE3\6SSE4.1\2P4\3AMD\2K8\4ATOM"
29 #elif LJ_TARGET_ARM
30 #define JIT_F_ARMV6 0x00000010
31 #define JIT_F_ARMV6T2 0x00000020
32 #define JIT_F_ARMV7 0x00000040
34 /* Names for the CPU-specific flags. Must match the order above. */
35 #define JIT_F_CPU_FIRST JIT_F_ARMV6
36 #define JIT_F_CPUSTRING "\5ARMv6\7ARMv6T2\5ARMv7"
37 #elif LJ_TARGET_PPC
38 #define JIT_F_PPC64 0x00000010
39 #define JIT_F_SQRT 0x00000020
40 #define JIT_F_ROUND 0x00000040
41 #define JIT_F_CELL 0x00000080
42 #define JIT_F_XENON 0x00000100
44 /* Names for the CPU-specific flags. Must match the order above. */
45 #define JIT_F_CPU_FIRST JIT_F_PPC64
46 #define JIT_F_CPUSTRING "\5PPC64\4SQRT\5ROUND\4CELL\5XENON"
47 #elif LJ_TARGET_MIPS
48 #define JIT_F_MIPS32R2 0x00000010
50 /* Names for the CPU-specific flags. Must match the order above. */
51 #define JIT_F_CPU_FIRST JIT_F_MIPS32R2
52 #define JIT_F_CPUSTRING "\010MIPS32R2"
53 #else
54 #define JIT_F_CPU_FIRST 0
55 #define JIT_F_CPUSTRING ""
56 #endif
58 /* Optimization flags. */
59 #define JIT_F_OPT_MASK 0x0fff0000
61 #define JIT_F_OPT_FOLD 0x00010000
62 #define JIT_F_OPT_CSE 0x00020000
63 #define JIT_F_OPT_DCE 0x00040000
64 #define JIT_F_OPT_FWD 0x00080000
65 #define JIT_F_OPT_DSE 0x00100000
66 #define JIT_F_OPT_NARROW 0x00200000
67 #define JIT_F_OPT_LOOP 0x00400000
68 #define JIT_F_OPT_ABC 0x00800000
69 #define JIT_F_OPT_FUSE 0x01000000
71 /* Optimizations names for -O. Must match the order above. */
72 #define JIT_F_OPT_FIRST JIT_F_OPT_FOLD
73 #define JIT_F_OPTSTRING \
74 "\4fold\3cse\3dce\3fwd\3dse\6narrow\4loop\3abc\4fuse"
76 /* Optimization levels set a fixed combination of flags. */
77 #define JIT_F_OPT_0 0
78 #define JIT_F_OPT_1 (JIT_F_OPT_FOLD|JIT_F_OPT_CSE|JIT_F_OPT_DCE)
79 #define JIT_F_OPT_2 (JIT_F_OPT_1|JIT_F_OPT_NARROW|JIT_F_OPT_LOOP)
80 #define JIT_F_OPT_3 \
81 (JIT_F_OPT_2|JIT_F_OPT_FWD|JIT_F_OPT_DSE|JIT_F_OPT_ABC|JIT_F_OPT_FUSE)
82 #define JIT_F_OPT_DEFAULT JIT_F_OPT_3
84 #if LJ_TARGET_WINDOWS || LJ_64
85 /* See: http://blogs.msdn.com/oldnewthing/archive/2003/10/08/55239.aspx */
86 #define JIT_P_sizemcode_DEFAULT 64
87 #else
88 /* Could go as low as 4K, but the mmap() overhead would be rather high. */
89 #define JIT_P_sizemcode_DEFAULT 32
90 #endif
92 /* Optimization parameters and their defaults. Length is a char in octal! */
93 #define JIT_PARAMDEF(_) \
94 _(\010, maxtrace, 1000) /* Max. # of traces in cache. */ \
95 _(\011, maxrecord, 4000) /* Max. # of recorded IR instructions. */ \
96 _(\012, maxirconst, 500) /* Max. # of IR constants of a trace. */ \
97 _(\007, maxside, 100) /* Max. # of side traces of a root trace. */ \
98 _(\007, maxsnap, 500) /* Max. # of snapshots for a trace. */ \
100 _(\007, hotloop, 56) /* # of iter. to detect a hot loop/call. */ \
101 _(\007, hotexit, 10) /* # of taken exits to start a side trace. */ \
102 _(\007, tryside, 4) /* # of attempts to compile a side trace. */ \
104 _(\012, instunroll, 4) /* Max. unroll for instable loops. */ \
105 _(\012, loopunroll, 15) /* Max. unroll for loop ops in side traces. */ \
106 _(\012, callunroll, 3) /* Max. unroll for recursive calls. */ \
107 _(\011, recunroll, 2) /* Min. unroll for true recursion. */ \
109 /* Size of each machine code area (in KBytes). */ \
110 _(\011, sizemcode, JIT_P_sizemcode_DEFAULT) \
111 /* Max. total size of all machine code areas (in KBytes). */ \
112 _(\010, maxmcode, 512) \
113 /* End of list. */
115 enum {
116 #define JIT_PARAMENUM(len, name, value) JIT_P_##name,
117 JIT_PARAMDEF(JIT_PARAMENUM)
118 #undef JIT_PARAMENUM
119 JIT_P__MAX
122 #define JIT_PARAMSTR(len, name, value) #len #name
123 #define JIT_P_STRING JIT_PARAMDEF(JIT_PARAMSTR)
125 /* Trace compiler state. */
126 typedef enum {
127 LJ_TRACE_IDLE, /* Trace compiler idle. */
128 LJ_TRACE_ACTIVE = 0x10,
129 LJ_TRACE_RECORD, /* Bytecode recording active. */
130 LJ_TRACE_START, /* New trace started. */
131 LJ_TRACE_END, /* End of trace. */
132 LJ_TRACE_ASM, /* Assemble trace. */
133 LJ_TRACE_ERR /* Trace aborted with error. */
134 } TraceState;
136 /* Post-processing action. */
137 typedef enum {
138 LJ_POST_NONE, /* No action. */
139 LJ_POST_FIXCOMP, /* Fixup comparison and emit pending guard. */
140 LJ_POST_FIXGUARD, /* Fixup and emit pending guard. */
141 LJ_POST_FIXGUARDSNAP, /* Fixup and emit pending guard and snapshot. */
142 LJ_POST_FIXBOOL, /* Fixup boolean result. */
143 LJ_POST_FFRETRY /* Suppress recording of retried fast functions. */
144 } PostProc;
146 /* Machine code type. */
147 #if LJ_TARGET_X86ORX64
148 typedef uint8_t MCode;
149 #else
150 typedef uint32_t MCode;
151 #endif
153 /* Stack snapshot header. */
154 typedef struct SnapShot {
155 uint16_t mapofs; /* Offset into snapshot map. */
156 IRRef1 ref; /* First IR ref for this snapshot. */
157 uint8_t nslots; /* Number of valid slots. */
158 uint8_t topslot; /* Maximum frame extent. */
159 uint8_t nent; /* Number of compressed entries. */
160 uint8_t count; /* Count of taken exits for this snapshot. */
161 } SnapShot;
163 #define SNAPCOUNT_DONE 255 /* Already compiled and linked a side trace. */
165 /* Compressed snapshot entry. */
166 typedef uint32_t SnapEntry;
168 #define SNAP_FRAME 0x010000 /* Frame slot. */
169 #define SNAP_CONT 0x020000 /* Continuation slot. */
170 #define SNAP_NORESTORE 0x040000 /* No need to restore slot. */
171 #define SNAP_SOFTFPNUM 0x080000 /* Soft-float number. */
172 LJ_STATIC_ASSERT(SNAP_FRAME == TREF_FRAME);
173 LJ_STATIC_ASSERT(SNAP_CONT == TREF_CONT);
175 #define SNAP(slot, flags, ref) (((SnapEntry)(slot) << 24) + (flags) + (ref))
176 #define SNAP_TR(slot, tr) \
177 (((SnapEntry)(slot) << 24) + ((tr) & (TREF_CONT|TREF_FRAME|TREF_REFMASK)))
178 #define SNAP_MKPC(pc) ((SnapEntry)u32ptr(pc))
179 #define SNAP_MKFTSZ(ftsz) ((SnapEntry)(ftsz))
180 #define snap_ref(sn) ((sn) & 0xffff)
181 #define snap_slot(sn) ((BCReg)((sn) >> 24))
182 #define snap_isframe(sn) ((sn) & SNAP_FRAME)
183 #define snap_pc(sn) ((const BCIns *)(uintptr_t)(sn))
184 #define snap_setref(sn, ref) (((sn) & (0xffff0000&~SNAP_NORESTORE)) | (ref))
186 /* Snapshot and exit numbers. */
187 typedef uint32_t SnapNo;
188 typedef uint32_t ExitNo;
190 /* Trace number. */
191 typedef uint32_t TraceNo; /* Used to pass around trace numbers. */
192 typedef uint16_t TraceNo1; /* Stored trace number. */
194 /* Type of link. ORDER LJ_TRLINK */
195 typedef enum {
196 LJ_TRLINK_NONE, /* Incomplete trace. No link, yet. */
197 LJ_TRLINK_ROOT, /* Link to other root trace. */
198 LJ_TRLINK_LOOP, /* Loop to same trace. */
199 LJ_TRLINK_TAILREC, /* Tail-recursion. */
200 LJ_TRLINK_UPREC, /* Up-recursion. */
201 LJ_TRLINK_DOWNREC, /* Down-recursion. */
202 LJ_TRLINK_INTERP, /* Fallback to interpreter. */
203 LJ_TRLINK_RETURN /* Return to interpreter. */
204 } TraceLink;
206 /* Trace object. */
207 typedef struct GCtrace {
208 GCHeader;
209 uint8_t topslot; /* Top stack slot already checked to be allocated. */
210 uint8_t linktype; /* Type of link. */
211 IRRef nins; /* Next IR instruction. Biased with REF_BIAS. */
212 GCRef gclist;
213 IRIns *ir; /* IR instructions/constants. Biased with REF_BIAS. */
214 IRRef nk; /* Lowest IR constant. Biased with REF_BIAS. */
215 uint16_t nsnap; /* Number of snapshots. */
216 uint16_t nsnapmap; /* Number of snapshot map elements. */
217 SnapShot *snap; /* Snapshot array. */
218 SnapEntry *snapmap; /* Snapshot map. */
219 GCRef startpt; /* Starting prototype. */
220 MRef startpc; /* Bytecode PC of starting instruction. */
221 BCIns startins; /* Original bytecode of starting instruction. */
222 MSize szmcode; /* Size of machine code. */
223 MCode *mcode; /* Start of machine code. */
224 MSize mcloop; /* Offset of loop start in machine code. */
225 uint16_t nchild; /* Number of child traces (root trace only). */
226 uint16_t spadjust; /* Stack pointer adjustment (offset in bytes). */
227 TraceNo1 traceno; /* Trace number. */
228 TraceNo1 link; /* Linked trace (or self for loops). */
229 TraceNo1 root; /* Root trace of side trace (or 0 for root traces). */
230 TraceNo1 nextroot; /* Next root trace for same prototype. */
231 TraceNo1 nextside; /* Next side trace of same root trace. */
232 uint16_t unused2;
233 #ifdef LUAJIT_USE_GDBJIT
234 void *gdbjit_entry; /* GDB JIT entry. */
235 #endif
236 } GCtrace;
238 #define gco2trace(o) check_exp((o)->gch.gct == ~LJ_TTRACE, (GCtrace *)(o))
239 #define traceref(J, n) \
240 check_exp((n)>0 && (MSize)(n)<J->sizetrace, (GCtrace *)gcref(J->trace[(n)]))
242 LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCtrace, gclist));
244 static LJ_AINLINE MSize snap_nextofs(GCtrace *T, SnapShot *snap)
246 if (snap+1 == &T->snap[T->nsnap])
247 return T->nsnapmap;
248 else
249 return (snap+1)->mapofs;
252 /* Round-robin penalty cache for bytecodes leading to aborted traces. */
253 typedef struct HotPenalty {
254 MRef pc; /* Starting bytecode PC. */
255 uint16_t val; /* Penalty value, i.e. hotcount start. */
256 uint16_t reason; /* Abort reason (really TraceErr). */
257 } HotPenalty;
259 #define PENALTY_SLOTS 64 /* Penalty cache slot. Must be a power of 2. */
260 #define PENALTY_MIN (36*2) /* Minimum penalty value. */
261 #define PENALTY_MAX 60000 /* Maximum penalty value. */
262 #define PENALTY_RNDBITS 4 /* # of random bits to add to penalty value. */
264 /* Round-robin backpropagation cache for narrowing conversions. */
265 typedef struct BPropEntry {
266 IRRef1 key; /* Key: original reference. */
267 IRRef1 val; /* Value: reference after conversion. */
268 IRRef mode; /* Mode for this entry (currently IRCONV_*). */
269 } BPropEntry;
271 /* Number of slots for the backpropagation cache. Must be a power of 2. */
272 #define BPROP_SLOTS 16
274 /* Scalar evolution analysis cache. */
275 typedef struct ScEvEntry {
276 IRRef1 idx; /* Index reference. */
277 IRRef1 start; /* Constant start reference. */
278 IRRef1 stop; /* Constant stop reference. */
279 IRRef1 step; /* Constant step reference. */
280 IRType1 t; /* Scalar type. */
281 uint8_t dir; /* Direction. 1: +, 0: -. */
282 } ScEvEntry;
284 /* 128 bit SIMD constants. */
285 enum {
286 LJ_KSIMD_ABS,
287 LJ_KSIMD_NEG,
288 LJ_KSIMD__MAX
291 /* Get 16 byte aligned pointer to SIMD constant. */
292 #define LJ_KSIMD(J, n) \
293 ((TValue *)(((intptr_t)&J->ksimd[2*(n)] + 15) & ~(intptr_t)15))
295 /* Set/reset flag to activate the SPLIT pass for the current trace. */
296 #if LJ_SOFTFP || (LJ_32 && LJ_HASFFI)
297 #define lj_needsplit(J) (J->needsplit = 1)
298 #define lj_resetsplit(J) (J->needsplit = 0)
299 #else
300 #define lj_needsplit(J) UNUSED(J)
301 #define lj_resetsplit(J) UNUSED(J)
302 #endif
304 /* Fold state is used to fold instructions on-the-fly. */
305 typedef struct FoldState {
306 IRIns ins; /* Currently emitted instruction. */
307 IRIns left; /* Instruction referenced by left operand. */
308 IRIns right; /* Instruction referenced by right operand. */
309 } FoldState;
311 /* JIT compiler state. */
312 typedef struct jit_State {
313 GCtrace cur; /* Current trace. */
315 lua_State *L; /* Current Lua state. */
316 const BCIns *pc; /* Current PC. */
317 GCfunc *fn; /* Current function. */
318 GCproto *pt; /* Current prototype. */
319 TRef *base; /* Current frame base, points into J->slots. */
321 uint32_t flags; /* JIT engine flags. */
322 BCReg maxslot; /* Relative to baseslot. */
323 BCReg baseslot; /* Current frame base, offset into J->slots. */
325 uint8_t mergesnap; /* Allowed to merge with next snapshot. */
326 uint8_t needsnap; /* Need snapshot before recording next bytecode. */
327 IRType1 guardemit; /* Accumulated IRT_GUARD for emitted instructions. */
328 uint8_t bcskip; /* Number of bytecode instructions to skip. */
330 FoldState fold; /* Fold state. */
332 const BCIns *bc_min; /* Start of allowed bytecode range for root trace. */
333 MSize bc_extent; /* Extent of the range. */
335 TraceState state; /* Trace compiler state. */
337 int32_t instunroll; /* Unroll counter for instable loops. */
338 int32_t loopunroll; /* Unroll counter for loop ops in side traces. */
339 int32_t tailcalled; /* Number of successive tailcalls. */
340 int32_t framedepth; /* Current frame depth. */
341 int32_t retdepth; /* Return frame depth (count of RETF). */
343 MRef k64; /* Pointer to chained array of 64 bit constants. */
344 TValue ksimd[LJ_KSIMD__MAX*2+1]; /* 16 byte aligned SIMD constants. */
346 IRIns *irbuf; /* Temp. IR instruction buffer. Biased with REF_BIAS. */
347 IRRef irtoplim; /* Upper limit of instuction buffer (biased). */
348 IRRef irbotlim; /* Lower limit of instuction buffer (biased). */
349 IRRef loopref; /* Last loop reference or ref of final LOOP (or 0). */
351 MSize sizesnap; /* Size of temp. snapshot buffer. */
352 SnapShot *snapbuf; /* Temp. snapshot buffer. */
353 SnapEntry *snapmapbuf; /* Temp. snapshot map buffer. */
354 MSize sizesnapmap; /* Size of temp. snapshot map buffer. */
356 PostProc postproc; /* Required post-processing after execution. */
357 #if LJ_SOFTFP || (LJ_32 && LJ_HASFFI)
358 int needsplit; /* Need SPLIT pass. */
359 #endif
361 GCRef *trace; /* Array of traces. */
362 TraceNo freetrace; /* Start of scan for next free trace. */
363 MSize sizetrace; /* Size of trace array. */
365 IRRef1 chain[IR__MAX]; /* IR instruction skip-list chain anchors. */
366 TRef slot[LJ_MAX_JSLOTS+LJ_STACK_EXTRA]; /* Stack slot map. */
368 int32_t param[JIT_P__MAX]; /* JIT engine parameters. */
370 MCode *exitstubgroup[LJ_MAX_EXITSTUBGR]; /* Exit stub group addresses. */
372 HotPenalty penalty[PENALTY_SLOTS]; /* Penalty slots. */
373 uint32_t penaltyslot; /* Round-robin index into penalty slots. */
374 uint32_t prngstate; /* PRNG state. */
376 BPropEntry bpropcache[BPROP_SLOTS]; /* Backpropagation cache slots. */
377 uint32_t bpropslot; /* Round-robin index into bpropcache slots. */
379 ScEvEntry scev; /* Scalar evolution analysis cache slots. */
381 const BCIns *startpc; /* Bytecode PC of starting instruction. */
382 TraceNo parent; /* Parent of current side trace (0 for root traces). */
383 ExitNo exitno; /* Exit number in parent of current side trace. */
385 BCIns *patchpc; /* PC for pending re-patch. */
386 BCIns patchins; /* Instruction for pending re-patch. */
388 int mcprot; /* Protection of current mcode area. */
389 MCode *mcarea; /* Base of current mcode area. */
390 MCode *mctop; /* Top of current mcode area. */
391 MCode *mcbot; /* Bottom of current mcode area. */
392 size_t szmcarea; /* Size of current mcode area. */
393 size_t szallmcarea; /* Total size of all allocated mcode areas. */
395 TValue errinfo; /* Additional info element for trace errors. */
397 #if LJ_TARGET_ARM
398 LJ_ALIGN(16) /* For DISPATCH-relative addresses in assembler part. */
399 #endif
400 jit_State;
402 /* Trivial PRNG e.g. used for penalty randomization. */
403 static LJ_AINLINE uint32_t LJ_PRNG_BITS(jit_State *J, int bits)
405 /* Yes, this LCG is very weak, but that doesn't matter for our use case. */
406 J->prngstate = J->prngstate * 1103515245 + 12345;
407 return J->prngstate >> (32-bits);
410 #endif