-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathllvmbuildutils.h
More file actions
211 lines (156 loc) · 8.03 KB
/
llvmbuildutils.h
File metadata and controls
211 lines (156 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <scratchcpp/valuedata.h>
#include "test_export.h"
#include "llvmfunctions.h"
#include "llvmlocalvariableinfo.h"
#include "llvmvariableptr.h"
#include "llvmlistptr.h"
#include "llvmcoroutine.h"
#include "llvmifstatement.h"
#include "llvmloop.h"
#include "llvmcompilercontext.h"
namespace libscratchcpp
{
class LLVMRegister;
class LLVMCoroutine;
class LIBSCRATCHCPP_TEST_EXPORT LLVMBuildUtils
{
public:
enum class Comparison
{
EQ,
GT,
LT
};
enum class NumberType
{
Int,
Double
};
LLVMBuildUtils(LLVMCompilerContext *ctx, llvm::IRBuilder<> &builder, Compiler::CodeType codeType);
void init(llvm::Function *function, BlockPrototype *procedurePrototype, bool warp, const std::vector<std::shared_ptr<LLVMRegister>> ®s);
void end(LLVMInstruction *lastInstruction, LLVMRegister *lastConstant);
LLVMCompilerContext *compilerCtx() const;
llvm::LLVMContext &llvmCtx();
llvm::Module *module() const;
llvm::IRBuilder<> &builder();
llvm::Function *function() const;
LLVMFunctions &functions();
std::string scriptFunctionName(BlockPrototype *procedurePrototype);
llvm::FunctionType *scriptFunctionType(BlockPrototype *procedurePrototype);
function_id_t scriptFunctionId() const;
size_t stringCount() const;
llvm::BasicBlock *endBranch() const;
llvm::BasicBlock *endThreadBranch() const;
llvm::Value *threadEndSentinel() const;
BlockPrototype *procedurePrototype() const;
bool warp() const;
llvm::Value *executionContextPtr();
llvm::Value *targetPtr();
llvm::Value *targetVariables();
llvm::Value *targetLists();
llvm::Value *warpArg();
LLVMCoroutine *coroutine() const;
void createLocalVariableInfo(CompilerLocalVariable *variable);
void createVariablePtr(Variable *variable);
void createListPtr(List *list);
LLVMLocalVariableInfo &localVariableInfo(CompilerLocalVariable *variable);
LLVMVariablePtr &variablePtr(Variable *variable);
LLVMListPtr &listPtr(List *list);
void syncVariables();
void reloadVariables();
void reloadLists();
void invalidateTarget();
std::vector<LLVMIfStatement> &ifStatements();
std::vector<LLVMLoop> &loops();
Compiler::StaticType optimizeRegisterType(const LLVMRegister *reg) const;
static Compiler::StaticType mapType(ValueType type);
static ValueType mapType(Compiler::StaticType type);
static bool isSingleType(Compiler::StaticType type);
llvm::Value *addAlloca(llvm::Type *type);
llvm::Value *addStringAlloca();
llvm::Value *castValue(LLVMRegister *reg, Compiler::StaticType targetType, NumberType targetNumType = NumberType::Double);
llvm::Type *getType(Compiler::StaticType type, bool isReturnType);
llvm::Value *isNaN(llvm::Value *num);
llvm::Value *removeNaN(llvm::Value *num);
void createValueStore(
llvm::Value *destPtr,
llvm::Value *destTypePtr,
llvm::Value *destIsIntVar,
llvm::Value *destIntVar,
LLVMRegister *reg,
Compiler::StaticType destType,
Compiler::StaticType targetType);
void createValueStore(llvm::Value *destPtr, llvm::Value *destTypePtr, llvm::Value *destIsIntVar, llvm::Value *destIntVar, LLVMRegister *reg, Compiler::StaticType targetType);
llvm::Value *getValueTypePtr(llvm::Value *value);
llvm::Value *getValueTypePtr(LLVMRegister *reg);
llvm::Value *getListSize(const LLVMListPtr &listPtr);
llvm::Value *getListItem(const LLVMListPtr &listPtr, llvm::Value *index);
llvm::Value *getListItemIndex(const LLVMListPtr &listPtr, Compiler::StaticType listType, LLVMRegister *item);
llvm::Value *createValue(LLVMRegister *reg);
llvm::Value *createNewValue(LLVMRegister *reg);
llvm::Value *createComparison(LLVMRegister *arg1, LLVMRegister *arg2, Comparison type);
llvm::Value *createStringComparison(LLVMRegister *arg1, LLVMRegister *arg2, bool caseSensitive);
void createSuspend();
private:
void initTypes();
void createVariableMap();
void createListMap();
llvm::Value *loadRegisterType(LLVMRegister *reg, Compiler::StaticType type);
llvm::Value *castRawValue(LLVMRegister *reg, Compiler::StaticType targetType, NumberType targetNumType);
llvm::Constant *castConstValue(const Value &value, Compiler::StaticType targetType, NumberType targetNumType);
std::pair<llvm::Value *, llvm::Value *> callStringToDoubleWithCheck(LLVMRegister *reg, llvm::Value *stringPtr);
llvm::Value *valueIsValidNumber(LLVMRegister *reg);
llvm::Value *rawValueIsValidNumber(LLVMRegister *reg);
llvm::Value *stringIsValidNumber(LLVMRegister *reg, llvm::Value *stringPtr);
void createValueCopy(llvm::Value *source, llvm::Value *target);
void copyStructField(llvm::Value *source, llvm::Value *target, int index, llvm::StructType *structType, llvm::Type *fieldType);
Comparison swapComparisonArgs(Comparison type);
llvm::Value *createNumberAndNumberComparison(LLVMRegister *arg1, LLVMRegister *arg2, Comparison type);
llvm::Value *createBoolAndBoolComparison(LLVMRegister *arg1, LLVMRegister *arg2, Comparison type);
llvm::Value *createStringAndStringComparison(LLVMRegister *arg1, LLVMRegister *arg2, Comparison type);
llvm::Value *createNumberAndBoolComparison(LLVMRegister *arg1, LLVMRegister *arg2, Comparison type);
llvm::Value *createNumberAndStringComparison(LLVMRegister *arg1, LLVMRegister *arg2, Comparison type);
llvm::Value *createBoolAndStringComparison(LLVMRegister *arg1, LLVMRegister *arg2, Comparison type);
llvm::Value *createStringsEqualComparison(llvm::Value *stringPtr1, llvm::Value *stringPtr2, bool caseSensitive);
llvm::Value *getVariablePtr(llvm::Value *targetVariables, Variable *variable);
llvm::Value *getListPtr(llvm::Value *targetLists, List *list);
llvm::Value *getListDataPtr(const LLVMListPtr &listPtr);
LLVMCompilerContext *m_ctx = nullptr;
llvm::LLVMContext &m_llvmCtx;
llvm::IRBuilder<> &m_builder;
LLVMFunctions m_functions;
Target *m_target = nullptr;
llvm::Function *m_function = nullptr;
function_id_t m_functionId = 0;
llvm::Value *m_functionIdValue = nullptr;
llvm::BasicBlock *m_endBranch = nullptr;
llvm::BasicBlock *m_endThreadBranch = nullptr;
llvm::StructType *m_valueDataType = nullptr;
llvm::StructType *m_stringPtrType = nullptr;
llvm::Type *m_functionIdType = nullptr;
BlockPrototype *m_procedurePrototype = nullptr;
bool m_warp = false;
Compiler::CodeType m_codeType = Compiler::CodeType::Script;
std::unordered_set<std::string> m_unsafeConstants;
llvm::Value *m_executionContextPtr = nullptr;
llvm::Value *m_targetPtr = nullptr;
llvm::Value *m_targetVariables = nullptr;
llvm::Value *m_targetLists = nullptr;
llvm::Value *m_warpArg = nullptr;
llvm::Value *m_targetValidFlag = nullptr;
std::unique_ptr<LLVMCoroutine> m_coroutine;
std::unordered_map<CompilerLocalVariable *, LLVMLocalVariableInfo> m_localVariables;
std::unordered_map<Variable *, size_t> m_targetVariableMap;
std::unordered_map<Variable *, LLVMVariablePtr> m_variablePtrs;
std::unordered_map<List *, size_t> m_targetListMap;
std::unordered_map<List *, LLVMListPtr> m_listPtrs;
std::vector<LLVMIfStatement> m_ifStatements;
std::vector<LLVMLoop> m_loops;
llvm::BasicBlock *m_stringAllocaBlock = nullptr;
llvm::BasicBlock *m_stringAllocaNextBlock = nullptr;
llvm::Value *m_stringArray = nullptr;
size_t m_stringCount = 0;
};
} // namespace libscratchcpp