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/Bytecode.h"
|
|
|
|
#include "Luau/CodeAllocator.h"
|
2022-10-28 11:37:29 +01:00
|
|
|
#include "Luau/Label.h"
|
2022-10-14 20:48:41 +01:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2022-10-28 11:37:29 +01:00
|
|
|
#include "ldebug.h"
|
2022-10-14 20:48:41 +01:00
|
|
|
#include "lobject.h"
|
|
|
|
#include "ltm.h"
|
2022-10-28 11:37:29 +01:00
|
|
|
#include "lstate.h"
|
2022-10-14 20:48:41 +01:00
|
|
|
|
|
|
|
typedef int (*luau_FastFunction)(lua_State* L, StkId res, TValue* arg0, int nresults, StkId args, int nparams);
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
namespace CodeGen
|
|
|
|
{
|
|
|
|
|
|
|
|
class UnwindBuilder;
|
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
using FallbackFn = const Instruction* (*)(lua_State* L, const Instruction* pc, StkId base, TValue* k);
|
2022-10-14 20:48:41 +01:00
|
|
|
|
|
|
|
struct NativeProto
|
|
|
|
{
|
2023-04-28 20:55:13 +01:00
|
|
|
// This array is stored before NativeProto in reverse order, so to get offset of instruction i you need to index instOffsets[-i]
|
|
|
|
// This awkward layout is helpful for maximally efficient address computation on X64/A64
|
|
|
|
uint32_t instOffsets[1];
|
2022-10-14 20:48:41 +01:00
|
|
|
|
2023-04-28 20:55:13 +01:00
|
|
|
uintptr_t instBase = 0;
|
|
|
|
uintptr_t entryTarget = 0; // = instOffsets[0] + instBase
|
2022-10-14 20:48:41 +01:00
|
|
|
Proto* proto = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NativeContext
|
|
|
|
{
|
|
|
|
// Gateway (C => native transition) entry & exit, compiled at runtime
|
|
|
|
uint8_t* gateEntry = nullptr;
|
|
|
|
uint8_t* gateExit = nullptr;
|
|
|
|
|
|
|
|
// Helper functions, implemented in C
|
|
|
|
int (*luaV_lessthan)(lua_State* L, const TValue* l, const TValue* r) = nullptr;
|
|
|
|
int (*luaV_lessequal)(lua_State* L, const TValue* l, const TValue* r) = nullptr;
|
|
|
|
int (*luaV_equalval)(lua_State* L, const TValue* t1, const TValue* t2) = nullptr;
|
|
|
|
void (*luaV_doarith)(lua_State* L, StkId ra, const TValue* rb, const TValue* rc, TMS op) = nullptr;
|
|
|
|
void (*luaV_dolen)(lua_State* L, StkId ra, const TValue* rb) = nullptr;
|
|
|
|
void (*luaV_prepareFORN)(lua_State* L, StkId plimit, StkId pstep, StkId pinit) = nullptr;
|
|
|
|
void (*luaV_gettable)(lua_State* L, const TValue* t, TValue* key, StkId val) = nullptr;
|
|
|
|
void (*luaV_settable)(lua_State* L, const TValue* t, TValue* key, StkId val) = nullptr;
|
2022-10-21 18:54:01 +01:00
|
|
|
void (*luaV_getimport)(lua_State* L, Table* env, TValue* k, uint32_t id, bool propagatenil) = nullptr;
|
|
|
|
void (*luaV_concat)(lua_State* L, int total, int last) = nullptr;
|
2022-10-14 20:48:41 +01:00
|
|
|
|
|
|
|
int (*luaH_getn)(Table* t) = nullptr;
|
2022-10-21 18:54:01 +01:00
|
|
|
Table* (*luaH_new)(lua_State* L, int narray, int lnhash) = nullptr;
|
|
|
|
Table* (*luaH_clone)(lua_State* L, Table* tt) = nullptr;
|
|
|
|
void (*luaH_resizearray)(lua_State* L, Table* t, int nasize) = nullptr;
|
2022-10-14 20:48:41 +01:00
|
|
|
|
|
|
|
void (*luaC_barriertable)(lua_State* L, Table* t, GCObject* v) = nullptr;
|
2022-10-21 18:54:01 +01:00
|
|
|
void (*luaC_barrierf)(lua_State* L, GCObject* o, GCObject* v) = nullptr;
|
|
|
|
void (*luaC_barrierback)(lua_State* L, GCObject* o, GCObject** gclist) = nullptr;
|
|
|
|
size_t (*luaC_step)(lua_State* L, bool assist) = nullptr;
|
|
|
|
|
|
|
|
void (*luaF_close)(lua_State* L, StkId level) = nullptr;
|
2022-10-14 20:48:41 +01:00
|
|
|
|
2023-01-13 22:10:01 +00:00
|
|
|
const TValue* (*luaT_gettm)(Table* events, TMS event, TString* ename) = nullptr;
|
2023-03-17 19:20:37 +00:00
|
|
|
const TString* (*luaT_objtypenamestr)(lua_State* L, const TValue* o) = nullptr;
|
2023-01-13 22:10:01 +00:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
double (*libm_exp)(double) = nullptr;
|
2022-10-14 20:48:41 +01:00
|
|
|
double (*libm_pow)(double, double) = nullptr;
|
2023-01-04 20:53:17 +00:00
|
|
|
double (*libm_fmod)(double, double) = nullptr;
|
|
|
|
double (*libm_asin)(double) = nullptr;
|
|
|
|
double (*libm_sin)(double) = nullptr;
|
|
|
|
double (*libm_sinh)(double) = nullptr;
|
|
|
|
double (*libm_acos)(double) = nullptr;
|
|
|
|
double (*libm_cos)(double) = nullptr;
|
|
|
|
double (*libm_cosh)(double) = nullptr;
|
|
|
|
double (*libm_atan)(double) = nullptr;
|
|
|
|
double (*libm_atan2)(double, double) = nullptr;
|
|
|
|
double (*libm_tan)(double) = nullptr;
|
|
|
|
double (*libm_tanh)(double) = nullptr;
|
|
|
|
double (*libm_log)(double) = nullptr;
|
|
|
|
double (*libm_log2)(double) = nullptr;
|
|
|
|
double (*libm_log10)(double) = nullptr;
|
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
|
|
|
double (*libm_ldexp)(double, int) = nullptr;
|
|
|
|
double (*libm_round)(double) = nullptr;
|
|
|
|
double (*libm_frexp)(double, int*) = nullptr;
|
|
|
|
double (*libm_modf)(double, double*) = nullptr;
|
2022-10-28 11:37:29 +01:00
|
|
|
|
|
|
|
// Helper functions
|
2023-04-14 19:06:22 +01:00
|
|
|
bool (*forgLoopTableIter)(lua_State* L, Table* h, int index, TValue* ra) = nullptr;
|
2022-10-28 11:37:29 +01:00
|
|
|
bool (*forgLoopNodeIter)(lua_State* L, Table* h, int index, TValue* ra) = nullptr;
|
|
|
|
bool (*forgLoopNonTableFallback)(lua_State* L, int insnA, int aux) = nullptr;
|
|
|
|
void (*forgPrepXnextFallback)(lua_State* L, TValue* ra, int pc) = nullptr;
|
2022-11-04 17:33:22 +00:00
|
|
|
Closure* (*callProlog)(lua_State* L, TValue* ra, StkId argtop, int nresults) = nullptr;
|
|
|
|
void (*callEpilogC)(lua_State* L, int nresults, int n) = nullptr;
|
2023-03-31 19:42:49 +01:00
|
|
|
|
|
|
|
Closure* (*callFallback)(lua_State* L, StkId ra, StkId argtop, int nresults) = nullptr;
|
2023-04-21 23:14:26 +01:00
|
|
|
Closure* (*returnFallback)(lua_State* L, StkId ra, StkId valend) = nullptr;
|
2023-03-24 18:03:04 +00:00
|
|
|
|
|
|
|
// Opcode fallbacks, implemented in C
|
2023-04-14 19:06:22 +01:00
|
|
|
FallbackFn fallback[LOP__COUNT] = {};
|
2023-03-24 18:03:04 +00:00
|
|
|
|
|
|
|
// Fast call methods, implemented in C
|
|
|
|
luau_FastFunction luauF_table[256] = {};
|
2022-10-14 20:48:41 +01:00
|
|
|
};
|
|
|
|
|
2023-05-12 18:50:47 +01:00
|
|
|
using GateFn = int (*)(lua_State*, Proto*, uintptr_t, NativeContext*);
|
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
struct NativeState
|
|
|
|
{
|
|
|
|
NativeState();
|
|
|
|
~NativeState();
|
|
|
|
|
|
|
|
CodeAllocator codeAllocator;
|
|
|
|
std::unique_ptr<UnwindBuilder> unwindBuilder;
|
|
|
|
|
|
|
|
uint8_t* gateData = nullptr;
|
|
|
|
size_t gateDataSize = 0;
|
|
|
|
|
|
|
|
NativeContext context;
|
|
|
|
};
|
|
|
|
|
|
|
|
void initFallbackTable(NativeState& data);
|
|
|
|
void initHelperFunctions(NativeState& data);
|
|
|
|
|
|
|
|
} // namespace CodeGen
|
|
|
|
} // namespace Luau
|