| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | #ifndef _LJ_JIT_H |
| 7 | #define _LJ_JIT_H |
| 8 | |
| 9 | #include "lj_obj.h" |
| 10 | #include "lj_ir.h" |
| 11 | |
| 12 | |
| 13 | #define JIT_F_ON 0x00000001 |
| 14 | |
| 15 | |
| 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 |
| 25 | |
| 26 | |
| 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 |
| 33 | #define JIT_F_VFPV2 0x00000080 |
| 34 | #define JIT_F_VFPV3 0x00000100 |
| 35 | |
| 36 | #define JIT_F_ARMV6 (JIT_F_ARMV6_|JIT_F_ARMV6T2_|JIT_F_ARMV7) |
| 37 | #define JIT_F_ARMV6T2 (JIT_F_ARMV6T2_|JIT_F_ARMV7) |
| 38 | #define JIT_F_VFP (JIT_F_VFPV2|JIT_F_VFPV3) |
| 39 | |
| 40 | |
| 41 | #define JIT_F_CPU_FIRST JIT_F_ARMV6_ |
| 42 | #define JIT_F_CPUSTRING "\5ARMv6\7ARMv6T2\5ARMv7\5VFPv2\5VFPv3" |
| 43 | #elif LJ_TARGET_PPC |
| 44 | #define JIT_F_SQRT 0x00000010 |
| 45 | #define JIT_F_ROUND 0x00000020 |
| 46 | |
| 47 | |
| 48 | #define JIT_F_CPU_FIRST JIT_F_SQRT |
| 49 | #define JIT_F_CPUSTRING "\4SQRT\5ROUND" |
| 50 | #elif LJ_TARGET_MIPS |
| 51 | #define JIT_F_MIPS32R2 0x00000010 |
| 52 | |
| 53 | |
| 54 | #define JIT_F_CPU_FIRST JIT_F_MIPS32R2 |
| 55 | #define JIT_F_CPUSTRING "\010MIPS32R2" |
| 56 | #else |
| 57 | #define JIT_F_CPU_FIRST 0 |
| 58 | #define JIT_F_CPUSTRING "" |
| 59 | #endif |
| 60 | |
| 61 | |
| 62 | #define JIT_F_OPT_MASK 0x0fff0000 |
| 63 | |
| 64 | #define JIT_F_OPT_FOLD 0x00010000 |
| 65 | #define JIT_F_OPT_CSE 0x00020000 |
| 66 | #define JIT_F_OPT_DCE 0x00040000 |
| 67 | #define JIT_F_OPT_FWD 0x00080000 |
| 68 | #define JIT_F_OPT_DSE 0x00100000 |
| 69 | #define JIT_F_OPT_NARROW 0x00200000 |
| 70 | #define JIT_F_OPT_LOOP 0x00400000 |
| 71 | #define JIT_F_OPT_ABC 0x00800000 |
| 72 | #define JIT_F_OPT_SINK 0x01000000 |
| 73 | #define JIT_F_OPT_FUSE 0x02000000 |
| 74 | |
| 75 | |
| 76 | #define JIT_F_OPT_FIRST JIT_F_OPT_FOLD |
| 77 | #define JIT_F_OPTSTRING \ |
| 78 | "\4fold\3cse\3dce\3fwd\3dse\6narrow\4loop\3abc\4sink\4fuse" |
| 79 | |
| 80 | |
| 81 | #define JIT_F_OPT_0 0 |
| 82 | #define JIT_F_OPT_1 (JIT_F_OPT_FOLD|JIT_F_OPT_CSE|JIT_F_OPT_DCE) |
| 83 | #define JIT_F_OPT_2 (JIT_F_OPT_1|JIT_F_OPT_NARROW|JIT_F_OPT_LOOP) |
| 84 | #define JIT_F_OPT_3 (JIT_F_OPT_2|\ |
| 85 | JIT_F_OPT_FWD|JIT_F_OPT_DSE|JIT_F_OPT_ABC|JIT_F_OPT_SINK|JIT_F_OPT_FUSE) |
| 86 | #define JIT_F_OPT_DEFAULT JIT_F_OPT_3 |
| 87 | |
| 88 | #if LJ_TARGET_WINDOWS || LJ_64 |
| 89 | |
| 90 | #define JIT_P_sizemcode_DEFAULT 64 |
| 91 | #else |
| 92 | |
| 93 | #define JIT_P_sizemcode_DEFAULT 32 |
| 94 | #endif |
| 95 | |
| 96 | |
| 97 | #define JIT_PARAMDEF(_) \ |
| 98 | _(\010, maxtrace, 1000) \ |
| 99 | _(\011, maxrecord, 4000) \ |
| 100 | _(\012, maxirconst, 500) \ |
| 101 | _(\007, maxside, 100) \ |
| 102 | _(\007, maxsnap, 500) \ |
| 103 | \ |
| 104 | _(\007, hotloop, 56) \ |
| 105 | _(\007, hotexit, 10) \ |
| 106 | _(\007, tryside, 4) \ |
| 107 | \ |
| 108 | _(\012, instunroll, 4) \ |
| 109 | _(\012, loopunroll, 15) \ |
| 110 | _(\012, callunroll, 3) \ |
| 111 | _(\011, recunroll, 2) \ |
| 112 | \ |
| 113 | \ |
| 114 | _(\011, sizemcode, JIT_P_sizemcode_DEFAULT) \ |
| 115 | \ |
| 116 | _(\010, maxmcode, 512) \ |
| 117 | |
| 118 | |
| 119 | enum { |
| 120 | #define JIT_PARAMENUM(len, name, value) JIT_P_##name, |
| 121 | JIT_PARAMDEF(JIT_PARAMENUM) |
| 122 | #undef JIT_PARAMENUM |
| 123 | JIT_P__MAX |
| 124 | }; |
| 125 | |
| 126 | #define JIT_PARAMSTR(len, name, value) #len #name |
| 127 | #define JIT_P_STRING JIT_PARAMDEF(JIT_PARAMSTR) |
| 128 | |
| 129 | |
| 130 | typedef enum { |
| 131 | LJ_TRACE_IDLE, |
| 132 | LJ_TRACE_ACTIVE = 0x10, |
| 133 | LJ_TRACE_RECORD, |
| 134 | LJ_TRACE_START, |
| 135 | LJ_TRACE_END, |
| 136 | LJ_TRACE_ASM, |
| 137 | LJ_TRACE_ERR |
| 138 | } TraceState; |
| 139 | |
| 140 | |
| 141 | typedef enum { |
| 142 | LJ_POST_NONE, |
| 143 | LJ_POST_FIXCOMP, |
| 144 | LJ_POST_FIXGUARD, |
| 145 | LJ_POST_FIXGUARDSNAP, |
| 146 | LJ_POST_FIXBOOL, |
| 147 | LJ_POST_FIXCONST, |
| 148 | LJ_POST_FFRETRY |
| 149 | } PostProc; |
| 150 | |
| 151 | |
| 152 | #if LJ_TARGET_X86ORX64 |
| 153 | typedef uint8_t MCode; |
| 154 | #else |
| 155 | typedef uint32_t MCode; |
| 156 | #endif |
| 157 | |
| 158 | |
| 159 | typedef struct MCLink { |
| 160 | MCode *next; |
| 161 | size_t size; |
| 162 | } MCLink; |
| 163 | |
| 164 | |
| 165 | typedef struct SnapShot { |
| 166 | uint32_t mapofs; |
| 167 | IRRef1 ref; |
| 168 | uint8_t nslots; |
| 169 | uint8_t topslot; |
| 170 | uint8_t nent; |
| 171 | uint8_t count; |
| 172 | } SnapShot; |
| 173 | |
| 174 | #define SNAPCOUNT_DONE 255 |
| 175 | |
| 176 | |
| 177 | typedef uint32_t SnapEntry; |
| 178 | |
| 179 | #define SNAP_FRAME 0x010000 |
| 180 | #define SNAP_CONT 0x020000 |
| 181 | #define SNAP_NORESTORE 0x040000 |
| 182 | #define SNAP_SOFTFPNUM 0x080000 |
| 183 | LJ_STATIC_ASSERT(SNAP_FRAME == TREF_FRAME); |
| 184 | LJ_STATIC_ASSERT(SNAP_CONT == TREF_CONT); |
| 185 | |
| 186 | #define SNAP(slot, flags, ref) (((SnapEntry)(slot) << 24) + (flags) + (ref)) |
| 187 | #define SNAP_TR(slot, tr) \ |
| 188 | (((SnapEntry)(slot) << 24) + ((tr) & (TREF_CONT|TREF_FRAME|TREF_REFMASK))) |
| 189 | #define SNAP_MKPC(pc) ((SnapEntry)u32ptr(pc)) |
| 190 | #define SNAP_MKFTSZ(ftsz) ((SnapEntry)(ftsz)) |
| 191 | #define snap_ref(sn) ((sn) & 0xffff) |
| 192 | #define snap_slot(sn) ((BCReg)((sn) >> 24)) |
| 193 | #define snap_isframe(sn) ((sn) & SNAP_FRAME) |
| 194 | #define snap_pc(sn) ((const BCIns *)(uintptr_t)(sn)) |
| 195 | #define snap_setref(sn, ref) (((sn) & (0xffff0000&~SNAP_NORESTORE)) | (ref)) |
| 196 | |
| 197 | |
| 198 | typedef uint32_t SnapNo; |
| 199 | typedef uint32_t ExitNo; |
| 200 | |
| 201 | |
| 202 | typedef uint32_t TraceNo; |
| 203 | typedef uint16_t TraceNo1; |
| 204 | |
| 205 | |
| 206 | typedef enum { |
| 207 | LJ_TRLINK_NONE, |
| 208 | LJ_TRLINK_ROOT, |
| 209 | LJ_TRLINK_LOOP, |
| 210 | LJ_TRLINK_TAILREC, |
| 211 | LJ_TRLINK_UPREC, |
| 212 | LJ_TRLINK_DOWNREC, |
| 213 | LJ_TRLINK_INTERP, |
| 214 | LJ_TRLINK_RETURN |
| 215 | } TraceLink; |
| 216 | |
| 217 | |
| 218 | typedef struct GCtrace { |
| 219 | GCHeader; |
| 220 | uint16_t nsnap; |
| 221 | IRRef nins; |
| 222 | GCRef gclist; |
| 223 | IRIns *ir; |
| 224 | IRRef nk; |
| 225 | uint32_t nsnapmap; |
| 226 | SnapShot *snap; |
| 227 | SnapEntry *snapmap; |
| 228 | GCRef startpt; |
| 229 | MRef startpc; |
| 230 | BCIns startins; |
| 231 | MSize szmcode; |
| 232 | MCode *mcode; |
| 233 | MSize mcloop; |
| 234 | uint16_t nchild; |
| 235 | uint16_t spadjust; |
| 236 | TraceNo1 traceno; |
| 237 | TraceNo1 link; |
| 238 | TraceNo1 root; |
| 239 | TraceNo1 nextroot; |
| 240 | TraceNo1 nextside; |
| 241 | uint8_t sinktags; |
| 242 | uint8_t topslot; |
| 243 | uint8_t linktype; |
| 244 | uint8_t unused1; |
| 245 | #ifdef LUAJIT_USE_GDBJIT |
| 246 | void *gdbjit_entry; |
| 247 | #endif |
| 248 | } GCtrace; |
| 249 | |
| 250 | #define gco2trace(o) check_exp((o)->gch.gct == ~LJ_TTRACE, (GCtrace *)(o)) |
| 251 | #define traceref(J, n) \ |
| 252 | check_exp((n)>0 && (MSize)(n)<J->sizetrace, (GCtrace *)gcref(J->trace[(n)])) |
| 253 | |
| 254 | LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCtrace, gclist)); |
| 255 | |
| 256 | static LJ_AINLINE MSize snap_nextofs(GCtrace *T, SnapShot *snap) |
| 257 | { |
| 258 | if (snap+1 == &T->snap[T->nsnap]) |
| 259 | return T->nsnapmap; |
| 260 | else |
| 261 | return (snap+1)->mapofs; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | typedef struct HotPenalty { |
| 266 | MRef pc; |
| 267 | uint16_t val; |
| 268 | uint16_t reason; |
| 269 | } HotPenalty; |
| 270 | |
| 271 | #define PENALTY_SLOTS 64 |
| 272 | #define PENALTY_MIN (36*2) |
| 273 | #define PENALTY_MAX 60000 |
| 274 | #define PENALTY_RNDBITS 4 |
| 275 | |
| 276 | |
| 277 | typedef struct BPropEntry { |
| 278 | IRRef1 key; |
| 279 | IRRef1 val; |
| 280 | IRRef mode; |
| 281 | } BPropEntry; |
| 282 | |
| 283 | |
| 284 | #define BPROP_SLOTS 16 |
| 285 | |
| 286 | |
| 287 | typedef struct ScEvEntry { |
| 288 | MRef pc; |
| 289 | IRRef1 idx; |
| 290 | IRRef1 start; |
| 291 | IRRef1 stop; |
| 292 | IRRef1 step; |
| 293 | IRType1 t; |
| 294 | uint8_t dir; |
| 295 | } ScEvEntry; |
| 296 | |
| 297 | |
| 298 | enum { |
| 299 | LJ_KSIMD_ABS, |
| 300 | LJ_KSIMD_NEG, |
| 301 | LJ_KSIMD__MAX |
| 302 | }; |
| 303 | |
| 304 | |
| 305 | #define LJ_KSIMD(J, n) \ |
| 306 | ((TValue *)(((intptr_t)&J->ksimd[2*(n)] + 15) & ~(intptr_t)15)) |
| 307 | |
| 308 | |
| 309 | #if LJ_SOFTFP || (LJ_32 && LJ_HASFFI) |
| 310 | #define lj_needsplit(J) (J->needsplit = 1) |
| 311 | #define lj_resetsplit(J) (J->needsplit = 0) |
| 312 | #else |
| 313 | #define lj_needsplit(J) UNUSED(J) |
| 314 | #define lj_resetsplit(J) UNUSED(J) |
| 315 | #endif |
| 316 | |
| 317 | |
| 318 | typedef struct FoldState { |
| 319 | IRIns ins; |
| 320 | IRIns left; |
| 321 | IRIns right; |
| 322 | } FoldState; |
| 323 | |
| 324 | |
| 325 | typedef struct jit_State { |
| 326 | GCtrace cur; |
| 327 | |
| 328 | lua_State *L; |
| 329 | const BCIns *pc; |
| 330 | GCfunc *fn; |
| 331 | GCproto *pt; |
| 332 | TRef *base; |
| 333 | |
| 334 | uint32_t flags; |
| 335 | BCReg maxslot; |
| 336 | BCReg baseslot; |
| 337 | |
| 338 | uint8_t mergesnap; |
| 339 | uint8_t needsnap; |
| 340 | IRType1 guardemit; |
| 341 | uint8_t bcskip; |
| 342 | |
| 343 | FoldState fold; |
| 344 | |
| 345 | const BCIns *bc_min; |
| 346 | MSize bc_extent; |
| 347 | |
| 348 | TraceState state; |
| 349 | |
| 350 | int32_t instunroll; |
| 351 | int32_t loopunroll; |
| 352 | int32_t tailcalled; |
| 353 | int32_t framedepth; |
| 354 | int32_t retdepth; |
| 355 | |
| 356 | MRef k64; |
| 357 | TValue ksimd[LJ_KSIMD__MAX*2+1]; |
| 358 | |
| 359 | IRIns *irbuf; |
| 360 | IRRef irtoplim; |
| 361 | IRRef irbotlim; |
| 362 | IRRef loopref; |
| 363 | |
| 364 | MSize sizesnap; |
| 365 | SnapShot *snapbuf; |
| 366 | SnapEntry *snapmapbuf; |
| 367 | MSize sizesnapmap; |
| 368 | |
| 369 | PostProc postproc; |
| 370 | #if LJ_SOFTFP || (LJ_32 && LJ_HASFFI) |
| 371 | int needsplit; |
| 372 | #endif |
| 373 | |
| 374 | GCRef *trace; |
| 375 | TraceNo freetrace; |
| 376 | MSize sizetrace; |
| 377 | |
| 378 | IRRef1 chain[IR__MAX]; |
| 379 | TRef slot[LJ_MAX_JSLOTS+LJ_STACK_EXTRA]; |
| 380 | |
| 381 | int32_t param[JIT_P__MAX]; |
| 382 | |
| 383 | MCode *exitstubgroup[LJ_MAX_EXITSTUBGR]; |
| 384 | |
| 385 | HotPenalty penalty[PENALTY_SLOTS]; |
| 386 | uint32_t penaltyslot; |
| 387 | uint32_t prngstate; |
| 388 | |
| 389 | BPropEntry bpropcache[BPROP_SLOTS]; |
| 390 | uint32_t bpropslot; |
| 391 | |
| 392 | ScEvEntry scev; |
| 393 | |
| 394 | const BCIns *startpc; |
| 395 | TraceNo parent; |
| 396 | ExitNo exitno; |
| 397 | |
| 398 | BCIns *patchpc; |
| 399 | BCIns patchins; |
| 400 | |
| 401 | int mcprot; |
| 402 | MCode *mcarea; |
| 403 | MCode *mctop; |
| 404 | MCode *mcbot; |
| 405 | size_t szmcarea; |
| 406 | size_t szallmcarea; |
| 407 | |
| 408 | TValue errinfo; |
| 409 | } jit_State; |
| 410 | |
| 411 | |
| 412 | static LJ_AINLINE uint32_t LJ_PRNG_BITS(jit_State *J, int bits) |
| 413 | { |
| 414 | |
| 415 | J->prngstate = J->prngstate * 1103515245 + 12345; |
| 416 | return J->prngstate >> (32-bits); |
| 417 | } |
| 418 | |
| 419 | #endif |