Skip to content

Commit 59c6bdf

Browse files
committed
add prelude.bc and code in llvm_interpreter to load the prelude from the bitcode
1 parent a28f735 commit 59c6bdf

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

prelude.bc

2.29 KB
Binary file not shown.

src/llvm_interpreter.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use llvm_sys::prelude::*;
1414
use llvm_sys::analysis::*;
1515
use llvm_sys::target::*;
1616
use llvm_sys::target_machine::*;
17+
use std::os::raw::c_char;
18+
use llvm_sys::bit_reader::*;
1719
use llvm_sys::execution_engine::*;
1820

1921
use std::os::raw::{c_ulonglong, c_uint};
@@ -26,6 +28,12 @@ use value::{Value, Number};
2628
use libc;
2729

2830

31+
fn char_ptr_to_string(char_ptr: *const c_char) -> String {
32+
unsafe {
33+
CStr::from_ptr(char_ptr as *const _).to_str().expect("expected correct char* to str").to_owned()
34+
}
35+
}
36+
2937
const LLVM_FALSE: LLVMBool = 0;
3038
const LLVM_TRUE: LLVMBool = 1;
3139

@@ -960,10 +968,32 @@ fn create_module(module_name: &str, target_triple: Option<String>) -> Module {
960968
module
961969
}
962970

971+
972+
fn load_llvm_prelude_from_file(path: &str) -> Module {
973+
unsafe {
974+
let mut out : LLVMModuleRef = mem::uninitialized();
975+
976+
let mut out_message_raw : *mut c_char = mem::uninitialized();
977+
let path_cstr : CString = CString::new(path).unwrap();
978+
let mut membuf : LLVMMemoryBufferRef = mem::uninitialized();
979+
if 0 != LLVMCreateMemoryBufferWithContentsOfFile(path_cstr.as_ptr(), &mut membuf, &mut out_message_raw) {
980+
panic!("unable to read memory buffer from file: {}", char_ptr_to_string(out_message_raw))
981+
}
982+
983+
984+
if 0 != LLVMParseBitcode2(membuf, &mut out) {
985+
panic!("unable to load module")
986+
}
987+
Module {module: out, strings: Vec::new()}
988+
989+
}
990+
}
991+
963992
fn interpret_statements(stmts: &[StmtNode],
964993
_: Rc<RefCell<Environment>>)
965994
-> Result<Option<StmtResult>, RuntimeErrorWithPosition> {
966995
let target_triple: Option<String> = None;
996+
/*
967997
let mut module = create_module("ModuleName", target_triple);
968998
969999
let c_declarations = gen_c_declarations(&mut module);
@@ -974,6 +1004,11 @@ fn interpret_statements(stmts: &[StmtNode],
9741004
9751005
print!("@@@@@@Module after all prelude generation:\n{:?}\n----",
9761006
module);
1007+
*/
1008+
1009+
let other_module = load_llvm_prelude_from_file("prelude.bc");
1010+
println!("####@@@###@@@####@@@###\nother module: {:?}", other_module);
1011+
/*
9771012
9781013
unsafe {
9791014
@@ -1025,6 +1060,7 @@ fn interpret_statements(stmts: &[StmtNode],
10251060
jit.add_module(module, main_fn, c_declarations);
10261061
10271062
};
1063+
*/
10281064

10291065

10301066

0 commit comments

Comments
 (0)