1 // =================================
  2 // Copyright (c) 2021 Seppo Laakko
  3 // Distributed under the MIT license
  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* retTypeconst std::std::vector<void*>&paramTypes) override;
 47     void* GetIrTypeForVariableParamFunction(void* retType) override;
 48     void* GetIrTypeForArrayType(void* elementTypeint64_t size) override;
 49     void* GetIrTypeForClassType(const std::std::vector<void*>&elementTypes) override;
 50     void* CreateFwdIrTypeForClassType() override;
 51     void SetFwdIrTypeBody(void* forwardDeclaredTypeconst std::std::vector<void*>&elementTypes) override;
 52     void* GetIrTypeForDelegateType(void* retTypeconst std::std::vector<void*>&paramTypes) 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& typeIdvoid* 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* arrayIrTypeconst 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* irTypeconst 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* arrayIrTypeconst std::std::vector<void*>&elementConstantsconststd::string&prefix) override;
 97     void* CreateIrValueForConstantStruct(void* structIrTypeconst std::std::vector<void*>&elementConstants) override;
 98     void* CreateIrValueForUuid(void* uuidConstant) override;
 99     void* GetConversionValue(void* typevoid* 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* elementDITypeconst std::std::vector<void*>&elements) override;
116     void* CreateIrDIForwardDeclaration(void* irTypeconst std::string& nameconst std::string& mangledNameconst Span& spanconst boost::uuids::uuid& moduleId) override;
117     uint64_t GetOffsetInBits(void* classIrTypeint layoutIndex) override;
118     void* CreateDITypeForClassType(void* irTypeconst std::std::vector<void*>&memberVariableElementsconstSpan&classSpanconstboost::uuids::uuid&moduleIdconst std::string& namevoid* vtableHolderClass
119         const std::string& mangledNamevoid* baseClassDIType) override;
120     void* CreateDITypeForEnumConstant(const std::string& nameint64_t value) override;
121     void* CreateDITypeForEnumType(const std::string& nameconst std::string& mangledNameconst Span& spanconst boost::uuids::uuid& moduleIdconst std::std::vector<void*>&enumConstantElements
122         uint64_t sizeInBitsuint32_t alignInBitsvoid* underlyingDIType) override;
123     void MapFwdDeclaration(void* fwdDeclarationconst boost::uuids::uuid& typeId) override;
124     void* GetDITypeByTypeId(const boost::uuids::uuid& typeId) const override;
125     void SetDITypeByTypeId(const boost::uuids::uuid& typeIdvoid* diTypeconst std::string& typeName) override;
126     void* GetDIMemberType(const std::std::pair<boost::uuids::uuidint32_t>&memberVariableId) override;
127     void SetDIMemberType(const std::std::pair<boost::uuids::uuidint32_t>&memberVariableIdvoid*diType) override;
128     void* CreateDIMemberType(void* scopeconst std::string& nameconst Span& spanconst boost::uuids::uuid& moduleIduint64_t sizeInBitsuint64_t alignInBitsuint64_t offsetInBitsvoid* 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& typeIdvoid* classPtrconst 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* arrayPtruint64_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* tryBlockvoid* catchBlock) override;
145     void SetCleanupBlock(void* cleanupBlock) override;
146     int GetBasicBlockId(void* basicBlock) override;
147     void CreateBr(void* targetBasicBlock) override;
148     void CreateCondBr(void* condvoid* trueBasicBlockvoid* falseBasicBlock) override;
149     void* CreateArrayIndexAddress(void* arrayPtrvoid* index) override;
150     void CreateStore(void* valuevoid* ptr) override;
151     void* CreateLoad(void* ptr) override;
152     void* CreateAdd(void* leftvoid* right) override;
153     void* CreateFAdd(void* leftvoid* right) override;
154     void* CreateSub(void* leftvoid* right) override;
155     void* CreateFSub(void* leftvoid* right) override;
156     void* CreateMul(void* leftvoid* right) override;
157     void* CreateFMul(void* leftvoid* right) override;
158     void* CreateUDiv(void* leftvoid* right) override;
159     void* CreateSDiv(void* leftvoid* right) override;
160     void* CreateFDiv(void* leftvoid* right) override;
161     void* CreateURem(void* leftvoid* right) override;
162     void* CreateSRem(void* leftvoid* right) override;
163     void* CreateAnd(void* leftvoid* right) override;
164     void* CreateOr(void* leftvoid* right) override;
165     void* CreateXor(void* leftvoid* right) override;
166     void* CreateShl(void* leftvoid* right) override;
167     void* CreateAShr(void* leftvoid* right) override;
168     void* CreateLShr(void* leftvoid* right) override;
169     void* CreateICmpEQ(void* leftvoid* right) override;
170     void* CreateFCmpOEQ(void* leftvoid* right) override;
171     void* CreateICmpULT(void* leftValuevoid* rightValue) override;
172     void* CreateICmpSLT(void* leftValuevoid* rightValue) override;
173     void* CreateFCmpOLT(void* leftValuevoid* rightValue) override;
174     void* CreateSExt(void* operandvoid* destinationType) override;
175     void* CreateZExt(void* operandvoid* destinationType) override;
176     void* CreateFPExt(void* operandvoid* destinationType) override;
177     void* CreateTrunc(void* operandvoid* destinationType) override;
178     void* CreateFPTrunc(void* operandvoid* destinationType) override;
179     void* CreateBitCast(void* operandvoid* destinationType) override;
180     void* CreateUIToFP(void* operandvoid* destinationType) override;
181     void* CreateSIToFP(void* operandvoid* destinationType) override;
182     void* CreateFPToUI(void* operandvoid* destinationType) override;
183     void* CreateFPToSI(void* operandvoid* destinationType) override;
184     void* CreateIntToPtr(void* intValuevoid* destinationType) override;
185     void* CreatePtrToInt(void* ptrValuevoid* 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& namevoid* type) override;
196     void* GetOrInsertAnyComdat(const std::string& namevoid* global) override;
197     void* GetOrInsertAnyFunctionComdat(const std::string& namevoid* function) override;
198     void SetInitializer(void* globalvoid* initializer) override;
199     void SetPrivateLinkage(void* global) override;
200     void* GetOrInsertFunction(const std::string& namevoid* typebool 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* calleeconst std::std::vector<void*>&args) override;
205     void* CreateCallInst(void* calleeconst std::std::vector<void*>&argsconststd::std::vector<void*>&bundlesconstSpan&span) override;
206     void* CreateCallInstToBasicBlock(void* calleeconst std::std::vector<void*>&argsconststd::std::vector<void*>&bundlesvoid*basicBlockconstSpan&span) override;
207     void* CreateInvoke(void* calleevoid* normalBlockvoid* unwindBlockconst std::std::vector<void*>&args) override;
208     void* CreateInvokeInst(void* calleevoid* normalBlockvoid* unwindBlockconst std::std::vector<void*>&argsconststd::std::vector<void*>&bundlesconstSpan&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* imtPtrint32_t methodIndexvoid* interfaceMethodType) override;
217     void* GetVmtPtr(void* thisPtrint32_t vmtPtrIndexvoid* vmtPtrType) override;
218     void* GetMethodPtr(void* vmtPtrint32_t vmtIndex) override;
219     void* GetImtArray(void* vmtObjectPtrint32_t imtsVmtIndexOffset) override;
220     void* GetImt(void* imtArrayint32_t interfaceIndex) override;
221     void* GetMemberVariablePtr(void* classPtrint32_t memberVariableLayoutIndex) override;
222     void* SizeOf(void* ptrType) override;
223     void* GetClassIdPtr(void* vmtPtrint32_t classIdVmtIndexOffset) override;
224     void* GetClassName(void* vmtPtrint32_t classNameVmtIndexOffset) override;
225     void* ComputeAddress(void* ptrvoid* index) override;
226     void* CreatePtrDiff(void* leftvoid* 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 fileIndexconst boost::uuids::uuid& sourceModuleIdconst 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 startNodeIdint32_t endNodeId) override;
259     void AddLocalVariable(const std::string& localVariableNameconst boost::uuids::uuid& typeIdvoid* 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& sourceFilePathconst std::string& compilerVersionbool optimizedcmajor::common::ColumnSpanProvider* columnSpanProvider_) override;
271     void FinalizeDebugInfo() override;
272     void EndDebugInfo() override;
273     void* CreateDebugInfoForNamespace(void* scopeconst 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& bcFilePathconst std::string& optBCFilePathconst std::string& optimizationFlags) override;
277     void Disassemble(const std::string& bcFilePathconst std::string& llFilePath) override;
278     void Compile(const std::string& bcFilePathconst std::string& objectFilePathint 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& spanconst boost::uuids::uuid& moduleId) override;
285     void ReplaceForwardDeclarations();
286     void* GetIrObject(void* symbol) const override;
287     void SetIrObject(void* symbolvoid* irObject) override;
288     void* GetFunctionIrType(void* symbol) const override;
289     void SetFunctionIrType(void* symbolvoid* 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* symbolvoid* type) override;
296     void* GetVmtObjectType(void* symbol) const override;
297     void SetVmtObjectType(void* symbolvoid* vmtObjectType) override;
298     std::string GetStaticObjectName(void* symbol) const override;
299     void SetStaticObjectName(void* symbolconst std::string& staticObjectName) override;
300     std::string GetVmtObjectName(void* symbol) const override;
301     void SetVmtObjectName(void* symbolconst std::string& vmtObjectName) override;
302     std::string GetImtArrayObjectName(void* symbol) const override;
303     void SetImtArrayObjectName(void* symbolconst 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* functionbool 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 isStaticunsigned accessFlagsbool isExplicit) override;
314     void* CreateDIMethod(const std::string& nameconst std::string& mangledNameconst Span& spanconst boost::uuids::uuid& moduleIdvoid* subroutineTypeunsigned virtualityunsigned vtableIndexvoid* vtableHolder
315         unsigned flags) override;
316     void* CreateDIFunction(const std::string& nameconst std::string& mangledNameconst Span& spanconst boost::uuids::uuid& moduleIdvoid* subroutineTypeunsigned flags) override;
317     void SetDISubprogram(void* functionvoid* subprogram) override;
318     void* CreateAlloca(void* irType) override;
319     void* CreateDIParameterVariable(const std::string& nameint indexconst Span& spanconst boost::uuids::uuid& moduleIdvoid* irTypevoid* allocaInst) override;
320     void* CreateDIAutoVariable(const std::string& nameconst Span& spanconst boost::uuids::uuid& moduleIdvoid* irTypevoid* allocaInst) override;
321     void* GetFunctionArgument(void* functionint argumentIndex) override;
322     void SetDebugLoc(void* callInst) override;
323     void* CreateRet(void* value) override;
324     void* CreateRetVoid() override;
325     void SetPersonalityFunction(void* functionvoid* personalityFunction) override;
326     void AddNoUnwindAttribute(void* function) override;
327     void AddUWTableAttribute(void* function) override;
328     void* CreateLexicalBlock(const Span& spanconst boost::uuids::uuid& moduleId) override;
329     void* CreateSwitch(void* conditionvoid* defaultDestunsigned numCases) override;
330     void AddCase(void* switchInstvoid* caseValuevoid* caseDest) override;
331     void* CreateCleanupPadWithParent(void* parentPadconst std::std::vector<void*>&args) override;
332     void* CreateCleanupPad(const std::std::vector<void*>&args) override;
333     void* CreateCleanupRet(void* cleanupPadvoid* unwindTarget) override;
334     void* CreateCatchRet(void* catchPadvoid* returnTarget) override;
335     void* CreateCatchSwitch(void* unwindBlock) override;
336     void* CreateCatchSwitchWithParent(void* parentPadvoid* unwindBlock) override;
337     void AddHandlerToCatchSwitch(void* catchSwitchvoid* target) override;
338     void* CreateCatchPad(void* parentPadconst 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& spanconst boost::uuids::uuid& moduleId) { return emittingDelegate->GetSourceFilePath(spanmoduleId); }
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* mdStructconst std::string& fieldNamevoid* mdItem) override;
362     void SetFunctionMdId(void* functionint mdId) override;
363     void SetCurrentSourceSpan(int32_t lineint16_t scolint16_t ecol) override;
364     void* GetMDStructRefForSourceFile(const std::string& sourceFileName) override;
365     void SetMetadataRef(void* instvoid* mdStructRef) override;
366     void FinalizeFunction(void* functionbool 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* allocaInstvoid* lastAllocavoid* basicBlock) override;
373     void AddClauseToLangdingPad(void* landingPadvoid* exceptionTypeId) override;
374     void* CreateExtractValue(void* aggregateconst std::std::vector<unsigned int>&indeces) override;
375     void* CreateInsertValue(void* aggregatevoid* valueconst 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_tllvm::DIFile*> fileMap;
405     std::unordered_map<boost::uuids::uuidllvm::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::uuidint32_t>llvm::DIDerivedType*boost::boost::hash<std::std::pair<boost::uuids::uuidint32_t>>>diMemberTypeMap;
408     std::unordered_map<llvm::DIType*boost::uuids::uuid> fwdDeclarationMap;
409     std::unordered_map<boost::uuids::uuidvoid*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::uuidllvm::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 } // namespace cmllvm
427 
428 #endif // CMAJOR_CMLLVM_EMITTER_INCLUDED