2023-01-13 22:10:01 +00: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/Bytecode.h"
|
|
|
|
#include "Luau/Common.h"
|
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
|
|
|
#include "Luau/IrData.h"
|
2023-01-13 22:10:01 +00:00
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
namespace CodeGen
|
|
|
|
{
|
|
|
|
|
2023-02-17 23:41:51 +00:00
|
|
|
struct IrBuilder;
|
|
|
|
|
2023-01-13 22:10:01 +00:00
|
|
|
inline bool isJumpD(LuauOpcode op)
|
|
|
|
{
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case LOP_JUMP:
|
|
|
|
case LOP_JUMPIF:
|
|
|
|
case LOP_JUMPIFNOT:
|
|
|
|
case LOP_JUMPIFEQ:
|
|
|
|
case LOP_JUMPIFLE:
|
|
|
|
case LOP_JUMPIFLT:
|
|
|
|
case LOP_JUMPIFNOTEQ:
|
|
|
|
case LOP_JUMPIFNOTLE:
|
|
|
|
case LOP_JUMPIFNOTLT:
|
|
|
|
case LOP_FORNPREP:
|
|
|
|
case LOP_FORNLOOP:
|
|
|
|
case LOP_FORGPREP:
|
|
|
|
case LOP_FORGLOOP:
|
|
|
|
case LOP_FORGPREP_INEXT:
|
|
|
|
case LOP_FORGPREP_NEXT:
|
|
|
|
case LOP_JUMPBACK:
|
|
|
|
case LOP_JUMPXEQKNIL:
|
|
|
|
case LOP_JUMPXEQKB:
|
|
|
|
case LOP_JUMPXEQKN:
|
|
|
|
case LOP_JUMPXEQKS:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isSkipC(LuauOpcode op)
|
|
|
|
{
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case LOP_LOADB:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isFastCall(LuauOpcode op)
|
|
|
|
{
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case LOP_FASTCALL:
|
|
|
|
case LOP_FASTCALL1:
|
|
|
|
case LOP_FASTCALL2:
|
|
|
|
case LOP_FASTCALL2K:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int getJumpTarget(uint32_t insn, uint32_t pc)
|
|
|
|
{
|
|
|
|
LuauOpcode op = LuauOpcode(LUAU_INSN_OP(insn));
|
|
|
|
|
|
|
|
if (isJumpD(op))
|
|
|
|
return int(pc + LUAU_INSN_D(insn) + 1);
|
|
|
|
else if (isFastCall(op))
|
|
|
|
return int(pc + LUAU_INSN_C(insn) + 2);
|
|
|
|
else if (isSkipC(op) && LUAU_INSN_C(insn))
|
|
|
|
return int(pc + LUAU_INSN_C(insn) + 1);
|
|
|
|
else if (op == LOP_JUMPX)
|
|
|
|
return int(pc + LUAU_INSN_E(insn) + 1);
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isBlockTerminator(IrCmd cmd)
|
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case IrCmd::JUMP:
|
|
|
|
case IrCmd::JUMP_IF_TRUTHY:
|
|
|
|
case IrCmd::JUMP_IF_FALSY:
|
|
|
|
case IrCmd::JUMP_EQ_TAG:
|
2023-02-10 19:40:38 +00:00
|
|
|
case IrCmd::JUMP_EQ_INT:
|
2023-01-13 22:10:01 +00:00
|
|
|
case IrCmd::JUMP_EQ_POINTER:
|
|
|
|
case IrCmd::JUMP_CMP_NUM:
|
|
|
|
case IrCmd::JUMP_CMP_ANY:
|
2023-03-17 19:20:37 +00:00
|
|
|
case IrCmd::JUMP_SLOT_MATCH:
|
2023-03-31 19:42:49 +01:00
|
|
|
case IrCmd::RETURN:
|
|
|
|
case IrCmd::FORGLOOP:
|
|
|
|
case IrCmd::FORGLOOP_FALLBACK:
|
|
|
|
case IrCmd::FORGPREP_XNEXT_FALLBACK:
|
2023-01-13 22:10:01 +00:00
|
|
|
case IrCmd::FALLBACK_FORGPREP:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool hasResult(IrCmd cmd)
|
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case IrCmd::LOAD_TAG:
|
|
|
|
case IrCmd::LOAD_POINTER:
|
|
|
|
case IrCmd::LOAD_DOUBLE:
|
|
|
|
case IrCmd::LOAD_INT:
|
|
|
|
case IrCmd::LOAD_TVALUE:
|
|
|
|
case IrCmd::LOAD_NODE_VALUE_TV:
|
|
|
|
case IrCmd::LOAD_ENV:
|
|
|
|
case IrCmd::GET_ARR_ADDR:
|
|
|
|
case IrCmd::GET_SLOT_NODE_ADDR:
|
2023-03-17 19:20:37 +00:00
|
|
|
case IrCmd::GET_HASH_NODE_ADDR:
|
2023-01-13 22:10:01 +00:00
|
|
|
case IrCmd::ADD_INT:
|
|
|
|
case IrCmd::SUB_INT:
|
|
|
|
case IrCmd::ADD_NUM:
|
|
|
|
case IrCmd::SUB_NUM:
|
|
|
|
case IrCmd::MUL_NUM:
|
|
|
|
case IrCmd::DIV_NUM:
|
|
|
|
case IrCmd::MOD_NUM:
|
|
|
|
case IrCmd::POW_NUM:
|
2023-03-03 20:21:14 +00:00
|
|
|
case IrCmd::MIN_NUM:
|
|
|
|
case IrCmd::MAX_NUM:
|
2023-01-13 22:10:01 +00:00
|
|
|
case IrCmd::UNM_NUM:
|
2023-03-31 19:42:49 +01:00
|
|
|
case IrCmd::FLOOR_NUM:
|
|
|
|
case IrCmd::CEIL_NUM:
|
|
|
|
case IrCmd::ROUND_NUM:
|
|
|
|
case IrCmd::SQRT_NUM:
|
|
|
|
case IrCmd::ABS_NUM:
|
2023-01-13 22:10:01 +00:00
|
|
|
case IrCmd::NOT_ANY:
|
|
|
|
case IrCmd::TABLE_LEN:
|
|
|
|
case IrCmd::NEW_TABLE:
|
|
|
|
case IrCmd::DUP_TABLE:
|
2023-03-17 19:20:37 +00:00
|
|
|
case IrCmd::TRY_NUM_TO_INDEX:
|
|
|
|
case IrCmd::TRY_CALL_FASTGETTM:
|
2023-02-10 19:40:38 +00:00
|
|
|
case IrCmd::INT_TO_NUM:
|
2023-02-17 23:41:51 +00:00
|
|
|
case IrCmd::SUBSTITUTE:
|
2023-03-03 20:21:14 +00:00
|
|
|
case IrCmd::INVOKE_FASTCALL:
|
2023-01-13 22:10:01 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool hasSideEffects(IrCmd cmd)
|
|
|
|
{
|
2023-03-03 20:21:14 +00:00
|
|
|
if (cmd == IrCmd::INVOKE_FASTCALL)
|
|
|
|
return true;
|
|
|
|
|
2023-01-13 22:10:01 +00:00
|
|
|
// Instructions that don't produce a result most likely have other side-effects to make them useful
|
|
|
|
// Right now, a full switch would mirror the 'hasResult' function, so we use this simple condition
|
|
|
|
return !hasResult(cmd);
|
|
|
|
}
|
|
|
|
|
2023-02-17 23:41:51 +00:00
|
|
|
inline bool isPseudo(IrCmd cmd)
|
|
|
|
{
|
|
|
|
// Instructions that are used for internal needs and are not a part of final lowering
|
|
|
|
return cmd == IrCmd::NOP || cmd == IrCmd::SUBSTITUTE;
|
|
|
|
}
|
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
bool isGCO(uint8_t tag);
|
|
|
|
|
2023-03-03 20:21:14 +00:00
|
|
|
// Manually add or remove use of an operand
|
|
|
|
void addUse(IrFunction& function, IrOp op);
|
|
|
|
void removeUse(IrFunction& function, IrOp op);
|
|
|
|
|
2023-02-10 19:40:38 +00:00
|
|
|
// Remove a single instruction
|
|
|
|
void kill(IrFunction& function, IrInst& inst);
|
|
|
|
|
|
|
|
// Remove a range of instructions
|
|
|
|
void kill(IrFunction& function, uint32_t start, uint32_t end);
|
|
|
|
|
|
|
|
// Remove a block, including all instructions inside
|
|
|
|
void kill(IrFunction& function, IrBlock& block);
|
|
|
|
|
|
|
|
// Replace a single operand and update use counts (can cause chain removal of dead code)
|
|
|
|
void replace(IrFunction& function, IrOp& original, IrOp replacement);
|
|
|
|
|
|
|
|
// Replace a single instruction
|
|
|
|
// Target instruction index instead of reference is used to handle introduction of a new block terminator
|
2023-02-24 21:49:38 +00:00
|
|
|
void replace(IrFunction& function, IrBlock& block, uint32_t instIdx, IrInst replacement);
|
2023-02-10 19:40:38 +00:00
|
|
|
|
2023-02-17 23:41:51 +00:00
|
|
|
// Replace instruction with a different value (using IrCmd::SUBSTITUTE)
|
|
|
|
void substitute(IrFunction& function, IrInst& inst, IrOp replacement);
|
|
|
|
|
|
|
|
// Replace instruction arguments that point to substitutions with target values
|
|
|
|
void applySubstitutions(IrFunction& function, IrOp& op);
|
|
|
|
void applySubstitutions(IrFunction& function, IrInst& inst);
|
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
// Compare numbers using IR condition value
|
|
|
|
bool compare(double a, double b, IrCondition cond);
|
|
|
|
|
2023-02-17 23:41:51 +00:00
|
|
|
// Perform constant folding on instruction at index
|
|
|
|
// For most instructions, successful folding results in a IrCmd::SUBSTITUTE
|
|
|
|
// But it can also be successful on conditional control-flow, replacing it with an unconditional IrCmd::JUMP
|
2023-02-24 21:49:38 +00:00
|
|
|
void foldConstants(IrBuilder& build, IrFunction& function, IrBlock& block, uint32_t instIdx);
|
2023-02-17 23:41:51 +00:00
|
|
|
|
2023-01-13 22:10:01 +00:00
|
|
|
} // namespace CodeGen
|
|
|
|
} // namespace Luau
|