llvm.org GIT mirror llvm / d6b7b8f
Add a C binding to the Target and TargetMachine classes to allow for emitting binary and assembly. Patch by Carlo Kok. Emitting was inspired by but not based on the D llvm bindings. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154493 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 8 years ago
3 changed file(s) with 340 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 /*===-- llvm-c/TargetMachine.h - Target Machine Library C Interface - C++ -*-=*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file is distributed under the University of Illinois Open Source *|
5 |* License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* This header declares the C interface to the Target and TargetMachine *|
10 |* classes, which can be used to generate assembly or object files. *|
11 |* *|
12 |* Many exotic languages can interoperate with C code but have a harder time *|
13 |* with C++ due to name mangling. So in addition to C, this interface enables *|
14 |* tools written in such languages. *|
15 |* *|
16 \*===----------------------------------------------------------------------===*/
17
18 #ifndef LLVM_C_TARGETMACHINE_H
19 #define LLVM_C_TARGETMACHINE_H
20
21 #include "llvm-c/Core.h"
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 typedef struct LLVMTargetMachine *LLVMTargetMachineRef;
27 typedef struct LLVMTarget *LLVMTargetRef;
28
29 typedef enum {
30 LLVMCodeGenLevelNone,
31 LLVMCodeGenLevelLess,
32 LLVMCodeGenLevelDefault,
33 LLVMCodeGenLevelAggressive
34 } LLVMCodeGenOptLevel;
35
36 typedef enum {
37 LLVMRelocDefault,
38 LLVMRelocStatic,
39 LLVMRelocPIC,
40 LLVMRelocDynamicNoPic
41 } LLVMRelocMode;
42
43 typedef enum {
44 LLVMCodeModelDefault,
45 LLVMCodeModelJITDefault,
46 LLVMCodeModelSmall,
47 LLVMCodeModelKernel,
48 LLVMCodeModelMedium,
49 LLVMCodeModelLarge
50 } LLVMCodeModel;
51
52 typedef enum {
53 LLVMAssemblyFile,
54 LLVMObjectFile
55 } LLVMCodeGenFileType;
56
57 /** Returns the first llvm::Target in the registered targets list. */
58 LLVMTargetRef LLVMGetFirstTarget();
59 /** Returns the next llvm::Target given a previous one (or null if there's none) */
60 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T);
61
62 /*===-- Target ------------------------------------------------------------===*/
63 /** Returns the name of a target. See llvm::Target::getName */
64 const char *LLVMGetTargetName(LLVMTargetRef T);
65
66 /** Returns the description of a target. See llvm::Target::getDescription */
67 const char *LLVMGetTargetDescription(LLVMTargetRef T);
68
69 /** Returns if the target has a JIT */
70 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T);
71
72 /** Returns if the target has a TargetMachine associated */
73 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T);
74
75 /** Returns if the target as an ASM backend (required for emitting output) */
76 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T);
77
78 /*===-- Target Machine ----------------------------------------------------===*/
79 /** Creates a new llvm::TargetMachine. See llvm::Target::createTargetMachine */
80 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, char *Triple,
81 char *CPU, char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
82 LLVMCodeModel CodeModel);
83
84 /** Dispose the LLVMTargetMachineRef instance generated by
85 LLVMCreateTargetMachine. */
86 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T);
87
88 /** Returns the Target used in a TargetMachine */
89 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T);
90
91 /** Returns the triple used creating this target machine. See
92 llvm::TargetMachine::getTriple. The result needs to be disposed with
93 LLVMDisposeMessage. */
94 char *LLVMGetTargetMachineTriple(LLVMTargetMachineRef T);
95
96 /** Returns the cpu used creating this target machine. See
97 llvm::TargetMachine::getCPU. The result needs to be disposed with
98 LLVMDisposeMessage. */
99 char *LLVMGetTargetMachineCPU(LLVMTargetMachineRef T);
100
101 /** Returns the feature string used creating this target machine. See
102 llvm::TargetMachine::getFeatureString. The result needs to be disposed with
103 LLVMDisposeMessage. */
104 char *LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T);
105
106 /** Returns the llvm::TargetData used for this llvm:TargetMachine. */
107 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T);
108
109 /** Emits an asm or object file for the given module to the filename. This
110 wraps several c++ only classes (among them a file stream). Returns any
111 error in ErrorMessage. Use LLVMDisposeMessage to dispose the message. */
112 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
113 char *Filename, LLVMCodeGenFileType codegen, char **ErrorMessage);
114
115
116
117
118 #ifdef __cplusplus
119 }
120
121 namespace llvm {
122 class TargetMachine;
123 class Target;
124
125 inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
126 return reinterpret_cast(P);
127 }
128 inline Target *unwrap(LLVMTargetRef P) {
129 return reinterpret_cast(P);
130 }
131 inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
132 return reinterpret_cast(
133 const_cast(P));
134 }
135 inline LLVMTargetRef wrap(const Target * P) {
136 return reinterpret_cast(const_cast(P));
137 }
138 }
139 #endif
140
141 #endif
88 TargetLibraryInfo.cpp
99 TargetLoweringObjectFile.cpp
1010 TargetMachine.cpp
11 TargetMachineC.cpp
1112 TargetRegisterInfo.cpp
1213 TargetSubtargetInfo.cpp
1314 )
0 //===-- TargetMachine.cpp -------------------------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the LLVM-C part of TargetMachine.h
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm-c/Core.h"
14 #include "llvm-c/Target.h"
15 #include "llvm-c/TargetMachine.h"
16 #include "llvm/Target/TargetData.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Support/TargetRegistry.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Support/CodeGen.h"
21 #include "llvm/Support/FormattedStream.h"
22 #include "llvm/Module.h"
23 #include "llvm/PassManager.h"
24 #include
25 #include
26 #include
27
28 using namespace llvm;
29
30
31
32 LLVMTargetRef LLVMGetFirstTarget() {
33 const Target* target = &*TargetRegistry::begin();
34 return wrap(target);
35 }
36 LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
37 return wrap(unwrap(T)->getNext());
38 }
39
40 const char * LLVMGetTargetName(LLVMTargetRef T) {
41 return unwrap(T)->getName();
42 }
43
44 const char * LLVMGetTargetDescription(LLVMTargetRef T) {
45 return unwrap(T)->getShortDescription();
46 }
47
48 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
49 return unwrap(T)->hasJIT();
50 }
51
52 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
53 return unwrap(T)->hasTargetMachine();
54 }
55
56 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
57 return unwrap(T)->hasMCAsmBackend();
58 }
59
60 LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, char* Triple,
61 char* CPU, char* Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
62 LLVMCodeModel CodeModel) {
63 Reloc::Model RM;
64 switch (Reloc){
65 case LLVMRelocStatic:
66 RM = Reloc::Static;
67 break;
68 case LLVMRelocPIC:
69 RM = Reloc::PIC_;
70 break;
71 case LLVMRelocDynamicNoPic:
72 RM = Reloc::DynamicNoPIC;
73 break;
74 default:
75 RM = Reloc::Default;
76 break;
77 }
78
79 CodeModel::Model CM;
80 switch (CodeModel) {
81 case LLVMCodeModelJITDefault:
82 CM = CodeModel::JITDefault;
83 break;
84 case LLVMCodeModelSmall:
85 CM = CodeModel::Small;
86 break;
87 case LLVMCodeModelKernel:
88 CM = CodeModel::Kernel;
89 break;
90 case LLVMCodeModelMedium:
91 CM = CodeModel::Medium;
92 break;
93 case LLVMCodeModelLarge:
94 CM = CodeModel::Large;
95 break;
96 default:
97 CM = CodeModel::Default;
98 break;
99 }
100 CodeGenOpt::Level OL;
101
102 switch (Level) {
103 case LLVMCodeGenLevelNone:
104 OL = CodeGenOpt::None;
105 break;
106 case LLVMCodeGenLevelLess:
107 OL = CodeGenOpt::Less;
108 break;
109 case LLVMCodeGenLevelAggressive:
110 OL = CodeGenOpt::Aggressive;
111 break;
112 default:
113 OL = CodeGenOpt::Default;
114 break;
115 }
116
117 TargetOptions opt;
118 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
119 CM, OL));
120 }
121
122
123 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
124 delete unwrap(T);
125 }
126
127 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
128 const Target* target = &(unwrap(T)->getTarget());
129 return wrap(target);
130 }
131
132 char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
133 std::string StringRep = unwrap(T)->getTargetTriple();
134 return strdup(StringRep.c_str());
135 }
136
137 char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
138 std::string StringRep = unwrap(T)->getTargetCPU();
139 return strdup(StringRep.c_str());
140 }
141
142 char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
143 std::string StringRep = unwrap(T)->getTargetFeatureString();
144 return strdup(StringRep.c_str());
145 }
146
147 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
148 return wrap(unwrap(T)->getTargetData());
149 }
150
151 LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
152 char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
153 TargetMachine* TM = unwrap(T);
154 Module* Mod = unwrap(M);
155
156 PassManager pass;
157
158 std::string error;
159
160 const TargetData* td = TM->getTargetData();
161
162 if (!td) {
163 error = "No TargetData in TargetMachine";
164 *ErrorMessage = strdup(error.c_str());
165 return true;
166 }
167 pass.add(new TargetData(*td));
168
169 TargetMachine::CodeGenFileType ft;
170 switch (codegen) {
171 case LLVMAssemblyFile:
172 ft = TargetMachine::CGFT_AssemblyFile;
173 break;
174 default:
175 ft = TargetMachine::CGFT_ObjectFile;
176 break;
177 }
178 raw_fd_ostream dest(Filename, error, raw_fd_ostream::F_Binary);
179 formatted_raw_ostream destf(dest);
180 if (!error.empty()) {
181 *ErrorMessage = strdup(error.c_str());
182 return true;
183 }
184
185 if (TM->addPassesToEmitFile(pass, destf, ft)) {
186 error = "No TargetData in TargetMachine";
187 *ErrorMessage = strdup(error.c_str());
188 return true;
189 }
190
191 pass.run(*Mod);
192
193 destf.flush();
194 dest.flush();
195 return false;
196 }