Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/CodeGen/include/Luau/CodeGen.h
2727 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/CodeGenCommon.h"
5
#include "Luau/CodeGenOptions.h"
6
#include "Luau/LoweringStats.h"
7
8
#include <array>
9
#include <memory>
10
#include <string>
11
#include <vector>
12
13
#include <stddef.h>
14
#include <stdint.h>
15
16
struct lua_State;
17
18
namespace Luau
19
{
20
namespace CodeGen
21
{
22
23
// These enum values can be reported through telemetry.
24
// To ensure consistency, changes should be additive.
25
enum class CodeGenCompilationResult
26
{
27
Success = 0, // Successfully generated code for at least one function
28
NothingToCompile = 1, // There were no new functions to compile
29
NotNativeModule = 2, // Module does not have `--!native` comment
30
31
CodeGenNotInitialized = 3, // Native codegen system is not initialized
32
CodeGenOverflowInstructionLimit = 4, // Instruction limit overflow
33
CodeGenOverflowBlockLimit = 5, // Block limit overflow
34
CodeGenOverflowBlockInstructionLimit = 6, // Block instruction limit overflow
35
CodeGenAssemblerFinalizationFailure = 7, // Failure during assembler finalization
36
CodeGenLoweringFailure = 8, // Lowering failed
37
AllocationFailed = 9, // Native codegen failed due to an allocation error
38
39
Count = 10,
40
};
41
42
std::string toString(const CodeGenCompilationResult& result);
43
44
struct ProtoCompilationFailure
45
{
46
CodeGenCompilationResult result = CodeGenCompilationResult::Success;
47
48
std::string debugname;
49
int line = -1;
50
};
51
52
struct CompilationResult
53
{
54
CodeGenCompilationResult result = CodeGenCompilationResult::Success;
55
56
std::vector<ProtoCompilationFailure> protoFailures;
57
58
[[nodiscard]] bool hasErrors() const
59
{
60
return result != CodeGenCompilationResult::Success || !protoFailures.empty();
61
}
62
};
63
64
struct CompilationStats
65
{
66
size_t bytecodeSizeBytes = 0;
67
size_t nativeCodeSizeBytes = 0;
68
size_t nativeDataSizeBytes = 0;
69
size_t nativeMetadataSizeBytes = 0;
70
71
uint32_t functionsTotal = 0;
72
uint32_t functionsCompiled = 0;
73
uint32_t functionsBound = 0;
74
};
75
76
bool isSupported();
77
78
class SharedCodeGenContext;
79
80
struct SharedCodeGenContextDeleter
81
{
82
void operator()(const SharedCodeGenContext* context) const noexcept;
83
};
84
85
using UniqueSharedCodeGenContext = std::unique_ptr<SharedCodeGenContext, SharedCodeGenContextDeleter>;
86
87
// Creates a new SharedCodeGenContext that can be used by multiple Luau VMs
88
// concurrently, using either the default allocator parameters or custom
89
// allocator parameters.
90
[[nodiscard]] UniqueSharedCodeGenContext createSharedCodeGenContext();
91
92
[[nodiscard]] UniqueSharedCodeGenContext createSharedCodeGenContext(AllocationCallback* allocationCallback, void* allocationCallbackContext);
93
94
[[nodiscard]] UniqueSharedCodeGenContext createSharedCodeGenContext(
95
size_t blockSize,
96
size_t maxTotalSize,
97
AllocationCallback* allocationCallback,
98
void* allocationCallbackContext
99
);
100
101
// Destroys the provided SharedCodeGenContext. All Luau VMs using the
102
// SharedCodeGenContext must be destroyed before this function is called.
103
void destroySharedCodeGenContext(const SharedCodeGenContext* codeGenContext) noexcept;
104
105
// Initializes native code-gen on the provided Luau VM, using a VM-specific
106
// code-gen context and either the default allocator parameters or custom
107
// allocator parameters.
108
void create(lua_State* L);
109
void create(lua_State* L, AllocationCallback* allocationCallback, void* allocationCallbackContext);
110
void create(lua_State* L, size_t blockSize, size_t maxTotalSize, AllocationCallback* allocationCallback, void* allocationCallbackContext);
111
112
// Initializes native code-gen on the provided Luau VM, using the provided
113
// SharedCodeGenContext. Note that after this function is called, the
114
// SharedCodeGenContext must not be destroyed until after the Luau VM L is
115
// destroyed via lua_close.
116
void create(lua_State* L, SharedCodeGenContext* codeGenContext);
117
118
// Check if native execution is enabled
119
[[nodiscard]] bool isNativeExecutionEnabled(lua_State* L);
120
121
// Enable or disable native execution according to `enabled` argument
122
void setNativeExecutionEnabled(lua_State* L, bool enabled);
123
124
void disableNativeExecutionForFunction(lua_State* L, const int level) noexcept;
125
126
// Given a name, this function must return the index of the type which matches the type array used all CompilationOptions and AssemblyOptions
127
// If the type is unknown, 0xff has to be returned
128
using UserdataRemapperCallback = uint8_t(void* context, const char* name, size_t nameLength);
129
130
void setUserdataRemapper(lua_State* L, void* context, UserdataRemapperCallback cb);
131
132
using ModuleId = std::array<uint8_t, 16>;
133
134
// Builds target function and all inner functions
135
CompilationResult compile(lua_State* L, int idx, unsigned int flags, CompilationStats* stats = nullptr);
136
CompilationResult compile(const ModuleId& moduleId, lua_State* L, int idx, unsigned int flags, CompilationStats* stats = nullptr);
137
138
CompilationResult compile(lua_State* L, int idx, const CompilationOptions& options, CompilationStats* stats = nullptr);
139
CompilationResult compile(const ModuleId& moduleId, lua_State* L, int idx, const CompilationOptions& options, CompilationStats* stats = nullptr);
140
141
// Generates assembly for target function and all inner functions
142
std::string getAssembly(lua_State* L, int idx, AssemblyOptions options = {}, LoweringStats* stats = nullptr);
143
144
using PerfLogFn = void (*)(void* context, uintptr_t addr, unsigned size, const char* symbol);
145
146
void setPerfLog(void* context, PerfLogFn logFn);
147
148
} // namespace CodeGen
149
} // namespace Luau
150
151