From 6523585c66c04cea54df50013df8886b589847d8 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Mon, 23 Jan 2012 23:36:30 +1000 Subject: Add luaproc and LuaJIT libraries. Two versions of LuaJIT, the stable release, and the dev version. Try the dev version first, until ih fails badly. --- libraries/LuaJIT-1.1.7/jit/dis_x86.lua | 622 ++++++++++++++++++++++++++++++ libraries/LuaJIT-1.1.7/jit/dump.lua | 265 +++++++++++++ libraries/LuaJIT-1.1.7/jit/dumphints.lua | 239 ++++++++++++ libraries/LuaJIT-1.1.7/jit/opt.lua | 508 ++++++++++++++++++++++++ libraries/LuaJIT-1.1.7/jit/opt_inline.lua | 397 +++++++++++++++++++ libraries/LuaJIT-1.1.7/jit/trace.lua | 111 ++++++ 6 files changed, 2142 insertions(+) create mode 100644 libraries/LuaJIT-1.1.7/jit/dis_x86.lua create mode 100644 libraries/LuaJIT-1.1.7/jit/dump.lua create mode 100644 libraries/LuaJIT-1.1.7/jit/dumphints.lua create mode 100644 libraries/LuaJIT-1.1.7/jit/opt.lua create mode 100644 libraries/LuaJIT-1.1.7/jit/opt_inline.lua create mode 100644 libraries/LuaJIT-1.1.7/jit/trace.lua (limited to 'libraries/LuaJIT-1.1.7/jit') diff --git a/libraries/LuaJIT-1.1.7/jit/dis_x86.lua b/libraries/LuaJIT-1.1.7/jit/dis_x86.lua new file mode 100644 index 0000000..b9ca150 --- /dev/null +++ b/libraries/LuaJIT-1.1.7/jit/dis_x86.lua @@ -0,0 +1,622 @@ +---------------------------------------------------------------------------- +-- LuaJIT x86 disassembler module. +-- +-- Copyright (C) 2005-2011 Mike Pall. All rights reserved. +-- Released under the MIT/X license. See luajit.h for full copyright notice. +---------------------------------------------------------------------------- +-- This is a helper module used by the LuaJIT machine code dumper module. +-- +-- Sending small code snippets to an external disassembler and mixing the +-- output with our own stuff was too fragile. So I had to bite the bullet +-- and write yet another x86 disassembler. Oh well ... +-- +-- The output format is very similar to what ndisasm generates. But it has +-- been developed independently by looking at the opcode tables from the +-- Intel and AMD manuals. The supported instruction set is quite extensive +-- and reflects what a current generation P4 or K8 implements in 32 bit +-- mode. Yes, this includes MMX, SSE, SSE2, SSE3, SSSE3 and even privileged +-- instructions. +-- +-- Notes: +-- * The (useless) a16 prefix, 3DNow and pre-586 opcodes are unsupported. +-- * No attempt at optimization has been made -- it's fast enough for my needs. +-- * The public API may change when more architectures are added. +-- +-- TODO: +-- * More testing with arbitrary x86 code (not just LuaJIT generated code). +-- * The output for a few MMX/SSE opcodes could be improved. +-- * Adding x64 support would be straightforward. +-- * Better input API (iterator) and output API (structured access to instr). +------------------------------------------------------------------------------ + +local type = type +local sub, byte, format = string.sub, string.byte, string.format +local match, gmatch, gsub = string.match, string.gmatch, string.gsub + +-- Map for 1st opcode byte. Ugly? Well ... read on. +local map_opc1 = { +--0x +[0]="addBmr","addVmr","addBrm","addVrm","addBai","addVai","push es","pop es", +"orBmr","orVmr","orBrm","orVrm","orBai","orVai","push cs","opc2*", +--1x +"adcBmr","adcVmr","adcBrm","adcVrm","adcBai","adcVai","push ss","pop ss", +"sbbBmr","sbbVmr","sbbBrm","sbbVrm","sbbBai","sbbVai","push ds","pop ds", +--2x +"andBmr","andVmr","andBrm","andVrm","andBai","andVai","es:seg","daa", +"subBmr","subVmr","subBrm","subVrm","subBai","subVai","cs:seg","das", +--3x +"xorBmr","xorVmr","xorBrm","xorVrm","xorBai","xorVai","ss:seg","aaa", +"cmpBmr","cmpVmr","cmpBrm","cmpVrm","cmpBai","cmpVai","ds:seg","aas", +--4x +"incVR","incVR","incVR","incVR","incVR","incVR","incVR","incVR", +"decVR","decVR","decVR","decVR","decVR","decVR","decVR","decVR", +--5x +"pushVR","pushVR","pushVR","pushVR","pushVR","pushVR","pushVR","pushVR", +"popVR","popVR","popVR","popVR","popVR","popVR","popVR","popVR", +--6x +"pusha/pushaw","popa/popaw","boundVrm","arplWmr", +"fs:seg","gs:seg","o16:","a16", +"pushVi","imulVrmi","pushBs","imulVrms", +"insb","insd/insw","outsb","outsd/outsw", +--7x +"joBj","jnoBj","jbBj","jnbBj","jzBj","jnzBj","jbeBj","jaBj", +"jsBj","jnsBj","jpeBj","jpoBj","jlBj","jgeBj","jleBj","jgBj", +--8x +"arith!Bmi","arith!Vmi","arith!Bmi","arith!Vms", +"testBmr","testVmr","xchgBrm","xchgVrm", +"movBmr","movVmr","movBrm","movVrm", +"movVmg","leaVrm","movWgm","popVm", +--9x +"nop|pause|xchgWaR|repne nop","xchgVaR","xchgVaR","xchgVaR", +"xchgVaR","xchgVaR","xchgVaR","xchgVaR", +"cwde/cbw","cdq/cwd","call farViw","wait", +"pushf/pushfw","popf/popfw","sahf","lahf", +--Ax +"movBao","movVao","movBoa","movVoa", +"movsb","movsd/movsb","cmpsb","cmpsd/cmpsw", +"testBai","testVai","stosb","stosd/stosw", +"lodsb","lodsd/lodsw","scasb","scasd/scasw", +--Bx +"movBRi","movBRi","movBRi","movBRi","movBRi","movBRi","movBRi","movBRi", +"movVRi","movVRi","movVRi","movVRi","movVRi","movVRi","movVRi","movVRi", +--Cx +"shift!Bmu","shift!Vmu","retBw","ret","lesVrm","ldsVrm","movBmi","movVmi", +"enterBwu","leave","retfBw","retf","int3","intBu","into","iret/iretw", +--Dx +"shift!Bm1","shift!Vm1","shift!Bmc","shift!Vmc","aamBu","aadBu","salc","xlatb", +"fp*0","fp*1","fp*2","fp*3","fp*4","fp*5","fp*6","fp*7", +--Ex +"loopneBj","loopeBj","loopBj","jecxz/jcxzBj","inBau","inVau","outBua","outVua", +"callDj","jmpDj","jmp farViw","jmpBj","inBad","inVad","outBda","outVda", +--Fx +"lock:","int1","repne:rep","rep:","hlt","cmc","testb!Bm","testv!Vm", +"clc","stc","cli","sti","cld","std","inc!Bm","inc!Vm", +} +assert(#map_opc1 == 255) + +-- Map for 2nd opcode byte (0f xx). True CISC hell. Hey, I told you. +-- Prefix dependent MMX/SSE opcodes: (none)|rep|o16|repne +local map_opc2 = { +--0x +[0]="sldt!Dmp","sgdt!Dmp","larVrm","lslVrm",nil,"syscall","clts","sysret", +"invd","wbinvd",nil,"ud1",nil,"prefetch!Bm","femms","3dnowMrmu", +--1x +"movupsXrm|movssXrm|movupdXrm|movsdXrm", +"movupsXmr|movssXmr|movupdXmr|movsdXmr", +"movhlpsXrm|movsldupXrm|movlpdXrm|movddupXrm", -- TODO: movlpsXrMm (mem case). +"movlpsXmr||movlpdXmr", +"unpcklpsXrm||unpcklpdXrm", +"unpckhpsXrm||unpckhpdXrm", +"movlhpsXrm|movshdupXrm|movhpdXrm", -- TODO: movhpsXrMm (mem case). +"movhpsXmr||movhpdXmr", +"prefetcht!Bm","hintnopBm","hintnopBm","hintnopBm", +"hintnopBm","hintnopBm","hintnopBm","hintnopBm", +--2x +"movDmx","movDmy","movDxm","movDym","movDmz",nil,"movDzm",nil, +"movapsXrm||movapdXrm", +"movapsXmr||movapdXmr", +"cvtpi2psXrMm|cvtsi2ssXrDm|cvtpi2pdXrMm|cvtsi2sdXrDm", +"movntpsXmr||movntpdXmr", +"cvttps2piMrXm|cvttss2siDrXm|cvttpd2piMrXm|cvttsd2siDrXm", +"cvtps2piMrXm|cvtss2siDrXm|cvtpd2piMrXm|cvtsd2siDrXm", +"ucomissXrm||ucomisdXrm", +"comissXrm||comisdXrm", +--3x +"wrmsr","rdtsc","rdmsr","rdpmc","sysenter","sysexit",nil,nil, +"ssse3*38",nil,"ssse3*3a",nil,nil,nil,nil,nil, +--4x +"cmovoVrm","cmovnoVrm","cmovbVrm","cmovnbVrm", +"cmovzVrm","cmovnzVrm","cmovbeVrm","cmovaVrm", +"cmovsVrm","cmovnsVrm","cmovpeVrm","cmovpoVrm", +"cmovlVrm","cmovgeVrm","cmovleVrm","cmovgVrm", +--5x +"movmskpsDrXm||movmskpdDrXm","sqrtpsXrm|sqrtssXrm|sqrtpdXrm|sqrtsdXrm", +"rsqrtpsXrm|rsqrtssXrm","rcppsXrm|rcpssXrm", +"andpsXrm||andpdXrm","andnpsXrm||andnpdXrm", +"orpsXrm||orpdXrm","xorpsXrm||xorpdXrm", +"addpsXrm|addssXrm|addpdXrm|addsdXrm","mulpsXrm|mulssXrm|mulpdXrm|mulsdXrm", +"cvtps2pdXrm|cvtss2sdXrm|cvtpd2psXrm|cvtsd2ssXrm", +"cvtdq2psXrm|cvttps2dqXrm|cvtps2dqXrm", +"subpsXrm|subssXrm|subpdXrm|subsdXrm","minpsXrm|minssXrm|minpdXrm|minsdXrm", +"divpsXrm|divssXrm|divpdXrm|divsdXrm","maxpsXrm|maxssXrm|maxpdXrm|maxsdXrm", +--6x +"punpcklbwMrm||punpcklbqXrm","punpcklwdPrm","punpckldqPrm","packsswbPrm", +"pcmpgtbPrm","pcmpgtwPrm","pcmpgtdPrm","packuswbPrm", +"punpckhbwPrm","punpckhwdPrm","punpckhdqPrm","packssdwPrm", +"||punpcklqdqXrm","||punpckhqdqXrm", +"movdPrDm","movqMrm|movdquXrm|movdqaXrm", +--7x +"pshufwPrmu","pshiftw!Pmu","pshiftd!Pmu","pshiftq!Mmu||pshiftdq!Xmu", +"pcmpeqbPrm","pcmpeqwPrm","pcmpeqdPrm","emms|", +nil,nil,nil,nil, +"||haddpdXrm|haddpsXrm","||hsubpdXrm|hsubpsXrm", +"movdDmMr|movqXrm|movdDmXr","movqMmr|movdquXmr|movdqaXmr", +--8x +"joVj","jnoVj","jbVj","jnbVj","jzVj","jnzVj","jbeVj","jaVj", +"jsVj","jnsVj","jpeVj","jpoVj","jlVj","jgeVj","jleVj","jgVj", +--9x +"setoBm","setnoBm","setbBm","setnbBm","setzBm","setnzBm","setbeBm","setaBm", +"setsBm","setnsBm","setpeBm","setpoBm","setlBm","setgeBm","setleBm","setgBm", +--Ax +"push fs","pop fs","cpuid","btVmr","shldVmru","shldVmrc",nil,nil, +"push gs","pop gs","rsm","btsVmr","shrdVmru","shrdVmrc","fxsave!Dmp","imulVrm", +--Bx +"cmpxchgBmr","cmpxchgVmr","lssVrm","btrVmr", +"lfsVrm","lgsVrm","movzxVrBm","movzxDrWm", +nil,"ud2","bt!Vmu","btcVmr", +"bsfVrm","bsrVrm","movsxVrBm","movsxDrWm", +--Cx +"xaddBmr","xaddVmr", +"cmppsXrmu|cmpssXrmu|cmppdXrmu|cmpsdXrmu","movntiDmr|", +"pinsrwPrWmu","pextrwDrPmu", +"shufpsXrmu||shufpdXrmu","cmpxchg!Dmp", +"bswapDR","bswapDR","bswapDR","bswapDR","bswapDR","bswapDR","bswapDR","bswapDR", +--Dx +"||addsubpdXrm|addsubpsXrm","psrlwPrm","psrldPrm","psrlqPrm", +"paddqPrm","pmullwPrm", +"|movq2dqXrMm|movqXmr|movdq2qMrXm","pmovmskbDrPm", +"psubusbPrm","psubuswPrm","pminubPrm","pandPrm", +"paddusbPrm","padduswPrm","pmaxubPrm","pandnPrm", +--Ex +"pavgbPrm","psrawPrm","psradPrm","pavgwPrm", +"pmulhuwPrm","pmulhwPrm", +"|cvtdq2pdXrm|cvttpd2dqXrm|cvtpd2dqXrm","movntqMmr||movntdqXmr", +"psubsbPrm","psubswPrm","pminswPrm","porPrm", +"paddsbPrm","paddswPrm","pmaxswPrm","pxorPrm", +--Fx +"|||lddquXrm","psllwPrm","pslldPrm","psllqPrm", +"pmuludqPrm","pmaddwdPrm","psadbwPrm","maskmovqMrm||maskmovdquXrm", +"psubbPrm","psubwPrm","psubdPrm","psubqPrm", +"paddbPrm","paddwPrm","padddPrm","ud", +} +assert(map_opc2[255] == "ud") + +-- Map for SSSE3 opcodes. +local map_ssse3 = { +["38"] = { -- [66] 0f 38 xx +--0x +[0]="pshufbPrm","phaddwPrm","phadddPrm","phaddswPrm", +"pmaddubswPrm","phsubwPrm","phsubdPrm","phsubswPrm", +"psignbPrm","psignwPrm","psigndPrm","pmulhrswPrm", +nil,nil,nil,nil, +--1x +nil,nil,nil,nil,nil,nil,nil,nil, +nil,nil,nil,nil,"pabsbPrm","pabswPrm","pabsdPrm",nil, +}, +["3a"] = { -- [66] 0f 3a xx +[0x0f] = "palignrPrmu", +}, +} + +-- Map for FP opcodes. And you thought stack machines are simple? +local map_opcfp = { +-- D8-DF 00-BF: opcodes with a memory operand. +-- D8 +[0]="faddFm","fmulFm","fcomFm","fcompFm","fsubFm","fsubrFm","fdivFm","fdivrFm", +"fldFm",nil,"fstFm","fstpFm","fldenvDmp","fldcwWm","fnstenvDmp","fnstcwWm", +-- DA +"fiaddDm","fimulDm","ficomDm","ficompDm", +"fisubDm","fisubrDm","fidivDm","fidivrDm", +-- DB +"fildDm","fisttpDm","fistDm","fistpDm",nil,"fld twordFmp",nil,"fstp twordFmp", +-- DC +"faddGm","fmulGm","fcomGm","fcompGm","fsubGm","fsubrGm","fdivGm","fdivrGm", +-- DD +"fldGm","fisttpQm","fstGm","fstpGm","frstorDmp",nil,"fnsaveDmp","fnstswWm", +-- DE +"fiaddWm","fimulWm","ficomWm","ficompWm", +"fisubWm","fisubrWm","fidivWm","fidivrWm", +-- DF +"fildWm","fisttpWm","fistWm","fistpWm", +"fbld twordFmp","fildQm","fbstp twordFmp","fistpQm", +-- xx C0-FF: opcodes with a pseudo-register operand. +-- D8 +"faddFf","fmulFf","fcomFf","fcompFf","fsubFf","fsubrFf","fdivFf","fdivrFf", +-- D9 +"fldFf","fxchFf",{"fnop"},nil, +{"fchs","fabs",nil,nil,"ftst","fxam"}, +{"fld1","fldl2t","fldl2e","fldpi","fldlg2","fldln2","fldz"}, +{"f2xm1","fyl2x","fptan","fpatan","fxtract","fprem1","fdecstp","fincstp"}, +{"fprem","fyl2xp1","fsqrt","fsincos","frndint","fscale","fsin","fcos"}, +-- DA +"fcmovbFf","fcmoveFf","fcmovbeFf","fcmovuFf",nil,{nil,"fucompp"},nil,nil, +-- DB +"fcmovnbFf","fcmovneFf","fcmovnbeFf","fcmovnuFf", +{nil,nil,"fnclex","fninit"},"fucomiFf","fcomiFf",nil, +-- DC +"fadd toFf","fmul toFf",nil,nil, +"fsub toFf","fsubr toFf","fdivr toFf","fdiv toFf", +-- DD +"ffreeFf",nil,"fstFf","fstpFf","fucomFf","fucompFf",nil,nil, +-- DE +"faddpFf","fmulpFf",nil,{nil,"fcompp"}, +"fsubrpFf","fsubpFf","fdivrpFf","fdivpFf", +-- DF +nil,nil,nil,nil,{"fnstsw ax"},"fucomipFf","fcomipFf",nil, +} +assert(map_opcfp[126] == "fcomipFf") + +-- Map for opcode groups. The subkey is sp from the ModRM byte. +local map_opcgroup = { + arith = { "add", "or", "adc", "sbb", "and", "sub", "xor", "cmp" }, + shift = { "rol", "ror", "rcl", "rcr", "shl", "shr", "sal", "sar" }, + testb = { "testBmi", "testBmi", "not", "neg", "mul", "imul", "div", "idiv" }, + testv = { "testVmi", "testVmi", "not", "neg", "mul", "imul", "div", "idiv" }, + inc = { "inc", "dec", "callDmp", "call farDmp", + "jmpDmp", "jmp farDmp", "push" }, + sldt = { "sldt", "str", "lldt", "ltr", "verr", "verw" }, + sgdt = { "sgdt", "sidt", "lgdt", "lidt", "smsw", nil, "lmsw", "invlpg" }, + bt = { nil, nil, nil, nil, "bt", "bts", "btr", "btc" }, + cmpxchg = { nil, "cmpxchg8b" }, + pshiftw = { nil, nil, "psrlw", nil, "psraw", nil, "psllw" }, + pshiftd = { nil, nil, "psrld", nil, "psrad", nil, "pslld" }, + pshiftq = { nil, nil, "psrlq", nil, nil, nil, "psllq" }, + pshiftdq = { nil, nil, "psrlq", "psrldq", nil, nil, "psllq", "pslldq" }, + fxsave = { "fxsave", "fxrstor", "ldmxcsr", "stmxcsr", + nil, "lfenceDp", "mfenceDp", "sfenceDp" }, -- TODO: clflush. + prefetch = { "prefetch", "prefetchw" }, + prefetcht = { "prefetchnta", "prefetcht0", "prefetcht1", "prefetcht2" }, +} + +------------------------------------------------------------------------------ + +-- Maps for register names. +local map_aregs = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" } +local map_regs = { + B = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" }, + W = { "ax", "cx", "dx", "bx", "sp", "bp", "si", "di" }, + D = map_aregs, + M = { "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7" }, + X = { "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7" }, +} +local map_segregs = { "es", "cs", "ss", "ds", "fs", "gs", "segr6", "segr7" } + +-- Maps for size names. +local map_sz2n = { + B = 1, W = 2, D = 4, Q = 8, M = 8, X = 16, +} +local map_sz2prefix = { + B = "byte", W = "word", D = "dword", + Q = "qword", -- No associated reg in 32 bit mode. + F = "dword", G = "qword", -- No need for sizes/register names for these two. + M = "qword", X = "xword", +} + +------------------------------------------------------------------------------ + +-- Output a nicely formatted line with an opcode and operands. +local function putop(ctx, text, operands) + local code, pos, hex = ctx.code, ctx.pos, "" + for i=ctx.start,pos-1 do + hex = hex..format("%02X", byte(code, i, i)) + end + if #hex > 16 then hex = sub(hex, 1, 16).."." end + if operands then text = text.." "..operands end + if ctx.o16 then text = "o16 "..text; ctx.o16 = false end + if ctx.rep then text = ctx.rep.." "..text; ctx.rep = false end + if ctx.seg then + local text2, n = gsub(text, "%[", "["..ctx.seg..":") + if n == 0 then text = ctx.seg.." "..text else text = text2 end + ctx.seg = false + end + if ctx.lock then text = "lock "..text; ctx.lock = false end + local imm = ctx.imm + if imm then + local sym = ctx.symtab[imm] + if sym then text = text.."\t->"..sym end + end + ctx.out(format("%08x %-18s%s\n", ctx.addr+ctx.start, hex, text)) + ctx.mrm = false + ctx.start = pos + ctx.imm = nil +end + +-- Fallback for incomplete opcodes at the end. +local function incomplete(ctx) + ctx.pos = ctx.stop+1 + ctx.o16 = false; ctx.seg = false; ctx.lock = false; ctx.rep = false + return putop(ctx, "(incomplete)") +end + +-- Fallback for unknown opcodes. +local function unknown(ctx) + ctx.o16 = false; ctx.seg = false; ctx.lock = false; ctx.rep = false + return putop(ctx, "(unknown)") +end + +-- Return an immediate of the specified size. +local function getimm(ctx, pos, n) + if pos+n-1 > ctx.stop then return incomplete(ctx) end + local code = ctx.code + if n == 1 then + local b1 = byte(code, pos, pos) + return b1 + elseif n == 2 then + local b1, b2 = byte(code, pos, pos+1) + return b1+b2*256 + else + local b1, b2, b3, b4 = byte(code, pos, pos+3) + local imm = b1+b2*256+b3*65536+b4*16777216 + ctx.imm = imm + return imm + end +end + +-- Process pattern string and generate the operands. +local function putpat(ctx, name, pat) + local operands, regs, sz, mode, sp, rm, sc, rx, disp, sdisp + local code, pos, stop = ctx.code, ctx.pos, ctx.stop + + -- Chars used: 1DFGMPQRVWXacdfgijmoprsuwxyz + for p in gmatch(pat, ".") do + local x = nil + if p == "V" then + sz = ctx.o16 and "W" or "D"; ctx.o16 = false + regs = map_regs[sz] + elseif match(p, "[BWDQFGMX]") then + sz = p + regs = map_regs[sz] + elseif p == "P" then + sz = ctx.o16 and "X" or "M"; ctx.o16 = false + regs = map_regs[sz] + elseif p == "s" then + local imm = getimm(ctx, pos, 1); if not imm then return end + x = imm <= 127 and format("byte +0x%02x", imm) + or format("byte -0x%02x", 256-imm) + pos = pos+1 + elseif p == "u" then + local imm = getimm(ctx, pos, 1); if not imm then return end + x = format("0x%02x", imm) + pos = pos+1 + elseif p == "w" then + local imm = getimm(ctx, pos, 2); if not imm then return end + x = format("0x%x", imm) + pos = pos+2 + elseif p == "o" then -- [offset] + local imm = getimm(ctx, pos, 4); if not imm then return end + x = format("[0x%08x]", imm) + pos = pos+4 + elseif p == "i" then + local n = map_sz2n[sz] + local imm = getimm(ctx, pos, n); if not imm then return end + x = format(imm > 65535 and "0x%08x" or "0x%x", imm) + pos = pos+n + elseif p == "j" then + local n = map_sz2n[sz] + local imm = getimm(ctx, pos, n); if not imm then return end + if sz == "B" and imm > 127 then imm = imm-256 + elseif imm > 2147483647 then imm = imm-4294967296 end + pos = pos+n + imm = imm + pos + ctx.addr + ctx.imm = imm + x = sz == "W" and format("word 0x%04x", imm%65536) + or format("0x%08x", imm) + elseif p == "R" then x = regs[byte(code, pos-1, pos-1)%8+1] + elseif p == "a" then x = regs[1] + elseif p == "c" then x = "cl" + elseif p == "d" then x = "dx" + elseif p == "1" then x = "1" + else + if not mode then + mode = ctx.mrm + if not mode then + if pos > stop then return incomplete(ctx) end + mode = byte(code, pos, pos) + pos = pos+1 + end + rm = mode%8; mode = (mode-rm)/8 + sp = mode%8; mode = (mode-sp)/8 + sdisp = "" + if mode < 3 then + if rm == 4 then + if pos > stop then return incomplete(ctx) end + sc = byte(code, pos, pos) + pos = pos+1 + rm = sc%8; sc = (sc-rm)/8 + rx = sc%8; sc = (sc-rx)/8 + if rx == 4 then rx = nil end + end + if mode > 0 or rm == 5 then + local dsz = mode + if dsz ~= 1 then dsz = 4 end + disp = getimm(ctx, pos, dsz); if not disp then return end + sdisp = (dsz == 4 or disp <= 127) and + format(disp > 65535 and "+0x%08x" or "+0x%x", disp) or + format("-0x%x", 256-disp) + pos = pos+dsz + end + end + end + if p == "m" then + if mode == 3 then x = regs[rm+1] + else + local srm, srx = map_aregs[rm+1], "" + if rx then + srm = srm.."+" + srx = map_aregs[rx+1] + if sc > 0 then srx = srx.."*"..(2^sc) end + end + if mode == 0 and rm == 5 then + srm = "" + sdisp = format("%s0x%08x", rx and "+" or "", disp) + end + x = format("[%s%s%s]", srm, srx, sdisp) + end + if mode < 3 and + (not match(pat, "[aRrgp]") or + name == "movzx" or name == "movsx") then -- Yuck. + x = map_sz2prefix[sz].." "..x + end + elseif p == "r" then x = regs[sp+1] + elseif p == "g" then x = map_segregs[sp+1] + elseif p == "p" then -- Suppress prefix. + elseif p == "f" then x = "st"..rm + elseif p == "x" then x = "CR"..sp + elseif p == "y" then x = "DR"..sp + elseif p == "z" then x = "TR"..sp + else + error("bad pattern `"..pat.."'") + end + end + if x then operands = operands and operands..","..x or x end + end + ctx.pos = pos + return putop(ctx, name, operands) +end + +-- Forward declaration. +local map_act + +-- Get a pattern from an opcode map and dispatch to handler. +local function opcdispatch(ctx, opcmap) + local pos = ctx.pos + local opat = opcmap[byte(ctx.code, pos, pos)] + if not opat then return unknown(ctx) end + if match(opat, "%|") then -- MMX/SSE variants depending on prefix. + local p + if ctx.rep then p = ctx.rep=="rep" and "%|([^%|]*)" or "%|.-%|.-%|([^%|]*)" + elseif ctx.o16 then p = "%|.-%|([^%|]*)" + else p = "^[^%|]*" end + opat = match(opat, p) + if not opat or opat == "" then return unknown(ctx) end + ctx.rep = false; ctx.o16 = false + end + local name, pat, act = match(opat, "^([a-z0-9 ]*)((.?).*)") + ctx.pos = pos + 1 + return map_act[act](ctx, name, pat) +end + +-- Map for action codes. The key is the first char after the name. +map_act = { + -- Simple opcodes without operands. + [""] = function(ctx, name, pat) + return putop(ctx, name) + end, + + -- Operand size chars fall right through. + B = putpat, W = putpat, D = putpat, V = putpat, + F = putpat, G = putpat, + M = putpat, X = putpat, P = putpat, + + -- Collect prefixes. + [":"] = function(ctx, name, pat) + ctx[pat == ":" and name or sub(pat, 2)] = name + end, + + -- Select alternate opcode name when prefixed with o16. + ["/"] = function(ctx, name, pat) + local wname, rpat = match(pat, "^/([a-z0-9 ]+)(.*)") + if ctx.o16 then name = wname; ctx.o16 = false end + return putpat(ctx, name, rpat) + end, + + -- Chain to special handler specified by name. + ["*"] = function(ctx, name, pat) + return map_act[name](ctx, name, sub(pat, 2)) + end, + + -- Use named subtable for opcode group. + ["!"] = function(ctx, name, pat) + + local pos = ctx.pos + if pos > ctx.stop then return incomplete(ctx) end + local mrm = byte(ctx.code, pos, pos) + ctx.pos = pos+1 + ctx.mrm = mrm + + local opat = map_opcgroup[name][((mrm-(mrm%8))/8)%8+1] + if not opat then return unknown(ctx) end + local name, pat2 = match(opat, "^([a-z0-9 ]*)(.*)") + return putpat(ctx, name, pat2 ~= "" and pat2 or sub(pat, 2)) + end, + + -- Two-byte opcode dispatch. + opc2 = function(ctx, name, pat) + return opcdispatch(ctx, map_opc2) + end, + + -- SSSE3 dispatch. + ssse3 = function(ctx, name, pat) + return opcdispatch(ctx, map_ssse3[pat]) + end, + + -- Floating point opcode dispatch. + fp = function(ctx, name, pat) + + local pos = ctx.pos + if pos > ctx.stop then return incomplete(ctx) end + local mrm = byte(ctx.code, pos, pos) + ctx.pos = pos+1 + ctx.mrm = mrm + + local rm = mrm%8 + local idx = pat*8 + ((mrm-rm)/8)%8 + if mrm >= 192 then idx = idx + 64 end + local opat = map_opcfp[idx] + if type(opat) == "table" then opat = opat[rm+1] end + if not opat then return unknown(ctx) end + local name, pat2 = match(opat, "^([a-z0-9 ]*)(.*)") + return putpat(ctx, name, pat2) + end, +} + +------------------------------------------------------------------------------ + +-- Disassemble a block of code. +local function disass_block(ctx, ofs, len) + if not ofs then ofs = 0 end + local stop = len and ofs+len or #ctx.code + ofs = ofs + 1 + ctx.start = ofs + ctx.pos = ofs + ctx.stop = stop + ctx.imm = nil + ctx.mrm = false + ctx.o16 = false; ctx.seg = false; ctx.lock = false; ctx.rep = false + while ctx.pos <= stop do opcdispatch(ctx, map_opc1) end + if ctx.pos ~= ctx.start then incomplete(ctx) end +end + +-- Extended API: create a disassembler context. Then call ctx:disass(ofs, len). +local function create_(code, addr, out) + local ctx = {} + ctx.code = code + ctx.addr = (addr or 0) - 1 + ctx.out = out or io.write + ctx.symtab = {} + ctx.disass = disass_block + return ctx +end + +-- Simple API: disassemble code (a string) at address and output via out. +local function disass_(code, addr, out) + create_(code, addr, out):disass() +end + + +-- Public module functions. +module(...) + +create = create_ +disass = disass_ + diff --git a/libraries/LuaJIT-1.1.7/jit/dump.lua b/libraries/LuaJIT-1.1.7/jit/dump.lua new file mode 100644 index 0000000..2287c23 --- /dev/null +++ b/libraries/LuaJIT-1.1.7/jit/dump.lua @@ -0,0 +1,265 @@ +---------------------------------------------------------------------------- +-- LuaJIT machine code dumper module. +-- +-- Copyright (C) 2005-2011 Mike Pall. All rights reserved. +-- Released under the MIT/X license. See luajit.h for full copyright notice. +---------------------------------------------------------------------------- +-- Activate this module to dump the machine code for all functions +-- immediately after they have been compiled. The disassembler +-- output is mixed with the bytecode listing. +-- +-- Try: luajit -j dump -e 'print "foo"' +-- luajit -j dump=foo.dump foo.lua +-- luajit -j off -j dump -e 'jit.compile(assert(loadfile"foo.lua")))' +-- +-- Default output is to stderr. To redirect output to a file, +-- pass a filename as an argument or set the environment variable +-- "LUAJIT_DUMPFILE". +-- Note: The file is overwritten each time you run luajit. +-- +-- TODO: Find a way to be more selective on what to dump. +------------------------------------------------------------------------------ + +-- Priority for compiler pipeline. Must run after backend (negative) +-- and should be even because we only catch successful compiles. +local PRIORITY = -98 + +-- Cache some library functions and objects. +local jit = require("jit") +assert(jit.version_num == 10107, "LuaJIT core/library version mismatch") +local jutil = require("jit.util") +local type, format, gsub = type, string.format, string.gsub +local bytecode, const = jutil.bytecode, jutil.const +local getinfo = debug.getinfo +local stdout, stderr = io.stdout, io.stderr + +-- Load the right disassembler. +local dis = require("jit.dis_"..jit.arch) +local discreate, disass_ = dis.create, dis.disass + +-- Turn compilation off for the whole module. LuaJIT would do that anyway. +jit.off(true, true) + +-- Separator line. +local sepline = "-------------------------------" + +-- Map JSUB indices to names. +-- CHECK: must match the order in ljit_x86.h. Regenerate with: +-- grep '^ *JSUB_[^_].*,' ljit_x86.h | sed -e 's/^ *JSUB_/ "/' -e 's/,.*/",/' +local jsubnames = { + "STACKPTR", + "GATE_LJ", + "GATE_JL", + "GATE_JC", + "GROW_STACK", + "GROW_CI", + "GATE_JC_PATCH", + "GATE_JC_DEBUG", + "DEOPTIMIZE_CALLER", + "DEOPTIMIZE", + "DEOPTIMIZE_OPEN", + "HOOKINS", + "GCSTEP", + "STRING_SUB3", + "STRING_SUB2", + "HOOKCALL", + "HOOKRET", + "METACALL", + "METATAILCALL", + "BARRIERF", + "GETGLOBAL", + "GETTABLE_KSTR", + "GETTABLE_STR", + "BARRIERBACK", + "SETGLOBAL", + "SETTABLE_KSTR", + "SETTABLE_STR", + "GETTABLE_KNUM", + "GETTABLE_NUM", + "SETTABLE_KNUM", + "SETTABLE_NUM", + "LOG2_TWORD", + "CONCAT_STR2", +} + +-- Generate map from JSUB addresses to JSUB names. +local jsubmap = {} +do + local jsubmcode = jutil.jsubmcode + for pc=0,100000 do + local addr = jsubmcode(pc) + if not addr then break end + jsubmap[addr] = jsubnames[pc+1] or "JSUB#"..pc + end +end + +-- Pretty-print a constant. +local function conststr(func, idx) + local k = const(func, idx) + if k == nil then return "nil" + elseif k == true then return "true" + elseif k == false then return "false" + elseif type(k) == "string" then + if #k > 10 then return format('"%.10s"~', k) + else return '"'..k..'"' end + else return k.."" end +end + +-- Pretty-print a bytecode instruction (one or two lines). +local function bytecodeout(out, func, pc) + local op, a, b, c = bytecode(func, pc) + if not op then + return true + elseif op == "JMP" then + out:write(format("\n--%04d-- JMP => %04d", pc, pc+1+b)) + elseif op == "FORLOOP" or op == "FORPREP" then + out:write(format("\n--%04d-- %-9s %3d => %04d", pc, op, a, pc+1+b)) + else + out:write(format("\n--%04d-- %-9s %3d %4s %4s", + pc, op, a, b or "", c or "")) + if b and b < 0 then out:write(" ; ", conststr(func, b)) end + if c and c < 0 then out:write(" ; ", conststr(func, c)) end + end +end + +-- Dump machine code and mix it with the bytecode listing. +local function dumpfunc(func, out, deopt) + if not out then out = stderr end + local info = getinfo(func, "S") + + -- Don't bother checking for the right blocks to dump. + -- Dump the main block (if not deopt) and always all deopt blocks. + for block=deopt and 2 or 1,1000000 do + local addr, code, mfmiter = jutil.mcode(func, block) + if not addr then + if code then return code end -- Not compiled: return status. + break -- No more blocks to dump. + end + + -- Print header. + out:write(sepline, " ", info.source, ":", info.linedefined) + if block ~= 1 then out:write(" DEOPT block ", block) end + + -- Create disassembler context. + local ctx = discreate(code, addr, function(s) out:write(s) end) + ctx.symtab = jsubmap + + -- Dump an mcode block. + local pc, ofs = 1, 0 + local len, isdeopt = mfmiter() + if isdeopt then pc = len; len = 0 + elseif block ~= 1 then break end -- Stop before next main block. + for t, m in mfmiter do + if t == "COMBINE" then + bytecodeout(out, func, pc) + else + if len ~= 0 then + out:write("\n") + if len > 0 then + ctx:disass(ofs, len) + ofs = ofs + len + else + out:write(format("%08x ** deoptimized\n", addr+ofs)) + ofs = ofs - len + end + len = 0 + end + if type(t) == "number" then + if m then + if isdeopt then + pc = t - 1 + else + bytecodeout(out, func, pc) + len = -t + end + else + len = t + if bytecodeout(out, func, pc) then break end + end + end + end + pc = pc + 1 + end + if len and len ~= 0 then + out:write(sepline, " tail code\n") + ctx:disass(ofs, len) + end + end + + -- Print footer. + out:write(sepline, "\n") + out:flush() +end + +-- Dump the internal JIT subroutines. +local function dumpjsub_(out) + if not out then out = stderr end + local addr, code = jutil.jsubmcode() + + -- Create disassembler context. + local ctx = discreate(code, addr, function(s) out:write(s) end) + ctx.symtab = jsubmap + + -- Collect addresses and sort them. + local t = {} + for addr in pairs(jsubmap) do t[#t+1] = addr end + t[#t+1] = addr + #code + table.sort(t) + + -- Go through the addresses in ascending order. + local ofs = addr + for i=2,#t do + local next = t[i] + out:write("\n->", jsubmap[ofs], ":\n") -- Output label for JSUB. + ctx:disass(ofs-addr, next-ofs) -- Disassemble corresponding code block. + ofs = next + end + out:flush() +end + + +-- Active flag and output file handle. +local active, out + +-- Dump handler for compiler pipeline. +local function h_dump(st) + local ok, err = pcall(dumpfunc, st.func, out, st.deopt) + if not ok then + stderr:write("\nERROR: jit.dump disabled: ", err, "\n") + jit.attach(h_dump) -- Better turn ourselves off after a failure. + if out and out ~= stdout then out:close() end + out = nil + active = nil + end +end + +-- Detach dump handler from compiler pipeline. +local function dumpoff() + if active then + active = false + jit.attach(h_dump) + if out and out ~= stdout then out:close() end + out = nil + end +end + +-- Open the output file and attach dump handler to compiler pipeline. +local function dumpon(filename) + if active then dumpoff() end + local outfile = filename or os.getenv("LUAJIT_DUMPFILE") + out = outfile and (outfile == "-" and stdout or assert(io.open(outfile, "w"))) + jit.attach(h_dump, PRIORITY) + active = true +end + + +-- Public module functions. +module(...) + +disass = disass_ +dump = dumpfunc +dumpjsub = dumpjsub_ +on = dumpon +off = dumpoff +start = dumpon -- For -j command line option. + diff --git a/libraries/LuaJIT-1.1.7/jit/dumphints.lua b/libraries/LuaJIT-1.1.7/jit/dumphints.lua new file mode 100644 index 0000000..4a64676 --- /dev/null +++ b/libraries/LuaJIT-1.1.7/jit/dumphints.lua @@ -0,0 +1,239 @@ +---------------------------------------------------------------------------- +-- LuaJIT hints dumper module. +-- +-- Copyright (C) 2005-2011 Mike Pall. All rights reserved. +-- Released under the MIT/X license. See luajit.h for full copyright notice. +---------------------------------------------------------------------------- +-- Activate this module to dump the bytecode and the hints from +-- the optimizer for all functions to be compiled. +-- +-- Try: luajit -O -j dumphints -e 'return 1' +-- +-- Default output is to stderr. To redirect output to a file, +-- pass a filename as an argument or set the environment variable +-- "LUAJIT_DUMPHINTSFILE". +-- Note: The file is overwritten each time you run luajit. +-- +-- TODO: Find a way to be more selective on what to dump. +------------------------------------------------------------------------------ + +-- Priority for compiler pipeline. Should run before backend (positive) +-- and should be even because we only catch successful compiles. +local PRIORITY = 10 + +-- Cache some library functions and objects. +local jit = require("jit") +assert(jit.version_num == 10107, "LuaJIT core/library version mismatch") +local jutil = require("jit.util") +local type, pairs, format = type, pairs, string.format +local bytecode, const = jutil.bytecode, jutil.const +local hints, fhints = jutil.hints, jutil.fhints +local stdout, stderr = io.stdout, io.stderr + +-- Turn compilation off for the whole module. LuaJIT would do that anyway. +jit.off(true, true) + +-- Separator line. +local sepline = "-------------------------------" + + +-- Pretty-print a constant. +local function conststr(func, idx) + local k = const(func, idx) + if k == nil then return "nil" + elseif k == true then return "true" + elseif k == false then return "false" + elseif type(k) == "string" then + if #k > 10 then return format('"%.10s"~', k) + else return '"'..k..'"' end + else return k.."" end +end + +-- Pretty-print a bytecode instruction. +local function bytecodeline(func, pc, flag) + local op, a, b, c = bytecode(func, pc) + if not op then return end + if op == "JMP" then + return format("\n%04d %s JMP => %04d", pc, flag, pc+1+b) + end + if op == "FORLOOP" or op == "FORPREP" then + return format("\n%04d %s %-9s %3d => %04d", pc, flag, op, a, pc+1+b) + end + local s = format("\n%04d %s %-9s %3d %4s %4s", + pc, flag, op, a, b or "", c or "") + if b and b < 0 then s = s.." ; "..conststr(func, b) end + if c and c < 0 then s = s.." ; "..conststr(func, c) end + return s +end + +-- Precompute inverse hints table. +local invhints = {} +for k,v in pairs(hints) do invhints[v] = k end + +-- The inverse resolver for inline functions is loaded on demand. +local getname + +-- Helper functions to pretty-print hints. +local function typehint(h, v, st, pc) + if st[pc+hints.INLINE] then return "" end + local tp = type(v) + if tp == "function" then + tp = debug.getinfo(v, "S").what + elseif tp == "number" and v % 1 == 0 then + tp = "integer" + elseif v == false then + tp = "mixed" + end + return " #"..h.."("..tp..")" +end + +local hintprint = { + COMBINE = function(h, v, st, pc) + if v == false then return "" end -- Dead instructions are already marked. + end, + TYPE = typehint, + TYPEKEY = typehint, + INLINE = function(h, v, st, pc) + if not getname then getname = require("jit.opt_inline").getname end + return " #INLINE("..getname(st[pc+hints.TYPE], v)..")" + end, +} + +-- Generate range string from table: pc[-pc] [,...] +local function rangestring(t) + local s = "" + for i,range in ipairs(t) do + if i ~= 1 then s = s.."," end + local pc = range % 65536 + range = (range - pc) / 65536 + s = s..pc + if range ~= 0 then s = s..(-(pc+range)) end + end + return s +end + +-- Dump instructions and hints for a (to be compiled) function. +local function dumphints(st, out) + if not out then out = stderr end + local func = st.func + local COMBINE = hints.COMBINE + + -- Need to recompute branch targets (not part of hints). + local target = {} + for pc=1,1e6 do + local op, a, b, c = bytecode(func, pc) + if not op then break end + if op == "JMP" or op == "FORLOOP" then + local t = pc+1+b + if st[pc+COMBINE] ~= false then target[t] = true end + elseif op == "LOADBOOL" and c ~= 0 then + target[pc+2] = true + end + end + + -- Map hints to bytecode instructions. + local hintstr = {} + for k,v in pairs(st) do + -- CHECK: must match hint shift in ljit_hints.h:JIT_H2NUM(). + if type(k) == "number" and k >= 65536 then + local pc = k % 65536 + if pc > 0 then + k = k - pc + local h = invhints[k] or (k/65536) + local hp = hintprint[h] + local s = hp and hp(h, v, st, pc) or (" #"..h) + local hs = hintstr[pc] + hintstr[pc] = hs and (hs..s) or s + end + end + end + + -- Write header. + local info = debug.getinfo(func, "S") + out:write(sepline, " ", info.source, ":", info.linedefined) + + -- Write function hints. + for k,v in pairs(fhints) do + if st[v] then out:write("\n#", k) end + end + + -- Write instruction hints and bytecode. + local function dumprange(firstpc, lastpc) + for pc=firstpc,lastpc do + local prefix = " " + if st[pc+COMBINE] == false then prefix = "**" + elseif target[pc] then prefix = "=>" end + local line = bytecodeline(func, pc, prefix) + if not line then break end + local h = hintstr[pc] + if h then + out:write(format("%-40s %s", line, h)) + else + out:write(line) + end + end + end + + -- Handle deoptimization range table. + local t = st.deopt + if t then + out:write(" DEOPT=", rangestring(t)) + for i,range in ipairs(t) do + if i ~= 1 then out:write("\n----") end + local pc = range % 65536 + range = (range - pc) / 65536 + dumprange(pc, pc+range) + end + else + dumprange(1, 1000000) + end + + -- Write footer. + out:write("\n", sepline, "\n") + out:flush() +end + + +-- Active flag and output file handle. +local active, out + +-- Dump hints handler for compiler pipeline. +local function h_dumphints(st) + local ok, err = pcall(dumphints, st, out) + if not ok then + stderr:write("\nERROR: jit.dumphints disabled: ", err, "\n") + jit.attach(h_dumphints) -- Better turn ourselves off after a failure. + if out and out ~= stdout then out:close() end + out = nil + active = nil + end +end + +-- Detach dump handler from compiler pipeline. +local function dumphintsoff() + if active then + active = false + jit.attach(h_dumphints) + if out and out ~= stdout then out:close() end + out = nil + end +end + +-- Open the output file and attach dump handler to compiler pipeline. +local function dumphintson(filename) + if active then dumphintsoff() end + local outfile = filename or os.getenv("LUAJIT_DUMPHINTSFILE") + out = outfile and (outfile == "-" and stdout or assert(io.open(outfile, "w"))) + jit.attach(h_dumphints, PRIORITY) + active = true +end + + +-- Public module functions. +module(...) + +dump = dumphints +on = dumphintson +off = dumphintsoff +start = dumphintson -- For -j command line option. + diff --git a/libraries/LuaJIT-1.1.7/jit/opt.lua b/libraries/LuaJIT-1.1.7/jit/opt.lua new file mode 100644 index 0000000..5fe0f34 --- /dev/null +++ b/libraries/LuaJIT-1.1.7/jit/opt.lua @@ -0,0 +1,508 @@ +---------------------------------------------------------------------------- +-- LuaJIT optimizer. +-- +-- Copyright (C) 2005-2011 Mike Pall. All rights reserved. +-- Released under the MIT/X license. See luajit.h for full copyright notice. +---------------------------------------------------------------------------- +-- This module contains a simple optimizer that generates some hints for +-- the compiler backend. +-- +-- Compare: luajit -j dump -e 'return 1' +-- with: luajit -O -j dumphints -j dump -e 'return 1' +-- +-- This module uses a very simplistic (but fast) abstract interpretation +-- algorithm. It mostly ignores control flow and/or basic block boundaries. +-- Thus the results of the analysis are really only predictions (e.g. about +-- monomorphic use of operators). The backend _must_ check all contracts +-- (e.g. verify the object type) and use a (polymorphic) fallback or +-- deoptimization in case a contract is broken. +-- +-- Although simplistic, the generated hints are pretty accurate. Note that +-- some hints are really definitive and don't need to be checked (like +-- COMBINE or FOR_STEP_K). +-- +-- TODO: Try MFP with an extended lattice. But it's unclear whether the +-- added complexity really pays off with the current backend. +------------------------------------------------------------------------------ + +-- Priority for compiler pipeline. Right in the middle before the backend. +local PRIORITY = 50 + +-- Default optimizer level, i.e. what you get with -O. +-- Caveat: this may change in the future when more optimizations are added. +local OPTLEVEL = 2 + +-- Heuristic limits for what the compiler should reasonably handle. +-- Functions outside these limits are unlikely to be run more than once. +-- Maybe a bit on the generous side. Check ljit.h for backend limits, too. +-- TODO: make it depend on the bytecode distribution, too. +local LIMITS = { + bytecodes = 4000, + stackslots = 150, + params = 20, + consts = 200, + subs = 30, +} + +-- Cache some library functions and objects. +local jit = require("jit") +assert(jit.version_num == 10107, "LuaJIT core/library version mismatch") +local jutil = require("jit.util") +local type, rawget, next, pcall = type, rawget, next, pcall +local bytecode, const = jutil.bytecode, jutil.const +local hints, fhints = jutil.hints, jutil.fhints +local getmetatable = getmetatable + +-- Turn compilation off for the whole module. LuaJIT would do that anyway. +jit.off(true, true) + +-- Default optimizer level after loading. But -O runs setlevel(), too. +local optlevel = -1 + + +-- Use iterative path marking to mark live instructions and branch targets. +local function marklive(func) + local live, work, workn, pc = {}, {}, 0, 1 + repeat + repeat + local op, a, b, c, test = bytecode(func, pc) + live[pc] = true + pc = pc + 1 + if op == "JMP" then + pc = pc + b + live[-pc] = true + elseif op == "FORLOOP" then + local mpc = -pc + live[mpc - b] = true + live[mpc] = true + elseif op == "RETURN" then + break + elseif test then + local fpc = pc + 1 + -- No need for fallthrough target mark live[-fpc] in our analysis. + if not live[fpc] then -- Add fallthrough path to work list. + workn = workn + 1 + work[workn] = fpc + end + elseif op == "CLOSURE" then + pc = pc + jutil.closurenup(func, b) -- Do not mark pseudo-ins live. + elseif op == "LOADBOOL" and c ~= 0 then + pc = pc + 1 + live[-pc] = true + elseif op == "SETLIST" and c == 0 then + pc = pc + 1 -- Do not mark pseudo-ins live. + end + until live[pc] + if workn == 0 then return live end -- Return if work list is empty. + pc = work[workn] -- Else fetch next path to mark from work list. + workn = workn - 1 + until false +end + + +-- Empty objects. +local function empty() end + +-- Dummy function to set call hints. Replaced when jit.opt_inline is loaded. +local function callhint(st, slot, pc, base, narg, nres) + st[pc+hints.TYPE] = slot[base] + for i=base,base+nres-1 do slot[i] = nil end +end + +-- Set TYPE hint, but only for numbers, strings or tables. +local function typehint(st, pc, o) + local tp = type(o) + if tp == "number" or tp == "string" or tp == "table" then + st[pc+hints.TYPE] = o + end +end + +-- Set TYPE and TYPEKEY hints for table operations. +local function tablehint(st, slot, pc, t, kslot) + local tp = type(t) + if tp == "table" or tp == "userdata" then st[pc+hints.TYPE] = t end + if kslot >= 0 then -- Don't need this hint for constants. + local key = slot[kslot] + local tp = type(key) + if tp == "number" or tp == "string" then st[pc+hints.TYPEKEY] = key end + end +end + +-- Try to lookup a value. Guess the value or at least the value type. +local function trylookup(st, t, k) + if k == nil then return nil end + if type(t) == "table" then + local v = rawget(t, k) + if v ~= nil then return v end + end + local mt = getmetatable(t) + if type(mt) == "table" then + -- One __index level is enough for our purposes. + local it = rawget(mt, "__index") + if type(it) == "table" then + local v = rawget(it, k) + if v ~= nil then return v end + end + end + local v = st.tableval[t] -- Resort to a generic guess. + if v == nil and type(t) == "table" then v = next(t) end -- Getting desperate. + return v +end + +-- Check whether the previous instruction sets a const. +local function prevconst(st, slot, pc, reg) + if st.live[-pc] == nil then -- Current instruction must not be a target. + local op, ka, kb = bytecode(st.func, pc-1) + if ka == reg and (op == "LOADK" or op == "LOADBOOL" or + (op == "LOADNIL" and kb == reg)) then + return true, slot[reg] + end + end +end + +-- Common handler for arithmetic and comparison opcodes. +local function arithop(st, slot, pc, a, b, c, op) + local sb, sc = slot[b], slot[c] + if sb == nil then sb = sc elseif sc == nil then sc = sb end + local tb, tc = type(sb), type(sc) + if tb == tc then + if tb == "number" then -- Improve the guess for numbers. + if op == "DIV" or sb % 1 ~= 0 or sc % 1 ~= 0 then + sb = 0.5 -- Probably a non-integral number. + else + sb = 1 -- Optimistic guess. + end + end + if sb ~= nil then st[pc+hints.TYPE] = sb end + else + st[pc+hints.TYPE] = false -- Marker for mixed types. + end + if op ~= "LT" and op ~= "LE" then + slot[a] = sb -- Assume coercion to 1st type if different. + end +end + +-- Common handler for TEST and TESTSET. +local function testop(st, slot, pc, a, b, c) + -- Optimize the 'expr and k1 or k2' idiom. + local ok, k = prevconst(st, slot, pc, b) + if k and a == b then + st[pc+hints.COMBINE] = false -- Kill the TEST/TESTSET. + if c == 0 then st.live[pc+1] = nil end -- Kill the JMP. + end + slot[a] = slot[b] +end + +-- Dispatch table for opcode handlers. +local handler = { + MOVE = function(st, slot, pc, a, b, c) + slot[a] = slot[b] + end, + + LOADK = function(st, slot, pc, a, b, c) + slot[a] = const(st.func, b) + end, + + LOADBOOL = function(st, slot, pc, a, b, c) + slot[a] = (b == 1) + end, + + LOADNIL = function(st, slot, pc, a, b, c) + for i=a,b do slot[i] = nil end + end, + + GETUPVAL = function(st, slot, pc, a, b, c) + slot[a] = jutil.upvalue(st.func, b) + end, + + GETGLOBAL = function(st, slot, pc, a, b, c) + slot[a] = trylookup(st, st.stats.env, const(st.func, b)) + end, + + GETTABLE = function(st, slot, pc, a, b, c) + local t = slot[b] + tablehint(st, slot, pc, t, c) + slot[a] = trylookup(st, t, slot[c]) + end, + + SETGLOBAL = empty, + + SETUPVAL = empty, -- TODO: shortcut -- but this is rare? + + SETTABLE = function(st, slot, pc, a, b, c) + local t = slot[a] + tablehint(st, slot, pc, t, b) + if type(t) == "table" or type(t) == "userdata" then -- Must validate setkey. + local val = slot[c] + if val ~= nil then st.tableval[t] = val end + end + end, + + NEWTABLE = function(st, slot, pc, a, b, c) + slot[a] = {} -- Need unique tables for indexing st.tableval. + end, + + SELF = function(st, slot, pc, a, b, c) + local t = slot[b] + tablehint(st, slot, pc, t, c) + slot[a+1] = t + slot[a] = trylookup(st, t, slot[c]) + end, + + ADD = arithop, SUB = arithop, MUL = arithop, DIV = arithop, + MOD = arithop, POW = arithop, LT = arithop, LE = arithop, + + UNM = function(st, slot, pc, a, b, c) + return arithop(st, slot, pc, a, b, b, "UNM") + end, + + NOT = function(st, slot, pc, a, b, c) + slot[a] = true + end, + + LEN = function(st, slot, pc, a, b, c) + typehint(st, pc, slot[b]) + slot[a] = 1 + end, + + CONCAT = function(st, slot, pc, a, b, c) + local mixed + local sb = slot[b] + for i=b+1,c do + local si = slot[i] + if sb == nil then + sb = si + elseif si ~= nil and type(sb) ~= type(si) then + mixed = true + break + end + end + if sb == nil then + sb = "" + else + st[pc+hints.TYPE] = not mixed and sb or false + if type(sb) == "number" then sb = "" end + end + slot[a] = sb -- Assume coercion to 1st type (if different) or string. + end, + + JMP = function(st, slot, pc, a, b, c) + if b >= 0 then -- Kill JMPs across dead code. + local tpc = pc + b + while not st.live[tpc] do tpc = tpc - 1 end + if tpc == pc then st[pc+hints.COMBINE] = false end + end + end, + + EQ = function(st, slot, pc, a, b, c) + if b >= 0 and c >= 0 then typehint(st, pc, slot[b] or slot[c]) end + end, + + TEST = function(st, slot, pc, a, b, c) + return testop(st, slot, pc, a, a, c) + end, + + TESTSET = testop, + + CALL = function(st, slot, pc, a, b, c) + callhint(st, slot, pc, a, b-1, c-1) + end, + + TAILCALL = function(st, slot, pc, a, b, c) + callhint(st, slot, pc, a, b-1, -1) + end, + + RETURN = function(st, slot, pc, a, b, c) + if b == 2 and prevconst(st, slot, pc, a) then + st[pc-1+hints.COMBINE] = true -- Set COMBINE hint for prev. instruction. + end + end, + + FORLOOP = empty, + + FORPREP = function(st, slot, pc, a, b, c) + local ok, step = prevconst(st, slot, pc, a+2) + if type(step) == "number" then + st[pc+hints.FOR_STEP_K] = step + end + local nstart, nstep = slot[a], slot[a+2] + local tnstart, tnstep = type(nstart), type(nstep) + local ntype = ((tnstart == "number" and nstart % 1 ~= 0) or + (tnstep == "number" and nstep % 1 ~= 0)) and 0.5 or 1 + slot[a+3] = ntype + if tnstart == "number" and tnstep == "number" and + type(slot[a+1]) == "number" then + st[pc+hints.TYPE] = ntype + end + end, + + -- TFORLOOP is at the end of the loop. Setting slots would be pointless. + -- Inlining is handled by the corresponding iterator constructor (CALL). + TFORLOOP = function(st, slot, pc, a, b, c) + st[pc+hints.TYPE] = slot[a] + end, + + SETLIST = function(st, slot, pc, a, b, c) + -- TODO: if only (numeric) const: shortcut (+ nobarrier). + local t = slot[a] + -- Very imprecise. But better than nothing. + if type(t) == "table" then st.tableval[t] = slot[a+1] end + end, + + CLOSE = empty, + + CLOSURE = function(st, slot, pc, a, b, c) + slot[a] = empty + if st.noclose then + local nup = jutil.closurenup(st.func, b) + for i=pc+1,pc+nup do + local op = bytecode(st.func, i) + if op == "MOVE" then + st.noclose = false + return + end + end + end + end, + + VARARG = function(st, slot, pc, a, b, c) + local params = st.stats.params + for i=1,b do slot[a+i-1] = st[params+i] end + end, +} + +-- Generate some hints for the compiler backend. +local function optimize(st) + -- Deoptimization is simple: just don't generate any hints. :-) + if st.deopt then return end + + local func = st.func + local stats = jutil.stats(func) + if not stats then return jutil.status.COMPILER_ERROR end -- Eh? + + -- Check limits. + if stats.bytecodes > LIMITS.bytecodes or + stats.stackslots > LIMITS.stackslots or + stats.params > LIMITS.params or + stats.consts > LIMITS.consts or + stats.subs > LIMITS.subs then + return jutil.status.TOOLARGE + end + + -- Mark live instructions (live[pc]) and branch targets (live[-pc]). + local live = marklive(func) + + -- Handlers need access to some temporary state fields. + st.noclose = true + st.stats = stats + st.live = live + st.tableval = { [stats.env] = empty } -- Guess: unknown globals are functions. + + -- Initialize slots with argument hints and constants. + local slot = {} + for i=1,stats.params do slot[i-1] = st[i] end + for i=-1,-256,-1 do -- No need to copy non-RK-able consts. + local k, ok = const(func, i) + if not ok then break end + slot[i] = k + end + + -- Step through all live instructions, update slots and add hints. + for pc=1,stats.bytecodes do + if live[pc] then + local op, a, b, c, test = bytecode(func, pc) + handler[op](st, slot, pc, a, b, c, op) + else + st[pc+hints.COMBINE] = false -- Dead instruction hint. + end + end + + -- Update function hints. + if st.noclose then st[fhints.NOCLOSE] = true end + + -- Clear temporary state fields. + st.noclose = nil + st.stats = nil + st.live = nil + st.tableval = nil +end + + +-- Active flags. +local active, active_opt_inline + +-- Handler for compiler pipeline. +local function h_opt(st) + if optlevel <= 0 then return end + local ok, err = pcall(optimize, st) + if not ok then + io.stderr:write("\nERROR: jit.opt disabled: ", err, "\n") + jit.attach(h_opt) -- Better turn ourselves off after a failure. + active = nil + else + if err then return err end + end +end + +-- Load add-on module. +local function loadaddon(opt) + local name, val = string.match(opt, "^(.-)=(.*)$") -- Strip value. + if not name then name = opt end + name = "jit.opt_"..name + local ok, mod = pcall(require, name) + if not ok then + -- Return error if not installed, but propagate other errors. + if string.sub(mod, 1, 7) ~= "module " then error(mod, 0) end + return "optimizer add-on module "..name.." not found" + end + mod.start(val) +end + +-- Attach optimizer and set optimizer level or load add-on module. +local function setlevel_(opt) + -- Easier to always attach the optimizer (even for -O0). + if not active then + jit.attach(h_opt, PRIORITY) + active = true + end + + -- Parse -O or -O. + if opt == nil or opt == "" then + optlevel = OPTLEVEL + else + local level = tonumber(opt) -- Numeric level? + if level then + if level < 0 or level % 1 ~= 0 then + error("bad optimizer level", 0) + end + optlevel = level + else + if optlevel == -1 then optlevel = OPTLEVEL end + local err = loadaddon(opt) + if err then error(err, 0) end + end + end + + -- Load add-on module for inlining functions for -O2 and above. + if not active_opt_inline and optlevel >= 2 then + loadaddon("inline") -- Be silent if not installed. + active_opt_inline = true -- Try this only once. + end +end + + +-- Public module functions. +module(...) + +-- Callback to allow attaching a call hinter. Used by jit.opt_inline. +function attach_callhint(f) + callhint = f +end + +function getlevel() + return optlevel +end + +setlevel = setlevel_ +start = setlevel_ -- For -O command line option. + diff --git a/libraries/LuaJIT-1.1.7/jit/opt_inline.lua b/libraries/LuaJIT-1.1.7/jit/opt_inline.lua new file mode 100644 index 0000000..cc19bf4 --- /dev/null +++ b/libraries/LuaJIT-1.1.7/jit/opt_inline.lua @@ -0,0 +1,397 @@ +---------------------------------------------------------------------------- +-- LuaJIT optimizer add-on module for function inlining. +-- +-- Copyright (C) 2005-2011 Mike Pall. All rights reserved. +-- Released under the MIT/X license. See luajit.h for full copyright notice. +---------------------------------------------------------------------------- +-- This is a simple framework for C function signature maps. +-- It helps with type propagation and C function inlining. +-- +-- This module is automatically loaded with -O2 and above. +-- By default most standard library functions are added. +-- +-- TODO: generalize it, e.g. for back propagation (i.e. arg types). +-- TODO: extend it for Lua functions (but need to analyze them before use). +------------------------------------------------------------------------------ + +-- Cache some library functions and objects. +local jit = require("jit") +assert(jit.version_num == 10107, "LuaJIT core/library version mismatch") +local jutil = require("jit.util") +local type, rawget, next = type, rawget, next +local hints, fhints = jutil.hints, jutil.fhints +local sub, match, gsub = string.sub, string.match, string.gsub + +-- Turn compilation off for the whole module. LuaJIT would do that anyway. +jit.off(true, true) + +-- Prototypical objects used for type hints. +local TABLE = {} +local CFUNC = collectgarbage -- Pretty sure this is never inlined. + + +-- Map from C closures to signatures. Cannot use a weak table. +-- Closures must be kept alive because inlining checks against their addrs. +local map_sign = {} + +-- For jit.dumphints: get printable name for TYPE hint: "#INLINE(foo.bar)". +local function getname_(f, idx) + local sign = map_sign[f] + if sign then + local libname, name = sign.libname, sign.name + if libname then + return libname.."."..name + else + return name + end + elseif idx == 0 then + return "recursive" + else + return "?" + end +end + +-- Name, base table and running index for convenience functions. +-- CHECK: the library index and the order below must match with ljit_hints.h +local flibname, flib, fidx + +local function fadd(name, results, args, handler) + local f = rawget(flib, name) + if f then + map_sign[f] = { + libname = flibname, name = name, idx = fidx, + results = results, args = args, handler = handler, + } + end + if fidx then fidx = fidx + 1 end +end + +local function faddf(name, f, results, args, handler) + map_sign[f] = { + libname = flibname, name = name, idx = fidx, + results = results, args = args, handler = handler, + } + if fidx then fidx = fidx + 1 end +end + + +-- Signature handler: copy first argument to first result. +local function copyfirst(st, slot, pc, base, narg, nres) + slot[base] = slot[base+1] +end + +-- Helper for iterators: check if the function is an iterator constructor. +-- +-- 'for ivars in func(args) do body end' +-- +-- ...load func+args... +-- CALL func <-- pc +-- JMP fwd ---+ +-- back: | <--+ +-- ...body... | | +-- fwd: <--+ | +-- TFORLOOP ivars | <-- tforpc +-- JMP back ---+ +-- +local function itercheck(st, slot, pc, base, idx) + if idx then + local bytecode, func = jutil.bytecode, st.func + local op, _, fwd = bytecode(func, pc+1) + if op == "JMP" then + local tforpc = pc+2+fwd + local op, tfbase, _, tfnres = bytecode(func, tforpc) + if op == "TFORLOOP" and tfbase == base and tfnres <= 2 then + local op, _, back = bytecode(func, tforpc+1) + if op == "JMP" and fwd+back == -2 then + -- Must set inlining hint for TFORLOOP instruction here. + st[tforpc+hints.INLINE] = idx -- Serves as iterator index, too. + return -- Inline it. + end + end + end + end + slot[base] = CFUNC -- Better make it different from pairs. + return true -- Better not inline it, if not used in a for statement. +end + +-- Helper for pairs/next: guess result types for standard table iterator. +local function guessnext(st, slot, base, dest) + local t, k, v = slot[base+1] + if type(t) == "table" then + k, v = next(t) + if v == nil then v = st.tableval[t] end + end + slot[dest] = k or "" -- Strings are a good guess for the key type. + slot[dest+1] = v -- But better not guess any fixed value type. +end + + +-- Signatures for base library functions. +-- Note: Only add functions where result type hints or inlining makes sense. +flibname, flib, fidx = nil, _G, 65536*1 +fadd("pairs", "..0", "T", + function(st, slot, pc, base, narg, nres, idx) + -- Table in slot[base+1] is kept (2nd result = 1st arg). + -- Fill result slots for the iterator here (TFORLOOP is at the end). + guessnext(st, slot, base, base+3) + return itercheck(st, slot, pc, base, idx) + end) + +fadd("ipairs", "..I", "T", + function(st, slot, pc, base, narg, nres, idx) + -- Table in slot[base+1] is kept (2nd result = 1st arg). + -- Fill result slots for the iterator here (TFORLOOP is at the end). + local t = slot[base+1] + slot[base+3] = 1 -- Integer key. + local v + if type(t) == "table" then + v = rawget(t, 1) + if v == nil then v = st.tableval[t] end + end + slot[base+4] = v + return itercheck(st, slot, pc, base, idx) + end) + +fidx = nil -- Pure result type signatures follow: +fadd("next", "..", "T.?", + function(st, slot, pc, base, narg, nres) + guessnext(st, slot, base, base) + end) +fadd("type", "S", ".") +fadd("getmetatable", "T", ".") +fadd("setmetatable", ".", "TT?", copyfirst) +fadd("rawequal", "B", "..") +fadd("rawget", ".", "T.", + function(st, slot, pc, base, narg, nres) + local t = slot[base+1] + slot[base] = type(t) == "table" and rawget(t, slot[base+2]) or "" + end) +fadd("rawset", ".", "T..", copyfirst) +fadd("assert", "*", "..*", + function(st, slot, pc, base, narg, nres) + for i=1,nres do slot[base+i-1] = i <= narg and slot[base+i] or nil end + end) +fadd("tonumber", "I", ".I?") +fadd("tostring", "S", ".") +fadd("require", "T", "S") + +-- Signatures for coroutine library functions. +flibname, flib, fidx = "coroutine", coroutine, 65536*2 +if flib then + fadd("yield", "*", ".*") + fadd("resume", "*", "R.*", + function(st, slot, pc, base, narg, nres) + slot[base] = true + for i=1,nres-1 do slot[base+i] = nil end -- No guess. + end) + + fidx = nil -- Pure result type signatures follow: + fadd("wrap", "C", "F") +end + +-- Signatures for string library functions. +flibname, flib, fidx = "string", string, 65536*3 +if flib then + fadd("len", "I", "S") + fadd("sub", "S", "SII?") + fadd("char", "S", "I*") + + fidx = nil -- Pure result type signatures follow: + fadd("byte", "I", "S", + function(st, slot, pc, base, narg, nres) + for i=0,nres-1 do slot[base+i] = 1 end -- Set all result hints. + end) + fadd("rep", "S", "SI") + fadd("reverse", "S", "S") + fadd("upper", "S", "S") + fadd("lower", "S", "S") + + fadd("format", "S", "S.*") + fadd("find", "*", "SSI?.?", + function(st, slot, pc, base, narg, nres) + slot[base] = 1 + slot[base+1] = 1 + for i=2,nres-1 do slot[base+i] = "" end -- Hints for matches. + end) + fadd("match", "*", "SSI?", + function(st, slot, pc, base, narg, nres) + for i=0,nres-1 do slot[base+i] = "" end -- Hints for matches. + end) + fadd("gsub", "SI", "SSGI?") + fadd("gmatch", "C00", "SS", + function(st, slot, pc, base, narg, nres) + -- Fill result slots for gmatch_iter here (TFORLOOP is at the end). + for i=base+3,st.stats.stackslots-1 do slot[i] = "" end + end) + -- The gmatch iterator itself is never inlined. No point in adding it. +end + +-- Signatures for table library functions. +flibname, flib, fidx = "table", table, 65536*4 +if flib then + fadd("insert", "", "TI?.") + fadd("remove", ".", "T", + function(st, slot, pc, base, narg, nres) + if nres >= 1 then + local t = slot[base+1] + slot[base] = type(t) == "table" and rawget(t, 1) or "" + end + end) + fadd("getn", "I", "T") + + fidx = nil -- Pure result type signatures follow: + fadd("concat", "S", "TS?I?I?") +end + +-- Signatures for math library functions. +flibname, flib, fidx = "math", math, 65536*5 +if flib then + -- 1 arg, 1 result. + fadd("log", "N", "N") + fadd("log10", "N", "N") + fadd("exp", "N", "N") + fadd("sinh", "N", "N") + fadd("cosh", "N", "N") + fadd("tanh", "N", "N") + fadd("asin", "N", "N") + fadd("acos", "N", "N") + fadd("atan", "N", "N") + fadd("sin", "N", "N") + fadd("cos", "N", "N") + fadd("tan", "N", "N") + fadd("ceil", "I", "N") + fadd("floor", "I", "N") + fadd("abs", ".", "N", copyfirst) + fadd("sqrt", "N", "N") + -- 2 args, 1 result. + -- math.fmod is aliased to math.mod for compatibility. + fadd("fmod", ".", "NN", + function(st, slot, pc, base, narg, nres) + slot[base] = slot[base+2] or 1 -- Copy integer or number hint. + end) + fadd("atan2", "N", "NN") + + fidx = nil -- Pure result type signatures follow: + -- 1-n args, 1 result. + fadd("min", ".", "NN*", copyfirst) -- Really depends on all args. + fadd("max", ".", "NN*", copyfirst) -- Really depends on all args. + -- 1 arg, 1 result. + fadd("deg", "N", "N") + fadd("rad", "N", "N") + -- 1 arg, 2 results. + fadd("modf", "IN", "N") + fadd("frexp", "NI", "N") + -- 2 args, 1 result. + fadd("pow", "N", "NN") + fadd("ldexp", ".", "NI", copyfirst) + -- 1 arg, 0 results. + fadd("randomseed", "", "I") + -- 0-2 args, 1 result. + fadd("random", "N", "I?I?", + function(st, slot, pc, base, narg, nres) + if narg > 0 then slot[base] = 1 end + end) +end + +-- Signatures for I/O library functions. +-- Not inlined anytime soon. Used for result types only. +flibname, flib, fidx = "io", io, nil +if flib then + fadd("lines", "C00S", "S?") + fadd("read", "S", "") -- Simplified (a lot). + -- Adding io methods doesn't work, because we don't deal with userdata (yet). +end + + +-- Type names to argument type shorthands. +-- TODO: make the matches more exact? Need to differentiate nil/unknown. +local map_argtype = { + ["nil"] = "0", boolean = "b", number = "n", string = "s", + table = "t", ["function"] = "f", userdata = "u", thread = "r", +} + +-- Complex argument match patterns to regexp fragments. +local map_argmatch = { + B = "[b0]", S = "[s0]", T = "[t0]", F = "[f0]", U = "[u0]", R = "[r0]", + N = "[n0]", I = "[n0]", -- Number/int args are the same for now. + G = "[stf0]", -- For string.gsub. +} + +-- Result type shorthands to sample types. +local map_restype = { + -- ["0"] = nil, + B = true, S = "", T = {}, + N = 0.5, I = 1, + L = function() end, C = collectgarbage, -- Pretty sure this is never inlined. +} + +-- Create argument match regexp and cache it. +local function getargmatch(sign) + local argmatch = "^"..gsub(sign.args, ".", map_argmatch).."0*$" + sign.argmatch = argmatch + return argmatch +end + +-- Set INLINE hints and result types for known C functions. +local function inlinehint(sign, st, slot, pc, base, narg, nres) + local idx = sign.idx + if idx then + if narg ~= -1 then + local argpat = "" + for i=1,narg do argpat = argpat..map_argtype[type(slot[base+i])] end + if not match(argpat, sign.argmatch or getargmatch(sign)) then + idx = nil + end + end + end + + local results = sign.results + if results ~= "*" and nres ~= -1 then + if nres > #results then idx = nil end + for i=1,#results do + local c = sub(results, i, i) + if c ~= "." then slot[base+i-1] = map_restype[c] end + end + end + + local handler = sign.handler + if handler and handler(st, slot, pc, base, narg, nres, idx) then idx = nil end + + if idx then st[pc+hints.INLINE] = idx end +end + +-- Set call hints and result types during forward propagation. +local function fwdcallhint(st, slot, pc, base, narg, nres) + local f = slot[base] + st[pc+hints.TYPE] = f + if type(f) == "function" then + local sign = map_sign[f] + if sign then + inlinehint(sign, st, slot, pc, base, narg, nres) + return + end + if f == st.func and not st.stats.isvararg and + (narg == -1 or narg == st.stats.params) then + st[pc+hints.INLINE] = 0 -- Recursive call. + end + end + -- Clear result types for unknown functions. + for i=base,base+nres-1 do slot[i] = nil end +end + + +-- Attach call hinter to optimizer. +local function start_() + local jopt = require "jit.opt" + jopt.attach_callhint(fwdcallhint) + -- Note that just loading the optimizer does not start it, yet. +end + + +-- Public module functions. +module(...) + +-- TODO: Public API to add signatures. Alas, the API is still in flux. +getname = getname_ +start = start_ + diff --git a/libraries/LuaJIT-1.1.7/jit/trace.lua b/libraries/LuaJIT-1.1.7/jit/trace.lua new file mode 100644 index 0000000..42367c6 --- /dev/null +++ b/libraries/LuaJIT-1.1.7/jit/trace.lua @@ -0,0 +1,111 @@ +---------------------------------------------------------------------------- +-- LuaJIT compiler tracing module. +-- +-- Copyright (C) 2005-2011 Mike Pall. All rights reserved. +-- Released under the MIT/X license. See luajit.h for full copyright notice. +---------------------------------------------------------------------------- +-- Activate this module to trace the progress of the JIT compiler. +-- +-- Try: luajit -j trace -e 'print "foo"' +-- luajit -j trace=foo.trace foo.lua +-- +-- Default output is to stderr. To redirect output to a file, +-- pass a filename as an argument or set the environment variable +-- "LUAJIT_TRACEFILE". +-- Note: the file is overwritten each time you run luajit. +------------------------------------------------------------------------------ + +-- Priority for compiler pipeline. Must run after backend (negative) +-- and should be odd to catch compiler errors, too. +local PRIORITY = -99 + +-- Cache some library functions and objects. +local jit = require("jit") +assert(jit.version_num == 10107, "LuaJIT core/library version mismatch") +local jutil = require("jit.util") +local type, tostring, sub, format = type, tostring, string.sub, string.format +local getinfo, justats = debug.getinfo, jutil.stats +local stdout, stderr = io.stdout, io.stderr + +-- Turn compilation off for the whole module. LuaJIT would do that anyway. +jit.off(true, true) + +-- Active flag and output file handle. +local active, out + +-- Generate range string from table: pc[-pc] [,...] +local function rangestring(t) + local s = "" + for i,range in ipairs(t) do + if i ~= 1 then s = s.."," end + local pc = range % 65536 + range = (range - pc) / 65536 + s = s..pc + if range ~= 0 then s = s..(-(pc+range)) end + end + return s +end + +-- Trace handler for compiler pipeline. +local function h_trace(st, status) + local o = out or stderr + local func = st.func + if type(func) ~= "function" then return end + local info = getinfo(func, "S") + local src, line = info.source, info.linedefined or 0 + if src then + if sub(src, 1, 1) == "@" or sub(src, 1, 2) == "=(" then + src = sub(src, 2) + else + src = "**"..string.gsub(sub(src, 1, 40), "%c", " ").."**" + end + else + src = "?" + end + local aux = st.deopt and " DEOPT="..rangestring(st.deopt) or "" + if status == nil then + local stats = justats(func) + if not stats then return end + o:write(format("[LuaJIT: OK %4d %6d %s:%d%s]\n", + stats.bytecodes, stats.mcodesize or -1, src, line, aux)) + return + else + local stname = jit.util.status[status] or status + local pc, err = st.dasm_pc, st.dasm_err + if type(pc) == "number" and type(err) == "number" then + local op = jutil.bytecode(func, pc) or "END" + o:write(format("[LuaJIT: %s %s@%d %08x %s:%d%s]\n", + stname, op, pc, err, src, line, aux)) + else + o:write(format("[LuaJIT: %s %s:%d%s]\n", stname, src, line, aux)) + end + end +end + +-- Detach trace handler from compiler pipeline. +local function traceoff() + if active then + active = false + jit.attach(h_trace) + if out and out ~= stdout then out:close() end + out = nil + end +end + +-- Open the output file and attach trace handler to compiler pipeline. +local function traceon(filename) + if active then traceoff() end + local outfile = filename or os.getenv("LUAJIT_TRACEFILE") + out = outfile and (outfile == "-" and stdout or assert(io.open(outfile, "w"))) + jit.attach(h_trace, PRIORITY) + active = true +end + + +-- Public module functions. +module(...) + +on = traceon +off = traceoff +start = traceon -- For -j command line option. + -- cgit v1.1