Skip to content

Commit 563a75b

Browse files
committed
Add a Machine hook for inline assembly
1 parent dec6894 commit 563a75b

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

compiler/rustc_const_eval/src/interpret/machine.rs

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::borrow::{Borrow, Cow};
66
use std::fmt::Debug;
77
use std::hash::Hash;
88

9+
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
910
use rustc_middle::mir;
1011
use rustc_middle::ty::{self, Ty, TyCtxt};
1112
use rustc_span::def_id::DefId;
@@ -323,6 +324,15 @@ pub trait Machine<'mir, 'tcx>: Sized {
323324
kind: Option<MemoryKind<Self::MemoryKind>>,
324325
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra>>>;
325326

327+
fn eval_inline_asm(
328+
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
329+
_template: &'tcx [InlineAsmTemplatePiece],
330+
_operands: &[mir::InlineAsmOperand<'tcx>],
331+
_options: InlineAsmOptions,
332+
) -> InterpResult<'tcx> {
333+
throw_unsup_format!("inline assembly is not supported")
334+
}
335+
326336
/// Hook for performing extra checks on a memory read access.
327337
///
328338
/// Takes read-only access to the allocation so we can keep all the memory read

compiler/rustc_const_eval/src/interpret/terminator.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::borrow::Cow;
22

3+
use rustc_ast::ast::InlineAsmOptions;
34
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
45
use rustc_middle::ty::Instance;
56
use rustc_middle::{
@@ -166,8 +167,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
166167
terminator.kind
167168
),
168169

169-
// Inline assembly can't be interpreted.
170-
InlineAsm { .. } => throw_unsup_format!("inline assembly is not supported"),
170+
InlineAsm { template, ref operands, options, destination, .. } => {
171+
M::eval_inline_asm(self, template, operands, options)?;
172+
if options.contains(InlineAsmOptions::NORETURN) {
173+
throw_ub_format!("returned from noreturn inline assembly");
174+
}
175+
self.go_to_block(
176+
destination
177+
.expect("InlineAsm terminators without noreturn must have a destination"),
178+
)
179+
}
171180
}
172181

173182
Ok(())

0 commit comments

Comments
 (0)