Skip to content

Commit 5760afc

Browse files
bors[bot]vext01
andcommitted
Merge #3
3: Implement more TIR data structures. r=ltratt a=vext01 This is a companion commit to: softdevteam/ykrustc#18 Co-authored-by: Edd Barrett <vext01@gmail.com>
2 parents 65701a5 + adc81fe commit 5760afc

2 files changed

Lines changed: 121 additions & 22 deletions

File tree

ykpack/src/lib.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub use types::*;
4242
mod tests {
4343
use super::{
4444
BasicBlock, Decoder, DefId, Encoder, Mir, Pack, Place, Rvalue, Statement, Terminator,
45+
PlaceBase, Operand
4546
};
4647
use fallible_iterator::{self, FallibleIterator};
4748
use std::io::{Cursor, Seek, SeekFrom};
@@ -137,10 +138,11 @@ 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))),
141+
Statement::Assign(Place::Base(PlaceBase::Local(42)),
142+
Rvalue::Use(Operand::Place(Place::Base(PlaceBase::Local(43))))),
141143
Statement::Assign(
142-
Place::Local(44),
143-
Rvalue::Phi(vec![Place::Local(100), Place::Local(200)]),
144+
Place::Base(PlaceBase::Local(44)),
145+
Rvalue::Use(Operand::Place(Place::Base(PlaceBase::Local(300)))),
144146
),
145147
];
146148
let term_t1_b0 = Terminator::Abort;
@@ -168,21 +170,21 @@ mod tests {
168170
let got_lines = got.split("\n");
169171

170172
let expect = "[Begin TIR for item1]\n\
171-
DefId(1, 2):
172-
bb0:
173-
Nop
174-
Assign(Local(42), Place(Local(43)))
175-
Assign(Local(44), Phi([Local(100), Local(200)]))
176-
term: Abort
177-
178-
bb1:
179-
Unimplemented
180-
term: Goto { target_bb: 50 }
181-
182-
[End TIR for item1]
183-
[Begin TIR for item2]
184-
DefId(3, 4):
185-
[End TIR for item2]\n";
173+
DefId(1, 2):
174+
bb0:
175+
Nop
176+
Assign(Base(Local(42)), Use(Place(Base(Local(43)))))
177+
Assign(Base(Local(44)), Use(Place(Base(Local(300)))))
178+
term: Abort
179+
180+
bb1:
181+
Unimplemented
182+
term: Goto { target_bb: 50 }
183+
184+
[End TIR for item1]
185+
[Begin TIR for item2]
186+
DefId(3, 4):
187+
[End TIR for item2]\n";
186188

187189
let expect_lines = expect.split("\n");
188190

ykpack/src/types.rs

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
1212
use serde::{Deserialize, Serialize};
1313
use std::fmt::{self, Display};
14+
use std::marker::PhantomData;
1415

1516
pub type CrateHash = u64;
1617
pub type DefIndex = u32;
1718
pub type BasicBlockIndex = u32;
1819
pub type LocalIndex = u32;
20+
pub type VariantIndex = u32;
21+
pub type PromotedIndex = u32;
1922

2023
/// A mirror of the compiler's notion of a "definition ID".
2124
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
@@ -93,7 +96,8 @@ impl Display for BasicBlock {
9396
pub enum Statement {
9497
Nop,
9598
Assign(Place, Rvalue),
96-
Unimplemented, // FIXME
99+
SetDiscriminant(Place, VariantIndex),
100+
Unimplemented,
97101
}
98102

99103
impl Display for Statement {
@@ -102,19 +106,112 @@ impl Display for Statement {
102106
}
103107
}
104108

109+
/// A place for storing things.
105110
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
106111
pub enum Place {
112+
Base(PlaceBase),
113+
Projection(PlaceProjection),
114+
}
115+
116+
/// The "base" of a place projection.
117+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
118+
pub enum PlaceBase {
107119
Local(LocalIndex),
108-
Unimplemented, // FIXME
120+
Static(DefId),
121+
Promoted(PromotedIndex),
109122
}
110123

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

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

0 commit comments

Comments
 (0)