Skip to content

Commit

Permalink
Fix local variable shifting in insertParams and revert ASMUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
XXMA16 committed Sep 3, 2023
1 parent fa76b17 commit 9e2a9a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
35 changes: 19 additions & 16 deletions src/main/java/me/modmuss50/optifabric/compat/fix/ModMixinFixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,45 +65,44 @@ private static IntSupplier getIndex(MethodNode method, boolean afterCallback, bo
}

static void addParams(MethodNode method, IMixinInfo mixinInfo, String... params) {
insertParams(method, mixinInfo, Type.getArgumentTypes(method.desc).length, params);
insertParams(method, mixinInfo, Type.getArgumentTypes(method.desc).length, toTypeList(params));
}

static void insertParams(MethodNode method, IMixinInfo mixinInfo, IntSupplier index, String... params) {
insertParams(method, mixinInfo, index.getAsInt(), params);
insertParams(method, mixinInfo, index.getAsInt(), toTypeList(params));
}

static void insertParams(MethodNode method, IMixinInfo mixinInfo, int index, String... params) {
static void insertParams(MethodNode method, IMixinInfo mixinInfo, int index, List<Type> params) {
List<Type> newDesc = Arrays.stream(Type.getArgumentTypes(method.desc)).collect(Collectors.toList());
newDesc.addAll(index, Arrays.stream(params).map(Type::getType).collect(Collectors.toList()));
newDesc.addAll(index, params);
int shiftBy = 0;
for (String param : params) {
shiftBy++;
if (ASMUtils.isWideType(param)) shiftBy++;
int lvIndex = 0;
for (Type param : params) {
shiftBy += param.getSize();
}
method.maxLocals += shiftBy;

for (int i = 0; i < params.length; i++) {
for (int i = 0; i < params.size(); i++) {
method.parameters.add(index + i, new ParameterNode("syn_" + i, Opcodes.ACC_SYNTHETIC));
}

for (int i = index; i > 0; i--) {
if (ASMUtils.isWideType(newDesc.get(i))) {
index++;
}
if (!Modifier.isStatic(method.access)) lvIndex++;

for (int i = 0; i < index; i++) {
lvIndex += newDesc.get(i).getSize();
}
if (!Modifier.isStatic(method.access)) index++;

//shift locals (not mandatory)
for (LocalVariableNode local : method.localVariables) {
if (local.index >= index) {
if (local.index >= lvIndex) {
local.index += shiftBy;
}
}
//shift instructions
for (AbstractInsnNode insn : method.instructions) {
if (insn instanceof VarInsnNode && ((VarInsnNode) insn).var >= index) {
if (insn instanceof VarInsnNode && ((VarInsnNode) insn).var >= lvIndex) {
((VarInsnNode) insn).var += shiftBy;
} else if (insn instanceof IincInsnNode && ((IincInsnNode) insn).var >= index) {
} else if (insn instanceof IincInsnNode && ((IincInsnNode) insn).var >= lvIndex) {
((IincInsnNode) insn).var += shiftBy;
}
}
Expand All @@ -114,4 +113,8 @@ static void insertParams(MethodNode method, IMixinInfo mixinInfo, int index, Str
method.desc = Type.getMethodDescriptor(Type.getReturnType(method.desc), newDesc.toArray(new Type[0]));
methods.add(info.new Method(method, true));
}

private static List<Type> toTypeList(String[] types) {
return Arrays.stream(types).map(Type::getType).collect(Collectors.toList());
}
}
9 changes: 0 additions & 9 deletions src/main/java/me/modmuss50/optifabric/util/ASMUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,9 @@
import java.util.zip.ZipFile;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.ClassNode;

public class ASMUtils {
public static boolean isWideType(Type type) {
return type.equals(Type.LONG_TYPE) || type.equals(Type.DOUBLE_TYPE);
}

public static boolean isWideType(String type) {
return isWideType(Type.getType(type));
}

public static ClassNode readClass(byte[] bytes) {
return readClass(new ClassReader(Objects.requireNonNull(bytes, "Cannot read null class bytes")));
}
Expand Down

0 comments on commit 9e2a9a6

Please sign in to comment.