@@ -14,6 +14,8 @@ use llvm_sys::prelude::*;
14
14
use llvm_sys:: analysis:: * ;
15
15
use llvm_sys:: target:: * ;
16
16
use llvm_sys:: target_machine:: * ;
17
+ use std:: os:: raw:: c_char;
18
+ use llvm_sys:: bit_reader:: * ;
17
19
use llvm_sys:: execution_engine:: * ;
18
20
19
21
use std:: os:: raw:: { c_ulonglong, c_uint} ;
@@ -26,6 +28,12 @@ use value::{Value, Number};
26
28
use libc;
27
29
28
30
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
+
29
37
const LLVM_FALSE : LLVMBool = 0 ;
30
38
const LLVM_TRUE : LLVMBool = 1 ;
31
39
@@ -960,10 +968,32 @@ fn create_module(module_name: &str, target_triple: Option<String>) -> Module {
960
968
module
961
969
}
962
970
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
+
963
992
fn interpret_statements ( stmts : & [ StmtNode ] ,
964
993
_: Rc < RefCell < Environment > > )
965
994
-> Result < Option < StmtResult > , RuntimeErrorWithPosition > {
966
995
let target_triple: Option < String > = None ;
996
+ /*
967
997
let mut module = create_module("ModuleName", target_triple);
968
998
969
999
let c_declarations = gen_c_declarations(&mut module);
@@ -974,6 +1004,11 @@ fn interpret_statements(stmts: &[StmtNode],
974
1004
975
1005
print!("@@@@@@Module after all prelude generation:\n{:?}\n----",
976
1006
module);
1007
+ */
1008
+
1009
+ let other_module = load_llvm_prelude_from_file ( "prelude.bc" ) ;
1010
+ println ! ( "####@@@###@@@####@@@###\n other module: {:?}" , other_module) ;
1011
+ /*
977
1012
978
1013
unsafe {
979
1014
@@ -1025,6 +1060,7 @@ fn interpret_statements(stmts: &[StmtNode],
1025
1060
jit.add_module(module, main_fn, c_declarations);
1026
1061
1027
1062
};
1063
+ */
1028
1064
1029
1065
1030
1066
0 commit comments