diff --git a/cache/src/main/antlr4/net/runelite/cache/script/assembler/rs2asm.g4 b/cache/src/main/antlr4/net/runelite/cache/script/assembler/rs2asm.g4 index 07694a7f20..150c6a7e16 100644 --- a/cache/src/main/antlr4/net/runelite/cache/script/assembler/rs2asm.g4 +++ b/cache/src/main/antlr4/net/runelite/cache/script/assembler/rs2asm.g4 @@ -24,7 +24,19 @@ */ grammar rs2asm; -prog: (line NEWLINE)+ ; +prog: (header NEWLINE)* (line NEWLINE)+ ; + +header: int_stack_count | string_stack_count | int_var_count | string_var_count ; + +int_stack_count: '.int_stack_count ' int_stack_value ; +string_stack_count: '.string_stack_count ' string_stack_value ; +int_var_count: '.int_var_count ' int_var_value ; +string_var_count: '.string_var_count ' string_var_value ; + +int_stack_value: INT ; +string_stack_value: INT ; +int_var_value: INT ; +string_var_value: INT ; line: instruction | label | switch_lookup ; instruction: instruction_name instruction_operand ; diff --git a/cache/src/main/java/net/runelite/cache/definitions/ScriptDefinition.java b/cache/src/main/java/net/runelite/cache/definitions/ScriptDefinition.java index 37d330a91c..77a21c253a 100644 --- a/cache/src/main/java/net/runelite/cache/definitions/ScriptDefinition.java +++ b/cache/src/main/java/net/runelite/cache/definitions/ScriptDefinition.java @@ -29,13 +29,13 @@ import java.util.Map; public class ScriptDefinition { private int id; - private int intStackCount; private int[] instructions; private int[] intOperands; private String[] stringOperands; - private int localStringCount; + private int intStackCount; private int stringStackCount; private int localIntCount; + private int localStringCount; private Map[] switches; public int getId() diff --git a/cache/src/main/java/net/runelite/cache/script/assembler/ScriptWriter.java b/cache/src/main/java/net/runelite/cache/script/assembler/ScriptWriter.java index 39111e5901..8627a5ea23 100644 --- a/cache/src/main/java/net/runelite/cache/script/assembler/ScriptWriter.java +++ b/cache/src/main/java/net/runelite/cache/script/assembler/ScriptWriter.java @@ -42,6 +42,10 @@ public class ScriptWriter extends rs2asmBaseListener private final LabelVisitor labelVisitor; private int pos; + private int intStackCount; + private int stringStackCount; + private int localIntCount; + private int localStringCount; private List opcodes = new ArrayList<>(); private List iops = new ArrayList<>(); private List sops = new ArrayList<>(); @@ -52,6 +56,34 @@ public class ScriptWriter extends rs2asmBaseListener this.labelVisitor = labelVisitor; } + @Override + public void enterInt_stack_value(rs2asmParser.Int_stack_valueContext ctx) + { + int value = Integer.parseInt(ctx.getText()); + intStackCount = value; + } + + @Override + public void enterString_stack_value(rs2asmParser.String_stack_valueContext ctx) + { + int value = Integer.parseInt(ctx.getText()); + stringStackCount = value; + } + + @Override + public void enterInt_var_value(rs2asmParser.Int_var_valueContext ctx) + { + int value = Integer.parseInt(ctx.getText()); + localIntCount = value; + } + + @Override + public void enterString_var_value(rs2asmParser.String_var_valueContext ctx) + { + int value = Integer.parseInt(ctx.getText()); + localStringCount = value; + } + @Override public void exitInstruction(rs2asmParser.InstructionContext ctx) { @@ -175,6 +207,10 @@ public class ScriptWriter extends rs2asmBaseListener public ScriptDefinition buildScript() { ScriptDefinition script = new ScriptDefinition(); + script.setIntStackCount(intStackCount); + script.setStringStackCount(stringStackCount); + script.setLocalIntCount(localIntCount); + script.setLocalStringCount(localStringCount); script.setInstructions(opcodes.stream().mapToInt(Integer::valueOf).toArray()); script.setIntOperands(iops.stream() .map(i -> i == null ? 0 : i) diff --git a/cache/src/main/java/net/runelite/cache/script/disassembler/Disassembler.java b/cache/src/main/java/net/runelite/cache/script/disassembler/Disassembler.java index b3f4432988..45a46bb1e4 100644 --- a/cache/src/main/java/net/runelite/cache/script/disassembler/Disassembler.java +++ b/cache/src/main/java/net/runelite/cache/script/disassembler/Disassembler.java @@ -101,8 +101,6 @@ public class Disassembler public String disassemble(ScriptDefinition script) throws IOException { - StringBuilder writer = new StringBuilder(); - int[] instructions = script.getInstructions(); int[] iops = script.getIntOperands(); String[] sops = script.getStringOperands(); @@ -113,6 +111,9 @@ public class Disassembler boolean[] jumps = needLabel(script); + StringBuilder writer = new StringBuilder(); + writerHeader(writer, script); + for (int i = 0; i < instructions.length; ++i) { int opcode = instructions[i]; @@ -188,7 +189,33 @@ public class Disassembler return false; } - // Write if operand != 0 or if the instruction is specifically to load int - return operand != 0 || opcode == Opcodes.LOAD_INT; + if (operand != 0) + { + // always write non-zero operand + return true; + } + + switch (opcode) + { + case Opcodes.LOAD_INT: + case Opcodes.ILOAD: + return true; + } + + // int operand is not used, don't write it + return false; + } + + private void writerHeader(StringBuilder writer, ScriptDefinition script) + { + int intStackCount = script.getIntStackCount(); + int stringStackCount = script.getStringStackCount(); + int localIntCount = script.getLocalIntCount(); + int localStringCount = script.getLocalStringCount(); + + writer.append(".int_stack_count ").append(intStackCount).append('\n'); + writer.append(".string_stack_count ").append(stringStackCount).append('\n'); + writer.append(".int_var_count ").append(localIntCount).append('\n'); + writer.append(".string_var_count ").append(localStringCount).append('\n'); } } diff --git a/cache/src/test/java/net/runelite/cache/script/assembler/AssemblerTest.java b/cache/src/test/java/net/runelite/cache/script/assembler/AssemblerTest.java index 7bcb9fa9fa..eb0005dcca 100644 --- a/cache/src/test/java/net/runelite/cache/script/assembler/AssemblerTest.java +++ b/cache/src/test/java/net/runelite/cache/script/assembler/AssemblerTest.java @@ -50,7 +50,7 @@ public class AssemblerTest { return new String[] { - "395.rs2asm", "91.rs2asm" + "91.rs2asm" }; } diff --git a/cache/src/test/resources/net/runelite/cache/script/assembler/395.rs2asm b/cache/src/test/resources/net/runelite/cache/script/assembler/395.rs2asm deleted file mode 100644 index c6f8b3b88a..0000000000 --- a/cache/src/test/resources/net/runelite/cache/script/assembler/395.rs2asm +++ /dev/null @@ -1,99 +0,0 @@ - 033 - get_boostedskilllevels - int_to_string - 1112 - 033 - get_realskilllevels - 034 3 - 033 3 - int_to_string - 1112 1 - 033 - get_skillexperiences - 034 4 - load_string "," - 036 1 - 035 - load_string " XP:" - 037 2 - 036 2 - 033 4 - 035 1 - 040 46 - 036 3 - load_int 0 - 034 5 - 025 4181 - load_int 0 - if_icmpeq LABEL29 - jump LABEL60 -LABEL29: - 033 3 - load_int 99 - if_icmplt LABEL33 - jump LABEL59 -LABEL33: - load_int 105 - load_int 105 - load_int 256 - 033 3 - load_int 1 - iadd - 3408 - 034 5 - 035 2 - load_string "|Next level at:|Remaining XP:" - concat_string - 036 2 - 035 3 - load_string "|" - 033 5 - 035 1 - 040 46 - load_string "|" - 033 5 - 033 4 - isub - 035 1 - 040 46 - 037 4 - concat_string - 036 3 -LABEL59: - jump LABEL78 -LABEL60: - 035 2 - load_string "|Next level at:" - concat_string - 036 2 - 035 3 - load_string "|" - load_int 105 - load_int 105 - load_int 256 - 033 3 - load_int 1 - iadd - 3408 - 035 1 - 040 46 - 037 2 - concat_string - 036 3 -LABEL78: - load_int 992 - load_int -2147483645 - load_int -1 - 033 2 - 035 2 - 035 3 - load_int 495 - load_int 25 - load_int 5 - idiv - load_string "IiIssfi" - 033 1 - 2412 - load_int 0 - 043 2 - return diff --git a/cache/src/test/resources/net/runelite/cache/script/assembler/91.rs2asm b/cache/src/test/resources/net/runelite/cache/script/assembler/91.rs2asm index c4d12dc77f..0f88db5a12 100644 --- a/cache/src/test/resources/net/runelite/cache/script/assembler/91.rs2asm +++ b/cache/src/test/resources/net/runelite/cache/script/assembler/91.rs2asm @@ -1,4 +1,8 @@ - 033 +.int_stack_count 2 +.string_stack_count 1 +.int_var_count 2 +.string_var_count 1 + iload 0 switch 3: LABEL20 5: LABEL54 @@ -6,7 +10,7 @@ 7: LABEL3 jump LABEL84 LABEL3: - 033 1 + iload 1 042 175 if_icmplt LABEL7 jump LABEL9 @@ -14,7 +18,7 @@ LABEL7: load_int 0 return LABEL9: - 035 + sload string_remove_html 3623 load_int 1 @@ -28,7 +32,7 @@ LABEL17: return jump LABEL84 LABEL20: - 033 1 + iload 1 042 175 if_icmplt LABEL24 jump LABEL26 @@ -36,7 +40,7 @@ LABEL24: load_int 0 return LABEL26: - 035 + sload string_remove_html 3623 load_int 1 @@ -59,7 +63,7 @@ LABEL40: if_icmpeq LABEL44 jump LABEL51 LABEL44: - 035 + sload 3609 load_int 1 if_icmpeq LABEL49 @@ -72,7 +76,7 @@ LABEL51: return jump LABEL84 LABEL54: - 033 1 + iload 1 042 175 if_icmplt LABEL58 jump LABEL60 @@ -80,7 +84,7 @@ LABEL58: load_int 0 return LABEL60: - 033 + iload 0 load_int 5 if_icmpeq LABEL64 jump LABEL76 @@ -91,7 +95,7 @@ LABEL64: jump LABEL76 LABEL68: get_gamecycle - 033 1 + iload 1 isub load_int 500 if_icmpge LABEL74