Skip to content

Commit 4db39e6

Browse files
committed
Implement more TIR data structures.
This is a companion commit to: softdevteam/ykrustc#18
1 parent 0c7016c commit 4db39e6

2 files changed

Lines changed: 114 additions & 11 deletions

File tree

ykpack/src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ pub use types::*;
4141
#[cfg(test)]
4242
mod tests {
4343
use super::{
44-
BasicBlock, Decoder, DefId, Encoder, Mir, Pack, Place, Rvalue, Statement, Terminator,
44+
BasicBlock, Decoder, DefId, Encoder, Mir, Operand, Pack, Place, PlaceBase, Rvalue,
45+
Statement, Terminator,
4546
};
4647
use fallible_iterator::{self, FallibleIterator};
4748
use std::io::{Cursor, Seek, SeekFrom};
@@ -137,10 +138,13 @@ mod tests {
137138
fn test_text_dump() {
138139
let stmts_t1_b0 = vec![
139140
Statement::Nop,
140-
Statement::Assign(Place::Local(42), Rvalue::Place(Place::Local(43))),
141141
Statement::Assign(
142-
Place::Local(44),
143-
Rvalue::Phi(vec![Place::Local(100), Place::Local(200)]),
142+
Place::Base(PlaceBase::Local(42)),
143+
Rvalue::Use(Operand::Place(Place::Base(PlaceBase::Local(43)))),
144+
),
145+
Statement::Assign(
146+
Place::Base(PlaceBase::Local(44)),
147+
Rvalue::Use(Operand::Place(Place::Base(PlaceBase::Local(300)))),
144148
),
145149
];
146150
let term_t1_b0 = Terminator::Abort;
@@ -171,8 +175,8 @@ mod tests {
171175
DefId(1, 2):
172176
bb0:
173177
Nop
174-
Assign(Local(42), Place(Local(43)))
175-
Assign(Local(44), Phi([Local(100), Local(200)]))
178+
Assign(Base(Local(42)), Use(Place(Base(Local(43)))))
179+
Assign(Base(Local(44)), Use(Place(Base(Local(300)))))
176180
term: Abort
177181
178182
bb1:

ykpack/src/types.rs

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@
1010
//! Types for the Yorick intermediate language.
1111
1212
use serde::{Deserialize, Serialize};
13-
use std::fmt::{self, Display};
13+
use std::{
14+
fmt::{self, Display},
15+
marker::PhantomData,
16+
};
1417

1518
pub type CrateHash = u64;
1619
pub type DefIndex = u32;
1720
pub type BasicBlockIndex = u32;
1821
pub type LocalIndex = u32;
22+
pub type VariantIndex = u32;
23+
pub type PromotedIndex = u32;
1924

2025
/// A mirror of the compiler's notion of a "definition ID".
2126
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
@@ -93,7 +98,8 @@ impl Display for BasicBlock {
9398
pub enum Statement {
9499
Nop,
95100
Assign(Place, Rvalue),
96-
Unimplemented, // FIXME
101+
SetDiscriminant(Place, VariantIndex),
102+
Unimplemented,
97103
}
98104

99105
impl Display for Statement {
@@ -102,19 +108,112 @@ impl Display for Statement {
102108
}
103109
}
104110

111+
/// A place for storing things.
105112
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
106113
pub enum Place {
114+
Base(PlaceBase),
115+
Projection(PlaceProjection),
116+
}
117+
118+
/// The "base" of a place projection.
119+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
120+
pub enum PlaceBase {
107121
Local(LocalIndex),
108-
Unimplemented, // FIXME
122+
Static(DefId),
123+
Promoted(PromotedIndex),
109124
}
110125

126+
/// A projection (deref, index, field access, ...).
111127
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
112-
pub enum Rvalue {
128+
pub struct PlaceProjection {
129+
pub base: Box<Place>,
130+
pub elem: ProjectionElem<LocalIndex>,
131+
}
132+
133+
/// Describes a projection operation upon a projection base.
134+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
135+
pub enum ProjectionElem<V> {
136+
Unimplemented(PhantomData<V>), // FIXME
137+
}
138+
139+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
140+
pub enum Operand {
141+
/// In MIR this is either Move or Copy.
113142
Place(Place),
114-
Phi(Vec<Place>),
143+
Unimplemented, // FIXME constants
144+
}
145+
146+
/// Borrow descriptions.
147+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
148+
pub enum BorrowKind {
149+
Shared,
150+
Shallow,
151+
Unique,
152+
Mut, // FIXME two_phase borrow.
153+
}
154+
155+
/// Things that can appear on the right-hand side of an assignment.
156+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
157+
pub enum Rvalue {
158+
Use(Operand),
159+
Repeat(Operand, u64),
160+
Ref(BorrowKind, Place), // We do not store the region.
161+
Len(Place),
162+
BinaryOp(BinOp, Operand, Operand),
163+
CheckedBinaryOp(BinOp, Operand, Operand),
164+
NullaryOp(NullOp),
165+
UnaryOp(UnOp, Operand),
166+
Discriminant(Place),
167+
Aggregate(AggregateKind, Vec<Operand>),
115168
Unimplemented, // FIXME
116169
}
117170

171+
/// Kinds of aggregate types.
172+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
173+
pub enum AggregateKind {
174+
Array,
175+
Tuple,
176+
Closure(DefId),
177+
Generator(DefId),
178+
Unimplemented,
179+
}
180+
181+
/// Binary operations.
182+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
183+
pub enum BinOp {
184+
Add,
185+
Sub,
186+
Mul,
187+
Div,
188+
Rem,
189+
BitXor,
190+
BitAnd,
191+
BitOr,
192+
Shl,
193+
Shr,
194+
Eq,
195+
Lt,
196+
Le,
197+
Ne,
198+
Ge,
199+
Gt,
200+
Offset,
201+
}
202+
203+
// Operations with no arguments.
204+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
205+
pub enum NullOp {
206+
SizeOf,
207+
Box,
208+
}
209+
210+
// Unary operations.
211+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
212+
pub enum UnOp {
213+
Not,
214+
Neg,
215+
}
216+
118217
/// A call target.
119218
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
120219
pub enum CallOperand {

0 commit comments

Comments
 (0)