Skip to content

Commit

Permalink
Additional constant replacement for #6 in CF instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Apr 23, 2023
1 parent d75f0b9 commit bf23ec9
Show file tree
Hide file tree
Showing 134 changed files with 2,615 additions and 2,292 deletions.
206 changes: 141 additions & 65 deletions src/main/java/com/android/tools/r8/cf/code/CfArithmeticBinop.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,36 @@
import com.android.tools.r8.optimize.interfaces.analysis.CfFrameState;
import com.android.tools.r8.utils.structural.CompareToVisitor;
import com.android.tools.r8.utils.structural.HashingVisitor;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nonnull;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import java.util.Map;

public class CfArithmeticBinop extends CfInstruction {
public static final CfArithmeticBinop IADD = new CfArithmeticBinop(Opcode.Add, NumericType.INT);
public static final CfArithmeticBinop ISUB = new CfArithmeticBinop(Opcode.Sub, NumericType.INT);
public static final CfArithmeticBinop IMUL = new CfArithmeticBinop(Opcode.Mul, NumericType.INT);
public static final CfArithmeticBinop IDIV = new CfArithmeticBinop(Opcode.Div, NumericType.INT);
public static final CfArithmeticBinop IREM = new CfArithmeticBinop(Opcode.Rem, NumericType.INT);

public static final CfArithmeticBinop LADD = new CfArithmeticBinop(Opcode.Add, NumericType.LONG);
public static final CfArithmeticBinop LSUB = new CfArithmeticBinop(Opcode.Sub, NumericType.LONG);
public static final CfArithmeticBinop LMUL = new CfArithmeticBinop(Opcode.Mul, NumericType.LONG);
public static final CfArithmeticBinop LDIV = new CfArithmeticBinop(Opcode.Div, NumericType.LONG);
public static final CfArithmeticBinop LREM = new CfArithmeticBinop(Opcode.Rem, NumericType.LONG);

public static final CfArithmeticBinop FADD = new CfArithmeticBinop(Opcode.Add, NumericType.FLOAT);
public static final CfArithmeticBinop FSUB = new CfArithmeticBinop(Opcode.Sub, NumericType.FLOAT);
public static final CfArithmeticBinop FMUL = new CfArithmeticBinop(Opcode.Mul, NumericType.FLOAT);
public static final CfArithmeticBinop FDIV = new CfArithmeticBinop(Opcode.Div, NumericType.FLOAT);
public static final CfArithmeticBinop FREM = new CfArithmeticBinop(Opcode.Rem, NumericType.FLOAT);

public static final CfArithmeticBinop DADD = new CfArithmeticBinop(Opcode.Add, NumericType.DOUBLE);
public static final CfArithmeticBinop DSUB = new CfArithmeticBinop(Opcode.Sub, NumericType.DOUBLE);
public static final CfArithmeticBinop DMUL = new CfArithmeticBinop(Opcode.Mul, NumericType.DOUBLE);
public static final CfArithmeticBinop DDIV = new CfArithmeticBinop(Opcode.Div, NumericType.DOUBLE);
public static final CfArithmeticBinop DREM = new CfArithmeticBinop(Opcode.Rem, NumericType.DOUBLE);

public enum Opcode {
Add,
Expand All @@ -51,6 +74,89 @@ public CfArithmeticBinop(Opcode opcode, NumericType type) {
this.type = type;
}

@Nonnull
public static CfArithmeticBinop operation(Opcode opcode, NumericType type) {
switch (opcode) {
case Add:
switch (type) {
case BYTE:
case CHAR:
case SHORT:
case INT:
return IADD;
case LONG:
return LADD;
case FLOAT:
return FADD;
case DOUBLE:
return DADD;
}
break;
case Sub:
switch (type) {
case BYTE:
case CHAR:
case SHORT:
case INT:
return ISUB;
case LONG:
return LSUB;
case FLOAT:
return FSUB;
case DOUBLE:
return DSUB;
}
break;
case Mul:
switch (type) {
case BYTE:
case CHAR:
case SHORT:
case INT:
return IMUL;
case LONG:
return LMUL;
case FLOAT:
return FMUL;
case DOUBLE:
return DMUL;
}
break;
case Div:
switch (type) {
case BYTE:
case CHAR:
case SHORT:
case INT:
return IDIV;
case LONG:
return LDIV;
case FLOAT:
return FDIV;
case DOUBLE:
return DDIV;
}
break;
case Rem:
switch (type) {
case BYTE:
case CHAR:
case SHORT:
case INT:
return IREM;
case LONG:
return LREM;
case FLOAT:
return FREM;
case DOUBLE:
return DREM;
}
break;
}
throw new Unreachable("Unsupported operation: " + opcode.name() + " - " + type.name());
}


@Override
public int getCompareToId() {
return getAsmOpcode();
Expand All @@ -75,50 +181,30 @@ public NumericType getType() {
return type;
}

@Nonnull
public static CfArithmeticBinop fromAsm(int opcode) {
switch (opcode) {
case Opcodes.IADD:
return new CfArithmeticBinop(Opcode.Add, NumericType.INT);
case Opcodes.LADD:
return new CfArithmeticBinop(Opcode.Add, NumericType.LONG);
case Opcodes.FADD:
return new CfArithmeticBinop(Opcode.Add, NumericType.FLOAT);
case Opcodes.DADD:
return new CfArithmeticBinop(Opcode.Add, NumericType.DOUBLE);
case Opcodes.ISUB:
return new CfArithmeticBinop(Opcode.Sub, NumericType.INT);
case Opcodes.LSUB:
return new CfArithmeticBinop(Opcode.Sub, NumericType.LONG);
case Opcodes.FSUB:
return new CfArithmeticBinop(Opcode.Sub, NumericType.FLOAT);
case Opcodes.DSUB:
return new CfArithmeticBinop(Opcode.Sub, NumericType.DOUBLE);
case Opcodes.IMUL:
return new CfArithmeticBinop(Opcode.Mul, NumericType.INT);
case Opcodes.LMUL:
return new CfArithmeticBinop(Opcode.Mul, NumericType.LONG);
case Opcodes.FMUL:
return new CfArithmeticBinop(Opcode.Mul, NumericType.FLOAT);
case Opcodes.DMUL:
return new CfArithmeticBinop(Opcode.Mul, NumericType.DOUBLE);
case Opcodes.IDIV:
return new CfArithmeticBinop(Opcode.Div, NumericType.INT);
case Opcodes.LDIV:
return new CfArithmeticBinop(Opcode.Div, NumericType.LONG);
case Opcodes.FDIV:
return new CfArithmeticBinop(Opcode.Div, NumericType.FLOAT);
case Opcodes.DDIV:
return new CfArithmeticBinop(Opcode.Div, NumericType.DOUBLE);
case Opcodes.IREM:
return new CfArithmeticBinop(Opcode.Rem, NumericType.INT);
case Opcodes.LREM:
return new CfArithmeticBinop(Opcode.Rem, NumericType.LONG);
case Opcodes.FREM:
return new CfArithmeticBinop(Opcode.Rem, NumericType.FLOAT);
case Opcodes.DREM:
return new CfArithmeticBinop(Opcode.Rem, NumericType.DOUBLE);
default:
throw new Unreachable("Wrong ASM opcode for CfArithmeticBinop " + opcode);
case Opcodes.IADD: return IADD;
case Opcodes.LADD: return LADD;
case Opcodes.FADD: return FADD;
case Opcodes.DADD: return DADD;
case Opcodes.ISUB: return ISUB;
case Opcodes.LSUB: return LSUB;
case Opcodes.FSUB: return FSUB;
case Opcodes.DSUB: return DSUB;
case Opcodes.IMUL: return IMUL;
case Opcodes.LMUL: return LMUL;
case Opcodes.FMUL: return FMUL;
case Opcodes.DMUL: return DMUL;
case Opcodes.IDIV: return IDIV;
case Opcodes.LDIV: return LDIV;
case Opcodes.FDIV: return FDIV;
case Opcodes.DDIV: return DDIV;
case Opcodes.IREM: return IREM;
case Opcodes.LREM: return LREM;
case Opcodes.FREM: return FREM;
case Opcodes.DREM: return DREM;
default: throw new Unreachable("Wrong ASM opcode for CfArithmeticBinop " + opcode);
}
}

Expand All @@ -129,31 +215,21 @@ public int getAsmOpcode() {
public static int getAsmOpcode(Opcode opcode, NumericType type) {
int typeOffset = getAsmOpcodeTypeOffset(type);
switch (opcode) {
case Add:
return Opcodes.IADD + typeOffset;
case Sub:
return Opcodes.ISUB + typeOffset;
case Mul:
return Opcodes.IMUL + typeOffset;
case Div:
return Opcodes.IDIV + typeOffset;
case Rem:
return Opcodes.IREM + typeOffset;
default:
throw new Unreachable("CfArithmeticBinop has unknown opcode " + opcode);
case Add: return Opcodes.IADD + typeOffset;
case Sub: return Opcodes.ISUB + typeOffset;
case Mul: return Opcodes.IMUL + typeOffset;
case Div: return Opcodes.IDIV + typeOffset;
case Rem: return Opcodes.IREM + typeOffset;
default: throw new Unreachable("CfArithmeticBinop has unknown opcode " + opcode);
}
}

private static int getAsmOpcodeTypeOffset(NumericType type) {
switch (type) {
case LONG:
return Opcodes.LADD - Opcodes.IADD;
case FLOAT:
return Opcodes.FADD - Opcodes.IADD;
case DOUBLE:
return Opcodes.DADD - Opcodes.IADD;
default:
return 0;
case LONG: return Opcodes.LADD - Opcodes.IADD;
case FLOAT: return Opcodes.FADD - Opcodes.IADD;
case DOUBLE: return Opcodes.DADD - Opcodes.IADD;
default: return 0;
}
}

Expand All @@ -162,9 +238,9 @@ public void print(CfPrinter printer) {
printer.print(this);
}

@NotNull
@Nonnull
@Override
public CfInstruction copy(@NotNull Map<CfLabel, CfLabel> labelMap) {
public CfInstruction copy(@Nonnull Map<CfLabel, CfLabel> labelMap) {
return this;
}

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/android/tools/r8/cf/code/CfArrayLength.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.android.tools.r8.optimize.interfaces.analysis.CfFrameState;
import com.android.tools.r8.utils.structural.CompareToVisitor;
import com.android.tools.r8.utils.structural.HashingVisitor;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nonnull;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

Expand All @@ -33,6 +33,8 @@ public class CfArrayLength extends CfInstruction {

public static final CfArrayLength INSTANCE = new CfArrayLength();

private CfArrayLength() {}

@Override
public void write(
AppView<?> appView,
Expand Down Expand Up @@ -72,9 +74,9 @@ public void print(CfPrinter printer) {
printer.print(this);
}

@NotNull
@Nonnull
@Override
public CfInstruction copy(@NotNull Map<CfLabel, CfLabel> labelMap) {
public CfInstruction copy(@Nonnull Map<CfLabel, CfLabel> labelMap) {
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.android.tools.r8.ir.code.MemberType;
import com.android.tools.r8.utils.structural.CompareToVisitor;
import com.android.tools.r8.utils.structural.HashingVisitor;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nonnull;

import java.util.Map;

Expand All @@ -34,9 +34,9 @@ public boolean canThrow() {
return true;
}

@NotNull
@Nonnull
@Override
public CfInstruction copy(@NotNull Map<CfLabel, CfLabel> labelMap) {
public CfInstruction copy(@Nonnull Map<CfLabel, CfLabel> labelMap) {
return this;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/android/tools/r8/cf/code/CfCheckCast.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.ListIterator;
import java.util.Map;

import org.jetbrains.annotations.NotNull;
import javax.annotation.Nonnull;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

Expand Down Expand Up @@ -111,9 +111,9 @@ public void print(CfPrinter printer) {
printer.print(this);
}

@NotNull
@Nonnull
@Override
public CfInstruction copy(@NotNull Map<CfLabel, CfLabel> labelMap) {
public CfInstruction copy(@Nonnull Map<CfLabel, CfLabel> labelMap) {
return this;
}

Expand Down
Loading

0 comments on commit bf23ec9

Please sign in to comment.