cache: add stack and lvt string/int counts to rs2asm
This commit is contained in:
@@ -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 ;
|
||||
|
||||
@@ -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<Integer, Integer>[] switches;
|
||||
|
||||
public int getId()
|
||||
|
||||
@@ -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<Integer> opcodes = new ArrayList<>();
|
||||
private List<Integer> iops = new ArrayList<>();
|
||||
private List<String> 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)
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user