Skip to content

Commit 736969f

Browse files
committed
rustc: Add support for LLVM memory buffer creation via a wrapper function
1 parent bd9dd5e commit 736969f

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

src/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ RUNTIME_HDR := rt/globals.h \
311311
RUNTIME_INCS := -Irt/isaac -Irt/uthash
312312
RUNTIME_OBJS := $(RUNTIME_CS:.cpp=.o)
313313

314-
SUPPORT_CS := llvmext/MachOObjectFile.cpp llvmext/Object.cpp
314+
SUPPORT_CS := $(addprefix llvmext/, \
315+
MachOObjectFile.cpp Object.cpp RustWrapper.cpp)
315316

316317
SUPPORT_HDR := llvmext/include/llvm-c/Object.h
317318

src/comp/lib/llvm.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -738,9 +738,7 @@ native mod llvm = llvm_lib {
738738
/** Adds a verification pass. */
739739
fn LLVMAddVerifierPass(PassManagerRef PM);
740740

741-
// TODO: LLVMCreateMemoryBufferWithContentsOfFile is unrepresentable. Make
742-
// a shim.
743-
/** Destroys the memory buffer. */
741+
/** Destroys a memory buffer. */
744742
fn LLVMDisposeMemoryBuffer(MemoryBufferRef MemBuf);
745743
}
746744

@@ -770,6 +768,15 @@ native mod llvmext = llvmext_lib {
770768
fn LLVMGetSectionSize(SectionIteratorRef SI) -> uint;
771769
/** Returns the current section contents as a string buffer. */
772770
fn LLVMGetSectionContents(SectionIteratorRef SI) -> sbuf;
771+
772+
/** Reads the given file and returns it as a memory buffer. Use
773+
LLVMDisposeMemoryBuffer() to get rid of it. */
774+
fn LLVMRustCreateMemoryBufferWithContentsOfFile(sbuf Path) ->
775+
MemoryBufferRef;
776+
777+
/** Returns a string describing the last error caused by an LLVMRust*
778+
call. */
779+
fn LLVMRustGetLastError() -> sbuf;
773780
}
774781

775782
/* Slightly more terse object-interface to LLVM's 'builder' functions. */
@@ -1382,8 +1389,13 @@ obj memory_buffer_dtor(MemoryBufferRef MemBuf) {
13821389

13831390
type memory_buffer = rec(MemoryBufferRef llmb, memory_buffer_dtor dtor);
13841391

1385-
fn mk_memory_buffer() -> memory_buffer {
1386-
fail; // TODO
1392+
fn mk_memory_buffer(sbuf path) -> memory_buffer {
1393+
auto llmb = llvmext.LLVMRustCreateMemoryBufferWithContentsOfFile(path);
1394+
if ((llmb as int) == 0) {
1395+
log "failed to create memory buffer";
1396+
fail;
1397+
}
1398+
ret rec(llmb=llmb, dtor=memory_buffer_dtor(llmb));
13871399
}
13881400

13891401
/* Memory-managed interface to object files. */

src/llvmext/RustWrapper.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===- RustWrapper.cpp - Rust wrapper for core functions --------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file defines alternate interfaces to core functions that are more
11+
// readily callable by Rust's FFI.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#include "llvm-c/Core.h"
16+
#include "llvm-c/Object.h"
17+
#include <cstdlib>
18+
19+
static char *LLVMRustError;
20+
21+
extern "C" LLVMMemoryBufferRef
22+
LLVMRustCreateMemoryBufferWithContentsOfFile(const char *Path) {
23+
LLVMMemoryBufferRef MemBuf = NULL;
24+
LLVMCreateMemoryBufferWithContentsOfFile(Path, &MemBuf, &LLVMRustError);
25+
return MemBuf;
26+
}
27+
28+
extern "C" const char *LLVMRustGetLastError(void) {
29+
return LLVMRustError;
30+
}
31+

0 commit comments

Comments
 (0)