@@ -11,6 +11,7 @@ use rustc_codegen_ssa::traits::{
1111 GlobalAsmOperandRef , InlineAsmOperandRef ,
1212} ;
1313use rustc_middle:: bug;
14+ use rustc_middle:: mir:: interpret:: { GlobalAlloc , Scalar } ;
1415use rustc_middle:: ty:: Instance ;
1516use rustc_middle:: ty:: layout:: LayoutOf ;
1617use rustc_span:: Span ;
@@ -309,12 +310,6 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
309310 constants_len += 20 ;
310311 }
311312
312- InlineAsmOperandRef :: SymFn { instance } => {
313- // TODO(@Amanieu): Additional mangling is needed on
314- // some targets to add a leading underscore (Mach-O)
315- // or byte count suffixes (x86 Windows).
316- constants_len += self . tcx . symbol_name ( instance) . name . len ( ) ;
317- }
318313 InlineAsmOperandRef :: SymStatic { def_id } => {
319314 // TODO(@Amanieu): Additional mangling is needed on
320315 // some targets to add a leading underscore (Mach-O).
@@ -404,24 +399,32 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
404399 // processed in the previous pass
405400 }
406401
407- InlineAsmOperandRef :: SymFn { instance } => {
408- inputs. push ( AsmInOperand {
409- constraint : "X" . into ( ) ,
410- rust_idx,
411- val : get_fn ( self . cx , instance) . get_address ( None ) ,
412- } ) ;
413- }
402+ InlineAsmOperandRef :: Const { value, ty : _ } => match value {
403+ Scalar :: Int ( _) => ( ) ,
404+ Scalar :: Ptr ( ptr, _) => {
405+ let ( prov, offset) = ptr. prov_and_relative_offset ( ) ;
406+ assert_eq ! ( offset. bytes( ) , 0 ) ;
407+ let global_alloc = self . tcx . global_alloc ( prov. alloc_id ( ) ) ;
408+ let val = match global_alloc {
409+ GlobalAlloc :: Function { instance } => {
410+ get_fn ( self . cx , instance) . get_address ( None )
411+ }
412+ GlobalAlloc :: Static ( def_id) => {
413+ self . cx . get_static ( def_id) . get_address ( None )
414+ }
415+ GlobalAlloc :: Memory ( _)
416+ | GlobalAlloc :: VTable ( ..)
417+ | GlobalAlloc :: TypeId { .. } => unreachable ! ( ) ,
418+ } ;
419+ inputs. push ( AsmInOperand { constraint : "X" . into ( ) , rust_idx, val } ) ;
420+ }
421+ } ,
414422
415423 InlineAsmOperandRef :: SymStatic { def_id } => {
416- inputs. push ( AsmInOperand {
417- constraint : "X" . into ( ) ,
418- rust_idx,
419- val : self . cx . get_static ( def_id) . get_address ( None ) ,
420- } ) ;
421- }
422-
423- InlineAsmOperandRef :: Const { .. } => {
424- // processed in the previous pass
424+ // TODO(@Amanieu): Additional mangling is needed on
425+ // some targets to add a leading underscore (MachO).
426+ constants_len +=
427+ self . tcx . symbol_name ( Instance :: mono ( self . tcx , def_id) ) . name . len ( ) ;
425428 }
426429
427430 InlineAsmOperandRef :: Label { .. } => {
@@ -497,12 +500,43 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
497500 push_to_template ( modifier, gcc_index) ;
498501 }
499502
500- InlineAsmOperandRef :: SymFn { instance } => {
501- // TODO(@Amanieu): Additional mangling is needed on
502- // some targets to add a leading underscore (Mach-O)
503- // or byte count suffixes (x86 Windows).
504- let name = self . tcx . symbol_name ( instance) . name ;
505- template_str. push_str ( name) ;
503+ InlineAsmOperandRef :: Const { value, ty } => {
504+ match value {
505+ Scalar :: Int ( int) => {
506+ // Const operands get injected directly into the template
507+ let string = rustc_codegen_ssa:: common:: asm_const_to_str (
508+ self . tcx ,
509+ span,
510+ int,
511+ self . layout_of ( ty) ,
512+ ) ;
513+ template_str. push_str ( & string) ;
514+ }
515+
516+ Scalar :: Ptr ( ptr, _) => {
517+ let ( prov, offset) = ptr. prov_and_relative_offset ( ) ;
518+ assert_eq ! ( offset. bytes( ) , 0 ) ;
519+ let global_alloc = self . tcx . global_alloc ( prov. alloc_id ( ) ) ;
520+ let symbol_name = match global_alloc {
521+ GlobalAlloc :: Function { instance } => {
522+ // TODO(@Amanieu): Additional mangling is needed on
523+ // some targets to add a leading underscore (Mach-O)
524+ // or byte count suffixes (x86 Windows).
525+ self . tcx . symbol_name ( instance)
526+ }
527+ GlobalAlloc :: Static ( def_id) => {
528+ // TODO(@Amanieu): Additional mangling is needed on
529+ // some targets to add a leading underscore (Mach-O).
530+ let instance = Instance :: mono ( self . tcx , def_id) ;
531+ self . tcx . symbol_name ( instance)
532+ }
533+ GlobalAlloc :: Memory ( _)
534+ | GlobalAlloc :: VTable ( ..)
535+ | GlobalAlloc :: TypeId { .. } => unreachable ! ( ) ,
536+ } ;
537+ template_str. push_str ( symbol_name. name ) ;
538+ }
539+ }
506540 }
507541
508542 InlineAsmOperandRef :: SymStatic { def_id } => {
@@ -513,17 +547,6 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
513547 template_str. push_str ( name) ;
514548 }
515549
516- InlineAsmOperandRef :: Const { value, ty } => {
517- // Const operands get injected directly into the template
518- let string = rustc_codegen_ssa:: common:: asm_const_to_str (
519- self . tcx ,
520- span,
521- value,
522- self . layout_of ( ty) ,
523- ) ;
524- template_str. push_str ( & string) ;
525- }
526-
527550 InlineAsmOperandRef :: Label { label } => {
528551 let label_gcc_index =
529552 labels. iter ( ) . position ( |& l| l == label) . expect ( "wrong rust index" ) ;
@@ -903,28 +926,48 @@ impl<'gcc, 'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
903926 InlineAsmTemplatePiece :: Placeholder { operand_idx, modifier : _, span } => {
904927 match operands[ operand_idx] {
905928 GlobalAsmOperandRef :: Const { value, ty } => {
906- // Const operands get injected directly into the
907- // template. Note that we don't need to escape %
908- // here unlike normal inline assembly.
909- let string = rustc_codegen_ssa:: common:: asm_const_to_str (
910- self . tcx ,
911- span,
912- value,
913- self . layout_of ( ty) ,
914- ) ;
915- template_str. push_str ( & string) ;
916- }
929+ match value {
930+ Scalar :: Int ( int) => {
931+ // Const operands get injected directly into the
932+ // template. Note that we don't need to escape %
933+ // here unlike normal inline assembly.
934+ let string = rustc_codegen_ssa:: common:: asm_const_to_str (
935+ self . tcx ,
936+ span,
937+ int,
938+ self . layout_of ( ty) ,
939+ ) ;
940+ template_str. push_str ( & string) ;
941+ }
917942
918- GlobalAsmOperandRef :: SymFn { instance } => {
919- let function = get_fn ( self , instance) ;
920- self . add_used_function ( function) ;
921- // TODO(@Amanieu): Additional mangling is needed on
922- // some targets to add a leading underscore (Mach-O)
923- // or byte count suffixes (x86 Windows).
924- let name = self . tcx . symbol_name ( instance) . name ;
925- template_str. push_str ( name) ;
943+ Scalar :: Ptr ( ptr, _) => {
944+ let ( prov, offset) = ptr. prov_and_relative_offset ( ) ;
945+ assert_eq ! ( offset. bytes( ) , 0 ) ;
946+ let global_alloc = self . tcx . global_alloc ( prov. alloc_id ( ) ) ;
947+ let symbol_name = match global_alloc {
948+ GlobalAlloc :: Function { instance } => {
949+ let function = get_fn ( self , instance) ;
950+ self . add_used_function ( function) ;
951+ // TODO(@Amanieu): Additional mangling is needed on
952+ // some targets to add a leading underscore (Mach-O)
953+ // or byte count suffixes (x86 Windows).
954+ self . tcx . symbol_name ( instance)
955+ }
956+ GlobalAlloc :: Static ( def_id) => {
957+ // TODO(antoyo): set the global variable as used.
958+ // TODO(@Amanieu): Additional mangling is needed on
959+ // some targets to add a leading underscore (Mach-O).
960+ let instance = Instance :: mono ( self . tcx , def_id) ;
961+ self . tcx . symbol_name ( instance)
962+ }
963+ GlobalAlloc :: Memory ( _)
964+ | GlobalAlloc :: VTable ( ..)
965+ | GlobalAlloc :: TypeId { .. } => unreachable ! ( ) ,
966+ } ;
967+ template_str. push_str ( symbol_name. name ) ;
968+ }
969+ }
926970 }
927-
928971 GlobalAsmOperandRef :: SymStatic { def_id } => {
929972 // TODO(antoyo): set the global variable as used.
930973 // TODO(@Amanieu): Additional mangling is needed on
0 commit comments