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