@@ -4,7 +4,7 @@ use rustc_middle::mir::{Body, InlineAsmOperand};
44use rustc_middle:: ty:: layout:: { HasTyCtxt , HasTypingEnv , LayoutOf } ;
55use rustc_middle:: ty:: { Instance , TyCtxt } ;
66use rustc_middle:: { bug, ty} ;
7- use rustc_target :: asm :: InlineAsmArch ;
7+ use rustc_span :: sym ;
88
99use crate :: common;
1010use crate :: traits:: { AsmCodegenMethods , BuilderMethods , GlobalAsmOperandRef , MiscCodegenMethods } ;
@@ -31,7 +31,8 @@ pub(crate) fn codegen_naked_asm<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
3131 operands. iter ( ) . map ( |op| inline_to_global_operand :: < Bx > ( cx, instance, op) ) . collect ( ) ;
3232
3333 let item_data = cx. codegen_unit ( ) . items ( ) . get ( & MonoItem :: Fn ( instance) ) . unwrap ( ) ;
34- let ( begin, end) = crate :: mir:: naked_asm:: prefix_and_suffix ( cx. tcx ( ) , instance, item_data) ;
34+ let name = cx. mangled_name ( instance) ;
35+ let ( begin, end) = prefix_and_suffix ( cx. tcx ( ) , instance, & name, item_data) ;
3536
3637 let mut template_vec = Vec :: new ( ) ;
3738 template_vec. push ( rustc_ast:: ast:: InlineAsmTemplatePiece :: String ( begin. into ( ) ) ) ;
@@ -108,7 +109,7 @@ impl AsmBinaryFormat {
108109 fn from_target ( target : & rustc_target:: spec:: Target ) -> Self {
109110 if target. is_like_windows {
110111 Self :: Coff
111- } else if target. options . vendor == "apple" {
112+ } else if target. is_like_osx {
112113 Self :: Macho
113114 } else {
114115 Self :: Elf
@@ -119,33 +120,29 @@ impl AsmBinaryFormat {
119120fn prefix_and_suffix < ' tcx > (
120121 tcx : TyCtxt < ' tcx > ,
121122 instance : Instance < ' tcx > ,
123+ asm_name : & str ,
122124 item_data : & MonoItemData ,
123125) -> ( String , String ) {
124126 use std:: fmt:: Write ;
125127
126- let target = & tcx. sess . target ;
127- let target_arch = tcx. sess . asm_arch ;
128-
129- let is_arm = target. arch == "arm" ;
130- let is_thumb = is_arm && target. llvm_target . contains ( "thumb" ) ;
131-
132- let mangle = ( target. is_like_windows && matches ! ( target_arch, Some ( InlineAsmArch :: X86 ) ) )
133- || target. options . vendor == "apple" ;
134-
135- let asm_name = format ! ( "{}{}" , if mangle { "_" } else { "" } , tcx. symbol_name( instance) . name) ;
128+ let is_arm = tcx. sess . target . arch == "arm" ;
129+ let is_thumb = tcx. sess . unstable_target_features . contains ( & sym:: thumb_mode) ;
136130
137131 let attrs = tcx. codegen_fn_attrs ( instance. def_id ( ) ) ;
138132 let link_section = attrs. link_section . map ( |symbol| symbol. as_str ( ) . to_string ( ) ) ;
133+ let align = attrs. alignment . map ( |a| a. bytes ( ) ) . unwrap_or ( 4 ) ;
139134
135+ // See https://sourceware.org/binutils/docs/as/ARM-Directives.html for info on these directives.
136+ // In particular, `.arm` can also be written `.code 32` and `.thumb` as `.code 16`.
140137 let ( arch_prefix, arch_suffix) = if is_arm {
141138 (
142139 match attrs. instruction_set {
143140 None => match is_thumb {
144141 true => ".thumb\n .thumb_func" ,
145142 false => ".arm" ,
146143 } ,
147- Some ( InstructionSetAttr :: ArmA32 ) => ".arm" ,
148144 Some ( InstructionSetAttr :: ArmT32 ) => ".thumb\n .thumb_func" ,
145+ Some ( InstructionSetAttr :: ArmA32 ) => ".arm" ,
149146 } ,
150147 match is_thumb {
151148 true => ".thumb" ,
@@ -173,7 +170,7 @@ fn prefix_and_suffix<'tcx>(
173170 } ;
174171
175172 writeln ! ( begin, ".pushsection {section},\" ax\" , {progbits}" ) . unwrap ( ) ;
176- writeln ! ( begin, ".balign 4 " ) . unwrap ( ) ;
173+ writeln ! ( begin, ".balign {align} " ) . unwrap ( ) ;
177174 writeln ! ( begin, ".globl {asm_name}" ) . unwrap ( ) ;
178175 if let Visibility :: Hidden = item_data. visibility {
179176 writeln ! ( begin, ".hidden {asm_name}" ) . unwrap ( ) ;
@@ -194,7 +191,7 @@ fn prefix_and_suffix<'tcx>(
194191 AsmBinaryFormat :: Macho => {
195192 let section = link_section. unwrap_or ( "__TEXT,__text" . to_string ( ) ) ;
196193 writeln ! ( begin, ".pushsection {},regular,pure_instructions" , section) . unwrap ( ) ;
197- writeln ! ( begin, ".balign 4 " ) . unwrap ( ) ;
194+ writeln ! ( begin, ".balign {align} " ) . unwrap ( ) ;
198195 writeln ! ( begin, ".globl {asm_name}" ) . unwrap ( ) ;
199196 if let Visibility :: Hidden = item_data. visibility {
200197 writeln ! ( begin, ".private_extern {asm_name}" ) . unwrap ( ) ;
@@ -210,7 +207,7 @@ fn prefix_and_suffix<'tcx>(
210207 AsmBinaryFormat :: Coff => {
211208 let section = link_section. unwrap_or ( format ! ( ".text.{asm_name}" ) ) ;
212209 writeln ! ( begin, ".pushsection {},\" xr\" " , section) . unwrap ( ) ;
213- writeln ! ( begin, ".balign 4 " ) . unwrap ( ) ;
210+ writeln ! ( begin, ".balign {align} " ) . unwrap ( ) ;
214211 writeln ! ( begin, ".globl {asm_name}" ) . unwrap ( ) ;
215212 writeln ! ( begin, ".def {asm_name}" ) . unwrap ( ) ;
216213 writeln ! ( begin, ".scl 2" ) . unwrap ( ) ;
0 commit comments