Path: blob/main/cranelift/codegen/src/ir/stackslot.rs
1693 views
//! Stack slots.1//!2//! The `StackSlotData` struct keeps track of a single stack slot in a function.3//!45use crate::entity::PrimaryMap;6use crate::ir::StackSlot;7use crate::ir::entities::{DynamicStackSlot, DynamicType};8use core::fmt;9use core::str::FromStr;1011#[cfg(feature = "enable-serde")]12use serde_derive::{Deserialize, Serialize};1314/// The size of an object on the stack, or the size of a stack frame.15///16/// We don't use `usize` to represent object sizes on the target platform because Cranelift supports17/// cross-compilation, and `usize` is a type that depends on the host platform, not the target18/// platform.19pub type StackSize = u32;2021/// The kind of a stack slot.22#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]23#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]24pub enum StackSlotKind {25/// An explicit stack slot. This is a chunk of stack memory for use by the `stack_load`26/// and `stack_store` instructions.27ExplicitSlot,28/// An explicit stack slot for dynamic vector types. This is a chunk of stack memory29/// for use by the `dynamic_stack_load` and `dynamic_stack_store` instructions.30ExplicitDynamicSlot,31}3233impl FromStr for StackSlotKind {34type Err = ();3536fn from_str(s: &str) -> Result<Self, ()> {37use self::StackSlotKind::*;38match s {39"explicit_slot" => Ok(ExplicitSlot),40"explicit_dynamic_slot" => Ok(ExplicitDynamicSlot),41_ => Err(()),42}43}44}4546impl fmt::Display for StackSlotKind {47fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {48use self::StackSlotKind::*;49f.write_str(match *self {50ExplicitSlot => "explicit_slot",51ExplicitDynamicSlot => "explicit_dynamic_slot",52})53}54}5556/// Contents of a stack slot.57#[derive(Clone, Debug, PartialEq, Eq, Hash)]58#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]59pub struct StackSlotData {60/// The kind of stack slot.61pub kind: StackSlotKind,6263/// Size of stack slot in bytes.64pub size: StackSize,6566/// Alignment of stack slot as a power-of-two exponent (log267/// value). The stack slot will be at least this aligned; it may68/// be aligned according to other considerations, such as minimum69/// stack slot size or machine word size, as well.70pub align_shift: u8,71}7273impl StackSlotData {74/// Create a stack slot with the specified byte size and alignment.75pub fn new(kind: StackSlotKind, size: StackSize, align_shift: u8) -> Self {76Self {77kind,78size,79align_shift,80}81}82}8384impl fmt::Display for StackSlotData {85fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {86if self.align_shift != 0 {87write!(88f,89"{} {}, align = {}",90self.kind,91self.size,921u32 << self.align_shift93)94} else {95write!(f, "{} {}", self.kind, self.size)96}97}98}99100/// Contents of a dynamic stack slot.101#[derive(Clone, Debug, PartialEq, Eq, Hash)]102#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]103pub struct DynamicStackSlotData {104/// The kind of stack slot.105pub kind: StackSlotKind,106107/// The type of this slot.108pub dyn_ty: DynamicType,109}110111impl DynamicStackSlotData {112/// Create a stack slot with the specified byte size.113pub fn new(kind: StackSlotKind, dyn_ty: DynamicType) -> Self {114assert!(kind == StackSlotKind::ExplicitDynamicSlot);115Self { kind, dyn_ty }116}117118/// Get the alignment in bytes of this stack slot given the stack pointer alignment.119pub fn alignment(&self, max_align: StackSize) -> StackSize {120debug_assert!(max_align.is_power_of_two());121max_align122}123}124125impl fmt::Display for DynamicStackSlotData {126fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {127write!(f, "{} {}", self.kind, self.dyn_ty)128}129}130131/// All allocated stack slots.132pub type StackSlots = PrimaryMap<StackSlot, StackSlotData>;133134/// All allocated dynamic stack slots.135pub type DynamicStackSlots = PrimaryMap<DynamicStackSlot, DynamicStackSlotData>;136137#[cfg(test)]138mod tests {139use super::*;140use crate::ir::Function;141use crate::ir::types::*;142use crate::ir::{DynamicTypeData, GlobalValueData};143use alloc::string::ToString;144145#[test]146fn stack_slot() {147let mut func = Function::new();148149let ss0 =150func.create_sized_stack_slot(StackSlotData::new(StackSlotKind::ExplicitSlot, 4, 0));151let ss1 =152func.create_sized_stack_slot(StackSlotData::new(StackSlotKind::ExplicitSlot, 8, 0));153assert_eq!(ss0.to_string(), "ss0");154assert_eq!(ss1.to_string(), "ss1");155156assert_eq!(func.sized_stack_slots[ss0].size, 4);157assert_eq!(func.sized_stack_slots[ss1].size, 8);158159assert_eq!(func.sized_stack_slots[ss0].to_string(), "explicit_slot 4");160assert_eq!(func.sized_stack_slots[ss1].to_string(), "explicit_slot 8");161}162163#[test]164fn dynamic_stack_slot() {165let mut func = Function::new();166167let int_vector_ty = I32X4;168let fp_vector_ty = F64X2;169let scale0 = GlobalValueData::DynScaleTargetConst {170vector_type: int_vector_ty,171};172let scale1 = GlobalValueData::DynScaleTargetConst {173vector_type: fp_vector_ty,174};175let gv0 = func.create_global_value(scale0);176let gv1 = func.create_global_value(scale1);177let dtd0 = DynamicTypeData::new(int_vector_ty, gv0);178let dtd1 = DynamicTypeData::new(fp_vector_ty, gv1);179let dt0 = func.dfg.make_dynamic_ty(dtd0);180let dt1 = func.dfg.make_dynamic_ty(dtd1);181182let dss0 = func.create_dynamic_stack_slot(DynamicStackSlotData::new(183StackSlotKind::ExplicitDynamicSlot,184dt0,185));186let dss1 = func.create_dynamic_stack_slot(DynamicStackSlotData::new(187StackSlotKind::ExplicitDynamicSlot,188dt1,189));190assert_eq!(dss0.to_string(), "dss0");191assert_eq!(dss1.to_string(), "dss1");192193assert_eq!(194func.dynamic_stack_slots[dss0].to_string(),195"explicit_dynamic_slot dt0"196);197assert_eq!(198func.dynamic_stack_slots[dss1].to_string(),199"explicit_dynamic_slot dt1"200);201}202}203204205