Skip to content

Commit d3cfe97

Browse files
committed
Custom MIR: Support binary and unary operations
1 parent 4c83bd0 commit d3cfe97

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

compiler/rustc_mir_build/src/build/custom/parse/instruction.rs

+6
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
141141
ExprKind::AddressOf { mutability, arg } => Ok(
142142
Rvalue::AddressOf(*mutability, self.parse_place(*arg)?)
143143
),
144+
ExprKind::Binary { op, lhs, rhs } => Ok(
145+
Rvalue::BinaryOp(*op, Box::new((self.parse_operand(*lhs)?, self.parse_operand(*rhs)?)))
146+
),
147+
ExprKind::Unary { op, arg } => Ok(
148+
Rvalue::UnaryOp(*op, self.parse_operand(*arg)?)
149+
),
144150
_ => self.parse_operand(expr_id).map(Rvalue::Use),
145151
)
146152
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// MIR for `f` after built
2+
3+
fn f(_1: i32, _2: bool) -> i32 {
4+
let mut _0: i32; // return place in scope 0 at $DIR/operators.rs:+0:30: +0:33
5+
6+
bb0: {
7+
_1 = Neg(_1); // scope 0 at $DIR/operators.rs:+2:9: +2:15
8+
_2 = Not(_2); // scope 0 at $DIR/operators.rs:+3:9: +3:15
9+
_1 = Add(_1, _1); // scope 0 at $DIR/operators.rs:+4:9: +4:18
10+
_1 = Sub(_1, _1); // scope 0 at $DIR/operators.rs:+5:9: +5:18
11+
_1 = Mul(_1, _1); // scope 0 at $DIR/operators.rs:+6:9: +6:18
12+
_1 = Div(_1, _1); // scope 0 at $DIR/operators.rs:+7:9: +7:18
13+
_1 = Rem(_1, _1); // scope 0 at $DIR/operators.rs:+8:9: +8:18
14+
_1 = BitXor(_1, _1); // scope 0 at $DIR/operators.rs:+9:9: +9:18
15+
_1 = BitAnd(_1, _1); // scope 0 at $DIR/operators.rs:+10:9: +10:18
16+
_1 = Shl(_1, _1); // scope 0 at $DIR/operators.rs:+11:9: +11:19
17+
_1 = Shr(_1, _1); // scope 0 at $DIR/operators.rs:+12:9: +12:19
18+
_2 = Eq(_1, _1); // scope 0 at $DIR/operators.rs:+13:9: +13:19
19+
_2 = Lt(_1, _1); // scope 0 at $DIR/operators.rs:+14:9: +14:18
20+
_2 = Le(_1, _1); // scope 0 at $DIR/operators.rs:+15:9: +15:19
21+
_2 = Ge(_1, _1); // scope 0 at $DIR/operators.rs:+16:9: +16:19
22+
_2 = Gt(_1, _1); // scope 0 at $DIR/operators.rs:+17:9: +17:18
23+
_0 = _1; // scope 0 at $DIR/operators.rs:+18:9: +18:16
24+
return; // scope 0 at $DIR/operators.rs:+19:9: +19:17
25+
}
26+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// compile-flags: --crate-type=lib
2+
#![feature(custom_mir, core_intrinsics, inline_const)]
3+
use std::intrinsics::mir::*;
4+
5+
// EMIT_MIR operators.f.built.after.mir
6+
#[custom_mir(dialect = "built")]
7+
pub fn f(a: i32, b: bool) -> i32 {
8+
mir!({
9+
a = -a;
10+
b = !b;
11+
a = a + a;
12+
a = a - a;
13+
a = a * a;
14+
a = a / a;
15+
a = a % a;
16+
a = a ^ a;
17+
a = a & a;
18+
a = a << a;
19+
a = a >> a;
20+
b = a == a;
21+
b = a < a;
22+
b = a <= a;
23+
b = a >= a;
24+
b = a > a;
25+
RET = a;
26+
Return()
27+
})
28+
}

0 commit comments

Comments
 (0)