@@ -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} ;
@@ -420,6 +422,69 @@ pub(crate) fn codegen_instance<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
420422 mir:: codegen_mir :: < Bx > ( cx, instance) ;
421423}
422424
425+ pub fn codegen_global_asm < ' tcx , Cx > ( cx : & mut Cx , item_id : ItemId )
426+ where
427+ Cx : LayoutOf < ' tcx , LayoutOfResult = TyAndLayout < ' tcx > > + AsmCodegenMethods < ' tcx > ,
428+ {
429+ let item = cx. tcx ( ) . hir ( ) . item ( item_id) ;
430+ if let rustc_hir:: ItemKind :: GlobalAsm ( asm) = item. kind {
431+ let operands: Vec < _ > = asm
432+ . operands
433+ . iter ( )
434+ . map ( |( op, op_sp) | match * op {
435+ rustc_hir:: InlineAsmOperand :: Const { ref anon_const } => {
436+ match cx. tcx ( ) . const_eval_poly ( anon_const. def_id . to_def_id ( ) ) {
437+ Ok ( const_value) => {
438+ let ty =
439+ cx. tcx ( ) . typeck_body ( anon_const. body ) . node_type ( anon_const. hir_id ) ;
440+ let string = common:: asm_const_to_str (
441+ cx. tcx ( ) ,
442+ * op_sp,
443+ const_value,
444+ cx. layout_of ( ty) ,
445+ ) ;
446+ GlobalAsmOperandRef :: Const { string }
447+ }
448+ Err ( ErrorHandled :: Reported { .. } ) => {
449+ // An error has already been reported and
450+ // compilation is guaranteed to fail if execution
451+ // hits this path. So an empty string instead of
452+ // a stringified constant value will suffice.
453+ GlobalAsmOperandRef :: Const { string : String :: new ( ) }
454+ }
455+ Err ( ErrorHandled :: TooGeneric ( _) ) => {
456+ span_bug ! ( * op_sp, "asm const cannot be resolved; too generic" )
457+ }
458+ }
459+ }
460+ rustc_hir:: InlineAsmOperand :: SymFn { ref anon_const } => {
461+ let ty = cx. tcx ( ) . typeck_body ( anon_const. body ) . node_type ( anon_const. hir_id ) ;
462+ let instance = match ty. kind ( ) {
463+ & ty:: FnDef ( def_id, args) => Instance :: new ( def_id, args) ,
464+ _ => span_bug ! ( * op_sp, "asm sym is not a function" ) ,
465+ } ;
466+
467+ GlobalAsmOperandRef :: SymFn { instance }
468+ }
469+ rustc_hir:: InlineAsmOperand :: SymStatic { path : _, def_id } => {
470+ GlobalAsmOperandRef :: SymStatic { def_id }
471+ }
472+ rustc_hir:: InlineAsmOperand :: In { .. }
473+ | rustc_hir:: InlineAsmOperand :: Out { .. }
474+ | rustc_hir:: InlineAsmOperand :: InOut { .. }
475+ | rustc_hir:: InlineAsmOperand :: SplitInOut { .. }
476+ | rustc_hir:: InlineAsmOperand :: Label { .. } => {
477+ span_bug ! ( * op_sp, "invalid operand type for global_asm!" )
478+ }
479+ } )
480+ . collect ( ) ;
481+
482+ cx. codegen_global_asm ( asm. template , & operands, asm. options , asm. line_spans ) ;
483+ } else {
484+ span_bug ! ( item. span, "Mismatch between hir::Item type and MonoItem type" )
485+ }
486+ }
487+
423488/// Creates the `main` function which will initialize the rust runtime and call
424489/// users main function.
425490pub fn maybe_create_entry_wrapper < ' a , ' tcx , Bx : BuilderMethods < ' a , ' tcx > > (
0 commit comments