2011-10-21 17:35:52 -07:00
|
|
|
//===- RustWrapper.cpp - Rust wrapper for core functions --------*- C++ -*-===
|
2011-03-15 14:57:26 -07: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 17:35:52 -07:00
|
|
|
//===----------------------------------------------------------------------===
|
2011-03-15 14:57:26 -07:00
|
|
|
//
|
|
|
|
// This file defines alternate interfaces to core functions that are more
|
|
|
|
// readily callable by Rust's FFI.
|
|
|
|
//
|
2011-10-21 17:35:52 -07:00
|
|
|
//===----------------------------------------------------------------------===
|
2011-03-15 14:57:26 -07:00
|
|
|
|
2011-11-25 22:00:43 -08:00
|
|
|
#include "llvm/LLVMContext.h"
|
2011-05-04 21:27:00 -07:00
|
|
|
#include "llvm/Linker.h"
|
2011-04-15 17:35:46 -04:00
|
|
|
#include "llvm/PassManager.h"
|
|
|
|
#include "llvm/ADT/Triple.h"
|
2011-11-25 22:00:43 -08:00
|
|
|
#include "llvm/Assembly/Parser.h"
|
2011-11-07 21:32:13 +08:00
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
2011-04-15 17:35:46 -04:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2011-05-10 16:10:08 -07:00
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-04-15 17:35:46 -04:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2011-08-28 20:58:10 +02:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2011-11-25 22:00:43 -08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2011-04-26 15:21:20 -07:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2011-05-06 07:59:33 -06:00
|
|
|
#include "llvm/Support/Host.h"
|
2012-07-25 12:06:03 -07:00
|
|
|
#include "llvm/Support/Debug.h"
|
2011-03-15 14:57:26 -07:00
|
|
|
#include "llvm-c/Core.h"
|
2011-05-05 11:34:15 -07:00
|
|
|
#include "llvm-c/BitReader.h"
|
2011-03-15 14:57:26 -07:00
|
|
|
#include "llvm-c/Object.h"
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2011-04-15 17:35:46 -04:00
|
|
|
using namespace llvm;
|
|
|
|
|
2011-05-04 21:27:00 -07:00
|
|
|
static const char *LLVMRustError;
|
2011-03-15 14:57:26 -07:00
|
|
|
|
|
|
|
extern "C" LLVMMemoryBufferRef
|
|
|
|
LLVMRustCreateMemoryBufferWithContentsOfFile(const char *Path) {
|
|
|
|
LLVMMemoryBufferRef MemBuf = NULL;
|
2011-05-04 21:27:00 -07:00
|
|
|
LLVMCreateMemoryBufferWithContentsOfFile(Path, &MemBuf,
|
|
|
|
const_cast<char **>(&LLVMRustError));
|
2011-03-15 14:57:26 -07:00
|
|
|
return MemBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" const char *LLVMRustGetLastError(void) {
|
|
|
|
return LLVMRustError;
|
|
|
|
}
|
|
|
|
|
2011-04-13 13:53:19 -04:00
|
|
|
extern "C" void LLVMAddBasicAliasAnalysisPass(LLVMPassManagerRef PM);
|
2011-04-18 10:02:34 -04:00
|
|
|
|
2011-11-07 21:32:13 +08: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));
|
|
|
|
}
|
|
|
|
|
2012-07-21 17:20:45 +10:00
|
|
|
void LLVMInitializeX86TargetInfo();
|
|
|
|
void LLVMInitializeX86Target();
|
|
|
|
void LLVMInitializeX86TargetMC();
|
|
|
|
void LLVMInitializeX86AsmPrinter();
|
|
|
|
void LLVMInitializeX86AsmParser();
|
|
|
|
|
2012-07-13 17:06:30 -04:00
|
|
|
extern "C" bool
|
2011-10-21 17:35:52 -07:00
|
|
|
LLVMRustWriteOutputFile(LLVMPassManagerRef PMR,
|
|
|
|
LLVMModuleRef M,
|
|
|
|
const char *triple,
|
|
|
|
const char *path,
|
|
|
|
TargetMachine::CodeGenFileType FileType,
|
2011-11-26 22:38:36 -08:00
|
|
|
CodeGenOpt::Level OptLevel,
|
|
|
|
bool EnableSegmentedStacks) {
|
2011-04-26 15:21:20 -07:00
|
|
|
|
2012-07-21 17:20:45 +10:00
|
|
|
// Only initialize the platforms supported by Rust here,
|
|
|
|
// because using --llvm-root will have multiple platforms
|
|
|
|
// that rustllvm doesn't actually link to and it's pointless to put target info
|
|
|
|
// into the registry that Rust can not generate machine code for.
|
|
|
|
|
|
|
|
LLVMInitializeX86TargetInfo();
|
|
|
|
LLVMInitializeX86Target();
|
|
|
|
LLVMInitializeX86TargetMC();
|
|
|
|
LLVMInitializeX86AsmPrinter();
|
|
|
|
LLVMInitializeX86AsmParser();
|
2011-11-26 22:38:36 -08:00
|
|
|
|
|
|
|
TargetOptions Options;
|
|
|
|
Options.NoFramePointerElim = true;
|
|
|
|
Options.EnableSegmentedStacks = EnableSegmentedStacks;
|
|
|
|
|
2011-04-15 17:35:46 -04:00
|
|
|
std::string Err;
|
|
|
|
const Target *TheTarget = TargetRegistry::lookupTarget(triple, Err);
|
|
|
|
std::string FeaturesStr;
|
2011-07-01 00:56:49 -06:00
|
|
|
std::string Trip(triple);
|
2012-04-30 15:40:04 -07:00
|
|
|
std::string CPUStr("generic");
|
2011-07-25 11:11:24 -04:00
|
|
|
TargetMachine *Target =
|
2011-11-26 22:38:36 -08:00
|
|
|
TheTarget->createTargetMachine(Trip, CPUStr, FeaturesStr,
|
|
|
|
Options, Reloc::PIC_,
|
|
|
|
CodeModel::Default, OptLevel);
|
2011-04-15 17:35:46 -04:00
|
|
|
bool NoVerify = false;
|
|
|
|
PassManager *PM = unwrap<PassManager>(PMR);
|
|
|
|
std::string ErrorInfo;
|
|
|
|
raw_fd_ostream OS(path, ErrorInfo,
|
|
|
|
raw_fd_ostream::F_Binary);
|
2012-07-13 17:06:30 -04:00
|
|
|
if (ErrorInfo != "") {
|
|
|
|
LLVMRustError = ErrorInfo.c_str();
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-15 17:35:46 -04:00
|
|
|
formatted_raw_ostream FOS(OS);
|
2011-04-18 10:02:34 -04:00
|
|
|
|
2011-11-26 22:38:36 -08:00
|
|
|
bool foo = Target->addPassesToEmitFile(*PM, FOS, FileType, 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));
|
2011-05-14 00:48:51 -04:00
|
|
|
delete Target;
|
2012-07-13 17:06:30 -04:00
|
|
|
return true;
|
2011-04-15 17:35:46 -04:00
|
|
|
}
|
2011-05-05 11:34:15 -07:00
|
|
|
|
2011-11-25 22:00:43 -08: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 11:34:15 -07:00
|
|
|
extern "C" LLVMModuleRef LLVMRustParseBitcode(LLVMMemoryBufferRef MemBuf) {
|
|
|
|
LLVMModuleRef M;
|
|
|
|
return LLVMParseBitcode(MemBuf, &M, const_cast<char **>(&LLVMRustError))
|
|
|
|
? NULL : M;
|
|
|
|
}
|
|
|
|
|
2011-05-07 18:54:23 +00:00
|
|
|
extern "C" LLVMValueRef LLVMRustConstSmallInt(LLVMTypeRef IntTy, unsigned N,
|
|
|
|
LLVMBool SignExtend) {
|
|
|
|
return LLVMConstInt(IntTy, (unsigned long long)N, SignExtend);
|
|
|
|
}
|
2011-05-10 16:10:08 -07:00
|
|
|
|
2011-11-16 12:15:54 -08: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 16:10:08 -07: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 14:42:17 -07:00
|
|
|
|
2011-11-15 00:32:31 +08:00
|
|
|
extern "C" LLVMValueRef LLVMGetOrInsertFunction(LLVMModuleRef M,
|
|
|
|
const char* Name,
|
|
|
|
LLVMTypeRef FunctionTy) {
|
|
|
|
return wrap(unwrap(M)->getOrInsertFunction(Name,
|
|
|
|
unwrap<FunctionType>(FunctionTy)));
|
|
|
|
}
|
2011-11-10 00:55:09 -05:00
|
|
|
|
|
|
|
extern "C" LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
|
|
|
|
return wrap(Type::getMetadataTy(*unwrap(C)));
|
|
|
|
}
|
|
|
|
extern "C" LLVMTypeRef LLVMMetadataType(void) {
|
|
|
|
return LLVMMetadataTypeInContext(LLVMGetGlobalContext());
|
|
|
|
}
|
2012-06-21 15:01:32 -07:00
|
|
|
|
|
|
|
extern "C" LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,
|
|
|
|
AtomicRMWInst::BinOp op,
|
|
|
|
LLVMValueRef target,
|
|
|
|
LLVMValueRef source,
|
|
|
|
AtomicOrdering order) {
|
|
|
|
return wrap(unwrap(B)->CreateAtomicRMW(op,
|
|
|
|
unwrap(target), unwrap(source),
|
|
|
|
order));
|
|
|
|
}
|
2012-07-25 12:06:03 -07:00
|
|
|
|
|
|
|
extern "C" void LLVMSetDebug(int Enabled) {
|
|
|
|
DebugFlag = Enabled;
|
|
|
|
}
|