1 //===-- TargetMachine.cpp -------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LLVM-C part of TargetMachine.h
12 //===----------------------------------------------------------------------===//
14 #include "llvm-c/TargetMachine.h"
15 #include "llvm-c/Core.h"
16 #include "llvm-c/Target.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/Support/CodeGen.h"
21 #include "llvm/Support/FormattedStream.h"
22 #include "llvm/Support/TargetRegistry.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Target/TargetMachine.h"
33 LLVMTargetRef LLVMGetFirstTarget() {
34 const Target* target = &*TargetRegistry::begin();
37 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
38 return wrap(unwrap(T)->getNext());
41 const char * LLVMGetTargetName(LLVMTargetRef T) {
42 return unwrap(T)->getName();
45 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
46 return unwrap(T)->getShortDescription();
49 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
50 return unwrap(T)->hasJIT();
53 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
54 return unwrap(T)->hasTargetMachine();
57 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
58 return unwrap(T)->hasMCAsmBackend();
61 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, char* Triple,
62 char* CPU, char* Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
63 LLVMCodeModel CodeModel) {
72 case LLVMRelocDynamicNoPic:
73 RM = Reloc::DynamicNoPIC;
82 case LLVMCodeModelJITDefault:
83 CM = CodeModel::JITDefault;
85 case LLVMCodeModelSmall:
86 CM = CodeModel::Small;
88 case LLVMCodeModelKernel:
89 CM = CodeModel::Kernel;
91 case LLVMCodeModelMedium:
92 CM = CodeModel::Medium;
94 case LLVMCodeModelLarge:
95 CM = CodeModel::Large;
98 CM = CodeModel::Default;
101 CodeGenOpt::Level OL;
104 case LLVMCodeGenLevelNone:
105 OL = CodeGenOpt::None;
107 case LLVMCodeGenLevelLess:
108 OL = CodeGenOpt::Less;
110 case LLVMCodeGenLevelAggressive:
111 OL = CodeGenOpt::Aggressive;
114 OL = CodeGenOpt::Default;
119 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
124 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
128 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
129 const Target* target = &(unwrap(T)->getTarget());
133 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
134 std::string StringRep = unwrap(T)->getTargetTriple();
135 return strdup(StringRep.c_str());
138 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
139 std::string StringRep = unwrap(T)->getTargetCPU();
140 return strdup(StringRep.c_str());
143 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
144 std::string StringRep = unwrap(T)->getTargetFeatureString();
145 return strdup(StringRep.c_str());
148 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
149 return wrap(unwrap(T)->getDataLayout());
152 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
153 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
154 TargetMachine* TM = unwrap(T);
155 Module* Mod = unwrap(M);
161 const DataLayout* td = TM->getDataLayout();
164 error = "No DataLayout in TargetMachine";
165 *ErrorMessage = strdup(error.c_str());
168 pass.add(new DataLayout(*td));
170 TargetMachine::CodeGenFileType ft;
172 case LLVMAssemblyFile:
173 ft = TargetMachine::CGFT_AssemblyFile;
176 ft = TargetMachine::CGFT_ObjectFile;
179 raw_fd_ostream dest(Filename, error, raw_fd_ostream::F_Binary);
180 formatted_raw_ostream destf(dest);
181 if (!error.empty()) {
182 *ErrorMessage = strdup(error.c_str());
186 if (TM->addPassesToEmitFile(pass, destf, ft)) {
187 error = "No DataLayout in TargetMachine";
188 *ErrorMessage = strdup(error.c_str());