@@ -30,7 +30,7 @@ use rustc_metadata::EncodedMetadata;
30
30
use rustc_middle:: dep_graph:: { WorkProduct , WorkProductId } ;
31
31
use rustc_middle:: ty:: TyCtxt ;
32
32
use rustc_session:: { Session , config:: OutputFilenames } ;
33
- use std:: { any:: Any , io:: Write , path:: Path , vec } ;
33
+ use std:: { any:: Any , io:: Write , path:: Path } ;
34
34
35
35
mod lower1;
36
36
mod lower2;
@@ -57,6 +57,7 @@ impl CodegenBackend for MyBackend {
57
57
let mut oomir_module = oomir:: Module {
58
58
name : crate_name. clone ( ) ,
59
59
functions : std:: collections:: HashMap :: new ( ) ,
60
+ data_types : std:: collections:: HashMap :: new ( ) ,
60
61
} ;
61
62
62
63
// Iterate through all items in the crate and find functions
@@ -82,12 +83,16 @@ impl CodegenBackend for MyBackend {
82
83
println ! ( "MIR for function {i}: {:?}" , mir) ;
83
84
84
85
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) ;
86
87
println ! ( "--- Finished MIR to OOMIR Lowering for function: {i} ---" ) ;
87
88
89
+ let oomir_function = oomir_result. 0 ;
90
+
88
91
oomir_module
89
92
. functions
90
93
. insert ( oomir_function. name . clone ( ) , oomir_function) ;
94
+
95
+ oomir_module. merge_data_types ( & oomir_result. 1 ) ;
91
96
}
92
97
}
93
98
@@ -119,30 +124,46 @@ impl CodegenBackend for MyBackend {
119
124
outputs : & OutputFilenames ,
120
125
) -> ( CodegenResults , FxIndexMap < WorkProductId , WorkProduct > ) {
121
126
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
+
144
165
let codegen_results = CodegenResults {
145
- modules,
166
+ modules : compiled_modules ,
146
167
allocator_module : None ,
147
168
metadata_module : None ,
148
169
metadata,
@@ -152,7 +173,7 @@ impl CodegenBackend for MyBackend {
152
173
} ) )
153
174
. expect ( "Could not join_codegen" )
154
175
}
155
-
176
+
156
177
fn link ( & self , sess : & Session , codegen_results : CodegenResults , outputs : & OutputFilenames ) {
157
178
println ! ( "linking!" ) ;
158
179
use rustc_codegen_ssa:: back:: link:: link_binary;
0 commit comments