2011-03-15 14:57:26 -07:00
|
|
|
//===- RustWrapper.cpp - Rust wrapper for core functions --------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines alternate interfaces to core functions that are more
|
|
|
|
// readily callable by Rust's FFI.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-04-15 17:35:46 -04:00
|
|
|
#include "llvm/PassManager.h"
|
|
|
|
#include "llvm/ADT/Triple.h"
|
|
|
|
#include "llvm/Support/FormattedStream.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetSelect.h"
|
|
|
|
#include "llvm/Target/TargetRegistry.h"
|
2011-04-26 15:21:20 -07:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2011-03-15 14:57:26 -07:00
|
|
|
#include "llvm-c/Core.h"
|
|
|
|
#include "llvm-c/Object.h"
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2011-04-15 17:35:46 -04:00
|
|
|
using namespace llvm;
|
|
|
|
|
2011-03-15 14:57:26 -07:00
|
|
|
static char *LLVMRustError;
|
|
|
|
|
|
|
|
extern "C" LLVMMemoryBufferRef
|
|
|
|
LLVMRustCreateMemoryBufferWithContentsOfFile(const char *Path) {
|
|
|
|
LLVMMemoryBufferRef MemBuf = NULL;
|
|
|
|
LLVMCreateMemoryBufferWithContentsOfFile(Path, &MemBuf, &LLVMRustError);
|
|
|
|
return MemBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" const char *LLVMRustGetLastError(void) {
|
|
|
|
return LLVMRustError;
|
|
|
|
}
|
|
|
|
|
2011-04-13 13:53:19 -04:00
|
|
|
extern "C" void LLVMAddBasicAliasAnalysisPass(LLVMPassManagerRef PM);
|
|
|
|
|
|
|
|
void (*RustHackToFetchPassesO)(LLVMPassManagerRef PM) =
|
|
|
|
LLVMAddBasicAliasAnalysisPass;
|
2011-04-15 17:35:46 -04:00
|
|
|
|
2011-04-18 10:02:34 -04:00
|
|
|
enum LLVMCodeGenFileType {
|
|
|
|
LLVMAssemblyFile,
|
|
|
|
LLVMObjectFile,
|
|
|
|
LLVMNullFile // Do not emit any output.
|
|
|
|
};
|
|
|
|
|
2011-05-04 20:30:23 -07:00
|
|
|
extern "C" void LLVMRustWriteOutputFile(LLVMPassManagerRef PMR,
|
|
|
|
LLVMModuleRef M,
|
|
|
|
const char *triple,
|
|
|
|
const char *path,
|
2011-04-18 10:02:34 -04:00
|
|
|
LLVMCodeGenFileType FileType) {
|
2011-04-26 15:21:20 -07:00
|
|
|
|
|
|
|
// Set compilation options.
|
2011-04-28 16:14:04 -04:00
|
|
|
llvm::UnwindTablesMandatory = true;
|
2011-05-02 11:01:28 -07:00
|
|
|
llvm::NoFramePointerElim = true;
|
2011-04-26 15:21:20 -07:00
|
|
|
|
2011-04-15 17:35:46 -04:00
|
|
|
InitializeAllTargets();
|
|
|
|
InitializeAllAsmPrinters();
|
2011-04-19 17:07:30 -04:00
|
|
|
InitializeAllAsmParsers();
|
2011-04-15 19:29:13 -04:00
|
|
|
TargetMachine::setRelocationModel(Reloc::PIC_);
|
2011-04-15 17:35:46 -04:00
|
|
|
std::string Err;
|
|
|
|
const Target *TheTarget = TargetRegistry::lookupTarget(triple, Err);
|
|
|
|
std::string FeaturesStr;
|
|
|
|
TargetMachine &Target = *TheTarget->createTargetMachine(triple, FeaturesStr);
|
|
|
|
bool NoVerify = false;
|
|
|
|
CodeGenOpt::Level OLvl = CodeGenOpt::Default;
|
|
|
|
PassManager *PM = unwrap<PassManager>(PMR);
|
|
|
|
std::string ErrorInfo;
|
|
|
|
raw_fd_ostream OS(path, ErrorInfo,
|
|
|
|
raw_fd_ostream::F_Binary);
|
|
|
|
formatted_raw_ostream FOS(OS);
|
2011-04-18 10:02:34 -04:00
|
|
|
TargetMachine::CodeGenFileType FileType2 =
|
|
|
|
static_cast<TargetMachine::CodeGenFileType>(FileType);
|
|
|
|
|
|
|
|
bool foo = Target.addPassesToEmitFile(*PM, FOS, FileType2, OLvl, NoVerify);
|
2011-04-15 17:35:46 -04:00
|
|
|
assert(!foo);
|
2011-04-22 12:01:45 -04:00
|
|
|
(void)foo;
|
2011-04-15 17:35:46 -04:00
|
|
|
PM->run(*unwrap(M));
|
|
|
|
}
|