1
2
3
4
5
6 #ifndef CMAJOR_CMLLVM_EMITTER_INCLUDED
7 #define CMAJOR_CMLLVM_EMITTER_INCLUDED
8 #include <cmajor/ir/EmittingContext.hpp>
9 #include <cmajor/ir/Emitter.hpp>
10 #include <cmajor/common/Common.hpp>
11 #include <cmajor/cmllvm/ValueStack.hpp>
12 #include <llvm/IR/IRBuilder.h>
13 #include <llvm/IR/DIBuilder.h>
14 #include <boost/uuid/uuid.hpp>
15 #include <boost/uuid/nil_generator.hpp>
16 #include <boost/functional/hash.hpp>
17 #include <unordered_map>
18 #include <unordered_set>
19
20 namespace cmllvm {
21
22 using soulng::lexer::Span;
23
24 typedef llvm::SmallVector<llvm::Value*, 4> ArgVector;
25
26 class Emitter : public cmajor::ir::Emitter
27 {
28 public:
29 Emitter(llvm::LLVMContext& context_, cmajor::ir::EmittingContext& emittingContext_);
30 void SetEmittingDelegate(cmajor::ir::EmittingDelegate* emittingDelegate_) override { emittingDelegate = emittingDelegate_; }
31 void* GetIrTypeForBool() override;
32 void* GetIrTypeForSByte() override;
33 void* GetIrTypeForByte() override;
34 void* GetIrTypeForShort() override;
35 void* GetIrTypeForUShort() override;
36 void* GetIrTypeForInt() override;
37 void* GetIrTypeForUInt() override;
38 void* GetIrTypeForLong() override;
39 void* GetIrTypeForULong() override;
40 void* GetIrTypeForFloat() override;
41 void* GetIrTypeForDouble() override;
42 void* GetIrTypeForChar() override;
43 void* GetIrTypeForWChar() override;
44 void* GetIrTypeForUChar() override;
45 void* GetIrTypeForVoid() override;
46 void* GetIrTypeForFunction(void* retType, const std::std::vector<void*>¶mTypes) override;
47 void* GetIrTypeForVariableParamFunction(void* retType) override;
48 void* GetIrTypeForArrayType(void* elementType, int64_t size) override;
49 void* GetIrTypeForClassType(const std::std::vector<void*>&elementTypes) override;
50 void* CreateFwdIrTypeForClassType() override;
51 void SetFwdIrTypeBody(void* forwardDeclaredType, const std::std::vector<void*>&elementTypes) override;
52 void* GetIrTypeForDelegateType(void* retType, const std::std::vector<void*>¶mTypes) override;
53 void* GetIrTypeForVoidPtrType() override;
54 void* GetIrTypeForStructType(const std::std::vector<void*>&elementTypes) override;
55 void* GetIrTypeByTypeId(const boost::uuids::uuid& typeId) override;
56 void SetIrTypeByTypeId(const boost::uuids::uuid& typeId, void* irType) override;
57 void* GetIrTypeForPtrType(void* baseIrType) override;
58 std::string GetIrTypeName(void* irType) override;
59 std::string MakeVmtVariableName(const std::string& vmtObjectName) override;
60 void* CreateDefaultIrValueForArrayType(void* arrayIrType, const std::std::vector<void*>&arrayOfDefaults) override;
61 void* CreateDefaultIrValueForBool() override;
62 void* CreateDefaultIrValueForSByte() override;
63 void* CreateDefaultIrValueForByte() override;
64 void* CreateDefaultIrValueForShort() override;
65 void* CreateDefaultIrValueForUShort() override;
66 void* CreateDefaultIrValueForInt() override;
67 void* CreateDefaultIrValueForUInt() override;
68 void* CreateDefaultIrValueForLong() override;
69 void* CreateDefaultIrValueForULong() override;
70 void* CreateDefaultIrValueForFloat() override;
71 void* CreateDefaultIrValueForDouble() override;
72 void* CreateDefaultIrValueForChar() override;
73 void* CreateDefaultIrValueForWChar() override;
74 void* CreateDefaultIrValueForUChar() override;
75 void* CreateDefaultIrValueForStruct(void* irType, const std::std::vector<void*>&defaultMembers) override;
76 void* CreateDefaultIrValueForDelegateType(void* irType) override;
77 void* CreateDefaultIrValueForVoidPtrType() override;
78 void* CreateDefaultIrValueForDerivedType(void* irType) override;
79 void* CreateDefaultIrValueForPtrType(void* irType) override;
80 void* CreateIrValueForBool(bool value) override;
81 void* CreateIrValueForSByte(int8_t value) override;
82 void* CreateIrValueForByte(uint8_t value) override;
83 void* CreateIrValueForShort(int16_t value) override;
84 void* CreateIrValueForUShort(uint16_t value) override;
85 void* CreateIrValueForInt(int32_t value) override;
86 void* CreateIrValueForUInt(uint32_t value) override;
87 void* CreateIrValueForLong(int64_t value) override;
88 void* CreateIrValueForULong(uint64_t value) override;
89 void* CreateIrValueForFloat(float value) override;
90 void* CreateIrValueForDouble(double value) override;
91 void* CreateIrValueForChar(uint8_t value) override;
92 void* CreateIrValueForWChar(uint16_t value) override;
93 void* CreateIrValueForUChar(uint32_t value) override;
94 void* CreateIrValueForWString(void* wstringConstant) override;
95 void* CreateIrValueForUString(void* ustringConstant) override;
96 void* CreateIrValueForConstantArray(void* arrayIrType, const std::std::vector<void*>&elementConstants, conststd::string&prefix) override;
97 void* CreateIrValueForConstantStruct(void* structIrType, const std::std::vector<void*>&elementConstants) override;
98 void* CreateIrValueForUuid(void* uuidConstant) override;
99 void* GetConversionValue(void* type, void* from) override;
100 void* CreateDITypeForBool() override;
101 void* CreateDITypeForSByte() override;
102 void* CreateDITypeForByte() override;
103 void* CreateDITypeForShort() override;
104 void* CreateDITypeForUShort() override;
105 void* CreateDITypeForInt() override;
106 void* CreateDITypeForUInt() override;
107 void* CreateDITypeForLong() override;
108 void* CreateDITypeForULong() override;
109 void* CreateDITypeForFloat() override;
110 void* CreateDITypeForDouble() override;
111 void* CreateDITypeForChar() override;
112 void* CreateDITypeForWChar() override;
113 void* CreateDITypeForUChar() override;
114 void* CreateDITypeForVoid() override;
115 void* CreateDITypeForArray(void* elementDIType, const std::std::vector<void*>&elements) override;
116 void* CreateIrDIForwardDeclaration(void* irType, const std::string& name, const std::string& mangledName, const Span& span, const boost::uuids::uuid& moduleId) override;
117 uint64_t GetOffsetInBits(void* classIrType, int layoutIndex) override;
118 void* CreateDITypeForClassType(void* irType, const std::std::vector<void*>&memberVariableElements, constSpan&classSpan, constboost::uuids::uuid&moduleId, const std::string& name, void* vtableHolderClass,
119 const std::string& mangledName, void* baseClassDIType) override;
120 void* CreateDITypeForEnumConstant(const std::string& name, int64_t value) override;
121 void* CreateDITypeForEnumType(const std::string& name, const std::string& mangledName, const Span& span, const boost::uuids::uuid& moduleId, const std::std::vector<void*>&enumConstantElements,
122 uint64_t sizeInBits, uint32_t alignInBits, void* underlyingDIType) override;
123 void MapFwdDeclaration(void* fwdDeclaration, const boost::uuids::uuid& typeId) override;
124 void* GetDITypeByTypeId(const boost::uuids::uuid& typeId) const override;
125 void SetDITypeByTypeId(const boost::uuids::uuid& typeId, void* diType, const std::string& typeName) override;
126 void* GetDIMemberType(const std::std::pair<boost::uuids::uuid, int32_t>&memberVariableId) override;
127 void SetDIMemberType(const std::std::pair<boost::uuids::uuid, int32_t>&memberVariableId, void*diType) override;
128 void* CreateDIMemberType(void* scope, const std::string& name, const Span& span, const boost::uuids::uuid& moduleId, uint64_t sizeInBits, uint64_t alignInBits, uint64_t offsetInBits, void* diType) override;
129 void* CreateConstDIType(void* diType) override;
130 void* CreateLValueRefDIType(void* diType) override;
131 void* CreateRValueRefDIType(void* diType) override;
132 void* CreatePointerDIType(void* diType) override;
133 void* CreateUnspecifiedDIType(const std::string& name) override;
134 void MapClassPtr(const boost::uuids::uuid& typeId, void* classPtr, const std::string& className) override;
135 uint64_t GetSizeInBits(void* irType) override;
136 uint64_t GetAlignmentInBits(void* irType) override;
137 void SetCurrentDebugLocation(const Span& span) override;
138 void* GetArrayBeginAddress(void* arrayPtr) override;
139 void* GetArrayEndAddress(void* arrayPtr, uint64_t size) override;
140 void* CreateBasicBlock(const std::string& name) override;
141 void* CreateIncludeBasicBlockInstruction(void* basicBlock) override;
142 void PushParentBlock() override;
143 void PopParentBlock() override;
144 void SetHandlerBlock(void* tryBlock, void* catchBlock) override;
145 void SetCleanupBlock(void* cleanupBlock) override;
146 int GetBasicBlockId(void* basicBlock) override;
147 void CreateBr(void* targetBasicBlock) override;
148 void CreateCondBr(void* cond, void* trueBasicBlock, void* falseBasicBlock) override;
149 void* CreateArrayIndexAddress(void* arrayPtr, void* index) override;
150 void CreateStore(void* value, void* ptr) override;
151 void* CreateLoad(void* ptr) override;
152 void* CreateAdd(void* left, void* right) override;
153 void* CreateFAdd(void* left, void* right) override;
154 void* CreateSub(void* left, void* right) override;
155 void* CreateFSub(void* left, void* right) override;
156 void* CreateMul(void* left, void* right) override;
157 void* CreateFMul(void* left, void* right) override;
158 void* CreateUDiv(void* left, void* right) override;
159 void* CreateSDiv(void* left, void* right) override;
160 void* CreateFDiv(void* left, void* right) override;
161 void* CreateURem(void* left, void* right) override;
162 void* CreateSRem(void* left, void* right) override;
163 void* CreateAnd(void* left, void* right) override;
164 void* CreateOr(void* left, void* right) override;
165 void* CreateXor(void* left, void* right) override;
166 void* CreateShl(void* left, void* right) override;
167 void* CreateAShr(void* left, void* right) override;
168 void* CreateLShr(void* left, void* right) override;
169 void* CreateICmpEQ(void* left, void* right) override;
170 void* CreateFCmpOEQ(void* left, void* right) override;
171 void* CreateICmpULT(void* leftValue, void* rightValue) override;
172 void* CreateICmpSLT(void* leftValue, void* rightValue) override;
173 void* CreateFCmpOLT(void* leftValue, void* rightValue) override;
174 void* CreateSExt(void* operand, void* destinationType) override;
175 void* CreateZExt(void* operand, void* destinationType) override;
176 void* CreateFPExt(void* operand, void* destinationType) override;
177 void* CreateTrunc(void* operand, void* destinationType) override;
178 void* CreateFPTrunc(void* operand, void* destinationType) override;
179 void* CreateBitCast(void* operand, void* destinationType) override;
180 void* CreateUIToFP(void* operand, void* destinationType) override;
181 void* CreateSIToFP(void* operand, void* destinationType) override;
182 void* CreateFPToUI(void* operand, void* destinationType) override;
183 void* CreateFPToSI(void* operand, void* destinationType) override;
184 void* CreateIntToPtr(void* intValue, void* destinationType) override;
185 void* CreatePtrToInt(void* ptrValue, void* destinationType) override;
186 void* CreateNot(void* value) override;
187 void* CreateNeg(void* value) override;
188 void* CreateFNeg(void* value) override;
189 void* CreateNop() override;
190 void* CreateSave() override;
191 void* CreateBeginTry() override;
192 void* CreateEndTry(void* nextDest) override;
193 void* CreateBeginCatch() override;
194 void* CreateEndCatch(void* nextDest) override;
195 void* GetOrInsertGlobal(const std::string& name, void* type) override;
196 void* GetOrInsertAnyComdat(const std::string& name, void* global) override;
197 void* GetOrInsertAnyFunctionComdat(const std::string& name, void* function) override;
198 void SetInitializer(void* global, void* initializer) override;
199 void SetPrivateLinkage(void* global) override;
200 void* GetOrInsertFunction(const std::string& name, void* type, bool nothrow) override;
201 void* CreateGlobalStringPtr(const std::string& name) override;
202 void* CreateGlobalWStringPtr(const std::u16string& name) override;
203 void* CreateGlobalUStringPtr(const std::u32string& name) override;
204 void* CreateCall(void* callee, const std::std::vector<void*>&args) override;
205 void* CreateCallInst(void* callee, const std::std::vector<void*>&args, conststd::std::vector<void*>&bundles, constSpan&span) override;
206 void* CreateCallInstToBasicBlock(void* callee, const std::std::vector<void*>&args, conststd::std::vector<void*>&bundles, void*basicBlock, constSpan&span) override;
207 void* CreateInvoke(void* callee, void* normalBlock, void* unwindBlock, const std::std::vector<void*>&args) override;
208 void* CreateInvokeInst(void* callee, void* normalBlock, void* unwindBlock, const std::std::vector<void*>&args, conststd::std::vector<void*>&bundles, constSpan&span) override;
209 void* DIBuilder() override { return currentDIBuilder; }
210 void SetCurrentDIBuilder(void* diBuilder_) override { currentDIBuilder = static_cast<llvm::DIBuilder*>(diBuilder_); }
211 void* GetObjectFromClassDelegate(void* classDelegatePtr) override;
212 void* GetDelegateFromClassDelegate(void* classDelegatePtr) override;
213 void* GetObjectFromInterface(void* interfaceTypePtr) override;
214 void* GetObjectPtrFromInterface(void* interfaceTypePtr) override;
215 void* GetImtPtrFromInterface(void* interfaceTypePtr) override;
216 void* GetInterfaceMethod(void* imtPtr, int32_t methodIndex, void* interfaceMethodType) override;
217 void* GetVmtPtr(void* thisPtr, int32_t vmtPtrIndex, void* vmtPtrType) override;
218 void* GetMethodPtr(void* vmtPtr, int32_t vmtIndex) override;
219 void* GetImtArray(void* vmtObjectPtr, int32_t imtsVmtIndexOffset) override;
220 void* GetImt(void* imtArray, int32_t interfaceIndex) override;
221 void* GetMemberVariablePtr(void* classPtr, int32_t memberVariableLayoutIndex) override;
222 void* SizeOf(void* ptrType) override;
223 void* GetClassIdPtr(void* vmtPtr, int32_t classIdVmtIndexOffset) override;
224 void* GetClassName(void* vmtPtr, int32_t classNameVmtIndexOffset) override;
225 void* ComputeAddress(void* ptr, void* index) override;
226 void* CreatePtrDiff(void* left, void* right) override;
227 uint32_t GetPrivateFlag() override;
228 uint32_t GetProtectedFlag() override;
229 uint32_t GetPublicFlag() override;
230 uint32_t GetNoFlags() override;
231 void* CreateModule(const std::string& moduleName) override;
232 void DestroyModule(void* module) override;
233 void SetModule(void* module_) override { module = static_cast<llvm::Module*>(module_); }
234 llvm::LLVMContext& Context() { return context; }
235 llvm::IRBuilder<>& Builder() { return builder; }
236 llvm::Module* Module() { return module; }
237 llvm::DataLayout* DataLayout() { return dataLayout; }
238 llvm::DICompileUnit* DICompileUnit() { return diCompileUnit; }
239 llvm::DIFile* DIFile() { return diFile; }
240 ValueStack& Stack() { return stack; }
241 void SetTargetTriple(const std::string& targetTriple) override { module->setTargetTriple(targetTriple); }
242 void SetDataLayout(void* dataLayout_) override { dataLayout = static_cast<llvm::DataLayout*>(dataLayout_); module->setDataLayout(*dataLayout); }
243 void SetSourceFileName(const std::string& sourceFileName) override { module->setSourceFileName(sourceFileName); }
244 void SetDICompileUnit(void* diCompileUnit_) override { diCompileUnit = static_cast<llvm::DICompileUnit*>(diCompileUnit_); }
245 void SetDIFile(void* diFile_) override;
246 void SetColumnSpanProvider(cmajor::common::ColumnSpanProvider* columnSpanProvider_) override { columnSpanProvider = columnSpanProvider_; }
247 void SaveObjectPointer(void* objectPointer_) override;
248 void SetObjectPointer(void* objectPointer_) override { objectPointer = static_cast<llvm::Value*>(objectPointer_); }
249 void* GetObjectPointer() override { return objectPointer; }
250 void SetFunction(void* function_, int32_t fileIndex, const boost::uuids::uuid& sourceModuleId, const boost::uuids::uuid& functionId) override { function = static_cast<llvm::Function*>(function_); }
251 void SetFunctionName(const std::string& functionName) override;
252 void BeginScope() override;
253 void EndScope() override;
254 int16_t GetCurrentScopeId() const override;
255 void SetCurrentScopeId(int16_t scopeId) override;
256 int32_t AddControlFlowGraphNode() override;
257 void SetCurrentControlFlowGraphNodeId(int32_t controlFlowGraphNodeId) override;
258 void AddControlFlowGraphEdge(int32_t startNodeId, int32_t endNodeId) override;
259 void AddLocalVariable(const std::string& localVariableName, const boost::uuids::uuid& typeId, void* irObject) override;
260 void BeginInstructionFlag(int16_t flag) override;
261 void EndInstructionFlag(int16_t flag) override;
262 llvm::Function* Function() { return function; }
263 void* CurrentBasicBlock() const override { return currentBasicBlock; }
264 void SetCurrentBasicBlock(void* currentBasicBlock_) override { currentBasicBlock = static_cast<llvm::BasicBlock*>(currentBasicBlock_); builder.SetInsertPoint(currentBasicBlock); }
265 void SetInPrologue(bool inPrologue_) override { inPrologue = inPrologue_; }
266 void PushScope(void* scope) override;
267 void PopScope() override;
268 void* CurrentScope() override;
269 int GetColumn(const Span& span) const;
270 void StartDebugInfo(const std::string& sourceFilePath, const std::string& compilerVersion, bool optimized, cmajor::common::ColumnSpanProvider* columnSpanProvider_) override;
271 void FinalizeDebugInfo() override;
272 void EndDebugInfo() override;
273 void* CreateDebugInfoForNamespace(void* scope, const std::string& name) override;
274 void EmitIrText(const std::string& filePath) override;
275 void EmitIrFile(const std::string& filePath) override;
276 void Optimize(const std::string& bcFilePath, const std::string& optBCFilePath, const std::string& optimizationFlags) override;
277 void Disassemble(const std::string& bcFilePath, const std::string& llFilePath) override;
278 void Compile(const std::string& bcFilePath, const std::string& objectFilePath, int optimizationLevel) override;
279 void VerifyModule() override;
280 void EmitObjectCodeFile(const std::string& objectFilePath) override;
281 void ResetCurrentDebugLocation() override;
282 llvm::DebugLoc GetDebugLocation(const Span& span);
283 llvm::DebugLoc GetCurrentDebugLocation() { return currentDebugLocation; }
284 void* GetDebugInfoForFile(const Span& span, const boost::uuids::uuid& moduleId) override;
285 void ReplaceForwardDeclarations();
286 void* GetIrObject(void* symbol) const override;
287 void SetIrObject(void* symbol, void* irObject) override;
288 void* GetFunctionIrType(void* symbol) const override;
289 void SetFunctionIrType(void* symbol, void* irType) override;
290 bool IsVmtObjectCreated(void* symbol) const override;
291 void SetVmtObjectCreated(void* symbol) override;
292 bool IsStaticObjectCreated(void* symbol) const override;
293 void SetStaticObjectCreated(void* symbol) override;
294 void* GetStaticObjectType(void* symbol) const override;
295 void SetStaticObjectType(void* symbol, void* type) override;
296 void* GetVmtObjectType(void* symbol) const override;
297 void SetVmtObjectType(void* symbol, void* vmtObjectType) override;
298 std::string GetStaticObjectName(void* symbol) const override;
299 void SetStaticObjectName(void* symbol, const std::string& staticObjectName) override;
300 std::string GetVmtObjectName(void* symbol) const override;
301 void SetVmtObjectName(void* symbol, const std::string& vmtObjectName) override;
302 std::string GetImtArrayObjectName(void* symbol) const override;
303 void SetImtArrayObjectName(void* symbol, const std::string& imtArrayObjectName) override;
304 uint64_t GetClassTypeSizeInBits(void* classIrType) override;
305 uint64_t GetClassTypeAlignmentInBits(void* classIrType) override;
306 void AddInlineFunctionAttribute(void* function) override;
307 void SetFunctionLinkage(void* function, bool setInline) override;
308 void SetFunctionLinkageToLinkOnceODRLinkage(void* function) override;
309 void SetFunctionCallConventionToStdCall(void* function) override;
310 void* CreateSubroutineType(const std::std::vector<void*>&elementTypes) override;
311 unsigned GetPureVirtualVirtuality() override;
312 unsigned GetVirtualVirtuality() override;
313 unsigned GetFunctionFlags(bool isStatic, unsigned accessFlags, bool isExplicit) override;
314 void* CreateDIMethod(const std::string& name, const std::string& mangledName, const Span& span, const boost::uuids::uuid& moduleId, void* subroutineType, unsigned virtuality, unsigned vtableIndex, void* vtableHolder,
315 unsigned flags) override;
316 void* CreateDIFunction(const std::string& name, const std::string& mangledName, const Span& span, const boost::uuids::uuid& moduleId, void* subroutineType, unsigned flags) override;
317 void SetDISubprogram(void* function, void* subprogram) override;
318 void* CreateAlloca(void* irType) override;
319 void* CreateDIParameterVariable(const std::string& name, int index, const Span& span, const boost::uuids::uuid& moduleId, void* irType, void* allocaInst) override;
320 void* CreateDIAutoVariable(const std::string& name, const Span& span, const boost::uuids::uuid& moduleId, void* irType, void* allocaInst) override;
321 void* GetFunctionArgument(void* function, int argumentIndex) override;
322 void SetDebugLoc(void* callInst) override;
323 void* CreateRet(void* value) override;
324 void* CreateRetVoid() override;
325 void SetPersonalityFunction(void* function, void* personalityFunction) override;
326 void AddNoUnwindAttribute(void* function) override;
327 void AddUWTableAttribute(void* function) override;
328 void* CreateLexicalBlock(const Span& span, const boost::uuids::uuid& moduleId) override;
329 void* CreateSwitch(void* condition, void* defaultDest, unsigned numCases) override;
330 void AddCase(void* switchInst, void* caseValue, void* caseDest) override;
331 void* CreateCleanupPadWithParent(void* parentPad, const std::std::vector<void*>&args) override;
332 void* CreateCleanupPad(const std::std::vector<void*>&args) override;
333 void* CreateCleanupRet(void* cleanupPad, void* unwindTarget) override;
334 void* CreateCatchRet(void* catchPad, void* returnTarget) override;
335 void* CreateCatchSwitch(void* unwindBlock) override;
336 void* CreateCatchSwitchWithParent(void* parentPad, void* unwindBlock) override;
337 void AddHandlerToCatchSwitch(void* catchSwitch, void* target) override;
338 void* CreateCatchPad(void* parentPad, const std::std::vector<void*>&args) override;
339 void SetLineNumber(int32_t lineNumber) override { emittingDelegate->SetLineNumber(lineNumber); }
340 void* HandlerBlock() override { return emittingDelegate->HandlerBlock(); }
341 void* CleanupBlock() override { return emittingDelegate->CleanupBlock(); }
342 bool NewCleanupNeeded() override { return emittingDelegate->NewCleanupNeeded(); }
343 void CreateCleanup() override { emittingDelegate->CreateCleanup(); }
344 std::string GetSourceFilePath(const Span& span, const boost::uuids::uuid& moduleId) { return emittingDelegate->GetSourceFilePath(span, moduleId); }
345 cmajor::ir::Pad* CurrentPad() override { return emittingDelegate->CurrentPad(); }
346 void* CreateClassDIType(void* classPtr) override { return emittingDelegate->CreateClassDIType(classPtr); }
347 void* GetGlobalStringPtr(int stringId) override { return emittingDelegate->GetGlobalStringPtr(stringId); }
348 void* GetGlobalWStringConstant(int stringId) override { return emittingDelegate->GetGlobalWStringConstant(stringId); }
349 void* GetGlobalUStringConstant(int stringId) override { return emittingDelegate->GetGlobalUStringConstant(stringId); }
350 void* GetGlobalUuidConstant(int uuidId) override { return emittingDelegate->GetGlobalUuidConstant(uuidId); }
351 void* GenerateTrap(const std::std::vector<void*>&args) override;
352 void SetCompileUnitId(const std::string& compileUnitId_) override;
353 void* GetClsIdValue(const std::string& typeId) override;
354 void* CreateMDBool(bool value) override;
355 void* CreateMDLong(int64_t value) override;
356 void* CreateMDString(const std::string& value) override;
357 void* CreateMDStructRef(int id) override;
358 void* CreateMDStruct() override;
359 void* CreateMDBasicBlockRef(void* bb) override;
360 int GetMDStructId(void* mdStruct) override;
361 void AddMDItem(void* mdStruct, const std::string& fieldName, void* mdItem) override;
362 void SetFunctionMdId(void* function, int mdId) override;
363 void SetCurrentSourceSpan(int32_t line, int16_t scol, int16_t ecol) override;
364 void* GetMDStructRefForSourceFile(const std::string& sourceFileName) override;
365 void SetMetadataRef(void* inst, void* mdStructRef) override;
366 void FinalizeFunction(void* function, bool hasCleanup) override;
367 int Install(const std::string& str) override;
368 int Install(const std::u16string& str) override;
369 int Install(const std::u32string& str) override;
370 void* CreateLandingPad(void* lpType) override;
371 void SetLandindPadAsCleanup(void* landingPad) override;
372 void MoveAllocaIntoBasicBlock(void* allocaInst, void* lastAlloca, void* basicBlock) override;
373 void AddClauseToLangdingPad(void* landingPad, void* exceptionTypeId) override;
374 void* CreateExtractValue(void* aggregate, const std::std::vector<unsigned int>&indeces) override;
375 void* CreateInsertValue(void* aggregate, void* value, const std::std::vector<unsigned int>&indeces) override;
376 void* CreateUndefValue(void* type) override;
377 void CreateResume(void* exception) override;
378 void DebugPrintDebugInfo(const std::string& filePath) override;
379 void BeginSubstituteLineNumber(int32_t lineNumber) override;
380 void EndSubstituteLineNumber() override;
381 void SetBoundCompileUnit(void* boundCompileUnit_) override { }
382 void* GetBoundCompileUnit() const override { return nullptr; }
383 private:
384 cmajor::ir::EmittingContext& emittingContext;
385 cmajor::ir::EmittingDelegate* emittingDelegate;
386 llvm::LLVMContext& context;
387 llvm::IRBuilder<> builder;
388 llvm::Module* module;
389 llvm::DataLayout* dataLayout;
390 llvm::DICompileUnit* diCompileUnit;
391 llvm::DIFile* diFile;
392 std::unique_ptr<llvm::DIBuilder> diBuilder;
393 llvm::DIBuilder* currentDIBuilder;
394 cmajor::common::ColumnSpanProvider* columnSpanProvider;
395 int32_t compileUnitIndex;
396 std::string compileUnitId;
397 ValueStack stack;
398 llvm::Value* objectPointer;
399 llvm::Function* function;
400 llvm::BasicBlock* currentBasicBlock;
401 std::vector<llvm::DIScope*> scopes;
402 llvm::DebugLoc currentDebugLocation;
403 bool inPrologue;
404 std::unordered_map<int32_t, llvm::DIFile*> fileMap;
405 std::unordered_map<boost::uuids::uuid, llvm::DIType*, boost::boost::hash<boost::uuids::uuid>>diTypeTypeIdMap;
406 std::unordered_map<llvm::DIType*, std::string> diTypeNameMap;
407 std::unordered_map<std::std::pair<boost::uuids::uuid, int32_t>, llvm::DIDerivedType*, boost::boost::hash<std::std::pair<boost::uuids::uuid, int32_t>>>diMemberTypeMap;
408 std::unordered_map<llvm::DIType*, boost::uuids::uuid> fwdDeclarationMap;
409 std::unordered_map<boost::uuids::uuid, void*, boost::boost::hash<boost::uuids::uuid>>classPtrMap;
410 std::unordered_map<void*, std::string> classNameMap;
411 std::unordered_map<void*, llvm::Value*> irObjectMap;
412 std::unordered_map<void*, llvm::Type*> irTypeMap;
413 std::unordered_map<boost::uuids::uuid, llvm::Type*, boost::boost::hash<boost::uuids::uuid>>irTypeTypeIdMap;
414 std::unordered_map<void*, llvm::FunctionType*> functionIrTypeMap;
415 std::unordered_set<void*> vmtObjectCreatedSet;
416 std::unordered_set<void*> staticObjectCreatedSet;
417 std::unordered_map<void*, llvm::StructType*> staticTypeMap;
418 std::unordered_map<void*, llvm::ArrayType*> vmtObjectTypeMap;
419 std::unordered_map<void*, std::string> staticObjectNameMap;
420 std::unordered_map<void*, std::string> vmtObjectNameMap;
421 std::unordered_map<void*, std::string> imtArrayObjectNameMap;
422 int32_t currentLineNumber;
423 bool substituteLineNumber;
424 };
425
426 }
427
428 #endif // CMAJOR_CMLLVM_EMITTER_INCLUDED