Skip to content

Commit 798d09e

Browse files
committed
---
yaml --- r: 1661 b: refs/heads/master c: 736969f h: refs/heads/master i: 1659: 0596e40 v: v3
1 parent 9eea673 commit 798d09e

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

[refs]

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: bd9dd5ed1ab4565141c0c08b4cf2245e451a3eb1
2+
refs/heads/master: 736969f9fe49b17174c6e06fbb7b1a8331ca94b2

trunk/src/Makefile

+2-1
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

trunk/src/comp/lib/llvm.rs

+17-5
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. */

trunk/src/llvmext/RustWrapper.cpp

+31
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)