11// cSpell:ignoreRegExp [afkspqvwy]reg
22
33use std:: borrow:: Cow ;
4+ use std:: fmt:: Write ;
45
56use gccjit:: { LValue , RValue , ToRValue , Type } ;
7+ use rustc_abi:: Size ;
68use rustc_ast:: ast:: { InlineAsmOptions , InlineAsmTemplatePiece } ;
79use rustc_codegen_ssa:: mir:: operand:: OperandValue ;
810use rustc_codegen_ssa:: mir:: place:: PlaceRef ;
911use rustc_codegen_ssa:: traits:: {
1012 AsmBuilderMethods , AsmCodegenMethods , BaseTypeCodegenMethods , BuilderMethods ,
11- GlobalAsmOperandRef , InlineAsmOperandRef , ConstCodegenMethods ,
13+ ConstCodegenMethods , GlobalAsmOperandRef , InlineAsmOperandRef ,
1214} ;
1315use rustc_middle:: bug;
14- use rustc_middle:: mir:: interpret:: { GlobalAlloc , Scalar } ;
16+ use rustc_middle:: mir:: interpret:: { GlobalAlloc , PointerArithmetic , Scalar } ;
1517use rustc_middle:: ty:: Instance ;
1618use rustc_middle:: ty:: layout:: LayoutOf ;
1719use rustc_span:: Span ;
@@ -405,8 +407,7 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
405407 InlineAsmOperandRef :: Const { value, ty : _ } => match value {
406408 Scalar :: Int ( _) => ( ) ,
407409 Scalar :: Ptr ( ptr, _) => {
408- let ( prov, offset) = ptr. prov_and_relative_offset ( ) ;
409- assert_eq ! ( offset. bytes( ) , 0 ) ;
410+ let ( prov, _) = ptr. prov_and_relative_offset ( ) ;
410411 let global_alloc = self . tcx . global_alloc ( prov. alloc_id ( ) ) ;
411412 let ( val, sym) = self . cx . alloc_to_backend ( global_alloc) . unwrap ( ) ;
412413 const_syms. push ( sym. unwrap ( ) ) ;
@@ -509,12 +510,17 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
509510
510511 Scalar :: Ptr ( ptr, _) => {
511512 let ( _, offset) = ptr. prov_and_relative_offset ( ) ;
512- assert_eq ! ( offset. bytes( ) , 0 ) ;
513513 let instance = const_syms. remove ( 0 ) ;
514514 // TODO(@Amanieu): Additional mangling is needed on
515515 // some targets to add a leading underscore (Mach-O)
516516 // or byte count suffixes (x86 Windows).
517517 template_str. push_str ( self . tcx . symbol_name ( instance) . name ) ;
518+
519+ if offset != Size :: ZERO {
520+ let offset =
521+ self . sign_extend_to_target_isize ( offset. bytes ( ) ) ;
522+ write ! ( template_str, "{offset:+}" ) . unwrap ( ) ;
523+ }
518524 }
519525 }
520526 }
@@ -922,7 +928,6 @@ impl<'gcc, 'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
922928
923929 Scalar :: Ptr ( ptr, _) => {
924930 let ( prov, offset) = ptr. prov_and_relative_offset ( ) ;
925- assert_eq ! ( offset. bytes( ) , 0 ) ;
926931 let global_alloc = self . tcx . global_alloc ( prov. alloc_id ( ) ) ;
927932 let symbol_name = match global_alloc {
928933 GlobalAlloc :: Function { instance } => {
@@ -934,14 +939,21 @@ impl<'gcc, 'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
934939 self . tcx . symbol_name ( instance)
935940 }
936941 _ => {
937- let ( _, syms) = self . alloc_to_backend ( global_alloc) . unwrap ( ) ;
942+ let ( _, syms) =
943+ self . alloc_to_backend ( global_alloc) . unwrap ( ) ;
938944 // TODO(antoyo): set the global variable as used.
939945 // TODO(@Amanieu): Additional mangling is needed on
940946 // some targets to add a leading underscore (Mach-O).
941947 self . tcx . symbol_name ( syms. unwrap ( ) )
942948 }
943949 } ;
944950 template_str. push_str ( symbol_name. name ) ;
951+
952+ if offset != Size :: ZERO {
953+ let offset =
954+ self . sign_extend_to_target_isize ( offset. bytes ( ) ) ;
955+ write ! ( template_str, "{offset:+}" ) . unwrap ( ) ;
956+ }
945957 }
946958 }
947959 }
0 commit comments