Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/CodeGen/include/Luau/IrBuilder.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/Bytecode.h"
5
#include "Luau/Common.h"
6
#include "Luau/DenseHash.h"
7
#include "Luau/IrData.h"
8
9
#include <vector>
10
11
struct Proto;
12
typedef uint32_t Instruction;
13
14
namespace Luau
15
{
16
namespace CodeGen
17
{
18
19
struct HostIrHooks;
20
21
struct IrBuilder
22
{
23
IrBuilder(const HostIrHooks& hostHooks);
24
25
void buildFunctionIr(Proto* proto);
26
27
void rebuildBytecodeBasicBlocks(Proto* proto);
28
void translateInst(LuauOpcode op, const Instruction* pc, int i);
29
void handleFastcallFallback(IrOp fallbackOrUndef, const Instruction* pc, int i);
30
31
bool isInternalBlock(IrOp block);
32
void beginBlock(IrOp block);
33
34
void loadAndCheckTag(IrOp loc, uint8_t tag, IrOp fallback);
35
void checkSafeEnv(int pcpos);
36
37
// Clones all instructions into the current block
38
// Source block that is cloned cannot use values coming in from a predecessor
39
void clone(std::vector<uint32_t> sourceIdxs, bool removeCurrentTerminator);
40
41
IrOp undef();
42
43
IrOp constInt(int value);
44
IrOp constUint(unsigned value);
45
IrOp constImport(unsigned value);
46
IrOp constDouble(double value);
47
IrOp constTag(uint8_t value);
48
IrOp constAny(IrConst constant, uint64_t asCommonKey);
49
50
IrOp cond(IrCondition cond);
51
52
IrOp inst(IrCmd cmd, const IrOps& ops);
53
IrOp inst(IrCmd cmd, std::initializer_list<IrOp> ops);
54
IrOp inst(IrCmd cmd);
55
IrOp inst(IrCmd cmd, IrOp a);
56
IrOp inst(IrCmd cmd, IrOp a, IrOp b);
57
IrOp inst(IrCmd cmd, IrOp a, IrOp b, IrOp c);
58
IrOp inst(IrCmd cmd, IrOp a, IrOp b, IrOp c, IrOp d);
59
IrOp inst(IrCmd cmd, IrOp a, IrOp b, IrOp c, IrOp d, IrOp e);
60
IrOp inst(IrCmd cmd, IrOp a, IrOp b, IrOp c, IrOp d, IrOp e, IrOp f);
61
IrOp inst(IrCmd cmd, IrOp a, IrOp b, IrOp c, IrOp d, IrOp e, IrOp f, IrOp g);
62
63
IrOp block(IrBlockKind kind); // Requested kind can be ignored if we are in an outlined sequence
64
IrOp blockAtInst(uint32_t index);
65
IrOp fallbackBlock(uint32_t pcpos);
66
67
IrOp vmReg(uint8_t index);
68
IrOp vmConst(uint32_t index);
69
IrOp vmUpvalue(uint8_t index);
70
71
IrOp vmExit(uint32_t pcpos);
72
73
const HostIrHooks& hostHooks;
74
75
bool inTerminatedBlock = false;
76
77
bool interruptRequested = false;
78
79
bool activeFastcallFallback = false;
80
IrOp fastcallFallbackReturn;
81
82
// Force builder to skip source commands
83
int cmdSkipTarget = -1;
84
85
IrFunction function;
86
87
uint32_t activeBlockIdx = ~0u;
88
89
std::vector<uint32_t> instIndexToBlock; // Block index at the bytecode instruction
90
91
struct LoopInfo
92
{
93
IrOp step;
94
int startpc = 0;
95
};
96
97
std::vector<LoopInfo> numericLoopStack;
98
99
// Similar to BytecodeBuilder, duplicate constants are removed used the same method
100
struct ConstantKey
101
{
102
IrConstKind kind;
103
// Note: this stores value* from IrConst; when kind is Double, this stores the same bits as double does but in uint64_t.
104
uint64_t value;
105
106
bool operator==(const ConstantKey& key) const
107
{
108
return kind == key.kind && value == key.value;
109
}
110
};
111
112
struct ConstantKeyHash
113
{
114
size_t operator()(const ConstantKey& key) const
115
{
116
// finalizer from MurmurHash64B
117
const uint32_t m = 0x5bd1e995;
118
119
uint32_t h1 = uint32_t(key.value);
120
uint32_t h2 = uint32_t(key.value >> 32) ^ (int(key.kind) * m);
121
122
h1 ^= h2 >> 18;
123
h1 *= m;
124
h2 ^= h1 >> 22;
125
h2 *= m;
126
h1 ^= h2 >> 17;
127
h1 *= m;
128
h2 ^= h1 >> 19;
129
h2 *= m;
130
131
// ... truncated to 32-bit output (normally hash is equal to (uint64_t(h1) << 32) | h2, but we only really need the lower 32-bit half)
132
return size_t(h2);
133
}
134
};
135
136
DenseHashMap<ConstantKey, uint32_t, ConstantKeyHash> constantMap;
137
};
138
139
} // namespace CodeGen
140
} // namespace Luau
141
142