Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/CodeGen/include/Luau/CodeGenOptions.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 <string>
5
6
#include <stddef.h>
7
#include <stdint.h>
8
9
namespace Luau
10
{
11
namespace CodeGen
12
{
13
14
enum CodeGenFlags
15
{
16
// Only run native codegen for modules that have been marked with --!native
17
CodeGen_OnlyNativeModules = 1 << 0,
18
// Run native codegen for functions that the compiler considers not profitable
19
CodeGen_ColdFunctions = 1 << 1,
20
};
21
22
enum class CodeGenCounter : unsigned
23
{
24
RegularBlockExecuted = 1,
25
FallbackBlockExecuted = 2,
26
VmExitTaken = 3,
27
};
28
29
using AllocationCallback = void(void* context, void* oldPointer, size_t oldSize, void* newPointer, size_t newSize);
30
31
struct IrBuilder;
32
struct IrOp;
33
34
using HostVectorOperationBytecodeType = uint8_t (*)(const char* member, size_t memberLength);
35
using HostVectorAccessHandler = bool (*)(IrBuilder& builder, const char* member, size_t memberLength, int resultReg, int sourceReg, int pcpos);
36
using HostVectorNamecallHandler =
37
bool (*)(IrBuilder& builder, const char* member, size_t memberLength, int argResReg, int sourceReg, int params, int results, int pcpos);
38
39
enum class HostMetamethod
40
{
41
Add,
42
Sub,
43
Mul,
44
Div,
45
Idiv,
46
Mod,
47
Pow,
48
Minus,
49
Equal,
50
LessThan,
51
LessEqual,
52
Length,
53
Concat,
54
};
55
56
using HostUserdataOperationBytecodeType = uint8_t (*)(uint8_t type, const char* member, size_t memberLength);
57
using HostUserdataMetamethodBytecodeType = uint8_t (*)(uint8_t lhsTy, uint8_t rhsTy, HostMetamethod method);
58
using HostUserdataAccessHandler =
59
bool (*)(IrBuilder& builder, uint8_t type, const char* member, size_t memberLength, int resultReg, int sourceReg, int pcpos);
60
using HostUserdataMetamethodHandler =
61
bool (*)(IrBuilder& builder, uint8_t lhsTy, uint8_t rhsTy, int resultReg, IrOp lhs, IrOp rhs, HostMetamethod method, int pcpos);
62
using HostUserdataNamecallHandler = bool (*)(
63
IrBuilder& builder,
64
uint8_t type,
65
const char* member,
66
size_t memberLength,
67
int argResReg,
68
int sourceReg,
69
int params,
70
int results,
71
int pcpos
72
);
73
74
struct HostIrHooks
75
{
76
// Suggest result type of a vector field access
77
HostVectorOperationBytecodeType vectorAccessBytecodeType = nullptr;
78
79
// Suggest result type of a vector function namecall
80
HostVectorOperationBytecodeType vectorNamecallBytecodeType = nullptr;
81
82
// Handle vector value field access
83
// 'sourceReg' is guaranteed to be a vector
84
// Guards should take a VM exit to 'pcpos'
85
HostVectorAccessHandler vectorAccess = nullptr;
86
87
// Handle namecall performed on a vector value
88
// 'sourceReg' (self argument) is guaranteed to be a vector
89
// All other arguments can be of any type
90
// Guards should take a VM exit to 'pcpos'
91
HostVectorNamecallHandler vectorNamecall = nullptr;
92
93
// Suggest result type of a userdata field access
94
HostUserdataOperationBytecodeType userdataAccessBytecodeType = nullptr;
95
96
// Suggest result type of a metamethod call
97
HostUserdataMetamethodBytecodeType userdataMetamethodBytecodeType = nullptr;
98
99
// Suggest result type of a userdata namecall
100
HostUserdataOperationBytecodeType userdataNamecallBytecodeType = nullptr;
101
102
// Handle userdata value field access
103
// 'sourceReg' is guaranteed to be a userdata, but tag has to be checked
104
// Write to 'resultReg' might invalidate 'sourceReg'
105
// Guards should take a VM exit to 'pcpos'
106
HostUserdataAccessHandler userdataAccess = nullptr;
107
108
// Handle metamethod operation on a userdata value
109
// 'lhs' and 'rhs' operands can be VM registers of constants
110
// Operand types have to be checked and userdata operand tags have to be checked
111
// Write to 'resultReg' might invalidate source operands
112
// Guards should take a VM exit to 'pcpos'
113
HostUserdataMetamethodHandler userdataMetamethod = nullptr;
114
115
// Handle namecall performed on a userdata value
116
// 'sourceReg' (self argument) is guaranteed to be a userdata, but tag has to be checked
117
// All other arguments can be of any type
118
// Guards should take a VM exit to 'pcpos'
119
HostUserdataNamecallHandler userdataNamecall = nullptr;
120
};
121
122
struct CompilationOptions
123
{
124
unsigned int flags = 0;
125
HostIrHooks hooks;
126
127
// null-terminated array of userdata types names that might have custom lowering
128
const char* const* userdataTypes = nullptr;
129
130
bool recordCounters = false;
131
};
132
133
using AnnotatorFn = void (*)(void* context, std::string& result, int fid, int instpos);
134
135
// Output "#" before IR blocks and instructions
136
enum class IncludeIrPrefix
137
{
138
No,
139
Yes
140
};
141
142
// Output user count and last use information of blocks and instructions
143
enum class IncludeUseInfo
144
{
145
No,
146
Yes
147
};
148
149
// Output CFG informations like block predecessors, successors and etc
150
enum class IncludeCfgInfo
151
{
152
No,
153
Yes
154
};
155
156
// Output VM register live in/out information for blocks
157
enum class IncludeRegFlowInfo
158
{
159
No,
160
Yes
161
};
162
163
struct AssemblyOptions
164
{
165
enum Target
166
{
167
Host,
168
A64,
169
A64_NoFeatures,
170
X64_Windows,
171
X64_SystemV,
172
};
173
174
Target target = Host;
175
176
CompilationOptions compilationOptions;
177
178
bool outputBinary = false;
179
180
bool includeAssembly = false;
181
bool includeIr = false;
182
bool includeOutlinedCode = false;
183
bool includeIrTypes = false;
184
185
IncludeIrPrefix includeIrPrefix = IncludeIrPrefix::Yes;
186
IncludeUseInfo includeUseInfo = IncludeUseInfo::Yes;
187
IncludeCfgInfo includeCfgInfo = IncludeCfgInfo::Yes;
188
IncludeRegFlowInfo includeRegFlowInfo = IncludeRegFlowInfo::Yes;
189
190
// Optional annotator function can be provided to describe each instruction, it takes function id and sequential instruction id
191
AnnotatorFn annotator = nullptr;
192
void* annotatorContext = nullptr;
193
};
194
195
} // namespace CodeGen
196
} // namespace Luau
197
198