|
17 | 17 | #include "llvm/IR/LegacyPassManager.h"
|
18 | 18 | #include "llvm/IR/Module.h"
|
19 | 19 | #include "llvm/MC/TargetRegistry.h"
|
| 20 | +#include "llvm/Support/CBindingWrapping.h" |
20 | 21 | #include "llvm/Support/FileSystem.h"
|
21 | 22 | #include "llvm/Support/raw_ostream.h"
|
22 | 23 | #include "llvm/Target/CodeGenCWrappers.h"
|
|
28 | 29 |
|
29 | 30 | using namespace llvm;
|
30 | 31 |
|
| 32 | +namespace llvm { |
| 33 | + |
| 34 | +/// Options for LLVMCreateTargetMachine(). |
| 35 | +struct LLVMTargetMachineOptions { |
| 36 | + std::string CPU; |
| 37 | + std::string Features; |
| 38 | + std::string ABI; |
| 39 | + CodeGenOptLevel OL = CodeGenOptLevel::Default; |
| 40 | + std::optional<Reloc::Model> RM; |
| 41 | + std::optional<CodeModel::Model> CM; |
| 42 | + bool JIT; |
| 43 | +}; |
| 44 | + |
| 45 | +} // namespace llvm |
| 46 | + |
| 47 | +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMTargetMachineOptions, |
| 48 | + LLVMTargetMachineOptionsRef) |
| 49 | + |
31 | 50 | static TargetMachine *unwrap(LLVMTargetMachineRef P) {
|
32 | 51 | return reinterpret_cast<TargetMachine *>(P);
|
33 | 52 | }
|
@@ -96,56 +115,114 @@ LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
|
96 | 115 | return unwrap(T)->hasMCAsmBackend();
|
97 | 116 | }
|
98 | 117 |
|
99 |
| -LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, |
100 |
| - const char *Triple, const char *CPU, const char *Features, |
101 |
| - LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, |
102 |
| - LLVMCodeModel CodeModel) { |
103 |
| - std::optional<Reloc::Model> RM; |
104 |
| - switch (Reloc){ |
105 |
| - case LLVMRelocStatic: |
106 |
| - RM = Reloc::Static; |
107 |
| - break; |
108 |
| - case LLVMRelocPIC: |
109 |
| - RM = Reloc::PIC_; |
110 |
| - break; |
111 |
| - case LLVMRelocDynamicNoPic: |
112 |
| - RM = Reloc::DynamicNoPIC; |
113 |
| - break; |
114 |
| - case LLVMRelocROPI: |
115 |
| - RM = Reloc::ROPI; |
116 |
| - break; |
117 |
| - case LLVMRelocRWPI: |
118 |
| - RM = Reloc::RWPI; |
119 |
| - break; |
120 |
| - case LLVMRelocROPI_RWPI: |
121 |
| - RM = Reloc::ROPI_RWPI; |
122 |
| - break; |
123 |
| - default: |
124 |
| - break; |
125 |
| - } |
| 118 | +LLVMTargetMachineOptionsRef LLVMCreateTargetMachineOptions(void) { |
| 119 | + return wrap(new LLVMTargetMachineOptions()); |
| 120 | +} |
126 | 121 |
|
127 |
| - bool JIT; |
128 |
| - std::optional<CodeModel::Model> CM = unwrap(CodeModel, JIT); |
| 122 | +void LLVMDisposeTargetMachineOptions(LLVMTargetMachineOptionsRef Options) { |
| 123 | + delete unwrap(Options); |
| 124 | +} |
| 125 | + |
| 126 | +void LLVMTargetMachineOptionsSetCPU(LLVMTargetMachineOptionsRef Options, |
| 127 | + const char *CPU) { |
| 128 | + unwrap(Options)->CPU = CPU; |
| 129 | +} |
| 130 | + |
| 131 | +void LLVMTargetMachineOptionsSetFeatures(LLVMTargetMachineOptionsRef Options, |
| 132 | + const char *Features) { |
| 133 | + unwrap(Options)->Features = Features; |
| 134 | +} |
129 | 135 |
|
| 136 | +void LLVMTargetMachineOptionsSetABI(LLVMTargetMachineOptionsRef Options, |
| 137 | + const char *ABI) { |
| 138 | + unwrap(Options)->ABI = ABI; |
| 139 | +} |
| 140 | + |
| 141 | +void LLVMTargetMachineOptionsSetCodeGenOptLevel( |
| 142 | + LLVMTargetMachineOptionsRef Options, LLVMCodeGenOptLevel Level) { |
130 | 143 | CodeGenOptLevel OL;
|
| 144 | + |
131 | 145 | switch (Level) {
|
132 |
| - case LLVMCodeGenLevelNone: |
133 |
| - OL = CodeGenOptLevel::None; |
134 |
| - break; |
135 |
| - case LLVMCodeGenLevelLess: |
136 |
| - OL = CodeGenOptLevel::Less; |
137 |
| - break; |
138 |
| - case LLVMCodeGenLevelAggressive: |
139 |
| - OL = CodeGenOptLevel::Aggressive; |
140 |
| - break; |
141 |
| - default: |
142 |
| - OL = CodeGenOptLevel::Default; |
143 |
| - break; |
| 146 | + case LLVMCodeGenLevelNone: |
| 147 | + OL = CodeGenOptLevel::None; |
| 148 | + break; |
| 149 | + case LLVMCodeGenLevelLess: |
| 150 | + OL = CodeGenOptLevel::Less; |
| 151 | + break; |
| 152 | + case LLVMCodeGenLevelAggressive: |
| 153 | + OL = CodeGenOptLevel::Aggressive; |
| 154 | + break; |
| 155 | + case LLVMCodeGenLevelDefault: |
| 156 | + OL = CodeGenOptLevel::Default; |
| 157 | + break; |
144 | 158 | }
|
145 | 159 |
|
146 |
| - TargetOptions opt; |
147 |
| - return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM, CM, |
148 |
| - OL, JIT)); |
| 160 | + unwrap(Options)->OL = OL; |
| 161 | +} |
| 162 | + |
| 163 | +void LLVMTargetMachineOptionsSetRelocMode(LLVMTargetMachineOptionsRef Options, |
| 164 | + LLVMRelocMode Reloc) { |
| 165 | + std::optional<Reloc::Model> RM; |
| 166 | + |
| 167 | + switch (Reloc) { |
| 168 | + case LLVMRelocStatic: |
| 169 | + RM = Reloc::Static; |
| 170 | + break; |
| 171 | + case LLVMRelocPIC: |
| 172 | + RM = Reloc::PIC_; |
| 173 | + break; |
| 174 | + case LLVMRelocDynamicNoPic: |
| 175 | + RM = Reloc::DynamicNoPIC; |
| 176 | + break; |
| 177 | + case LLVMRelocROPI: |
| 178 | + RM = Reloc::ROPI; |
| 179 | + break; |
| 180 | + case LLVMRelocRWPI: |
| 181 | + RM = Reloc::RWPI; |
| 182 | + break; |
| 183 | + case LLVMRelocROPI_RWPI: |
| 184 | + RM = Reloc::ROPI_RWPI; |
| 185 | + break; |
| 186 | + case LLVMRelocDefault: |
| 187 | + break; |
| 188 | + } |
| 189 | + |
| 190 | + unwrap(Options)->RM = RM; |
| 191 | +} |
| 192 | + |
| 193 | +void LLVMTargetMachineOptionsSetCodeModel(LLVMTargetMachineOptionsRef Options, |
| 194 | + LLVMCodeModel CodeModel) { |
| 195 | + auto CM = unwrap(CodeModel, unwrap(Options)->JIT); |
| 196 | + unwrap(Options)->CM = CM; |
| 197 | +} |
| 198 | + |
| 199 | +LLVMTargetMachineRef |
| 200 | +LLVMCreateTargetMachineWithOptions(LLVMTargetRef T, const char *Triple, |
| 201 | + LLVMTargetMachineOptionsRef Options) { |
| 202 | + auto *Opt = unwrap(Options); |
| 203 | + TargetOptions TO; |
| 204 | + TO.MCOptions.ABIName = Opt->ABI; |
| 205 | + return wrap(unwrap(T)->createTargetMachine(Triple, Opt->CPU, Opt->Features, |
| 206 | + TO, Opt->RM, Opt->CM, Opt->OL, |
| 207 | + Opt->JIT)); |
| 208 | +} |
| 209 | + |
| 210 | +LLVMTargetMachineRef |
| 211 | +LLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple, const char *CPU, |
| 212 | + const char *Features, LLVMCodeGenOptLevel Level, |
| 213 | + LLVMRelocMode Reloc, LLVMCodeModel CodeModel) { |
| 214 | + auto *Options = LLVMCreateTargetMachineOptions(); |
| 215 | + |
| 216 | + LLVMTargetMachineOptionsSetCPU(Options, CPU); |
| 217 | + LLVMTargetMachineOptionsSetFeatures(Options, Features); |
| 218 | + LLVMTargetMachineOptionsSetCodeGenOptLevel(Options, Level); |
| 219 | + LLVMTargetMachineOptionsSetRelocMode(Options, Reloc); |
| 220 | + LLVMTargetMachineOptionsSetCodeModel(Options, CodeModel); |
| 221 | + |
| 222 | + auto *Machine = LLVMCreateTargetMachineWithOptions(T, Triple, Options); |
| 223 | + |
| 224 | + LLVMDisposeTargetMachineOptions(Options); |
| 225 | + return Machine; |
149 | 226 | }
|
150 | 227 |
|
151 | 228 | void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { delete unwrap(T); }
|
|
0 commit comments