Skip to content

Commit ab47293

Browse files
authored
Merge pull request RustPython#4275 from evilpie/general-unary-not
JIT: Use boolean_val helper for unary not.
2 parents eb337e5 + 8f5ae4b commit ab47293

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

jit/src/instructions.rs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -313,31 +313,25 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
313313
}
314314
Instruction::UnaryOperation { op, .. } => {
315315
let a = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;
316-
317-
match a {
318-
JitValue::Int(val) => match op {
319-
UnaryOperator::Minus => {
320-
// Compile minus as 0 - a.
321-
let zero = self.builder.ins().iconst(types::I64, 0);
322-
let out = self.compile_sub(zero, val);
323-
self.stack.push(JitValue::Int(out));
324-
Ok(())
325-
}
326-
UnaryOperator::Plus => {
327-
// Nothing to do
328-
self.stack.push(a);
329-
Ok(())
330-
}
331-
_ => Err(JitCompileError::NotSupported),
332-
},
333-
JitValue::Bool(val) => match op {
334-
UnaryOperator::Not => {
335-
let not_val = self.builder.ins().bxor_imm(val, 1);
336-
self.stack.push(JitValue::Bool(not_val));
337-
Ok(())
338-
}
339-
_ => Err(JitCompileError::NotSupported),
340-
},
316+
match (op, a) {
317+
(UnaryOperator::Minus, JitValue::Int(val)) => {
318+
// Compile minus as 0 - a.
319+
let zero = self.builder.ins().iconst(types::I64, 0);
320+
let out = self.compile_sub(zero, val);
321+
self.stack.push(JitValue::Int(out));
322+
Ok(())
323+
}
324+
(UnaryOperator::Plus, JitValue::Int(val)) => {
325+
// Nothing to do
326+
self.stack.push(JitValue::Int(val));
327+
Ok(())
328+
}
329+
(UnaryOperator::Not, a) => {
330+
let boolean = self.boolean_val(a)?;
331+
let not_boolean = self.builder.ins().bxor_imm(boolean, 1);
332+
self.stack.push(JitValue::Bool(not_boolean));
333+
Ok(())
334+
}
341335
_ => Err(JitCompileError::NotSupported),
342336
}
343337
}

jit/tests/int_tests.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,15 @@ fn test_plus() {
187187
assert_eq!(plus(-3), Ok(-3));
188188
assert_eq!(plus(0), Ok(0));
189189
}
190+
191+
#[test]
192+
fn test_not() {
193+
let not_ = jit_function! { not_(a: i64) -> bool => r##"
194+
def not_(a: int):
195+
return not a
196+
"## };
197+
198+
assert_eq!(not_(0), Ok(true));
199+
assert_eq!(not_(1), Ok(false));
200+
assert_eq!(not_(-1), Ok(false));
201+
}

0 commit comments

Comments
 (0)