luau/CodeGen/src/NativeState.h

131 lines
5.8 KiB
C
Raw Normal View History

2022-10-13 23:59:53 +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-27 23:22:49 +01:00
#include "Luau/Label.h"
2022-10-13 23:59:53 +01:00
#include <memory>
#include <stdint.h>
2022-10-27 23:22:49 +01:00
#include "ldebug.h"
2022-10-13 23:59:53 +01:00
#include "lobject.h"
#include "ltm.h"
2022-10-27 23:22:49 +01:00
#include "lstate.h"
2022-10-13 23:59:53 +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;
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_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;
2023-05-25 21:46:51 +01:00
void (*luaV_getimport)(lua_State* L, Table* env, TValue* k, StkId res, uint32_t id, bool propagatenil) = nullptr;
2022-10-21 18:33:43 +01:00
void (*luaV_concat)(lua_State* L, int total, int last) = nullptr;
2022-10-13 23:59:53 +01:00
int (*luaH_getn)(Table* t) = nullptr;
2022-10-21 18:33:43 +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;
2023-09-01 17:38:53 +01:00
TValue* (*luaH_setnum)(lua_State* L, Table* t, int key);
2022-10-13 23:59:53 +01:00
void (*luaC_barriertable)(lua_State* L, Table* t, GCObject* v) = nullptr;
2022-10-21 18:33:43 +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;
2023-07-28 12:37:00 +01:00
UpVal* (*luaF_findupval)(lua_State* L, StkId level) = nullptr;
Closure* (*luaF_newLclosure)(lua_State* L, int nelems, Table* e, Proto* p) = nullptr;
2022-10-13 23:59:53 +01:00
2023-01-13 20:36:28 +00:00
const TValue* (*luaT_gettm)(Table* events, TMS event, TString* ename) = nullptr;
2023-03-17 14:59:30 +00:00
const TString* (*luaT_objtypenamestr)(lua_State* L, const TValue* o) = nullptr;
2023-01-13 20:36:28 +00:00
2023-01-03 17:33:19 +00:00
double (*libm_exp)(double) = nullptr;
2022-10-13 23:59:53 +01:00
double (*libm_pow)(double, double) = nullptr;
2023-01-03 17:33:19 +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;
2023-02-03 12:34:12 +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-27 23:22:49 +01:00
// Helper functions
2023-04-14 13:05:27 +01:00
bool (*forgLoopTableIter)(lua_State* L, Table* h, int index, TValue* ra) = nullptr;
2022-10-27 23:22:49 +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:02:37 +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 13:21:14 +01:00
Closure* (*callFallback)(lua_State* L, StkId ra, StkId argtop, int nresults) = nullptr;
2023-03-24 17:34:14 +00:00
// Opcode fallbacks, implemented in C
2023-05-19 19:59:59 +01:00
const Instruction* (*executeGETGLOBAL)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
const Instruction* (*executeSETGLOBAL)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
const Instruction* (*executeGETTABLEKS)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
const Instruction* (*executeSETTABLEKS)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
const Instruction* (*executeNAMECALL)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
const Instruction* (*executeSETLIST)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
const Instruction* (*executeFORGPREP)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
void (*executeGETVARARGSMultRet)(lua_State* L, const Instruction* pc, StkId base, int rai) = nullptr;
void (*executeGETVARARGSConst)(lua_State* L, StkId base, int rai, int b) = nullptr;
2023-05-19 19:59:59 +01:00
const Instruction* (*executeDUPCLOSURE)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
const Instruction* (*executePREPVARARGS)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
2023-03-24 17:34:14 +00:00
// Fast call methods, implemented in C
luau_FastFunction luauF_table[256] = {};
2022-10-13 23:59:53 +01:00
};
2023-05-12 13:15:01 +01:00
using GateFn = int (*)(lua_State*, Proto*, uintptr_t, NativeContext*);
2022-10-13 23:59:53 +01:00
struct NativeState
{
NativeState();
2023-08-11 13:55:30 +01:00
NativeState(AllocationCallback* allocationCallback, void* allocationCallbackContext);
2022-10-13 23:59:53 +01:00
~NativeState();
CodeAllocator codeAllocator;
std::unique_ptr<UnwindBuilder> unwindBuilder;
uint8_t* gateData = nullptr;
size_t gateDataSize = 0;
NativeContext context;
};
2023-05-19 19:59:59 +01:00
void initFunctions(NativeState& data);
2024-03-30 22:49:03 +00:00
void initFunctions(NativeContext& context);
2022-10-13 23:59:53 +01:00
} // namespace CodeGen
} // namespace Luau