1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
15 #include "llvm/Support/CBindingWrapping.h"
16 #include "llvm/Target/TargetLibraryInfo.h"
17 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
19 #include "llvm-c/Transforms/PassManagerBuilder.h"
23 extern cl::opt<bool> EnableARMEHABI;
25 typedef struct LLVMOpaquePass *LLVMPassRef;
26 typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
28 DEFINE_STDCXX_CONVERSION_FUNCTIONS(Pass, LLVMPassRef)
29 DEFINE_STDCXX_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
30 DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBuilder, LLVMPassManagerBuilderRef)
33 LLVMInitializePasses() {
34 PassRegistry &Registry = *PassRegistry::getPassRegistry();
35 initializeCore(Registry);
36 initializeCodeGen(Registry);
37 initializeScalarOpts(Registry);
38 initializeVectorization(Registry);
39 initializeIPO(Registry);
40 initializeAnalysis(Registry);
41 initializeIPA(Registry);
42 initializeTransformUtils(Registry);
43 initializeInstCombine(Registry);
44 initializeInstrumentation(Registry);
45 initializeTarget(Registry);
49 LLVMRustAddPass(LLVMPassManagerRef PM, const char *PassName) {
50 PassManagerBase *pm = unwrap(PM);
52 StringRef SR(PassName);
53 PassRegistry *PR = PassRegistry::getPassRegistry();
55 const PassInfo *PI = PR->getPassInfo(SR);
57 pm->add(PI->createPass());
63 extern "C" LLVMTargetMachineRef
64 LLVMRustCreateTargetMachine(const char *triple,
69 CodeGenOpt::Level OptLevel,
70 bool EnableSegmentedStacks,
72 bool NoFramePointerElim) {
74 Triple Trip(Triple::normalize(triple));
75 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Trip.getTriple(),
77 if (TheTarget == NULL) {
78 LLVMRustSetLastError(Error.c_str());
82 TargetOptions Options;
83 Options.NoFramePointerElim = NoFramePointerElim;
84 #if LLVM_VERSION_MINOR < 5
85 Options.EnableSegmentedStacks = EnableSegmentedStacks;
87 Options.FloatABIType = FloatABI::Default;
88 Options.UseSoftFloat = UseSoftFloat;
90 Options.FloatABIType = FloatABI::Soft;
93 TargetMachine *TM = TheTarget->createTargetMachine(Trip.getTriple(),
104 LLVMRustDisposeTargetMachine(LLVMTargetMachineRef TM) {
108 // Unfortunately, LLVM doesn't expose a C API to add the corresponding analysis
109 // passes for a target to a pass manager. We export that functionality through
112 LLVMRustAddAnalysisPasses(LLVMTargetMachineRef TM,
113 LLVMPassManagerRef PMR,
115 PassManagerBase *PM = unwrap(PMR);
116 #if LLVM_VERSION_MINOR >= 5
117 PM->add(new DataLayoutPass(unwrap(M)));
119 PM->add(new DataLayout(unwrap(M)));
121 unwrap(TM)->addAnalysisPasses(*PM);
124 // Unfortunately, the LLVM C API doesn't provide a way to set the `LibraryInfo`
125 // field of a PassManagerBuilder, we expose our own method of doing so.
127 LLVMRustAddBuilderLibraryInfo(LLVMPassManagerBuilderRef PMB, LLVMModuleRef M) {
128 Triple TargetTriple(unwrap(M)->getTargetTriple());
129 unwrap(PMB)->LibraryInfo = new TargetLibraryInfo(TargetTriple);
132 // Unfortunately, the LLVM C API doesn't provide a way to create the
133 // TargetLibraryInfo pass, so we use this method to do so.
135 LLVMRustAddLibraryInfo(LLVMPassManagerRef PMB, LLVMModuleRef M) {
136 Triple TargetTriple(unwrap(M)->getTargetTriple());
137 unwrap(PMB)->add(new TargetLibraryInfo(TargetTriple));
140 // Unfortunately, the LLVM C API doesn't provide an easy way of iterating over
141 // all the functions in a module, so we do that manually here. You'll find
142 // similar code in clang's BackendUtil.cpp file.
144 LLVMRustRunFunctionPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
145 FunctionPassManager *P = unwrap<FunctionPassManager>(PM);
146 P->doInitialization();
147 for (Module::iterator I = unwrap(M)->begin(),
148 E = unwrap(M)->end(); I != E; ++I)
149 if (!I->isDeclaration())
155 LLVMRustSetLLVMOptions(int Argc, char **Argv) {
156 // Initializing the command-line options more than once is not allowed. So,
157 // check if they've already been initialized. (This could happen if we're
158 // being called from rustpkg, for example). If the arguments change, then
159 // that's just kinda unfortunate.
160 static bool initialized = false;
161 if (initialized) return;
163 cl::ParseCommandLineOptions(Argc, Argv);
167 LLVMRustWriteOutputFile(LLVMTargetMachineRef Target,
168 LLVMPassManagerRef PMR,
171 TargetMachine::CodeGenFileType FileType) {
172 PassManager *PM = unwrap<PassManager>(PMR);
174 std::string ErrorInfo;
175 #if LLVM_VERSION_MINOR >= 4
176 raw_fd_ostream OS(path, ErrorInfo, sys::fs::F_None);
178 raw_fd_ostream OS(path, ErrorInfo, raw_fd_ostream::F_Binary);
180 if (ErrorInfo != "") {
181 LLVMRustSetLastError(ErrorInfo.c_str());
184 formatted_raw_ostream FOS(OS);
186 unwrap(Target)->addPassesToEmitFile(*PM, FOS, FileType, false);
192 LLVMRustPrintModule(LLVMPassManagerRef PMR,
195 PassManager *PM = unwrap<PassManager>(PMR);
196 std::string ErrorInfo;
198 #if LLVM_VERSION_MINOR >= 4
199 raw_fd_ostream OS(path, ErrorInfo, sys::fs::F_None);
201 raw_fd_ostream OS(path, ErrorInfo, raw_fd_ostream::F_Binary);
204 formatted_raw_ostream FOS(OS);
206 #if LLVM_VERSION_MINOR >= 5
207 PM->add(createPrintModulePass(FOS));
209 PM->add(createPrintModulePass(&FOS));
216 LLVMRustPrintPasses() {
217 LLVMInitializePasses();
218 struct MyListener : PassRegistrationListener {
219 void passEnumerate(const PassInfo *info) {
220 if (info->getPassArgument() && *info->getPassArgument()) {
221 printf("%15s - %s\n", info->getPassArgument(),
222 info->getPassName());
227 PassRegistry *PR = PassRegistry::getPassRegistry();
228 PR->enumerateWith(&listener);
232 LLVMRustAddAlwaysInlinePass(LLVMPassManagerBuilderRef PMB, bool AddLifetimes) {
233 unwrap(PMB)->Inliner = createAlwaysInlinerPass(AddLifetimes);
237 LLVMRustRunRestrictionPass(LLVMModuleRef M, char **symbols, size_t len) {
239 ArrayRef<const char*> ref(symbols, len);
240 passes.add(llvm::createInternalizePass(ref));
241 passes.run(*unwrap(M));
245 LLVMRustMarkAllFunctionsNounwind(LLVMModuleRef M) {
246 for (Module::iterator GV = unwrap(M)->begin(),
247 E = unwrap(M)->end(); GV != E; ++GV) {
248 GV->setDoesNotThrow();
249 Function *F = dyn_cast<Function>(GV);
253 for (Function::iterator B = F->begin(), BE = F->end(); B != BE; ++B) {
254 for (BasicBlock::iterator I = B->begin(), IE = B->end();
256 if (isa<InvokeInst>(I)) {
257 InvokeInst *CI = cast<InvokeInst>(I);
258 CI->setDoesNotThrow();