//! Простые инструкции без ссылок на пул (кроме `ldc`) и без ветвлений. use ristretto_classfile::ConstantPool; use ristretto_classfile::attributes::Instruction; use super::super::pool::const_from; use crate::error::ClassfileError; use crate::insn::{Arith, ArrayKind, Const, Convert, Insn, Prim, SlotKind}; /// `Some` — инструкция покрыта, `None` — отдать следующим конвертерам. pub(super) fn convert_basic( instruction: &Instruction, pool: &ConstantPool<'_>, ) -> Result, ClassfileError> { Ok(Some(match instruction { Instruction::Nop => Insn::Nop, Instruction::Aconst_null => Insn::AconstNull, Instruction::Iconst_m1 => Insn::Const(Const::Int(-1)), Instruction::Iconst_0 => Insn::Const(Const::Int(0)), Instruction::Iconst_1 => Insn::Const(Const::Int(1)), Instruction::Iconst_2 => Insn::Const(Const::Int(2)), Instruction::Iconst_3 => Insn::Const(Const::Int(3)), Instruction::Iconst_4 => Insn::Const(Const::Int(4)), Instruction::Iconst_5 => Insn::Const(Const::Int(5)), Instruction::Lconst_0 => Insn::Const(Const::Long(0)), Instruction::Lconst_1 => Insn::Const(Const::Long(1)), Instruction::Fconst_0 => Insn::Const(Const::Float(0.0)), Instruction::Fconst_1 => Insn::Const(Const::Float(1.0)), Instruction::Fconst_2 => Insn::Const(Const::Float(2.0)), Instruction::Dconst_0 => Insn::Const(Const::Double(0.0)), Instruction::Dconst_1 => Insn::Const(Const::Double(1.0)), Instruction::Bipush(v) => Insn::Const(Const::Int(i32::from(*v))), Instruction::Sipush(v) => Insn::Const(Const::Int(i32::from(*v))), Instruction::Ldc(i) => Insn::Const(const_from(pool, u16::from(*i))?), Instruction::Ldc_w(i) | Instruction::Ldc2_w(i) => Insn::Const(const_from(pool, *i)?), Instruction::Iload(i) => load(u16::from(*i), SlotKind::Int), Instruction::Lload(i) => load(u16::from(*i), SlotKind::Long), Instruction::Fload(i) => load(u16::from(*i), SlotKind::Float), Instruction::Dload(i) => load(u16::from(*i), SlotKind::Double), Instruction::Aload(i) => load(u16::from(*i), SlotKind::Ref), Instruction::Iload_w(i) => load(*i, SlotKind::Int), Instruction::Lload_w(i) => load(*i, SlotKind::Long), Instruction::Fload_w(i) => load(*i, SlotKind::Float), Instruction::Dload_w(i) => load(*i, SlotKind::Double), Instruction::Aload_w(i) => load(*i, SlotKind::Ref), Instruction::Iload_0 => load(0, SlotKind::Int), Instruction::Iload_1 => load(1, SlotKind::Int), Instruction::Iload_2 => load(2, SlotKind::Int), Instruction::Iload_3 => load(3, SlotKind::Int), Instruction::Lload_0 => load(0, SlotKind::Long), Instruction::Lload_1 => load(1, SlotKind::Long), Instruction::Lload_2 => load(2, SlotKind::Long), Instruction::Lload_3 => load(3, SlotKind::Long), Instruction::Fload_0 => load(0, SlotKind::Float), Instruction::Fload_1 => load(1, SlotKind::Float), Instruction::Fload_2 => load(2, SlotKind::Float), Instruction::Fload_3 => load(3, SlotKind::Float), Instruction::Dload_0 => load(0, SlotKind::Double), Instruction::Dload_1 => load(1, SlotKind::Double), Instruction::Dload_2 => load(2, SlotKind::Double), Instruction::Dload_3 => load(3, SlotKind::Double), Instruction::Aload_0 => load(0, SlotKind::Ref), Instruction::Aload_1 => load(1, SlotKind::Ref), Instruction::Aload_2 => load(2, SlotKind::Ref), Instruction::Aload_3 => load(3, SlotKind::Ref), Instruction::Istore(i) => store(u16::from(*i), SlotKind::Int), Instruction::Lstore(i) => store(u16::from(*i), SlotKind::Long), Instruction::Fstore(i) => store(u16::from(*i), SlotKind::Float), Instruction::Dstore(i) => store(u16::from(*i), SlotKind::Double), Instruction::Astore(i) => store(u16::from(*i), SlotKind::Ref), Instruction::Istore_w(i) => store(*i, SlotKind::Int), Instruction::Lstore_w(i) => store(*i, SlotKind::Long), Instruction::Fstore_w(i) => store(*i, SlotKind::Float), Instruction::Dstore_w(i) => store(*i, SlotKind::Double), Instruction::Astore_w(i) => store(*i, SlotKind::Ref), Instruction::Istore_0 => store(0, SlotKind::Int), Instruction::Istore_1 => store(1, SlotKind::Int), Instruction::Istore_2 => store(2, SlotKind::Int), Instruction::Istore_3 => store(3, SlotKind::Int), Instruction::Lstore_0 => store(0, SlotKind::Long), Instruction::Lstore_1 => store(1, SlotKind::Long), Instruction::Lstore_2 => store(2, SlotKind::Long), Instruction::Lstore_3 => store(3, SlotKind::Long), Instruction::Fstore_0 => store(0, SlotKind::Float), Instruction::Fstore_1 => store(1, SlotKind::Float), Instruction::Fstore_2 => store(2, SlotKind::Float), Instruction::Fstore_3 => store(3, SlotKind::Float), Instruction::Dstore_0 => store(0, SlotKind::Double), Instruction::Dstore_1 => store(1, SlotKind::Double), Instruction::Dstore_2 => store(2, SlotKind::Double), Instruction::Dstore_3 => store(3, SlotKind::Double), Instruction::Astore_0 => store(0, SlotKind::Ref), Instruction::Astore_1 => store(1, SlotKind::Ref), Instruction::Astore_2 => store(2, SlotKind::Ref), Instruction::Astore_3 => store(3, SlotKind::Ref), Instruction::Iinc(i, d) => Insn::Iinc { index: u16::from(*i), delta: i32::from(*d), }, Instruction::Iinc_w(i, d) => Insn::Iinc { index: *i, delta: i32::from(*d), }, Instruction::Baload => Insn::ArrayLoad(ArrayKind::Byte), Instruction::Caload => Insn::ArrayLoad(ArrayKind::Char), Instruction::Saload => Insn::ArrayLoad(ArrayKind::Short), Instruction::Iaload => Insn::ArrayLoad(ArrayKind::Int), Instruction::Laload => Insn::ArrayLoad(ArrayKind::Long), Instruction::Faload => Insn::ArrayLoad(ArrayKind::Float), Instruction::Daload => Insn::ArrayLoad(ArrayKind::Double), Instruction::Aaload => Insn::ArrayLoad(ArrayKind::Reference), Instruction::Bastore => Insn::ArrayStore(ArrayKind::Byte), Instruction::Castore => Insn::ArrayStore(ArrayKind::Char), Instruction::Sastore => Insn::ArrayStore(ArrayKind::Short), Instruction::Iastore => Insn::ArrayStore(ArrayKind::Int), Instruction::Lastore => Insn::ArrayStore(ArrayKind::Long), Instruction::Fastore => Insn::ArrayStore(ArrayKind::Float), Instruction::Dastore => Insn::ArrayStore(ArrayKind::Double), Instruction::Aastore => Insn::ArrayStore(ArrayKind::Reference), Instruction::Pop => Insn::Pop, Instruction::Pop2 => Insn::Pop2, Instruction::Dup => Insn::Dup, Instruction::Dup_x1 => Insn::DupX1, Instruction::Dup_x2 => Insn::DupX2, Instruction::Dup2 => Insn::Dup2, Instruction::Dup2_x1 => Insn::Dup2X1, Instruction::Dup2_x2 => Insn::Dup2X2, Instruction::Swap => Insn::Swap, Instruction::Iadd => binary(Prim::Int, Arith::Add), Instruction::Isub => binary(Prim::Int, Arith::Sub), Instruction::Imul => binary(Prim::Int, Arith::Mul), Instruction::Idiv => binary(Prim::Int, Arith::Div), Instruction::Irem => binary(Prim::Int, Arith::Rem), Instruction::Ineg => binary(Prim::Int, Arith::Neg), Instruction::Iand => binary(Prim::Int, Arith::And), Instruction::Ior => binary(Prim::Int, Arith::Or), Instruction::Ixor => binary(Prim::Int, Arith::Xor), Instruction::Ishl => binary(Prim::Int, Arith::Shl), Instruction::Ishr => binary(Prim::Int, Arith::Shr), Instruction::Iushr => binary(Prim::Int, Arith::Ushr), Instruction::Ladd => binary(Prim::Long, Arith::Add), Instruction::Lsub => binary(Prim::Long, Arith::Sub), Instruction::Lmul => binary(Prim::Long, Arith::Mul), Instruction::Ldiv => binary(Prim::Long, Arith::Div), Instruction::Lrem => binary(Prim::Long, Arith::Rem), Instruction::Lneg => binary(Prim::Long, Arith::Neg), Instruction::Land => binary(Prim::Long, Arith::And), Instruction::Lor => binary(Prim::Long, Arith::Or), Instruction::Lxor => binary(Prim::Long, Arith::Xor), Instruction::Lshl => binary(Prim::Long, Arith::Shl), Instruction::Lshr => binary(Prim::Long, Arith::Shr), Instruction::Lushr => binary(Prim::Long, Arith::Ushr), Instruction::Fadd => binary(Prim::Float, Arith::Add), Instruction::Fsub => binary(Prim::Float, Arith::Sub), Instruction::Fmul => binary(Prim::Float, Arith::Mul), Instruction::Fdiv => binary(Prim::Float, Arith::Div), Instruction::Frem => binary(Prim::Float, Arith::Rem), Instruction::Fneg => binary(Prim::Float, Arith::Neg), Instruction::Dadd => binary(Prim::Double, Arith::Add), Instruction::Dsub => binary(Prim::Double, Arith::Sub), Instruction::Dmul => binary(Prim::Double, Arith::Mul), Instruction::Ddiv => binary(Prim::Double, Arith::Div), Instruction::Drem => binary(Prim::Double, Arith::Rem), Instruction::Dneg => binary(Prim::Double, Arith::Neg), Instruction::Lcmp => cmp(Prim::Long, false), Instruction::Fcmpl => cmp(Prim::Float, false), Instruction::Fcmpg => cmp(Prim::Float, true), Instruction::Dcmpl => cmp(Prim::Double, false), Instruction::Dcmpg => cmp(Prim::Double, true), Instruction::I2l => Insn::Convert(Convert::I2L), Instruction::I2f => Insn::Convert(Convert::I2F), Instruction::I2d => Insn::Convert(Convert::I2D), Instruction::I2b => Insn::Convert(Convert::I2B), Instruction::I2c => Insn::Convert(Convert::I2C), Instruction::I2s => Insn::Convert(Convert::I2S), Instruction::L2i => Insn::Convert(Convert::L2I), Instruction::L2f => Insn::Convert(Convert::L2F), Instruction::L2d => Insn::Convert(Convert::L2D), Instruction::F2i => Insn::Convert(Convert::F2I), Instruction::F2l => Insn::Convert(Convert::F2L), Instruction::F2d => Insn::Convert(Convert::F2D), Instruction::D2i => Insn::Convert(Convert::D2I), Instruction::D2l => Insn::Convert(Convert::D2L), Instruction::D2f => Insn::Convert(Convert::D2F), _ => return Ok(None), })) } fn load(slot: u16, kind: SlotKind) -> Insn { Insn::Load { slot, kind } } fn store(slot: u16, kind: SlotKind) -> Insn { Insn::Store { slot, kind } } fn binary(prim: Prim, op: Arith) -> Insn { Insn::Binary { prim, op } } fn cmp(prim: Prim, nan_great: bool) -> Insn { Insn::Cmp { prim, nan_great } }