@@ -9,19 +9,21 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
99use rustc_data_structures:: profiling:: { get_resident_set_size, print_time_passes_entry} ;
1010use rustc_data_structures:: sync:: { Lrc , par_map} ;
1111use rustc_data_structures:: unord:: UnordMap ;
12+ use rustc_hir:: ItemId ;
1213use rustc_hir:: def_id:: { DefId , LOCAL_CRATE } ;
1314use rustc_hir:: lang_items:: LangItem ;
1415use rustc_metadata:: EncodedMetadata ;
15- use rustc_middle:: bug;
1616use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrs ;
1717use rustc_middle:: middle:: debugger_visualizer:: { DebuggerVisualizerFile , DebuggerVisualizerType } ;
1818use rustc_middle:: middle:: exported_symbols:: SymbolExportKind ;
1919use rustc_middle:: middle:: { exported_symbols, lang_items} ;
2020use rustc_middle:: mir:: BinOp ;
21+ use rustc_middle:: mir:: interpret:: ErrorHandled ;
2122use rustc_middle:: mir:: mono:: { CodegenUnit , CodegenUnitNameBuilder , MonoItem } ;
2223use rustc_middle:: query:: Providers ;
2324use rustc_middle:: ty:: layout:: { HasTyCtxt , HasTypingEnv , LayoutOf , TyAndLayout } ;
2425use rustc_middle:: ty:: { self , Instance , Ty , TyCtxt } ;
26+ use rustc_middle:: { bug, span_bug} ;
2527use rustc_session:: Session ;
2628use rustc_session:: config:: { self , CrateType , EntryFnType , OptLevel , OutputType } ;
2729use rustc_span:: { DUMMY_SP , Symbol , sym} ;
@@ -419,6 +421,69 @@ pub(crate) fn codegen_instance<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
419421 mir:: codegen_mir :: < Bx > ( cx, instance) ;
420422}
421423
424+ pub fn codegen_global_asm < ' tcx , Cx > ( cx : & mut Cx , item_id : ItemId )
425+ where
426+ Cx : LayoutOf < ' tcx , LayoutOfResult = TyAndLayout < ' tcx > > + AsmCodegenMethods < ' tcx > ,
427+ {
428+ let item = cx. tcx ( ) . hir ( ) . item ( item_id) ;
429+ if let rustc_hir:: ItemKind :: GlobalAsm ( asm) = item. kind {
430+ let operands: Vec < _ > = asm
431+ . operands
432+ . iter ( )
433+ . map ( |( op, op_sp) | match * op {
434+ rustc_hir:: InlineAsmOperand :: Const { ref anon_const } => {
435+ match cx. tcx ( ) . const_eval_poly ( anon_const. def_id . to_def_id ( ) ) {
436+ Ok ( const_value) => {
437+ let ty =
438+ cx. tcx ( ) . typeck_body ( anon_const. body ) . node_type ( anon_const. hir_id ) ;
439+ let string = common:: asm_const_to_str (
440+ cx. tcx ( ) ,
441+ * op_sp,
442+ const_value,
443+ cx. layout_of ( ty) ,
444+ ) ;
445+ GlobalAsmOperandRef :: Const { string }
446+ }
447+ Err ( ErrorHandled :: Reported { .. } ) => {
448+ // An error has already been reported and
449+ // compilation is guaranteed to fail if execution
450+ // hits this path. So an empty string instead of
451+ // a stringified constant value will suffice.
452+ GlobalAsmOperandRef :: Const { string : String :: new ( ) }
453+ }
454+ Err ( ErrorHandled :: TooGeneric ( _) ) => {
455+ span_bug ! ( * op_sp, "asm const cannot be resolved; too generic" )
456+ }
457+ }
458+ }
459+ rustc_hir:: InlineAsmOperand :: SymFn { ref anon_const } => {
460+ let ty = cx. tcx ( ) . typeck_body ( anon_const. body ) . node_type ( anon_const. hir_id ) ;
461+ let instance = match ty. kind ( ) {
462+ & ty:: FnDef ( def_id, args) => Instance :: new ( def_id, args) ,
463+ _ => span_bug ! ( * op_sp, "asm sym is not a function" ) ,
464+ } ;
465+
466+ GlobalAsmOperandRef :: SymFn { instance }
467+ }
468+ rustc_hir:: InlineAsmOperand :: SymStatic { path : _, def_id } => {
469+ GlobalAsmOperandRef :: SymStatic { def_id }
470+ }
471+ rustc_hir:: InlineAsmOperand :: In { .. }
472+ | rustc_hir:: InlineAsmOperand :: Out { .. }
473+ | rustc_hir:: InlineAsmOperand :: InOut { .. }
474+ | rustc_hir:: InlineAsmOperand :: SplitInOut { .. }
475+ | rustc_hir:: InlineAsmOperand :: Label { .. } => {
476+ span_bug ! ( * op_sp, "invalid operand type for global_asm!" )
477+ }
478+ } )
479+ . collect ( ) ;
480+
481+ cx. codegen_global_asm ( asm. template , & operands, asm. options , asm. line_spans ) ;
482+ } else {
483+ span_bug ! ( item. span, "Mismatch between hir::Item type and MonoItem type" )
484+ }
485+ }
486+
422487/// Creates the `main` function which will initialize the rust runtime and call
423488/// users main function.
424489pub fn maybe_create_entry_wrapper < ' a , ' tcx , Bx : BuilderMethods < ' a , ' tcx > > (
0 commit comments