(
&mut self,
func_ref: FuncRef,
bytecode: &CodeObject<C>,
offset: u32,
instruction: Instruction,
arg: OpArg,
)
| 351 | } |
| 352 | |
| 353 | pub fn add_instruction<C: bytecode::Constant>( |
| 354 | &mut self, |
| 355 | func_ref: FuncRef, |
| 356 | bytecode: &CodeObject<C>, |
| 357 | offset: u32, |
| 358 | instruction: Instruction, |
| 359 | arg: OpArg, |
| 360 | ) -> Result<(), JitCompileError> { |
| 361 | match instruction { |
| 362 | Instruction::BinaryOp { op } => { |
| 363 | let op = op.get(arg); |
| 364 | // the rhs is popped off first |
| 365 | let b = self.stack.pop().ok_or(JitCompileError::BadBytecode)?; |
| 366 | let a = self.stack.pop().ok_or(JitCompileError::BadBytecode)?; |
| 367 | |
| 368 | let a_type = a.to_jit_type(); |
| 369 | let b_type = b.to_jit_type(); |
| 370 | |
| 371 | let val = match (op, a, b) { |
| 372 | ( |
| 373 | BinaryOperator::Add | BinaryOperator::InplaceAdd, |
| 374 | JitValue::Int(a), |
| 375 | JitValue::Int(b), |
| 376 | ) => { |
| 377 | let (out, carry) = self.builder.ins().sadd_overflow(a, b); |
| 378 | self.builder.ins().trapnz(carry, TrapCode::INTEGER_OVERFLOW); |
| 379 | JitValue::Int(out) |
| 380 | } |
| 381 | ( |
| 382 | BinaryOperator::Subtract | BinaryOperator::InplaceSubtract, |
| 383 | JitValue::Int(a), |
| 384 | JitValue::Int(b), |
| 385 | ) => JitValue::Int(self.compile_sub(a, b)), |
| 386 | ( |
| 387 | BinaryOperator::FloorDivide | BinaryOperator::InplaceFloorDivide, |
| 388 | JitValue::Int(a), |
| 389 | JitValue::Int(b), |
| 390 | ) => JitValue::Int(self.builder.ins().sdiv(a, b)), |
| 391 | ( |
| 392 | BinaryOperator::TrueDivide | BinaryOperator::InplaceTrueDivide, |
| 393 | JitValue::Int(a), |
| 394 | JitValue::Int(b), |
| 395 | ) => { |
| 396 | // Check if b == 0, If so trap with a division by zero error |
| 397 | self.builder |
| 398 | .ins() |
| 399 | .trapz(b, TrapCode::INTEGER_DIVISION_BY_ZERO); |
| 400 | // Else convert to float and divide |
| 401 | let a_float = self.builder.ins().fcvt_from_sint(types::F64, a); |
| 402 | let b_float = self.builder.ins().fcvt_from_sint(types::F64, b); |
| 403 | JitValue::Float(self.builder.ins().fdiv(a_float, b_float)) |
| 404 | } |
| 405 | ( |
| 406 | BinaryOperator::Multiply | BinaryOperator::InplaceMultiply, |
| 407 | JitValue::Int(a), |
| 408 | JitValue::Int(b), |
| 409 | ) => JitValue::Int(self.builder.ins().imul(a, b)), |
| 410 | ( |
no test coverage detected