1 // =================================
   2 // Copyright (c) 2021 Seppo Laakko
   3 // Distributed under the MIT license
   4 // =================================
   5 
   6 #include <cmajor/cmllvm/Emitter.hpp>
   7 #include <soulng/util/Path.hpp>
   8 #include <soulng/util/System.hpp>
   9 #include <soulng/util/TextUtils.hpp>
  10 #include <soulng/util/MappedInputFile.hpp>
  11 #include <soulng/util/Process.hpp>
  12 #include <llvm/Support/raw_os_ostream.h>
  13 #include <llvm/IR/Module.h>
  14 #include <llvm/IR/Verifier.h>
  15 #include <llvm/IR/LegacyPassManager.h>
  16 #include <llvm/Bitcode/BitcodeWriter.h>
  17 #include <llvm/IRReader/IRReader.h>
  18 #include <llvm/Support/FileSystem.h>
  19 #include <llvm/Target/TargetMachine.h>
  20 #include <llvm/Config/llvm-config.h>
  21 #include <llvm/Support/SourceMgr.h>
  22 #include <boost/filesystem.hpp>
  23 #include <fstream>
  24 #include <iostream>
  25 #include <iomanip>
  26 #include <sstream>
  27 
  28 namespace cmllvm {
  29 
  30 using namespace soulng::util;
  31 
  32 const unsigned cmajorLanguageTag = llvm::dwarf::DW_LANG_C_plus_plus_11;
  33 
  34 Emitter::Emitter(llvm::LLVMContext& context_cmajor::ir::EmittingContext& emittingContext_) :
  35     cmajor::ir::Emitter(&stack)emittingContext(emittingContext_)emittingDelegate(nullptr)
  36     context(context_)builder(context)module(nullptr)dataLayout(nullptr)diCompileUnit(nullptr)diFile(nullptr)diBuilder(nullptr)columnSpanProvider(nullptr)compileUnitIndex(-1)
  37     stack()objectPointer(nullptr)function(nullptr)currentBasicBlock(nullptr)inPrologue(false)currentDIBuilder(nullptr)substituteLineNumber(false)currentLineNumber(-1)
  38 {
  39 }
  40 
  41 void* Emitter::GetIrTypeForBool()
  42 {
  43     return llvm::Type::getInt1Ty(context);
  44 }
  45 
  46 void* Emitter::GetIrTypeForSByte()
  47 {
  48     return llvm::Type::getInt8Ty(context);
  49 }
  50 
  51 void* Emitter::GetIrTypeForByte()
  52 {
  53     return llvm::Type::getInt8Ty(context);
  54 }
  55 
  56 void* Emitter::GetIrTypeForShort()
  57 {
  58     return llvm::Type::getInt16Ty(context);
  59 }
  60 
  61 void* Emitter::GetIrTypeForUShort()
  62 {
  63     return llvm::Type::getInt16Ty(context);
  64 }
  65 
  66 void* Emitter::GetIrTypeForInt()
  67 {
  68     return llvm::Type::getInt32Ty(context);
  69 }
  70 
  71 void* Emitter::GetIrTypeForUInt()
  72 {
  73     return llvm::Type::getInt32Ty(context);
  74 }
  75 
  76 void* Emitter::GetIrTypeForLong()
  77 {
  78     return llvm::Type::getInt64Ty(context);
  79 }
  80 
  81 void* Emitter::GetIrTypeForULong()
  82 {
  83     return llvm::Type::getInt64Ty(context);
  84 }
  85 
  86 void* Emitter::GetIrTypeForFloat()
  87 {
  88     return llvm::Type::getFloatTy(context);
  89 }
  90 
  91 void* Emitter::GetIrTypeForDouble()
  92 {
  93     return llvm::Type::getDoubleTy(context);
  94 }
  95 
  96 void* Emitter::GetIrTypeForChar()
  97 {
  98     return llvm::Type::getInt8Ty(context);
  99 }
 100 
 101 void* Emitter::GetIrTypeForWChar()
 102 {
 103     return llvm::Type::getInt16Ty(context);
 104 }
 105 
 106 void* Emitter::GetIrTypeForUChar()
 107 {
 108     return llvm::Type::getInt32Ty(context);
 109 }
 110 
 111 void* Emitter::GetIrTypeForVoid()
 112 {
 113     return llvm::Type::getVoidTy(context);
 114 }
 115 
 116 void* Emitter::GetIrTypeForFunction(void* retTypeconst std::std::vector<void*>&paramTypes)
 117 {
 118     std::vector<llvm::Type*> parameterTypes;
 119     for (void* paramType : paramTypes)
 120     {
 121         parameterTypes.push_back(static_cast<llvm::Type*>(paramType));
 122     }
 123     return llvm::FunctionType::get(static_cast<llvm::Type*>(retType)parameterTypesfalse);
 124 }
 125 
 126 void* Emitter::GetIrTypeForVariableParamFunction(void* retType)
 127 {
 128     return llvm::FunctionType::get(static_cast<llvm::Type*>(retType)true);
 129 }
 130 
 131 void* Emitter::GetIrTypeForArrayType(void* elementTypeint64_t size)
 132 {
 133     llvm::Type* elementIrType = static_cast<llvm::Type*>(elementType);
 134     return llvm::ArrayType::get(elementIrTypesize);
 135 }
 136 
 137 void* Emitter::GetIrTypeForClassType(const std::std::vector<void*>&elementTypes)
 138 {
 139     std::vector<llvm::Type*> elementIrTypes;
 140     for (void* elementType : elementTypes)
 141     {
 142         elementIrTypes.push_back(static_cast<llvm::Type*>(elementType));
 143     }
 144     return llvm::StructType::get(contextelementIrTypes);
 145 }
 146 
 147 void* Emitter::CreateFwdIrTypeForClassType()
 148 {
 149     return llvm::StructType::create(context);
 150 }
 151 
 152 void Emitter::SetFwdIrTypeBody(void* forwardDeclaredTypeconst std::std::vector<void*>&elementTypes)
 153 {
 154     llvm::StructType* structType = static_cast<llvm::StructType*>(forwardDeclaredType);
 155     std::vector<llvm::Type*> elementIrTypes;
 156     for (void* elementType : elementTypes)
 157     {
 158         elementIrTypes.push_back(static_cast<llvm::Type*>(elementType));
 159     }
 160     structType->setBody(elementIrTypes);
 161 }
 162 
 163 void* Emitter::GetIrTypeForDelegateType(void* retTypeconst std::std::vector<void*>&paramTypes)
 164 {
 165     llvm::Type* returnType = static_cast<llvm::Type*>(retType);
 166     std::vector<llvm::Type*> parameterTypes;
 167     for (void* paramType : paramTypes)
 168     {
 169         parameterTypes.push_back(static_cast<llvm::Type*>(paramType));
 170     }
 171     return llvm::PointerType::get(llvm::FunctionType::get(returnTypeparameterTypesfalse)0);
 172 }
 173 
 174 void* Emitter::GetIrTypeForVoidPtrType()
 175 {
 176     return llvm::Type::getInt8PtrTy(context);
 177 }
 178 
 179 void* Emitter::GetIrTypeForStructType(const std::std::vector<void*>&elementTypes)
 180 {
 181     std::vector<llvm::Type*> elementIrTypes;
 182     for (void* elementType : elementTypes)
 183     {
 184         elementIrTypes.push_back(static_cast<llvm::Type*>(elementType));
 185     }
 186     return llvm::StructType::get(contextelementIrTypes);
 187 }
 188 
 189 void* Emitter::CreateDITypeForEnumConstant(const std::string& nameint64_t value)
 190 {
 191     return diBuilder->createEnumerator(namevalue);
 192 }
 193 
 194 void* Emitter::CreateDITypeForEnumType(const std::string& nameconst std::string& mangledNameconst Span& spanconst boost::uuids::uuid& moduleIdconst std::std::vector<void*>&enumConstantElements
 195     uint64_t sizeInBitsuint32_t alignInBitsvoid* underlyingDIType)
 196 {
 197     std::vector<llvm::Metadata*> elements;
 198     for (void* element : enumConstantElements)
 199     {
 200         elements.push_back(static_cast<llvm::Metadata*>(element));
 201     }
 202     return diBuilder->createEnumerationType(nullptrnamestatic_cast<llvm::DIFile*>(GetDebugInfoForFile(spanmoduleId))span.linesizeInBitsalignInBits
 203         diBuilder->getOrCreateArray(elements)static_cast<llvm::DIType*>(underlyingDIType)mangledName);
 204 }
 205 
 206 void Emitter::MapFwdDeclaration(void* fwdDeclarationconst boost::uuids::uuid& typeId)
 207 {
 208     fwdDeclarationMap[static_cast<llvm::DIType*>(fwdDeclaration)] = typeId;
 209 }
 210 
 211 void Emitter::SaveObjectPointer(void* objectPointer_)
 212 {
 213     if (objectPointer == nullptr)
 214     {
 215         objectPointer = static_cast<llvm::Value*>(objectPointer_);
 216     }
 217 }
 218 
 219 void Emitter::SetFunctionName(const std::string& functionName)
 220 {
 221 }
 222 
 223 void Emitter::BeginScope()
 224 {
 225 }
 226 
 227 void Emitter::EndScope()
 228 {
 229 }
 230 
 231 int16_t Emitter::GetCurrentScopeId() const
 232 {
 233     return 0;
 234 }
 235 
 236 void Emitter::SetCurrentScopeId(int16_t scopeId)
 237 {
 238 }
 239 
 240 int32_t Emitter::AddControlFlowGraphNode()
 241 {
 242     return -1;
 243 }
 244 
 245 void Emitter::SetCurrentControlFlowGraphNodeId(int32_t controlFlowGraphNodeId)
 246 {
 247 }
 248 
 249 void Emitter::AddControlFlowGraphEdge(int32_t startNodeIdint32_t endNodeId)
 250 {
 251 }
 252 
 253 void Emitter::AddLocalVariable(const std::string& localVariableNameconst boost::uuids::uuid& typeIdvoid* irObject)
 254 {
 255 }
 256 
 257 void Emitter::BeginInstructionFlag(int16_t flag)
 258 {
 259 }
 260 
 261 void Emitter::EndInstructionFlag(int16_t flag)
 262 {
 263 }
 264 
 265 void Emitter::PushScope(void* scope)
 266 {
 267     scopes.push_back(static_cast<llvm::DIScope*>(scope));
 268 }
 269 
 270 void Emitter::PopScope()
 271 {
 272     scopes.pop_back();
 273 }
 274 
 275 void* Emitter::CurrentScope()
 276 {
 277     llvm::DIScope* currentScope = diCompileUnit;
 278     if (!scopes.empty())
 279     {
 280         currentScope = scopes.back();
 281     }
 282     return currentScope;
 283 }
 284 
 285 int Emitter::GetColumn(const Span& span) const
 286 {
 287     int column = 1;
 288     if (columnSpanProvider)
 289     {
 290         column = columnSpanProvider->GetColumn(span);
 291     }
 292     return column;
 293 }
 294 
 295 void Emitter::StartDebugInfo(const std::string& sourceFilePathconst std::string& compilerVersionbool optimizedcmajor::common::ColumnSpanProvider* columnSpanProvider_)
 296 {
 297     module->addModuleFlag(llvm::Module::Warning"Debug Info Version"llvm::DEBUG_METADATA_VERSION);
 298 #ifdef _WIN32
 299 
 300 #endif
 301     diBuilder.reset(new llvm::DIBuilder(*module));
 302     currentDIBuilder = diBuilder.get();
 303     llvm::DIFile* sourceFile = diBuilder->createFile(Path::GetFileName(sourceFilePath)Path::GetDirectoryName(sourceFilePath));
 304     SetDIFile(sourceFile);
 305     llvm::DICompileUnit* diCompileUnit = diBuilder->createCompileUnit(cmajorLanguageTagsourceFile"Cmajor compiler version " + compilerVersionoptimized""0);
 306     SetDICompileUnit(diCompileUnit);
 307     SetColumnSpanProvider(columnSpanProvider_);
 308     PushScope(sourceFile);
 309 }
 310 
 311 void Emitter::FinalizeDebugInfo()
 312 {
 313     ReplaceForwardDeclarations();
 314     diBuilder->finalize();
 315 }
 316 
 317 void Emitter::EndDebugInfo()
 318 {
 319     PopScope();
 320     diBuilder.reset();
 321 }
 322 
 323 void* Emitter::CreateDebugInfoForNamespace(void* scopeconst std::string& name)
 324 {
 325     llvm::DINamespace* ns = diBuilder->createNameSpace(static_cast<llvm::DIScope*>(CurrentScope())namefalse);
 326     return ns;
 327 }
 328 
 329 void Emitter::EmitIrText(const std::string& filePath)
 330 {
 331     std::ofstream llFile(filePath);
 332     llvm::raw_os_ostream llOs(llFile);
 333     module->print(llOsnullptr);
 334     llOs.flush();
 335 }
 336 
 337 void Emitter::EmitIrFile(const std::string& filePath)
 338 {
 339     std::ofstream bcFile(filePathstd::ios::binary);
 340     llvm::raw_os_ostream bcOs(bcFile);
 341     llvm::WriteBitcodeToFile(*modulebcOs);
 342     bcOs.flush();
 343 }
 344 
 345 void Emitter::Optimize(const std::string& bcFilePathconst std::string& optBCFilePathconst std::string& optimizationFlags)
 346 {
 347     std::string optErrors;
 348     std::string optCommandLine;
 349     optCommandLine.append("opt ").append(optimizationFlags).append(" -o=").append(QuotedPath(optBCFilePath)).append(" ").append(QuotedPath(bcFilePath));
 350     try
 351     {
 352         Process process(optCommandLineProcess::Redirections::processStdErr);
 353         optErrors = process.ReadToEnd(Process::StdHandle::stdErr);
 354         process.WaitForExit();
 355         int exitCode = process.ExitCode();
 356         if (exitCode != 0)
 357         {
 358             throw std::runtime_error("executing '" + optCommandLine + "' failed with exit code: " + std::to_string(exitCode));
 359         }
 360     }
 361     catch (const std::exception& ex;)
 362     {
 363         throw std::runtime_error("optimization of '" + bcFilePath + "' failed: " + ex.what() + ":\nerrors:\n" + optErrors);
 364     }
 365 }
 366 
 367 void Emitter::Disassemble(const std::string& bcFilePathconst std::string& llFilePath)
 368 {
 369     std::string disCommandLine;
 370     std::string errors;
 371     disCommandLine.append("llvm-dis -o=").append(QuotedPath(llFilePath)).append(" ").append(QuotedPath(bcFilePath));
 372     try
 373     {
 374         Process process(disCommandLineProcess::Redirections::processStdErr);
 375         errors = process.ReadToEnd(Process::StdHandle::stdErr);
 376         process.WaitForExit();
 377         int exitCode = process.ExitCode();
 378         if (exitCode != 0)
 379         {
 380             throw std::runtime_error("executing '" + disCommandLine + "' failed with exit code: " + std::to_string(exitCode));
 381         }
 382     }
 383     catch (const std::exception& ex;)
 384     {
 385         throw std::runtime_error("disassembly of '" + bcFilePath + "' failed: " + ex.what() + ":\nerrors:\n" + errors);
 386     }
 387 }
 388 
 389 void Emitter::Compile(const std::string& bcFilePathconst std::string& objectFilePathint optimizationLevel)
 390 {
 391 /*
 392     llvm::SMDiagnostic error;
 393     std::unique_ptr<llvm::Module> mod = llvm::parseIRFile(bcFilePath, error, context);
 394     auto it = mod->begin();
 395     bool modified = false;
 396     while (it != mod->end())
 397     {
 398         if (it->getUnnamedAddr() == llvm::GlobalValue::UnnamedAddr::Local)
 399         {
 400             it->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::None);
 401             modified = true;
 402         }
 403         ++it;
 404     }
 405     if (modified)
 406     {
 407         std::ofstream bcFile(bcFilePath, std::ios::binary);
 408         llvm::raw_os_ostream bcOs(bcFile);
 409         llvm::WriteBitcodeToFile(*mod, bcOs);
 410         bcOs.flush();
 411     }
 412 */
 413 
 414     std::string llcCommandLine;
 415     std::string errors;
 416     llcCommandLine.append("llc -O=").append(std::to_string(optimizationLevel)).append(" --filetype=obj").append(" -o=").append(QuotedPath(objectFilePath).append(" ").append(QuotedPath(bcFilePath)));
 417     try
 418     {
 419         Process process(llcCommandLineProcess::Redirections::processStdErr);
 420         errors = process.ReadToEnd(Process::StdHandle::stdErr);
 421         process.WaitForExit();
 422         int exitCode = process.ExitCode();
 423         if (exitCode != 0)
 424         {
 425             throw std::runtime_error("executing '" + llcCommandLine + "' failed with exit code: " + std::to_string(exitCode));
 426         }
 427     }
 428     catch (const std::exception&  ex;)
 429     {
 430         throw std::runtime_error("compilation of '" + bcFilePath + "' failed: " + ex.what() + ":\nerrors:\n" + errors);
 431     }
 432 }
 433 
 434 void Emitter::VerifyModule()
 435 {
 436     std::string errorMessageStore;
 437     llvm::raw_string_ostream errorMessage(errorMessageStore);
 438     if (llvm::verifyModule(*module&errorMessage))
 439     {
 440         throw std::runtime_error("Emitter: verification of module '" + module->getSourceFileName() + "' failed. " + errorMessage.str());
 441     }
 442 }
 443 
 444 void Emitter::EmitObjectCodeFile(const std::string& objectFilePath)
 445 {
 446     llvm::legacy::PassManager passManager;
 447     std::error_code errorCode;
 448     llvm::raw_fd_ostream objectFile(objectFilePatherrorCodellvm::sys::fs::F_None);
 449 #if (LLVM_VERSION_MAJOR >= 10)
 450     if (static_cast<llvm::TargetMachine*>(emittingContext.TargetMachine())->addPassesToEmitFile(passManagerobjectFilenullptrllvm::CodeGenFileType::CGFT_ObjectFile))
 451     {
 452         throw std::runtime_error("Emitter: cannot emit object code file '" + objectFilePath + "': addPassesToEmitFile failed");
 453     }
 454 #else
 455     if (static_cast<llvm::TargetMachine*>(emittingContext.TargetMachine())->addPassesToEmitFile(passManagerobjectFilenullptrllvm::TargetMachine::CodeGenFileType::CGFT_ObjectFile))
 456     {
 457         throw std::runtime_error("Emitter: cannot emit object code file '" + objectFilePath + "': addPassesToEmitFile failed");
 458     }
 459 #endif
 460     passManager.run(*module);
 461     objectFile.flush();
 462     if (objectFile.has_error())
 463     {
 464         throw std::runtime_error("Emitter: could not emit object code file '" + objectFilePath + "': " + PlatformStringToUtf8(errorCode.message()));
 465     }
 466 }
 467 
 468 llvm::DebugLoc Emitter::GetDebugLocation(const Span& span)
 469 {
 470     if (!diCompileUnit || !span.Valid() || !currentDIBuilder) return llvm::DebugLoc();
 471     int column = GetColumn(span);
 472     return llvm::DebugLoc::get(span.linecolumnstatic_cast<llvm::DIScope*>(CurrentScope()));
 473 }
 474 
 475 void Emitter::ResetCurrentDebugLocation()
 476 {
 477     currentDebugLocation = llvm::DebugLoc();
 478     builder.SetCurrentDebugLocation(currentDebugLocation);
 479 }
 480 
 481 void Emitter::SetCurrentDebugLocation(const Span& span)
 482 {
 483     if (!diCompileUnit || !currentDIBuilder) return;
 484     if (inPrologue || !span.Valid())
 485     {
 486         currentDebugLocation = llvm::DebugLoc();
 487         builder.SetCurrentDebugLocation(currentDebugLocation);
 488     }
 489     else
 490     {
 491         currentDebugLocation = GetDebugLocation(span);
 492         builder.SetCurrentDebugLocation(currentDebugLocation);
 493     }
 494 }
 495 
 496 void* Emitter::GetArrayBeginAddress(void* arrayPtr)
 497 {
 498     ArgVector elementIndeces;
 499     elementIndeces.push_back(builder.getInt64(0));
 500     elementIndeces.push_back(builder.getInt64(0));
 501     llvm::Value* beginPtr = builder.CreateGEP(static_cast<llvm::Value*>(arrayPtr)elementIndeces);
 502     return beginPtr;
 503 }
 504 
 505 void* Emitter::GetArrayEndAddress(void* arrayPtruint64_t size)
 506 {
 507     ArgVector elementIndeces;
 508     elementIndeces.push_back(builder.getInt64(0));
 509     elementIndeces.push_back(builder.getInt64(size));
 510     llvm::Value* endPtr = builder.CreateGEP(static_cast<llvm::Value*>(arrayPtr)elementIndeces);
 511     return endPtr;
 512 
 513 }
 514 
 515 void* Emitter::CreateBasicBlock(const std::string& name)
 516 {
 517     return llvm::BasicBlock::Create(contextnameFunction());
 518 }
 519 
 520 void* Emitter::CreateIncludeBasicBlockInstruction(void* basicBlock)
 521 {
 522     return nullptr;
 523 }
 524 
 525 void Emitter::PushParentBlock()
 526 {
 527 }
 528 
 529 void Emitter::PopParentBlock()
 530 {
 531 }
 532 
 533 void Emitter::SetHandlerBlock(void* tryBlockvoid* catchBlock)
 534 {
 535 }
 536 
 537 void Emitter::SetCleanupBlock(void* cleanupBlock)
 538 {
 539 }
 540 
 541 int Emitter::GetBasicBlockId(void* basicBlock)
 542 {
 543     return -1;
 544 }
 545 
 546 void Emitter::CreateBr(void* targetBasicBlock)
 547 {
 548     builder.CreateBr(static_cast<llvm::BasicBlock*>(targetBasicBlock));
 549 }
 550 
 551 void Emitter::CreateCondBr(void* condvoid* trueBasicBlockvoid* falseBasicBlock)
 552 {
 553     builder.CreateCondBr(static_cast<llvm::Value*>(cond)static_cast<llvm::BasicBlock*>(trueBasicBlock)static_cast<llvm::BasicBlock*>(falseBasicBlock));
 554 }
 555 
 556 void* Emitter::CreateArrayIndexAddress(void* arrayPtrvoid* index)
 557 {
 558     ArgVector elementIndeces;
 559     elementIndeces.push_back(builder.getInt64(0));
 560     elementIndeces.push_back(static_cast<llvm::Value*>(index));
 561     return builder.CreateGEP(static_cast<llvm::Value*>(arrayPtr)elementIndeces);
 562 }
 563 
 564 void Emitter::CreateStore(void* valuevoid* ptr)
 565 {
 566     builder.CreateStore(static_cast<llvm::Value*>(value)static_cast<llvm::Value*>(ptr));
 567 }
 568 
 569 void* Emitter::CreateLoad(void* ptr)
 570 {
 571     return builder.CreateLoad(static_cast<llvm::Value*>(ptr));
 572 }
 573 
 574 void* Emitter::CreateAdd(void* leftvoid* right)
 575 {
 576     return builder.CreateAdd(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 577 }
 578 
 579 void* Emitter::CreateFAdd(void* leftvoid* right)
 580 {
 581     return builder.CreateFAdd(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 582 }
 583 
 584 void* Emitter::CreateSub(void* leftvoid* right)
 585 {
 586     return builder.CreateSub(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 587 }
 588 
 589 void* Emitter::CreateFSub(void* leftvoid* right)
 590 {
 591     return builder.CreateFSub(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 592 }
 593 
 594 void* Emitter::CreateMul(void* leftvoid* right)
 595 {
 596     return builder.CreateMul(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 597 }
 598 
 599 void* Emitter::CreateFMul(void* leftvoid* right)
 600 {
 601     return builder.CreateFMul(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 602 }
 603 
 604 void* Emitter::CreateUDiv(void* leftvoid* right)
 605 {
 606     return builder.CreateUDiv(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 607 }
 608 
 609 void* Emitter::CreateSDiv(void* leftvoid* right)
 610 {
 611     return builder.CreateSDiv(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 612 }
 613 
 614 void* Emitter::CreateFDiv(void* leftvoid* right)
 615 {
 616     return builder.CreateFDiv(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 617 }
 618 
 619 void* Emitter::CreateURem(void* leftvoid* right)
 620 {
 621     return builder.CreateURem(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 622 }
 623 
 624 void* Emitter::CreateSRem(void* leftvoid* right)
 625 {
 626     return builder.CreateSRem(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 627 }
 628 
 629 void* Emitter::CreateAnd(void* leftvoid* right)
 630 {
 631     return builder.CreateAnd(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 632 }
 633 
 634 void* Emitter::CreateOr(void* leftvoid* right)
 635 {
 636     return builder.CreateOr(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 637 }
 638 
 639 void* Emitter::CreateXor(void* leftvoid* right)
 640 {
 641     return builder.CreateXor(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 642 }
 643 
 644 void* Emitter::CreateShl(void* leftvoid* right)
 645 {
 646     return builder.CreateShl(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 647 }
 648 
 649 void* Emitter::CreateAShr(void* leftvoid* right)
 650 {
 651     return builder.CreateAShr(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 652 }
 653 
 654 void* Emitter::CreateLShr(void* leftvoid* right)
 655 {
 656     return builder.CreateLShr(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 657 }
 658 
 659 void* Emitter::CreateICmpEQ(void* leftvoid* right)
 660 {
 661     return builder.CreateICmpEQ(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 662 }
 663 
 664 void* Emitter::CreateFCmpOEQ(void* leftvoid* right)
 665 {
 666     return builder.CreateFCmpOEQ(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
 667 }
 668 
 669 void* Emitter::CreateICmpULT(void* leftValuevoid* rightValue)
 670 {
 671     return builder.CreateICmpULT(static_cast<llvm::Value*>(leftValue)static_cast<llvm::Value*>(rightValue));
 672 }
 673 
 674 void* Emitter::CreateICmpSLT(void* leftValuevoid* rightValue)
 675 {
 676     return builder.CreateICmpSLT(static_cast<llvm::Value*>(leftValue)static_cast<llvm::Value*>(rightValue));
 677 }
 678 
 679 void* Emitter::CreateFCmpOLT(void* leftValuevoid* rightValue)
 680 {
 681     return builder.CreateFCmpOLT(static_cast<llvm::Value*>(leftValue)static_cast<llvm::Value*>(rightValue));
 682 }
 683 
 684 void* Emitter::CreateSExt(void* operandvoid* destinationType)
 685 {
 686     return builder.CreateSExt(static_cast<llvm::Value*>(operand)static_cast<llvm::Type*>(destinationType));
 687 }
 688 
 689 void* Emitter::CreateZExt(void* operandvoid* destinationType)
 690 {
 691     return builder.CreateZExt(static_cast<llvm::Value*>(operand)static_cast<llvm::Type*>(destinationType));
 692 }
 693 
 694 void* Emitter::CreateFPExt(void* operandvoid* destinationType)
 695 {
 696     return builder.CreateFPExt(static_cast<llvm::Value*>(operand)static_cast<llvm::Type*>(destinationType));
 697 }
 698 
 699 void* Emitter::CreateTrunc(void* operandvoid* destinationType)
 700 {
 701     return builder.CreateTrunc(static_cast<llvm::Value*>(operand)static_cast<llvm::Type*>(destinationType));
 702 }
 703 
 704 void* Emitter::CreateFPTrunc(void* operandvoid* destinationType)
 705 {
 706     return builder.CreateFPTrunc(static_cast<llvm::Value*>(operand)static_cast<llvm::Type*>(destinationType));
 707 }
 708 
 709 void* Emitter::CreateBitCast(void* operandvoid* destinationType)
 710 {
 711     return builder.CreateBitCast(static_cast<llvm::Value*>(operand)static_cast<llvm::Type*>(destinationType));
 712 }
 713 
 714 void* Emitter::CreateUIToFP(void* operandvoid* destinationType)
 715 {
 716     return builder.CreateUIToFP(static_cast<llvm::Value*>(operand)static_cast<llvm::Type*>(destinationType));
 717 }
 718 
 719 void* Emitter::CreateSIToFP(void* operandvoid* destinationType)
 720 {
 721     return builder.CreateSIToFP(static_cast<llvm::Value*>(operand)static_cast<llvm::Type*>(destinationType));
 722 }
 723 
 724 void* Emitter::CreateFPToUI(void* operandvoid* destinationType)
 725 {
 726     return builder.CreateFPToUI(static_cast<llvm::Value*>(operand)static_cast<llvm::Type*>(destinationType));
 727 }
 728 
 729 void* Emitter::CreateFPToSI(void* operandvoid* destinationType)
 730 {
 731     return builder.CreateFPToSI(static_cast<llvm::Value*>(operand)static_cast<llvm::Type*>(destinationType));
 732 }
 733 
 734 void* Emitter::CreateIntToPtr(void* intValuevoid* destinationType)
 735 {
 736     return builder.CreateIntToPtr(static_cast<llvm::Value*>(intValue)static_cast<llvm::Type*>(destinationType));
 737 }
 738 
 739 void* Emitter::CreatePtrToInt(void* ptrValuevoid* destinationType)
 740 {
 741     return builder.CreatePtrToInt(static_cast<llvm::Value*>(ptrValue)static_cast<llvm::Type*>(destinationType));
 742 }
 743 
 744 void* Emitter::CreateNot(void* value)
 745 {
 746     return builder.CreateNot(static_cast<llvm::Value*>(value));
 747 }
 748 
 749 void* Emitter::CreateNeg(void* value)
 750 {
 751     return builder.CreateNeg(static_cast<llvm::Value*>(value));
 752 }
 753 
 754 void* Emitter::CreateFNeg(void* value)
 755 {
 756     return builder.CreateFNeg(static_cast<llvm::Value*>(value));
 757 }
 758 
 759 void* Emitter::CreateNop()
 760 {
 761     return nullptr;
 762 }
 763 
 764 void* Emitter::CreateSave()
 765 {
 766     return nullptr;
 767 }
 768 
 769 void* Emitter::CreateBeginTry()
 770 {
 771     return nullptr;
 772 }
 773 
 774 void* Emitter::CreateEndTry(void* nextDest)
 775 {
 776     return nullptr;
 777 }
 778 
 779 void* Emitter::CreateBeginCatch()
 780 {
 781     return nullptr;
 782 }
 783 
 784 void* Emitter::CreateEndCatch(void* nextDest)
 785 {
 786     return nullptr;
 787 }
 788 
 789 void* Emitter::GetOrInsertGlobal(const std::string& namevoid* type)
 790 {
 791     return module->getOrInsertGlobal(namestatic_cast<llvm::Type*>(type));
 792 }
 793 
 794 void* Emitter::GetOrInsertAnyComdat(const std::string& namevoid* global)
 795 {
 796     llvm::GlobalVariable* globalVar = llvm::cast<llvm::GlobalVariable>(static_cast<llvm::Constant*>(global));
 797     llvm::Comdat* comdat = module->getOrInsertComdat(name);
 798     comdat->setSelectionKind(llvm::Comdat::SelectionKind::Any);
 799     globalVar->setComdat(comdat);
 800     return comdat;
 801 }
 802 
 803 void* Emitter::GetOrInsertAnyFunctionComdat(const std::string& namevoid* function)
 804 {
 805     llvm::Comdat* comdat = module->getOrInsertComdat(name);
 806     comdat->setSelectionKind(llvm::Comdat::SelectionKind::Any);
 807     static_cast<llvm::Function*>(function)->setComdat(comdat);
 808     return comdat;
 809 }
 810 
 811 void Emitter::SetInitializer(void* globalvoid* initializer)
 812 {
 813     llvm::GlobalVariable* globalVar = llvm::cast<llvm::GlobalVariable>(static_cast<llvm::Constant*>(global));
 814     globalVar->setInitializer(static_cast<llvm::Constant*>(initializer));
 815 }
 816 
 817 void Emitter::SetPrivateLinkage(void* global)
 818 {
 819     llvm::GlobalVariable* globalVar = llvm::cast<llvm::GlobalVariable>(static_cast<llvm::Constant*>(global));
 820     globalVar->setLinkage(llvm::GlobalValue::PrivateLinkage);
 821 }
 822 
 823 void* Emitter::GetOrInsertFunction(const std::string& namevoid* typebool nothrow)
 824 {
 825 #if (LLVM_VERSION_MAJOR >= 9)
 826     return module->getOrInsertFunction(namestatic_cast<llvm::FunctionType*>(type)).getCallee();
 827 #else
 828     return module->getOrInsertFunction(namestatic_cast<llvm::FunctionType*>(type));
 829 #endif
 830 }
 831 
 832 void* Emitter::CreateGlobalStringPtr(const std::string& name)
 833 {
 834     llvm::Constant* strConstant = llvm::ConstantDataArray::getString(contextname);
 835     llvm::GlobalVariable* gv = new llvm::GlobalVariable(*modulestrConstant->getType()truellvm::GlobalValue::PrivateLinkagestrConstant""nullptrllvm::GlobalVariable::NotThreadLocal0);
 836     gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
 837     gv->setAlignment(llvm::MaybeAlign(1));
 838     llvm::Constant* zero = llvm::ConstantInt::get(llvm::Type::getInt32Ty(context)0);
 839     llvm::Constant* indices[] = { zerozero };
 840     return llvm::ConstantExpr::getInBoundsGetElementPtr(gv->getValueType()gvindices);
 841 }
 842 
 843 void* Emitter::CreateGlobalWStringPtr(const std::u16string& name)
 844 {
 845     return nullptr;
 846 }
 847 
 848 void* Emitter::CreateGlobalUStringPtr(const std::u32string& name)
 849 {
 850     return nullptr;
 851 }
 852 
 853 void* Emitter::CreateCall(void* calleeconst std::std::vector<void*>&args)
 854 {
 855     std::vector<llvm::Value*> arguments;
 856     for (void* arg : args)
 857     {
 858         arguments.push_back(static_cast<llvm::Value*>(arg));
 859     }
 860 #if (LLVM_VERSION_MAJOR >= 11)
 861     return builder.CreateCall(llvm::cast<llvm::FunctionType>(static_cast<llvm::Value*>(callee)->getType()->getPointerElementType())static_cast<llvm::Value*>(callee)arguments);
 862 #else
 863     return builder.CreateCall(static_cast<llvm::Value*>(callee)arguments);
 864 #endif
 865 }
 866 
 867 void* Emitter::CreateCallInst(void* calleeconst std::std::vector<void*>&argsconststd::std::vector<void*>&bundlesconstSpan&span)
 868 {
 869     std::vector<llvm::Value*> arguments;
 870     for (void* arg : args)
 871     {
 872         arguments.push_back(static_cast<llvm::Value*>(arg));
 873     }
 874     std::vector<llvm::OperandBundleDef> bundleDefs;
 875     for (void* bundle : bundles)
 876     {
 877         std::vector<llvm::Value*> inputs;
 878         inputs.push_back(static_cast<llvm::Value*>(bundle));
 879         bundleDefs.push_back(llvm::OperandBundleDef("funclet"inputs));
 880     }
 881 #if (LLVM_VERSION_MAJOR >= 11)
 882     llvm::CallInst* callInst = llvm::CallInst::Create(llvm::cast<llvm::FunctionType>(
 883         llvm::cast<llvm::PointerType>(static_cast<llvm::Value*>(callee)->getType())->getElementType())
 884         static_cast<llvm::Value*>(callee)argumentsbundleDefs""static_cast<llvm::BasicBlock*>(CurrentBasicBlock()));
 885 #else
 886     llvm::CallInst* callInst = llvm::CallInst::Create(static_cast<llvm::Value*>(callee)argumentsbundleDefs""static_cast<llvm::BasicBlock*>(CurrentBasicBlock()));
 887 #endif
 888     if (DIBuilder())
 889     {
 890         callInst->setDebugLoc(GetDebugLocation(span));
 891     }
 892     return callInst;
 893 }
 894 
 895 void* Emitter::CreateCallInstToBasicBlock(void* calleeconst std::std::vector<void*>&argsconststd::std::vector<void*>&bundlesvoid*basicBlockconstSpan&span)
 896 {
 897     std::vector<llvm::Value*> arguments;
 898     for (void* arg : args)
 899     {
 900         arguments.push_back(static_cast<llvm::Value*>(arg));
 901     }
 902     std::vector<llvm::OperandBundleDef> bundleDefs;
 903     for (void* bundle : bundles)
 904     {
 905         std::vector<llvm::Value*> inputs;
 906         inputs.push_back(static_cast<llvm::Value*>(bundle));
 907         bundleDefs.push_back(llvm::OperandBundleDef("funclet"inputs));
 908     }
 909 #if (LLVM_VERSION_MAJOR >= 11)
 910     llvm::CallInst* callInst = llvm::CallInst::Create(llvm::cast<llvm::FunctionType>(
 911         llvm::cast<llvm::PointerType>(static_cast<llvm::Value*>(callee)->getType())->getElementType())
 912         static_cast<llvm::Value*>(callee)argumentsbundleDefs""static_cast<llvm::BasicBlock*>(basicBlock));
 913 #else
 914     llvm::CallInst* callInst = llvm::CallInst::Create(static_cast<llvm::Value*>(callee)argumentsbundleDefs""static_cast<llvm::BasicBlock*>(basicBlock));
 915 #endif
 916     if (DIBuilder())
 917     {
 918         callInst->setDebugLoc(GetDebugLocation(span));
 919     }
 920     return callInst;
 921 }
 922 
 923 void* Emitter::CreateInvoke(void* calleevoid* normalBlockvoid* unwindBlockconst std::std::vector<void*>&args)
 924 {
 925     std::vector<llvm::Value*> arguments;
 926     for (void* arg : args)
 927     {
 928         arguments.push_back(static_cast<llvm::Value*>(arg));
 929     }
 930 #if (LLVM_VERSION_MAJOR >= 11)
 931     return builder.CreateInvoke(llvm::cast<llvm::FunctionType>(
 932         llvm::cast<llvm::PointerType>(static_cast<llvm::Value*>(callee)->getType())->getElementType())
 933         static_cast<llvm::Value*>(callee)static_cast<llvm::BasicBlock*>(normalBlock)static_cast<llvm::BasicBlock*>(unwindBlock)arguments);
 934 #else
 935     return builder.CreateInvoke(static_cast<llvm::Value*>(callee)static_cast<llvm::BasicBlock*>(normalBlock)static_cast<llvm::BasicBlock*>(unwindBlock)arguments);
 936 #endif
 937 }
 938 
 939 void* Emitter::CreateInvokeInst(void* calleevoid* normalBlockvoid* unwindBlockconst std::std::vector<void*>&argsconststd::std::vector<void*>&bundlesconstSpan&span)
 940 {
 941     std::vector<llvm::Value*> arguments;
 942     for (void* arg : args)
 943     {
 944         arguments.push_back(static_cast<llvm::Value*>(arg));
 945     }
 946     std::vector<llvm::OperandBundleDef> bundleDefs;
 947     for (void* bundle : bundles)
 948     {
 949         std::vector<llvm::Value*> inputs;
 950         inputs.push_back(static_cast<llvm::Value*>(bundle));
 951         bundleDefs.push_back(llvm::OperandBundleDef("funclet"inputs));
 952     }
 953 #if (LLVM_VERSION_MAJOR >= 11)
 954     llvm::InvokeInst* invokeInst = llvm::InvokeInst::Create(llvm::cast<llvm::FunctionType>(
 955         llvm::cast<llvm::PointerType>(static_cast<llvm::Value*>(callee)->getType())->getElementType())
 956         static_cast<llvm::Value*>(callee)static_cast<llvm::BasicBlock*>(normalBlock)static_cast<llvm::BasicBlock*>(unwindBlock)
 957         argumentsbundleDefs""static_cast<llvm::BasicBlock*>(CurrentBasicBlock()));
 958 #else
 959     llvm::InvokeInst* invokeInst = llvm::InvokeInst::Create(static_cast<llvm::Value*>(callee)static_cast<llvm::BasicBlock*>(normalBlock)static_cast<llvm::BasicBlock*>(unwindBlock)
 960         argumentsbundleDefs""static_cast<llvm::BasicBlock*>(CurrentBasicBlock()));
 961 #endif
 962     if (DIBuilder())
 963     {
 964         invokeInst->setDebugLoc(GetDebugLocation(span));
 965     }
 966     return invokeInst;
 967 }
 968 
 969 void* Emitter::GetObjectFromClassDelegate(void* classDelegatePtr)
 970 {
 971     ArgVector objectIndeces;
 972     objectIndeces.push_back(builder.getInt32(0));
 973     objectIndeces.push_back(builder.getInt32(0));
 974     llvm::Value* objectPtr = builder.CreateGEP(static_cast<llvm::Value*>(classDelegatePtr)objectIndeces);
 975     return objectPtr;
 976 }
 977 
 978 void* Emitter::GetDelegateFromClassDelegate(void* classDelegatePtr)
 979 {
 980     ArgVector delegateIndeces;
 981     delegateIndeces.push_back(builder.getInt32(0));
 982     delegateIndeces.push_back(builder.getInt32(1));
 983     llvm::Value* delegatePtr = builder.CreateGEP(static_cast<llvm::Value*>(classDelegatePtr)delegateIndeces);
 984     return delegatePtr;
 985 }
 986 
 987 void* Emitter::GetObjectFromInterface(void* interfaceTypePtr)
 988 {
 989     ArgVector objectIndeces;
 990     objectIndeces.push_back(builder.getInt32(0));
 991     objectIndeces.push_back(builder.getInt32(0));
 992     llvm::Value* objectPtrPtr = builder.CreateGEP(static_cast<llvm::Value*>(interfaceTypePtr)objectIndeces);
 993     llvm::Value* objectPtr = builder.CreateLoad(objectPtrPtr);
 994     return objectPtr;
 995 }
 996 
 997 void* Emitter::GetObjectPtrFromInterface(void* interfaceTypePtr)
 998 {
 999     ArgVector objectIndeces;
1000     objectIndeces.push_back(builder.getInt32(0));
1001     objectIndeces.push_back(builder.getInt32(0));
1002     llvm::Value* objectPtrPtr = builder.CreateGEP(static_cast<llvm::Value*>(interfaceTypePtr)objectIndeces);
1003     return objectPtrPtr;
1004 }
1005 
1006 void* Emitter::GetImtPtrFromInterface(void* interfaceTypePtr)
1007 {
1008     ArgVector interfaceIndeces;
1009     interfaceIndeces.push_back(builder.getInt32(0));
1010     interfaceIndeces.push_back(builder.getInt32(1));
1011     llvm::Value* interfacePtrPtr = builder.CreateGEP(static_cast<llvm::Value*>(interfaceTypePtr)interfaceIndeces);
1012     llvm::Value* interfacePtr = builder.CreateLoad(interfacePtrPtr);
1013     llvm::Value* imtPtr = builder.CreateBitCast(interfacePtrllvm::PointerType::get(builder.getInt8PtrTy()0));
1014     return imtPtr;
1015 }
1016 
1017 void* Emitter::GetInterfaceMethod(void* imtPtrint32_t methodIndexvoid* interfaceMethodType)
1018 {
1019     ArgVector methodIndeces;
1020     methodIndeces.push_back(builder.getInt32(methodIndex));
1021     llvm::Value* methodPtrPtr = builder.CreateGEP(static_cast<llvm::Value*>(imtPtr)methodIndeces);
1022     llvm::Value* methodPtr = builder.CreateLoad(methodPtrPtr);
1023     llvm::Value* callee = builder.CreateBitCast(methodPtrllvm::PointerType::get(static_cast<llvm::Type*>(interfaceMethodType)0));
1024     return callee;
1025 }
1026 
1027 void* Emitter::GetVmtPtr(void* thisPtrint32_t vmtPtrIndexvoid* vmtPtrType)
1028 {
1029     ArgVector vmtPtrIndeces;
1030     vmtPtrIndeces.push_back(builder.getInt32(0));
1031     vmtPtrIndeces.push_back(builder.getInt32(vmtPtrIndex));
1032     llvm::Value* vmtPtrPtr = builder.CreateGEP(static_cast<llvm::Value*>(thisPtr)vmtPtrIndeces);
1033     void* vmtPtr = builder.CreateBitCast(builder.CreateLoad(vmtPtrPtr)static_cast<llvm::Type*>(vmtPtrType));
1034     return vmtPtr;
1035 }
1036 
1037 void* Emitter::GetMethodPtr(void* vmtPtrint32_t vmtIndex)
1038 {
1039     ArgVector funPtrIndeces;
1040     funPtrIndeces.push_back(builder.getInt32(0));
1041     funPtrIndeces.push_back(builder.getInt32(vmtIndex));
1042     llvm::Value* funPtrPtr = builder.CreateGEP(static_cast<llvm::Value*>(vmtPtr)funPtrIndeces);
1043     void* funAsVoidPtr = builder.CreateLoad(funPtrPtr);
1044     return funAsVoidPtr;
1045 }
1046 
1047 void* Emitter::GetImtArray(void* vmtObjectPtrint32_t imtsVmtIndexOffset)
1048 {
1049     ArgVector imtsArrayIndeces;
1050     imtsArrayIndeces.push_back(builder.getInt32(0));
1051     imtsArrayIndeces.push_back(builder.getInt32(imtsVmtIndexOffset));
1052     llvm::Value* imtsArrayPtrPtr = builder.CreateGEP(static_cast<llvm::Value*>(vmtObjectPtr)imtsArrayIndeces);
1053     llvm::Value* imtsArrayPtr = builder.CreateBitCast(imtsArrayPtrPtrllvm::PointerType::get(llvm::PointerType::get(builder.getInt8PtrTy()0)0));
1054     llvm::Value* imtArray = builder.CreateLoad(imtsArrayPtr);
1055     return imtArray;
1056 }
1057 
1058 void* Emitter::GetImt(void* imtArrayint32_t interfaceIndex)
1059 {
1060     ArgVector imtArrayIndeces;
1061     imtArrayIndeces.push_back(builder.getInt32(interfaceIndex));
1062     llvm::Value* imtArrayPtr = builder.CreateGEP(static_cast<llvm::Value*>(imtArray)imtArrayIndeces);
1063     llvm::Value* imt = builder.CreateLoad(imtArrayPtr);
1064     return imt;
1065 }
1066 
1067 void* Emitter::GetMemberVariablePtr(void* classPtrint32_t memberVariableLayoutIndex)
1068 {
1069     ArgVector indeces;
1070     indeces.push_back(builder.getInt32(0));
1071     indeces.push_back(builder.getInt32(memberVariableLayoutIndex));
1072     llvm::Value* memberVariablePtr = builder.CreateGEP(static_cast<llvm::Value*>(classPtr)indeces);
1073     return memberVariablePtr;
1074 }
1075 
1076 void* Emitter::SizeOf(void* ptrType)
1077 {
1078     void* nullPtr = CreateDefaultIrValueForPtrType(static_cast<llvm::Type*>(ptrType));
1079     llvm::Value* gep = builder.CreateGEP(static_cast<llvm::Value*>(nullPtr)builder.getInt64(1));
1080     llvm::Value* size = builder.CreatePtrToInt(gepbuilder.getInt64Ty());
1081     return size;
1082 }
1083 
1084 /*
1085 void* Emitter::GetClassIdPtr(void* vmtPtr)
1086 {
1087     ArgVector indeces;
1088     indeces.push_back(builder.getInt32(0));
1089     indeces.push_back(builder.getInt32(0));
1090     llvm::Value* classIdPtr = builder.CreateGEP(static_cast<llvm::Value*>(vmtPtr), indeces);
1091     return classIdPtr;
1092 }
1093 */
1094 
1095 
1096 void* Emitter::GetClassIdPtr(void* vmtPtrint32_t classIdVmtIndexOffset)
1097 {
1098     ArgVector indeces;
1099     indeces.push_back(builder.getInt32(0));
1100     indeces.push_back(builder.getInt32(classIdVmtIndexOffset));
1101     llvm::Value* classIdPtr = builder.CreateGEP(static_cast<llvm::Value*>(vmtPtr)indeces);
1102     return classIdPtr;
1103 }
1104 
1105 void* Emitter::GetClassName(void* vmtPtrint32_t classNameVmtIndexOffset)
1106 {
1107     ArgVector indeces;
1108     indeces.push_back(builder.getInt32(0));
1109     indeces.push_back(builder.getInt32(classNameVmtIndexOffset));
1110     llvm::Value* classNamePtr = builder.CreateGEP(static_cast<llvm::Value*>(vmtPtr)indeces);
1111     llvm::Value* className = builder.CreateLoad(classNamePtr);
1112     return className;
1113 }
1114 
1115 void* Emitter::ComputeAddress(void* ptrvoid* index)
1116 {
1117     return builder.CreateGEP(static_cast<llvm::Value*>(ptr)static_cast<llvm::Value*>(index));
1118 }
1119 
1120 void* Emitter::CreatePtrDiff(void* leftvoid* right)
1121 {
1122     return builder.CreatePtrDiff(static_cast<llvm::Value*>(left)static_cast<llvm::Value*>(right));
1123 }
1124 
1125 uint32_t Emitter::GetPrivateFlag()
1126 {
1127     return static_cast<uint32_t>(llvm::DINode::DIFlags::FlagPrivate);
1128 }
1129 
1130 uint32_t Emitter::GetProtectedFlag()
1131 {
1132     return static_cast<uint32_t>(llvm::DINode::DIFlags::FlagProtected);
1133 }
1134 
1135 uint32_t Emitter::GetPublicFlag()
1136 {
1137     return static_cast<uint32_t>(llvm::DINode::DIFlags::FlagPublic);
1138 }
1139 
1140 uint32_t Emitter::GetNoFlags()
1141 {
1142     return static_cast<uint32_t>(llvm::DINode::DIFlags::FlagZero);
1143 }
1144 
1145 void* Emitter::CreateModule(const std::string& moduleName)
1146 {
1147     return new llvm::Module(moduleNamecontext);
1148 }
1149 
1150 void Emitter::DestroyModule(void* module)
1151 {
1152     delete static_cast<llvm::Module*>(module);
1153 }
1154 
1155 void Emitter::SetDIFile(void* diFile_)
1156 {
1157     diFile = static_cast<llvm::DIFile*>(diFile_);
1158 }
1159 
1160 void* Emitter::GetDebugInfoForFile(const Span& spanconst boost::uuids::uuid& moduleId)
1161 {
1162     if (span.fileIndex == -1)
1163     {
1164         return diFile;
1165     }
1166     std::string sourceFilePath = GetSourceFilePath(spanmoduleId);
1167     if (sourceFilePath.empty())
1168     {
1169         return diFile;
1170     }
1171     llvm::DIFile* file = diBuilder->createFile(Path::GetFileName(sourceFilePath)Path::GetDirectoryName(sourceFilePath));
1172     return file;
1173 }
1174 
1175 void* Emitter::GetDITypeByTypeId(const boost::uuids::uuid& typeId) const
1176 {
1177     auto it = diTypeTypeIdMap.find(typeId);
1178     if (it != diTypeTypeIdMap.cend())
1179     {
1180         return it->second;
1181     }
1182     else
1183     {
1184         return nullptr;
1185     }
1186 }
1187 
1188 void Emitter::SetDITypeByTypeId(const boost::uuids::uuid& typeIdvoid* diTypeconst std::string& typeName)
1189 {
1190     diTypeTypeIdMap[typeId] = static_cast<llvm::DIType*>(diType);
1191     diTypeNameMap[static_cast<llvm::DIType*>(diType)] = typeName;
1192 }
1193 
1194 void* Emitter::GetDIMemberType(const std::std::pair<boost::uuids::uuidint32_t>&memberVariableId)
1195 {
1196     auto it = diMemberTypeMap.find(memberVariableId);
1197     if (it != diMemberTypeMap.cend())
1198     {
1199         return it->second;
1200     }
1201     return nullptr;
1202 }
1203 
1204 void Emitter::SetDIMemberType(const std::std::pair<boost::uuids::uuidint32_t>&memberVariableIdvoid*diType)
1205 {
1206     diMemberTypeMap[memberVariableId] = static_cast<llvm::DIDerivedType*>(diType);
1207 }
1208 
1209 void* Emitter::CreateDIMemberType(void* scopeconst std::string& nameconst Span& spanconst boost::uuids::uuid& moduleIduint64_t sizeInBitsuint64_t alignInBitsuint64_t offsetInBitsvoid* diType)
1210 {
1211     llvm::DINode::DIFlags flags = llvm::DINode::DIFlags::FlagZero;
1212     return diBuilder->createMemberType(static_cast<llvm::DIType*>(scope)namestatic_cast<llvm::DIFile*>(
1213         GetDebugInfoForFile(spanmoduleId))span.linesizeInBitsalignInBitsoffsetInBitsflags
1214         static_cast<llvm::DIType*>(diType));
1215 }
1216 
1217 void* Emitter::CreateConstDIType(void* diType)
1218 {
1219     return diBuilder->createQualifiedType(llvm::dwarf::DW_TAG_const_typestatic_cast<llvm::DIType*>(diType));
1220 }
1221 
1222 void* Emitter::CreateLValueRefDIType(void* diType)
1223 {
1224     return diBuilder->createReferenceType(llvm::dwarf::DW_TAG_reference_typestatic_cast<llvm::DIType*>(diType));
1225 }
1226 
1227 void* Emitter::CreateRValueRefDIType(void* diType)
1228 {
1229     return diBuilder->createReferenceType(llvm::dwarf::DW_TAG_rvalue_reference_typestatic_cast<llvm::DIType*>(diType));
1230 }
1231 
1232 void* Emitter::CreatePointerDIType(void* diType)
1233 {
1234     return diBuilder->createPointerType(static_cast<llvm::DIType*>(diType)64);
1235 }
1236 
1237 void* Emitter::CreateUnspecifiedDIType(const std::string& name)
1238 {
1239     return diBuilder->createUnspecifiedType(name);
1240 }
1241 
1242 void Emitter::MapClassPtr(const boost::uuids::uuid& typeIdvoid* classPtrconst std::string& className)
1243 {
1244     if (classPtrMap.find(typeId) == classPtrMap.cend())
1245     {
1246         classPtrMap[typeId] = classPtr;
1247         classNameMap[classPtr] = className;
1248     }
1249 }
1250 
1251 uint64_t Emitter::GetSizeInBits(void* irType)
1252 {
1253     return dataLayout->getTypeSizeInBits(static_cast<llvm::Type*>(irType));
1254 }
1255 
1256 uint64_t Emitter::GetAlignmentInBits(void* irType)
1257 {
1258     return 8 * dataLayout->getABITypeAlignment(static_cast<llvm::Type*>(irType));
1259 }
1260 
1261 void Emitter::ReplaceForwardDeclarations()
1262 {
1263     std::unordered_map<llvm::DIType*boost::uuids::uuid> currentFwdDeclarationMap;
1264     std::swap(currentFwdDeclarationMapfwdDeclarationMap);
1265     while (!currentFwdDeclarationMap.empty())
1266     {
1267         for (const auto& p : currentFwdDeclarationMap)
1268         {
1269             llvm::DIType* fwdDeclaration = p.first;
1270             const boost::uuids::uuid& typeId = p.second;
1271             void* diType = GetDITypeByTypeId(typeId);
1272             if (!diType)
1273             {
1274                 auto it = classPtrMap.find(typeId);
1275                 if (it != classPtrMap.cend())
1276                 {
1277                     void* classPtr = it->second;
1278                     //std::string className = classNameMap[classPtr];
1279                     //std::cout << "> create di type for class" << className << std::endl;
1280                     diType = CreateClassDIType(classPtr);
1281                     //std::cout << "< create di type for class" << className << std::endl;
1282                 }
1283                 else
1284                 {
1285                     throw std::runtime_error("Emitter::ReplaceForwardDeclarations(): class ptr not mapped");
1286                 }
1287             }
1288             //std::string typeName = diTypeNameMap[static_cast<llvm::DIType*>(diType)];
1289             //std::cout << "> replacing " << typeName << std::endl;
1290             fwdDeclaration->replaceAllUsesWith(static_cast<llvm::DIType*>(diType));
1291             //std::cout << "< replacing " << typeName << std::endl;
1292         }
1293         currentFwdDeclarationMap.clear();
1294         std::swap(currentFwdDeclarationMapfwdDeclarationMap);
1295     }
1296 }
1297 
1298 void* Emitter::GetIrObject(void* symbol) const
1299 {
1300     auto it = irObjectMap.find(symbol);
1301     if (it != irObjectMap.cend())
1302     {
1303         return it->second;
1304     }
1305     else
1306     {
1307         throw std::runtime_error("emitter: IR object not found");
1308     }
1309 }
1310 
1311 void Emitter::SetIrObject(void* symbolvoid* irObject)
1312 {
1313     irObjectMap[symbol] = static_cast<llvm::Value*>(irObject);
1314 }
1315 
1316 void* Emitter::GetIrTypeByTypeId(const boost::uuids::uuid& typeId)
1317 {
1318     auto it = irTypeTypeIdMap.find(typeId);
1319     if (it != irTypeTypeIdMap.cend())
1320     {
1321         return it->second;
1322     }
1323     else
1324     {
1325         return nullptr;
1326     }
1327 }
1328 
1329 void Emitter::SetIrTypeByTypeId(const boost::uuids::uuid& typeIdvoid* irType)
1330 {
1331     irTypeTypeIdMap[typeId] = static_cast<llvm::Type*>(irType);
1332 }
1333 
1334 void* Emitter::GetIrTypeForPtrType(void* baseIrType)
1335 {
1336     return llvm::PointerType::get(static_cast<llvm::Type*>(baseIrType)0);
1337 }
1338 
1339 std::string Emitter::GetIrTypeName(void* irType)
1340 {
1341     return std::string();
1342 }
1343 
1344 std::string Emitter::MakeVmtVariableName(const std::string& vmtObjectName)
1345 {
1346     return std::string();
1347 }
1348 
1349 void* Emitter::CreateDefaultIrValueForArrayType(void* arrayIrTypeconst std::std::vector<void*>&arrayOfDefaults)
1350 {
1351     std::vector<llvm::Constant*> arrayOfConstants;
1352     for (void* constant : arrayOfDefaults)
1353     {
1354         arrayOfConstants.push_back(static_cast<llvm::Constant*>(constant));
1355     }
1356     llvm::Type* arrayType = static_cast<llvm::Type*>(arrayIrType);
1357     return llvm::ConstantArray::get(llvm::cast<llvm::ArrayType>(arrayType)arrayOfConstants);
1358 }
1359 
1360 void* Emitter::CreateDefaultIrValueForBool()
1361 {
1362     return builder.getInt1(false);
1363 }
1364 
1365 void* Emitter::CreateDefaultIrValueForSByte()
1366 {
1367     return builder.getInt8(0);
1368 }
1369 
1370 void* Emitter::CreateDefaultIrValueForByte()
1371 {
1372     return builder.getInt8(0);
1373 }
1374 
1375 void* Emitter::CreateDefaultIrValueForShort()
1376 {
1377     return builder.getInt16(0);
1378 }
1379 
1380 void* Emitter::CreateDefaultIrValueForUShort()
1381 {
1382     return builder.getInt16(0);
1383 }
1384 
1385 void* Emitter::CreateDefaultIrValueForInt()
1386 {
1387     return builder.getInt32(0);
1388 }
1389 
1390 void* Emitter::CreateDefaultIrValueForUInt()
1391 {
1392     return builder.getInt32(0);
1393 }
1394 
1395 void* Emitter::CreateDefaultIrValueForLong()
1396 {
1397     return builder.getInt64(0);
1398 }
1399 
1400 void* Emitter::CreateDefaultIrValueForULong()
1401 {
1402     return builder.getInt64(0);
1403 }
1404 
1405 void* Emitter::CreateDefaultIrValueForFloat()
1406 {
1407     return llvm::ConstantFP::get(llvm::Type::getFloatTy(context)0.0);
1408 }
1409 
1410 void* Emitter::CreateDefaultIrValueForDouble()
1411 {
1412     return llvm::ConstantFP::get(llvm::Type::getDoubleTy(context)0.0);
1413 }
1414 
1415 void* Emitter::CreateDefaultIrValueForChar()
1416 {
1417     return builder.getInt8(0);
1418 }
1419 
1420 void* Emitter::CreateDefaultIrValueForWChar()
1421 {
1422     return builder.getInt16(0);
1423 }
1424 
1425 void* Emitter::CreateDefaultIrValueForUChar()
1426 {
1427     return builder.getInt32(0);
1428 }
1429 
1430 void* Emitter::CreateDefaultIrValueForStruct(void* irTypeconst std::std::vector<void*>&defaultMembers)
1431 {
1432     std::vector<llvm::Constant*> arrayOfDefaults;
1433     for (void* constant : defaultMembers)
1434     {
1435         arrayOfDefaults.push_back(static_cast<llvm::Constant*>(constant));
1436     }
1437     return llvm::ConstantStruct::get(llvm::cast<llvm::StructType>(static_cast<llvm::Type*>(irType))arrayOfDefaults);
1438 }
1439 
1440 void* Emitter::CreateDefaultIrValueForDelegateType(void* irType)
1441 {
1442     return llvm::Constant::getNullValue(static_cast<llvm::Type*>(irType));
1443 }
1444 
1445 void* Emitter::CreateDefaultIrValueForVoidPtrType()
1446 {
1447     return llvm::Constant::getNullValue(builder.getInt8PtrTy());
1448 }
1449 
1450 void* Emitter::CreateDefaultIrValueForDerivedType(void* irType)
1451 {
1452     return llvm::Constant::getNullValue(static_cast<llvm::Type*>(irType));
1453 }
1454 
1455 void* Emitter::CreateDefaultIrValueForPtrType(void* irType)
1456 {
1457     return llvm::Constant::getNullValue(static_cast<llvm::Type*>(irType));
1458 }
1459 
1460 void* Emitter::CreateIrValueForBool(bool value)
1461 {
1462     return builder.getInt1(value);
1463 }
1464 
1465 void* Emitter::CreateIrValueForSByte(int8_t value)
1466 {
1467     return builder.getInt8(static_cast<uint8_t>(value));
1468 }
1469 
1470 void* Emitter::CreateIrValueForByte(uint8_t value)
1471 {
1472     return builder.getInt8(value);
1473 }
1474 
1475 void* Emitter::CreateIrValueForShort(int16_t value)
1476 {
1477     return builder.getInt16(static_cast<uint16_t>(value));
1478 }
1479 
1480 void* Emitter::CreateIrValueForUShort(uint16_t value)
1481 {
1482     return builder.getInt16(value);
1483 }
1484 
1485 void* Emitter::CreateIrValueForInt(int32_t value)
1486 {
1487     if (substituteLineNumber)
1488     {
1489         return builder.getInt32(static_cast<uint32_t>(currentLineNumber));
1490     }
1491     else
1492     {
1493         return builder.getInt32(static_cast<uint32_t>(value));
1494     }
1495 }
1496 
1497 void* Emitter::CreateIrValueForUInt(uint32_t value)
1498 {
1499     return builder.getInt32(value);
1500 }
1501 
1502 void* Emitter::CreateIrValueForLong(int64_t value)
1503 {
1504     return builder.getInt64(static_cast<uint64_t>(value));
1505 }
1506 
1507 void* Emitter::CreateIrValueForULong(uint64_t value)
1508 {
1509     return builder.getInt64(value);
1510 }
1511 
1512 void* Emitter::CreateIrValueForFloat(float value)
1513 {
1514     return llvm::ConstantFP::get(builder.getFloatTy()value);
1515 }
1516 
1517 void* Emitter::CreateIrValueForDouble(double value)
1518 {
1519     return llvm::ConstantFP::get(builder.getDoubleTy()value);
1520 }
1521 
1522 void* Emitter::CreateIrValueForChar(uint8_t value)
1523 {
1524     return builder.getInt8(value);
1525 }
1526 
1527 void* Emitter::CreateIrValueForWChar(uint16_t value)
1528 {
1529     return builder.getInt16(value);
1530 }
1531 
1532 void* Emitter::CreateIrValueForUChar(uint32_t value)
1533 {
1534     return builder.getInt32(value);
1535 }
1536 
1537 void* Emitter::CreateIrValueForWString(void* wstringConstant)
1538 {
1539     ArgVector indeces;
1540     indeces.push_back(builder.getInt32(0));
1541     indeces.push_back(builder.getInt32(0));
1542     return builder.CreateGEP(static_cast<llvm::Value*>(wstringConstant)indeces);
1543 }
1544 
1545 void* Emitter::CreateIrValueForUString(void* ustringConstant)
1546 {
1547     ArgVector indeces;
1548     indeces.push_back(builder.getInt32(0));
1549     indeces.push_back(builder.getInt32(0));
1550     return builder.CreateGEP(static_cast<llvm::Value*>(ustringConstant)indeces);
1551 }
1552 
1553 void* Emitter::CreateIrValueForConstantArray(void* arrayIrTypeconst std::std::vector<void*>&elementsconststd::string&prefix)
1554 {
1555     std::vector<llvm::Constant*> elementConstants;
1556     for (void* elementConstant : elements)
1557     {
1558         elementConstants.push_back(static_cast<llvm::Constant*>(elementConstant));
1559     }
1560     return llvm::ConstantArray::get(llvm::cast<llvm::ArrayType>(static_cast<llvm::Type*>(arrayIrType))elementConstants);
1561 }
1562 
1563 void* Emitter::CreateIrValueForConstantStruct(void* structIrTypeconst std::std::vector<void*>&elementConstants)
1564 {
1565     std::vector<llvm::Constant*> memberConstants;
1566     for (void* elementConstant : elementConstants)
1567     {
1568         memberConstants.push_back(static_cast<llvm::Constant*>(elementConstant));
1569     }
1570     return llvm::ConstantStruct::get(llvm::cast<llvm::StructType>(static_cast<llvm::Type*>(structIrType))memberConstants);
1571 }
1572 
1573 void* Emitter::CreateIrValueForUuid(void* uuidConstant)
1574 {
1575     ArgVector indeces;
1576     indeces.push_back(builder.getInt32(0));
1577     indeces.push_back(builder.getInt32(0));
1578     return builder.CreateBitCast(builder.CreateGEP(static_cast<llvm::Value*>(uuidConstant)indeces)builder.getInt8PtrTy());
1579 }
1580 
1581 void* Emitter::GetConversionValue(void* typevoid* from)
1582 {
1583     return nullptr;
1584 }
1585 
1586 void* Emitter::CreateDITypeForBool()
1587 {
1588     return diBuilder->createBasicType("bool"8llvm::dwarf::DW_ATE_boolean);
1589 }
1590 
1591 void* Emitter::CreateDITypeForSByte()
1592 {
1593     return diBuilder->createBasicType("sbyte"8llvm::dwarf::DW_ATE_signed);
1594 }
1595 
1596 void* Emitter::CreateDITypeForByte()
1597 {
1598     return diBuilder->createBasicType("byte"8llvm::dwarf::DW_ATE_unsigned);
1599 }
1600 
1601 void* Emitter::CreateDITypeForShort()
1602 {
1603     return diBuilder->createBasicType("short"16llvm::dwarf::DW_ATE_signed);
1604 }
1605 
1606 void* Emitter::CreateDITypeForUShort()
1607 {
1608     return diBuilder->createBasicType("ushort"16llvm::dwarf::DW_ATE_unsigned);
1609 }
1610 
1611 void* Emitter::CreateDITypeForInt()
1612 {
1613     return diBuilder->createBasicType("int"32llvm::dwarf::DW_ATE_signed);
1614 }
1615 
1616 void* Emitter::CreateDITypeForUInt()
1617 {
1618     return diBuilder->createBasicType("uint"32llvm::dwarf::DW_ATE_unsigned);
1619 }
1620 
1621 void* Emitter::CreateDITypeForLong()
1622 {
1623     return diBuilder->createBasicType("long"64llvm::dwarf::DW_ATE_signed);
1624 }
1625 
1626 void* Emitter::CreateDITypeForULong()
1627 {
1628     return diBuilder->createBasicType("ulong"64llvm::dwarf::DW_ATE_unsigned);
1629 }
1630 
1631 void* Emitter::CreateDITypeForFloat()
1632 {
1633     return diBuilder->createBasicType("float"32llvm::dwarf::DW_ATE_float);
1634 }
1635 
1636 void* Emitter::CreateDITypeForDouble()
1637 {
1638     return diBuilder->createBasicType("double"64llvm::dwarf::DW_ATE_float);
1639 }
1640 
1641 void* Emitter::CreateDITypeForChar()
1642 {
1643     return diBuilder->createBasicType("char"8llvm::dwarf::DW_ATE_unsigned_char);
1644 }
1645 
1646 void* Emitter::CreateDITypeForWChar()
1647 {
1648     return diBuilder->createBasicType("wchar"16llvm::dwarf::DW_ATE_unsigned_char);
1649 }
1650 
1651 void* Emitter::CreateDITypeForUChar()
1652 {
1653     return diBuilder->createBasicType("uchar"32llvm::dwarf::DW_ATE_unsigned_char);
1654 }
1655 
1656 void* Emitter::CreateDITypeForVoid()
1657 {
1658     return diBuilder->createUnspecifiedType("void");
1659 }
1660 
1661 void* Emitter::CreateDITypeForArray(void* elementDITypeconst std::std::vector<void*>&elements)
1662 {
1663     // todo...
1664     std::vector<llvm::Metadata*> metadataElements;
1665     return diBuilder->createArrayType(elements.size()8static_cast<llvm::DIType*>(elementDIType)diBuilder->getOrCreateArray(metadataElements));
1666 
1667 }
1668 
1669 #if (LLVM_VERSION_MAJOR >= 10)
1670 
1671 void* Emitter::CreateIrDIForwardDeclaration(void* irTypeconst std::string& nameconst std::string& mangledNameconst Span& spanconst boost::uuids::uuid& moduleId)
1672 {
1673     uint64_t sizeInBits = dataLayout->getStructLayout(llvm::cast<llvm::StructType>(static_cast<llvm::Type*>(irType)))->getSizeInBits();
1674     uint64_t alignInBits = 8 * dataLayout->getStructLayout(llvm::cast<llvm::StructType>(static_cast<llvm::Type*>(irType)))->getAlignment().value();
1675     uint64_t offsetInBits = 0;
1676     return diBuilder->createReplaceableCompositeType(llvm::dwarf::DW_TAG_class_typenamenullptrstatic_cast<llvm::DIFile*>(GetDebugInfoForFile(spanmoduleId))span.line
1677         0sizeInBitsalignInBitsllvm::DINode::DIFlags::FlagZeromangledName);
1678 }
1679 
1680 #else
1681 
1682 void* Emitter::CreateIrDIForwardDeclaration(void* irTypeconst std::string& nameconst std::string& mangledNameconst Span& spanconst boost::uuids::uuid& moduleId)
1683 {
1684     uint64_t sizeInBits = dataLayout->getStructLayout(llvm::cast<llvm::StructType>(static_cast<llvm::Type*>(irType)))->getSizeInBits();
1685     uint32_t alignInBits = 8 * dataLayout->getStructLayout(llvm::cast<llvm::StructType>(static_cast<llvm::Type*>(irType)))->getAlignment();
1686     uint64_t offsetInBits = 0;
1687     return diBuilder->createReplaceableCompositeType(llvm::dwarf::DW_TAG_class_typenamenullptrstatic_cast<llvm::DIFile*>(
1688         GetDebugInfoForFile(spanmoduleId))span.line
1689         0sizeInBitsalignInBitsllvm::DINode::DIFlags::FlagZeromangledName);
1690 }
1691 
1692 #endif
1693 
1694 uint64_t Emitter::GetOffsetInBits(void* classIrTypeint layoutIndex)
1695 {
1696     const llvm::StructLayout* structLayout = dataLayout->getStructLayout(llvm::cast<llvm::StructType>(static_cast<llvm::Type*>(classIrType)));
1697     uint64_t offsetInBits = structLayout->getElementOffsetInBits(layoutIndex);
1698     return offsetInBits;
1699 }
1700 
1701 #if (LLVM_VERSION_MAJOR >= 10)
1702 
1703 void* Emitter::CreateDITypeForClassType(void* irTypeconst std::std::vector<void*>&memberVariableElementsconstSpan&classSpanconstboost::uuids::uuid&moduleIdconst std::string& namevoid* vtableHolderClass
1704     const std::string& mangledNamevoid* baseClassDIType)
1705 {
1706     std::vector<llvm::Metadata*> elements;
1707     const llvm::StructLayout* structLayout = dataLayout->getStructLayout(llvm::cast<llvm::StructType>(static_cast<llvm::Type*>(irType)));
1708     for (void* element : memberVariableElements)
1709     {
1710         elements.push_back(static_cast<llvm::Metadata*>(element));
1711     }
1712     llvm::MDNode* templateParams = nullptr;
1713     uint64_t sizeInBits = structLayout->getSizeInBits();
1714     uint64_t alignInBits = 8 * structLayout->getAlignment().value();
1715     uint64_t offsetInBits = 0;
1716     llvm::DINode::DIFlags flags = llvm::DINode::DIFlags::FlagZero;
1717     return diBuilder->createClassType(static_cast<llvm::DIScope*>(CurrentScope())namestatic_cast<llvm::DIFile*>(GetDebugInfoForFile(classSpanmoduleId))classSpan.linesizeInBitsalignInBitsoffsetInBits
1718         flagsstatic_cast<llvm::DIType*>(baseClassDIType)diBuilder->getOrCreateArray(elements)static_cast<llvm::DIType*>(vtableHolderClass)templateParamsmangledName);
1719 }
1720 
1721 
1722 #else
1723 
1724 void* Emitter::CreateDITypeForClassType(void* irTypeconst std::std::vector<void*>&memberVariableElementsconstSpan&classSpanconstboost::uuids::uuid&moduleId
1725     const std::string& namevoid* vtableHolderClassconst std::string& mangledNamevoid* baseClassDIType)
1726 {
1727     std::vector<llvm::Metadata*> elements;
1728     const llvm::StructLayout* structLayout = dataLayout->getStructLayout(llvm::cast<llvm::StructType>(static_cast<llvm::Type*>(irType)));
1729     for (void* element : memberVariableElements)
1730     {
1731         elements.push_back(static_cast<llvm::Metadata*>(element));
1732     }
1733     llvm::MDNode* templateParams = nullptr;
1734     uint64_t sizeInBits = structLayout->getSizeInBits();
1735     uint32_t alignInBits = 8 * structLayout->getAlignment();
1736     uint64_t offsetInBits = 0;
1737     llvm::DINode::DIFlags flags = llvm::DINode::DIFlags::FlagZero;
1738     return diBuilder->createClassType(static_cast<llvm::DIScope*>(CurrentScope())namestatic_cast<llvm::DIFile*>(
1739         GetDebugInfoForFile(classSpanmoduleId))classSpan.linesizeInBitsalignInBitsoffsetInBits
1740         flagsstatic_cast<llvm::DIType*>(baseClassDIType)diBuilder->getOrCreateArray(elements)static_cast<llvm::DIType*>(vtableHolderClass)templateParamsmangledName);
1741 }
1742 
1743 #endif
1744 
1745 void* Emitter::GetFunctionIrType(void* symbol) const
1746 {
1747     auto it = functionIrTypeMap.find(symbol);
1748     if (it != functionIrTypeMap.cend())
1749     {
1750         return it->second;
1751     }
1752     else
1753     {
1754         return nullptr;
1755     }
1756 }
1757 
1758 void Emitter::SetFunctionIrType(void* symbolvoid* irType)
1759 {
1760     functionIrTypeMap[symbol] = static_cast<llvm::FunctionType*>(irType);
1761 }
1762 
1763 bool Emitter::IsVmtObjectCreated(void* symbol) const
1764 {
1765     return vmtObjectCreatedSet.find(symbol) != vmtObjectCreatedSet.cend();
1766 }
1767 
1768 void Emitter::SetVmtObjectCreated(void* symbol)
1769 {
1770     vmtObjectCreatedSet.insert(symbol);
1771 }
1772 
1773 bool Emitter::IsStaticObjectCreated(void* symbol) const
1774 {
1775     return staticObjectCreatedSet.find(symbol) != staticObjectCreatedSet.cend();
1776 }
1777 
1778 void Emitter::SetStaticObjectCreated(void* symbol)
1779 {
1780     staticObjectCreatedSet.insert(symbol);
1781 }
1782 
1783 void* Emitter::GetStaticObjectType(void* symbol) const
1784 {
1785     auto it = staticTypeMap.find(symbol);
1786     if (it != staticTypeMap.cend())
1787     {
1788         return it->second;
1789     }
1790     else
1791     {
1792         return nullptr;
1793     }
1794 }
1795 
1796 void Emitter::SetStaticObjectType(void* symbolvoid* type)
1797 {
1798     staticTypeMap[symbol] = static_cast<llvm::StructType*>(type);
1799 }
1800 
1801 void* Emitter::GetVmtObjectType(void* symbol) const
1802 {
1803     auto it = vmtObjectTypeMap.find(symbol);
1804     if (it != vmtObjectTypeMap.cend())
1805     {
1806         return it->second;
1807     }
1808     else
1809     {
1810         return nullptr;
1811     }
1812 }
1813 
1814 void Emitter::SetVmtObjectType(void* symbolvoid* vmtObjectType)
1815 {
1816     vmtObjectTypeMap[symbol] = static_cast<llvm::ArrayType*>(vmtObjectType);
1817 }
1818 
1819 std::string Emitter::GetStaticObjectName(void* symbol) const
1820 {
1821     auto it = staticObjectNameMap.find(symbol);
1822     if (it != staticObjectNameMap.cend())
1823     {
1824         return it->second;
1825     }
1826     else
1827     {
1828         return std::string();
1829     }
1830 }
1831 
1832 void Emitter::SetStaticObjectName(void* symbolconst std::string& staticObjectName)
1833 {
1834     staticObjectNameMap[symbol] = staticObjectName;
1835 }
1836 
1837 std::string Emitter::GetVmtObjectName(void* symbol) const
1838 {
1839     auto it = vmtObjectNameMap.find(symbol);
1840     if (it != vmtObjectNameMap.cend())
1841     {
1842         return it->second;
1843     }
1844     else
1845     {
1846         return std::string();
1847     }
1848 }
1849 
1850 void Emitter::SetVmtObjectName(void* symbolconst std::string& vmtObjectName)
1851 {
1852     vmtObjectNameMap[symbol] = vmtObjectName;
1853 }
1854 
1855 std::string Emitter::GetImtArrayObjectName(void* symbol) const
1856 {
1857     auto it = imtArrayObjectNameMap.find(symbol);
1858     if (it != imtArrayObjectNameMap.cend())
1859     {
1860         return it->second;
1861     }
1862     else
1863     {
1864         return std::string();
1865     }
1866 }
1867 
1868 void Emitter::SetImtArrayObjectName(void* symbolconst std::string& imtArrayObjectName)
1869 {
1870     imtArrayObjectNameMap[symbol] = imtArrayObjectName;
1871 }
1872 
1873 uint64_t Emitter::GetClassTypeSizeInBits(void* classIrType)
1874 {
1875     uint64_t sizeInBits = dataLayout->getStructLayout(llvm::cast<llvm::StructType>(static_cast<llvm::StructType*>(classIrType)))->getSizeInBits();
1876     return sizeInBits;
1877 }
1878 
1879 #if (LLVM_VERSION_MAJOR >= 10)
1880 
1881 uint64_t Emitter::GetClassTypeAlignmentInBits(void* classIrType)
1882 {
1883     uint64_t alignInBits = 8 * dataLayout->getStructLayout(llvm::cast<llvm::StructType>(static_cast<llvm::StructType*>(classIrType)))->getAlignment().value();
1884     return alignInBits;
1885 }
1886 
1887 #else
1888 
1889 uint64_t Emitter::GetClassTypeAlignmentInBits(void* classIrType)
1890 {
1891     uint32_t alignInBits = 8 * dataLayout->getStructLayout(llvm::cast<llvm::StructType>(static_cast<llvm::StructType*>(classIrType)))->getAlignment();
1892     return alignInBits;
1893 }
1894 
1895 #endif
1896 
1897 void Emitter::AddInlineFunctionAttribute(void* function)
1898 {
1899     llvm::Function* fun = static_cast<llvm::Function*>(function);
1900     fun->addFnAttr(llvm::Attribute::InlineHint);
1901 }
1902 
1903 void Emitter::SetFunctionLinkage(void* functionbool setInline)
1904 {
1905     llvm::Function* fun = static_cast<llvm::Function*>(function);
1906     if (setInline)
1907     {
1908         fun->setLinkage(llvm::GlobalValue::LinkageTypes::WeakODRLinkage);
1909     }
1910 }
1911 
1912 void Emitter::SetFunctionLinkageToLinkOnceODRLinkage(void* function)
1913 {
1914     llvm::Function* fun = static_cast<llvm::Function*>(function);
1915     fun->setLinkage(llvm::GlobalValue::LinkageTypes::LinkOnceODRLinkage);
1916 }
1917 
1918 void Emitter::SetFunctionCallConventionToStdCall(void* function)
1919 {
1920     llvm::Function* fun = static_cast<llvm::Function*>(function);
1921     fun->setCallingConv(llvm::CallingConv::X86_StdCall);
1922     //fun->setCallingConv(llvm::CallingConv::X86_ThisCall);
1923 }
1924 
1925 void* Emitter::CreateSubroutineType(const std::std::vector<void*>&elementTypes)
1926 {
1927     std::vector<llvm::Metadata*> elements;
1928     for (void* elementType : elementTypes)
1929     {
1930         elements.push_back(static_cast<llvm::Metadata*>(elementType));
1931     }
1932     llvm::DISubroutineType* subroutineType = diBuilder->createSubroutineType(diBuilder->getOrCreateTypeArray(elements));
1933     return subroutineType;
1934 }
1935 
1936 unsigned Emitter::GetPureVirtualVirtuality()
1937 {
1938     return static_cast<unsigned>(llvm::dwarf::DW_VIRTUALITY_pure_virtual);
1939 }
1940 
1941 unsigned Emitter::GetVirtualVirtuality()
1942 {
1943     return static_cast<unsigned>(llvm::dwarf::DW_VIRTUALITY_virtual);
1944 }
1945 
1946 unsigned Emitter::GetFunctionFlags(bool isStaticunsigned accessFlagsbool isExplicit)
1947 {
1948     unsigned flags = llvm::DINode::DIFlags::FlagZero;
1949     if (isStatic)
1950     {
1951         flags |= llvm::DINode::FlagStaticMember;
1952     }
1953     flags |= accessFlags;
1954     if (isExplicit)
1955     {
1956         flags |= llvm::DINode::FlagExplicit;
1957     }
1958     return flags;
1959 }
1960 
1961 /*
1962 void* Emitter::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,
1963     unsigned flags)
1964 {
1965     void* subprogram = diBuilder->createMethod(static_cast<llvm::DIScope*>(CurrentScope()), name, mangledName, static_cast<llvm::DIFile*>(GetDebugInfoForFile(span, moduleId)),
1966         span.line, static_cast<llvm::DISubroutineType*>(subroutineType), false, true, virtuality, vtableIndex, 0, static_cast<llvm::DIType*>(vtableHolder),
1967         static_cast<llvm::DINode::DIFlags>(flags));
1968     return subprogram;
1969 }
1970 */
1971 
1972 
1973 void* Emitter::CreateDIMethod(const std::string& nameconst std::string& mangledNameconst Span& spanconst boost::uuids::uuid& moduleIdvoid* subroutineTypeunsigned virtualityunsigned vtableIndexvoid* vtableHolder
1974     unsigned flags)
1975 {
1976     void* subprogram = diBuilder->createMethod(static_cast<llvm::DIScope*>(CurrentScope())namemangledNamestatic_cast<llvm::DIFile*>(GetDebugInfoForFile(spanmoduleId))
1977         span.linestatic_cast<llvm::DISubroutineType*>(subroutineType)vtableIndex0static_cast<llvm::DIType*>(vtableHolder)
1978         static_cast<llvm::DINode::DIFlags>(flags)llvm::DISubprogram::SPFlagDefinition);
1979     return subprogram;
1980 }
1981 
1982 /*
1983 void* Emitter::CreateDIFunction(const std::string& name, const std::string& mangledName, const Span& span, const boost::uuids::uuid& moduleId, void* subroutineType, unsigned flags)
1984 {
1985     void* subprogram = diBuilder->createFunction(static_cast<llvm::DIScope*>(CurrentScope()), name, mangledName, static_cast<llvm::DIFile*>(
1986     GetDebugInfoForFile(span, moduleId)),
1987         span.line, static_cast<llvm::DISubroutineType*>(subroutineType), false, true, span.line, static_cast<llvm::DINode::DIFlags>(flags));
1988     return subprogram;
1989 }
1990 */
1991 
1992 
1993 void* Emitter::CreateDIFunction(const std::string& nameconst std::string& mangledNameconst Span& spanconst boost::uuids::uuid& moduleIdvoid* subroutineTypeunsigned flags)
1994 {
1995     void* subprogram = diBuilder->createFunction(static_cast<llvm::DIScope*>(CurrentScope())namemangledNamestatic_cast<llvm::DIFile*>(
1996         GetDebugInfoForFile(spanmoduleId))
1997         span.linestatic_cast<llvm::DISubroutineType*>(subroutineType)span.linestatic_cast<llvm::DINode::DIFlags>(flags)
1998         llvm::DISubprogram::SPFlagDefinition);
1999     return subprogram;
2000 }
2001 
2002 void Emitter::SetDISubprogram(void* functionvoid* subprogram)
2003 {
2004     static_cast<llvm::Function*>(function)->setSubprogram(static_cast<llvm::DISubprogram*>(subprogram));
2005 }
2006 
2007 void* Emitter::CreateAlloca(void* irType)
2008 {
2009     return builder.CreateAlloca(static_cast<llvm::Type*>(irType));
2010 }
2011 
2012 void* Emitter::CreateDIParameterVariable(const std::string& nameint indexconst Span& spanconst boost::uuids::uuid& moduleIdvoid* irTypevoid* allocaInst)
2013 {
2014     llvm::DILocalVariable* paramVar = diBuilder->createParameterVariable(static_cast<llvm::DIScope*>(CurrentScope())nameindexstatic_cast<llvm::DIFile*>(
2015         GetDebugInfoForFile(spanmoduleId))
2016         span.linestatic_cast<llvm::DIType*>(irType));
2017     llvm::SmallVector<int64_t13> expr;
2018     diBuilder->insertDeclare(static_cast<llvm::Value*>(allocaInst)paramVardiBuilder->createExpression(expr)GetDebugLocation(span)builder.GetInsertBlock());
2019     return paramVar;
2020 }
2021 
2022 void* Emitter::CreateDIAutoVariable(const std::string& nameconst Span& spanconst boost::uuids::uuid& moduleIdvoid* irTypevoid* allocaInst)
2023 {
2024     llvm::DILocalVariable* localVar = diBuilder->createAutoVariable(static_cast<llvm::DIScope*>(CurrentScope())namestatic_cast<llvm::DIFile*>(GetDebugInfoForFile(spanmoduleId))
2025         span.linestatic_cast<llvm::DIType*>(irType));
2026     llvm::SmallVector<int64_t13> expr;
2027     diBuilder->insertDeclare(static_cast<llvm::Value*>(allocaInst)localVardiBuilder->createExpression(expr)GetDebugLocation(span)builder.GetInsertBlock());
2028     return localVar;
2029 }
2030 
2031 void* Emitter::GetFunctionArgument(void* functionint argumentIndex)
2032 {
2033     auto it = static_cast<llvm::Function*>(function)->args().begin() + argumentIndex;
2034     return &*it;
2035 }
2036 
2037 void Emitter::SetDebugLoc(void* callInst)
2038 {
2039     static_cast<llvm::CallInst*>(callInst)->setDebugLoc(GetCurrentDebugLocation());
2040 }
2041 
2042 void* Emitter::CreateRet(void* value)
2043 {
2044     return builder.CreateRet(static_cast<llvm::Value*>(value));
2045 }
2046 
2047 void* Emitter::CreateRetVoid()
2048 {
2049     return builder.CreateRetVoid();
2050 }
2051 
2052 void Emitter::SetPersonalityFunction(void* functionvoid* personalityFunction)
2053 {
2054     static_cast<llvm::Function*>(function)->setPersonalityFn(llvm::ConstantExpr::getBitCast(static_cast<llvm::Function*>(personalityFunction)builder.getInt8PtrTy()));
2055 }
2056 
2057 void Emitter::AddNoUnwindAttribute(void* function)
2058 {
2059     static_cast<llvm::Function*>(function)->addFnAttr(llvm::Attribute::NoUnwind);
2060 }
2061 
2062 void Emitter::AddUWTableAttribute(void* function)
2063 {
2064     static_cast<llvm::Function*>(function)->addFnAttr(llvm::Attribute::UWTable);
2065 }
2066 
2067 void* Emitter::CreateLexicalBlock(const Span& spanconst boost::uuids::uuid& moduleId)
2068 {
2069     llvm::DILexicalBlock* block = diBuilder->createLexicalBlock(static_cast<llvm::DIScope*>(CurrentScope())static_cast<llvm::DIFile*>(GetDebugInfoForFile(spanmoduleId))
2070         span.lineGetColumn(span));
2071     PushScope(block);
2072     return block;
2073 }
2074 
2075 void* Emitter::CreateSwitch(void* conditionvoid* defaultDestunsigned numCases)
2076 {
2077     void* switchInst = builder.CreateSwitch(static_cast<llvm::Value*>(condition)static_cast<llvm::BasicBlock*>(defaultDest)numCases);
2078     return switchInst;
2079 }
2080 
2081 void Emitter::AddCase(void* switchInstvoid* caseValuevoid* caseDest)
2082 {
2083     static_cast<llvm::SwitchInst*>(switchInst)->addCase(llvm::cast<llvm::ConstantInt>(static_cast<llvm::Value*>(caseValue))static_cast<llvm::BasicBlock*>(caseDest));
2084 }
2085 
2086 void* Emitter::CreateCleanupPadWithParent(void* parentPadconst std::std::vector<void*>&args)
2087 {
2088     std::vector<llvm::Value*> arguments;
2089     for (void* arg : args)
2090     {
2091         arguments.push_back(static_cast<llvm::Value*>(arg));
2092     }
2093     return builder.CreateCleanupPad(static_cast<llvm::Value*>(parentPad)arguments);
2094 }
2095 
2096 void* Emitter::CreateCleanupPad(const std::std::vector<void*>&args)
2097 {
2098     std::vector<llvm::Value*> arguments;
2099     for (void* arg : args)
2100     {
2101         arguments.push_back(static_cast<llvm::Value*>(arg));
2102     }
2103     return builder.CreateCleanupPad(llvm::ConstantTokenNone::get(context)arguments);
2104 }
2105 
2106 void* Emitter::CreateCleanupRet(void* cleanupPadvoid* unwindTarget)
2107 {
2108     return builder.CreateCleanupRet(llvm::cast<llvm::CleanupPadInst>(static_cast<llvm::Value*>(cleanupPad))static_cast<llvm::BasicBlock*>(unwindTarget));
2109 }
2110 
2111 void* Emitter::CreateCatchRet(void* catchPadvoid* returnTarget)
2112 {
2113     return builder.CreateCatchRet(llvm::cast<llvm::CatchPadInst>(static_cast<llvm::Value*>(catchPad))static_cast<llvm::BasicBlock*>(returnTarget));
2114 }
2115 
2116 void* Emitter::CreateCatchSwitch(void* unwindBlock)
2117 {
2118     return builder.CreateCatchSwitch(llvm::ConstantTokenNone::get(context)static_cast<llvm::BasicBlock*>(unwindBlock)1);
2119 }
2120 
2121 void* Emitter::CreateCatchSwitchWithParent(void* parentPadvoid* unwindBlock)
2122 {
2123     return builder.CreateCatchSwitch(static_cast<llvm::Value*>(parentPad)static_cast<llvm::BasicBlock*>(unwindBlock)1);
2124 }
2125 
2126 void Emitter::AddHandlerToCatchSwitch(void* catchSwitchvoid* target)
2127 {
2128     static_cast<llvm::CatchSwitchInst*>(catchSwitch)->addHandler(static_cast<llvm::BasicBlock*>(target));
2129 }
2130 
2131 void* Emitter::CreateCatchPad(void* parentPadconst std::std::vector<void*>&args)
2132 {
2133     std::vector<llvm::Value*> arguments;
2134     for (void* arg : args)
2135     {
2136         arguments.push_back(static_cast<llvm::Value*>(arg));
2137     }
2138     return builder.CreateCatchPad(static_cast<llvm::Value*>(parentPad)arguments);
2139 }
2140 
2141 void* Emitter::GenerateTrap(const std::std::vector<void*>&args)
2142 {
2143     return nullptr;
2144 }
2145 
2146 void Emitter::SetCompileUnitId(const std::string& compileUnitId_)
2147 {
2148     compileUnitId = compileUnitId_;
2149 }
2150 
2151 void* Emitter::GetClsIdValue(const std::string& typeId)
2152 {
2153     return nullptr;
2154 }
2155 
2156 void* Emitter::CreateMDBool(bool value)
2157 {
2158     return nullptr;
2159 }
2160 
2161 void* Emitter::CreateMDLong(int64_t value)
2162 {
2163     return nullptr;
2164 }
2165 
2166 void* Emitter::CreateMDString(const std::string& value)
2167 {
2168     return nullptr;
2169 }
2170 
2171 void* Emitter::CreateMDStructRef(int id)
2172 {
2173     return nullptr;
2174 }
2175 
2176 void* Emitter::CreateMDStruct()
2177 {
2178     return nullptr;
2179 }
2180 
2181 void* Emitter::CreateMDBasicBlockRef(void* bb)
2182 {
2183     return nullptr;
2184 }
2185 
2186 int Emitter::GetMDStructId(void* mdStruct)
2187 {
2188     return 0;
2189 }
2190 
2191 void Emitter::AddMDItem(void* mdStructconst std::string& fieldNamevoid* mdItem)
2192 {
2193 }
2194 
2195 void Emitter::SetFunctionMdId(void* functionint mdId)
2196 {
2197 }
2198 
2199 void Emitter::SetCurrentSourceSpan(int32_t lineint16_t scolint16_t ecol)
2200 {
2201 }
2202 
2203 void* Emitter::GetMDStructRefForSourceFile(const std::string& sourceFileName)
2204 {
2205     return nullptr;
2206 }
2207 
2208 void Emitter::SetMetadataRef(void* instvoid* mdStructRef)
2209 {
2210 }
2211 
2212 void Emitter::FinalizeFunction(void* functionbool hasCleanup)
2213 {
2214 }
2215 
2216 int Emitter::Install(const std::string& str)
2217 {
2218     return emittingDelegate->Install(str);
2219 }
2220 
2221 int Emitter::Install(const std::u16string& str)
2222 {
2223     return emittingDelegate->Install(str);
2224 }
2225 
2226 int Emitter::Install(const std::u32string& str)
2227 {
2228     return emittingDelegate->Install(str);
2229 }
2230 
2231 void* Emitter::CreateLandingPad(void* lpType)
2232 {
2233     llvm::LandingPadInst* lp = builder.CreateLandingPad(static_cast<llvm::Type*>(lpType)1);
2234     return lp;
2235 }
2236 
2237 void Emitter::SetLandindPadAsCleanup(void* landingPad)
2238 {
2239     llvm::LandingPadInst* lp = static_cast<llvm::LandingPadInst*>(landingPad);
2240     lp->setCleanup(true);
2241 }
2242 
2243 void Emitter::MoveAllocaIntoBasicBlock(void* allocaInstvoid* lastAllocavoid* basicBlock)
2244 {
2245     if (lastAlloca)
2246     {
2247         static_cast<llvm::AllocaInst*>(allocaInst)->moveAfter(static_cast<llvm::AllocaInst*>(lastAlloca));
2248     }
2249     else
2250     {
2251         llvm::BasicBlock* block = static_cast<llvm::BasicBlock*>(basicBlock);
2252         if (block->empty())
2253         {
2254             static_cast<llvm::AllocaInst*>(allocaInst)->removeFromParent();
2255             block->getInstList().push_back(static_cast<llvm::AllocaInst*>(allocaInst));
2256         }
2257         else
2258         {
2259             static_cast<llvm::AllocaInst*>(allocaInst)->moveBefore(block->getTerminator());
2260         }
2261     }
2262 }
2263 
2264 void Emitter::AddClauseToLangdingPad(void* landingPadvoid* exceptionTypeId)
2265 {
2266     llvm::LandingPadInst* lp = static_cast<llvm::LandingPadInst*>(landingPad);
2267     llvm::Constant* typeId = static_cast<llvm::Constant*>(exceptionTypeId);
2268     lp->addClause(llvm::cast<llvm::Constant>(builder.CreateBitCast(typeIdbuilder.getInt8PtrTy())));
2269 }
2270 
2271 void* Emitter::CreateExtractValue(void* aggregateconst std::std::vector<unsigned int>&indeces)
2272 {
2273     return builder.CreateExtractValue(static_cast<llvm::Value*>(aggregate)indeces);
2274 }
2275 
2276 void* Emitter::CreateInsertValue(void* aggregatevoid* valueconst std::std::vector<unsigned int>&indeces)
2277 {
2278     return builder.CreateInsertValue(static_cast<llvm::Value*>(aggregate)static_cast<llvm::Value*>(value)indeces);
2279 }
2280 
2281 void* Emitter::CreateUndefValue(void* type)
2282 {
2283     return llvm::UndefValue::get(static_cast<llvm::Type*>(type));
2284 }
2285 
2286 void Emitter::CreateResume(void* exception)
2287 {
2288     builder.CreateResume(static_cast<llvm::Value*>(exception));
2289 }
2290 
2291 void Emitter::DebugPrintDebugInfo(const std::string& filePath)
2292 {
2293     std::vector<std::string> info;
2294     for (auto& p : diTypeNameMap)
2295     {
2296         std::stringstream s;
2297         s << std::setw(16) << std::setfill('0');
2298         s << std::hex << p.first;
2299         s << std::setw(0);
2300         s << " = " << p.second;
2301         info.push_back(s.str());
2302     }
2303     std::sort(info.begin()info.end());
2304     std::ofstream file(filePath);
2305     for (const std::string& line : info)
2306     {
2307         file << line << std::endl;
2308     }
2309 }
2310 
2311 void Emitter::BeginSubstituteLineNumber(int32_t lineNumber)
2312 {
2313     substituteLineNumber = true;
2314     currentLineNumber = lineNumber;
2315 }
2316 
2317 void Emitter::EndSubstituteLineNumber()
2318 {
2319     substituteLineNumber = false;
2320 }
2321 
2322 } // namespace cmllvm