rustc: Fall back to intrinsics.ll if we can't parse the bc

This will allow us to transition to the new bitcode format.
This commit is contained in:
Brian Anderson 2011-11-25 22:00:43 -08:00
parent 143f87899b
commit 9a188b2e94
4 changed files with 61 additions and 4 deletions

View File

@ -37,16 +37,19 @@ fn llvm_err(sess: session::session, msg: str) unsafe {
} else { sess.fatal(msg + ": " + str::str_from_cstr(buf)); }
}
fn link_intrinsics(sess: session::session, llmod: ModuleRef) {
fn load_intrinsics_bc(sess: session::session) -> option::t<ModuleRef> {
let path = alt filesearch::search(
sess.filesearch(),
bind filesearch::pick_file("intrinsics.bc", _)) {
option::some(path) { path }
option::none. { sess.fatal("couldn't find intrinsics.bc") }
option::none. {
sess.warn("couldn't find intrinsics.bc");
ret option::none;
}
};
let membuf = str::as_buf(path, {|buf|
llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
});
});
if membuf as uint == 0u {
llvm_err(sess, "installation problem: couldn't open " + path);
fail;
@ -54,9 +57,44 @@ fn link_intrinsics(sess: session::session, llmod: ModuleRef) {
let llintrinsicsmod = llvm::LLVMRustParseBitcode(membuf);
llvm::LLVMDisposeMemoryBuffer(membuf);
if llintrinsicsmod as uint == 0u {
llvm_err(sess, "installation problem: couldn't parse intrinsics.bc");
sess.warn("couldn't parse intrinsics.bc");
ret option::none;
}
ret option::some(llintrinsicsmod);
}
fn load_intrinsics_ll(sess: session::session) -> ModuleRef {
let path = alt filesearch::search(
sess.filesearch(),
bind filesearch::pick_file("intrinsics.ll", _)) {
option::some(path) { path }
option::none. { sess.fatal("couldn't find intrinsics.ll") }
};
let llintrinsicsmod = str::as_buf(path, { |buf|
llvm::LLVMRustParseAssemblyFile(buf)
});
if llintrinsicsmod as uint == 0u {
llvm_err(sess, "couldn't parse intrinsics.ll");
fail;
}
ret llintrinsicsmod;
}
fn link_intrinsics(sess: session::session, llmod: ModuleRef) {
let llintrinsicsmod = {
alt load_intrinsics_bc(sess) {
option::some(m) { m }
option::none. {
// When the bitcode format changes we can't parse a .bc
// file produced with a newer LLVM (as happens when stage0
// is trying to build against a new LLVM revision), in
// that case we'll try to parse the assembly.
sess.warn("couldn't parse intrinsics.bc, trying intrinsics.ll");
load_intrinsics_ll(sess)
}
}
};
let linkres = llvm::LLVMLinkModules(llmod, llintrinsicsmod);
llvm::LLVMDisposeModule(llintrinsicsmod);
if linkres == False {

View File

@ -864,6 +864,9 @@ native mod llvm {
/** Parses the bitcode in the given memory buffer. */
fn LLVMRustParseBitcode(MemBuf: MemoryBufferRef) -> ModuleRef;
/** Parses LLVM asm in the given file */
fn LLVMRustParseAssemblyFile(Filename: sbuf) -> ModuleRef;
/** FiXME: Hacky adaptor for lack of ULongLong in FFI: */
fn LLVMRustConstInt(IntTy: TypeRef, N_hi: uint, N_lo: uint,
SignExtend: Bool) -> ValueRef;

View File

@ -12,9 +12,11 @@
//
//===----------------------------------------------------------------------===
#include "llvm/LLVMContext.h"
#include "llvm/Linker.h"
#include "llvm/PassManager.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Assembly/Parser.h"
#include "llvm/Assembly/PrintModulePass.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/Timer.h"
@ -22,6 +24,7 @@
#include "llvm/Target/TargetMachine.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Support/Host.h"
#include "llvm-c/Core.h"
@ -109,6 +112,18 @@ LLVMRustWriteOutputFile(LLVMPassManagerRef PMR,
delete Target;
}
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;
}
}
extern "C" LLVMModuleRef LLVMRustParseBitcode(LLVMMemoryBufferRef MemBuf) {
LLVMModuleRef M;
return LLVMParseBitcode(MemBuf, &M, const_cast<char **>(&LLVMRustError))

View File

@ -6,6 +6,7 @@ LLVMRustGetHostTriple
LLVMRustConstSmallInt
LLVMRustConstInt
LLVMRustParseBitcode
LLVMRustParseAssemblyFile
LLVMRustPrintPassTimings
LLVMRustEnableSegmentedStacks
LLVMLinkModules