1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use crate::entity::PrimaryMap;
use crate::ir::entities::{DynamicStackSlot, DynamicType};
use crate::ir::StackSlot;
use core::fmt;
use core::str::FromStr;
#[allow(unused_imports)]
use crate::ir::{DynamicTypeData, GlobalValueData};
#[allow(unused_imports)]
use crate::ir::types::*;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
pub type StackSize = u32;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub enum StackSlotKind {
ExplicitSlot,
ExplicitDynamicSlot,
}
impl FromStr for StackSlotKind {
type Err = ();
fn from_str(s: &str) -> Result<Self, ()> {
use self::StackSlotKind::*;
match s {
"explicit_slot" => Ok(ExplicitSlot),
"explicit_dynamic_slot" => Ok(ExplicitDynamicSlot),
_ => Err(()),
}
}
}
impl fmt::Display for StackSlotKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::StackSlotKind::*;
f.write_str(match *self {
ExplicitSlot => "explicit_slot",
ExplicitDynamicSlot => "explicit_dynamic_slot",
})
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct StackSlotData {
pub kind: StackSlotKind,
pub size: StackSize,
}
impl StackSlotData {
pub fn new(kind: StackSlotKind, size: StackSize) -> Self {
Self { kind, size }
}
pub fn alignment(&self, max_align: StackSize) -> StackSize {
debug_assert!(max_align.is_power_of_two());
if self.kind == StackSlotKind::ExplicitDynamicSlot {
max_align
} else {
let x = self.size | max_align;
x & x.wrapping_neg()
}
}
}
impl fmt::Display for StackSlotData {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}", self.kind, self.size)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct DynamicStackSlotData {
pub kind: StackSlotKind,
pub dyn_ty: DynamicType,
}
impl DynamicStackSlotData {
pub fn new(kind: StackSlotKind, dyn_ty: DynamicType) -> Self {
assert!(kind == StackSlotKind::ExplicitDynamicSlot);
Self { kind, dyn_ty }
}
pub fn alignment(&self, max_align: StackSize) -> StackSize {
debug_assert!(max_align.is_power_of_two());
max_align
}
}
impl fmt::Display for DynamicStackSlotData {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}", self.kind, self.dyn_ty)
}
}
pub type StackSlots = PrimaryMap<StackSlot, StackSlotData>;
pub type DynamicStackSlots = PrimaryMap<DynamicStackSlot, DynamicStackSlotData>;
#[cfg(test)]
mod tests {
use super::*;
use crate::ir::Function;
use alloc::string::ToString;
#[test]
fn stack_slot() {
let mut func = Function::new();
let ss0 = func.create_sized_stack_slot(StackSlotData::new(StackSlotKind::ExplicitSlot, 4));
let ss1 = func.create_sized_stack_slot(StackSlotData::new(StackSlotKind::ExplicitSlot, 8));
assert_eq!(ss0.to_string(), "ss0");
assert_eq!(ss1.to_string(), "ss1");
assert_eq!(func.sized_stack_slots[ss0].size, 4);
assert_eq!(func.sized_stack_slots[ss1].size, 8);
assert_eq!(func.sized_stack_slots[ss0].to_string(), "explicit_slot 4");
assert_eq!(func.sized_stack_slots[ss1].to_string(), "explicit_slot 8");
}
#[test]
fn dynamic_stack_slot() {
let mut func = Function::new();
let int_vector_ty = I32X4;
let fp_vector_ty = F64X2;
let scale0 = GlobalValueData::DynScaleTargetConst {
vector_type: int_vector_ty,
};
let scale1 = GlobalValueData::DynScaleTargetConst {
vector_type: fp_vector_ty,
};
let gv0 = func.create_global_value(scale0);
let gv1 = func.create_global_value(scale1);
let dtd0 = DynamicTypeData::new(int_vector_ty, gv0);
let dtd1 = DynamicTypeData::new(fp_vector_ty, gv1);
let dt0 = func.dfg.make_dynamic_ty(dtd0);
let dt1 = func.dfg.make_dynamic_ty(dtd1);
let dss0 = func.create_dynamic_stack_slot(DynamicStackSlotData::new(
StackSlotKind::ExplicitDynamicSlot,
dt0,
));
let dss1 = func.create_dynamic_stack_slot(DynamicStackSlotData::new(
StackSlotKind::ExplicitDynamicSlot,
dt1,
));
assert_eq!(dss0.to_string(), "dss0");
assert_eq!(dss1.to_string(), "dss1");
assert_eq!(
func.dynamic_stack_slots[dss0].to_string(),
"explicit_dynamic_slot dt0"
);
assert_eq!(
func.dynamic_stack_slots[dss1].to_string(),
"explicit_dynamic_slot dt1"
);
}
#[test]
fn alignment() {
let slot = StackSlotData::new(StackSlotKind::ExplicitSlot, 8);
assert_eq!(slot.alignment(4), 4);
assert_eq!(slot.alignment(8), 8);
assert_eq!(slot.alignment(16), 8);
let slot2 = StackSlotData::new(StackSlotKind::ExplicitSlot, 24);
assert_eq!(slot2.alignment(4), 4);
assert_eq!(slot2.alignment(8), 8);
assert_eq!(slot2.alignment(16), 8);
assert_eq!(slot2.alignment(32), 8);
}
}