2022-10-14 20:48:41 +01:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Luau/AssemblyBuilderX64.h"
|
|
|
|
|
2023-03-03 20:21:14 +00:00
|
|
|
#include "EmitCommon.h"
|
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
#include "lobject.h"
|
|
|
|
#include "ltm.h"
|
|
|
|
|
|
|
|
// MS x64 ABI reminder:
|
|
|
|
// Arguments: rcx, rdx, r8, r9 ('overlapped' with xmm0-xmm3)
|
|
|
|
// Return: rax, xmm0
|
|
|
|
// Nonvolatile: r12-r15, rdi, rsi, rbx, rbp
|
|
|
|
// SIMD: only xmm6-xmm15 are non-volatile, all ymm upper parts are volatile
|
|
|
|
|
|
|
|
// AMD64 ABI reminder:
|
|
|
|
// Arguments: rdi, rsi, rdx, rcx, r8, r9 (xmm0-xmm7)
|
|
|
|
// Return: rax, rdx, xmm0, xmm1
|
|
|
|
// Nonvolatile: r12-r15, rbx, rbp
|
|
|
|
// SIMD: all volatile
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
namespace CodeGen
|
|
|
|
{
|
|
|
|
|
2023-03-03 20:21:14 +00:00
|
|
|
enum class IrCondition : uint8_t;
|
2022-10-14 20:48:41 +01:00
|
|
|
struct NativeState;
|
2023-03-31 19:42:49 +01:00
|
|
|
struct IrOp;
|
2022-10-14 20:48:41 +01:00
|
|
|
|
2023-03-03 20:21:14 +00:00
|
|
|
namespace X64
|
|
|
|
{
|
|
|
|
|
2023-03-31 19:42:49 +01:00
|
|
|
struct IrRegAllocX64;
|
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
constexpr uint32_t kFunctionAlignment = 32;
|
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
// Data that is very common to access is placed in non-volatile registers
|
|
|
|
constexpr RegisterX64 rState = r15; // lua_State* L
|
|
|
|
constexpr RegisterX64 rBase = r14; // StkId base
|
|
|
|
constexpr RegisterX64 rNativeContext = r13; // NativeContext* context
|
|
|
|
constexpr RegisterX64 rConstants = r12; // TValue* k
|
|
|
|
|
|
|
|
// Native code is as stackless as the interpreter, so we can place some data on the stack once and have it accessible at any point
|
2022-11-10 22:53:13 +00:00
|
|
|
// See CodeGenX64.cpp for layout
|
2023-04-07 22:01:29 +01:00
|
|
|
constexpr unsigned kStackSize = 32 + 16; // 4 home locations for registers, 16 bytes for additional function call arguments
|
|
|
|
constexpr unsigned kSpillSlots = 4; // locations for register allocator to spill data into
|
|
|
|
constexpr unsigned kLocalsSize = 24 + 8 * kSpillSlots; // 3 extra slots for our custom locals (also aligns the stack to 16 byte boundary)
|
2022-11-10 22:53:13 +00:00
|
|
|
|
|
|
|
constexpr OperandX64 sClosure = qword[rsp + kStackSize + 0]; // Closure* cl
|
|
|
|
constexpr OperandX64 sCode = qword[rsp + kStackSize + 8]; // Instruction* code
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-03 19:26:13 +00:00
|
|
|
constexpr OperandX64 sTemporarySlot = addr[rsp + kStackSize + 16];
|
2023-04-07 22:01:29 +01:00
|
|
|
constexpr OperandX64 sSpillArea = addr[rsp + kStackSize + 24];
|
2022-10-14 20:48:41 +01:00
|
|
|
|
|
|
|
inline OperandX64 luauReg(int ri)
|
|
|
|
{
|
|
|
|
return xmmword[rBase + ri * sizeof(TValue)];
|
|
|
|
}
|
|
|
|
|
2022-11-04 17:33:22 +00:00
|
|
|
inline OperandX64 luauRegAddress(int ri)
|
|
|
|
{
|
|
|
|
return addr[rBase + ri * sizeof(TValue)];
|
|
|
|
}
|
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
inline OperandX64 luauRegValue(int ri)
|
|
|
|
{
|
|
|
|
return qword[rBase + ri * sizeof(TValue) + offsetof(TValue, value)];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline OperandX64 luauRegTag(int ri)
|
|
|
|
{
|
|
|
|
return dword[rBase + ri * sizeof(TValue) + offsetof(TValue, tt)];
|
|
|
|
}
|
|
|
|
|
2022-12-09 19:57:01 +00:00
|
|
|
inline OperandX64 luauRegValueInt(int ri)
|
2022-10-14 20:48:41 +01:00
|
|
|
{
|
|
|
|
return dword[rBase + ri * sizeof(TValue) + offsetof(TValue, value)];
|
|
|
|
}
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
inline OperandX64 luauRegValueVector(int ri, int index)
|
|
|
|
{
|
|
|
|
return dword[rBase + ri * sizeof(TValue) + offsetof(TValue, value) + (sizeof(float) * index)];
|
|
|
|
}
|
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
inline OperandX64 luauConstant(int ki)
|
|
|
|
{
|
|
|
|
return xmmword[rConstants + ki * sizeof(TValue)];
|
|
|
|
}
|
|
|
|
|
2022-11-04 17:33:22 +00:00
|
|
|
inline OperandX64 luauConstantAddress(int ki)
|
|
|
|
{
|
|
|
|
return addr[rConstants + ki * sizeof(TValue)];
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:54:01 +01:00
|
|
|
inline OperandX64 luauConstantTag(int ki)
|
|
|
|
{
|
|
|
|
return dword[rConstants + ki * sizeof(TValue) + offsetof(TValue, tt)];
|
|
|
|
}
|
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
inline OperandX64 luauConstantValue(int ki)
|
|
|
|
{
|
|
|
|
return qword[rConstants + ki * sizeof(TValue) + offsetof(TValue, value)];
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:54:01 +01:00
|
|
|
inline OperandX64 luauNodeKeyValue(RegisterX64 node)
|
|
|
|
{
|
|
|
|
return qword[node + offsetof(LuaNode, key) + offsetof(TKey, value)];
|
|
|
|
}
|
|
|
|
|
2022-10-28 11:37:29 +01:00
|
|
|
// Note: tag has dirty upper bits
|
|
|
|
inline OperandX64 luauNodeKeyTag(RegisterX64 node)
|
|
|
|
{
|
2023-05-19 20:37:30 +01:00
|
|
|
return dword[node + offsetof(LuaNode, key) + kOffsetOfTKeyTagNext];
|
2022-10-28 11:37:29 +01:00
|
|
|
}
|
|
|
|
|
2022-10-21 18:54:01 +01:00
|
|
|
inline OperandX64 luauNodeValue(RegisterX64 node)
|
|
|
|
{
|
|
|
|
return xmmword[node + offsetof(LuaNode, val)];
|
|
|
|
}
|
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
inline void setLuauReg(AssemblyBuilderX64& build, RegisterX64 tmp, int ri, OperandX64 op)
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(op.cat == CategoryX64::mem);
|
|
|
|
|
|
|
|
build.vmovups(tmp, op);
|
|
|
|
build.vmovups(luauReg(ri), tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void jumpIfTagIs(AssemblyBuilderX64& build, int ri, lua_Type tag, Label& label)
|
|
|
|
{
|
|
|
|
build.cmp(luauRegTag(ri), tag);
|
2022-11-04 17:33:22 +00:00
|
|
|
build.jcc(ConditionX64::Equal, label);
|
2022-10-14 20:48:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void jumpIfTagIsNot(AssemblyBuilderX64& build, int ri, lua_Type tag, Label& label)
|
|
|
|
{
|
|
|
|
build.cmp(luauRegTag(ri), tag);
|
2022-11-04 17:33:22 +00:00
|
|
|
build.jcc(ConditionX64::NotEqual, label);
|
2022-10-14 20:48:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note: fallthrough label should be placed after this condition
|
|
|
|
inline void jumpIfFalsy(AssemblyBuilderX64& build, int ri, Label& target, Label& fallthrough)
|
|
|
|
{
|
|
|
|
jumpIfTagIs(build, ri, LUA_TNIL, target); // false if nil
|
|
|
|
jumpIfTagIsNot(build, ri, LUA_TBOOLEAN, fallthrough); // true if not nil or boolean
|
|
|
|
|
2022-12-09 19:57:01 +00:00
|
|
|
build.cmp(luauRegValueInt(ri), 0);
|
2022-11-04 17:33:22 +00:00
|
|
|
build.jcc(ConditionX64::Equal, target); // true if boolean value is 'true'
|
2022-10-14 20:48:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note: fallthrough label should be placed after this condition
|
|
|
|
inline void jumpIfTruthy(AssemblyBuilderX64& build, int ri, Label& target, Label& fallthrough)
|
|
|
|
{
|
|
|
|
jumpIfTagIs(build, ri, LUA_TNIL, fallthrough); // false if nil
|
|
|
|
jumpIfTagIsNot(build, ri, LUA_TBOOLEAN, target); // true if not nil or boolean
|
|
|
|
|
2022-12-09 19:57:01 +00:00
|
|
|
build.cmp(luauRegValueInt(ri), 0);
|
2022-11-04 17:33:22 +00:00
|
|
|
build.jcc(ConditionX64::NotEqual, target); // true if boolean value is 'true'
|
2022-10-14 20:48:41 +01:00
|
|
|
}
|
|
|
|
|
2023-03-03 20:21:14 +00:00
|
|
|
void jumpOnNumberCmp(AssemblyBuilderX64& build, RegisterX64 tmp, OperandX64 lhs, OperandX64 rhs, IrCondition cond, Label& label);
|
2023-03-31 19:42:49 +01:00
|
|
|
void jumpOnAnyCmpFallback(IrRegAllocX64& regs, AssemblyBuilderX64& build, int ra, int rb, IrCondition cond, Label& label);
|
2022-10-14 20:48:41 +01:00
|
|
|
|
2022-12-09 19:57:01 +00:00
|
|
|
void getTableNodeAtCachedSlot(AssemblyBuilderX64& build, RegisterX64 tmp, RegisterX64 node, RegisterX64 table, int pcpos);
|
|
|
|
void convertNumberToIndexOrJump(AssemblyBuilderX64& build, RegisterX64 tmp, RegisterX64 numd, RegisterX64 numi, Label& label);
|
2022-10-14 20:48:41 +01:00
|
|
|
|
2023-03-31 19:42:49 +01:00
|
|
|
void callArithHelper(IrRegAllocX64& regs, AssemblyBuilderX64& build, int ra, int rb, OperandX64 c, TMS tm);
|
|
|
|
void callLengthHelper(IrRegAllocX64& regs, AssemblyBuilderX64& build, int ra, int rb);
|
|
|
|
void callPrepareForN(IrRegAllocX64& regs, AssemblyBuilderX64& build, int limit, int step, int init);
|
|
|
|
void callGetTable(IrRegAllocX64& regs, AssemblyBuilderX64& build, int rb, OperandX64 c, int ra);
|
|
|
|
void callSetTable(IrRegAllocX64& regs, AssemblyBuilderX64& build, int rb, OperandX64 c, int ra);
|
2023-06-24 07:19:39 +01:00
|
|
|
void checkObjectBarrierConditions(AssemblyBuilderX64& build, RegisterX64 tmp, RegisterX64 object, int ra, int ratag, Label& skip);
|
|
|
|
void callBarrierObject(IrRegAllocX64& regs, AssemblyBuilderX64& build, RegisterX64 object, IrOp objectOp, int ra, int ratag);
|
2023-04-07 22:01:29 +01:00
|
|
|
void callBarrierTableFast(IrRegAllocX64& regs, AssemblyBuilderX64& build, RegisterX64 table, IrOp tableOp);
|
|
|
|
void callStepGc(IrRegAllocX64& regs, AssemblyBuilderX64& build);
|
2022-10-14 20:48:41 +01:00
|
|
|
|
|
|
|
void emitExit(AssemblyBuilderX64& build, bool continueInVm);
|
|
|
|
void emitUpdateBase(AssemblyBuilderX64& build);
|
2023-06-16 18:35:18 +01:00
|
|
|
void emitInterrupt(AssemblyBuilderX64& build);
|
2023-05-19 20:37:30 +01:00
|
|
|
void emitFallback(IrRegAllocX64& regs, AssemblyBuilderX64& build, int offset, int pcpos);
|
2022-10-14 20:48:41 +01:00
|
|
|
|
2022-11-04 17:33:22 +00:00
|
|
|
void emitContinueCallInVm(AssemblyBuilderX64& build);
|
|
|
|
|
2023-06-09 18:08:00 +01:00
|
|
|
void emitReturn(AssemblyBuilderX64& build, ModuleHelpers& helpers);
|
|
|
|
|
2023-03-03 20:21:14 +00:00
|
|
|
} // namespace X64
|
2022-10-14 20:48:41 +01:00
|
|
|
} // namespace CodeGen
|
|
|
|
} // namespace Luau
|