|
1 | 1 | use cranelift::prelude::*; |
2 | 2 | use num_traits::cast::ToPrimitive; |
3 | 3 | use rustpython_bytecode::bytecode::{ |
4 | | - BinaryOperator, CodeObject, ComparisonOperator, Constant, Instruction, Label, NameScope, |
| 4 | + UnaryOperator, BinaryOperator, CodeObject, ComparisonOperator, Constant, Instruction, Label, NameScope, |
5 | 5 | }; |
6 | 6 | use std::collections::HashMap; |
7 | 7 |
|
@@ -233,6 +233,36 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> { |
233 | 233 | _ => Err(JitCompileError::NotSupported), |
234 | 234 | } |
235 | 235 | } |
| 236 | + Instruction::UnaryOperation { op, .. } => { |
| 237 | + let a = self.stack.pop().ok_or(JitCompileError::BadBytecode)?; |
| 238 | + |
| 239 | + match a.ty { |
| 240 | + JitType::Int => match op { |
| 241 | + UnaryOperator::Minus => { |
| 242 | + // Compile minus as 0 - a. |
| 243 | + let zero = self.builder.ins().iconst(types::I64, 0); |
| 244 | + let (out, carry) = self.builder.ins().isub_ifbout(zero, a.val); |
| 245 | + self.builder.ins().trapif( |
| 246 | + IntCC::Overflow, |
| 247 | + carry, |
| 248 | + TrapCode::IntegerOverflow, |
| 249 | + ); |
| 250 | + self.stack.push(JitValue { |
| 251 | + val: out, |
| 252 | + ty: JitType::Int, |
| 253 | + }); |
| 254 | + Ok(()) |
| 255 | + } |
| 256 | + UnaryOperator::Plus => { |
| 257 | + // Nothing to do |
| 258 | + self.stack.push(a); |
| 259 | + Ok(()) |
| 260 | + } |
| 261 | + _ => Err(JitCompileError::NotSupported), |
| 262 | + }, |
| 263 | + _ => Err(JitCompileError::NotSupported), |
| 264 | + } |
| 265 | + } |
236 | 266 | Instruction::BinaryOperation { op, .. } => { |
237 | 267 | // the rhs is popped off first |
238 | 268 | let b = self.stack.pop().ok_or(JitCompileError::BadBytecode)?; |
|
0 commit comments