use of net.runelite.asm.attributes.code.instructions.LDC in project runelite by runelite.
the class ExprArgOrderTest method test5.
@Test
public void test5() {
ClassGroup group = ClassGroupFactory.generateGroup();
Code code = group.findClass("test").findMethod("func").getCode();
Instructions ins = code.getInstructions();
code.setMaxStack(2);
// vars[0] = 3
Instruction[] prepareVariables = { new LDC(ins, 3), new IStore(ins, 0) };
for (Instruction i : prepareVariables) {
ins.addInstruction(i);
}
Label label = new Label(ins);
Instruction[] body = { // if (0 == 3 + var0) -> if (var0 + 3 == 0)
new LDC(ins, 0), new LDC(ins, 3), new ILoad(ins, 0), new IAdd(ins), new IfICmpEq(ins, label), label, new VReturn(ins) };
for (Instruction i : body) {
ins.addInstruction(i);
}
ExprArgOrder exprArgOrder = new ExprArgOrder();
exprArgOrder.run(group);
List<Instruction> instructions = ins.getInstructions();
// ldc iload add -> iload ldc iadd
assertEquals(ILOAD, instructions.get(2).getType());
assertEquals(LDC, instructions.get(3).getType());
assertEquals(IADD, instructions.get(4).getType());
// idc moves from 2 to 5
assertEquals(LDC, instructions.get(5).getType());
assertEquals(IF_ICMPEQ, instructions.get(6).getType());
}
use of net.runelite.asm.attributes.code.instructions.LDC in project runelite by runelite.
the class ExprArgOrderTest method test6.
@Test
public void test6() {
ClassGroup group = ClassGroupFactory.generateGroup();
Code code = group.findClass("test").findMethod("func").getCode();
Instructions ins = code.getInstructions();
code.setMaxStack(2);
// vars[0] = 3
Instruction[] prepareVariables = { new LDC(ins, 3), new IStore(ins, 0) };
for (Instruction i : prepareVariables) {
ins.addInstruction(i);
}
Label label = new Label(ins);
/*
iconst_0
ldc 8388608
iload_3
iadd
ldc -16777216
iand
if_icmpeq LABEL0x49
*/
Instruction[] body = { new LDC(ins, 0), new LDC(ins, 8388608), new ILoad(ins, 0), new IAdd(ins), new LDC(ins, -16777216), new IAnd(ins), // 8
new IfICmpEq(ins, label), label, new VReturn(ins) };
for (Instruction i : body) {
ins.addInstruction(i);
}
ExprArgOrder exprArgOrder = new ExprArgOrder();
exprArgOrder.run(group);
List<Instruction> instructions = ins.getInstructions();
assertEquals(ILOAD, instructions.get(2).getType());
assertEquals(LDC, instructions.get(3).getType());
assertEquals(IADD, instructions.get(4).getType());
assertEquals(LDC, instructions.get(5).getType());
assertEquals(IAND, instructions.get(6).getType());
assertEquals(LDC, instructions.get(7).getType());
assertEquals(IF_ICMPEQ, instructions.get(8).getType());
}
use of net.runelite.asm.attributes.code.instructions.LDC in project runelite by runelite.
the class ExprArgOrderTest method test.
@Test
public void test() {
ClassGroup group = ClassGroupFactory.generateGroup();
Code code = group.findClass("test").findMethod("func").getCode();
Instructions ins = code.getInstructions();
code.setMaxStack(2);
// vars[0] = 3
Instruction[] prepareVariables = { new LDC(ins, 3), new IStore(ins, 0) };
for (Instruction i : prepareVariables) {
ins.addInstruction(i);
}
Instruction[] body = { // 2
new LDC(ins, 3), new ILoad(ins, 0), new IAdd(ins), new Pop(ins), new VReturn(ins) };
for (Instruction i : body) {
ins.addInstruction(i);
}
ExprArgOrder exprArgOrder = new ExprArgOrder();
exprArgOrder.run(group);
List<Instruction> instructions = ins.getInstructions();
assertEquals(ILOAD, instructions.get(2).getType());
assertEquals(LDC, instructions.get(3).getType());
assertEquals(IADD, instructions.get(4).getType());
}
use of net.runelite.asm.attributes.code.instructions.LDC in project runelite by runelite.
the class LvtTest method testReuseIndex.
@Test
public void testReuseIndex() {
ClassGroup group = ClassGroupFactory.generateGroup();
Code code = group.findClass("test").findMethod("func").getCode();
Instructions ins = code.getInstructions();
Instruction[] body = { // var0 = null
new AConstNull(ins), new AStore(ins, 0), // this forces a reindex to varn
new LDC(ins, 0), new IStore(ins, 0), // var2 = null
new AConstNull(ins), new AStore(ins, 2), // this forces a reindex to varn+1
new LDC(ins, 0), new IStore(ins, 2), // var0 = 0L
new LDC(ins, 0L), new LStore(ins, 0), new VReturn(ins) };
for (Instruction i : body) {
ins.addInstruction(i);
}
Lvt lvt = new Lvt();
lvt.run(group);
AStore astore1 = (AStore) body[1];
IStore istore1 = (IStore) body[3];
AStore astore2 = (AStore) body[5];
IStore istore2 = (IStore) body[7];
LStore lstore1 = (LStore) body[9];
int astore1Idx = astore1.getVariableIndex();
int istore1Idx = istore1.getVariableIndex();
int astore2Idx = astore2.getVariableIndex();
int istore2Idx = istore2.getVariableIndex();
int lstore1Idx = lstore1.getVariableIndex();
logger.debug("{} -> {}", astore1, astore1.getVariableIndex());
logger.debug("{} -> {}", istore1, istore1.getVariableIndex());
logger.debug("{} -> {}", astore2, astore2.getVariableIndex());
logger.debug("{} -> {}", istore2, istore2.getVariableIndex());
logger.debug("{} -> {}", lstore1, lstore1.getVariableIndex());
Assert.assertNotEquals(astore1Idx, istore1Idx);
Assert.assertNotEquals(astore2Idx, istore2Idx);
// assert that the lstore doesn't overwrite an existing index
Assert.assertNotEquals(lstore1Idx + 1, astore1Idx);
Assert.assertNotEquals(lstore1Idx + 1, istore1Idx);
Assert.assertNotEquals(lstore1Idx + 1, astore2Idx);
Assert.assertNotEquals(lstore1Idx + 1, istore2Idx);
}
use of net.runelite.asm.attributes.code.instructions.LDC in project runelite by runelite.
the class ModArith method findUses.
// find potential getters/setters for each field
private void findUses(MethodContext mctx) {
for (InstructionContext ctx : mctx.getInstructionContexts()) {
if (ctx.getInstruction() instanceof IMul || ctx.getInstruction() instanceof LMul) {
Instruction one = ctx.getPops().get(0).getPushed().getInstruction();
Instruction two = ctx.getPops().get(1).getPushed().getInstruction();
PushConstantInstruction pc = null;
GetFieldInstruction gf = null;
if (one instanceof PushConstantInstruction && two instanceof GetFieldInstruction) {
pc = (PushConstantInstruction) one;
gf = (GetFieldInstruction) two;
} else if (two instanceof PushConstantInstruction && one instanceof GetFieldInstruction) {
pc = (PushConstantInstruction) two;
gf = (GetFieldInstruction) one;
}
if (pc == null) {
continue;
}
Field field = gf.getMyField();
if (field == null) {
continue;
}
FieldInfo fieldInfo = getFieldInfo(field);
// parse the full multiplication expression to
// get all associated constants
List<InstructionContext> insInExpr = getInsInExpr(ctx, new HashSet(), true);
for (InstructionContext ctx2 : insInExpr) {
if (!(ctx2.getInstruction() instanceof PushConstantInstruction)) {
continue;
}
PushConstantInstruction pci3 = (PushConstantInstruction) ctx2.getInstruction();
Number value = (Number) pci3.getConstant();
// field * constant
if (value instanceof Integer || value instanceof Long) {
fieldInfo.getters.add(value);
}
}
} else if (ctx.getInstruction() instanceof SetFieldInstruction) {
SetFieldInstruction sf = (SetFieldInstruction) ctx.getInstruction();
Field field = sf.getMyField();
if (field == null) {
continue;
}
FieldInfo fieldInfo = getFieldInfo(field);
// value being set
InstructionContext pushedsfi = ctx.getPops().get(0).getPushed();
pushedsfi = pushedsfi.resolve(ctx.getPops().get(0));
if (!(pushedsfi.getInstruction() instanceof IMul) && !(pushedsfi.getInstruction() instanceof LMul) && !(pushedsfi.getInstruction() instanceof IAdd) && !(pushedsfi.getInstruction() instanceof LAdd) && !(pushedsfi.getInstruction() instanceof ISub) && !(pushedsfi.getInstruction() instanceof LSub)) {
if (pushedsfi.getInstruction() instanceof LDC) {
PushConstantInstruction ldc = (PushConstantInstruction) pushedsfi.getInstruction();
if (ldc.getConstant() instanceof Integer || ldc.getConstant() instanceof Long) {
Number i = (Number) ldc.getConstant();
// field = constant
fieldInfo.setters.add(i);
}
}
continue;
}
Instruction one = pushedsfi.getPops().get(0).getPushed().getInstruction();
Instruction two = pushedsfi.getPops().get(1).getPushed().getInstruction();
// field = field + imul
if (pushedsfi.getInstruction() instanceof IAdd) {
if (one instanceof IMul && two instanceof GetFieldInstruction) {
one = pushedsfi.getPops().get(0).getPushed().getPops().get(0).getPushed().getInstruction();
two = pushedsfi.getPops().get(0).getPushed().getPops().get(1).getPushed().getInstruction();
}
}
// if both one and two are constants then one of them must not be a setter
PushConstantInstruction pc = null;
if (one instanceof PushConstantInstruction && !(two instanceof PushConstantInstruction)) {
pc = (PushConstantInstruction) one;
} else if (two instanceof PushConstantInstruction && !(one instanceof PushConstantInstruction)) {
pc = (PushConstantInstruction) two;
}
if (pc == null) {
continue;
}
Number value2 = (Number) pc.getConstant();
// field = something * constant
if (value2 instanceof Integer || value2 instanceof Long) {
fieldInfo.setters.add(value2);
}
}
}
}
Aggregations