generating x86 code works
[kazan.git] / src / llvm_wrapper / llvm_wrapper.cpp
1 /*
2 * Copyright 2017 Jacob Lifshay
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
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
20 * SOFTWARE.
21 *
22 */
23 #include "llvm_wrapper.h"
24 #include <llvm/Support/Host.h>
25 #include <llvm/ExecutionEngine/SectionMemoryManager.h>
26 #include <llvm-c/ExecutionEngine.h>
27 #include <iostream>
28 #include <cstdlib>
29 #include <algorithm>
30
31 namespace vulkan_cpu
32 {
33 namespace llvm_wrapper
34 {
35 void Context::init_helper()
36 {
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");
47 }
48
49 LLVM_string Target::get_process_target_triple()
50 {
51 Context::init();
52 return LLVM_string::from(llvm::sys::getProcessTriple());
53 }
54
55 LLVM_string Target::get_host_cpu_name()
56 {
57 Context::init();
58 return LLVM_string::from(llvm::sys::getHostCPUName());
59 }
60
61 LLVM_string Target::get_host_cpu_features()
62 {
63 Context::init();
64 llvm::StringMap<bool> features{};
65 if(!llvm::sys::getHostCPUFeatures(features))
66 return LLVM_string::from("");
67 std::string retval;
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());
73 bool first = true;
74 for(auto &name : names)
75 {
76 if(first)
77 first = false;
78 else
79 retval += ',';
80 if(features[name])
81 retval += '+';
82 else
83 retval += '-';
84 retval += name;
85 }
86 return LLVM_string::from(retval);
87 }
88
89 Target_machine Target_machine::create_native_target_machine()
90 {
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,
97 ::LLVMRelocDefault,
98 ::LLVMCodeModelJITDefault));
99 }
100
101 void Module::set_target_machine(::LLVMModuleRef module, ::LLVMTargetMachineRef target_machine)
102 {
103 ::LLVMSetTarget(module, Target_machine::get_target_triple(target_machine).get());
104 ::LLVMSetModuleDataLayout(module,
105 Target_machine::create_target_data_layout(target_machine).get());
106 }
107
108 void Module::set_function_target_machine(::LLVMValueRef function,
109 ::LLVMTargetMachineRef target_machine)
110 {
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());
115 }
116 }
117 }