From 937d27a1e64f5dc95258143d854cdd0222ad0226 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 7 Mar 2017 17:34:34 -0500 Subject: [PATCH] cache: some work on the interpreter --- .../cache/script/interpreter/Frame.java | 92 +++++++++++++++ .../GenericInstructionHandler.java | 80 +++++++++++++ .../interpreter/InstructionContext.java | 101 ++++++++++++++++ .../interpreter/InstructionHandler.java | 30 +++++ .../interpreter/InstructionHandlers.java | 72 ++++++++++++ .../cache/script/interpreter/Interpreter.java | 111 ++++++++++++++++++ .../script/interpreter/ScriptInstruction.java | 68 +++++++++++ .../cache/script/interpreter/Stack.java | 57 +++++++++ .../script/interpreter/StackContext.java | 62 ++++++++++ .../script/interpreter/instructions/If.java | 49 ++++++++ .../interpreter/instructions/If_ICmpEQ.java | 30 +++++ .../interpreter/instructions/If_ICmpGE.java | 30 +++++ .../interpreter/instructions/If_ICmpGT.java | 30 +++++ .../interpreter/instructions/If_ICmpLE.java | 30 +++++ .../interpreter/instructions/If_ICmpLT.java | 30 +++++ .../interpreter/instructions/If_ICmpNE.java | 30 +++++ .../script/interpreter/instructions/Jump.java | 40 +++++++ .../interpreter/instructions/LoadInt.java | 46 ++++++++ .../interpreter/instructions/Return.java | 39 ++++++ .../script/interpreter/InterpreterTest.java | 49 ++++++++ .../cache/script/interpreter/397.rs2asm | 50 ++++++++ 21 files changed, 1126 insertions(+) create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/Frame.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/GenericInstructionHandler.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/InstructionContext.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/InstructionHandler.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/InstructionHandlers.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/Interpreter.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/ScriptInstruction.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/Stack.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/StackContext.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpEQ.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpGE.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpGT.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpLE.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpLT.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpNE.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/instructions/Jump.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/instructions/LoadInt.java create mode 100644 cache/src/main/java/net/runelite/cache/script/interpreter/instructions/Return.java create mode 100644 cache/src/test/java/net/runelite/cache/script/interpreter/InterpreterTest.java create mode 100644 cache/src/test/resources/net/runelite/cache/script/interpreter/397.rs2asm diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/Frame.java b/cache/src/main/java/net/runelite/cache/script/interpreter/Frame.java new file mode 100644 index 0000000000..6ab8fda798 --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/Frame.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter; + +import net.runelite.cache.definitions.ScriptDefinition; + +public class Frame +{ + private final Interpreter interpreter; + private final ScriptDefinition script; + private final Stack intStack; + private final Stack stringStack; + + private boolean running = true; + int pc; + + public Frame(Interpreter interpreter, ScriptDefinition script) + { + this.interpreter = interpreter; + this.script = script; + this.intStack = new Stack(); + this.stringStack = new Stack(); + } + + public Frame(Interpreter interpreter, Frame other) + { + this.interpreter = interpreter; + this.script = other.script; + this.intStack = new Stack(other.intStack); + this.stringStack = new Stack(other.stringStack); + this.pc = other.pc; + } + + public Stack getIntStack() + { + return intStack; + } + + public Stack getStringStack() + { + return stringStack; + } + + public ScriptDefinition getScript() + { + return script; + } + + public Frame dup() + { + Frame frame = new Frame(interpreter, this); + interpreter.addFrame(this); + return frame; + } + + public boolean isRunning() + { + return running; + } + + public void stop() + { + running = false; + } + + public void jump(int offset) + { + pc += offset + 1; + } +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/GenericInstructionHandler.java b/cache/src/main/java/net/runelite/cache/script/interpreter/GenericInstructionHandler.java new file mode 100644 index 0000000000..ff93744fcb --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/GenericInstructionHandler.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter; + +import net.runelite.cache.script.Instruction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class GenericInstructionHandler extends InstructionHandler +{ + private static final Logger logger = LoggerFactory.getLogger(GenericInstructionHandler.class); + + @Override + public void execute(Frame frame, InstructionContext ctx) + { + Instruction i = ctx.getScriptInstruction().getInstruction(); + Stack istack = frame.getIntStack(); + Stack sstack = frame.getStringStack(); + + int ipops = i.getIntStackPops(); + for (int j = 0; j < ipops; ++j) + { + StackContext value = istack.pop(); + ctx.popsInt(value); + value.poppedBy(ctx); + } + + int spops = i.getStringStackPops(); + for (int j = 0; j < spops; ++j) + { + StackContext value = sstack.pop(); + ctx.popsString(value); + value.poppedBy(ctx); + } + + int ipushes = i.getIntStackPushes(); + for (int j = 0; j < ipushes; ++j) + { + StackContext sctx = new StackContext(ctx, null); + istack.push(sctx); + ctx.pushesInt(sctx); + } + + int spushes = i.getStringStackPushes(); + for (int j = 0; j < spushes; ++j) + { + StackContext sctx = new StackContext(ctx, null); + sstack.push(sctx); + ctx.pushesString(sctx); + } + + if (ipushes > 0 || spushes > 0) + { + logger.debug("Executed instruction {} with {}/{} pushes!", i, ipushes, spushes); + } + } + +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/InstructionContext.java b/cache/src/main/java/net/runelite/cache/script/interpreter/InstructionContext.java new file mode 100644 index 0000000000..9387346a67 --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/InstructionContext.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter; + +import java.util.ArrayList; +import java.util.List; + +public class InstructionContext +{ + private final ScriptInstruction scriptInstruction; + + private final List ipops = new ArrayList<>(); + private final List spops = new ArrayList<>(); + private final List ipushes = new ArrayList<>(); + private final List spushes = new ArrayList<>(); + + public InstructionContext(ScriptInstruction scriptInstruction) + { + this.scriptInstruction = scriptInstruction; + } + + public ScriptInstruction getScriptInstruction() + { + return scriptInstruction; + } + + public void popsInt(StackContext... ctx) + { + for (StackContext s : ctx) + { + ipops.add(s); + } + } + + public void popsString(StackContext... ctx) + { + for (StackContext s : ctx) + { + spops.add(s); + } + } + + public void pushesInt(StackContext... ctx) + { + for (StackContext s : ctx) + { + ipushes.add(s); + } + } + + public void pushesString(StackContext... ctx) + { + for (StackContext s : ctx) + { + spushes.add(s); + } + + } + + public List getIpops() + { + return ipops; + } + + public List getSpops() + { + return spops; + } + + public List getIpushes() + { + return ipushes; + } + + public List getSpushes() + { + return spushes; + } +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/InstructionHandler.java b/cache/src/main/java/net/runelite/cache/script/interpreter/InstructionHandler.java new file mode 100644 index 0000000000..82f468d1b8 --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/InstructionHandler.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter; + +public abstract class InstructionHandler +{ + public abstract void execute(Frame frame, InstructionContext ctx); +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/InstructionHandlers.java b/cache/src/main/java/net/runelite/cache/script/interpreter/InstructionHandlers.java new file mode 100644 index 0000000000..0c0cab5de3 --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/InstructionHandlers.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter; + +import java.util.HashMap; +import java.util.Map; +import net.runelite.cache.script.Opcodes; +import net.runelite.cache.script.interpreter.instructions.If_ICmpEQ; +import net.runelite.cache.script.interpreter.instructions.If_ICmpGE; +import net.runelite.cache.script.interpreter.instructions.If_ICmpGT; +import net.runelite.cache.script.interpreter.instructions.If_ICmpLE; +import net.runelite.cache.script.interpreter.instructions.If_ICmpLT; +import net.runelite.cache.script.interpreter.instructions.If_ICmpNE; +import net.runelite.cache.script.interpreter.instructions.Jump; +import net.runelite.cache.script.interpreter.instructions.LoadInt; +import net.runelite.cache.script.interpreter.instructions.Return; + +public class InstructionHandlers +{ + private static final Map handlers = new HashMap<>(); + + static + { + init(); + } + + private static void init() + { + add(Opcodes.LOAD_INT, new LoadInt()); + add(Opcodes.JUMP, new Jump()); + add(Opcodes.IF_ICMPNE, new If_ICmpNE()); + add(Opcodes.IF_ICMPEQ, new If_ICmpEQ()); + add(Opcodes.IF_ICMPLT, new If_ICmpLT()); + add(Opcodes.IF_ICMPGT, new If_ICmpGT()); + add(Opcodes.IF_ICMPLE, new If_ICmpLE()); + add(Opcodes.IF_ICMPGE, new If_ICmpGE()); + add(Opcodes.RETURN, new Return()); + } + + private static void add(int opcode, InstructionHandler handler) + { + assert handlers.containsKey(opcode) == false; + handlers.put(opcode, handler); + } + + public static InstructionHandler find(int opcode) + { + return handlers.get(opcode); + } +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/Interpreter.java b/cache/src/main/java/net/runelite/cache/script/interpreter/Interpreter.java new file mode 100644 index 0000000000..6e923f4db4 --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/Interpreter.java @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter; + +import java.util.ArrayDeque; +import java.util.Queue; +import net.runelite.cache.definitions.ScriptDefinition; +import net.runelite.cache.script.Instruction; +import net.runelite.cache.script.Instructions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Interpreter +{ + private static final Logger logger = LoggerFactory.getLogger(Interpreter.class); + + private final Queue frames = new ArrayDeque<>(); + private final GenericInstructionHandler genericInstructionHandler = new GenericInstructionHandler(); + + public void run(ScriptDefinition script) + { + Instructions.init(); + + Frame frame = new Frame(this, script); + frames.add(frame); + + int count = 0; + + while (!frames.isEmpty()) + { + frame = frames.remove(); + run(frame); + ++count; + } + + logger.info("Processed {} frames", count); + } + + private void run(Frame frame) + { + ScriptDefinition script = frame.getScript(); + + int[] instructions = script.getInstructions(); + int[] iops = script.getIntOperands(); + String[] sops = script.getStringOperands(); + + while (frame.isRunning()) + { + if (frame.pc >= instructions.length) + { + throw new RuntimeException("PC went past end of instructions - maybe missing return"); + } + + int opcode = instructions[frame.pc]; + int iop = iops[frame.pc]; + String sop = sops[frame.pc]; + + Instruction i = Instructions.find(opcode); + if (i == null) + { + throw new RuntimeException("Unknown instruction " + opcode + " in script at pc " + frame.pc); + } + + ScriptInstruction scriptInstruction = new ScriptInstruction(frame.pc, i, iop, sop); + InstructionContext ctx = new InstructionContext(scriptInstruction); + + InstructionHandler handler = InstructionHandlers.find(opcode); + if (handler == null) + { + handler = genericInstructionHandler; + } + + int old = frame.pc; + + handler.execute(frame, ctx); + + if (old == frame.pc) + { + // not a jump + ++frame.pc; + } + } + } + + public void addFrame(Frame frame) + { + frames.add(frame); + } +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/ScriptInstruction.java b/cache/src/main/java/net/runelite/cache/script/interpreter/ScriptInstruction.java new file mode 100644 index 0000000000..d59ddc2640 --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/ScriptInstruction.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter; + +import net.runelite.cache.script.Instruction; + +/** + * an instruction in a script + * + * @author Adam + */ +public class ScriptInstruction +{ + private final int pc; + private final Instruction instruction; + private final int iop; + private final String sop; + + public ScriptInstruction(int pc, Instruction instruction, int iop, String sop) + { + this.pc = pc; + this.instruction = instruction; + this.iop = iop; + this.sop = sop; + } + + public int getPc() + { + return pc; + } + + public Instruction getInstruction() + { + return instruction; + } + + public int getIop() + { + return iop; + } + + public String getSop() + { + return sop; + } +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/Stack.java b/cache/src/main/java/net/runelite/cache/script/interpreter/Stack.java new file mode 100644 index 0000000000..49fd0010e4 --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/Stack.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter; + +import java.util.ArrayList; +import java.util.List; + +public class Stack +{ + private final List stack = new ArrayList<>(); + + public Stack() + { + } + + public Stack(Stack other) + { + stack.addAll(other.stack); + } + + public void push(StackContext ctx) + { + stack.add(ctx); + } + + public StackContext pop() + { + if (stack.isEmpty()) + { + throw new RuntimeException("stack underflow"); + } + + return stack.remove(stack.size() - 1); + } +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/StackContext.java b/cache/src/main/java/net/runelite/cache/script/interpreter/StackContext.java new file mode 100644 index 0000000000..63349240ae --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/StackContext.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter; + +import java.util.ArrayList; +import java.util.List; + +public class StackContext +{ + private final InstructionContext pushed; + private final List popped = new ArrayList<>(); + private final Object value; + + public StackContext(InstructionContext pushed, Object value) + { + assert value == null || value instanceof String || value instanceof Integer; + this.pushed = pushed; + this.value = value; + } + + public void poppedBy(InstructionContext ctx) + { + popped.add(ctx); + } + + public InstructionContext getPushed() + { + return pushed; + } + + public List getPopped() + { + return popped; + } + + public Object getValue() + { + return value; + } +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If.java b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If.java new file mode 100644 index 0000000000..eac64a76e9 --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter.instructions; + +import net.runelite.cache.script.interpreter.Frame; +import net.runelite.cache.script.interpreter.InstructionContext; +import net.runelite.cache.script.interpreter.InstructionHandler; +import net.runelite.cache.script.interpreter.Stack; +import net.runelite.cache.script.interpreter.StackContext; + +public abstract class If extends InstructionHandler +{ + @Override + public final void execute(Frame frame, InstructionContext ctx) + { + Stack intStack = frame.getIntStack(); + int iop = ctx.getScriptInstruction().getIop(); + + StackContext sctx1 = intStack.pop(); + StackContext sctx2 = intStack.pop(); + + ctx.popsInt(sctx1, sctx2); + + Frame dup = frame.dup(); + dup.jump(iop); + } +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpEQ.java b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpEQ.java new file mode 100644 index 0000000000..ca6fc4a731 --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpEQ.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter.instructions; + +public class If_ICmpEQ extends If +{ + +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpGE.java b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpGE.java new file mode 100644 index 0000000000..9f0ae98e3c --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpGE.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter.instructions; + +public class If_ICmpGE extends If +{ + +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpGT.java b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpGT.java new file mode 100644 index 0000000000..d3e0fcbe61 --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpGT.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter.instructions; + +public class If_ICmpGT extends If +{ + +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpLE.java b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpLE.java new file mode 100644 index 0000000000..0e2b87e4d1 --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpLE.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter.instructions; + +public class If_ICmpLE extends If +{ + +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpLT.java b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpLT.java new file mode 100644 index 0000000000..29d63ab4a0 --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpLT.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter.instructions; + +public class If_ICmpLT extends If +{ + +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpNE.java b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpNE.java new file mode 100644 index 0000000000..e155845379 --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/If_ICmpNE.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter.instructions; + +public class If_ICmpNE extends If +{ + +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/Jump.java b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/Jump.java new file mode 100644 index 0000000000..e88d8febd7 --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/Jump.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter.instructions; + +import net.runelite.cache.script.interpreter.Frame; +import net.runelite.cache.script.interpreter.InstructionContext; +import net.runelite.cache.script.interpreter.InstructionHandler; + +public class Jump extends InstructionHandler +{ + @Override + public void execute(Frame frame, InstructionContext ctx) + { + int iop = ctx.getScriptInstruction().getIop(); + frame.jump(iop); + } + +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/LoadInt.java b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/LoadInt.java new file mode 100644 index 0000000000..7122bdb523 --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/LoadInt.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter.instructions; + +import net.runelite.cache.script.interpreter.Frame; +import net.runelite.cache.script.interpreter.InstructionContext; +import net.runelite.cache.script.interpreter.InstructionHandler; +import net.runelite.cache.script.interpreter.Stack; +import net.runelite.cache.script.interpreter.StackContext; + +public class LoadInt extends InstructionHandler +{ + @Override + public void execute(Frame frame, InstructionContext ctx) + { + Stack intStack = frame.getIntStack(); + int iop = ctx.getScriptInstruction().getIop(); + + StackContext sctx = new StackContext(ctx, iop); + ctx.pushesInt(sctx); + + intStack.push(sctx); + } +} diff --git a/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/Return.java b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/Return.java new file mode 100644 index 0000000000..78a34575a6 --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/script/interpreter/instructions/Return.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter.instructions; + +import net.runelite.cache.script.interpreter.Frame; +import net.runelite.cache.script.interpreter.InstructionContext; +import net.runelite.cache.script.interpreter.InstructionHandler; + +public class Return extends InstructionHandler +{ + @Override + public void execute(Frame frame, InstructionContext ctx) + { + frame.stop(); + } + +} diff --git a/cache/src/test/java/net/runelite/cache/script/interpreter/InterpreterTest.java b/cache/src/test/java/net/runelite/cache/script/interpreter/InterpreterTest.java new file mode 100644 index 0000000000..61012a70f2 --- /dev/null +++ b/cache/src/test/java/net/runelite/cache/script/interpreter/InterpreterTest.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.script.interpreter; + +import java.io.IOException; +import java.io.InputStream; +import net.runelite.cache.definitions.ScriptDefinition; +import net.runelite.cache.script.assembler.Assembler; +import org.junit.Assert; +import org.junit.Test; + +public class InterpreterTest +{ + @Test + public void testRun() throws IOException + { + InputStream in = InterpreterTest.class.getResourceAsStream("397.rs2asm"); + Assert.assertNotNull(in); + + Assembler assembler = new Assembler(); + ScriptDefinition script = assembler.assemble(in); + + Interpreter interpreter = new Interpreter(); + interpreter.run(script); + } + +} diff --git a/cache/src/test/resources/net/runelite/cache/script/interpreter/397.rs2asm b/cache/src/test/resources/net/runelite/cache/script/interpreter/397.rs2asm new file mode 100644 index 0000000000..61c32d22c7 --- /dev/null +++ b/cache/src/test/resources/net/runelite/cache/script/interpreter/397.rs2asm @@ -0,0 +1,50 @@ + 033 + load_int 1 + if_icmpne LABEL4 + jump LABEL5 +LABEL4: + return +LABEL5: + load_int 2871 + load_int 1 + load_int 0 + 3200 + 033 1 + load_int 1 + if_icmpeq LABEL13 + jump LABEL20 +LABEL13: + 025 3985 + 033 2 + iadd + load_int 4 + modulo + 027 3985 + jump LABEL41 +LABEL20: + 033 1 + load_int 2 + if_icmpeq LABEL24 + jump LABEL31 +LABEL24: + 025 3986 + 033 2 + iadd + load_int 4 + modulo + 027 3986 + jump LABEL41 +LABEL31: + 033 1 + load_int 3 + if_icmpeq LABEL35 + jump LABEL41 +LABEL35: + 025 3987 + 033 2 + iadd + load_int 4 + modulo + 027 3987 +LABEL41: + return