luau/CodeGen/src/EmitBuiltinsX64.cpp

169 lines
6.2 KiB
C++
Raw Normal View History

2022-10-14 01:59:53 +03:00
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#include "EmitBuiltinsX64.h"
#include "Luau/AssemblyBuilderX64.h"
#include "Luau/Bytecode.h"
2023-03-31 15:21:14 +03:00
#include "Luau/IrCallWrapperX64.h"
#include "Luau/IrRegAllocX64.h"
2022-10-14 01:59:53 +03:00
#include "EmitCommonX64.h"
2023-01-03 19:33:19 +02:00
#include "NativeState.h"
2022-10-14 01:59:53 +03:00
#include "lstate.h"
namespace Luau
{
namespace CodeGen
{
2023-03-03 15:45:38 +02:00
namespace X64
2022-10-14 01:59:53 +03:00
{
2023-03-03 15:45:38 +02:00
void emitBuiltinMathLog(IrRegAllocX64& regs, AssemblyBuilderX64& build, int nparams, int ra, int arg, OperandX64 args, int nresults)
2023-01-03 19:33:19 +02:00
{
2023-03-03 15:45:38 +02:00
regs.assertAllFree();
2023-01-03 19:33:19 +02:00
build.vmovsd(xmm0, luauRegValue(arg));
2023-04-14 15:05:27 +03:00
// TODO: IR builtin lowering assumes that the only valid 2-argument call is log2; ideally, we use a less hacky way to indicate that
if (nparams == 2)
2023-01-03 19:33:19 +02:00
build.call(qword[rNativeContext + offsetof(NativeContext, libm_log2)]);
2023-04-14 15:05:27 +03:00
else
2023-01-03 19:33:19 +02:00
build.call(qword[rNativeContext + offsetof(NativeContext, libm_log)]);
build.vmovsd(luauRegValue(ra), xmm0);
}
2023-03-03 15:45:38 +02:00
void emitBuiltinMathLdexp(IrRegAllocX64& regs, AssemblyBuilderX64& build, int nparams, int ra, int arg, OperandX64 args, int nresults)
2023-02-03 14:34:12 +02:00
{
2023-03-31 15:21:14 +03:00
ScopedRegX64 tmp{regs, SizeX64::qword};
build.vcvttsd2si(tmp.reg, qword[args + offsetof(TValue, value)]);
2023-02-03 14:34:12 +02:00
2023-03-31 15:21:14 +03:00
IrCallWrapperX64 callWrap(regs, build);
callWrap.addArgument(SizeX64::xmmword, luauRegValue(arg));
callWrap.addArgument(SizeX64::qword, tmp);
callWrap.call(qword[rNativeContext + offsetof(NativeContext, libm_ldexp)]);
2023-02-03 14:34:12 +02:00
build.vmovsd(luauRegValue(ra), xmm0);
}
2023-03-03 15:45:38 +02:00
void emitBuiltinMathFrexp(IrRegAllocX64& regs, AssemblyBuilderX64& build, int nparams, int ra, int arg, OperandX64 args, int nresults)
2023-02-03 14:34:12 +02:00
{
2023-03-31 15:21:14 +03:00
IrCallWrapperX64 callWrap(regs, build);
callWrap.addArgument(SizeX64::xmmword, luauRegValue(arg));
callWrap.addArgument(SizeX64::qword, sTemporarySlot);
callWrap.call(qword[rNativeContext + offsetof(NativeContext, libm_frexp)]);
2023-02-03 14:34:12 +02:00
build.vmovsd(luauRegValue(ra), xmm0);
2023-04-07 12:56:27 -07:00
if (nresults > 1)
{
build.vcvtsi2sd(xmm0, xmm0, dword[sTemporarySlot + 0]);
build.vmovsd(luauRegValue(ra + 1), xmm0);
}
2023-02-03 14:34:12 +02:00
}
2023-03-03 15:45:38 +02:00
void emitBuiltinMathModf(IrRegAllocX64& regs, AssemblyBuilderX64& build, int nparams, int ra, int arg, OperandX64 args, int nresults)
2023-02-03 14:34:12 +02:00
{
2023-03-31 15:21:14 +03:00
IrCallWrapperX64 callWrap(regs, build);
callWrap.addArgument(SizeX64::xmmword, luauRegValue(arg));
callWrap.addArgument(SizeX64::qword, sTemporarySlot);
callWrap.call(qword[rNativeContext + offsetof(NativeContext, libm_modf)]);
2023-02-03 14:34:12 +02:00
build.vmovsd(xmm1, qword[sTemporarySlot + 0]);
build.vmovsd(luauRegValue(ra), xmm1);
2023-04-07 12:56:27 -07:00
if (nresults > 1)
build.vmovsd(luauRegValue(ra + 1), xmm0);
2023-02-03 14:34:12 +02:00
}
2023-03-03 15:45:38 +02:00
void emitBuiltinMathSign(IrRegAllocX64& regs, AssemblyBuilderX64& build, int nparams, int ra, int arg, OperandX64 args, int nresults)
2023-02-03 14:34:12 +02:00
{
2023-03-03 15:45:38 +02:00
ScopedRegX64 tmp0{regs, SizeX64::xmmword};
ScopedRegX64 tmp1{regs, SizeX64::xmmword};
ScopedRegX64 tmp2{regs, SizeX64::xmmword};
ScopedRegX64 tmp3{regs, SizeX64::xmmword};
2023-02-03 14:34:12 +02:00
2023-03-03 15:45:38 +02:00
build.vmovsd(tmp0.reg, luauRegValue(arg));
build.vxorpd(tmp1.reg, tmp1.reg, tmp1.reg);
2023-02-03 14:34:12 +02:00
2023-03-03 15:45:38 +02:00
// Set tmp2 to -1 if arg < 0, else 0
build.vcmpltsd(tmp2.reg, tmp0.reg, tmp1.reg);
build.vmovsd(tmp3.reg, build.f64(-1));
build.vandpd(tmp2.reg, tmp2.reg, tmp3.reg);
2023-02-03 14:34:12 +02:00
// Set mask bit to 1 if 0 < arg, else 0
2023-03-03 15:45:38 +02:00
build.vcmpltsd(tmp0.reg, tmp1.reg, tmp0.reg);
2023-02-03 14:34:12 +02:00
2023-03-03 15:45:38 +02:00
// Result = (mask-bit == 1) ? 1.0 : tmp2
// If arg < 0 then tmp2 is -1 and mask-bit is 0, result is -1
// If arg == 0 then tmp2 is 0 and mask-bit is 0, result is 0
// If arg > 0 then tmp2 is 0 and mask-bit is 1, result is 1
build.vblendvpd(tmp0.reg, tmp2.reg, build.f64x2(1, 1), tmp0.reg);
2023-02-03 14:34:12 +02:00
2023-03-03 15:45:38 +02:00
build.vmovsd(luauRegValue(ra), tmp0.reg);
2023-02-03 14:34:12 +02:00
}
2023-03-17 16:59:30 +02:00
void emitBuiltinType(IrRegAllocX64& regs, AssemblyBuilderX64& build, int nparams, int ra, int arg, OperandX64 args, int nresults)
{
ScopedRegX64 tmp0{regs, SizeX64::qword};
ScopedRegX64 tag{regs, SizeX64::dword};
build.mov(tag.reg, luauRegTag(arg));
build.mov(tmp0.reg, qword[rState + offsetof(lua_State, global)]);
build.mov(tmp0.reg, qword[tmp0.reg + qwordReg(tag.reg) * sizeof(TString*) + offsetof(global_State, ttname)]);
build.mov(luauRegValue(ra), tmp0.reg);
}
void emitBuiltinTypeof(IrRegAllocX64& regs, AssemblyBuilderX64& build, int nparams, int ra, int arg, OperandX64 args, int nresults)
{
2023-03-31 15:21:14 +03:00
IrCallWrapperX64 callWrap(regs, build);
callWrap.addArgument(SizeX64::qword, rState);
callWrap.addArgument(SizeX64::qword, luauRegAddress(arg));
callWrap.call(qword[rNativeContext + offsetof(NativeContext, luaT_objtypenamestr)]);
2023-03-17 16:59:30 +02:00
build.mov(luauRegValue(ra), rax);
}
2023-03-03 15:45:38 +02:00
void emitBuiltin(IrRegAllocX64& regs, AssemblyBuilderX64& build, int bfid, int ra, int arg, IrOp args, int nparams, int nresults)
2023-02-03 14:34:12 +02:00
{
2023-03-03 15:45:38 +02:00
OperandX64 argsOp = 0;
2023-02-03 14:34:12 +02:00
2023-03-03 15:45:38 +02:00
if (args.kind == IrOpKind::VmReg)
2023-04-07 12:56:27 -07:00
argsOp = luauRegAddress(vmRegOp(args));
2023-03-03 15:45:38 +02:00
else if (args.kind == IrOpKind::VmConst)
2023-04-07 12:56:27 -07:00
argsOp = luauConstantAddress(vmConstOp(args));
2023-02-03 14:34:12 +02:00
2022-10-14 01:59:53 +03:00
switch (bfid)
{
2023-01-03 19:33:19 +02:00
case LBF_MATH_LOG:
2023-04-14 15:05:27 +03:00
LUAU_ASSERT((nparams == 1 || nparams == 2) && nresults == 1);
2023-03-03 15:45:38 +02:00
return emitBuiltinMathLog(regs, build, nparams, ra, arg, argsOp, nresults);
2023-02-03 14:34:12 +02:00
case LBF_MATH_LDEXP:
2023-04-14 15:05:27 +03:00
LUAU_ASSERT(nparams == 2 && nresults == 1);
2023-03-03 15:45:38 +02:00
return emitBuiltinMathLdexp(regs, build, nparams, ra, arg, argsOp, nresults);
2023-02-03 14:34:12 +02:00
case LBF_MATH_FREXP:
2023-04-14 15:05:27 +03:00
LUAU_ASSERT(nparams == 1 && (nresults == 1 || nresults == 2));
2023-03-03 15:45:38 +02:00
return emitBuiltinMathFrexp(regs, build, nparams, ra, arg, argsOp, nresults);
2023-02-03 14:34:12 +02:00
case LBF_MATH_MODF:
2023-04-14 15:05:27 +03:00
LUAU_ASSERT(nparams == 1 && (nresults == 1 || nresults == 2));
2023-03-03 15:45:38 +02:00
return emitBuiltinMathModf(regs, build, nparams, ra, arg, argsOp, nresults);
2023-02-03 14:34:12 +02:00
case LBF_MATH_SIGN:
2023-04-14 15:05:27 +03:00
LUAU_ASSERT(nparams == 1 && nresults == 1);
2023-03-03 15:45:38 +02:00
return emitBuiltinMathSign(regs, build, nparams, ra, arg, argsOp, nresults);
2023-03-17 16:59:30 +02:00
case LBF_TYPE:
2023-04-14 15:05:27 +03:00
LUAU_ASSERT(nparams == 1 && nresults == 1);
2023-03-17 16:59:30 +02:00
return emitBuiltinType(regs, build, nparams, ra, arg, argsOp, nresults);
case LBF_TYPEOF:
2023-04-14 15:05:27 +03:00
LUAU_ASSERT(nparams == 1 && nresults == 1);
2023-03-17 16:59:30 +02:00
return emitBuiltinTypeof(regs, build, nparams, ra, arg, argsOp, nresults);
2022-10-14 01:59:53 +03:00
default:
2023-04-14 15:05:27 +03:00
LUAU_ASSERT(!"Missing x64 lowering");
2023-03-03 15:45:38 +02:00
break;
2022-10-14 01:59:53 +03:00
}
}
2023-03-03 15:45:38 +02:00
} // namespace X64
2022-10-14 01:59:53 +03:00
} // namespace CodeGen
} // namespace Luau