1 // =================================
 2 // Copyright (c) 2021 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 #include <cmajor/cmllvm/EmittingContext.hpp>
 7 
 8 namespace cmllvm {
 9 
10 EmittingContext::EmittingContext(int optimizationLevel) : targetTriple()optimizationLevel(0)targetMachine()dataLayout()
11 {
12     llvm::InitializeAllTargetInfos();
13     llvm::InitializeAllTargets();
14     llvm::InitializeAllTargetMCs();
15     llvm::InitializeAllAsmParsers();
16     llvm::InitializeAllAsmPrinters();
17     llvm::PassRegistry* passRegistry = llvm::PassRegistry::getPassRegistry();
18     llvm::initializeCore(*passRegistry);
19     llvm::initializeScalarOpts(*passRegistry);
20     llvm::initializeVectorization(*passRegistry);
21     llvm::initializeIPO(*passRegistry);
22     llvm::initializeAnalysis(*passRegistry);
23     llvm::initializeTransformUtils(*passRegistry);
24     llvm::initializeInstCombine(*passRegistry);
25     llvm::initializeInstrumentation(*passRegistry);
26     llvm::initializeTarget(*passRegistry);
27     llvm::initializeCodeGen(*passRegistry);
28 #ifdef _WIN32
29 
30 #else
31     targetTriple = llvm::sys::getDefaultTargetTriple();
32 #endif
33     std::string error;
34     const llvm::Target* target = llvm::TargetRegistry::lookupTarget(targetTripleerror);
35     if (!target)
36     {
37         throw std::runtime_error("EmittingContext: TargetRegistry::lookupTarget failed: " + error);
38     }
39     llvm::TargetOptions targetOptions = {};
40 #ifdef _WIN32
41 
42 #endif
43     llvm::Optional<llvm::Reloc::Model> relocModel;
44     llvm::CodeModel::Model codeModel = llvm::CodeModel::Large;
45     llvm::CodeGenOpt::Level codeGenLevel = llvm::CodeGenOpt::None;
46     switch (optimizationLevel)
47     {
48     case 0: codeGenLevel = llvm::CodeGenOpt::None; break;
49     case 1: codeGenLevel = llvm::CodeGenOpt::Less; break;
50     case 2: codeGenLevel = llvm::CodeGenOpt::Default; break;
51     case 3: codeGenLevel = llvm::CodeGenOpt::Aggressive; break;
52     }
53     targetMachine.reset(target->createTargetMachine(targetTriple"generic"""targetOptionsrelocModelcodeModelcodeGenLevel));
54     dataLayout.reset(new llvm::DataLayout(targetMachine->createDataLayout()));
55 }
56 
57 } // namespace cmllvm