Path: blob/main/cranelift/codegen/src/ir/progpoint.rs
1693 views
//! Program points.12use crate::ir::{Block, Inst};3use core::fmt;45/// A `ProgramPoint` represents a position in a function where the live range of an SSA value can6/// begin or end. It can be either:7///8/// 1. An instruction or9/// 2. A block header.10///11/// This corresponds more or less to the lines in the textual form of Cranelift IR.12#[derive(PartialEq, Eq, Clone, Copy)]13pub enum ProgramPoint {14/// An instruction in the function.15Inst(Inst),16/// A block header.17Block(Block),18}1920impl ProgramPoint {21/// Get the instruction we know is inside.22pub fn unwrap_inst(self) -> Inst {23match self {24Self::Inst(x) => x,25Self::Block(x) => panic!("expected inst: {x}"),26}27}28}2930impl From<Inst> for ProgramPoint {31fn from(inst: Inst) -> Self {32Self::Inst(inst)33}34}3536impl From<Block> for ProgramPoint {37fn from(block: Block) -> Self {38Self::Block(block)39}40}4142impl fmt::Display for ProgramPoint {43fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {44match *self {45Self::Inst(x) => write!(f, "{x}"),46Self::Block(x) => write!(f, "{x}"),47}48}49}5051impl fmt::Debug for ProgramPoint {52fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {53write!(f, "ProgramPoint({self})")54}55}5657#[cfg(test)]58mod tests {59use super::*;60use crate::entity::EntityRef;61use alloc::string::ToString;6263#[test]64fn convert() {65let i5 = Inst::new(5);66let b3 = Block::new(3);6768let pp1: ProgramPoint = i5.into();69let pp2: ProgramPoint = b3.into();7071assert_eq!(pp1.to_string(), "inst5");72assert_eq!(pp2.to_string(), "block3");73}74}757677