diff --git a/gradle.properties b/gradle.properties index 7dbd839d..27a6b9bf 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,5 +4,5 @@ packaging=jar description=Mixin url=https://www.spongepowered.org organization=SpongePowered -buildVersion=0.7 +buildVersion=0.7.1 buildType=SNAPSHOT diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index 6476f1e9..40ab7765 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -62,7 +62,7 @@ public abstract class MixinBootstrap { /** * Subsystem version */ - public static final String VERSION = "0.7"; + public static final String VERSION = "0.7.1"; // Consts private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch."; 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 1238859a..986fd88e 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 @@ -43,8 +43,8 @@ import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; import org.spongepowered.asm.mixin.injection.struct.Target; import org.spongepowered.asm.mixin.injection.throwables.InjectionError; import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException; -import org.spongepowered.asm.util.Bytecode; import org.spongepowered.asm.util.Annotations; +import org.spongepowered.asm.util.Bytecode; import org.spongepowered.asm.util.Constants; import org.spongepowered.asm.util.Locals; import org.spongepowered.asm.util.PrettyPrinter; @@ -67,6 +67,11 @@ public class CallbackInjector extends Injector { */ private final MethodNode handler; + /** + * HEAD instruction + */ + private final AbstractInsnNode head; + /** * Target method handle */ @@ -142,14 +147,14 @@ public class CallbackInjector extends Injector { int ctor, invoke; /** - * Marshall var is the local where we marshall the utility references we + * Marshal var is the local where we marshal the utility references we * need during invoke of the callback, those being the current return * value for value-return scenarios (we store the topmost stack entry * and then push it into the ctor of the CallbackInfo) and also the * CallbackInfo reference itself (we use the same local var for these * two purposes because they don't exist at the same time). */ - final int marshallVar; + private int marshalVar = -1; /** * True if this callback expects the target method arguments to be @@ -161,6 +166,7 @@ public class CallbackInjector extends Injector { Callback(MethodNode handler, Target target, final InjectionNode node, final LocalVariableNode[] locals, boolean captureLocals) { this.handler = handler; this.target = target; + this.head = target.insns.getFirst(); this.node = node; this.locals = locals; this.localTypes = locals != null ? new Type[locals.length] : null; @@ -193,7 +199,6 @@ public class CallbackInjector extends Injector { // this.typeCasts = new Type[this.frameSize + this.extraArgs]; this.invoke = target.arguments.length + (this.canCaptureLocals ? this.localTypes.length - this.frameSize : 0); - this.marshallVar = target.allocateLocal(); } /** @@ -215,6 +220,10 @@ public class CallbackInjector extends Injector { return this.target.getCallbackDescriptor(true, this.localTypes, this.target.arguments, this.frameSize, Short.MAX_VALUE); } + String getCallbackInfoConstructorDescriptor() { + return this.isAtReturn ? CallbackInfo.getConstructorDescriptor(this.target.returnType) : CallbackInfo.getConstructorDescriptor(); + } + /** * Add an instruction to this callback and increment the appropriate * stack sizes @@ -224,10 +233,18 @@ public class CallbackInjector extends Injector { * @param invokeStack true if this insn contributes to the invoke stack */ void add(AbstractInsnNode insn, boolean ctorStack, boolean invokeStack) { - this.add(insn); + this.add(insn, ctorStack, invokeStack, false); + } + + void add(AbstractInsnNode insn, boolean ctorStack, boolean invokeStack, boolean head) { + if (head) { + this.target.insns.insertBefore(this.head, insn); + } else { + this.add(insn); + } this.ctor += (ctorStack ? 1 : 0); this.invoke += (invokeStack ? 1 : 0); - } + } /** * Inject our generated code into the method and set the max stack size @@ -284,6 +301,15 @@ public class CallbackInjector extends Injector { boolean captureArgs() { return this.captureArgs; } + + int marshalVar() { + if (this.marshalVar < 0) { + this.marshalVar = this.target.allocateLocal(); + } + + return this.marshalVar; + } + } /** @@ -306,6 +332,16 @@ public class CallbackInjector extends Injector { */ private final Map ids = new HashMap(); + /** + * Total number of times this injector will be injected into the target. If + * greater than 1 we will cache the generated CallbackInfo + */ + private int totalInjections = 0; + private int callbackInfoVar = -1; + private String lastId, lastDesc; + private Target lastTarget; + private String callbackInfoClass; + /** * Make a new CallbackInjector with the supplied args * @@ -351,7 +387,7 @@ public class CallbackInjector extends Injector { */ @Override protected void addTargetNode(Target target, List myNodes, AbstractInsnNode node, Set nominators) { - InjectionNode injectionNode = target.injectionNodes.add(node); + InjectionNode injectionNode = target.addInjectionNode(node); for (InjectionPoint ip : nominators) { String id = ip.getId(); @@ -370,6 +406,7 @@ public class CallbackInjector extends Injector { } myNodes.add(injectionNode); + this.totalInjections++; } /* (non-Javadoc) @@ -462,7 +499,7 @@ public class CallbackInjector extends Injector { } this.dupReturnValue(callback); - if (this.cancellable) { + if (this.cancellable || this.totalInjections > 1) { this.createCallbackInfo(callback, true); } this.invokeCallback(callback, callbackMethod); @@ -552,21 +589,30 @@ public class CallbackInjector extends Injector { * @param store store the callback info in a local variable */ private void createCallbackInfo(final Callback callback, boolean store) { - callback.add(new TypeInsnNode(Opcodes.NEW, callback.target.callbackInfoClass), true, !store); - callback.add(new InsnNode(Opcodes.DUP), true, true); - - this.invokeCallbackInfoCtor(callback, store); - if (store) { - callback.add(new VarInsnNode(Opcodes.ASTORE, callback.marshallVar)); + // Reset vars on new target + if (callback.target != this.lastTarget) { + this.lastId = null; + this.lastDesc = null; } + this.lastTarget = callback.target; + + String id = this.getIdentifier(callback); + String desc = callback.getCallbackInfoConstructorDescriptor(); + + // If ID and descriptor match, and if we're not handling a returnable or cancellable CI, just re-use the last one + if (id.equals(this.lastId) && desc.equals(this.lastDesc) && !callback.isAtReturn && !this.cancellable) { + return; + } + + this.instanceCallbackInfo(callback, id, desc, store); } /** * @param callback callback handle */ private void loadOrCreateCallbackInfo(final Callback callback) { - if (this.cancellable) { - callback.add(new VarInsnNode(Opcodes.ALOAD, callback.marshallVar), false, true); + if (this.cancellable || this.totalInjections > 1) { + callback.add(new VarInsnNode(Opcodes.ALOAD, this.callbackInfoVar), false, true); } else { this.createCallbackInfo(callback, false); } @@ -586,25 +632,43 @@ public class CallbackInjector extends Injector { } callback.add(new InsnNode(Opcodes.DUP)); - callback.add(new VarInsnNode(callback.target.returnType.getOpcode(Opcodes.ISTORE), callback.marshallVar)); + callback.add(new VarInsnNode(callback.target.returnType.getOpcode(Opcodes.ISTORE), callback.marshalVar())); } /** * @param callback callback handle + * @param id callback id + * @param desc constructor descriptor * @param store true if storing in a local, false if this is happening at an * invoke */ - protected void invokeCallbackInfoCtor(final Callback callback, boolean store) { - callback.add(new LdcInsnNode(this.getIdentifier(callback)), true, !store); - callback.add(new InsnNode(this.cancellable ? Opcodes.ICONST_1 : Opcodes.ICONST_0), true, !store); + protected void instanceCallbackInfo(final Callback callback, String id, String desc, boolean store) { + this.lastId = id; + this.lastDesc = desc; + this.callbackInfoVar = callback.marshalVar(); + this.callbackInfoClass = callback.target.getCallbackInfoClass(); + + // If we were going to store the CI anyway, and if we need it again, and if the current injection isn't at + // return or cancellable, inject the CI creation at the method head so that it's available everywhere + boolean head = store && this.totalInjections > 1 && !callback.isAtReturn && !this.cancellable; + + callback.add(new TypeInsnNode(Opcodes.NEW, this.callbackInfoClass), true, !store, head); + callback.add(new InsnNode(Opcodes.DUP), true, true, head); + callback.add(new LdcInsnNode(id), true, !store, head); + callback.add(new InsnNode(this.cancellable ? Opcodes.ICONST_1 : Opcodes.ICONST_0), true, !store, head); if (callback.isAtReturn) { - callback.add(new VarInsnNode(callback.target.returnType.getOpcode(Opcodes.ILOAD), callback.marshallVar), true, !store); + callback.add(new VarInsnNode(callback.target.returnType.getOpcode(Opcodes.ILOAD), callback.marshalVar()), true, !store); callback.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, - callback.target.callbackInfoClass, Constants.CTOR, CallbackInfo.getConstructorDescriptor(callback.target.returnType), false)); + this.callbackInfoClass, Constants.CTOR, desc, false)); } else { callback.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, - callback.target.callbackInfoClass, Constants.CTOR, CallbackInfo.getConstructorDescriptor(), false)); + this.callbackInfoClass, Constants.CTOR, desc, false), false, false, head); + } + + if (store) { + callback.target.addLocalVariable(this.callbackInfoVar, "callbackInfo" + this.callbackInfoVar, "L" + this.callbackInfoClass + ";"); + callback.add(new VarInsnNode(Opcodes.ASTORE, this.callbackInfoVar), false, false, head); } } @@ -658,8 +722,8 @@ public class CallbackInjector extends Injector { return; } - callback.add(new VarInsnNode(Opcodes.ALOAD, callback.marshallVar)); - callback.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, callback.target.callbackInfoClass, CallbackInfo.getIsCancelledMethodName(), + callback.add(new VarInsnNode(Opcodes.ALOAD, this.callbackInfoVar)); + callback.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, this.callbackInfoClass, CallbackInfo.getIsCancelledMethodName(), CallbackInfo.getIsCancelledMethodSig(), false)); LabelNode notCancelled = new LabelNode(); @@ -684,10 +748,10 @@ public class CallbackInjector extends Injector { } else { // Non-void method, so work out which accessor to call to get the // return value, and return it - callback.add(new VarInsnNode(Opcodes.ALOAD, callback.marshallVar)); + callback.add(new VarInsnNode(Opcodes.ALOAD, callback.marshalVar())); String accessor = CallbackInfoReturnable.getReturnAccessor(callback.target.returnType); String descriptor = CallbackInfoReturnable.getReturnDescriptor(callback.target.returnType); - callback.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, callback.target.callbackInfoClass, accessor, descriptor, false)); + callback.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, this.callbackInfoClass, accessor, descriptor, false)); if (callback.target.returnType.getSort() == Type.OBJECT) { callback.add(new TypeInsnNode(Opcodes.CHECKCAST, callback.target.returnType.getInternalName())); } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java b/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java index 6dd1b8be..e1184ca3 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java @@ -27,11 +27,11 @@ package org.spongepowered.asm.mixin.injection.code; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TreeMap; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -58,22 +58,22 @@ import org.spongepowered.asm.util.Bytecode; * Base class for bytecode injectors */ public abstract class Injector { - + /** * A nominated target node */ public static final class TargetNode { - final AbstractInsnNode node; + final AbstractInsnNode insn; final Set nominators = new HashSet(); - TargetNode(AbstractInsnNode node) { - this.node = node; + TargetNode(AbstractInsnNode insn) { + this.insn = insn; } public AbstractInsnNode getNode() { - return this.node; + return this.insn; } public Set getNominators() { @@ -86,12 +86,12 @@ public abstract class Injector { return false; } - return ((TargetNode)obj).node == this.node; + return ((TargetNode)obj).insn == this.insn; } @Override public int hashCode() { - return this.node.hashCode(); + return this.insn.hashCode(); } } @@ -173,13 +173,13 @@ public abstract class Injector { List myNodes = new ArrayList(); for (TargetNode node : this.findTargetNodes(injectorTarget, injectionPoints)) { - this.addTargetNode(injectorTarget.getTarget(), myNodes, node.node, node.nominators); + this.addTargetNode(injectorTarget.getTarget(), myNodes, node.insn, node.nominators); } return myNodes; } protected void addTargetNode(Target target, List myNodes, AbstractInsnNode node, Set nominators) { - myNodes.add(target.injectionNodes.add(node)); + myNodes.add(target.addInjectionNode(node)); } /** @@ -214,19 +214,20 @@ public abstract class Injector { * @return Target insn nodes in the target method */ private Collection findTargetNodes(InjectorTarget injectorTarget, List injectionPoints) { - Map targetNodes = new HashMap(); - + MethodNode method = injectorTarget.getMethod(); + Map targetNodes = new TreeMap(); Collection nodes = new ArrayList(32); for (InjectionPoint injectionPoint : injectionPoints) { nodes.clear(); - if (this.findTargetNodes(injectorTarget.getMethod(), injectionPoint, injectorTarget.getSlice(injectionPoint), nodes)) { - for (AbstractInsnNode node : nodes) { - TargetNode targetNode = targetNodes.get(node); + if (this.findTargetNodes(method, injectionPoint, injectorTarget.getSlice(injectionPoint), nodes)) { + for (AbstractInsnNode insn : nodes) { + Integer key = method.instructions.indexOf(insn); + TargetNode targetNode = targetNodes.get(key); if (targetNode == null) { - targetNode = new TargetNode(node); - targetNodes.put(node, targetNode); + targetNode = new TargetNode(insn); + targetNodes.put(key, targetNode); } targetNode.nominators.add(injectionPoint); } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyConstantInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyConstantInjector.java index 37fb2d5b..8c690f51 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyConstantInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyConstantInjector.java @@ -121,7 +121,7 @@ public class ModifyConstantInjector extends RedirectInjector { } if (withArgs) { - this.pushArgs(target.arguments, after, target.argIndices, 0, target.arguments.length); + this.pushArgs(target.arguments, after, target.getArgIndices(), 0, target.arguments.length); target.addToStack(Bytecode.getArgsSize(target.arguments)); } 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 35436c90..ffcd76ca 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 @@ -167,7 +167,7 @@ public class RedirectInjector extends InvokeInjector { @Override protected void addTargetNode(Target target, List myNodes, AbstractInsnNode insn, Set nominators) { - InjectionNode node = target.injectionNodes.get(insn); + InjectionNode node = target.getInjectionNode(insn); ConstructorRedirectData ctorData = null; int fuzz = BeforeFieldAccess.ARRAY_SEARCH_FUZZ_DEFAULT; int opcode = 0; @@ -197,7 +197,7 @@ public class RedirectInjector extends InvokeInjector { } } - InjectionNode targetNode = target.injectionNodes.add(insn); + InjectionNode targetNode = target.addInjectionNode(insn); targetNode.decorate(Meta.KEY, this.meta); targetNode.decorate(RedirectInjector.KEY_NOMINATORS, nominators); if (insn instanceof TypeInsnNode && insn.getOpcode() == Opcodes.NEW) { @@ -307,7 +307,7 @@ public class RedirectInjector extends InvokeInjector { int argSize = Bytecode.getArgsSize(target.arguments); extraLocals += argSize; extraStack += argSize; - argMap = Ints.concat(argMap, target.argIndices); + argMap = Ints.concat(argMap, target.getArgIndices()); } AbstractInsnNode insn = this.invokeHandlerWithArgs(this.methodArgs, insns, argMap); target.replaceNode(methodNode, insn, insns); @@ -402,7 +402,7 @@ public class RedirectInjector extends InvokeInjector { InsnList invokeInsns = new InsnList(); if (withArgs) { - this.pushArgs(target.arguments, invokeInsns, target.argIndices, 0, target.arguments.length); + this.pushArgs(target.arguments, invokeInsns, target.getArgIndices(), 0, target.arguments.length); target.addToStack(Bytecode.getArgsSize(target.arguments)); } target.replaceNode(varNode, this.invokeHandler(invokeInsns), invokeInsns); @@ -449,7 +449,7 @@ public class RedirectInjector extends InvokeInjector { } if (withArgs) { - this.pushArgs(target.arguments, insns, target.argIndices, 0, target.arguments.length); + this.pushArgs(target.arguments, insns, target.getArgIndices(), 0, target.arguments.length); target.addToStack(Bytecode.getArgsSize(target.arguments)); } @@ -481,7 +481,7 @@ public class RedirectInjector extends InvokeInjector { } if (withArgs) { - this.pushArgs(target.arguments, insns, target.argIndices, 0, target.arguments.length); + this.pushArgs(target.arguments, insns, target.getArgIndices(), 0, target.arguments.length); target.addToStack(Bytecode.getArgsSize(target.arguments)); } @@ -554,7 +554,7 @@ public class RedirectInjector extends InvokeInjector { InsnList insns = new InsnList(); if (withArgs) { - this.pushArgs(target.arguments, insns, target.argIndices, 0, target.arguments.length); + this.pushArgs(target.arguments, insns, target.getArgIndices(), 0, target.arguments.length); target.addToStack(Bytecode.getArgsSize(target.arguments)); } 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 4c90be05..8792acdb 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 @@ -24,13 +24,18 @@ */ package org.spongepowered.asm.mixin.injection.struct; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; +import org.spongepowered.asm.lib.Label; import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.lib.Type; import org.spongepowered.asm.lib.tree.AbstractInsnNode; import org.spongepowered.asm.lib.tree.ClassNode; import org.spongepowered.asm.lib.tree.InsnList; +import org.spongepowered.asm.lib.tree.LabelNode; +import org.spongepowered.asm.lib.tree.LocalVariableNode; import org.spongepowered.asm.lib.tree.MethodInsnNode; import org.spongepowered.asm.lib.tree.MethodNode; import org.spongepowered.asm.lib.tree.TypeInsnNode; @@ -77,30 +82,10 @@ public class Target implements Comparable, Iterable { */ public final Type[] arguments; - /** - * Method argument slots - */ - public final int[] argIndices; - /** * Return type computed from the method descriptor */ public final Type returnType; - - /** - * Callback method descriptor based on this target - */ - public final String callbackDescriptor; - - /** - * Callback info class - */ - public final String callbackInfoClass; - - /** - * Nodes targetted by injectors - */ - public final InjectionNodes injectionNodes = new InjectionNodes(); /** * Method's (original) MAXS @@ -111,6 +96,36 @@ public class Target implements Comparable, Iterable { * Method's original max locals */ private final int maxLocals; + + /** + * Nodes targetted by injectors + */ + private final InjectionNodes injectionNodes = new InjectionNodes(); + + /** + * Callback info class + */ + private String callbackInfoClass; + + /** + * Callback method descriptor based on this target + */ + private String callbackDescriptor; + + /** + * Method argument slots + */ + private int[] argIndices; + + /** + * Local variables used for argmap slots + */ + private List argMapVars; + + /** + * Labels for LVT ranges, generated as needed + */ + private LabelNode start, end; /** * Make a new Target for the supplied method @@ -124,13 +139,32 @@ public class Target implements Comparable, Iterable { this.isStatic = Bytecode.methodIsStatic(method); this.isCtor = method.name.equals(Constants.CTOR); this.arguments = Type.getArgumentTypes(method.desc); - this.argIndices = this.calcArgIndices(this.isStatic ? 0 : 1); this.returnType = Type.getReturnType(method.desc); this.maxStack = method.maxStack; this.maxLocals = method.maxLocals; - this.callbackInfoClass = CallbackInfo.getCallInfoClassName(this.returnType); - this.callbackDescriptor = String.format("(%sL%s;)V", method.desc.substring(1, method.desc.indexOf(')')), this.callbackInfoClass); + } + + /** + * Add an injection node to this target if it does not already exist, + * returns the existing node if it exists + * + * @param node Instruction node to add + * @return wrapper for the specified node + */ + public InjectionNode addInjectionNode(AbstractInsnNode node) { + return this.injectionNodes.add(node); + } + + /** + * Get an injection node from this collection if it already exists, returns + * null if the node is not tracked + * + * @param node instruction node + * @return wrapper node or null if not tracked + */ + public InjectionNode getInjectionNode(AbstractInsnNode node) { + return this.injectionNodes.get(node); } /** @@ -244,15 +278,73 @@ public class Target implements Comparable, Iterable { * of the supplied args array */ public int[] generateArgMap(Type[] args, int start) { - int local = this.maxLocals; + if (this.argMapVars == null) { + this.argMapVars = new ArrayList(); + } + int[] argMap = new int[args.length]; - for (int arg = start; arg < args.length; arg++) { - argMap[arg] = local; - local += args[arg].getSize(); + for (int arg = start, index = 0; arg < args.length; arg++) { + int size = args[arg].getSize(); + argMap[arg] = this.allocateArgMapLocal(index, size); + index += size; } return argMap; } + /** + * Allocates a local variable to be used for argmap. Since argmaps do not + * overlap we can safely reuse local variables allocated for this purpose. + * We do however need to deal with expanding the argmap allocation when + * necessary, which is complicated slightly by the fact that some variables + * occupy more than one local slot. + * + * @param index Argmap index + * @param size Size of variable + * @return local offset to use + */ + private int allocateArgMapLocal(int index, int size) { + // Allocate extra space if we've reached the end + if (index >= this.argMapVars.size()) { + int base = this.allocateLocals(size); + for (int offset = 0; offset < size; offset++) { + this.argMapVars.add(Integer.valueOf(base + offset)); + } + return base; + } + + int local = this.argMapVars.get(index); + + // Allocate extra space if the variable is oversize + if (size > 1 && index + size > this.argMapVars.size()) { + int nextLocal = this.allocateLocals(1); + if (nextLocal == local + 1) { + // Next allocated was contiguous, so we can continue + this.argMapVars.add(Integer.valueOf(nextLocal)); + return local; + } + + // Next allocated was not contiguous, allocate a new local slot so + // that indexes are contiguous + this.argMapVars.set(index, Integer.valueOf(nextLocal)); + this.argMapVars.add(Integer.valueOf(this.allocateLocals(1))); + return nextLocal; + } + + return local; + } + + /** + * Get the argument indices for this target, calculated on first use + * + * @return argument indices for this target + */ + public int[] getArgIndices() { + if (this.argIndices == null) { + this.argIndices = this.calcArgIndices(this.isStatic ? 0 : 1); + } + return this.argIndices; + } + private int[] calcArgIndices(int local) { int[] argIndices = new int[this.arguments.length]; for (int arg = 0; arg < this.arguments.length; arg++) { @@ -262,6 +354,19 @@ public class Target implements Comparable, Iterable { return argIndices; } + /** + * Get the CallbackInfo class used for this target, based on the target + * return type + * + * @return CallbackInfo class name + */ + public String getCallbackInfoClass() { + if (this.callbackInfoClass == null) { + this.callbackInfoClass = CallbackInfo.getCallInfoClassName(this.returnType); + } + return this.callbackInfoClass; + } + /** * Get "simple" callback descriptor (descriptor with only CallbackInfo) * @@ -293,6 +398,11 @@ public class Target implements Comparable, Iterable { * @return generated descriptor */ public String getCallbackDescriptor(final boolean captureLocals, final Type[] locals, Type[] argumentTypes, int startIndex, int extra) { + if (this.callbackDescriptor == null) { + this.callbackDescriptor = String.format("(%sL%s;)V", this.method.desc.substring(1, this.method.desc.indexOf(')')), + this.callbackInfoClass); + } + if (!captureLocals || locals == null) { return this.callbackDescriptor; } @@ -479,5 +589,38 @@ public class Target implements Comparable, Iterable { this.insns.remove(insn); this.injectionNodes.remove(insn); } + + /** + * Add an entry to the target LVT + * + * @param index local variable index + * @param name local variable name + * @param desc local variable type + */ + public void addLocalVariable(int index, String name, String desc) { + if (this.start == null) { + this.start = new LabelNode(new Label()); + this.end = new LabelNode(new Label()); + this.insns.insert(this.start); + this.insns.add(this.end); + } + this.addLocalVariable(index, name, desc, this.start, this.end); + } + + /** + * Add an entry to the target LVT between the specified start and end labels + * + * @param index local variable index + * @param name local variable name + * @param desc local variable type + * @param start start of range + * @param end end of range + */ + private void addLocalVariable(int index, String name, String desc, LabelNode start, LabelNode end) { + if (this.method.localVariables == null) { + this.method.localVariables = new ArrayList(); + } + this.method.localVariables.add(new LocalVariableNode(name, desc, null, start, end, index)); + } }