2 * Copyright 2017 Jacob Lifshay
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #include "llvm_wrapper.h"
24 #include <llvm/Support/Host.h>
25 #include <llvm/ExecutionEngine/SectionMemoryManager.h>
26 #include <llvm-c/ExecutionEngine.h>
33 namespace llvm_wrapper
35 void Context::init_helper()
37 if(!::LLVMIsMultithreaded())
38 throw std::runtime_error("LLVM is not multithreaded");
39 if(::LLVMInitializeNativeTarget() != 0)
40 throw std::runtime_error("LLVMInitializeNativeTarget failed");
41 if(::LLVMInitializeNativeAsmParser() != 0)
42 throw std::runtime_error("LLVMInitializeNativeAsmParser failed");
43 if(::LLVMInitializeNativeAsmPrinter() != 0)
44 throw std::runtime_error("LLVMInitializeNativeAsmPrinter failed");
45 if(::LLVMInitializeNativeDisassembler() != 0)
46 throw std::runtime_error("LLVMInitializeNativeDisassembler failed");
49 LLVM_string
Target::get_process_target_triple()
52 return LLVM_string::from(llvm::sys::getProcessTriple());
55 LLVM_string
Target::get_host_cpu_name()
58 return LLVM_string::from(llvm::sys::getHostCPUName());
61 LLVM_string
Target::get_host_cpu_features()
64 llvm::StringMap
<bool> features
{};
65 if(!llvm::sys::getHostCPUFeatures(features
))
66 return LLVM_string::from("");
68 std::vector
<std::string
> names
;
69 names
.reserve(features
.size());
70 for(auto &entry
: features
)
71 names
.push_back(entry
.first());
72 std::sort(names
.begin(), names
.end());
74 for(auto &name
: names
)
86 return LLVM_string::from(retval
);
89 Target_machine
Target_machine::create_native_target_machine()
91 auto target
= Target::get_native_target();
92 return Target_machine(::LLVMCreateTargetMachine(target
.get(),
93 Target::get_process_target_triple().get(),
94 Target::get_host_cpu_name().get(),
95 Target::get_host_cpu_features().get(),
96 ::LLVMCodeGenLevelDefault
,
98 ::LLVMCodeModelJITDefault
));
101 void Module::set_target_machine(::LLVMModuleRef module
, ::LLVMTargetMachineRef target_machine
)
103 ::LLVMSetTarget(module
, Target_machine::get_target_triple(target_machine
).get());
104 ::LLVMSetModuleDataLayout(module
,
105 Target_machine::create_target_data_layout(target_machine
).get());
108 void Module::set_function_target_machine(::LLVMValueRef function
,
109 ::LLVMTargetMachineRef target_machine
)
111 ::LLVMAddTargetDependentFunctionAttr(
112 function
, "target-cpu", Target_machine::get_cpu(target_machine
).get());
113 ::LLVMAddTargetDependentFunctionAttr(
114 function
, "target-features", Target_machine::get_feature_string(target_machine
).get());