From 76cb99e914e150834d4d48c6e54bfeca1f97cd81 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Mon, 27 Apr 2015 17:13:48 +0100 Subject: [PATCH] Centralise stack and locals logic into Target --- .../injection/callback/CallbackInjector.java | 8 +- .../injection/invoke/InvokeInjector.java | 22 +--- .../injection/invoke/ModifyArgInjector.java | 10 +- .../injection/invoke/RedirectInjector.java | 8 +- .../asm/mixin/injection/struct/Target.java | 108 +++++++++++++++++- 5 files changed, 119 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java index 6d662d43..e3e4ec5d 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java @@ -169,7 +169,7 @@ public class CallbackInjector extends Injector { this.descl = target.getCallbackDescriptor(true, this.localTypes, target.arguments, this.frameSize); this.invoke = target.arguments.length + (this.canCaptureLocals ? this.localTypes.length - this.frameSize : 0); - this.marshallVar = target.method.maxLocals++; + this.marshallVar = target.allocateLocal(); } /** @@ -206,8 +206,8 @@ public class CallbackInjector extends Injector { * for the method based on our calculated values */ void inject() { - this.target.method.instructions.insertBefore(this.node, this); - this.target.method.maxStack = Math.max(this.target.method.maxStack, this.target.maxStack + Math.max(this.invoke, this.ctor)); + this.target.insns.insertBefore(this.node, this); + this.target.addToStack(Math.max(this.invoke, this.ctor)); } } @@ -385,7 +385,7 @@ public class CallbackInjector extends Injector { PrettyPrinter printer = new PrettyPrinter(); printer.add("%20s : %s", "Target Class", this.classNode.name.replace('/', '.')); printer.add("%20s : %s", "Target Method", methodSig); - printer.add("%20s : %d", "Target Max LOCALS", callback.target.maxLocals); + printer.add("%20s : %d", "Target Max LOCALS", callback.target.getMaxLocals()); printer.add("%20s : %d", "Initial Frame Size", callback.frameSize); printer.add("%20s : %s", "Callback Name", this.methodNode.name); printer.add("%20s : %s %s", "Instruction", callback.node.getClass().getSimpleName(), ASMHelper.getOpcodeName(callback.node.getOpcode())); diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java index 2d605de5..6daba7e0 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java @@ -125,26 +125,6 @@ public abstract class InvokeInjector extends Injector { this.invokeHandler(insns); } - /** - * Generate an array containing local indexes for the specified args, - * returns an array of identical size to the supplied array with an - * allocated local index in each corresponding position - * - * @param args Argument types - * @param start starting index - * @param local starting local index - * @return array containing a corresponding local arg index for each member - * of the supplied args array - */ - protected int[] generateArgMap(Type[] args, int start, int local) { - int[] argMap = new int[args.length]; - for (int arg = start; arg < args.length; arg++) { - argMap[arg] = local; - local += args[arg].getSize(); - } - return argMap; - } - /** * Store args on the stack starting at the end and working back to position * specified by start, return the generated argMap @@ -156,7 +136,7 @@ public abstract class InvokeInjector extends Injector { * @return the generated argmap */ protected int[] storeArgs(Target target, Type[] args, InsnList insns, int start) { - int[] argMap = this.generateArgMap(args, start, target.method.maxLocals); + int[] argMap = target.generateArgMap(args, start); this.storeArgs(args, insns, argMap, start, args.length); return argMap; } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java index 8710caa9..9374bbb9 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java @@ -98,9 +98,9 @@ public class ModifyArgInjector extends InvokeInjector { extraLocals = this.injectMultiArgHandler(target, args, argIndex, insns); } - target.method.instructions.insertBefore(node, insns); - target.method.maxLocals = Math.max(target.maxLocals, target.method.maxLocals + extraLocals); - target.method.maxStack = Math.max(target.maxStack, target.method.maxStack + 2 - (extraLocals - 1)); + target.insns.insertBefore(node, insns); + target.addToLocals(extraLocals); + target.addToStack(2 - (extraLocals - 1)); } /** @@ -110,7 +110,7 @@ public class ModifyArgInjector extends InvokeInjector { int[] argMap = this.storeArgs(target, args, insns, argIndex); this.invokeHandlerWithArgs(args, insns, argMap, argIndex, argIndex + 1); this.pushArgs(args, insns, argMap, argIndex + 1, args.length); - return (target.method.maxLocals - argMap[argMap.length - 1]) + 1 + args[args.length - 1].getSize(); + return (target.getCurrentMaxLocals() - argMap[argMap.length - 1]) + 1 + args[args.length - 1].getSize(); } /** @@ -126,7 +126,7 @@ public class ModifyArgInjector extends InvokeInjector { this.pushArgs(args, insns, argMap, 0, argIndex); this.invokeHandlerWithArgs(args, insns, argMap, 0, args.length); this.pushArgs(args, insns, argMap, argIndex + 1, args.length); - return (target.method.maxLocals - argMap[argMap.length - 1]) + 1 + args[args.length - 1].getSize(); + return (target.getCurrentMaxLocals() - argMap[argMap.length - 1]) + 1 + args[args.length - 1].getSize(); } protected int findArgIndex(Target target, Type[] args) { diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java index 0fc4d711..37b57116 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java @@ -104,9 +104,9 @@ public class RedirectInjector extends InvokeInjector { } this.invokeHandlerWithArgs(this.methodArgs, insns, argMap); - target.method.instructions.insertBefore(node, insns); - target.method.instructions.remove(node); - target.method.maxLocals = Math.max(target.method.maxLocals, target.maxLocals + extraLocals); - target.method.maxStack = Math.max(target.method.maxStack, target.maxStack + 1); + target.insns.insertBefore(node, insns); + target.insns.remove(node); + target.addToLocals(extraLocals); + target.addToStack(1); } } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/Target.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/Target.java index 301598f7..fa00663c 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/Target.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/Target.java @@ -25,6 +25,7 @@ package org.spongepowered.asm.mixin.injection.struct; import org.objectweb.asm.Type; +import org.objectweb.asm.tree.InsnList; import org.objectweb.asm.tree.MethodNode; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.util.ASMHelper; @@ -41,6 +42,11 @@ public class Target { */ public final MethodNode method; + /** + * Method instructions + */ + public final InsnList insns; + /** * True if the method is static */ @@ -64,13 +70,13 @@ public class Target { /** * Method's (original) MAXS */ - public final int maxStack; + private final int maxStack; /** * Method's original max locals */ - public final int maxLocals; - + private final int maxLocals; + /** * Callback method descriptor based on this target */ @@ -88,6 +94,7 @@ public class Target { */ public Target(MethodNode method) { this.method = method; + this.insns = method.instructions; this.isStatic = ASMHelper.methodIsStatic(method); this.arguments = Type.getArgumentTypes(method.desc); this.argIndices = this.calcArgIndices(this.isStatic ? 0 : 1); @@ -98,6 +105,101 @@ public class Target { this.callbackInfoClass = CallbackInfo.getCallInfoClassName(this.returnType); this.callbackDescriptor = String.format("(%sL%s;)V", method.desc.substring(1, method.desc.indexOf(')')), this.callbackInfoClass); } + + /** + * Get the original max locals of the method + */ + public int getMaxLocals() { + return this.maxLocals; + } + + /** + * Get the original max stack of the method + */ + public int getMaxStack() { + return this.maxStack; + } + + /** + * Get the current max locals of the method + */ + public int getCurrentMaxLocals() { + return this.method.maxLocals; + } + + /** + * Get the current max stack of the method + */ + public int getCurrentMaxStack() { + return this.method.maxStack; + } + + /** + * Allocate a new local variable for the method + */ + public int allocateLocal() { + return this.allocateLocals(1); + } + + /** + * Allocate a number of new local variables for this method, returns the + * first local variable index of the allocated range + * + * @param locals number of locals to allocate + * @return the first local variable index of the allocated range + */ + public int allocateLocals(int locals) { + int nextLocal = this.method.maxLocals; + this.method.maxLocals += locals; + return nextLocal; + } + + /** + * Allocate a number of new stack variables for this method, returns the + * first stack index of the allocated range + * + * @param locals number of locals to allocate + * @return the first local variable index of the allocated range + */ + public void addToLocals(int locals) { + this.setMaxLocals(this.maxLocals + locals); + } + + public void setMaxLocals(int maxLocals) { + if (maxLocals > this.method.maxLocals) { + this.method.maxLocals = maxLocals; + } + } + + public void addToStack(int stack) { + this.setMaxStack(this.maxStack + stack); + } + + public void setMaxStack(int maxStack) { + if (maxStack > this.method.maxStack) { + this.method.maxStack = maxStack; + } + } + + /** + * Generate an array containing local indexes for the specified args, + * returns an array of identical size to the supplied array with an + * allocated local index in each corresponding position + * + * @param args Argument types + * @param start starting index + * @return array containing a corresponding local arg index for each member + * of the supplied args array + */ + public int[] generateArgMap(Type[] args, int start) { + int local = this.maxLocals; + int[] argMap = new int[args.length]; + for (int arg = start; arg < args.length; arg++) { + argMap[arg] = local; + local += args[arg].getSize(); + } + return argMap; + } private int[] calcArgIndices(int local) { int[] argIndices = new int[this.arguments.length];