2011-10-21 19:35:52 -05:00
|
|
|
//===- RustWrapper.cpp - Rust wrapper for core functions --------*- C++ -*-===
|
2011-03-15 16:57:26 -05:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
2011-10-21 19:35:52 -05:00
|
|
|
//===----------------------------------------------------------------------===
|
2011-03-15 16:57:26 -05:00
|
|
|
//
|
|
|
|
// This file defines alternate interfaces to core functions that are more
|
|
|
|
// readily callable by Rust's FFI.
|
|
|
|
//
|
2011-10-21 19:35:52 -05:00
|
|
|
//===----------------------------------------------------------------------===
|
2011-03-15 16:57:26 -05:00
|
|
|
|
2011-11-26 00:00:43 -06:00
|
|
|
#include "llvm/LLVMContext.h"
|
2011-05-04 23:27:00 -05:00
|
|
|
#include "llvm/Linker.h"
|
2011-04-15 16:35:46 -05:00
|
|
|
#include "llvm/PassManager.h"
|
|
|
|
#include "llvm/ADT/Triple.h"
|
2011-11-26 00:00:43 -06:00
|
|
|
#include "llvm/Assembly/Parser.h"
|
2011-11-07 07:32:13 -06:00
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
2011-04-15 16:35:46 -05:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2011-05-10 18:10:08 -05:00
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-04-15 16:35:46 -05:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2011-08-28 13:58:10 -05:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2011-11-26 00:00:43 -06:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2011-04-26 17:21:20 -05:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2011-05-06 08:59:33 -05:00
|
|
|
#include "llvm/Support/Host.h"
|
2011-03-15 16:57:26 -05:00
|
|
|
#include "llvm-c/Core.h"
|
2011-05-05 13:34:15 -05:00
|
|
|
#include "llvm-c/BitReader.h"
|
2011-03-15 16:57:26 -05:00
|
|
|
#include "llvm-c/Object.h"
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2011-04-15 16:35:46 -05:00
|
|
|
using namespace llvm;
|
|
|
|
|
2011-05-04 23:27:00 -05:00
|
|
|
static const char *LLVMRustError;
|
2011-03-15 16:57:26 -05:00
|
|
|
|
|
|
|
extern "C" LLVMMemoryBufferRef
|
|
|
|
LLVMRustCreateMemoryBufferWithContentsOfFile(const char *Path) {
|
|
|
|
LLVMMemoryBufferRef MemBuf = NULL;
|
2011-05-04 23:27:00 -05:00
|
|
|
LLVMCreateMemoryBufferWithContentsOfFile(Path, &MemBuf,
|
|
|
|
const_cast<char **>(&LLVMRustError));
|
2011-03-15 16:57:26 -05:00
|
|
|
return MemBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" const char *LLVMRustGetLastError(void) {
|
|
|
|
return LLVMRustError;
|
|
|
|
}
|
|
|
|
|
2011-04-13 12:53:19 -05:00
|
|
|
extern "C" void LLVMAddBasicAliasAnalysisPass(LLVMPassManagerRef PM);
|
2011-04-18 09:02:34 -05:00
|
|
|
|
2011-11-07 07:32:13 -06:00
|
|
|
extern "C" void LLVMRustAddPrintModulePass(LLVMPassManagerRef PMR,
|
|
|
|
LLVMModuleRef M,
|
|
|
|
const char* path) {
|
|
|
|
PassManager *PM = unwrap<PassManager>(PMR);
|
|
|
|
std::string ErrorInfo;
|
|
|
|
raw_fd_ostream OS(path, ErrorInfo, raw_fd_ostream::F_Binary);
|
|
|
|
formatted_raw_ostream FOS(OS);
|
|
|
|
PM->add(createPrintModulePass(&FOS));
|
|
|
|
PM->run(*unwrap(M));
|
|
|
|
}
|
|
|
|
|
2011-05-04 23:27:00 -05:00
|
|
|
extern "C" bool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src) {
|
|
|
|
static std::string err;
|
|
|
|
|
|
|
|
// For some strange reason, unwrap() doesn't work here. "No matching
|
|
|
|
// function" error.
|
|
|
|
Module *DM = reinterpret_cast<Module *>(Dest);
|
|
|
|
Module *SM = reinterpret_cast<Module *>(Src);
|
2011-10-11 20:32:34 -05:00
|
|
|
if (Linker::LinkModules(DM, SM, Linker::DestroySource, &err)) {
|
2011-05-04 23:27:00 -05:00
|
|
|
LLVMRustError = err.c_str();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-21 19:35:52 -05:00
|
|
|
extern "C" void
|
|
|
|
LLVMRustWriteOutputFile(LLVMPassManagerRef PMR,
|
|
|
|
LLVMModuleRef M,
|
|
|
|
const char *triple,
|
|
|
|
const char *path,
|
|
|
|
TargetMachine::CodeGenFileType FileType,
|
|
|
|
CodeGenOpt::Level OptLevel) {
|
2011-04-26 17:21:20 -05:00
|
|
|
|
|
|
|
// Set compilation options.
|
2011-05-02 13:01:28 -05:00
|
|
|
llvm::NoFramePointerElim = true;
|
2011-04-26 17:21:20 -05:00
|
|
|
|
2011-04-15 16:35:46 -05:00
|
|
|
InitializeAllTargets();
|
2011-07-25 10:11:24 -05:00
|
|
|
InitializeAllTargetMCs();
|
2011-04-15 16:35:46 -05:00
|
|
|
InitializeAllAsmPrinters();
|
2011-04-19 16:07:30 -05:00
|
|
|
InitializeAllAsmParsers();
|
2011-04-15 16:35:46 -05:00
|
|
|
std::string Err;
|
|
|
|
const Target *TheTarget = TargetRegistry::lookupTarget(triple, Err);
|
|
|
|
std::string FeaturesStr;
|
2011-07-01 01:56:49 -05:00
|
|
|
std::string Trip(triple);
|
|
|
|
std::string CPUStr = llvm::sys::getHostCPUName();
|
2011-07-25 10:11:24 -05:00
|
|
|
TargetMachine *Target =
|
|
|
|
TheTarget->createTargetMachine(Trip, CPUStr, FeaturesStr, Reloc::PIC_);
|
2011-04-15 16:35:46 -05:00
|
|
|
bool NoVerify = false;
|
|
|
|
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 09:02:34 -05:00
|
|
|
|
2011-06-22 15:47:13 -05:00
|
|
|
bool foo = Target->addPassesToEmitFile(*PM, FOS, FileType, OptLevel,
|
|
|
|
NoVerify);
|
2011-04-15 16:35:46 -05:00
|
|
|
assert(!foo);
|
2011-04-22 11:01:45 -05:00
|
|
|
(void)foo;
|
2011-04-15 16:35:46 -05:00
|
|
|
PM->run(*unwrap(M));
|
2011-05-13 23:48:51 -05:00
|
|
|
delete Target;
|
2011-04-15 16:35:46 -05:00
|
|
|
}
|
2011-05-05 13:34:15 -05:00
|
|
|
|
2011-11-26 00:00:43 -06:00
|
|
|
extern "C" LLVMModuleRef LLVMRustParseAssemblyFile(const char *Filename) {
|
|
|
|
|
|
|
|
SMDiagnostic d;
|
|
|
|
Module *m = ParseAssemblyFile(Filename, d, getGlobalContext());
|
|
|
|
if (m) {
|
|
|
|
return wrap(m);
|
|
|
|
} else {
|
|
|
|
LLVMRustError = d.getMessage().c_str();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-05 13:34:15 -05:00
|
|
|
extern "C" LLVMModuleRef LLVMRustParseBitcode(LLVMMemoryBufferRef MemBuf) {
|
|
|
|
LLVMModuleRef M;
|
|
|
|
return LLVMParseBitcode(MemBuf, &M, const_cast<char **>(&LLVMRustError))
|
|
|
|
? NULL : M;
|
|
|
|
}
|
|
|
|
|
2011-05-06 08:59:33 -05:00
|
|
|
extern "C" const char *LLVMRustGetHostTriple(void)
|
|
|
|
{
|
|
|
|
static std::string str = llvm::sys::getHostTriple();
|
|
|
|
return str.c_str();
|
|
|
|
}
|
2011-05-07 13:54:23 -05:00
|
|
|
|
|
|
|
extern "C" LLVMValueRef LLVMRustConstSmallInt(LLVMTypeRef IntTy, unsigned N,
|
|
|
|
LLVMBool SignExtend) {
|
|
|
|
return LLVMConstInt(IntTy, (unsigned long long)N, SignExtend);
|
|
|
|
}
|
2011-05-10 18:10:08 -05:00
|
|
|
|
2011-11-16 14:15:54 -06:00
|
|
|
extern "C" LLVMValueRef LLVMRustConstInt(LLVMTypeRef IntTy,
|
|
|
|
unsigned N_hi,
|
|
|
|
unsigned N_lo,
|
|
|
|
LLVMBool SignExtend) {
|
|
|
|
unsigned long long N = N_hi;
|
|
|
|
N <<= 32;
|
|
|
|
N |= N_lo;
|
|
|
|
return LLVMConstInt(IntTy, N, SignExtend);
|
|
|
|
}
|
|
|
|
|
2011-05-10 18:10:08 -05:00
|
|
|
extern bool llvm::TimePassesIsEnabled;
|
|
|
|
extern "C" void LLVMRustEnableTimePasses() {
|
|
|
|
TimePassesIsEnabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void LLVMRustPrintPassTimings() {
|
|
|
|
raw_fd_ostream OS (2, false); // stderr.
|
|
|
|
TimerGroup::printAll(OS);
|
|
|
|
}
|
2011-10-31 16:42:17 -05:00
|
|
|
|
|
|
|
extern bool llvm::EnableSegmentedStacks;
|
|
|
|
extern "C" void LLVMRustEnableSegmentedStacks() {
|
|
|
|
EnableSegmentedStacks = true;
|
|
|
|
}
|
|
|
|
|
2011-11-14 10:32:31 -06:00
|
|
|
extern "C" LLVMValueRef LLVMGetOrInsertFunction(LLVMModuleRef M,
|
|
|
|
const char* Name,
|
|
|
|
LLVMTypeRef FunctionTy) {
|
|
|
|
return wrap(unwrap(M)->getOrInsertFunction(Name,
|
|
|
|
unwrap<FunctionType>(FunctionTy)));
|
|
|
|
}
|