Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/CodeGen/src/NativeState.h
2725 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#pragma once
3
4
#include "Luau/Bytecode.h"
5
#include "Luau/CodeAllocator.h"
6
#include "Luau/Label.h"
7
8
#include <memory>
9
10
#include <stdint.h>
11
12
#include "ldebug.h"
13
#include "lobject.h"
14
#include "ltm.h"
15
#include "lstate.h"
16
17
typedef int (*luau_FastFunction)(lua_State* L, StkId res, TValue* arg0, int nresults, StkId args, int nparams);
18
19
namespace Luau
20
{
21
namespace CodeGen
22
{
23
24
class UnwindBuilder;
25
26
struct NativeContext
27
{
28
// Gateway (C => native transition) entry & exit, compiled at runtime
29
uint8_t* gateEntry = nullptr;
30
uint8_t* gateExit = nullptr;
31
32
// Helper functions, implemented in C
33
int (*luaV_lessthan)(lua_State* L, const TValue* l, const TValue* r) = nullptr;
34
int (*luaV_lessequal)(lua_State* L, const TValue* l, const TValue* r) = nullptr;
35
int (*luaV_equalval)(lua_State* L, const TValue* t1, const TValue* t2) = nullptr;
36
void (*luaV_doarithadd)(lua_State* L, StkId ra, const TValue* rb, const TValue* rc) = nullptr;
37
void (*luaV_doarithsub)(lua_State* L, StkId ra, const TValue* rb, const TValue* rc) = nullptr;
38
void (*luaV_doarithmul)(lua_State* L, StkId ra, const TValue* rb, const TValue* rc) = nullptr;
39
void (*luaV_doarithdiv)(lua_State* L, StkId ra, const TValue* rb, const TValue* rc) = nullptr;
40
void (*luaV_doarithidiv)(lua_State* L, StkId ra, const TValue* rb, const TValue* rc) = nullptr;
41
void (*luaV_doarithmod)(lua_State* L, StkId ra, const TValue* rb, const TValue* rc) = nullptr;
42
void (*luaV_doarithpow)(lua_State* L, StkId ra, const TValue* rb, const TValue* rc) = nullptr;
43
void (*luaV_doarithunm)(lua_State* L, StkId ra, const TValue* rb, const TValue* rc) = nullptr;
44
void (*luaV_dolen)(lua_State* L, StkId ra, const TValue* rb) = nullptr;
45
void (*luaV_gettable)(lua_State* L, const TValue* t, TValue* key, StkId val) = nullptr;
46
void (*luaV_settable)(lua_State* L, const TValue* t, TValue* key, StkId val) = nullptr;
47
void (*luaV_concat)(lua_State* L, int total, int last) = nullptr;
48
49
int (*luaH_getn)(LuaTable* t) = nullptr;
50
LuaTable* (*luaH_new)(lua_State* L, int narray, int lnhash) = nullptr;
51
LuaTable* (*luaH_clone)(lua_State* L, LuaTable* tt) = nullptr;
52
void (*luaH_resizearray)(lua_State* L, LuaTable* t, int nasize) = nullptr;
53
TValue* (*luaH_setnum)(lua_State* L, LuaTable* t, int key);
54
55
void (*luaC_barriertable)(lua_State* L, LuaTable* t, GCObject* v) = nullptr;
56
void (*luaC_barrierf)(lua_State* L, GCObject* o, GCObject* v) = nullptr;
57
void (*luaC_barrierback)(lua_State* L, GCObject* o, GCObject** gclist) = nullptr;
58
size_t (*luaC_step)(lua_State* L, bool assist) = nullptr;
59
60
void (*luaF_close)(lua_State* L, StkId level) = nullptr;
61
UpVal* (*luaF_findupval)(lua_State* L, StkId level) = nullptr;
62
Closure* (*luaF_newLclosure)(lua_State* L, int nelems, LuaTable* e, Proto* p) = nullptr;
63
64
const TValue* (*luaT_gettm)(LuaTable* events, TMS event, TString* ename) = nullptr;
65
const TString* (*luaT_objtypenamestr)(lua_State* L, const TValue* o) = nullptr;
66
67
double (*libm_exp)(double) = nullptr;
68
double (*libm_pow)(double, double) = nullptr;
69
double (*libm_fmod)(double, double) = nullptr;
70
double (*libm_asin)(double) = nullptr;
71
double (*libm_sin)(double) = nullptr;
72
double (*libm_sinh)(double) = nullptr;
73
double (*libm_acos)(double) = nullptr;
74
double (*libm_cos)(double) = nullptr;
75
double (*libm_cosh)(double) = nullptr;
76
double (*libm_atan)(double) = nullptr;
77
double (*libm_atan2)(double, double) = nullptr;
78
double (*libm_tan)(double) = nullptr;
79
double (*libm_tanh)(double) = nullptr;
80
double (*libm_log)(double) = nullptr;
81
double (*libm_log2)(double) = nullptr;
82
double (*libm_log10)(double) = nullptr;
83
double (*libm_ldexp)(double, int) = nullptr;
84
double (*libm_round)(double) = nullptr;
85
double (*libm_frexp)(double, int*) = nullptr;
86
double (*libm_modf)(double, double*) = nullptr;
87
88
// Helper functions
89
bool (*forgLoopTableIter)(lua_State* L, LuaTable* h, int index, TValue* ra) = nullptr;
90
bool (*forgLoopNodeIter)(lua_State* L, LuaTable* h, int index, TValue* ra) = nullptr;
91
bool (*forgLoopNonTableFallback)(lua_State* L, int insnA, int aux) = nullptr;
92
void (*forgPrepXnextFallback)(lua_State* L, TValue* ra, int pc) = nullptr;
93
Closure* (*callProlog)(lua_State* L, TValue* ra, StkId argtop, int nresults) = nullptr;
94
void (*callEpilogC)(lua_State* L, int nresults, int n) = nullptr;
95
Udata* (*newUserdata)(lua_State* L, size_t s, int tag) = nullptr;
96
void (*getImport)(lua_State* L, StkId res, unsigned id, unsigned pc) = nullptr;
97
98
Closure* (*callFallback)(lua_State* L, StkId ra, StkId argtop, int nresults) = nullptr;
99
100
// Opcode fallbacks, implemented in C
101
const Instruction* (*executeGETGLOBAL)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
102
const Instruction* (*executeSETGLOBAL)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
103
const Instruction* (*executeGETTABLEKS)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
104
const Instruction* (*executeSETTABLEKS)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
105
const Instruction* (*executeNAMECALL)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
106
const Instruction* (*executeSETLIST)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
107
const Instruction* (*executeFORGPREP)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
108
void (*executeGETVARARGSMultRet)(lua_State* L, const Instruction* pc, StkId base, int rai) = nullptr;
109
void (*executeGETVARARGSConst)(lua_State* L, StkId base, int rai, int b) = nullptr;
110
const Instruction* (*executeDUPCLOSURE)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
111
const Instruction* (*executePREPVARARGS)(lua_State* L, const Instruction* pc, StkId base, TValue* k) = nullptr;
112
113
// Fast call methods, implemented in C
114
luau_FastFunction luauF_table[256] = {};
115
};
116
117
using GateFn = int (*)(lua_State*, Proto*, uintptr_t, NativeContext*);
118
119
void initFunctions(NativeContext& context);
120
121
} // namespace CodeGen
122
} // namespace Luau
123
124