@@ -72,30 +72,46 @@ impl Module {
7272
7373 /// Get the `GlobalAlias` having the given `Name` (if any).
7474 pub fn get_global_alias_by_name ( & self , name : & Name ) -> Option < & GlobalAlias > {
75- self . global_aliases . iter ( ) . find ( |global| global. name == * name)
75+ self . global_aliases
76+ . iter ( )
77+ . find ( |global| global. name == * name)
7678 }
7779
7880 /// Get the `GlobalIFunc` having the given `Name` (if any).
7981 pub fn get_global_ifunc_by_name ( & self , name : & Name ) -> Option < & GlobalIFunc > {
80- self . global_ifuncs . iter ( ) . find ( |global| global. name == * name)
82+ self . global_ifuncs
83+ . iter ( )
84+ . find ( |global| global. name == * name)
8185 }
8286
8387 /// Parse the LLVM bitcode (.bc) file at the given path to create a `Module`
8488 pub fn from_bc_path ( path : impl AsRef < Path > ) -> Result < Self , String > {
85- unsafe fn parse_bc (
86- context_ref : LLVMContextRef ,
87- mem_buf : LLVMMemoryBufferRef ,
88- out_module : * mut LLVMModuleRef ,
89- ) -> Result < ( ) , String > {
90- let result =
91- llvm_sys:: bit_reader:: LLVMParseBitcodeInContext2 ( context_ref, mem_buf, out_module) ;
92- LLVMDisposeMemoryBuffer ( mem_buf) ;
93- match result {
94- 0 => Ok ( ( ) ) ,
95- _ => Err ( "Failed to parse bitcode" . to_owned ( ) )
96- }
89+ Self :: from_path ( path, Self :: parse_bc)
90+ }
91+
92+ pub fn from_bc_bytes ( bytes : & [ u8 ] ) -> Result < Self , String > {
93+ let memory_buffer = unsafe {
94+ LLVMCreateMemoryBufferWithMemoryRangeCopy (
95+ bytes. as_ptr ( ) as * const _ ,
96+ bytes. len ( ) ,
97+ std:: ffi:: CString :: default ( ) . as_ptr ( ) ,
98+ )
99+ } ;
100+ Self :: from_buffer ( memory_buffer, Self :: parse_bc)
101+ }
102+
103+ unsafe fn parse_bc (
104+ context_ref : LLVMContextRef ,
105+ mem_buf : LLVMMemoryBufferRef ,
106+ out_module : * mut LLVMModuleRef ,
107+ ) -> Result < ( ) , String > {
108+ let result =
109+ llvm_sys:: bit_reader:: LLVMParseBitcodeInContext2 ( context_ref, mem_buf, out_module) ;
110+ LLVMDisposeMemoryBuffer ( mem_buf) ;
111+ match result {
112+ 0 => Ok ( ( ) ) ,
113+ _ => Err ( "Failed to parse bitcode" . to_owned ( ) ) ,
97114 }
98- Self :: from_path ( path, parse_bc)
99115 }
100116
101117 /// Parse the LLVM text IR (.ll) file at the given path to create a `Module`
@@ -123,10 +139,19 @@ impl Module {
123139 use std:: ffi:: CStr ;
124140 let mut err_string = std:: mem:: zeroed ( ) ;
125141 // This call takes ownership of the buffer, so we don't free it.
126- match llvm_sys:: ir_reader:: LLVMParseIRInContext ( context_ref, mem_buf, out_module, & mut err_string) {
142+ match llvm_sys:: ir_reader:: LLVMParseIRInContext (
143+ context_ref,
144+ mem_buf,
145+ out_module,
146+ & mut err_string,
147+ ) {
127148 0 => Ok ( ( ) ) ,
128- _ => Err ( format ! ( "Failed to parse IR: {}" ,
129- CStr :: from_ptr( err_string) . to_str( ) . expect( "Failed to convert CStr" ) ) )
149+ _ => Err ( format ! (
150+ "Failed to parse IR: {}" ,
151+ CStr :: from_ptr( err_string)
152+ . to_str( )
153+ . expect( "Failed to convert CStr" )
154+ ) ) ,
130155 }
131156 }
132157
@@ -1059,8 +1084,7 @@ impl DataLayout {
10591084 independent : true ,
10601085 abi,
10611086 } ;
1062- data_layout. alignments . fptr_alignment_as_alignment =
1063- Alignment { abi, pref : abi } ;
1087+ data_layout. alignments . fptr_alignment_as_alignment = Alignment { abi, pref : abi } ;
10641088 } else if let Some ( stripped) = spec. strip_prefix ( "Fn" ) {
10651089 let abi: u32 = stripped
10661090 . parse ( )
@@ -1069,8 +1093,7 @@ impl DataLayout {
10691093 independent : false ,
10701094 abi,
10711095 } ;
1072- data_layout. alignments . fptr_alignment_as_alignment =
1073- Alignment { abi, pref : abi } ;
1096+ data_layout. alignments . fptr_alignment_as_alignment = Alignment { abi, pref : abi } ;
10741097 } else if spec. starts_with ( 'm' ) {
10751098 let mut chunks = spec. split ( ':' ) ;
10761099 let first_chunk = chunks. next ( ) . unwrap ( ) ;
0 commit comments