@@ -30,7 +30,7 @@ use rustc_metadata::EncodedMetadata;
3030use rustc_middle:: dep_graph:: { WorkProduct , WorkProductId } ;
3131use rustc_middle:: ty:: TyCtxt ;
3232use rustc_session:: { Session , config:: OutputFilenames } ;
33- use std:: { any:: Any , io:: Write , path:: Path , vec } ;
33+ use std:: { any:: Any , io:: Write , path:: Path } ;
3434
3535mod lower1;
3636mod lower2;
@@ -57,6 +57,7 @@ impl CodegenBackend for MyBackend {
5757 let mut oomir_module = oomir:: Module {
5858 name : crate_name. clone ( ) ,
5959 functions : std:: collections:: HashMap :: new ( ) ,
60+ data_types : std:: collections:: HashMap :: new ( ) ,
6061 } ;
6162
6263 // Iterate through all items in the crate and find functions
@@ -82,12 +83,16 @@ impl CodegenBackend for MyBackend {
8283 println ! ( "MIR for function {i}: {:?}" , mir) ;
8384
8485 println ! ( "--- Starting MIR to OOMIR Lowering for function: {i} ---" ) ;
85- let oomir_function = lower1:: mir_to_oomir ( tcx, instance, & mut mir) ;
86+ let oomir_result = lower1:: mir_to_oomir ( tcx, instance, & mut mir) ;
8687 println ! ( "--- Finished MIR to OOMIR Lowering for function: {i} ---" ) ;
8788
89+ let oomir_function = oomir_result. 0 ;
90+
8891 oomir_module
8992 . functions
9093 . insert ( oomir_function. name . clone ( ) , oomir_function) ;
94+
95+ oomir_module. merge_data_types ( & oomir_result. 1 ) ;
9196 }
9297 }
9398
@@ -119,30 +124,46 @@ impl CodegenBackend for MyBackend {
119124 outputs : & OutputFilenames ,
120125 ) -> ( CodegenResults , FxIndexMap < WorkProductId , WorkProduct > ) {
121126 std:: panic:: catch_unwind ( std:: panic:: AssertUnwindSafe ( || {
122- let ( bytecode, crate_name, metadata, crate_info) = * ongoing_codegen
123- . downcast :: < ( Vec < u8 > , String , EncodedMetadata , CrateInfo ) > ( )
124- . expect ( "in join_codegen: ongoing_codegen is not bytecode vector" ) ;
125-
126- let class_path = outputs. temp_path_ext ( "class" , None ) ;
127-
128- let mut class_file =
129- std:: fs:: File :: create ( & class_path) . expect ( "Could not create the Java .class file!" ) ;
130- class_file
131- . write_all ( & bytecode)
132- . expect ( "Could not write Java bytecode to file!" ) ;
133-
134- let modules = vec ! [ CompiledModule {
135- name: crate_name,
136- kind: ModuleKind :: Regular ,
137- object: Some ( class_path) ,
138- bytecode: None ,
139- dwarf_object: None ,
140- llvm_ir: None ,
141- links_from_incr_cache: Vec :: new( ) ,
142- assembly: None ,
143- } ] ;
127+ // Update the downcast to expect a HashMap now.
128+ let ( bytecode_map, _, metadata, crate_info) = * ongoing_codegen
129+ . downcast :: < (
130+ std:: collections:: HashMap < String , Vec < u8 > > ,
131+ String ,
132+ EncodedMetadata ,
133+ CrateInfo ,
134+ ) > ( )
135+ . expect ( "in join_codegen: ongoing_codegen is not a bytecode map" ) ;
136+
137+ let mut compiled_modules = Vec :: new ( ) ;
138+
139+ // Iterate over each (file_name, bytecode) pair in the map.
140+ for ( name, bytecode) in bytecode_map. into_iter ( ) {
141+ // The key is expected to be the file name without the ".class" extension.
142+ // Append ".class" here.
143+ let file_name = format ! ( "{}.class" , name) ;
144+ let file_path = outputs. temp_path_ext ( & file_name, None ) ;
145+
146+ // Write the bytecode to the file
147+ let mut file = std:: fs:: File :: create ( & file_path)
148+ . unwrap_or_else ( |e| panic ! ( "Could not create file {}: {}" , file_path. display( ) , e) ) ;
149+ file. write_all ( & bytecode)
150+ . unwrap_or_else ( |e| panic ! ( "Could not write bytecode to file {}: {}" , file_path. display( ) , e) ) ;
151+
152+ // Create a CompiledModule for this file
153+ compiled_modules. push ( CompiledModule {
154+ name : name. clone ( ) ,
155+ kind : ModuleKind :: Regular ,
156+ object : Some ( file_path) ,
157+ bytecode : None ,
158+ dwarf_object : None ,
159+ llvm_ir : None ,
160+ links_from_incr_cache : Vec :: new ( ) ,
161+ assembly : None ,
162+ } ) ;
163+ }
164+
144165 let codegen_results = CodegenResults {
145- modules,
166+ modules : compiled_modules ,
146167 allocator_module : None ,
147168 metadata_module : None ,
148169 metadata,
@@ -152,7 +173,7 @@ impl CodegenBackend for MyBackend {
152173 } ) )
153174 . expect ( "Could not join_codegen" )
154175 }
155-
176+
156177 fn link ( & self , sess : & Session , codegen_results : CodegenResults , outputs : & OutputFilenames ) {
157178 println ! ( "linking!" ) ;
158179 use rustc_codegen_ssa:: back:: link:: link_binary;
0 commit comments