From c7c602677081692c63474e1e0e85311d2a509cdd Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 13 Jan 2025 16:46:00 -0500 Subject: [PATCH] Add utility to map variable insns to their respective accessed/written variable types --- .../software/coley/recaf/util/AsmInsnUtil.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/recaf-core/src/main/java/software/coley/recaf/util/AsmInsnUtil.java b/recaf-core/src/main/java/software/coley/recaf/util/AsmInsnUtil.java index d22d3c58a..cb17b58cd 100644 --- a/recaf-core/src/main/java/software/coley/recaf/util/AsmInsnUtil.java +++ b/recaf-core/src/main/java/software/coley/recaf/util/AsmInsnUtil.java @@ -7,6 +7,7 @@ import org.objectweb.asm.tree.InsnNode; import org.objectweb.asm.tree.IntInsnNode; import org.objectweb.asm.tree.LdcInsnNode; +import org.objectweb.asm.tree.VarInsnNode; /** * ASM instruction utilities. @@ -31,6 +32,23 @@ public static AbstractInsnNode getDefaultValue(@Nonnull Type type) { }; } + /** + * @param varInsn + * Variable instruction. + * + * @return Type that encompasses the variable being accessed/written to, + */ + @Nonnull + public static Type getTypeForVarInsn(@Nonnull VarInsnNode varInsn) { + return switch (varInsn.getOpcode()) { + case Opcodes.ISTORE, Opcodes.ILOAD -> Type.INT_TYPE; + case Opcodes.LSTORE, Opcodes.LLOAD -> Type.LONG_TYPE; + case Opcodes.FSTORE, Opcodes.FLOAD -> Type.FLOAT_TYPE; + case Opcodes.DSTORE, Opcodes.DLOAD -> Type.DOUBLE_TYPE; + default -> Types.OBJECT_TYPE; + }; + } + /** * Create an instruction to hold a given {@code int} value. *