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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
use super::{LocalIdx, StackOperand, StackPos};
use crate::{
Error,
ValType,
core::{RawVal, TypedRawVal},
engine::translator::{
func::{layout::StackLayout, stack::Allocation},
utils::required_cells_for_ty,
},
ir::{BoundedSlotSpan, Slot, SlotSpan},
};
#[cfg(doc)]
use super::Stack;
/// The location of an operand.
#[derive(Debug, Copy, Clone)]
pub enum Location {
/// The operand's location is a register.
Reg(ValType),
/// The operand's location is a slot.
Slot(Slot),
}
#[derive(Debug, Copy, Clone)]
pub enum ResolvedOperand<T> {
/// The operand is a register.
Reg(ValType),
/// The operand is located in a [`Slot`].
Slot(Slot),
/// The operand is an immediate value of type `T`.
Immediate(T),
}
impl<T> From<Location> for ResolvedOperand<T> {
fn from(location: Location) -> Self {
match location {
Location::Reg(ty) => Self::Reg(ty),
Location::Slot(slot) => Self::Slot(slot),
}
}
}
impl<T> ResolvedOperand<T> {
pub fn sort(a: Self, b: Self) -> (Self, Self) {
match (&a, &b) {
| (Self::Slot(_), Self::Reg(_))
| (Self::Immediate(_), Self::Reg(_))
| (Self::Immediate(_), Self::Slot(_)) => (b, a),
_ => (a, b),
}
}
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ResolvedOperand<U> {
match self {
Self::Reg(ty) => ResolvedOperand::Reg(ty),
Self::Slot(slot) => ResolvedOperand::Slot(slot),
Self::Immediate(value) => ResolvedOperand::Immediate(f(value)),
}
}
pub fn filter_map<U>(self, f: impl FnOnce(T) -> Option<U>) -> Option<ResolvedOperand<U>> {
self.map(f).transpose()
}
}
impl<T> ResolvedOperand<Option<T>> {
/// Transposes a [`ResolvedOperand<Option<T>>`] into an [`Option<ResolvedOperand<T>>`].
pub fn transpose(self) -> Option<ResolvedOperand<T>> {
let resolved = match self {
Self::Reg(ty) => ResolvedOperand::Reg(ty),
Self::Slot(slot) => ResolvedOperand::Slot(slot),
Self::Immediate(ok_or_err) => ResolvedOperand::Immediate(ok_or_err?),
};
Some(resolved)
}
}
/// An operand on the [`Stack`].
#[derive(Debug, Copy, Clone)]
pub enum Operand {
/// A local variable operand.
Local(LocalOperand),
/// A temporary operand.
Temp(TempOperand),
/// An immediate value operand.
Immediate(ImmediateOperand),
}
impl Operand {
/// Creates a new [`Operand`] from the given [`StackOperand`] and its [`StackPos`].
///
/// The `in_reg` argument is a hint to tell if a [`StackOperand::Local`] is live in a register.
pub(super) fn new(stack_pos: StackPos, operand: StackOperand, in_reg: bool) -> Self {
use StackOperand as Opd;
match operand {
Opd::Temp {
temp_slots,
ty,
in_reg,
} => Self::temp(stack_pos, temp_slots, ty, in_reg),
Opd::Local {
temp_slots,
ty,
local_index,
..
} => Self::local(temp_slots, local_index, ty, in_reg),
Opd::Immediate {
ty,
temp_slots,
val,
..
} => Self::immediate(temp_slots, ty, val),
}
}
/// Returns `true` if `self` and `other` evaluate to the same value.
pub fn is_same(&self, other: &Self) -> bool {
match (self, other) {
(Self::Local(lhs), Self::Local(rhs)) => lhs.local_index() == rhs.local_index(),
(Self::Temp(lhs), Self::Temp(rhs)) => lhs.stack_pos() == rhs.stack_pos(),
(Self::Immediate(lhs), Self::Immediate(rhs)) => lhs.val() == rhs.val(),
_ => false,
}
}
/// Creates a local [`Operand`].
pub(super) fn local(
temp_slots: SlotSpan,
local_index: LocalIdx,
ty: ValType,
in_reg: bool,
) -> Self {
Self::Local(LocalOperand::new(temp_slots, ty, local_index, in_reg))
}
/// Creates a temporary [`Operand`].
pub(super) fn temp(
stack_pos: StackPos,
temp_slots: SlotSpan,
ty: ValType,
in_reg: bool,
) -> Self {
Self::Temp(TempOperand::new(temp_slots, ty, stack_pos, in_reg))
}
/// Creates an immediate [`Operand`].
pub(super) fn immediate(temp_slots: SlotSpan, ty: ValType, val: RawVal) -> Self {
Self::Immediate(ImmediateOperand::new(temp_slots, ty, val))
}
/// Returns `true` if `self` is an [`Operand::Temp`].
pub fn is_temp(&self) -> bool {
matches!(self, Self::Temp(_))
}
/// Returns the temporary [`BoundedSlotSpan`] of the [`Operand`].
///
/// # Note
///
/// This is required to copy an span of operand to its temporary [`BoundedSlotSpan`].
pub fn temp_slots(&self) -> BoundedSlotSpan {
match self {
Self::Local(operand) => operand.temp_slots(),
Self::Temp(operand) => operand.temp_slots(),
Self::Immediate(operand) => operand.temp_slots(),
}
}
/// Returns the type of the [`Operand`].
pub fn ty(&self) -> ValType {
match self {
Self::Local(operand) => operand.ty(),
Self::Temp(operand) => operand.ty(),
Self::Immediate(operand) => operand.ty(),
}
}
/// Resolves the [`Operand`] into a [`ResolvedOperand<TypedRawVal>`].
///
/// [`ResolvedOperand`] is a more destructed form which is simpler to handle,
/// especially in pattern matching contexts. However, in contrast to [`Operand`]
/// it loses some information during the conversion process.
pub fn resolve(&self, layout: &StackLayout) -> Result<ResolvedOperand<TypedRawVal>, Error> {
let resolved = match self {
Operand::Local(operand) => match operand.in_reg() {
true => ResolvedOperand::Reg(operand.ty()),
false => {
let slot = layout.local_to_slot(operand)?;
ResolvedOperand::Slot(slot)
}
},
Operand::Temp(operand) => match operand.in_reg() {
true => ResolvedOperand::Reg(operand.ty()),
false => {
let slot = operand.temp_slots().head();
ResolvedOperand::Slot(slot)
}
},
Operand::Immediate(operand) => ResolvedOperand::Immediate(operand.val()),
};
Ok(resolved)
}
/// Resolves the [`Operand`] into a [`ResolvedOperand`].
///
/// This is a convenience wrapper for [`Self::resolve`] and a call to `map`.
pub fn resolve_as<T>(&self, layout: &StackLayout) -> Result<ResolvedOperand<T>, Error>
where
T: From<TypedRawVal>,
{
Ok(self.resolve(layout)?.map(T::from))
}
/// Returns `true` if the operand is stored in a register.
pub fn in_reg(&self) -> bool {
match self {
Operand::Local(operand) => operand.in_reg(),
Operand::Temp(operand) => operand.in_reg(),
Operand::Immediate(_) => false,
}
}
}
/// A local variable on the stack.
#[derive(Debug, Copy, Clone)]
pub struct LocalOperand {
/// The temporary [`SlotSpan`] of the local operand.
temp_slots: SlotSpan,
/// The type of the local variable.
ty: ValType,
/// This is `true` if the operand is stored in a register.
in_reg: bool,
/// The index of the local variable.
local_index: LocalIdx,
}
impl From<LocalOperand> for Operand {
fn from(operand: LocalOperand) -> Self {
Self::Local(operand)
}
}
impl LocalOperand {
/// Creates a new [`LocalOperand`] from its parts.
pub(super) fn new(
temp_slots: SlotSpan,
ty: ValType,
local_index: LocalIdx,
in_reg: bool,
) -> Self {
Self {
temp_slots,
ty,
in_reg,
local_index,
}
}
/// Returns the temporary [`BoundedSlotSpan`] of the [`LocalOperand`].
pub fn temp_slots(&self) -> BoundedSlotSpan {
let len = required_cells_for_ty(self.ty());
BoundedSlotSpan::new(self.temp_slots, len)
}
/// Returns the index of the [`LocalOperand`].
pub fn local_index(&self) -> LocalIdx {
self.local_index
}
/// Returns the type of the [`LocalOperand`].
pub fn ty(&self) -> ValType {
self.ty
}
/// Returns `true` if the operand is stored in a register.
pub fn in_reg(&self) -> bool {
self.in_reg
}
}
/// A temporary on the [`Stack`].
#[derive(Debug, Copy, Clone)]
pub struct TempOperand {
/// The temporary [`SlotSpan`] of the local operand.
temp_slots: SlotSpan,
/// The type of the temporary.
ty: ValType,
/// The position of the operand on the operand stack.
stack_pos: StackPos,
/// This is `true` if the operand is stored in a register.
in_reg: bool,
}
impl From<TempOperand> for Operand {
fn from(operand: TempOperand) -> Self {
Self::Temp(operand)
}
}
impl TempOperand {
/// Creates a new [`TempOperand`] from its parts.
pub(super) fn new(
temp_slots: SlotSpan,
ty: ValType,
stack_pos: StackPos,
in_reg: bool,
) -> Self {
Self {
temp_slots,
ty,
stack_pos,
in_reg,
}
}
/// Returns the stack position of the [`TempOperand`].
fn stack_pos(&self) -> StackPos {
self.stack_pos
}
/// Returns the temporary [`BoundedSlotSpan`] of the [`TempOperand`].
pub fn temp_slots(&self) -> BoundedSlotSpan {
let len = required_cells_for_ty(self.ty());
BoundedSlotSpan::new(self.temp_slots, len)
}
/// Returns the type of the [`TempOperand`].
pub fn ty(&self) -> ValType {
self.ty
}
/// Returns `true` if the operand is stored in a register.
pub fn in_reg(&self) -> bool {
self.in_reg
}
/// Returns the associated [`Allocation`] of `self.`
pub fn alloc(&self) -> Allocation {
match self.in_reg() {
true => Allocation::Reg,
false => Allocation::None,
}
}
}
/// An immediate value on the [`Stack`].
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ImmediateOperand {
/// The temporary [`SlotSpan`] of the local operand.
temp_slots: SlotSpan,
/// The type of the immediate value.
ty: ValType,
/// The value of the immediate value.
val: RawVal,
}
impl From<ImmediateOperand> for Operand {
fn from(operand: ImmediateOperand) -> Self {
Self::Immediate(operand)
}
}
impl ImmediateOperand {
/// Creates a new [`ImmediateOperand`] from its parts.
pub(super) fn new(temp_slots: SlotSpan, ty: ValType, val: RawVal) -> Self {
Self {
temp_slots,
ty,
val,
}
}
/// Returns the temporary [`Slot`](crate::ir::BoundedSlotSpan) of the [`ImmediateOperand`].
pub fn temp_slots(&self) -> BoundedSlotSpan {
let len = required_cells_for_ty(self.ty());
BoundedSlotSpan::new(self.temp_slots, len)
}
/// Returns the immediate value (and its type) of the [`ImmediateOperand`].
pub fn val(&self) -> TypedRawVal {
TypedRawVal::new(self.ty, self.val)
}
/// Returns the type of the [`ImmediateOperand`].
pub fn ty(&self) -> ValType {
self.ty
}
}
impl AsRef<Operand> for Operand {
fn as_ref(&self) -> &Operand {
self
}
}