Path: blob/main/cranelift/codegen/src/ir/stackslot.rs
3073 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,7172/// Opaque stackslot metadata handle, passed through to73/// compilation result metadata describing stackslot location.74///75/// In the face of compiler transforms like inlining that may move76/// stackslots between functions, when an embedder wants to77/// externally observe stackslots, it needs a first-class way for78/// the identity of stackslots to be carried along with the IR79/// entities. This opaque `StackSlotKey` allows the embedder to do80/// so.81pub key: Option<StackSlotKey>,82}8384/// An opaque key uniquely identifying a stack slot.85#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]86#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]87pub struct StackSlotKey(u64);88impl StackSlotKey {89/// Construct a [`StackSlotKey`] from raw bits.90///91/// An embedder can use any 64-bit value to describe a stack slot;92/// there are no restrictions, and the value does not mean93/// anything to Cranelift itself.94pub fn new(value: u64) -> StackSlotKey {95StackSlotKey(value)96}9798/// Get the raw bits from the [`StackSlotKey`].99pub fn bits(&self) -> u64 {100self.0101}102}103104impl StackSlotData {105/// Create a stack slot with the specified byte size and alignment.106pub fn new(kind: StackSlotKind, size: StackSize, align_shift: u8) -> Self {107Self {108kind,109size,110align_shift,111key: None,112}113}114115/// Create a stack slot with the specified byte size and alignment116/// and the given user-defined key.117pub fn new_with_key(118kind: StackSlotKind,119size: StackSize,120align_shift: u8,121key: StackSlotKey,122) -> Self {123Self {124kind,125size,126align_shift,127key: Some(key),128}129}130}131132impl fmt::Display for StackSlotData {133fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {134let align_shift = if self.align_shift != 0 {135format!(", align = {}", 1u32 << self.align_shift)136} else {137"".into()138};139let key = match self.key {140Some(value) => format!(", key = {}", value.bits()),141None => "".into(),142};143144write!(f, "{} {}{align_shift}{key}", self.kind, self.size)145}146}147148/// Contents of a dynamic stack slot.149#[derive(Clone, Debug, PartialEq, Eq, Hash)]150#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]151pub struct DynamicStackSlotData {152/// The kind of stack slot.153pub kind: StackSlotKind,154155/// The type of this slot.156pub dyn_ty: DynamicType,157}158159impl DynamicStackSlotData {160/// Create a stack slot with the specified byte size.161pub fn new(kind: StackSlotKind, dyn_ty: DynamicType) -> Self {162assert!(kind == StackSlotKind::ExplicitDynamicSlot);163Self { kind, dyn_ty }164}165166/// Get the alignment in bytes of this stack slot given the stack pointer alignment.167pub fn alignment(&self, max_align: StackSize) -> StackSize {168debug_assert!(max_align.is_power_of_two());169max_align170}171}172173impl fmt::Display for DynamicStackSlotData {174fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {175write!(f, "{} {}", self.kind, self.dyn_ty)176}177}178179/// All allocated stack slots.180pub type StackSlots = PrimaryMap<StackSlot, StackSlotData>;181182/// All allocated dynamic stack slots.183pub type DynamicStackSlots = PrimaryMap<DynamicStackSlot, DynamicStackSlotData>;184185#[cfg(test)]186mod tests {187use super::*;188use crate::ir::Function;189use crate::ir::types::*;190use crate::ir::{DynamicTypeData, GlobalValueData};191use alloc::string::ToString;192193#[test]194fn stack_slot() {195let mut func = Function::new();196197let ss0 =198func.create_sized_stack_slot(StackSlotData::new(StackSlotKind::ExplicitSlot, 4, 0));199let ss1 =200func.create_sized_stack_slot(StackSlotData::new(StackSlotKind::ExplicitSlot, 8, 0));201assert_eq!(ss0.to_string(), "ss0");202assert_eq!(ss1.to_string(), "ss1");203204assert_eq!(func.sized_stack_slots[ss0].size, 4);205assert_eq!(func.sized_stack_slots[ss1].size, 8);206207assert_eq!(func.sized_stack_slots[ss0].to_string(), "explicit_slot 4");208assert_eq!(func.sized_stack_slots[ss1].to_string(), "explicit_slot 8");209}210211#[test]212fn dynamic_stack_slot() {213let mut func = Function::new();214215let int_vector_ty = I32X4;216let fp_vector_ty = F64X2;217let scale0 = GlobalValueData::DynScaleTargetConst {218vector_type: int_vector_ty,219};220let scale1 = GlobalValueData::DynScaleTargetConst {221vector_type: fp_vector_ty,222};223let gv0 = func.create_global_value(scale0);224let gv1 = func.create_global_value(scale1);225let dtd0 = DynamicTypeData::new(int_vector_ty, gv0);226let dtd1 = DynamicTypeData::new(fp_vector_ty, gv1);227let dt0 = func.dfg.make_dynamic_ty(dtd0);228let dt1 = func.dfg.make_dynamic_ty(dtd1);229230let dss0 = func.create_dynamic_stack_slot(DynamicStackSlotData::new(231StackSlotKind::ExplicitDynamicSlot,232dt0,233));234let dss1 = func.create_dynamic_stack_slot(DynamicStackSlotData::new(235StackSlotKind::ExplicitDynamicSlot,236dt1,237));238assert_eq!(dss0.to_string(), "dss0");239assert_eq!(dss1.to_string(), "dss1");240241assert_eq!(242func.dynamic_stack_slots[dss0].to_string(),243"explicit_dynamic_slot dt0"244);245assert_eq!(246func.dynamic_stack_slots[dss1].to_string(),247"explicit_dynamic_slot dt1"248);249}250}251252253