-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathllvmcodebuilder.h
More file actions
247 lines (204 loc) · 13 KB
/
llvmcodebuilder.h
File metadata and controls
247 lines (204 loc) · 13 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <scratchcpp/value.h>
#include <scratchcpp/blockprototype.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/IRBuilder.h>
#include "../icodebuilder.h"
#include "llvminstruction.h"
#include "llvmcoroutine.h"
#include "llvmvariableptr.h"
#include "llvmlistptr.h"
namespace libscratchcpp
{
class LLVMCompilerContext;
class LLVMConstantRegister;
class LLVMLoopScope;
class LLVMCodeBuilder : public ICodeBuilder
{
public:
LLVMCodeBuilder(LLVMCompilerContext *ctx, BlockPrototype *procedurePrototype = nullptr);
std::shared_ptr<ExecutableCode> finalize() override;
CompilerValue *addFunctionCall(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args) override;
CompilerValue *addTargetFunctionCall(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args) override;
CompilerValue *addFunctionCallWithCtx(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args) override;
CompilerConstant *addConstValue(const Value &value) override;
CompilerValue *addLoopIndex() override;
CompilerValue *addLocalVariableValue(CompilerLocalVariable *variable) override;
CompilerValue *addVariableValue(Variable *variable) override;
CompilerValue *addListContents(List *list) override;
CompilerValue *addListItem(List *list, CompilerValue *index) override;
CompilerValue *addListItemIndex(List *list, CompilerValue *item) override;
CompilerValue *addListContains(List *list, CompilerValue *item) override;
CompilerValue *addListSize(List *list) override;
CompilerValue *addProcedureArgument(const std::string &name) override;
CompilerValue *createAdd(CompilerValue *operand1, CompilerValue *operand2) override;
CompilerValue *createSub(CompilerValue *operand1, CompilerValue *operand2) override;
CompilerValue *createMul(CompilerValue *operand1, CompilerValue *operand2) override;
CompilerValue *createDiv(CompilerValue *operand1, CompilerValue *operand2) override;
CompilerValue *createRandom(CompilerValue *from, CompilerValue *to) override;
CompilerValue *createRandomInt(CompilerValue *from, CompilerValue *to) override;
CompilerValue *createCmpEQ(CompilerValue *operand1, CompilerValue *operand2) override;
CompilerValue *createCmpGT(CompilerValue *operand1, CompilerValue *operand2) override;
CompilerValue *createCmpLT(CompilerValue *operand1, CompilerValue *operand2) override;
CompilerValue *createStrCmpEQ(CompilerValue *string1, CompilerValue *string2, bool caseSensitive = false) override;
CompilerValue *createAnd(CompilerValue *operand1, CompilerValue *operand2) override;
CompilerValue *createOr(CompilerValue *operand1, CompilerValue *operand2) override;
CompilerValue *createNot(CompilerValue *operand) override;
CompilerValue *createMod(CompilerValue *num1, CompilerValue *num2) override;
CompilerValue *createRound(CompilerValue *num) override;
CompilerValue *createAbs(CompilerValue *num) override;
CompilerValue *createFloor(CompilerValue *num) override;
CompilerValue *createCeil(CompilerValue *num) override;
CompilerValue *createSqrt(CompilerValue *num) override;
CompilerValue *createSin(CompilerValue *num) override;
CompilerValue *createCos(CompilerValue *num) override;
CompilerValue *createTan(CompilerValue *num) override;
CompilerValue *createAsin(CompilerValue *num) override;
CompilerValue *createAcos(CompilerValue *num) override;
CompilerValue *createAtan(CompilerValue *num) override;
CompilerValue *createLn(CompilerValue *num) override;
CompilerValue *createLog10(CompilerValue *num) override;
CompilerValue *createExp(CompilerValue *num) override;
CompilerValue *createExp10(CompilerValue *num) override;
CompilerValue *createSelect(CompilerValue *cond, CompilerValue *trueValue, CompilerValue *falseValue, Compiler::StaticType valueType) override;
CompilerLocalVariable *createLocalVariable(Compiler::StaticType type) override;
void createLocalVariableWrite(CompilerLocalVariable *variable, CompilerValue *value) override;
void createVariableWrite(Variable *variable, CompilerValue *value) override;
void createListClear(List *list) override;
void createListRemove(List *list, CompilerValue *index) override;
void createListAppend(List *list, CompilerValue *item) override;
void createListInsert(List *list, CompilerValue *index, CompilerValue *item) override;
void createListReplace(List *list, CompilerValue *index, CompilerValue *item) override;
void beginIfStatement(CompilerValue *cond) override;
void beginElseBranch() override;
void endIf() override;
void beginRepeatLoop(CompilerValue *count) override;
void beginWhileLoop(CompilerValue *cond) override;
void beginRepeatUntilLoop(CompilerValue *cond) override;
void beginLoopCondition() override;
void endLoop() override;
void yield() override;
void createStop() override;
void createProcedureCall(BlockPrototype *prototype, const Compiler::Args &args) override;
private:
enum class Comparison
{
EQ,
GT,
LT
};
void initTypes();
void createVariableMap();
void createListMap();
void pushScopeLevel();
void popScopeLevel();
void pushLoopScope(bool buildPhase);
void popLoopScope();
std::string getMainFunctionName(BlockPrototype *procedurePrototype);
std::string getResumeFunctionName(BlockPrototype *procedurePrototype);
llvm::FunctionType *getMainFunctionType(BlockPrototype *procedurePrototype);
llvm::Function *getOrCreateFunction(const std::string &name, llvm::FunctionType *type);
void verifyFunction(llvm::Function *func);
LLVMRegister *addReg(std::shared_ptr<LLVMRegister> reg, std::shared_ptr<LLVMInstruction> ins);
llvm::Value *addAlloca(llvm::Type *type);
void freeLater(llvm::Value *value);
void freeScopeHeap();
llvm::Value *castValue(LLVMRegister *reg, Compiler::StaticType targetType);
llvm::Value *castRawValue(LLVMRegister *reg, Compiler::StaticType targetType);
llvm::Constant *castConstValue(const Value &value, Compiler::StaticType targetType);
Compiler::StaticType optimizeRegisterType(LLVMRegister *reg) const;
llvm::Type *getType(Compiler::StaticType type);
Compiler::StaticType getProcedureArgType(BlockPrototype::ArgType type);
llvm::Value *isNaN(llvm::Value *num);
llvm::Value *removeNaN(llvm::Value *num);
llvm::Value *getVariablePtr(llvm::Value *targetVariables, Variable *variable);
llvm::Value *getListPtr(llvm::Value *targetLists, List *list);
void syncVariables(llvm::Value *targetVariables);
void reloadVariables(llvm::Value *targetVariables);
void reloadLists();
void updateListDataPtr(const LLVMListPtr &listPtr);
bool isVarOrListTypeSafe(std::shared_ptr<LLVMInstruction> ins, Compiler::StaticType expectedType) const;
bool isVarOrListTypeSafe(std::shared_ptr<LLVMInstruction> ins, Compiler::StaticType expectedType, std::unordered_set<LLVMInstruction *> &log, int &c) const;
bool isVarOrListWriteResultTypeSafe(std::shared_ptr<LLVMInstruction> ins, Compiler::StaticType expectedType, bool ignoreSavedType, std::unordered_set<LLVMInstruction *> &log, int &c) const;
LLVMRegister *createOp(LLVMInstruction::Type type, Compiler::StaticType retType, Compiler::StaticType argType, const Compiler::Args &args);
LLVMRegister *createOp(LLVMInstruction::Type type, Compiler::StaticType retType, const Compiler::ArgTypes &argTypes = {}, const Compiler::Args &args = {});
LLVMRegister *createOp(const LLVMInstruction &ins, Compiler::StaticType retType, Compiler::StaticType argType, const Compiler::Args &args);
LLVMRegister *createOp(const LLVMInstruction &ins, Compiler::StaticType retType, const Compiler::ArgTypes &argTypes = {}, const Compiler::Args &args = {});
std::shared_ptr<LLVMLoopScope> currentLoopScope() const;
void createValueStore(LLVMRegister *reg, llvm::Value *targetPtr, Compiler::StaticType sourceType, Compiler::StaticType targetType);
void createReusedValueStore(LLVMRegister *reg, llvm::Value *targetPtr, Compiler::StaticType sourceType, Compiler::StaticType targetType);
void createValueCopy(llvm::Value *source, llvm::Value *target);
void copyStructField(llvm::Value *source, llvm::Value *target, int index, llvm::StructType *structType, llvm::Type *fieldType);
llvm::Value *getListItem(const LLVMListPtr &listPtr, llvm::Value *index);
llvm::Value *getListItemIndex(const LLVMListPtr &listPtr, LLVMRegister *item);
llvm::Value *createValue(LLVMRegister *reg);
llvm::Value *createComparison(LLVMRegister *arg1, LLVMRegister *arg2, Comparison type);
llvm::Value *createStringComparison(LLVMRegister *arg1, LLVMRegister *arg2, bool caseSensitive);
void createSuspend(LLVMCoroutine *coro, llvm::Value *warpArg, llvm::Value *targetVariables);
llvm::FunctionCallee resolveFunction(const std::string name, llvm::FunctionType *type);
llvm::FunctionCallee resolve_value_init();
llvm::FunctionCallee resolve_value_free();
llvm::FunctionCallee resolve_value_assign_long();
llvm::FunctionCallee resolve_value_assign_double();
llvm::FunctionCallee resolve_value_assign_bool();
llvm::FunctionCallee resolve_value_assign_cstring();
llvm::FunctionCallee resolve_value_assign_special();
llvm::FunctionCallee resolve_value_assign_copy();
llvm::FunctionCallee resolve_value_toDouble();
llvm::FunctionCallee resolve_value_toBool();
llvm::FunctionCallee resolve_value_toCString();
llvm::FunctionCallee resolve_value_doubleToCString();
llvm::FunctionCallee resolve_value_boolToCString();
llvm::FunctionCallee resolve_value_stringToDouble();
llvm::FunctionCallee resolve_value_stringToBool();
llvm::FunctionCallee resolve_value_equals();
llvm::FunctionCallee resolve_value_greater();
llvm::FunctionCallee resolve_value_lower();
llvm::FunctionCallee resolve_list_clear();
llvm::FunctionCallee resolve_list_remove();
llvm::FunctionCallee resolve_list_append_empty();
llvm::FunctionCallee resolve_list_insert_empty();
llvm::FunctionCallee resolve_list_data();
llvm::FunctionCallee resolve_list_size_ptr();
llvm::FunctionCallee resolve_list_alloc_size_ptr();
llvm::FunctionCallee resolve_list_to_string();
llvm::FunctionCallee resolve_llvm_random();
llvm::FunctionCallee resolve_llvm_random_double();
llvm::FunctionCallee resolve_llvm_random_long();
llvm::FunctionCallee resolve_llvm_random_bool();
llvm::FunctionCallee resolve_strcmp();
llvm::FunctionCallee resolve_strcasecmp();
Target *m_target = nullptr;
std::unordered_map<Variable *, size_t> m_targetVariableMap;
std::unordered_map<Variable *, LLVMVariablePtr> m_variablePtrs;
std::vector<std::unordered_map<LLVMVariablePtr *, Compiler::StaticType>> m_scopeVariables;
std::unordered_map<List *, size_t> m_targetListMap;
std::unordered_map<List *, LLVMListPtr> m_listPtrs;
std::vector<std::unordered_map<LLVMListPtr *, Compiler::StaticType>> m_scopeLists;
LLVMCompilerContext *m_ctx;
llvm::LLVMContext &m_llvmCtx;
llvm::Module *m_module = nullptr;
llvm::IRBuilder<> m_builder;
llvm::Function *m_function = nullptr;
llvm::StructType *m_valueDataType = nullptr;
llvm::FunctionType *m_resumeFuncType = nullptr;
std::vector<std::shared_ptr<LLVMInstruction>> m_instructions;
std::vector<std::shared_ptr<LLVMRegister>> m_regs;
std::vector<std::shared_ptr<CompilerLocalVariable>> m_localVars;
BlockPrototype *m_procedurePrototype = nullptr;
bool m_defaultWarp = false;
bool m_warp = false;
int m_defaultArgCount = 0;
long m_loopScope = -1; // index
std::vector<std::shared_ptr<LLVMLoopScope>> m_loopScopes;
long m_loopScopeCounter = 0; // replacement for m_loopScopes size in build phase
std::vector<long> m_loopScopeTree;
bool m_loopCondition = false; // whether we're currently compiling a loop condition
std::vector<std::shared_ptr<LLVMInstruction>> m_variableInstructions;
std::vector<std::shared_ptr<LLVMInstruction>> m_listInstructions;
std::vector<std::vector<llvm::Value *>> m_heap; // scopes
std::shared_ptr<ExecutableCode> m_output;
};
} // namespace libscratchcpp