2023-01-21 04:27:03 +08: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"
|
2023-03-18 03:20:37 +08:00
|
|
|
#include "Luau/Common.h"
|
|
|
|
#include "Luau/DenseHash.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-04 03:26:13 +08:00
|
|
|
#include "Luau/IrData.h"
|
2023-01-21 04:27:03 +08:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
struct Proto;
|
|
|
|
typedef uint32_t Instruction;
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
namespace CodeGen
|
|
|
|
{
|
|
|
|
|
|
|
|
struct AssemblyOptions;
|
|
|
|
|
|
|
|
struct IrBuilder
|
|
|
|
{
|
2023-03-18 03:20:37 +08:00
|
|
|
IrBuilder();
|
|
|
|
|
2023-01-21 04:27:03 +08:00
|
|
|
void buildFunctionIr(Proto* proto);
|
|
|
|
|
|
|
|
void rebuildBytecodeBasicBlocks(Proto* proto);
|
|
|
|
void translateInst(LuauOpcode op, const Instruction* pc, int i);
|
2023-07-15 02:08:53 +08:00
|
|
|
void handleFastcallFallback(IrOp fallbackOrUndef, const Instruction* pc, int i);
|
2023-01-21 04:27:03 +08:00
|
|
|
|
|
|
|
bool isInternalBlock(IrOp block);
|
|
|
|
void beginBlock(IrOp block);
|
|
|
|
|
2023-03-04 04:21:14 +08:00
|
|
|
void loadAndCheckTag(IrOp loc, uint8_t tag, IrOp fallback);
|
|
|
|
|
|
|
|
// Clones all instructions into the current block
|
|
|
|
// Source block that is cloned cannot use values coming in from a predecessor
|
|
|
|
void clone(const IrBlock& source, bool removeCurrentTerminator);
|
|
|
|
|
2023-04-29 03:55:13 +08:00
|
|
|
IrOp undef();
|
|
|
|
|
2023-01-21 04:27:03 +08:00
|
|
|
IrOp constInt(int value);
|
|
|
|
IrOp constUint(unsigned value);
|
|
|
|
IrOp constDouble(double value);
|
|
|
|
IrOp constTag(uint8_t value);
|
2023-03-18 03:20:37 +08:00
|
|
|
IrOp constAny(IrConst constant, uint64_t asCommonKey);
|
2023-01-21 04:27:03 +08:00
|
|
|
|
|
|
|
IrOp cond(IrCondition cond);
|
|
|
|
|
|
|
|
IrOp inst(IrCmd cmd);
|
|
|
|
IrOp inst(IrCmd cmd, IrOp a);
|
|
|
|
IrOp inst(IrCmd cmd, IrOp a, IrOp b);
|
|
|
|
IrOp inst(IrCmd cmd, IrOp a, IrOp b, IrOp c);
|
|
|
|
IrOp inst(IrCmd cmd, IrOp a, IrOp b, IrOp c, IrOp d);
|
|
|
|
IrOp inst(IrCmd cmd, IrOp a, IrOp b, IrOp c, IrOp d, IrOp e);
|
2023-02-25 05:49:38 +08:00
|
|
|
IrOp inst(IrCmd cmd, IrOp a, IrOp b, IrOp c, IrOp d, IrOp e, IrOp f);
|
2023-01-21 04:27:03 +08:00
|
|
|
|
|
|
|
IrOp block(IrBlockKind kind); // Requested kind can be ignored if we are in an outlined sequence
|
|
|
|
IrOp blockAtInst(uint32_t index);
|
|
|
|
|
|
|
|
IrOp vmReg(uint8_t index);
|
|
|
|
IrOp vmConst(uint32_t index);
|
|
|
|
IrOp vmUpvalue(uint8_t index);
|
|
|
|
|
2023-07-15 02:08:53 +08:00
|
|
|
IrOp vmExit(uint32_t pcpos);
|
|
|
|
|
2023-02-11 03:40:38 +08:00
|
|
|
bool inTerminatedBlock = false;
|
|
|
|
|
2023-09-08 08:13:49 +08:00
|
|
|
bool interruptRequested = false;
|
|
|
|
|
2023-01-21 04:27:03 +08:00
|
|
|
bool activeFastcallFallback = false;
|
|
|
|
IrOp fastcallFallbackReturn;
|
2023-07-15 02:08:53 +08:00
|
|
|
int fastcallSkipTarget = -1;
|
2023-01-21 04:27:03 +08:00
|
|
|
|
|
|
|
IrFunction function;
|
|
|
|
|
2023-02-25 05:49:38 +08:00
|
|
|
uint32_t activeBlockIdx = ~0u;
|
|
|
|
|
2023-01-21 04:27:03 +08:00
|
|
|
std::vector<uint32_t> instIndexToBlock; // Block index at the bytecode instruction
|
2023-03-18 03:20:37 +08:00
|
|
|
|
2023-12-02 15:46:57 +08:00
|
|
|
struct LoopInfo
|
|
|
|
{
|
|
|
|
IrOp step;
|
|
|
|
int startpc = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<LoopInfo> numericLoopStack;
|
2023-09-08 08:13:49 +08:00
|
|
|
|
2023-03-18 03:20:37 +08:00
|
|
|
// Similar to BytecodeBuilder, duplicate constants are removed used the same method
|
|
|
|
struct ConstantKey
|
|
|
|
{
|
|
|
|
IrConstKind kind;
|
|
|
|
// Note: this stores value* from IrConst; when kind is Double, this stores the same bits as double does but in uint64_t.
|
|
|
|
uint64_t value;
|
|
|
|
|
|
|
|
bool operator==(const ConstantKey& key) const
|
|
|
|
{
|
|
|
|
return kind == key.kind && value == key.value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ConstantKeyHash
|
|
|
|
{
|
|
|
|
size_t operator()(const ConstantKey& key) const
|
|
|
|
{
|
|
|
|
// finalizer from MurmurHash64B
|
|
|
|
const uint32_t m = 0x5bd1e995;
|
|
|
|
|
|
|
|
uint32_t h1 = uint32_t(key.value);
|
|
|
|
uint32_t h2 = uint32_t(key.value >> 32) ^ (int(key.kind) * m);
|
|
|
|
|
|
|
|
h1 ^= h2 >> 18;
|
|
|
|
h1 *= m;
|
|
|
|
h2 ^= h1 >> 22;
|
|
|
|
h2 *= m;
|
|
|
|
h1 ^= h2 >> 17;
|
|
|
|
h1 *= m;
|
|
|
|
h2 ^= h1 >> 19;
|
|
|
|
h2 *= m;
|
|
|
|
|
|
|
|
// ... truncated to 32-bit output (normally hash is equal to (uint64_t(h1) << 32) | h2, but we only really need the lower 32-bit half)
|
|
|
|
return size_t(h2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DenseHashMap<ConstantKey, uint32_t, ConstantKeyHash> constantMap;
|
2023-01-21 04:27:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace CodeGen
|
|
|
|
} // namespace Luau
|