Skip to content

Commit f86f9a4

Browse files
committed
Extract codegen_inline_asm_inner function and use in codegen_xgetbv
1 parent 9f7bcd7 commit f86f9a4

File tree

1 file changed

+26
-56
lines changed

1 file changed

+26
-56
lines changed

src/inline_asm.rs

+26-56
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ pub(crate) fn codegen_inline_asm_terminator<'tcx>(
4242
options: InlineAsmOptions,
4343
destination: Option<mir::BasicBlock>,
4444
) {
45-
// FIXME add .eh_frame unwind info directives
46-
4745
// Used by panic_abort on Windows, but uses a syntax which only happens to work with
4846
// asm!() by accident and breaks with the GNU assembler as well as global_asm!() for
4947
// the LLVM backend.
@@ -135,12 +133,33 @@ pub(crate) fn codegen_inline_asm_terminator<'tcx>(
135133
})
136134
.collect::<Vec<_>>();
137135

136+
codegen_inline_asm_inner(fx, template, &operands, options);
137+
138+
match destination {
139+
Some(destination) => {
140+
let destination_block = fx.get_block(destination);
141+
fx.bcx.ins().jump(destination_block, &[]);
142+
}
143+
None => {
144+
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
145+
}
146+
}
147+
}
148+
149+
fn codegen_inline_asm_inner<'tcx>(
150+
fx: &mut FunctionCx<'_, '_, 'tcx>,
151+
template: &[InlineAsmTemplatePiece],
152+
operands: &[CInlineAsmOperand<'tcx>],
153+
options: InlineAsmOptions,
154+
) {
155+
// FIXME add .eh_frame unwind info directives
156+
138157
let mut asm_gen = InlineAssemblyGenerator {
139158
tcx: fx.tcx,
140159
arch: fx.tcx.sess.asm_arch.unwrap(),
141160
enclosing_def_id: fx.instance.def_id(),
142161
template,
143-
operands: &operands,
162+
operands,
144163
options,
145164
registers: Vec::new(),
146165
stack_slots_clobber: Vec::new(),
@@ -185,16 +204,6 @@ pub(crate) fn codegen_inline_asm_terminator<'tcx>(
185204
}
186205

187206
call_inline_asm(fx, &asm_name, asm_gen.stack_slot_size, inputs, outputs);
188-
189-
match destination {
190-
Some(destination) => {
191-
let destination_block = fx.get_block(destination);
192-
fx.bcx.ins().jump(destination_block, &[]);
193-
}
194-
None => {
195-
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
196-
}
197-
}
198207
}
199208

200209
struct InlineAssemblyGenerator<'a, 'tcx> {
@@ -754,14 +763,9 @@ pub(crate) fn codegen_xgetbv<'tcx>(
754763
];
755764
let options = InlineAsmOptions::NOSTACK | InlineAsmOptions::PURE | InlineAsmOptions::NOMEM;
756765

757-
let mut inputs = Vec::new();
758-
let mut outputs = Vec::new();
759-
760-
let mut asm_gen = InlineAssemblyGenerator {
761-
tcx: fx.tcx,
762-
arch: fx.tcx.sess.asm_arch.unwrap(),
763-
enclosing_def_id: fx.instance.def_id(),
764-
template: &[InlineAsmTemplatePiece::String(
766+
codegen_inline_asm_inner(
767+
fx,
768+
&[InlineAsmTemplatePiece::String(
765769
"
766770
xgetbv
767771
// out = rdx << 32 | rax
@@ -770,41 +774,7 @@ pub(crate) fn codegen_xgetbv<'tcx>(
770774
"
771775
.to_string(),
772776
)],
773-
operands: &operands,
777+
&operands,
774778
options,
775-
registers: Vec::new(),
776-
stack_slots_clobber: Vec::new(),
777-
stack_slots_input: Vec::new(),
778-
stack_slots_output: Vec::new(),
779-
stack_slot_size: Size::from_bytes(0),
780-
};
781-
asm_gen.allocate_registers();
782-
asm_gen.allocate_stack_slots();
783-
784-
let inline_asm_index = fx.cx.inline_asm_index.get();
785-
fx.cx.inline_asm_index.set(inline_asm_index + 1);
786-
let asm_name = format!(
787-
"__inline_asm_{}_n{}",
788-
fx.cx.cgu_name.as_str().replace('.', "__").replace('-', "_"),
789-
inline_asm_index
790779
);
791-
792-
let generated_asm = asm_gen.generate_asm_wrapper(&asm_name);
793-
fx.cx.global_asm.push_str(&generated_asm);
794-
795-
for (i, operand) in operands.iter().enumerate() {
796-
match operand {
797-
CInlineAsmOperand::In { reg: _, value } => {
798-
inputs.push((asm_gen.stack_slots_input[i].unwrap(), *value));
799-
}
800-
CInlineAsmOperand::Out { reg: _, late: _, place } => {
801-
if let Some(place) = place {
802-
outputs.push((asm_gen.stack_slots_output[i].unwrap(), *place));
803-
}
804-
}
805-
_ => unreachable!(),
806-
}
807-
}
808-
809-
call_inline_asm(fx, &asm_name, asm_gen.stack_slot_size, inputs, outputs);
810780
}

0 commit comments

Comments
 (0)