1 use crate::mir::interpret::Scalar;
2 use crate::ty::{self, Ty, TyCtxt};
3 use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
4 use smallvec::{smallvec, SmallVec};
7 AssertMessage, BasicBlock, InlineAsmOperand, Operand, Place, SourceInfo, Successors,
10 pub use rustc_ast::Mutability;
11 use rustc_macros::HashStable;
14 use std::fmt::{self, Debug, Formatter, Write};
18 pub use super::query::*;
20 #[derive(Debug, Clone, TyEncodable, TyDecodable, HashStable, PartialEq)]
21 pub struct SwitchTargets {
22 /// Possible values. The locations to branch to in each case
23 /// are found in the corresponding indices from the `targets` vector.
24 values: SmallVec<[u128; 1]>,
26 /// Possible branch sites. The last element of this vector is used
27 /// for the otherwise branch, so targets.len() == values.len() + 1
30 // This invariant is quite non-obvious and also could be improved.
31 // One way to make this invariant is to have something like this instead:
33 // branches: Vec<(ConstInt, BasicBlock)>,
34 // otherwise: Option<BasicBlock> // exhaustive if None
36 // However we’ve decided to keep this as-is until we figure a case
37 // where some other approach seems to be strictly better than other.
38 targets: SmallVec<[BasicBlock; 2]>,
42 /// Creates switch targets from an iterator of values and target blocks.
44 /// The iterator may be empty, in which case the `SwitchInt` instruction is equivalent to
45 /// `goto otherwise;`.
46 pub fn new(targets: impl Iterator<Item = (u128, BasicBlock)>, otherwise: BasicBlock) -> Self {
47 let (values, mut targets): (SmallVec<_>, SmallVec<_>) = targets.unzip();
48 targets.push(otherwise);
49 Self { values, targets }
52 /// Builds a switch targets definition that jumps to `then` if the tested value equals `value`,
53 /// and to `else_` if not.
54 pub fn static_if(value: u128, then: BasicBlock, else_: BasicBlock) -> Self {
55 Self { values: smallvec![value], targets: smallvec![then, else_] }
58 /// Returns the fallback target that is jumped to when none of the values match the operand.
59 pub fn otherwise(&self) -> BasicBlock {
60 *self.targets.last().unwrap()
63 /// Returns an iterator over the switch targets.
65 /// The iterator will yield tuples containing the value and corresponding target to jump to, not
66 /// including the `otherwise` fallback target.
68 /// Note that this may yield 0 elements. Only the `otherwise` branch is mandatory.
69 pub fn iter(&self) -> SwitchTargetsIter<'_> {
70 SwitchTargetsIter { inner: self.values.iter().zip(self.targets.iter()) }
73 /// Returns a slice with all possible jump targets (including the fallback target).
74 pub fn all_targets(&self) -> &[BasicBlock] {
78 pub fn all_targets_mut(&mut self) -> &mut [BasicBlock] {
83 pub struct SwitchTargetsIter<'a> {
84 inner: iter::Zip<slice::Iter<'a, u128>, slice::Iter<'a, BasicBlock>>,
87 impl<'a> Iterator for SwitchTargetsIter<'a> {
88 type Item = (u128, BasicBlock);
90 fn next(&mut self) -> Option<Self::Item> {
91 self.inner.next().map(|(val, bb)| (*val, *bb))
94 fn size_hint(&self) -> (usize, Option<usize>) {
95 self.inner.size_hint()
99 impl<'a> ExactSizeIterator for SwitchTargetsIter<'a> {}
101 #[derive(Clone, TyEncodable, TyDecodable, HashStable, PartialEq)]
102 pub enum TerminatorKind<'tcx> {
103 /// Block should have one successor in the graph; we jump there.
104 Goto { target: BasicBlock },
106 /// Operand evaluates to an integer; jump depending on its value
107 /// to one of the targets, and otherwise fallback to `otherwise`.
109 /// The discriminant value being tested.
110 discr: Operand<'tcx>,
112 /// The type of value being tested.
113 /// This is always the same as the type of `discr`.
114 /// FIXME: remove this redundant information. Currently, it is relied on by pretty-printing.
117 targets: SwitchTargets,
120 /// Indicates that the landing pad is finished and unwinding should
121 /// continue. Emitted by `build::scope::diverge_cleanup`.
124 /// Indicates that the landing pad is finished and that the process
125 /// should abort. Used to prevent unwinding for foreign items.
128 /// Indicates a normal return. The return place should have
129 /// been filled in before this executes. This can occur multiple times
130 /// in different basic blocks.
133 /// Indicates a terminator that can never be reached.
136 /// Drop the `Place`.
137 Drop { place: Place<'tcx>, target: BasicBlock, unwind: Option<BasicBlock> },
139 /// Drop the `Place` and assign the new value over it. This ensures
140 /// that the assignment to `P` occurs *even if* the destructor for
141 /// place unwinds. Its semantics are best explained by the
146 /// DropAndReplace(P <- V, goto BB1, unwind BB2)
154 /// Drop(P, goto BB1, unwind BB2)
157 /// // P is now uninitialized
161 /// // P is now uninitialized -- its dtor panicked
166 /// Note that DropAndReplace is eliminated as part of the `ElaborateDrops` pass.
169 value: Operand<'tcx>,
171 unwind: Option<BasicBlock>,
174 /// Block ends with a call of a function.
176 /// The function that’s being called.
178 /// Arguments the function is called with.
179 /// These are owned by the callee, which is free to modify them.
180 /// This allows the memory occupied by "by-value" arguments to be
181 /// reused across function calls without duplicating the contents.
182 args: Vec<Operand<'tcx>>,
183 /// Destination for the return value. If some, the call is converging.
184 destination: Option<(Place<'tcx>, BasicBlock)>,
185 /// Cleanups to be done if the call unwinds.
186 cleanup: Option<BasicBlock>,
187 /// `true` if this is from a call in HIR rather than from an overloaded
188 /// operator. True for overloaded function call.
190 /// This `Span` is the span of the function, without the dot and receiver
191 /// (e.g. `foo(a, b)` in `x.foo(a, b)`
195 /// Jump to the target if the condition has the expected value,
196 /// otherwise panic with a message and a cleanup target.
200 msg: AssertMessage<'tcx>,
202 cleanup: Option<BasicBlock>,
207 /// The value to return.
208 value: Operand<'tcx>,
209 /// Where to resume to.
211 /// The place to store the resume argument in.
212 resume_arg: Place<'tcx>,
213 /// Cleanup to be done if the generator is dropped at this suspend point.
214 drop: Option<BasicBlock>,
217 /// Indicates the end of the dropping of a generator.
220 /// A block where control flow only ever takes one real path, but borrowck
221 /// needs to be more conservative.
223 /// The target normal control flow will take.
224 real_target: BasicBlock,
225 /// A block control flow could conceptually jump to, but won't in
227 imaginary_target: BasicBlock,
229 /// A terminator for blocks that only take one path in reality, but where we
230 /// reserve the right to unwind in borrowck, even if it won't happen in practice.
231 /// This can arise in infinite loops with no function calls for example.
233 /// The target normal control flow will take.
234 real_target: BasicBlock,
235 /// The imaginary cleanup block link. This particular path will never be taken
236 /// in practice, but in order to avoid fragility we want to always
237 /// consider it in borrowck. We don't want to accept programs which
238 /// pass borrowck only when `panic=abort` or some assertions are disabled
239 /// due to release vs. debug mode builds. This needs to be an `Option` because
240 /// of the `remove_noop_landing_pads` and `no_landing_pads` passes.
241 unwind: Option<BasicBlock>,
244 /// Block ends with an inline assembly block. This is a terminator since
245 /// inline assembly is allowed to diverge.
247 /// The template for the inline assembly, with placeholders.
248 template: &'tcx [InlineAsmTemplatePiece],
250 /// The operands for the inline assembly, as `Operand`s or `Place`s.
251 operands: Vec<InlineAsmOperand<'tcx>>,
253 /// Miscellaneous options for the inline assembly.
254 options: InlineAsmOptions,
256 /// Source spans for each line of the inline assembly code. These are
257 /// used to map assembler errors back to the line in the source code.
258 line_spans: &'tcx [Span],
260 /// Destination block after the inline assembly returns, unless it is
261 /// diverging (InlineAsmOptions::NORETURN).
262 destination: Option<BasicBlock>,
265 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
266 pub struct Terminator<'tcx> {
267 pub source_info: SourceInfo,
268 pub kind: TerminatorKind<'tcx>,
271 impl<'tcx> Terminator<'tcx> {
272 pub fn successors(&self) -> Successors<'_> {
273 self.kind.successors()
276 pub fn successors_mut(&mut self) -> SuccessorsMut<'_> {
277 self.kind.successors_mut()
280 pub fn unwind(&self) -> Option<&Option<BasicBlock>> {
284 pub fn unwind_mut(&mut self) -> Option<&mut Option<BasicBlock>> {
285 self.kind.unwind_mut()
289 impl<'tcx> TerminatorKind<'tcx> {
295 ) -> TerminatorKind<'tcx> {
296 TerminatorKind::SwitchInt {
298 switch_ty: tcx.types.bool,
299 targets: SwitchTargets::static_if(0, f, t),
303 pub fn successors(&self) -> Successors<'_> {
304 use self::TerminatorKind::*;
311 | Call { destination: None, cleanup: None, .. }
312 | InlineAsm { destination: None, .. } => None.into_iter().chain(&[]),
313 Goto { target: ref t }
314 | Call { destination: None, cleanup: Some(ref t), .. }
315 | Call { destination: Some((_, ref t)), cleanup: None, .. }
316 | Yield { resume: ref t, drop: None, .. }
317 | DropAndReplace { target: ref t, unwind: None, .. }
318 | Drop { target: ref t, unwind: None, .. }
319 | Assert { target: ref t, cleanup: None, .. }
320 | FalseUnwind { real_target: ref t, unwind: None }
321 | InlineAsm { destination: Some(ref t), .. } => Some(t).into_iter().chain(&[]),
322 Call { destination: Some((_, ref t)), cleanup: Some(ref u), .. }
323 | Yield { resume: ref t, drop: Some(ref u), .. }
324 | DropAndReplace { target: ref t, unwind: Some(ref u), .. }
325 | Drop { target: ref t, unwind: Some(ref u), .. }
326 | Assert { target: ref t, cleanup: Some(ref u), .. }
327 | FalseUnwind { real_target: ref t, unwind: Some(ref u) } => {
328 Some(t).into_iter().chain(slice::from_ref(u))
330 SwitchInt { ref targets, .. } => None.into_iter().chain(&targets.targets[..]),
331 FalseEdge { ref real_target, ref imaginary_target } => {
332 Some(real_target).into_iter().chain(slice::from_ref(imaginary_target))
337 pub fn successors_mut(&mut self) -> SuccessorsMut<'_> {
338 use self::TerminatorKind::*;
345 | Call { destination: None, cleanup: None, .. }
346 | InlineAsm { destination: None, .. } => None.into_iter().chain(&mut []),
347 Goto { target: ref mut t }
348 | Call { destination: None, cleanup: Some(ref mut t), .. }
349 | Call { destination: Some((_, ref mut t)), cleanup: None, .. }
350 | Yield { resume: ref mut t, drop: None, .. }
351 | DropAndReplace { target: ref mut t, unwind: None, .. }
352 | Drop { target: ref mut t, unwind: None, .. }
353 | Assert { target: ref mut t, cleanup: None, .. }
354 | FalseUnwind { real_target: ref mut t, unwind: None }
355 | InlineAsm { destination: Some(ref mut t), .. } => Some(t).into_iter().chain(&mut []),
356 Call { destination: Some((_, ref mut t)), cleanup: Some(ref mut u), .. }
357 | Yield { resume: ref mut t, drop: Some(ref mut u), .. }
358 | DropAndReplace { target: ref mut t, unwind: Some(ref mut u), .. }
359 | Drop { target: ref mut t, unwind: Some(ref mut u), .. }
360 | Assert { target: ref mut t, cleanup: Some(ref mut u), .. }
361 | FalseUnwind { real_target: ref mut t, unwind: Some(ref mut u) } => {
362 Some(t).into_iter().chain(slice::from_mut(u))
364 SwitchInt { ref mut targets, .. } => None.into_iter().chain(&mut targets.targets[..]),
365 FalseEdge { ref mut real_target, ref mut imaginary_target } => {
366 Some(real_target).into_iter().chain(slice::from_mut(imaginary_target))
371 pub fn unwind(&self) -> Option<&Option<BasicBlock>> {
373 TerminatorKind::Goto { .. }
374 | TerminatorKind::Resume
375 | TerminatorKind::Abort
376 | TerminatorKind::Return
377 | TerminatorKind::Unreachable
378 | TerminatorKind::GeneratorDrop
379 | TerminatorKind::Yield { .. }
380 | TerminatorKind::SwitchInt { .. }
381 | TerminatorKind::FalseEdge { .. }
382 | TerminatorKind::InlineAsm { .. } => None,
383 TerminatorKind::Call { cleanup: ref unwind, .. }
384 | TerminatorKind::Assert { cleanup: ref unwind, .. }
385 | TerminatorKind::DropAndReplace { ref unwind, .. }
386 | TerminatorKind::Drop { ref unwind, .. }
387 | TerminatorKind::FalseUnwind { ref unwind, .. } => Some(unwind),
391 pub fn unwind_mut(&mut self) -> Option<&mut Option<BasicBlock>> {
393 TerminatorKind::Goto { .. }
394 | TerminatorKind::Resume
395 | TerminatorKind::Abort
396 | TerminatorKind::Return
397 | TerminatorKind::Unreachable
398 | TerminatorKind::GeneratorDrop
399 | TerminatorKind::Yield { .. }
400 | TerminatorKind::SwitchInt { .. }
401 | TerminatorKind::FalseEdge { .. }
402 | TerminatorKind::InlineAsm { .. } => None,
403 TerminatorKind::Call { cleanup: ref mut unwind, .. }
404 | TerminatorKind::Assert { cleanup: ref mut unwind, .. }
405 | TerminatorKind::DropAndReplace { ref mut unwind, .. }
406 | TerminatorKind::Drop { ref mut unwind, .. }
407 | TerminatorKind::FalseUnwind { ref mut unwind, .. } => Some(unwind),
412 impl<'tcx> Debug for TerminatorKind<'tcx> {
413 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
415 let successor_count = self.successors().count();
416 let labels = self.fmt_successor_labels();
417 assert_eq!(successor_count, labels.len());
419 match successor_count {
422 1 => write!(fmt, " -> {:?}", self.successors().next().unwrap()),
425 write!(fmt, " -> [")?;
426 for (i, target) in self.successors().enumerate() {
430 write!(fmt, "{}: {:?}", labels[i], target)?;
438 impl<'tcx> TerminatorKind<'tcx> {
439 /// Writes the "head" part of the terminator; that is, its name and the data it uses to pick the
440 /// successor basic block, if any. The only information not included is the list of possible
441 /// successors, which may be rendered differently between the text and the graphviz format.
442 pub fn fmt_head<W: Write>(&self, fmt: &mut W) -> fmt::Result {
443 use self::TerminatorKind::*;
445 Goto { .. } => write!(fmt, "goto"),
446 SwitchInt { discr, .. } => write!(fmt, "switchInt({:?})", discr),
447 Return => write!(fmt, "return"),
448 GeneratorDrop => write!(fmt, "generator_drop"),
449 Resume => write!(fmt, "resume"),
450 Abort => write!(fmt, "abort"),
451 Yield { value, resume_arg, .. } => write!(fmt, "{:?} = yield({:?})", resume_arg, value),
452 Unreachable => write!(fmt, "unreachable"),
453 Drop { place, .. } => write!(fmt, "drop({:?})", place),
454 DropAndReplace { place, value, .. } => {
455 write!(fmt, "replace({:?} <- {:?})", place, value)
457 Call { func, args, destination, .. } => {
458 if let Some((destination, _)) = destination {
459 write!(fmt, "{:?} = ", destination)?;
461 write!(fmt, "{:?}(", func)?;
462 for (index, arg) in args.iter().enumerate() {
466 write!(fmt, "{:?}", arg)?;
470 Assert { cond, expected, msg, .. } => {
471 write!(fmt, "assert(")?;
475 write!(fmt, "{:?}, ", cond)?;
476 msg.fmt_assert_args(fmt)?;
479 FalseEdge { .. } => write!(fmt, "falseEdge"),
480 FalseUnwind { .. } => write!(fmt, "falseUnwind"),
481 InlineAsm { template, ref operands, options, .. } => {
482 write!(fmt, "asm!(\"{}\"", InlineAsmTemplatePiece::to_string(template))?;
485 let print_late = |&late| if late { "late" } else { "" };
487 InlineAsmOperand::In { reg, value } => {
488 write!(fmt, "in({}) {:?}", reg, value)?;
490 InlineAsmOperand::Out { reg, late, place: Some(place) } => {
491 write!(fmt, "{}out({}) {:?}", print_late(late), reg, place)?;
493 InlineAsmOperand::Out { reg, late, place: None } => {
494 write!(fmt, "{}out({}) _", print_late(late), reg)?;
496 InlineAsmOperand::InOut {
500 out_place: Some(out_place),
504 "in{}out({}) {:?} => {:?}",
511 InlineAsmOperand::InOut { reg, late, in_value, out_place: None } => {
512 write!(fmt, "in{}out({}) {:?} => _", print_late(late), reg, in_value)?;
514 InlineAsmOperand::Const { value } => {
515 write!(fmt, "const {:?}", value)?;
517 InlineAsmOperand::SymFn { value } => {
518 write!(fmt, "sym_fn {:?}", value)?;
520 InlineAsmOperand::SymStatic { def_id } => {
521 write!(fmt, "sym_static {:?}", def_id)?;
525 write!(fmt, ", options({:?}))", options)
530 /// Returns the list of labels for the edges to the successor basic blocks.
531 pub fn fmt_successor_labels(&self) -> Vec<Cow<'static, str>> {
532 use self::TerminatorKind::*;
534 Return | Resume | Abort | Unreachable | GeneratorDrop => vec![],
535 Goto { .. } => vec!["".into()],
536 SwitchInt { ref targets, switch_ty, .. } => ty::tls::with(|tcx| {
537 let param_env = ty::ParamEnv::empty();
538 let switch_ty = tcx.lift(switch_ty).unwrap();
539 let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().size;
544 ty::Const::from_scalar(tcx, Scalar::from_uint(u, size), switch_ty)
548 .chain(iter::once("otherwise".into()))
551 Call { destination: Some(_), cleanup: Some(_), .. } => {
552 vec!["return".into(), "unwind".into()]
554 Call { destination: Some(_), cleanup: None, .. } => vec!["return".into()],
555 Call { destination: None, cleanup: Some(_), .. } => vec!["unwind".into()],
556 Call { destination: None, cleanup: None, .. } => vec![],
557 Yield { drop: Some(_), .. } => vec!["resume".into(), "drop".into()],
558 Yield { drop: None, .. } => vec!["resume".into()],
559 DropAndReplace { unwind: None, .. } | Drop { unwind: None, .. } => {
560 vec!["return".into()]
562 DropAndReplace { unwind: Some(_), .. } | Drop { unwind: Some(_), .. } => {
563 vec!["return".into(), "unwind".into()]
565 Assert { cleanup: None, .. } => vec!["".into()],
566 Assert { .. } => vec!["success".into(), "unwind".into()],
567 FalseEdge { .. } => vec!["real".into(), "imaginary".into()],
568 FalseUnwind { unwind: Some(_), .. } => vec!["real".into(), "cleanup".into()],
569 FalseUnwind { unwind: None, .. } => vec!["real".into()],
570 InlineAsm { destination: Some(_), .. } => vec!["".into()],
571 InlineAsm { destination: None, .. } => vec![],