beta-0.89.2
[luatex.git] / source / libs / luajit / LuaJIT-src / src / lj_jit.h
blob1df56cae55ec6e5f700c2a1d341351b417f6ddb4
1 /*
2 ** Common definitions for the JIT compiler.
3 ** Copyright (C) 2005-2015 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_SSE2 0x00000010
18 #define JIT_F_SSE3 0x00000020
19 #define JIT_F_SSE4_1 0x00000040
20 #define JIT_F_PREFER_IMUL 0x00000080
21 #define JIT_F_LEA_AGU 0x00000100
23 /* Names for the CPU-specific flags. Must match the order above. */
24 #define JIT_F_CPU_FIRST JIT_F_SSE2
25 #define JIT_F_CPUSTRING "\4SSE2\4SSE3\6SSE4.1\3AMD\4ATOM"
26 #elif LJ_TARGET_ARM
27 #define JIT_F_ARMV6_ 0x00000010
28 #define JIT_F_ARMV6T2_ 0x00000020
29 #define JIT_F_ARMV7 0x00000040
30 #define JIT_F_VFPV2 0x00000080
31 #define JIT_F_VFPV3 0x00000100
33 #define JIT_F_ARMV6 (JIT_F_ARMV6_|JIT_F_ARMV6T2_|JIT_F_ARMV7)
34 #define JIT_F_ARMV6T2 (JIT_F_ARMV6T2_|JIT_F_ARMV7)
35 #define JIT_F_VFP (JIT_F_VFPV2|JIT_F_VFPV3)
37 /* Names for the CPU-specific flags. Must match the order above. */
38 #define JIT_F_CPU_FIRST JIT_F_ARMV6_
39 #define JIT_F_CPUSTRING "\5ARMv6\7ARMv6T2\5ARMv7\5VFPv2\5VFPv3"
40 #elif LJ_TARGET_PPC
41 #define JIT_F_SQRT 0x00000010
42 #define JIT_F_ROUND 0x00000020
44 /* Names for the CPU-specific flags. Must match the order above. */
45 #define JIT_F_CPU_FIRST JIT_F_SQRT
46 #define JIT_F_CPUSTRING "\4SQRT\5ROUND"
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_SINK 0x01000000
70 #define JIT_F_OPT_FUSE 0x02000000
72 /* Optimizations names for -O. Must match the order above. */
73 #define JIT_F_OPT_FIRST JIT_F_OPT_FOLD
74 #define JIT_F_OPTSTRING \
75 "\4fold\3cse\3dce\3fwd\3dse\6narrow\4loop\3abc\4sink\4fuse"
77 /* Optimization levels set a fixed combination of flags. */
78 #define JIT_F_OPT_0 0
79 #define JIT_F_OPT_1 (JIT_F_OPT_FOLD|JIT_F_OPT_CSE|JIT_F_OPT_DCE)
80 #define JIT_F_OPT_2 (JIT_F_OPT_1|JIT_F_OPT_NARROW|JIT_F_OPT_LOOP)
81 #define JIT_F_OPT_3 (JIT_F_OPT_2|\
82 JIT_F_OPT_FWD|JIT_F_OPT_DSE|JIT_F_OPT_ABC|JIT_F_OPT_SINK|JIT_F_OPT_FUSE)
83 #define JIT_F_OPT_DEFAULT JIT_F_OPT_3
85 #if LJ_TARGET_WINDOWS || LJ_64
86 /* See: http://blogs.msdn.com/oldnewthing/archive/2003/10/08/55239.aspx */
87 #define JIT_P_sizemcode_DEFAULT 64
88 #else
89 /* Could go as low as 4K, but the mmap() overhead would be rather high. */
90 #define JIT_P_sizemcode_DEFAULT 32
91 #endif
93 /* Optimization parameters and their defaults. Length is a char in octal! */
94 #define JIT_PARAMDEF(_) \
95 _(\010, maxtrace, 1000) /* Max. # of traces in cache. */ \
96 _(\011, maxrecord, 4000) /* Max. # of recorded IR instructions. */ \
97 _(\012, maxirconst, 500) /* Max. # of IR constants of a trace. */ \
98 _(\007, maxside, 100) /* Max. # of side traces of a root trace. */ \
99 _(\007, maxsnap, 500) /* Max. # of snapshots for a trace. */ \
100 _(\011, minstitch, 0) /* Min. # of IR ins for a stitched trace. */ \
102 _(\007, hotloop, 56) /* # of iter. to detect a hot loop/call. */ \
103 _(\007, hotexit, 10) /* # of taken exits to start a side trace. */ \
104 _(\007, tryside, 4) /* # of attempts to compile a side trace. */ \
106 _(\012, instunroll, 4) /* Max. unroll for instable loops. */ \
107 _(\012, loopunroll, 15) /* Max. unroll for loop ops in side traces. */ \
108 _(\012, callunroll, 3) /* Max. unroll for recursive calls. */ \
109 _(\011, recunroll, 2) /* Min. unroll for true recursion. */ \
111 /* Size of each machine code area (in KBytes). */ \
112 _(\011, sizemcode, JIT_P_sizemcode_DEFAULT) \
113 /* Max. total size of all machine code areas (in KBytes). */ \
114 _(\010, maxmcode, 512) \
115 /* End of list. */
117 enum {
118 #define JIT_PARAMENUM(len, name, value) JIT_P_##name,
119 JIT_PARAMDEF(JIT_PARAMENUM)
120 #undef JIT_PARAMENUM
121 JIT_P__MAX
124 #define JIT_PARAMSTR(len, name, value) #len #name
125 #define JIT_P_STRING JIT_PARAMDEF(JIT_PARAMSTR)
127 /* Trace compiler state. */
128 typedef enum {
129 LJ_TRACE_IDLE, /* Trace compiler idle. */
130 LJ_TRACE_ACTIVE = 0x10,
131 LJ_TRACE_RECORD, /* Bytecode recording active. */
132 LJ_TRACE_START, /* New trace started. */
133 LJ_TRACE_END, /* End of trace. */
134 LJ_TRACE_ASM, /* Assemble trace. */
135 LJ_TRACE_ERR /* Trace aborted with error. */
136 } TraceState;
138 /* Post-processing action. */
139 typedef enum {
140 LJ_POST_NONE, /* No action. */
141 LJ_POST_FIXCOMP, /* Fixup comparison and emit pending guard. */
142 LJ_POST_FIXGUARD, /* Fixup and emit pending guard. */
143 LJ_POST_FIXGUARDSNAP, /* Fixup and emit pending guard and snapshot. */
144 LJ_POST_FIXBOOL, /* Fixup boolean result. */
145 LJ_POST_FIXCONST, /* Fixup constant results. */
146 LJ_POST_FFRETRY /* Suppress recording of retried fast functions. */
147 } PostProc;
149 /* Machine code type. */
150 #if LJ_TARGET_X86ORX64
151 typedef uint8_t MCode;
152 #else
153 typedef uint32_t MCode;
154 #endif
156 /* Stack snapshot header. */
157 typedef struct SnapShot {
158 uint16_t mapofs; /* Offset into snapshot map. */
159 IRRef1 ref; /* First IR ref for this snapshot. */
160 uint8_t nslots; /* Number of valid slots. */
161 uint8_t topslot; /* Maximum frame extent. */
162 uint8_t nent; /* Number of compressed entries. */
163 uint8_t count; /* Count of taken exits for this snapshot. */
164 } SnapShot;
166 #define SNAPCOUNT_DONE 255 /* Already compiled and linked a side trace. */
168 /* Compressed snapshot entry. */
169 typedef uint32_t SnapEntry;
171 #define SNAP_FRAME 0x010000 /* Frame slot. */
172 #define SNAP_CONT 0x020000 /* Continuation slot. */
173 #define SNAP_NORESTORE 0x040000 /* No need to restore slot. */
174 #define SNAP_SOFTFPNUM 0x080000 /* Soft-float number. */
175 LJ_STATIC_ASSERT(SNAP_FRAME == TREF_FRAME);
176 LJ_STATIC_ASSERT(SNAP_CONT == TREF_CONT);
178 #define SNAP(slot, flags, ref) (((SnapEntry)(slot) << 24) + (flags) + (ref))
179 #define SNAP_TR(slot, tr) \
180 (((SnapEntry)(slot) << 24) + ((tr) & (TREF_CONT|TREF_FRAME|TREF_REFMASK)))
181 #define SNAP_MKPC(pc) ((SnapEntry)u32ptr(pc))
182 #define SNAP_MKFTSZ(ftsz) ((SnapEntry)(ftsz))
183 #define snap_ref(sn) ((sn) & 0xffff)
184 #define snap_slot(sn) ((BCReg)((sn) >> 24))
185 #define snap_isframe(sn) ((sn) & SNAP_FRAME)
186 #define snap_pc(sn) ((const BCIns *)(uintptr_t)(sn))
187 #define snap_setref(sn, ref) (((sn) & (0xffff0000&~SNAP_NORESTORE)) | (ref))
189 /* Snapshot and exit numbers. */
190 typedef uint32_t SnapNo;
191 typedef uint32_t ExitNo;
193 /* Trace number. */
194 typedef uint32_t TraceNo; /* Used to pass around trace numbers. */
195 typedef uint16_t TraceNo1; /* Stored trace number. */
197 /* Type of link. ORDER LJ_TRLINK */
198 typedef enum {
199 LJ_TRLINK_NONE, /* Incomplete trace. No link, yet. */
200 LJ_TRLINK_ROOT, /* Link to other root trace. */
201 LJ_TRLINK_LOOP, /* Loop to same trace. */
202 LJ_TRLINK_TAILREC, /* Tail-recursion. */
203 LJ_TRLINK_UPREC, /* Up-recursion. */
204 LJ_TRLINK_DOWNREC, /* Down-recursion. */
205 LJ_TRLINK_INTERP, /* Fallback to interpreter. */
206 LJ_TRLINK_RETURN, /* Return to interpreter. */
207 LJ_TRLINK_STITCH /* Trace stitching. */
208 } TraceLink;
210 /* Trace object. */
211 typedef struct GCtrace {
212 GCHeader;
213 uint8_t topslot; /* Top stack slot already checked to be allocated. */
214 uint8_t linktype; /* Type of link. */
215 IRRef nins; /* Next IR instruction. Biased with REF_BIAS. */
216 #if LJ_GC64
217 uint32_t unused_gc64;
218 #endif
219 GCRef gclist;
220 IRIns *ir; /* IR instructions/constants. Biased with REF_BIAS. */
221 IRRef nk; /* Lowest IR constant. Biased with REF_BIAS. */
222 uint16_t nsnap; /* Number of snapshots. */
223 uint16_t nsnapmap; /* Number of snapshot map elements. */
224 SnapShot *snap; /* Snapshot array. */
225 SnapEntry *snapmap; /* Snapshot map. */
226 GCRef startpt; /* Starting prototype. */
227 MRef startpc; /* Bytecode PC of starting instruction. */
228 BCIns startins; /* Original bytecode of starting instruction. */
229 MSize szmcode; /* Size of machine code. */
230 MCode *mcode; /* Start of machine code. */
231 MSize mcloop; /* Offset of loop start in machine code. */
232 uint16_t nchild; /* Number of child traces (root trace only). */
233 uint16_t spadjust; /* Stack pointer adjustment (offset in bytes). */
234 TraceNo1 traceno; /* Trace number. */
235 TraceNo1 link; /* Linked trace (or self for loops). */
236 TraceNo1 root; /* Root trace of side trace (or 0 for root traces). */
237 TraceNo1 nextroot; /* Next root trace for same prototype. */
238 TraceNo1 nextside; /* Next side trace of same root trace. */
239 uint8_t sinktags; /* Trace has SINK tags. */
240 uint8_t unused1;
241 #ifdef LUAJIT_USE_GDBJIT
242 void *gdbjit_entry; /* GDB JIT entry. */
243 #endif
244 } GCtrace;
246 #define gco2trace(o) check_exp((o)->gch.gct == ~LJ_TTRACE, (GCtrace *)(o))
247 #define traceref(J, n) \
248 check_exp((n)>0 && (MSize)(n)<J->sizetrace, (GCtrace *)gcref(J->trace[(n)]))
250 LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCtrace, gclist));
252 static LJ_AINLINE MSize snap_nextofs(GCtrace *T, SnapShot *snap)
254 if (snap+1 == &T->snap[T->nsnap])
255 return T->nsnapmap;
256 else
257 return (snap+1)->mapofs;
260 /* Round-robin penalty cache for bytecodes leading to aborted traces. */
261 typedef struct HotPenalty {
262 MRef pc; /* Starting bytecode PC. */
263 uint16_t val; /* Penalty value, i.e. hotcount start. */
264 uint16_t reason; /* Abort reason (really TraceErr). */
265 } HotPenalty;
267 #define PENALTY_SLOTS 64 /* Penalty cache slot. Must be a power of 2. */
268 #define PENALTY_MIN (36*2) /* Minimum penalty value. */
269 #define PENALTY_MAX 60000 /* Maximum penalty value. */
270 #define PENALTY_RNDBITS 4 /* # of random bits to add to penalty value. */
272 /* Round-robin backpropagation cache for narrowing conversions. */
273 typedef struct BPropEntry {
274 IRRef1 key; /* Key: original reference. */
275 IRRef1 val; /* Value: reference after conversion. */
276 IRRef mode; /* Mode for this entry (currently IRCONV_*). */
277 } BPropEntry;
279 /* Number of slots for the backpropagation cache. Must be a power of 2. */
280 #define BPROP_SLOTS 16
282 /* Scalar evolution analysis cache. */
283 typedef struct ScEvEntry {
284 MRef pc; /* Bytecode PC of FORI. */
285 IRRef1 idx; /* Index reference. */
286 IRRef1 start; /* Constant start reference. */
287 IRRef1 stop; /* Constant stop reference. */
288 IRRef1 step; /* Constant step reference. */
289 IRType1 t; /* Scalar type. */
290 uint8_t dir; /* Direction. 1: +, 0: -. */
291 } ScEvEntry;
293 /* Reverse bytecode map (IRRef -> PC). Only for selected instructions. */
294 typedef struct RBCHashEntry {
295 MRef pc; /* Bytecode PC. */
296 GCRef pt; /* Prototype. */
297 IRRef ref; /* IR reference. */
298 } RBCHashEntry;
300 /* Number of slots in the reverse bytecode hash table. Must be a power of 2. */
301 #define RBCHASH_SLOTS 8
303 /* 128 bit SIMD constants. */
304 enum {
305 LJ_KSIMD_ABS,
306 LJ_KSIMD_NEG,
307 LJ_KSIMD__MAX
310 /* Get 16 byte aligned pointer to SIMD constant. */
311 #define LJ_KSIMD(J, n) \
312 ((TValue *)(((intptr_t)&J->ksimd[2*(n)] + 15) & ~(intptr_t)15))
314 /* Set/reset flag to activate the SPLIT pass for the current trace. */
315 #if LJ_SOFTFP || (LJ_32 && LJ_HASFFI)
316 #define lj_needsplit(J) (J->needsplit = 1)
317 #define lj_resetsplit(J) (J->needsplit = 0)
318 #else
319 #define lj_needsplit(J) UNUSED(J)
320 #define lj_resetsplit(J) UNUSED(J)
321 #endif
323 /* Fold state is used to fold instructions on-the-fly. */
324 typedef struct FoldState {
325 IRIns ins; /* Currently emitted instruction. */
326 IRIns left; /* Instruction referenced by left operand. */
327 IRIns right; /* Instruction referenced by right operand. */
328 } FoldState;
330 /* JIT compiler state. */
331 typedef struct jit_State {
332 GCtrace cur; /* Current trace. */
334 lua_State *L; /* Current Lua state. */
335 const BCIns *pc; /* Current PC. */
336 GCfunc *fn; /* Current function. */
337 GCproto *pt; /* Current prototype. */
338 TRef *base; /* Current frame base, points into J->slots. */
340 uint32_t flags; /* JIT engine flags. */
341 BCReg maxslot; /* Relative to baseslot. */
342 BCReg baseslot; /* Current frame base, offset into J->slots. */
344 uint8_t mergesnap; /* Allowed to merge with next snapshot. */
345 uint8_t needsnap; /* Need snapshot before recording next bytecode. */
346 IRType1 guardemit; /* Accumulated IRT_GUARD for emitted instructions. */
347 uint8_t bcskip; /* Number of bytecode instructions to skip. */
349 FoldState fold; /* Fold state. */
351 const BCIns *bc_min; /* Start of allowed bytecode range for root trace. */
352 MSize bc_extent; /* Extent of the range. */
354 TraceState state; /* Trace compiler state. */
356 int32_t instunroll; /* Unroll counter for instable loops. */
357 int32_t loopunroll; /* Unroll counter for loop ops in side traces. */
358 int32_t tailcalled; /* Number of successive tailcalls. */
359 int32_t framedepth; /* Current frame depth. */
360 int32_t retdepth; /* Return frame depth (count of RETF). */
362 MRef k64; /* Pointer to chained array of 64 bit constants. */
363 TValue ksimd[LJ_KSIMD__MAX*2+1]; /* 16 byte aligned SIMD constants. */
365 IRIns *irbuf; /* Temp. IR instruction buffer. Biased with REF_BIAS. */
366 IRRef irtoplim; /* Upper limit of instuction buffer (biased). */
367 IRRef irbotlim; /* Lower limit of instuction buffer (biased). */
368 IRRef loopref; /* Last loop reference or ref of final LOOP (or 0). */
370 MSize sizesnap; /* Size of temp. snapshot buffer. */
371 SnapShot *snapbuf; /* Temp. snapshot buffer. */
372 SnapEntry *snapmapbuf; /* Temp. snapshot map buffer. */
373 MSize sizesnapmap; /* Size of temp. snapshot map buffer. */
375 PostProc postproc; /* Required post-processing after execution. */
376 #if LJ_SOFTFP || (LJ_32 && LJ_HASFFI)
377 uint8_t needsplit; /* Need SPLIT pass. */
378 #endif
379 uint8_t retryrec; /* Retry recording. */
381 GCRef *trace; /* Array of traces. */
382 TraceNo freetrace; /* Start of scan for next free trace. */
383 MSize sizetrace; /* Size of trace array. */
385 IRRef1 chain[IR__MAX]; /* IR instruction skip-list chain anchors. */
386 TRef slot[LJ_MAX_JSLOTS+LJ_STACK_EXTRA]; /* Stack slot map. */
388 int32_t param[JIT_P__MAX]; /* JIT engine parameters. */
390 MCode *exitstubgroup[LJ_MAX_EXITSTUBGR]; /* Exit stub group addresses. */
392 HotPenalty penalty[PENALTY_SLOTS]; /* Penalty slots. */
393 uint32_t penaltyslot; /* Round-robin index into penalty slots. */
394 uint32_t prngstate; /* PRNG state. */
396 #ifdef LUAJIT_ENABLE_TABLE_BUMP
397 RBCHashEntry rbchash[RBCHASH_SLOTS]; /* Reverse bytecode map. */
398 #endif
400 BPropEntry bpropcache[BPROP_SLOTS]; /* Backpropagation cache slots. */
401 uint32_t bpropslot; /* Round-robin index into bpropcache slots. */
403 ScEvEntry scev; /* Scalar evolution analysis cache slots. */
405 const BCIns *startpc; /* Bytecode PC of starting instruction. */
406 TraceNo parent; /* Parent of current side trace (0 for root traces). */
407 ExitNo exitno; /* Exit number in parent of current side trace. */
409 BCIns *patchpc; /* PC for pending re-patch. */
410 BCIns patchins; /* Instruction for pending re-patch. */
412 int mcprot; /* Protection of current mcode area. */
413 MCode *mcarea; /* Base of current mcode area. */
414 MCode *mctop; /* Top of current mcode area. */
415 MCode *mcbot; /* Bottom of current mcode area. */
416 size_t szmcarea; /* Size of current mcode area. */
417 size_t szallmcarea; /* Total size of all allocated mcode areas. */
419 TValue errinfo; /* Additional info element for trace errors. */
421 #if LJ_HASPROFILE
422 GCproto *prev_pt; /* Previous prototype. */
423 BCLine prev_line; /* Previous line. */
424 int prof_mode; /* Profiling mode: 0, 'f', 'l'. */
425 #endif
427 #if LJ_TARGET_ARM
428 LJ_ALIGN(16) /* For DISPATCH-relative addresses in assembler part. */
429 #endif
430 jit_State;
432 /* Trivial PRNG e.g. used for penalty randomization. */
433 static LJ_AINLINE uint32_t LJ_PRNG_BITS(jit_State *J, int bits)
435 /* Yes, this LCG is very weak, but that doesn't matter for our use case. */
436 J->prngstate = J->prngstate * 1103515245 + 12345;
437 return J->prngstate >> (32-bits);
440 #endif